Project

General

Profile

1 3989 berkley
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 setLoginHeader(loggedin)
30
{
31
  if(loggedin)
32
  {
33
    var user = getCookie("sms-user");
34
    $('#loginheader').replaceWith("<h2 style=\"text-align:center\" id=\"loginheader\">"
35
      + user + " Logged In <a onclick=\"logout()\" style=\"font-size:70%\">[logout]</a></h2>");
36
    slideUp("#loginformdiv");
37
    $('#maindiv').css("display", "block");
38
    $('#bottomimg').css("bottom", "0px");
39
  }
40
  else
41
  {
42
    $('#loginheader').replaceWith("<h2 style=\"text-align:center\" id=\"loginheader\">"
43
      + "Please Log In</h2>");
44
    slideDown("#loginformdiv");
45
    $('#maindiv').css("display", "none");
46
    $('#bottomimg').css("bottom", "15px");
47
  }
48
}
49
50
function logout()
51
{
52
  $.get("metacat", {action:"logout", qformat:"xml"});
53
  setLoginHeader(false);
54
  setCookie("sms-login", false);
55
}
56
57
function checkLogin()
58
{
59
  if(getCookie("sms-login") == "true")
60
  {
61
    setLoginHeader(true);
62
  }
63
  else
64
  {
65
    setLoginHeader(false);
66
  }
67
}
68
69
function slideUp(id)
70
{
71
  $(id).slideUp("slow");
72
}
73
74
function slideDown(id)
75
{
76
  $(id).slideDown("slow");
77
}
78
79
function setCookie( name, value, expires, path, domain, secure )
80
{
81
  // set time, it's in milliseconds
82
  var today = new Date();
83
  today.setTime( today.getTime() );
84
85
  /*
86
  if the expires variable is set, make the correct
87
  expires time, the current script below will set
88
  it for x number of days, to make it for hours,
89
  delete * 24, for minutes, delete * 60 * 24
90
  */
91
  if ( expires )
92
  {
93
    expires = expires * 1000 * 60 * 60 * 24;
94
  }
95
  var expires_date = new Date( today.getTime() + (expires) );
96
97
  document.cookie = name + "=" +escape( value ) +
98
  ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
99
  ( ( path ) ? ";path=" + path : "" ) +
100
  ( ( domain ) ? ";domain=" + domain : "" ) +
101
  ( ( secure ) ? ";secure" : "" );
102
}
103
104
function getCookie( check_name ) {
105
	// first we'll split this cookie up into name/value pairs
106
	// note: document.cookie only returns name=value, not the other components
107
	var a_all_cookies = document.cookie.split( ';' );
108
	var a_temp_cookie = '';
109
	var cookie_name = '';
110
	var cookie_value = '';
111
	var b_cookie_found = false; // set boolean t/f default f
112
113
	for ( i = 0; i < a_all_cookies.length; i++ )
114
	{
115
		// now we'll split apart each name=value pair
116
		a_temp_cookie = a_all_cookies[i].split( '=' );
117
118
		// and trim left/right whitespace while we're at it
119
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
120
121
		// if the extracted name matches passed check_name
122
		if ( cookie_name == check_name )
123
		{
124
			b_cookie_found = true;
125
			// we need to handle case where cookie has no value but exists (no = sign, that is):
126
			if ( a_temp_cookie.length > 1 )
127
			{
128
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
129
			}
130
			// note that in cases where cookie is initialized but no value, null is returned
131
			return cookie_value;
132
			break;
133
		}
134
		a_temp_cookie = null;
135
		cookie_name = '';
136
	}
137
	if ( !b_cookie_found )
138
	{
139
		return null;
140
	}
141
}