function getSkyguardLoginURL() {
	return "ajax-login.do?nothing";
}
function getSkyguardForgotPasswordURL() {
	return "ajax-forgotpassword.do?nothing";
}
function getSkyguardLoadContext() {
	return "ajax-loadcontext.do?nothing";
}
function SGGotoPage(page){
	var goToForm = document.forms['sgGotoForm'];
	goToForm.action = page;
	goToForm.submit();
}
var SGLoginDialog = null;
function hideSGLoginErrorMessage() {
		if(SGLoginDialog != null){
		    SGLoginDialog.hide();
		}
}
function showSGLoginErrorMessage(msg) {
	
	if(SGLoginDialog == null){
		var region = YAHOO.util.Dom.getRegion("headLogin");
		SGLoginDialog = new YAHOO.widget.SimpleDialog("SGLoginErrorDialog", {  
				    width: "400px",
				    y: 100,
				    x: region.width / 2 - 200,  
				    iframe: true,
				    modal:false, 
				    visible:false, 
				    draggable:false,
				    buttons: [ { text:"ok", handler:hideSGLoginErrorMessage, isDefault:true }] 
				               				     }); 
		SGLoginDialog.setHeader("Unable to complete the request."); 

		SGLoginDialog.cfg.setProperty("icon",YAHOO.widget.SimpleDialog.ICON_WARN);
		//SGLoginDialog.render(YAHOO.util.Dom.get("SGLoginErrorDialogContainer"));  
		SGLoginDialog.render("SGLoginDialogContainer");
	}
	SGLoginDialog.setBody(msg);
	SGLoginDialog.show();
}


function loadRememberedLoginInfo() {

		var sSkyguardLoginURL = getSkyguardLoginURL();
		sSkyguardLoginURL += "&callBack=setRememberedLoginInfo";
		sSkyguardLoginURL += "&errorsCallBack=showSGLoginErrorMessage";
		var scriptRequest = new JSONscriptRequest(sSkyguardLoginURL);
		scriptRequest.buildScriptTag();
		scriptRequest.addScriptTag();
}

function setRememberedLoginInfo(data){
	var account = data.account;
	var user= data.username;
	var pw = data.password;
	var rememberLogin = data.rememberLogin;
	var rememberpassword = data.rememberPassword;

	document.forms['logonForm'].elements['loginAccountName'].value=account;
	document.forms['logonForm'].elements['loginUsername'].value=user;
	document.forms['logonForm'].elements['loginPassword'].value=pw;
	document.forms['logonForm'].elements['rememberLogin'].checked=rememberLogin;
	document.forms['logonForm'].elements['rememberLoginCheckBox'].checked=rememberLogin;
	document.forms['logonForm'].elements['rememberPassword'].checked=rememberpassword;
	document.forms['logonForm'].elements['rememberPasswordCheckBox'].checked=rememberpassword;
}

function doLogin() {
		try{
		var sSkyguardLoginURL = getSkyguardLoginURL();
		sSkyguardLoginURL += "&loginSubmitValue=Submit";
		if(document.forms['logonForm'].elements['rememberPasswordCheckBox'].checked){
			sSkyguardLoginURL += "&rememberPassword=true";
		} else {
			sSkyguardLoginURL += "&rememberPassword=false";
		}
		if(document.forms['logonForm'].elements['rememberLoginCheckBox'].checked){
			sSkyguardLoginURL += "&rememberLogin=true";
		} else {
			sSkyguardLoginURL += "&rememberLogin=false";
		}
		sSkyguardLoginURL += "&loginAccountName=" + encodeURIComponent(document.forms['logonForm'].elements['loginAccountName'].value);
		sSkyguardLoginURL += "&loginUsername=" + encodeURIComponent(document.forms['logonForm'].elements['loginUsername'].value);
		sSkyguardLoginURL += "&loginPassword=" + encodeURIComponent(document.forms['logonForm'].elements['loginPassword'].value);
		sSkyguardLoginURL += "&timeZoneOffset=" + encodeURIComponent(document.forms['logonForm'].elements['timeZoneOffset'].value);
		sSkyguardLoginURL += "&daylightSavingAdjust=" + encodeURIComponent(document.forms['logonForm'].elements['daylightSavingAdjust'].value);
		sSkyguardLoginURL += "&loginSuccessCallBack=goToSGSuccessPage";
		sSkyguardLoginURL += "&errorsCallBack=showSGLoginErrorMessage";

		var scriptRequest = new JSONscriptRequest(sSkyguardLoginURL);
		scriptRequest.buildScriptTag();
		scriptRequest.addScriptTag();
		}catch (ex){
			alert("Error: "+ex.message);
		}
		return false;
}

function gotoWRXLogin() {
	SGGotoPage('https://sfo.wrx-us.net/fleetoutlook/fleetoutlook.jsp');
}

function goToSGSuccessPage(page, shouldShowWRXMessage) {
	var acknowledgeFunction = null;
	if (page.indexOf("/fh") == -1) {
		acknowledgeFunction = function() {
			document.body.style.cursor="wait";
			SGGotoPage(page);
		};
	} else {
		// This is a FH account, just submit the form the original way.
		acknowledgeFunction = loginSubmitEvent;
	}
	
	if(shouldShowWRXMessage)  {
		showWRXMessage(gotoWRXLogin);

	} else {
		acknowledgeFunction();
	}
}

