Project

General

Profile

1
function login()
2
{
3
  var user = document.getElementById("un").value;
4
  var org = document.getElementById("org").value;
5
  var pass = document.getElementById("pw").value;
6
  var ldapUsername = 'uid=' + user + ',o=' + org + ',dc=ecoinformatics,dc=org';
7
  
8
  $.get("metacat", {username: ldapUsername, password: pass, action:"login", qformat:"xml"}, 
9
    function(data) {
10
      //alert('user ' + ldapUsername + ' logged in.  data:' + data);
11
      if(data.indexOf('<sessionId>') != -1)
12
      { //login successful
13
        //alert('user logged in');
14
        slideUp("#loginformdiv");
15
        setCookie("sms-login", true);
16
        setCookie("sms-user", user);
17
        setCookie("sms-org", org);
18
        setLoginHeader(true);
19
        checkLogin();
20
      }
21
      else
22
      { //login not successful
23
        alert('Sorry, your login failed.  Please try again.  If you need a username, please go to http://knb.ecoinformatics.org.');
24
        setCookie("sms-login", false);
25
      }
26
      
27
    }, "XML");
28
}
29

    
30
function updateStatus()
31
{
32
  var url = window.location.href;
33
  if(url.indexOf('docid') != -1)
34
  { //if there's a docid in the url, set the cookie
35
    var docid = url.substring(url.indexOf("docid=") + 6, url.indexOf("&status"));
36
    var docidcookie = getCookie("sms-lastdocid");
37
    if(docid != docidcookie)
38
    { //set the cookie for next time
39
      setCookie("sms-lastdocid", docid);
40
      //slideDown("#uploadstatus");
41
      $('#uploadstatus').css("display", "block");
42
    }
43
    else
44
    { //hide the status
45
      $('#uploadstatus').css("display", "none");
46
      //slideUp("#uploadstatus");
47
    }
48
  }
49
  
50
}
51

    
52
function setLoginHeader(loggedin)
53
{
54
  if(loggedin)
55
  {
56
    updateStatus();
57
    var user = getCookie("sms-user");
58
    $('#loginheader').replaceWith("<h2 style=\"text-align:center\" id=\"loginheader\">" 
59
      + user + " Logged In <a href=\"javascript:logout()\" style=\"font-size:70%\">[logout]</a></h2>");
60
    slideUp("#loginformdiv");
61
    $('#maindiv').css("display", "block");
62
    $('#bottomimg').css("bottom", "0px");
63
    $.get("metacat", {action:"getlastdocid", scope:"sms"}, 
64
      function(data)
65
      {
66
        var docid = data.substring(data.indexOf("<docid>") + 7, data.indexOf("</docid>"));
67
        var nextid;
68
        if(docid == 'null')
69
        {
70
          nextid = "sms.1.1";
71
        }
72
        else
73
        {
74
          nextid = docid.substring(docid.indexOf(".") + 1, docid.lastIndexOf("."));
75
          nextid++;
76
          nextid = "sms." + nextid + ".1"; 
77
        }
78
        $('#docidtextfield').val(nextid); 
79
      }, 
80
      "XML");
81
  }
82
  else
83
  {
84
    $('#loginheader').replaceWith("<h2 style=\"text-align:center\" id=\"loginheader\">" 
85
      + "Please Log In</h2>");
86
    slideDown("#loginformdiv");
87
    $('#maindiv').css("display", "none");
88
    $('#bottomimg').css("bottom", "15px");
89
  }
90
}
91

    
92
function logout()
93
{
94
  $.get("metacat", {action:"logout", qformat:"xml"});
95
  setLoginHeader(false);
96
  setCookie("sms-login", false);
97
}
98

    
99
function checkLogin()
100
{
101
  if(getCookie("sms-login") == "true")
102
  {
103
    setLoginHeader(true);
104
    showDatasets();
105
  }
106
  else
107
  {
108
    setLoginHeader(false);
109
  }
110
}
111

    
112
function search()
113
{
114
  var searchval = document.getElementById("searchtextbox").value
115
  var url = '/sms/metacat?action=query&anyfield=' + searchval + '&returnfield=dataset/title&qformat=sms&pagesize=10&pagestart=0';
116
  setCookie("sms-searchval", searchval);
117
  reloadSearchContent(url);
118
}
119

    
120
function showDatasets()
121
{
122
  var searchval = getCookie('sms-searchval');
123
  alert('searchval: ' + searchval);
124
  if(searchval == '')
125
  {
126
    searchval = '%25';
127
  }
128
  var page = getCookie('sms-pagestart');
129
  if(page)
130
  {
131
    reloadSearchContent('/sms/metacat?action=query&anyfield=' + searchval + '&returnfield=dataset/title&qformat=sms&pagesize=10&pagestart=' + page);
132
  }
133
  else
134
  {
135
    reloadSearchContent('/sms/metacat?action=query&anyfield=' + searchval + '&returnfield=dataset/title&qformat=sms&pagesize=10&pagestart=0');
136
  }
137
}
138

    
139
function reloadSearchContent(url)
140
{
141
  var page = url.substring(url.indexOf('pagestart=') + 10, url.length);
142
  setCookie("sms-pagestart", page);
143
  $("#searchresulttable").load(url);
144
}
145

    
146
function uploadfile()
147
{
148
  if(getCookie("sms-login") != "true")
149
  {
150
    alert('You cannot upload.  You are not logged in.');
151
    return;
152
  }
153
  $("form").submit();
154
}
155

    
156
function slideUp(id)
157
{
158
  $(id).slideUp("slow");
159
}
160

    
161
function slideDown(id)
162
{
163
  $(id).slideDown("slow");
164
}
165

    
166
function setCookie( name, value, expires, path, domain, secure ) 
167
{
168
  // set time, it's in milliseconds
169
  var today = new Date();
170
  today.setTime( today.getTime() );
171
  
172
  /*
173
  if the expires variable is set, make the correct 
174
  expires time, the current script below will set 
175
  it for x number of days, to make it for hours, 
176
  delete * 24, for minutes, delete * 60 * 24
177
  */
178
  if ( expires )
179
  {
180
    expires = expires * 1000 * 60 * 60 * 24;
181
  }
182
  var expires_date = new Date( today.getTime() + (expires) );
183
  
184
  document.cookie = name + "=" +escape( value ) +
185
  ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
186
  ( ( path ) ? ";path=" + path : "" ) + 
187
  ( ( domain ) ? ";domain=" + domain : "" ) +
188
  ( ( secure ) ? ";secure" : "" );
189
}
190

    
191
function getCookie( check_name ) {
192
	// first we'll split this cookie up into name/value pairs
193
	// note: document.cookie only returns name=value, not the other components
194
	var a_all_cookies = document.cookie.split( ';' );
195
	var a_temp_cookie = '';
196
	var cookie_name = '';
197
	var cookie_value = '';
198
	var b_cookie_found = false; // set boolean t/f default f
199
	
200
	for ( i = 0; i < a_all_cookies.length; i++ )
201
	{
202
		// now we'll split apart each name=value pair
203
		a_temp_cookie = a_all_cookies[i].split( '=' );
204
		
205
		// and trim left/right whitespace while we're at it
206
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
207
	
208
		// if the extracted name matches passed check_name
209
		if ( cookie_name == check_name )
210
		{
211
			b_cookie_found = true;
212
			// we need to handle case where cookie has no value but exists (no = sign, that is):
213
			if ( a_temp_cookie.length > 1 )
214
			{
215
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
216
			}
217
			// note that in cases where cookie is initialized but no value, null is returned
218
			return cookie_value;
219
			break;
220
		}
221
		a_temp_cookie = null;
222
		cookie_name = '';
223
	}
224
	if ( !b_cookie_found )
225
	{
226
		return null;
227
	}
228
}		
(14-14/15)