/*
	USED BY: ReturningCustomerLogin.xsl
	
	needs to be a better way to store the div names
	NOTE: takes advantage of MooTools short hand for get element by id

*/
	var gsContLink = '';

	function returnCustomerLogin(frm, divBtn, divFeedback){
		gsDiv1 = divBtn;
		gsDiv2 = divFeedback;
		
		if(validateLogin(frm)){
			toggleSaveButton(false);
			gsContLink = frm.sContLink.value;
			returnLoginCall(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;
	}

	function returnLoginCall(frm){
		//var frm = document.frmGenericLogin;
		var oRecord = GreenhouseCustom.CustomLoginAjax.GetRecordObject().value;
		oRecord.Username = frm.txtUserName.value;
		oRecord.Password = frm.txtPassword.value;
		oRecord.AccessGroupID = 2;
		GreenhouseCustom.CustomLoginAjax.ValidateCustomer(oRecord,submit_callback);
	}
	
	function submit_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);
		}
		
	}

	// NEW CUSTOMER
	function saveNewCustomerForm(frm, divBtn, divFeedback){
		gsDiv1 = divBtn;
		gsDiv2 = divFeedback;
		
		if(validateNewCustomerForm(frm)){
			toggleSaveButton(false);
			gsContLink = frm.sContLink.value;
			submitNewCustomerForm();
		} 
			
	}
	
	function validateNewCustomerForm(frm){	
		var sMessage = '';	
			
		if (!validateEmail(frm.txtEmail)) sMessage += 'Enter a valid email address.\n'; 
		if (!validatePassword()) sMessage += 'Password does not match.\n';
    	if (!validatePasswordLength()) sMessage += 'Password must be at least 4 characters.\n';
    	
		//-- Required Text Fields -->
		if(frm.txtFirstName.value == '') sMessage += 'First Name is required.\n'; 
		if(frm.txtLastName.value == '') sMessage += 'Last Name is required.\n'; 
		if(frm.txtPassword.value == '') sMessage += 'Password is required.\n'; 
		if(frm.txtConfirm.value == '') sMessage += 'Password confirmation is required.\n'; 
			
		if (sMessage != ''){		
			alert('Please enter the following information:\n' + sMessage);		
			return false;	
		} else {		
			return 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;
	}
	function validatePasswordLength(){
		var frm = document.frmNewCustomer;
		var pw = frm.txtPassword.value;
		var cpw = frm.txtConfirm.value;

		if (pw.length < 4) {
		frm.txtPassword.value = '';
		frm.txtConfirm.value = '';
		return false;}

		return true;
	}
	
	//-- AJAX SAVE -->
	function submitNewCustomerForm(){
		var frm = document.frmNewCustomer;
		var oRecord = GreenhouseCustom.NewCustomerFormAjax.GetRecordObject().value;
			oRecord.FirstName = frm.txtFirstName.value;
			oRecord.LastName = frm.txtLastName.value;
			oRecord.Phone = frm.txtPhone.value;
			oRecord.Email = frm.txtEmail.value;
			oRecord.Password = frm.txtPassword.value;
			oRecord.Newsletter = frm.chkNewsletter.checked;
    		oRecord.Birthdate = '';
		    oRecord.AccessGroupID = 2;
		GreenhouseCustom.NewCustomerFormAjax.SaveNewCustomer(oRecord,submit_callback);
	}
	

	
	