function doForgotPassword() {
	
	 var givenAccountName = document.forms['logonForm'].elements['loginAccountName'].value;
	 if(givenAccountName.length == 0) {
		 showSGLoginErrorMessage("Please provide your account number.");
		 return;
	 }
	 var sSkyguardForgotPasswordURL = getSkyguardLoadContext();
	 sSkyguardForgotPasswordURL += "&accountName=" + encodeURIComponent(givenAccountName);
	 sSkyguardForgotPasswordURL += "&loginUsername=" + encodeURIComponent(document.forms['logonForm'].elements['loginUsername'].value);
	 sSkyguardForgotPasswordURL +="&errorsCallBack=showSGLoginErrorMessage";
	 sSkyguardForgotPasswordURL +="&successCallBack=goToSGForgotPasswordPage";
	 
	 var scriptRequest = new JSONscriptRequest(sSkyguardForgotPasswordURL);
	 scriptRequest.buildScriptTag();
	 scriptRequest.addScriptTag();
}

function goToSGForgotPasswordPage(context) {
	var givenAccountName = document.forms['logonForm'].elements['loginAccountName'].value;
	var givenLoginName = document.forms['logonForm'].elements['loginUsername'].value;
	document.body.style.cursor="wait";
	var newURL = context+"/forgotpassword.do?accountName="+encodeURIComponent(givenAccountName);
	newURL += "&loginId=" + encodeURIComponent(givenLoginName);
	if(givenLoginName.length > 0 && givenAccountName.length > 0) {
		newURL += "&submitValue=Submit";
	}
	SGGotoPage(newURL);
}

var WRXDialog = null;
function showWRXMessage(acknowledgeFunction){
	
	if(WRXDialog == null){
		var region = YAHOO.util.Dom.getRegion("headLogin");
		WRXDialog = new YAHOO.widget.SimpleDialog("WRXDialog", {  
				    width: "400px",
				    y: 100,
				    x: region.width / 2 - 200,  
				    iframe: true,
				    modal:false, 
				    visible:false, 
				    draggable:false,
				    buttons: [ { text:"ok", handler:acknowledgeFunction, isDefault:true }] 
				               				     }); 
		WRXDialog.setHeader("Customer Notification "); 

		//WRXDialog.cfg.setProperty("icon",YAHOO.widget.SimpleDialog.ICON_WARN);
		WRXDialog.render("SGLoginDialogContainer");
		WRXDialog.hideEvent.subscribe(acknowledgeFunction);
	}

	var msg = "<p style='text-align:left'>Your SkyGuard account has been transitioned to FleetOutlook Express.  Please click OK below to be automatically redirected to that login page.  If you need any support during this process, our Customer Care team is ready to assist you 24x7x365. Contact Customer Care toll-free at 1.866.456.7522 or via email at customercare@wirelessmatrix.com.</p>";


	WRXDialog.setBody(msg);
	WRXDialog.show();
}




/*  Below it Forgot Password code that will be used later.
var handleCancel = function() { 
   this.cancel(); 
} 
var handleSubmit = function() {
	this.submit(); 
	//document.forms['sgforgotpasswordform'].submit();
}

var SGForgotPasswordDialog = null;
	function getForgotPasswordDialog(){
 if(SGForgotPasswordDialog ==null){

	 var region = YAHOO.util.Dom.getRegion("headLogin");

	 var submitAccountButtons = [ { text:"Submit", handler:handleSubmit, isDefault:true }, 
	        	                  { text:"Cancel", handler:handleCancel } ];

	  SGForgotPasswordDialog = new YAHOO.widget.Dialog("SGForgotPasswordDialog", {  
	    width: "400px",
	    y: region.top,
	    x: region.left,  
	    iframe: true,
	    modal:false, 
	    visible:false, 
	    draggable:false,
	    buttons: submitAccountButtons
	    });
	 SGForgotPasswordDialog.setHeader("Forgot Password");
	 var bodyText = "<form id=\"sgforgotpasswordform\" name=\"sgforgotpasswordform\" method=\"post\" action=\""+getSkyguardForgotPasswordURL()+"\" onsubmit=\"return false;\"> \n"
	 bodyText += "<p>Please enter your account Name</p>";
	 bodyText += "<input type=\"text\" name=\"accountName\" value=\"\">";
	 bodyText +="</form>";
	 SGForgotPasswordDialog.setBody(bodyText);

	 SGForgotPasswordDialog.render("SGLoginDialogContainer");
	 //SGForgotPasswordDialog.cfg.queueProperty("buttons", submitAccountButtons);
 } 
 return SGForgotPasswordDialog;
}
function collectForgotPasswordAnswer() {
 var dialog = getForgotPasswordDialog();
}
function submitForgotPasswordAnswer() {
 var dialog = getForgotPasswordDialog();
}

var onFailure = function(o) { 
alert("Your submission failed. Status: " + o.status); 
} 
function handleAccountSubmitResponse () {
 alert("Your data was successfully submitted. The response was: " + o.responseText); 
}
function doForgotPassword() {

 var dialog = getForgotPasswordDialog();

 var givenAccountName = document.forms['logonForm'].elements['loginAccountName'].value;
 document.forms['sgforgotpasswordform'].elements['accountName'].value=givenAccountName;

 dialog.callback.success = handleAccountSubmitResponse;
 dialog.callback.failure = onFailure;

 dialog.show();
}
*/

