/******************************************************************************************************\

Form Validation And Navigation:

								__________ .___    _____    
								\______   \|   |  /  _  \   
								 |       _/|   | /  /_\  \  
								 |    |   \|   |/    |    \ 
								 |____|_  /|___|\____|__  / 
										\/              \/  
_______________   ____ _____    _______     ________ ___________.____     .___   ____________________ 
\_   _____/\   \ /   //  _  \   \      \   /  _____/ \_   _____/|    |    |   | /   _____/\__    ___/ 
 |    __)_  \   Y   //  /_\  \  /   |   \ /   \  ___  |    __)_ |    |    |   | \_____  \   |    |    
 |        \  \     //    |    \/    |    \\    \_\  \ |        \|    |___ |   | /        \  |    |    
/_______  /   \___/ \____|__  /\____|__  / \______  //_______  /|_______ \|___|/_______  /  |____|    
        \/                  \/         \/         \/         \/         \/             \/  

********************************************************************************************************


	
********************************************************************************************************
** 																									  **
********************************************************************************************************

\******************************************************************************************************/

function buildBirthYearSelector(elementId,inputAreaId) {
	
	var element=document.getElementById(elementId);
	var listData=new String();
	
	element.style.display="block";
	element.innerHTML=currentPage.loadingHTML;
	
	for (var currentNode=currentPage.year-17; currentNode>currentPage.year-117; currentNode--) {
		listData+="	<li onClick=\"document.getElementById('"+inputAreaId+"').value='"+currentNode+"'; document.getElementById('"+elementId+"').style.display='none'\">\
						"+currentNode+"\
					</li>";
	}

	element.innerHTML="	<ul>\
							"+listData+"\
						</ul>";
	
	
}



function buildMonthDaySelector(elementId,inputAreaId) {
	
	var element=document.getElementById(elementId);
	var listData=new String();
	
	element.style.display="block";
	element.innerHTML=currentPage.loadingHTML;
	
	for (var currentNode=1; currentNode<32; currentNode++) {
		var currentDay=currentNode.toString();
		if(currentDay.length<2) {
			currentDay=0+currentDay;
		}
		listData+="	<li onClick=\"document.getElementById('"+inputAreaId+"').value='"+currentDay+"'; document.getElementById('"+elementId+"').style.display='none'\">\
						"+currentDay+"\
					</li>";
	}

	element.innerHTML="	<ul>\
							"+listData+"\
						</ul>";
	
}



function buildMonthSelector(elementId,inputAreaId) {
	
	var element=document.getElementById(elementId);
	var listData=new String();
	var monthArray= new Array 	(	
							   		["January","01"],
							   		["February","02"],
									["March","03"],
									["April","04"],
									["May","05"],
									["June","06"],
									["July","07"],
									["August","08"],
									["September","09"],
									["October","10"],
									["November","11"],
									["December","12"]
								);
	
	element.style.display="block";
	element.innerHTML=currentPage.loadingHTML;
	
	for (var currentNode=0; currentNode<monthArray.length; currentNode++) {
		listData+="	<li onClick=\"document.getElementById('"+inputAreaId+"').value='"+monthArray[currentNode][1]+"'; document.getElementById('"+elementId+"').style.display='none'\">\
						"+monthArray[currentNode][1]+" - "+monthArray[currentNode][0]+"\
					</li>";
	}

	element.innerHTML="	<ul>\
							"+listData+"\
						</ul>";
	
}



function buildCCExperationYearSelector(elementId,inputAreaId) {
	
	var element=document.getElementById(elementId);
	var listData=new String();
	
	element.style.display="block";
	element.innerHTML=currentPage.loadingHTML;
	
	for (var currentYear=currentPage.year; currentYear<currentPage.year+15; currentYear++) {
		listData+="	<li onClick=\"document.getElementById('"+inputAreaId+"').value='"+currentYear.toString().substring(2)+"'; document.getElementById('"+elementId+"').style.display='none'\">\
						"+currentYear+"\
					</li>";
	}

	element.innerHTML="	<ul>\
							"+listData+"\
						</ul>";
	
}

