/* 
   inc_login.js
   
 */
function md5auth(seed) 
{ 
  try
  {
	  ToggleProcessing(true);
	  var oPass = document.getElementById('lgn_Password');
	  var oEmail = document.getElementById('lgn_EmailAddress');
	  var password = oPass.value;
	  var emailaddress = oEmail.value;
	  //var hash = MD5(seed+password);
	  var hash = password;
	  //var hash = MD5(password);
	  oPass.value='';
	  $.ajax({
      url: 'LoginHandler.asp',
      type: 'POST',
      dataType: 'xml',
	  data:'password='+ hash + '&' + 'emailaddress=' + emailaddress,  
      timeout: 10000,
      error: function(xmlhttp,errMsg){
        alert(xmlhttp.responseText);
		alert('There was a login error, please contact an administrator.  More info:' + errMsg + ':' + xmlhttp.status );
		ToggleProcessing(false);
	  },
      success: function(xmldoc){
		//alert(xmldoc);
		var userid_node = xmldoc.getElementsByTagName('userid').item(0);
		var divServerMessage = document.getElementById("div_lgn_ServerMessage");
		if(userid_node.firstChild.data > 0)
		{
			var div_login = GetDivLogin();
			frm_remove_all_child_nodes(div_login);
			var firstname = xmldoc.getElementsByTagName('userfirstname').item(0).firstChild.data;
			var lastname =  xmldoc.getElementsByTagName('userlastname').item(0).firstChild.data;
			var isRegistered = xmldoc.getElementsByTagName('isregistered').item(0).firstChild.data;
			var userid = xmldoc.getElementsByTagName('userid').item(0).firstChild.data;
			var email = xmldoc.getElementsByTagName('email').item(0).firstChild.data;
			//alert(userid);
			//alert(isRegistered);
		    if(userid > 0)
			{
				if(isRegistered=='True')
				{
					ServerMsg("lightgreen","Welcome <b>"+firstname+"&nbsp;" + lastname + "</b>, thank you for visiting DSC online. You are now logged in and may access additional content on this website.",divServerMessage);
					div_login.appendChild(divServerMessage);
					setTimeout('window.OnLoginSuccessful()',4000);
				}
				else
				{
					ServerMsg("white","Hello <b>"+firstname+"&nbsp;" + lastname + "</b>, thank you for visiting DSC online. You still need to activate your account. The activation email was sent to " + email + ".  If you have any problems please contact an administrator. Thank-you.",divServerMessage);
					div_login.appendChild(divServerMessage);
					//setTimeout('window.OnLoginSuccessful()',130);
				}
			}
		}
		else
		{
			ServerMsg("red","Sorry, incorrect. Please try again.",divServerMessage);
			try { setTimeout('g_ElementToFocus.focus()',500); } //because ie has a bug
		catch(e) {  }
		}	
		ToggleProcessing(false);
       }
     });     
	 
  }
  catch(e) {	alert(e); button.disabled = false;}	
}
window.OnLoginSuccessful = function()
{
	//alert('tippy');
}
function ToggleProcessing(isProcessing)
{
  if(isProcessing)
  {
	GetDivLogin().style.visibility = 'hidden';
	GetDivLogin().height = "0px";
    document.getElementById("div_lgn_loading").style.visibility = 'visible';  
  }
  else
  {
    document.getElementById("div_lgn_loading").style.visibility = 'hidden';  
    GetDivLogin().style.visibility = 'visible';
	GetDivLogin().height = "100px";
  }
}
function GetDivLogin(){ return document.getElementById("div_login"); }
function ServerMsg(color,str,div) {
    frm_server_msg(color,str,div,"span_lgn_ServerMessage");
}
function checkEnter(e){ //only passed in in firefox
    var characterCode; 
    var eventObj;
    if(e && (e.which!=null))
    { 	
	//NN4
	eventObj = e;
    }
    else
    {
	//IE
	eventObj = event
    }
    characterCode = eventObj.keyCode 
    if(characterCode == 13) { //ascii 13 (enter key)
	// click either the product search button or the login
	// button - depending on where the focus is

	if(document.activeElement.id == "txt_prodsearch") {
	    var psearch_submit = document.getElementById("btn_prodsearch_submit");
	    psearch_submit.click(); 
	} else {
	    var lgn_Submit = document.getElementById("lgn_Submit");
	    lgn_Submit.click(); 
	}
	return false
    } else {
	return true
    }
}

