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
  }
104
  else
105
  {
106
    setLoginHeader(false);
107
  }
108
}
109

    
110
function uploadfile()
111
{
112
  if(getCookie("sms-login") != "true")
113
  {
114
    alert('You cannot upload.  You are not logged in.');
115
    return;
116
  }
117
  
118
  /*$.post("metacat", $("form").serialize(), 
119
    function(data)
120
    {
121
      alert($("form").serialize());
122
      alert(data);
123
    }, 
124
    "XML");*/
125
  $("form").submit();
126
}
127

    
128
function slideUp(id)
129
{
130
  $(id).slideUp("slow");
131
}
132

    
133
function slideDown(id)
134
{
135
  $(id).slideDown("slow");
136
}
137

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

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