Apollo.cart.accountRegistration = {

	'__validationRules' : null ,
	
	'initRegistrationForm' : function() {

		// Activate the Trigger for the country list box
		
		$("#lstBoxCountry").change(function() {

			// Hide the state field and show the wait spinner

			$('#stateList').css('display', 'none');
			$('#otherstate').css('display', 'none');
			$('#stateWaitSpinner').css('display', 'block');


			// Clear the list of states
			document.clientregistration.state.options.length = 0;

			Apollo.cart.accountRegistration._initCpfField();
			
			// Do the ajax request 

			$.ajax({
				type 		: 'POST',
				url			: getSecureBaseUrl() + '?ajaxStateList',
				dataType	: 'json',
				async		: false ,
				data		: {
					CountryCode : document.clientregistration.country.value 
				},
				success		: function(data) {

					if ( data['listAvail'] && data['listAvail'] == true ) {

						for ( var stateElem in data['data'] ) {
							Apollo.cart.accountRegistration.addState( data['data'][stateElem]['Code'] , data['data'][stateElem]['Name'] );
						}

						$('#stateWaitSpinner').css('display', 'none');
						$('#stateList').css('display', 'block');
					} else {
						$('#stateWaitSpinner').css('display', 'none');
						$('#otherstate').css('display', 'block');
					}
				}, 
				error		: function(request, error, exceptObj) {
					$('#stateWaitSpinner').css('display', 'none');
					$('#otherstate').css('display', 'block');
				}
			});

		});
		
		// Load the fields information
		
		$.ajax({
				type 		: 'POST',
				url			: getSecureBaseUrl() + '?ajaxRegisterValidationRules',
				dataType	: 'script'
		});
		
		
		Apollo.cart.accountRegistration._initCpfField();

	},

	'CheckForm' : function(frm){
		
		var isBusiness = false ;
		
		if ( businessRegistrationEnabled ) {
			if ( individualsRegistrationEnabled ) {
				if ( document.clientregistration.business[0].checked ) {
					isBusines = true ;
				}
			} else {
					isBusines = true ;
			}
		}
		
		if ( ! Apollo.cart.accountRegistration._checkSection('base') )	{ return false ; }
		
		if ( isBusiness ) {
			
			if ( ! Apollo.cart.accountRegistration._checkSection('business') )	{ return false ; }
			
			if ( requireBusinessLicenceNumber ) {

				if ( ! Apollo.cart.accountRegistration._checkSection('businesspharm') )	{ return false ; }
				
			}

			if ( businessSurvey ) {

				if ( ! Apollo.cart.accountRegistration._checkSection('businessSurvey') )	{ return false ; }
				
			}
			
		}
	
		if ( showUserNamePassword ) {

			if ( ! Apollo.cart.accountRegistration._checkSection('password') )	{ return false ; }
			
			if ( document.clientregistration.password.value.length <= 7 || document.clientregistration.password.value.length >= 20  ) {
				Apollo.common.displayErrorMessage( Apollo.cart.accountRegistration.__validationRules['misc']['pwdLenghtError'] );
				return false;
			}

			if ( document.clientregistration.password.value != document.clientregistration.passwordconfirm.value ) {
				Apollo.common.displayErrorMessage( Apollo.cart.accountRegistration.__validationRules['misc']['pwdMismatch'] );
				return false;
			}
			
		}

			
		if ( ! Apollo.cart.accountRegistration._isValidEmail( document.clientregistration.elements["email"].value ) ){
			Apollo.common.displayErrorMessage( Apollo.cart.accountRegistration.__validationRules['misc']['invalidEmail'] );
			return false;
		}

		if ( Apollo.cart.accountRegistration._isCpfFieldIsRequired() ) {
			
			var valCpf1 = document.clientregistration.elements["cpf1"].value ;
			var valCpf2 = document.clientregistration.elements["cpf2"].value ;
			var valCpf3 = document.clientregistration.elements["cpf3"].value ;
			var valCpf4 = document.clientregistration.elements["cpf4"].value ;
			
			if ( ! valCpf1.match(/^[0-9][0-9][0-9]$/) || 
				 ! valCpf2.match(/^[0-9][0-9][0-9]$/) || 
				 ! valCpf3.match(/^[0-9][0-9][0-9]$/) || 
				 ! valCpf4.match(/^[0-9][0-9]$/) ) {
				Apollo.common.displayErrorMessage( Apollo.cart.accountRegistration.__validationRules['misc']['invalidCFP'] );
				return false;
			}
			
		}
		
		
		if ( document.clientregistration.elements["agree"].checked == false) {
			Apollo.common.displayErrorMessage( Apollo.cart.accountRegistration.__validationRules['misc']['acceptTerms'] );
			return false;
		}

		document.clientregistration.submit();

	},
	
	'_checkSection' : function( sectionName ) {

		var sectionData = Apollo.cart.accountRegistration.__validationRules[ sectionName ];
		
		for ( var i in sectionData ) {
			if ( jQuery.trim( document.clientregistration.elements[ i ].value ) == '' ) {
				Apollo.common.displayErrorMessage( Apollo.cart.accountRegistration.__validationRules['misc']['fieldMissing'] +
													' : '+ sectionData[ i ] , '');
				return false ;
			}
	    }
	    
	    return true;
		
	},

	'_isValidEmail' : function (entry) {
		var rex= /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,5})(\]?)$/;
		return rex.test(entry);
	},
	
	'clkBusiness' : function() {

		document.clientregistration.company.disabled=false;

		if ( requireBusinessLicenceNumber ) {
			document.clientregistration.licensenumber.disabled=false;
			document.clientregistration.licenseexpiration.disabled=false;
		}

		if ( businessSurvey ) {
			document.clientregistration.TypeOfFacility.disabled=false;
			document.clientregistration.PrimaryUseForTheSite.disabled=false;
			document.clientregistration.CurrentPrimaryWholesaler.disabled=false;
			document.clientregistration.AlternateWholesaler.disabled=false;
			document.clientregistration.TradeReferences.disabled=false;
			document.clientregistration.EstimatedTotalMonthlyVolume.disabled=false;
			document.clientregistration.EstimatedTotalMonthlyVolumeWithUs.disabled=false;
		}

		Apollo.cart.accountRegistration._showRegistrationPane();	

		// Show the Business Pane

		document.getElementById('businessPane1').style.display = 'inline'; 
		document.getElementById('businessPane2').style.display = 'inline'; 
		document.getElementById('businessPane3').style.display = 'inline';

		
		Apollo.cart.accountRegistration._initCpfField();
	},

	'clkIndividual' : function() {
		document.clientregistration.company.disabled=true;  
		document.clientregistration.company.value='';

		if ( requireBusinessLicenceNumber ) {
			document.clientregistration.licensenumber.disabled=true;
			document.clientregistration.licenseexpiration.disabled=true;
		}

		if ( businessSurvey ) {
			document.clientregistration.TypeOfFacility.disabled=true;
			document.clientregistration.PrimaryUseForTheSite.disabled=true;
			document.clientregistration.CurrentPrimaryWholesaler.disabled=true;
			document.clientregistration.AlternateWholesaler.disabled=true;
			document.clientregistration.TradeReferences.disabled=true;
			document.clientregistration.EstimatedTotalMonthlyVolume.disabled=true;
			document.clientregistration.EstimatedTotalMonthlyVolumeWithUs.disabled=true;
		}

		Apollo.cart.accountRegistration._showRegistrationPane();

		// Hide the Business Pane

		document.getElementById('businessPane1').style.display = 'none'; 
		document.getElementById('businessPane2').style.display = 'none'; 
		document.getElementById('businessPane3').style.display = 'none';

		
		Apollo.cart.accountRegistration._initCpfField();
	},

	'_showRegistrationPane' : function() {
		$('#mainForm').css('display', 'inline');	
	},
	
	'updatestate' : function(frm){
		//frm.elements["country"].value
		var frm2 = document.forms['clientregistration'];
		var vParm = frm2.elements["state"].options[frm2.elements["state"].selectedIndex].value;
		if (vParm == '_O') {
			otherstate.innerHTML = 'Other State: <input type=\"text\" name=\"otherstate\">';
		} else {
			otherstate.innerHTML = '';
		}
	},
	
	'addState' : function(value, name) {
		var elOptNew = document.createElement('option');
		elOptNew.text = name;
		elOptNew.value = value;
			
		document.clientregistration.state.options.add(elOptNew);
	},
	
	'clearCustomerTypeButtons' : function() {
		document.clientregistration.business[0].checked = false ; 
		document.clientregistration.business[1].checked = false ;
	},
	
	'_initCpfField' : function() {

		if ( ! brazilCpfField ) {
			return false;
		}
		
		if ( Apollo.cart.accountRegistration._isCpfFieldIsRequired() ) {
			$('#brCustCPFForm').css('display' , 'inline');
			return false;
		}

		$('#brCustCPFForm').css('display' , 'none');
	},
	
	'_isCpfFieldIsRequired' : function() {
		
		if ( ! brazilCpfField ) {
			return false;
		}
		
		var isBusiness = false ;
		
		if ( businessRegistrationEnabled ) {
			if ( individualsRegistrationEnabled ) {
				if ( $('#radioCustTypeBusiness').is(':checked') ) {
					isBusiness = true ;
				}
			} else {
				isBusiness = true ;
			}
		}

		if ( document.clientregistration.country.value == 'BR' && ! isBusiness ) {
			return true;
		}

		return false;
	}

};
