var undefined;

function onPageLoad() 
{
	onPageLoadGlobal();
	onLayoutResize(true);
	if (typeof(ObjWdsForm.LG_logintemp) != 'undefined') {
		if (ObjWdsForm.LG_logintemp.type == "text") {
			setTimeout("myfocus(ObjWdsForm.LG_logintemp);", 1);
		}
	}
}

function onLogoutPageLoad() 
{
	onLayoutResize(false);

   initWdsFormObj();
   if (typeof(ObjWdsForm.LG_ReturnToReports) != "undefined") {
      ObjWdsForm.LG_ReturnToReports.focus();
	}
	
	if (G_strRootPath == "") {
		G_strRootPath = document.location.href.replace("/Common/Login/logout.aspx", "");
	}
}

// Verifies that the given string is not empty
function IsControlEmpty(obj, strMsg) 
{
   var str = obj.value;
   if (str == null || str == "") {
      alert(strMsg);
      myfocus(obj);
      return true;
   }

   return false;
}

// Checks the length of the given string
function CheckForLength(obj, maxLen, strMessage) 
{
	if (obj.value.length > maxLen) {
	    alert(strMessage);
	    obj.select();
	    myfocus(obj);
	    return false;
	}
	return true;
}

// Returns true if the string contains only AlphaNumeric characters.
// i_bIgnoreSpaces specifies whether a space is permitted.
function IsAlphaNum(i_strString, i_bIgnoreSpaces, i_bLogin) 
{
   var nIndex;
   var nCode;
   var nTemp;
   for (nIndex = 0; nIndex < i_strString.length; nIndex++) {
      nTemp = i_strString.charAt(nIndex);
      nCode = i_strString.charCodeAt(nIndex);
      if (i_bIgnoreSpaces == false) {
         if (nCode == 32)
            return false;
      }
      if (i_bLogin) {
         if (((nCode >= 0) && (nCode <= 31)) ||    // invalid chars
            ((nCode >= 33) && (nCode <= 47)) ||    // invalid chars
            ((nCode >= 58) && (nCode <= 64)) ||    // :;<=>?@
            ((nCode >= 91) && (nCode <= 96)) ||    // [\]^_`
            ((nCode >= 123) && (nCode <= 127)))
         {
            return false;
         }
      }
   }
   return true;
}

function SetLogin(obj, strLogin, strPassword) 
{
      obj.LG_login.value = strLogin;
      obj.LG_pwd.value = strPassword;   

	return;
}

function CheckLoginParameters(object, strAdmin) 
{
   var bValid = true;

  	if (typeof(object.LG_logintemp) != 'undefined') {
      var strLogin = object.LG_logintemp.value;
   }
   if (typeof(object.LG_pwdtemp) != 'undefined') {
      var strPassword = object.LG_pwdtemp.value;
   }
   
 	var strTemp;
   // Login
   if (IsControlEmpty(object.LG_logintemp, resLoginName))
      bValid = false;

   if (bValid) {
		strTemp = strLogin.toLowerCase();
      if (strTemp == strAdmin) {
			alert(resReserved);
         object.LG_logintemp.select();
         myfocus(object.LG_logintemp);          
		   bValid = false;
		}
   }
   if (bValid) {
      if (!CheckForLength(object.LG_logintemp, 50, resLoginNameLength))
		   bValid = false;
   }

   // Password
   if (bValid) {
      if (IsControlEmpty(object.LG_pwdtemp, resNoPassword))
         bValid = false;
   }
   // look for white spaces in the Password
   if (bValid) {
      if (strPassword.indexOf(" ") != -1) {
         alert(resSpaces);
         object.LG_pwdtemp.select();
         myfocus(object.LG_pwdtemp);
         bValid = false;
      }
   }
   if (bValid) {
      if (!CheckForLength(object.LG_pwdtemp, 50, resPasswordLength))
		   bValid = false;
   }

   return bValid;
}

