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
  reloadSearchContent('/sms/metacat?action=query&anyfield=%25&qformat=sms&pagesize=10&pagestart=0');
122
}
123

    
124
function reloadSearchContent(url)
125
{
126
  $("#searchresulttable").load(url);
127
}
128

    
129
function uploadfile()
130
{
131
  if(getCookie("sms-login") != "true")
132
  {
133
    alert('You cannot upload.  You are not logged in.');
134
    return;
135
  }
136
  $("form").submit();
137
}
138

    
139
function slideUp(id)
140
{
141
  $(id).slideUp("slow");
142
}
143

    
144
function slideDown(id)
145
{
146
  $(id).slideDown("slow");
147
}
148

    
149
function setCookie( name, value, expires, path, domain, secure ) 
150
{
151
  // set time, it's in milliseconds
152
  var today = new Date();
153
  today.setTime( today.getTime() );
154
  
155
  /*
156
  if the expires variable is set, make the correct 
157
  expires time, the current script below will set 
158
  it for x number of days, to make it for hours, 
159
  delete * 24, for minutes, delete * 60 * 24
160
  */
161
  if ( expires )
162
  {
163
    expires = expires * 1000 * 60 * 60 * 24;
164
  }
165
  var expires_date = new Date( today.getTime() + (expires) );
166
  
167
  document.cookie = name + "=" +escape( value ) +
168
  ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
169
  ( ( path ) ? ";path=" + path : "" ) + 
170
  ( ( domain ) ? ";domain=" + domain : "" ) +
171
  ( ( secure ) ? ";secure" : "" );
172
}
173

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