var frm = document.makePayment;

//var tabsArray = new Array("SOrderDetails","SCustomerInfo","SDeliveryInfo","SProcessPayment");
var mainMenu = ['LatestPromotion','OnlineCtg','MakePayment','TrackOrder','DeliveryZone','faq'];
var subMenu = ['subLatestPromotion','subOnlineCatalog'];
var subSubMenu = ['subWellnessGroup'];

function initPage() {
	show( 'SectionOrderDetail' );
	hide( 'SectionCustomerInfomation' );	
	var sectionName = document.updateCart.pageSection.value;
//	initPageLoading(document.getElementById(sectionName));
	if (sectionName == "SCustomerInfo") {
		showCustomerInfo();
		if (document.customerInfo.validMember.value != "") {
			if(document.customerInfo.validMember.value == "F" || document.customerInfo.validMember.value == "X") {
				show( 'SectionMemberVerification' );
				hide( 'SectionCustomerDetails' );
				document.verifyMember.isMember[0].checked = true;
			} else {
				hide( 'SectionMemberVerification' );
				show( 'SectionCustomerDetails' );
				document.verifyMember.isMember[0].checked = true;			
			}
		} else {
			hide( 'SectionMemberVerification' );
			show( 'SectionCustomerDetails' );
			document.verifyMember.isMember[1].checked = true;
		}
//		showCustomerDetails(document.getElementById("isMemberValue"));		
	} else if (sectionName == "SDeliveryInfo") {
		showDeliveryInfo();
//		var objName = document.getElementById("isSameAddress");
//		objName.checked = true;
	} else if (sectionName == "SProcessPayment") {
		showMakePayment();
	}
}

function processUpdateCart(inValue) {
	var numericFields = "'qty'";		
	if (ValidateNumericFields (numericFields)) {
		if (inValue == 2) 
			document.updateCart.pageSection.value = "CustomerInfo";
		document.updateCart.submit();
	}
}

function showCustomerInfo() {
	hide( 'SectionOrderDetail' );
	show( 'SectionCustomerInfomation' );
	hide( 'SectionMemberVerification' );
	document.updateCart.pageSection.value = "SCustomerInfo";
	var sectionName = document.updateCart.pageSection.value;
//	initPageLoading(document.getElementById(sectionName));		
}

function showCustomerDetails(obj) {
	document.verifyMember.isMemberValue.value = obj.value;
	hide( 'SectionOrderDetail' );
	show( 'SectionCustomerInfomation' );	
	if (obj.value == "SY" || obj.value == "0" || document.verifyMember.isMember[0].checked) {
		show( 'SectionMemberVerification' );
		hide( 'SectionCustomerDetails' );
		document.verifyMember.isMember[0].checked = true;
	} else if (obj.value == "SN" || obj.value == "1" || document.verifyMember.isMember[1].checked) {
		hide( 'SectionMemberVerification' );
		show( 'SectionCustomerDetails' );
		document.verifyMember.isMember[1].checked = true;
	}
}

function showDeliveryInfo() {
	hide( 'SectionOrderDetail' );
	hide( 'SectionCustomerInfomation' );
	hide( 'SectionMemberVerification' );
	hide( 'SectionCustomerDetails' );		
	show( 'SectionDeliveryInfomation' );	
	show( 'SectionSameDeliveryDetails' );
	var nowDate = todayDate("F");
	inputValueText(document.sameRecipient.recipientPreferredDate, getDeliveryDate(nowDate, "F"));

	var sectionName = document.updateCart.pageSection.value;
//	alert(sectionName);
//	initPageLoading(document.getElementById(sectionName));		
}

function showMakePayment() {
	hide( 'SectionOrderDetail' );
	hide( 'SectionCustomerInfomation' );
	hide( 'SectionMemberVerification' );
	hide( 'SectionCustomerDetails' );		
	hide( 'SectionDeliveryInfomation' );	
	hide( 'SectionSameDeliveryDetails' );		
	hide( 'SectionDeliveryDetails' );		
	show( 'SectionMakePayment' );		
	var sectionName = document.updateCart.pageSection.value;
//	initPageLoading(document.getElementById(sectionName));		
}

function checkSameAddress(obj) {
	if (obj.value == "Y") {
		show( 'SectionSameDeliveryDetails' );
		hide( 'SectionDeliveryDetails' );
	} else {
		hide( 'SectionSameDeliveryDetails' );
		show( 'SectionDeliveryDetails' );
	}	

}

function validateInputDate(obj){
	var nowDate = todayDate("C");
	var inputDeliveryDate = obj.value;
	var deliveryDate = inputDeliveryDate.substring(6,10)+inputDeliveryDate.substring(3,5)+inputDeliveryDate.substring(0,2);
			
	if(isValidDate(obj.value)) {
		if (deliveryDate < nowDate) {
			alert ("You are not allowed to backdate the preferred delivery date.");
			obj.select();
		}
	} else {
		obj.select();
	}
}