function validate(type) {
	try {
		document.getElementById('joinPageAjaxAnimation').style.display="block";
	}catch(err) {
		
	}
	try {
		document.getElementById('paymentButton').style.display='none';
	}catch(err) {
		
	}
	setTimeout("validationByType('"+type+"');",200);
}

function validationByType(type) {
	var errorMessage=new String();
	var errorCount=0;
	var soapVars=new String();
	var soapMethod=new String();
	var soapReturnNode=new String();
	
	switch(type) {
		case "liveSmartJoinPageStep1" :var joinSponsorID=document.getElementById("joinSponsorID");
			
			user.joinSponsorID=document.getElementById("joinSponsorID").value;
			user.joinSponsorID=joinSponsorID.value=user.joinSponsorID.replace(removeSpacesExpression,"");
			soapVars="	<URL>\
							"+user.joinSponsorID+"\
						</URL>";
			soapMethod="CheckRepURL";
			if(singleNodeAPICall(soapVars,soapMethod,soapMethod+"Result")==false||user.joinSponsorID.length<1) {
				hideAjaxAnimation();
				alert("Unable to locate referrer");
				joinSponsorID.value=user.name;
			}else{
				document.location.href="/"+joinSponsorID.value+"#liveSmartJoinPageStep2";
			}
			
			break;

		case "liveSmartJoinPageStep2" :
		
			var joinFirstName=document.getElementById("joinFirstName");
			var joinLastName=document.getElementById("joinLastName");
			var joinUserName=document.getElementById("joinUserName");
			var joinCompany=document.getElementById("joinCompanyName");
			var joinUserPassword=document.getElementById("joinUserPassword");
			var joinUserPasswordConfirm=document.getElementById("joinUserPasswordConfirm");
			var joinEMail=document.getElementById("joinEMail");
			var joinConfirmEMail=document.getElementById("joinConfirmEMail");
			var joinSSN=document.getElementById("joinSSN");
			var joinBirthMonth=document.getElementById("joinBirthMonth");
			var joinBirthDay=document.getElementById("joinBirthDay");
			var joinBirthYear=document.getElementById("joinBirthYear");
			var joinDayTimePhone=document.getElementById("joinDayTimePhone");
			var joinCellPhone=document.getElementById("joinCellPhone");
			var joinHomePhone=document.getElementById("joinHomePhone");
			var joinFax=document.getElementById("joinFax");
			
			user.firstName=joinFirstName.value=joinFirstName.value.replace(specialCharsExpression,"");
			user.firstName=joinFirstName.value=joinFirstName.value.replace(removeNumbersExpression,"");
			user.lastName=joinLastName.value=joinLastName.value.replace(specialCharsExpression,"");
			user.lastName=joinLastName.value=joinLastName.value.replace(removeNumbersExpression,"");
			user.company=joinCompany.value;
			user.joinUserName=joinUserName.value=joinUserName.value.replace(specialCharsExpression,"");
			user.joinUserName=joinUserName.value=joinUserName.value.replace(removeSpacesExpression,"");
			user.joinUserPassword=joinUserPassword.value=joinUserPassword.value.replace(removeSpacesExpression,"")
			user.joinUserPassword=joinUserPassword.value=joinUserPassword.value.replace(specialCharsExpression,"")
			user.joinEMail=joinEMail.value=joinEMail.value.replace(emailSpecialCharsExpression,"");
			user.joinSSN=joinSSN.value=joinSSN.value.replace(keepOnlyNumbersExpression,"");
			user.joinBirthMonth=joinBirthMonth.value=joinBirthMonth.value.replace(keepOnlyNumbersExpression,"");
			user.joinBirthDay=joinBirthDay.value=joinBirthDay.value.replace(keepOnlyNumbersExpression,"");
			user.joinBirthYear=joinBirthYear.value=joinBirthYear.value.replace(keepOnlyNumbersExpression,"");
			user.joinDayTimePhone=joinDayTimePhone.value=joinDayTimePhone.value.replace(keepOnlyNumbersExpression,"");
			user.joinCellPhone=joinCellPhone.value=joinCellPhone.value.replace(keepOnlyNumbersExpression,"");
			user.joinHomePhone=joinHomePhone.value=joinHomePhone.value.replace(keepOnlyNumbersExpression,"");
			user.joinFax=joinFax.value=joinFax.value.replace(keepOnlyNumbersExpression,"");
			
			if (user.firstName.length<1) {
				errorMessage+="\n\rFirst Name Required";
				errorCount+=1;
			}
			
			if (user.lastName.length<1) {
				errorMessage+="\n\rLast Name Required";
				errorCount+=1;
			}
			
			if (/[A-z]/.test(user.joinUserName)!=true && user.lastName.length>0) {
				errorMessage+="\n\rUserName Must Contain ateast 1 letter";
				errorCount+=1;
			}
			
			soapVars="	<URL>\
							"+user.joinUserName+"\
						</URL>";
			soapMethod="CheckRepURL";
			if(singleNodeAPICall(soapVars,soapMethod,soapMethod+"Result")==true||user.joinUserName.length<1) {
				errorMessage+="\n\rUser Name invalid or taken";
				errorCount+=1;
			}
			
			if (user.joinUserPassword.length<1) {
				errorMessage+="\n\rPassword Required";
				errorCount+=1;
			}
			
			if (user.joinUserPassword!=joinUserPasswordConfirm.value) {
				errorMessage+="\n\rConfirmation Password does not match entered password";
				errorCount+=1;
			}
			
			soapVars="	<Email>\
							"+user.joinEMail+"\
						</Email>";
			soapMethod="ValidateEmail";
			if(singleNodeAPICall(soapVars,soapMethod,soapMethod+"Result")==false) {
				errorMessage+="\n\rE-mail invalid";
				errorCount+=1;
			}
			if (user.joinEMail!=joinConfirmEMail.value) {
				errorMessage+="\n\rE-mails do not match";
				errorCount+=1;
			}
			
			soapVars="	<SSN>\
							"+user.joinSSN+"\
						</SSN>";
			soapMethod="CheckRepSSN";
			if(singleNodeAPICall(soapVars,soapMethod,soapMethod+"Result")==true||user.joinSSN.length<9 && user.joinSSN!=0) {
				errorMessage+="\n\rSSN invalid or already in use ";
				errorCount+=1;
			}
			
			if(user.joinBirthMonth.length<2) {
				errorMessage+="\n\rBirth Month Required";
				errorCount+=1;
			}
			
			if(user.joinBirthDay.length<2) {
				errorMessage+="\n\rBirth Day Required";
				errorCount+=1;
			}
			
			if(user.joinBirthYear.length<4) {
				errorMessage+="\n\rBirth Year Required";
				errorCount+=1;
			}
			
			if(user.joinBirthYear>currentPage.year-17) {
				errorMessage+="\n\rYOU MUST be 18 or OLDER to join";
				errorCount+=1;
			}
			
			if(user.joinDayTimePhone.length<10) {
				errorMessage+="\n\rDaytime phone number invalid";
				errorCount+=1;
			}
			
			if (errorCount>0) {
				hideAjaxAnimation();
				showErrorMessage(errorCount,errorMessage);
			}else{
				if(createOnlineSignup()) {
					if(createEnrollOrder()) {
						user.orderPhone=user.joinDayTimePhone;
						user.orderEMail=user.joinEMail;
						window.location.hash="liveSmartJoinPageStep3";
					}else{
						hideAjaxAnimation();
						alert("Your enroll order was not created.\r\n Please check all of your information and try again.");	
					}
				}else{
					hideAjaxAnimation();
					alert("Your temporary position was not created.\r\n Please check all of your information and try again.");
				}
			}
			
			break;
		
		case "liveSmartJoinPageStep3" :
				
				//skip step 4 here
					window.location.hash="liveSmartJoinPageStep5";
					//window.location.hash="liveSmartJoinPageStep4";
			
			break;
			
		case "liveSmartJoinPageStep4" :
		
			window.location.hash="liveSmartJoinPageStep5";
			
			break;
			
		case "liveSmartJoinPageStep5" :
			
			
			var joinAgreePolicyAndProc=document.getElementById("joinAgreePolicyAndProc");
			var joinAgreeTermsAndConditions=document.getElementById("joinAgreeTermsAndConditions");
			var joinAgreeCompPlan=document.getElementById("joinAgreeCompPlan");
			try{
				var joinAgreeOAP=document.getElementById("joinAgreeOAP");
				user.joinAgreeOAP=joinAgreeOAP.checked;	
			}catch(err){
				user.joinAgreeOAP=true;
			}
			
			user.joinAgreePolicyAndProc=joinAgreePolicyAndProc.checked;
			user.joinAgreeTermsAndConditions=joinAgreeTermsAndConditions.checked;
			user.joinAgreeCompPlan=joinAgreeCompPlan.checked;
			
			if (user.joinAgreePolicyAndProc!=true || user.joinAgreeTermsAndConditions!=true || user.joinAgreeCompPlan!=true || user.joinAgreeOAP!=true) {
				hideAjaxAnimation();
				alert("You MUST Agree to all of the above before you may join");
			}else{
				user.currentMemberNumber=user.joinTempID
				user.orderID=user.joinOrderID
				window.location.hash="liveSmartCheckOut";
			}
			
			break;
			
		case "liveSmartAddressAndShipping" :
		
			var joinBillingAddressOne=document.getElementById("joinBillingAddressOne");
			var joinBillingAddressTwo=document.getElementById("joinBillingAddressTwo");
			var joinBillingZip=document.getElementById("joinBillingZip");
			var joinBillingCity=document.getElementById("joinBillingCity");
			var joinBillingCountry=document.getElementById("joinBillingCountry");
			var joinBillingState=document.getElementById("joinBillingState");
			var joinBillingMethod=document.getElementById("joinBillingMethod");
			
			var joinShippingAddressOne=document.getElementById("joinShippingAddressOne");
			var joinShippingAddressTwo=document.getElementById("joinShippingAddressTwo");
			var joinShippingZip=document.getElementById("joinShippingZip");
			var joinShippingCity=document.getElementById("joinShippingCity");
			var joinShippingCountry=document.getElementById("joinShippingCountry");
			var joinShippingState=document.getElementById("joinShippingState");
			var joinShippingMethod=document.getElementById("joinShippingMethod");
			
			user.joinBillingAddressOne=joinBillingAddressOne.value=joinBillingAddressOne.value.replace(specialCharsExpression,"");
			user.joinBillingAddressTwo=joinBillingAddressTwo.value=joinBillingAddressTwo.value.replace(specialCharsExpression,"");
			user.joinBillingZip=joinBillingZip.value=joinBillingZip.value.replace(specialCharsExpression,"");
			user.joinBillingCity=joinBillingCity.value=joinBillingCity.value.replace(specialCharsExpression,"");
			user.joinBillingCountry=joinBillingCountry.value=joinBillingCountry.value.replace(specialCharsExpression,"");
			user.joinBillingState=joinBillingState.value=joinBillingState.value.replace(specialCharsExpression,"");
			
			
			user.joinShippingAddressOne=joinShippingAddressOne.value=joinShippingAddressOne.value.replace(specialCharsExpression,"");
			user.joinShippingAddressTwo=joinShippingAddressTwo.value=joinShippingAddressTwo.value.replace(specialCharsExpression,"");
			user.joinShippingZip=joinShippingZip.value=joinShippingZip.value.replace(specialCharsExpression,"");
			user.joinShippingCity=joinShippingCity.value=joinShippingCity.value.replace(specialCharsExpression,"");
			user.joinShippingCountry=joinShippingCountry.value=joinShippingCountry.value.replace(specialCharsExpression,"");
			user.joinShippingState=joinShippingState.value=joinShippingState.value.replace(specialCharsExpression,"");
			
			if(user.joinBillingAddressOne.length<3) {
				errorMessage+="\n\rBilling Address 1 is required";
				errorCount+=1;
			}
			
			if(user.joinBillingZip.length<3) {
				errorMessage+="\n\rBilling Zip/Postal Code is required";
				errorCount+=1;
			}else{
				if(user.joinBillingCountry=="USA") {
					soapVars="	<ZipCode>\
									"+user.joinBillingZip+"\
								</ZipCode>";
					soapMethod="ZipLookup_v2";
					var zipData=multiNodeAPICall(soapVars,soapMethod,soapMethod+"Result")
					if(zipData.getElementsByTagName('*').length==0) {
						errorMessage+="\n\rBilling Zip/Postal Code is invalid";
						errorCount+=1;
					}
				}
			}
			
			if(user.joinBillingCity.length<2) {
				errorMessage+="\n\rBilling City is required";
				errorCount+=1;
			}
			
			if(user.joinBillingCountry.length<2) {
				errorMessage+="\n\rBilling Country is required";
				errorCount+=1;
			}
			
			if(user.joinBillingState.length<1) {
				errorMessage+="\n\rBilling State is required";
				errorCount+=1;
			}
			
			if(user.joinShippingAddressSameAsBilling==true) {
				
				user.joinShippingAddressOne=joinShippingAddressOne.value=user.joinBillingAddressOne
			 	user.joinShippingAddressTwo=joinShippingAddressTwo.value=user.joinBillingAddressTwo
			 	user.joinShippingZip=joinShippingZip.value=joinBillingZip.value=user.joinBillingZip
			 	user.joinShippingCity=joinShippingCity.value=joinBillingCity.value=user.joinBillingCity
			 	user.joinShippingCountry=joinShippingCountry.value=user.joinBillingCountry
			 	user.joinShippingState=joinShippingState.value=user.joinBillingState
				
			}else{
				if(user.joinShippingZip.length<3) {
					errorMessage+="\n\rShipping Zip/Postal Codeis required";
					errorCount+=1;
				}else{
					soapVars="	<ZipCode>\
									"+user.joinShippingZip+"\
								</ZipCode>";
					soapMethod="ZipLookup_v2";
					var zipData=multiNodeAPICall(soapVars,soapMethod,soapMethod+"Result")
					if(zipData.getElementsByTagName('*').length==0) {
						errorMessage+="\n\rShipping Zip/Postal Codeis invalid";
						errorCount+=1;
					}
				}
				
				if(user.joinShippingCity.length<2) {
					errorMessage+="\n\rShipping City is required";
					errorCount+=1;
				}
				
				if(user.joinShippingCountry.length<2) {
					errorMessage+="\n\rShipping Country is required";
					errorCount+=1;
				}
				
				if(user.joinShippingState.length<1) {
					errorMessage+="\n\rShipping State is required";
					errorCount+=1;
				}
				
			}
			
			if (errorCount>0) {
				hideAjaxAnimation();
				showErrorMessage(errorCount,errorMessage);
			}else{
				hideAjaxAnimation();
				user.billingAddressOne=user.joinBillingAddressOne
				user.billingAddressTwo=user.joinBillingAddressTwo
				user.billingZip=user.joinBillingZip
				user.billingCity=user.joinBillingCity
				user.billingCountry=user.joinBillingCountry
				user.billingState=user.joinBillingState
				
				
				user.shippingAddressOne=user.joinShippingAddressOne
				user.shippingAddressTwo=user.joinShippingAddressTwo
				user.shippingZip=user.joinShippingZip
				user.shippingCity=user.joinShippingCity
				user.shippingCountry=user.joinShippingCountry
				user.shippingState=user.joinShippingState
				
				if(user.joinShippingAddressSameAsBilling==true) {
					onlineOrderUpdateAddress('ShippingAndBilling');
				}else{
					onlineOrderUpdateAddress('Shipping');
					onlineOrderUpdateAddress('Billing');
				}
				buildShippingOptions('shippingMethodList');
			}
			
			break;
			
		case "liveSmartCheckOut" :
		
			if(user.orderTotal>0) {
				window.location.hash="liveSmartPayment"
			}else{
				hideAjaxAnimation();
				alert("You must supply your billing and shipping information\r\nas well as calculate your Shipping Costs\r\nbefore you may continue.");
			}
		
			break;
			
		case "liveSmartPayment" :
			window.location.hash="liveSmartCheckOutCreditCardInfo"
			break;
		
		case "liveSmartCheckOutCreditCardInfo" :
		
			var creditCardNumber=document.getElementById("creditCardNumber");
			var creditCardExperationYear=document.getElementById("creditCardExperationYear");
			var creditCardExperationMonth=document.getElementById("creditCardExperationMonth");
			var creditCardFirstName=document.getElementById("creditCardFirstName");
			var creditCardLastName=document.getElementById("creditCardLastName");
			var creditCardCVC=document.getElementById("creditCardCVC");
		
			user.creditCardNumber=creditCardNumber.value=creditCardNumber.value.replace(keepOnlyNumbersExpression,"");
			user.creditCardExperationYear=creditCardExperationYear.value
			user.creditCardExperationMonth=creditCardExperationMonth.value
			user.creditCardFirstName=creditCardFirstName.value=creditCardFirstName.value.replace(specialCharsExpression,"");
			user.creditCardLastName=creditCardLastName.value=creditCardLastName.value.replace(specialCharsExpression,"");
			user.creditCardCVC=creditCardCVC.value=creditCardCVC.value.replace(keepOnlyNumbersExpression,"");
			
			if(user.creditCardNumber.length<10) {
				errorMessage+="\n\rCredit Card Number invalid or empty";
				errorCount+=1;
			}else{
				soapVars="	<CreditCardNumber>\
								"+user.creditCardNumber+"\
							</CreditCardNumber>";
				soapMethod="ValidateCCNumber";
				if(singleNodeAPICall(soapVars,soapMethod,soapMethod+"Result")=="false") {
					errorMessage+="\n\rCredit Card Number invalid";
					errorCount+=1;
				}
			}
			
			if(user.creditCardExperationYear.length<2 && user.creditCardExperationYear < currentPage.year.toString().substring(2)) {
				errorMessage+="\n\rExperation Year invalid";
				errorCount+=1;
			}
			
			if(user.creditCardExperationMonth.length<2 || user.creditCardExperationMonth > 12 || user.creditCardExperationMonth < 1) {
				errorMessage+="\n\rExperation Month invalid";
				errorCount+=1;
			}
			
			if(user.creditCardFirstName.length<2) {
				errorMessage+="\n\rFirst Name as it appears on card is required";
				errorCount+=1;
			}
			
			if(user.creditCardLastName.length<2) {
				errorMessage+="\n\rLast Name as it appears on card is required";
				errorCount+=1;
			}
			
			if(user.creditCardCVC.length<3 || user.creditCardCVC.length>4) {
				errorMessage+="\n\rCVC / CVC invalid or blank";
				errorCount+=1;
			}
			
			
			if (errorCount>0) {
				hideAjaxAnimation();
				showErrorMessage(errorCount,errorMessage);
			}else{
				var orderComplete=onlineOrderProcessCreditCard();
				if (orderComplete.getElementsByTagName('TransactionCompleted')[0].childNodes[0].nodeValue=="true") {
					if (user.joinTempID) createRep();
				}else{
					hideAjaxAnimation();
					alert('Failed Transaction :\n'+orderComplete.getElementsByTagName('Description')[0].childNodes[0].nodeValue.replace(/[+]/g,' '));
					document.getElementById('paymentButton').style.display='block';
				}
			}
			
			break;
		
	}	
}

function showErrorMessage(errorCount,errorMessage) {
	alert("Please fix the following "+errorCount+" errors\n\r"+errorMessage);
}


function hideAjaxAnimation() {
	if(document.getElementById('joinPageAjaxAnimation')) document.getElementById('joinPageAjaxAnimation').style.display="none";
	if(document.getElementById('ajaxAnimation')) document.getElementById('ajaxAnimation').style.display="none";
}