//-----------------------------------------------------------------
// Licensed Materials - Property of IBM
//
// WebSphere Commerce
//
// (C) Copyright IBM Corp. 2007, 2008 All Rights Reserved.
//
// US Government Users Restricted Rights - Use, duplication or
// disclosure restricted by GSA ADP Schedule Contract with
// IBM Corp.
//-----------------------------------------------------------------

/** 
 * @fileOverview This javascript is used by UserRegistrationAddForm.jsp and CheckoutLogon.jsp.
 * @version 1.0
 */

  /* Import dojo classes */
dojo.require("wc.service.common");

/**
 *  The functions defined in the class helps the customer to register with the store. Another function enables a returning 
 *  customer to Sign in for quickcheckout from the shopping cart page.
 *
 *  @class This LogonForm class defines all the functions and variables used to validate the information provided by the
 *	customer to register with the store. To register, a customer creates a logon ID and password. Then, the customer provides their first name, 
 *  last name, street address, city, country/region, state/province, ZIP/postal code, e-mail address and phone number. Other registration options 
 *  include promotional e-mails, preferred language and currency, age, gender, and the remember me option. 
 */
LogonForm ={
	/** Flag which indicates whether 'AjaxMyAccount' option is enabled or not. The value of this variable is automatically populated
	 *  based on the change flow option in the store by the setAjaxVar function.
	 */
	ajaxMyAccountEnabled: "false",
	
	/**
	 * This function validates the logon ID and password for returning customers to sign in and complete the checkout process.
	 * @param {string} form The name of the form containing logon ID and password fields.
	 */
	SubmitAjaxLogin:function(form){
		reWhiteSpace = new RegExp(/^\s+$/);

		if(form.logonId != null && reWhiteSpace.test(form.logonId.value) || form.logonId.value == ""){ 
			MessageHelper.formErrorHandleClient(form.logonId.id,MessageHelper.messages["REQUIRED_FIELD_ENTER"]); return;}
			
		if(form.logonPassword != null && reWhiteSpace.test(form.logonPassword.value) || form.logonPassword.value == ""){ 
			MessageHelper.formErrorHandleClient(form.logonPassword.id,MessageHelper.messages["REQUIRED_FIELD_ENTER"]); return;}
		
		/*For Handling multiple clicks. */
		if(!submitRequest()){
			return;
		}
				
		form.submit();	

	},
	
	/** 
	 * This function is called when the Submit button is clicked on the Registration page. All the fields containing customer
	 * information are validated and PersonProcessServicePersonRegistration is called. 
	 * @param {string} form The name of the registration form containing all the customer information.
	 */
	prepareSubmit:function (form)
	{
	    reWhiteSpace = new RegExp(/^\s+$/);
		if(form.logonId != null && reWhiteSpace.test(form.logonId.value) || form.logonId.value == ""){ 
			MessageHelper.formErrorHandleClient(form.logonId.id,MessageHelper.messages["ERROR_LogonIdEmpty"]); return;}
		if(form.logonPassword != null && reWhiteSpace.test(form.logonPassword.value) || form.logonPassword.value == ""){ 
			MessageHelper.formErrorHandleClient(form.logonPassword.id,MessageHelper.messages["ERROR_PasswordEmpty"]); return;}
		if(form.logonPasswordVerify != null && reWhiteSpace.test(form.logonPasswordVerify.value) || form.logonPasswordVerify.value == ""){ 
			MessageHelper.formErrorHandleClient(form.logonPasswordVerify.id,MessageHelper.messages["ERROR_VerifyPasswordEmpty"]); return;}
		if(form.logonPassword.value!= form.logonPasswordVerify.value){ 
			MessageHelper.formErrorHandleClient(form.logonPasswordVerify.id,MessageHelper.messages["PWDREENTER_DO_NOT_MATCH"]);
			return;
		}
		
		/** Uses the common validation function defined in AddressHelper class for validating first name, 
		 *  last name, street address, city, country/region, state/province, ZIP/postal code, e-mail address and phone number. 
		 */
		if(!AddressHelper.validateAddressForm(form, '')){
			return;
		}
		
		/* Checks whether the customer has registered for promotional e-mails. */
		if(form.sendMeEmail && form.sendMeEmail.checked){
		    form.receiveEmail.value = true;
		}
		else {
			form.receiveEmail.value = false;
		}
		
		/* Checks whether the birthday option is enabled, if so, calls the validateBirthday function defined in MyAccountDisplay class. */   
		if(form.birthdayEnabled != null && form.birthdayEnabled.value == "true"){
			if(!MyAccountDisplay.validateBirthday(form)){
				return;
			}
		}
		/* For Handling multiple clicks. */
		if(!submitRequest()){
			return;
		}
		
		form.submit();
	},
	
	/**
	 * Sets the flag which indicates whether 'AjaxMyAccount' feature is enabled or not.
	 * Based on this, relevant code is generated.
	 * @param {boolean} temp A value that is set temporarily.
	 */

	setAjaxVar: function(temp)
	{
		this.ajaxMyAccountEnabled = temp;
	},
	
	/**
	 * Gets the flag which indicates whether 'AjaxMyAccount' feature is enabled or not.
	 * Based on this, relevant code is generated.
	 * 
	 * @return {boolean} value of the flag.
	 */

	getAjaxVar: function()
	{
		return(this.ajaxMyAccountEnabled);
	},
	
	/**
	  *	This function is used when "Age" option is changed.
	  * This will show one alert message if the user age is under 13.
	  * @param {string} form The name of the registration form containing customer's age.
	  */

	checkAge:function (form)
	{
		if(form.age.value==1) alert(form.AgeWarning.value); 
	}
}