function formValidation(formName){
    var fields = "";
    var numericFields = "";

    if (formName == "verifyMember") {
	    fields = "'memberCardNo','memberICNo'";
		if (ValidateFields (fields)) {
			document.verifyMember.submit();
		}
	}
    if (formName == "customerInfo") {
       // remove secret id as mandatory field
	   // fields = "'fullName','address','postalCode','telNo','email','secretId'";
	   	fields = "'fullName','address','postalCode','telNo','email'";
		if (ValidateFields (fields)) {
			if (isEmailFormat(document.customerInfo.email.value)){
				document.customerInfo.submit();
			} else {
				alert("Invalid Email Address Format.");
				document.customerInfo.email.select();
				document.customerInfo.email.focus();
			}
		}
	}
	if (formName == "sameRecipient") {
			var nowDate = todayDate("C");
			var inputDeliveryDate = document.getElementById("recipientPreferredDate").value;
			var deliveryDate = inputDeliveryDate.substring(6,10)+inputDeliveryDate.substring(3,5)+inputDeliveryDate.substring(0,2);
			
		    fields = "'recipientName','recipientAddress','recipientPostalCode','recipientTown','recipientState',"+
		    		"'recipientTelNo','recipientPreferredDate','recipientSenderName','recipientMessage'";
		    		
		    numericFields = "'recipientPostalCode','recipientTelNo'";		
		if (ValidateFields (fields)) {
			if (ValidateNumericFields (numericFields)) {
				if (deliveryDate < nowDate) {
					alert ("You are not allowed to backdate the preferred delivery date.");
					document.getElementById("recipientPreferredDate").select();
				} else {
					document.sameRecipient.submit();
				}
			}
		}
	}

	if (formName == "multipleRecipients") {
	    fields = "'multipleRecipientName','multipleAddress','multiplePostalCode','multipleTown','multipleState',"+
	    		"'multipleTelNo','senderName','senderMessage'";
		var totalItems = document.multipleRecipients.itemNo.value;

		if (totalItems > 1) {
			for (var i=0; i < (totalItems - 1); i++) {
				fields = fields + (",'multipleRecipientName_"+i+"','multipleAddress_"+i+"','multiplePostalCode_"+i+"','multipleTown_"+i+
	    		"','multipleState_" +i+ "','multipleTelNo_"+ i + "','senderName_"+i+"','senderMessage_"+i+"'");
			}
		}
	
		if (ValidateFields (fields)) {
			document.multipleRecipients.submit();
		}
	}
	
	if (formName == "commercePayment") {
		var nowDate = todayDate("C").substring(0,6);
		var expDate =(document.commercePayment.expiryDate.value).substring(3) + (document.commercePayment.expiryDate.value).substring(0,2);
	    fields = "'CARDNAME','CARDNO','CARDTYPE','expiryDate','verificationCode','CUSTEMAIL'";
		    		
	    numericFields = "'verificationCode'";		
		if (ValidateFields (fields)) {
			if(isCreditCardFormat(document.commercePayment.CARDNO.value)){
				if(isCreditCardExpiryDateFormat(document.commercePayment.expiryDate.value)) {
					if (ValidateNumericFields (numericFields)) {
						if (isEmailFormat(document.commercePayment.CUSTEMAIL.value)){
							if (expDate > nowDate) {
								document.commercePayment.MERCHANT_TRANID.value = document.sameRecipient.uniqueCode.value;
								document.commercePayment.CARDCVC.value = document.commercePayment.verificationCode.value;
								document.commercePayment.AMOUNT.value = (document.commercePayment.amount.value).replace('S','');							
								document.commercePayment.REMOTEIP.value = (document.commercePayment.remoteAddr.value).replace('S','');	
								document.commercePayment.EXPIRYMONTH.value = (document.commercePayment.expiryDate.value).substring(0,2);								
								document.commercePayment.EXPIRYYEAR.value = (document.commercePayment.expiryDate.value).substring(3);															
								document.commercePayment.FRAUDRISK_EMAIL.value = document.commercePayment.CUSTEMAIL.value;
//								alert(document.commercePayment.MERCHANTID.value);
//								alert(document.commercePayment.CARDTYPE.value);							
//								alert(document.commercePayment.REMOTEIP.value);							
//								alert(document.commercePayment.AMOUNT.value);														
//								alert(document.commercePayment.EXPIRYMONTH.value);														
//								alert(document.commercePayment.EXPIRYYEAR.value);														
//								alert(document.commercePayment.CARDNO.value);														
//								alert(document.commercePayment.CARDNAME.value);																																										
								document.commercePayment.action="https://merchant.commercepayment.com/HttpPaymentSubmit.jsp";
//								document.commercePayment.action="https://merchant.commercepayment.com/PaymentWindowStd_RM.jsp";
								document.commercePayment.method="Post";
								document.commercePayment.submit();
							} else {
								alert("Invalid Expiry Date.");
								document.commercePayment.expiryDate.select();
								document.commercePayment.expiryDate.focus();
							}
						} else {
							alert("Invalid Email Address Format.");
							document.commercePayment.CUSTEMAIL.select();
							document.commercePayment.CUSTEMAIL.focus();
						}
					}
				} else {
					alert("Invalid Expiry Date Format.");
					document.commercePayment.expiryDate.select();
					document.commercePayment.expiryDate.focus();
				}
			} else {
				alert("Invalid Credit Card Number Format.");
				document.commercePayment.CARDNO.select();				
				document.commercePayment.CARDNO.focus();					
			}
		}
	}
}
