/*
	USED BY: ReturningDealerLogin.xsl
	
	needs to be a better way to store the div names
	NOTE: takes advantage of MooTools short hand for get element by id
		some forms will have a special call - coudl do a switch be type to make smaller

*/
	var gsContLink = '';

	function submitLogin(frm, divBtn, divFeedback){
		gsDiv1 = divBtn;
		gsDiv2 = divFeedback;
		
		if(validateLogin(frm)){
			toggleSaveButton(false);
			gsContLink = (null != frm.sContLink.value && frm.sContLink.value != '')? frm.sContLink.value : window.location.href;
			callLogin(frm);
		} else {
			alert('Please enter a username and password.');
		}
	}
	
	function submitDealerLogin(frm, divBtn, divFeedback){
		gsDiv1 = divBtn;
		gsDiv2 = divFeedback;
		
		if(validateLogin(frm)){
			toggleSaveButton(false);
			gsContLink = (null != frm.sContLink.value && frm.sContLink.value != '')? frm.sContLink.value : window.location.href;
			callDealerLogin(frm);
		} else {
			alert('Please enter a username and password.');
		}
	}
	
	function validateLogin(frm){
		var sName = frm.txtUserName.value;
		var sPass = frm.txtPassword.value;
		var bReturn = true;
		if (sPass.length == 0 || sName.length == 0)	bReturn = false;
		return bReturn;
	}


	// AJAX CALLS AND CALL BACKS
	function callLogin(frm){
		var oRecord = GreenhouseCustom.CustomLoginAjax.GetRecordObject().value;
		oRecord.Username = frm.txtUserName.value;
		oRecord.Password = frm.txtPassword.value;
		oRecord.AccessGroupID = frm.groupId.value;//
		GreenhouseCustom.CustomLoginAjax.Validate(oRecord,login_callback);
	}
	
	function callDealerLogin(frm){
		var oRecord = GreenhouseCustom.CustomLoginAjax.GetRecordObject().value;
		oRecord.Username = frm.txtUserName.value;
		oRecord.Password = frm.txtPassword.value;
		oRecord.AccessGroupID = frm.groupId.value;//
		GreenhouseCustom.CustomLoginAjax.ValidateDealer(oRecord,login_callback);
	}
	
	function login_callback(response) {
		var oResult = response.value;
		if (oResult.Success == true) {
	   		if (oResult.NavURL == ''){
				window.location.href = gsContLink;//
	    	}else{
	    		window.location.href = oResult.NavURL;
			}
		} else {
			alert(oResult.ErrorText);
			toggleSaveButton(true);
		}
		
	}

	
	function validatePassword(){
		var frm = document.frmNewCustomer;
		var pw = frm.txtPassword.value;
		var cpw = frm.txtConfirm.value;

		if (pw != cpw) {
		frm.txtPassword.value = '';
		frm.txtConfirm.value = '';
		return false;}

		return true;
	}
	
	