// Checks for white spaces or single and double quotes in the Login name
function ValidateLogin(object, strAdmin) 
{
 var bValid;

	bValid = CheckLoginParameters(object, strAdmin);
	if (bValid) {
		object.LG_verifylogin.value = "True";
		SetLogin(object, object.LG_logintemp.value, object.LG_pwdtemp.value);
		object.action = object.LG_targetpage.value;
	}

	return bValid;
}

//Handles "return to reports" after logging out.
function onJump() 
{
	initWdsFormObj();
	document.location = G_strRootPath + "/ReportFolders/reportFolders.aspx?sCS_ChosenLang=" + ObjWdsForm.sCS_ChosenLang.value;
}

function OnHelpWindow(bOnTab) 
{
   var strPage = "";
   var strMainFrame;

	strMainFrame = location.toString().toLowerCase() 
	if (strMainFrame.indexOf("register.aspx") >= 0) {
	  strPage = "registerPage.htm";
	}
  	else {
	  strPage = "signinpage.htm";
	}

   OpenHelpWindow(strPage, bOnTab);
}

function OnRegisterUser() {
	var strUserName = ObjWdsForm.LG_logintemp.value;
	var strPassword = ObjWdsForm.LG_regpassword.value;
	var strConfirm = ObjWdsForm.LG_regconfirm.value;
	var strFirstName = ObjWdsForm.LG_regfirstname.value;
	var strLastName = ObjWdsForm.LG_reglastname.value;
	var strOrganization = ObjWdsForm.LG_regorg.value;
	var strEmail = ObjWdsForm.LG_regemail.value;
	var strLang = ObjWdsForm.sCS_ChosenLang.value;
	var clMessage = document.getElementById("registerMessage");
	var strMessage = "";
	
	if (strUserName.length == 0) { 
		NotifyOnRegisterUser({status:false, message:resRegistrationHelp, invalidElement:0});
	}
	else if (strPassword.length == 0) { 
		NotifyOnRegisterUser({status:false, message:resRegistrationHelp, invalidElement:1});
	}
	else if (strPassword != strConfirm) {
		NotifyOnRegisterUser({status:false, message:resPasswordMismatch, invalidElement:1});
	}
	else {
		var clRequest = new wdsRequest();
		clRequest.Handler = NotifyOnRegisterUser;
		clRequest.ExecRegister(strUserName, strPassword, strFirstName, strLastName, strOrganization, strEmail, strLang);
	}
}

function NotifyOnRegisterUser(i_oResult) {
	if (i_oResult.status) {
		CreateHiddenFormField(ObjWdsForm, "LG_login", i_oResult.username.toLowerCase());
		CreateHiddenFormField(ObjWdsForm, "LG_pwd", i_oResult.password);
		if (typeof(ObjWdsForm.LG_targetpage) != "undefined" && ObjWdsForm.LG_targetpage.value.length > 0) {
			ObjWdsForm.action = ObjWdsForm.LG_targetpage.value;
		}
		else {
			ObjWdsForm.action = G_strRootPath + "/ReportFolders/reportFolders.aspx";
		}
		executeWait(ObjWdsForm);
	}
	else {
		document.getElementById("registerMessage").innerHTML = i_oResult.message;
		switch (i_oResult.invalidElement) {
		case 0: obj = ObjWdsForm.LG_logintemp; break;
		case 1: obj = ObjWdsForm.LG_regpassword; break;
		case 2: obj = ObjWdsForm.LG_regconfirm; break;
		case 3: obj = ObjWdsForm.LG_regfirstname; break;
		case 4: obj = ObjWdsForm.LG_reglastname; break;
		case 5: obj = ObjWdsForm.LG_regorg; break;
		case 6: obj = ObjWdsForm.LG_regemail; break;
		default: obj = ObjWdsForm.LG_logintemp; break;
		}
		obj.select();
		obj.focus();
		return;
	}
}

function OnRegisterKeyPressHandler(e) {
	OnKeyPressHandler(e, OnRegisterUser);
}

function OnCancelLogin() {
	ObjWdsForm.action = ObjWdsForm.CS_TargetPage.value;
	executeWait(ObjWdsForm);
}

