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
      }
20
      else
21
      { //login not successful
22
        alert('Sorry, your login failed.  Please try again.  If you need a username, please go to http://knb.ecoinformatics.org.');
23
        setCookie("sms-login", false);
24
      }
25
      
26
    }, "XML");
27
}
28

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

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

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

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

    
111
function showDatasets()
112
{
113
  reloadSearchContent('/sms/metacat?action=query&anytext=%25&qformat=sms&pagesize=10&pagestart=0');
114
}
115

    
116
function reloadSearchContent(url)
117
{
118
  $("#searchresulttable").load(url);
119
}
120

    
121
function uploadfile()
122
{
123
  if(getCookie("sms-login") != "true")
124
  {
125
    alert('You cannot upload.  You are not logged in.');
126
    return;
127
  }
128
  $("form").submit();
129
}
130

    
131
function slideUp(id)
132
{
133
  $(id).slideUp("slow");
134
}
135

    
136
function slideDown(id)
137
{
138
  $(id).slideDown("slow");
139
}
140

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

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