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 + '&qformat=sms&pagesize=10&pagestart=0';
116
  reloadSearchContent(url);
117
}
118

    
119
function showDatasets()
120
{
121
  var page = getCookie('sms-pagestart');
122
  if(page)
123
  {
124
    reloadSearchContent('/sms/metacat?action=query&anyfield=%25&qformat=sms&pagesize=10&pagestart=' + page);
125
  }
126
  else
127
  {
128
    reloadSearchContent('/sms/metacat?action=query&anyfield=%25&qformat=sms&pagesize=10&pagestart=0');
129
  }
130
}
131

    
132
function reloadSearchContent(url)
133
{
134
  var page = url.substring(url.indexOf('pagestart=') + 10, url.length);
135
  setCookie("sms-pagestart", page);
136
  $("#searchresulttable").load(url);
137
}
138

    
139
function uploadfile()
140
{
141
  if(getCookie("sms-login") != "true")
142
  {
143
    alert('You cannot upload.  You are not logged in.');
144
    return;
145
  }
146
  $("form").submit();
147
}
148

    
149
function slideUp(id)
150
{
151
  $(id).slideUp("slow");
152
}
153

    
154
function slideDown(id)
155
{
156
  $(id).slideDown("slow");
157
}
158

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

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