function MooPayment(sFormId){
   
    //Constructor  / properties
    var sCurrencySymbol = get('hidCurrencySymbol').value;
	var m_sFormId = sFormId;
	var sWarnings = '';
	
	//set focus
	if (get('hidMooUserId').value ==0 && document.getElementById('txtPurchaserFullName')){
		get('txtPurchaserFullName').focus();
	}else{ 
		if(get("hidProductFree").value !=  true){
			if (get("radioPaymentMethodUs").checked == true){
				if(document.getElementById('txtBillingFullName')){
					get('txtBillingFullName').focus();
				}else if(document.getElementById('txtShippingFullName')){
					get('txtShippingFullName').focus();
				}
			}
		}
	}

    //set total
    this.setTotal=function() {

    	var quantity = parseInt(get('txtPackCount').value);
        var price = parseFloat(get('hidPrice').value);
        var unit_price = parseFloat(get('hidUnitPrice').value);
        var shipping = parseFloat(get('hidShipping').value);
		var shipping_discount = parseFloat(get('hidShippingDiscount').value);
		var unit_shipping = parseFloat(get('hidUnitShipping').value);
		var free_packs = parseInt(get('hidFreePacks').value);

		quantity = quantity - free_packs;
		if (quantity < 0) {
			quantity = 0;
		}

		if (isNaN(quantity)==true) {
		    quantity = 0;
		}

		totalShipping = 0;
		if (shipping > 0) {
			totalShipping = unit_shipping * quantity;
		}
		
		
		
		totalCost = (quantity * unit_price);
		grandTotalCost = (quantity * unit_price) + totalShipping;
		

		if (totalCost < 0 || isNaN(totalCost) ==true){    
			oMooDebug = new MooDebug();
			oMooDebug.error("Pack price not calculating correctly", false);
		}
		if (quantity >= 0){
			get('sTotal').innerHTML = sCurrencySymbol + totalCost.toFixed(2);
			get('sShipping').innerHTML = sCurrencySymbol + totalShipping.toFixed(2);
			get('sGrandTotal').innerHTML = sCurrencySymbol + grandTotalCost.toFixed(2);
		}
    }
    
    //enable/disable Shipping
    this.enableDisableShipping=function (){

		var aControls = new Array ("txtShippingFullName","txtShippingAddress1","txtShippingAddress2","txtShippingTownCity", "ddlShippingCountry", "txtShippingState","txtShippingPostcode");
		
	    var sDisabled = '';
	    if (get('txtShippingFullName').disabled == ''){
	       sDisabled = 'disabled';
	    }
	
	    for (var i=aControls.length-1; i > -1; i--) {
			var oControl = get(aControls[i]);
			oControl.disabled=sDisabled;
			if (oControl.className.indexOf(" disabled") >0){
				oControl.className = oControl.className.replace(" disabled", "")
			}else{
				oControl.className += " disabled";
			}
		}

    }
    
    //validation
    this.validate=function (){
    	//Clear any previous warnings
    	sWarnings = ''
	   
	    var bShippingAsBilling = false;
		var bProductfree = get("hidProductFree").value;
		var checkingPostcode = false;
		var sPost = '';
	
	    if (bProductfree == false && get('chkShippingAsBilling').checked){
	       bShippingAsBilling = true;
	    }
	
	    //validate controls
		if(bProductfree == false){
		    if (textboxNotEmpty(get('txtPackCount'), true)==false) {
		        sWarnings += '你想做多少套?\n';
		    }
		    var quantity = parseInt(get('txtPackCount').value);
			if (quantity <= 0)
			{
			 	sWarnings += '请输入一个有效数字\n';
			}
		}
		

		if (get("hidMooUserId").value ==0 && get("radAccountMode_new").checked == true){
			if (textboxNotEmpty(get('txtPurchaserFullName'), true)==false) {
		        sWarnings += 'Please enter your name\n';
		    }

			if (textboxIsEmail(get('txtPurchaserEmail'), true)==false) {
		        sWarnings += '请输入你的Email\n';
		    }
			if (textboxNotEmpty(get('txtPurchaserPassword1'), true)==false | textboxNotEmpty(get('txtPurchaserPassword2'), true)==false) {
		        sWarnings += '请输入密码\n';
		    }
			if (get('txtPurchaserPassword1').value != get('txtPurchaserPassword2').value){
				sWarnings += "抱歉, 你输入的密码不正确\n";
			}	
		}
		
		if (get("hidMooUserId").value ==0 && get("radAccountMode_return").checked == true){
			sWarnings += "登录或者创建新的用户\n";		
		}

		if (bProductfree == false && get("radioPaymentMethodUs").checked == true) {
			// Process the rest of the form.

			if (bProductfree == false){
				if (!document.getElementById('ddlBillingUsed') || get('ddlBillingUsed').value == 'new') {

		       	 if (textboxNotEmpty(get('txtBillingFullName'), true)==false) {
			   	        sWarnings += '请输入在你的卡上输入你的名字n';
			   	    }
	
			   	    if (textboxIsNumeric(get('txtBillingCardNumber'), true)==false) {
			   	        sWarnings += '请输入你的卡号\n';
			   	    }
	
			   	    if (dropdownNotEmpty(get('ddlBillingExpiryMonth'), true)==false | dropdownNotEmpty(get('ddlBillingExpiryYear'), true)==false) {
			   	        sWarnings += '请输入你卡片的有效期限\n';
			   	    }
			   	    if (textboxIsNumeric(get('txtBillingSecurityCode'), true)==false) {
			   	        sWarnings += '请键入在你的卡片后面安全码\n';
			   	    }

			   	    if (document.getElementById('ddlBillingCardType').value ==""){
			   	        sWarnings += '请输入卡片的类型\n';
			   	    }
	
			   	    if (document.getElementById('ddlBillingCardType').value =="Switch/Solo/Maestro"){
						bWarnControls = false;
			    	    if ((dropdownNotEmpty(get('ddlBillingStartDateMonth'), false)==false | dropdownNotEmpty(get('ddlBillingStartDateYear'), false)==false) && textboxIsNumeric(get('txtBillingIssueNumber'), false)==false) {
			    	        sWarnings += '请输入你卡片的制作时间\n';
							bWarnControls = true;
			    	    }
						setControlWarning(get("ddlBillingStartDateMonth"), bWarnControls);
						setControlWarning(get("ddlBillingStartDateYear"), bWarnControls);
						setControlWarning(get("txtBillingIssueNumber"), bWarnControls);
			   	    }
	
				     if (textboxNotEmpty(get('txtBillingAddress1'), true)==false) {
				        sWarnings += '请输入你的第一地址\n';
				    }
				    if (textboxNotEmpty(get('txtBillingTownCity'), true)==false) {
				        sWarnings += '请输入你所在城市\n';
				    }
				    if (get('ddlBillingCountry').value == 'United States') {
				        if (textboxNotEmpty(get('txtBillingState'),true)==false){
				            sWarnings += '请输入计费状态\n';
				        }
				    }
				    if (dropdownNotEmpty(get('ddlBillingCountry'), true)==false) {
				        sWarnings += '请输入你的运输国家 \n';
				    }
				    if (textboxNotEmpty(get('txtBillingPostcode'), false)==false) 
					{
						//Lets check and see if this country requires a postcode with the address
						sPost += "billingcountry=" + get('ddlBillingCountry').value;
						checkingPostcode = true;
					}	
				    if (get('ddlBillingCountry').value == 'United States') {
				        if (get('txtBillingPostcode').value.length > 5){
				            sWarnings += '请输入你的邮编\n';
							setControlWarning(get('txtBillingPostcode'), true);
				        }
				    }
				}
			}
 
		    //Shipping
		    if ((bProductfree == true)  || (bProductfree == false && bShippingAsBilling != true)){
		      if (textboxNotEmpty(get('txtShippingFullName'), true)==false) {
		          sWarnings += '请输入你的名字\n';
		      }
		      if (textboxNotEmpty(get('txtShippingAddress1'), true)==false) {
		          sWarnings += '请输入你的收货地址 \n';
		      }
		      if (textboxNotEmpty(get('txtShippingTownCity'), true)==false) {
		          sWarnings += '请输入所在城市\n';
		      }
		      if (get('ddlShippingCountry').value == 'United States') {
		          if (textboxNotEmpty(get('txtShippingState'),true)==false){
		              sWarnings += '请输入送货方式\n';
		          }
		      }
		      if (dropdownNotEmpty(get('ddlShippingCountry'), true)==false) {
		          sWarnings += '请输入你的运输国家 \n';
		      }
		      if (textboxNotEmpty(get('txtShippingPostcode'), true)==false) {
		      	//Lets check and see if this country requires a postcode with the address
				if (sPost != '')
				{
					sPost += '&';
				}
				sPost += "shippingcountry=" + get('ddlShippingCountry').value;
				checkingPostcode = true;
		      }
		    }
	    }
	    
	    	
		if (checkingPostcode)
		{
			//If we are in here it's because we have an empty required postcode field.
			//The warnings will get shown on callback from the request
			var oAjax = new Ajax.Request('http://gordon.office.moo.com/ajaxrequests/requiresPostcode.php', {method: 'post',  postBody: sPost,  onComplete: this.postcodeCheckCallback});
		}
		else
		{
	    	//for test	    
	    	if (sWarnings !=''){
            	showWarning(sWarnings);
        	}else{
				postBackForm(sFormId);
			}
		}
    }

	this.changeAccountMode = function (sMode){

		if (sMode=="return"){
			get("txtPurchaserFullName").disabled = "disabled";
			get("txtPurchaserFullName").value = "";
			setControlWarning(get("txtPurchaserFullName"), false);
			get("txtPurchaserEmail").disabled = "disabled";
			get("txtPurchaserEmail").value = "";
			setControlWarning(get("txtPurchaserEmail"), false);
			get("txtPurchaserPassword1").disabled = "disabled";
			get("txtPurchaserPassword1").value = "";
			setControlWarning(get("txtPurchaserPassword1"), false);
			get("txtPurchaserPassword2").disabled = "disabled";		
			get("txtPurchaserPassword2").value = "";
			get("txtScreenName").disabled = "disabled";		
			get("txtScreenName").value = "";
			setControlWarning(get("txtPurchaserPassword2"), false);			
			get("txtSignInEmail").disabled = false;
			get("txtSignInPassword").disabled = false;				
			get("btnLogin").disabled = false;	
			get("aPasswordReset").className="";
		}else{
			get("txtPurchaserFullName").disabled = "";
			get("txtPurchaserEmail").disabled = "";
			get("txtPurchaserPassword1").disabled = "";
			get("txtPurchaserPassword2").disabled = "";
			get("txtSignInEmail").disabled = "disabled";			
			get("txtSignInEmail").value = "";			
			get("txtScreenName").disabled = "";				
			get("txtSignInPassword").disabled = "disabled";		
			get("txtSignInPassword").value = "";		
			get("btnLogin").disabled = true;
			get("aPasswordReset").className="disabledlink";
		}

	}

	this.cardTypeChanged = function(oCardType){

		bDisabled = false;
		if(oCardType.value =="Switch/Solo/Maestro"){
			bDisabled = true;
		}

		setControlDisabled(get("ddlBillingStartDateMonth"), bDisabled);
		setControlDisabled(get("ddlBillingStartDateYear"), bDisabled);
		setControlDisabled(get("txtBillingIssueNumber"), bDisabled);
	}
    
    this.currencyChanged = function (sCurrency){
		get("hidShopChanged").value = sCurrency;
		postBackForm("frmPayment");
	}
	
	this.hidePaymentInfo = function(hide_flag) {
		if (hide_flag==true) {
			get('divPaymentInfo').className="hide";
			get('btnPaymentDone').value="Continue on PayPal >";
		}
		else {
			get('divPaymentInfo').className="";
  		    get('btnPaymentDone').value="Preview >";		
		}
	}
	
	this.disableCardDetails = function() {
		val = get('ddlBillingUsed').value;
		
		switches = new Array('txtBillingFullName','ddlBillingCardType','txtBillingCardNumber','ddlBillingExpiryMonth','ddlBillingExpiryYear','txtBillingSecurityCode','chkRememberDetails');
	
		if (val != 'new') {
			for (i = 0; i < switches.length; i++) {
				get(switches[i]).disabled = true;
				get(switches[i]).className += ' disabled';
			}
		}
		else {
			for (i = 0; i < switches.length; i++) {
				get(switches[i]).disabled = false;
				get(switches[i]).className = get(switches[i]).className.replace(' disabled', '');
			}
		}
	}
    
    this.postcodeCheckCallback = function(originalRequest)
    {
    	if (originalRequest.responseText == "null"){
			oMooDebug = new MooDebug();
			oMooDebug.error("Empty JSON response when trying to check postcode");
		}
		//get the response and convert to js
		var oResponse = originalRequest.responseText;
		if (oResponse == 1)
		{
			sWarnings += '请输入你的运输邮政或邮政编码\n';
		}
		else if (oResponse == 2)
		{
			sWarnings += '请输入你的运输邮政或邮政编码 \n';
		}
		else if (oResponse == 3)
		{
			sWarnings += '请输入你的运输邮政或邮政编码\n';
			sWarnings += '请输入你的运输邮政或邮政编码\n';
		}
		
		if (sWarnings !=''){
			//make sure we show the red box around postcode field
	    	textboxNotEmpty(get('txtBillingPostcode'), true);
            showWarning(sWarnings);
        }else{
			postBackForm(sFormId);
		}
    }
    
}