/* WWWWHITTARD Checkout Scripts */

var checkoutAjaxPath="/ajax/checkout_ajax_subroutines.raa";

/**********************************************************************************/

/* Basket Remove Item */

function basketRemoveItem(theTargetInputId,callingMode) {
	var theTargetForm=document.getElementById("BASKETFORM");
	var theTargetInput=document.getElementById(theTargetInputId);
	theTargetInput.value=0;

	var theTargetInputArray=theTargetInputId.split("_");

	var theTargetSkuInput="";
	var theTargetSkuId="";

	if(callingMode==0) {
		theTargetSkuInput=document.getElementById("CATALOGUEITEMSKU_"+theTargetInputArray[1]);
		theTargetSkuId=theTargetSkuInput.value;

		for (var i=0; i<theTargetForm.length; i++) {
			
			var theTargetSubElementId=theTargetForm.elements[i].id;
			
			if(theTargetSubElementId !="") {

				var theTargetSubElement=document.getElementById(theTargetSubElementId);
						
				if(theTargetSubElement.type=="hidden" && theTargetSubElement.value==theTargetSkuId) {
					var theTargetSubElementQuantityId=theTargetForm.elements[i].id.toString();
					theTargetSubElementQuantityId=theTargetSubElementQuantityId.replace(/ITEMSKU/gi,"ITEMQTY");
					var theTargetSubElementQuantityInput=document.getElementById(theTargetSubElementQuantityId);
					theTargetSubElementQuantityInput.value=0;


				}
			}
		}
	}
	
	theTargetForm.submit();
}

/**********************************************************************************/

/* Basket Proceed To Checkout */

function basketProceedToCheckout() {

	var theTargetForm=document.getElementById("CATALOGUEITEMFORM_1");
	var theTargetInput=document.getElementById("BASKETREQUEST");

	theTargetInput.value="PROCEEDTOCHECKOUT";	/* Define Request */
	theTargetForm.submit();				/* Submit Form */

}


/**********************************************************************************/

/* Pop Delivery Calculator */

function popDeliveryCalculator() {
	var newWindow=window.open('/store/delivery-charge-estimator.raa','deliveryCalculatorWindow','width=525,height=350,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,copyhistory=no,resizable=no')
	newWindow.focus();
}

/**********************************************************************************/

/* Customer Account Sign In / New Account Creation Form Validation */

function evalCheckoutCustomerAccountSignInOrRegisterForm(callingMode) {
	
	var theTargetForm=document.getElementById("CHECKOUTCUSTOMERACCOUNTSIGNINFORM");
	var theTargetInput=document.getElementById("CHECKOUTREQUEST");

	/* Test If Checkout Mode */

	var theCheckoutTargetInput=document.getElementById("CHECKOUTREQUEST");

	if(theCheckoutTargetInput) {
		theTargetInput=theCheckoutTargetInput; /* Change Target Input */
	}

	var customerAccountEmailAddress=document.getElementById("EMAIL1");
	var customerAccountPassword=document.getElementById("PASSWORD");

	callingMode=callingMode.toUpperCase(); /* Upper Case To Ensure Testing Like With Like */

	/* Update Calling Mode To Form Submission */

	theTargetInput.value=callingMode;
	
	theTargetForm.submit();	/* Submit Form */
}


/**********************************************************************************/

/* Checkout Delivery Type Switch */

function checkoutDeliveryTypeSwitch(callingSource) {

	var theSingleAddressInput=document.getElementById("SINGLEADDRESS");
	var theMultipleAddressInput=document.getElementById("MULTIPLEADDRESS");

	if(callingSource.id=="SINGLEADDRESS") {
			theSingleAddressInput.checked=true;
			theMultipleAddressInput.checked=false;
	}

	if(callingSource.id=="MULTIPLEADDRESS") {
			theSingleAddressInput.checked=false;
			theMultipleAddressInput.checked=true;
	}
}

/**********************************************************************************/

/* Checkout Delivery Type Submit */

function checkoutDeliveryTypeSubmit() {
	var theTargetForm=document.getElementById("CHECKOUTDELIVERYFORM");
	var theTargetRequestInput=document.getElementById("CHECKOUTREQUEST");

	theTargetRequestInput.value="CHECKOUTDELIVERYTYPESELECT";	/* Define Request */
	theTargetForm.submit();						/* Submit Form */
	
}

/**********************************************************************************/

/* Preload Checkout Arrow Images */

var checkoutArrowRightImage=new Image(); checkoutArrowRightImage.src="/images/global/checkout_personalisation_arrow_right.gif";
var checkoutArrowDownImage=new Image(); checkoutArrowDownImage.src="/images/global/checkout_personalisation_arrow_down.gif";

/* Checkout Show Personalisation Line */

function showPersonalisationLine(theDeliveryId,theSkuId,theSkuQuantityId) {

	var theTargetElement=document.getElementById("SKUPERSONALISATIONCONTAINER_"+theDeliveryId+"_"+theSkuId+"_"+theSkuQuantityId);
	var theTargetArrowElement=document.getElementById("SKUPERSONALISATIONCONTAINERARROW_"+theDeliveryId+"_"+theSkuId+"_"+theSkuQuantityId);

	if(theTargetElement.style.display.toString()=="none") {
		theTargetElement.style.display="inline";
		theTargetArrowElement.src="/images/global/checkout_personalisation_arrow_down.gif";
	} else {
		theTargetElement.style.display="none";
		theTargetArrowElement.src="/images/global/checkout_personalisation_arrow_right.gif";
	}
}

/**********************************************************************************/

/* Checkout Delivery Details Update */

function checkoutDeliveryDetailsUpdate() {

	var theTargetForm=document.getElementById("CHECKOUTDELIVERYFORM");
	var theTargetRequestInput=document.getElementById("CHECKOUTREQUEST");

	theTargetRequestInput.value="CHECKOUTDELIVERYDETAILSUPDATE";	/* Define Request */
	theTargetForm.submit();					/* Submit Form */

}

/**********************************************************************************/

/* Checkout Validate Message Input Length */

function checkoutValidateMessageLength(theInputId,theMaxMessageLength) {

	var theTargetInput=document.getElementById(theInputId);

	if(theTargetInput.value.length > theMaxMessageLength) {
		theTargetInput.value=theTargetInput.value.substr(0,theMaxMessageLength);
	}	
}

/**********************************************************************************/

/* Checkout Validate Default Message */

function checkoutValidateDefaultMessage(theInputId) {

	var theTargetInput=document.getElementById(theInputId);
	var theInputValue=theTargetInput.value;

	if(theInputValue=="e.g Happy Birthday Mum" || theInputValue=="e.g Leave With Number 12 If Out") {
			theTargetInput.value="";
	}

}

/**********************************************************************************/

/* Checkout Add New Delivery Details */

function checkoutAddNewDeliveryDetails() {

	var theTargetForm=document.getElementById("CHECKOUTDELIVERYFORM");
	var theTargetRequestInput=document.getElementById("CHECKOUTREQUEST");

	theTargetRequestInput.value="CHECKOUTADDNEWDELIVERYDETAILS";	/* Define Request */
	theTargetForm.submit();						/* Submit Form */
}

/**********************************************************************************/

/* Remove Delivery Details */

function checkoutRemoveDeliveryDetails(deliveryIdInput) {

	var theTargetForm=document.getElementById("CHECKOUTDELIVERYFORM");
	var theTargetRequestInput=document.getElementById("CHECKOUTREQUEST");
	var theTargetRequestDataInput=document.getElementById("CHECKOUTREQUESTDATA");

	var theSourceDataInput=document.getElementById(deliveryIdInput);

	theTargetRequestInput.value="CHECKOUTREMOVEDELIVERYDETAILS";	/* Define Request */
	theTargetRequestDataInput.value=theSourceDataInput.value;

	theTargetForm.submit();						/* Submit Form */

}

/**********************************************************************************/

/* Move Delivery Item To Other Delivery */

function checkoutMoveDeliveryItem(moveToDeliveryIdInput) {

	var theTargetForm=document.getElementById("CHECKOUTDELIVERYFORM");
	var theTargetRequestInput=document.getElementById("CHECKOUTREQUEST");
	var theTargetRequestDataInput=document.getElementById("CHECKOUTREQUESTDATA");

	var theSourceDataInput=document.getElementById(moveToDeliveryIdInput);

	if(theSourceDataInput.value !="") {
		theTargetRequestInput.value="CHECKOUTMOVEDELIVERYITEM";		/* Define Request */
		var theTargetRequestSubmitData=theSourceDataInput.id+"~"+theSourceDataInput.value;	/* Which Item | Where To Move */
		theTargetRequestDataInput.value=theTargetRequestSubmitData;
		theTargetForm.submit();						/* Submit Form */
	} else {
		alert("Please select a delivery to move this item to.");
	}
}

/**********************************************************************************/

/* Checkout Create New Empty Delivery - MDA Mode 1 Delivery Only Present */

function checkoutCreateNewEmptyDelivery() {
		var theTargetInput=document.getElementById("CHECKOUTSCROLLPOSITIONCACHE");
		theTargetInput.value=9999;
		checkoutAddNewDeliveryDetails();
}

/**********************************************************************************/

/* Checkout Delivery Details Update */

function checkoutDeliveryDetailsUpdate() {

	var theTargetForm=document.getElementById("CHECKOUTDELIVERYFORM");
	var theTargetRequestInput=document.getElementById("CHECKOUTREQUEST");

	theTargetRequestInput.value="CHECKOUTDELIVERYDETAILSUPDATE";	/* Define Request */
	theTargetForm.submit();						/* Submit Form */

}

/**********************************************************************************/

/* Checkout Delivery Details Submit */

function checkoutDeliveryDetailsSubmit() {

	var theTargetForm=document.getElementById("CHECKOUTDELIVERYFORM");
	var theTargetRequestInput=document.getElementById("CHECKOUTREQUEST");

	theTargetRequestInput.value="CHECKOUTDELIVERYDETAILSUBMIT";	/* Define Request */
	theTargetForm.submit();						/* Submit Form */

}


/**********************************************************************************/

/* Checkout Back To Checkout Start From Delivery Details */

function checkoutBackFromDeliveryDetails() {

	var theTargetForm=document.getElementById("CHECKOUTDELIVERYFORM");
	var theTargetRequestInput=document.getElementById("CHECKOUTREQUEST");

	theTargetRequestInput.value="CHECKOUTMDASELECTBACK";		/* Define Request */
	theTargetForm.submit();						/* Submit Form */

}


/**********************************************************************************/

/* Checkout Add New Delivery Address */

function checkoutAddNewDeliveryAddress(deliveryId) {

	var theTargetForm=document.getElementById("CHECKOUTDELIVERYFORM");
	var theTargetRequestInput=document.getElementById("CHECKOUTREQUEST");
	var theTargetRequestDataInput=document.getElementById("CHECKOUTREQUESTDATA");

	theTargetRequestInput.value="ADDNEWDELIVERYADDRESS";		/* Define Request */
	theTargetRequestDataInput.value=deliveryId;			/* Define Delivery ID To Add New Address To */

	theTargetForm.submit();						/* Submit Form */

}

/**********************************************************************************/

/* Checkout Cancel Add New Delivery Address */

function checkoutCancelAddNewDeliveryAddress() {
	var theTargetForm=document.getElementById("CHECKOUTADDRESSFORM");
	var theTargetRequestInput=document.getElementById("CHECKOUTREQUEST");

	theTargetRequestInput.value="CHECKOUTDELIVERYSUMMARY";		/* Define Request */
	theTargetForm.submit();						/* Submit Form */
}

/**********************************************************************************/

/* Checkout Save New Address Book entry (Delivery & Billing) */

function checkoutSaveNewAddressBookEntry() {
	var theTargetForm=document.getElementById("CHECKOUTADDRESSFORM");
	var theTargetRequestInput=document.getElementById("CHECKOUTREQUEST");

	theTargetRequestInput.value="CHECKOUTSAVEADDRESSBOOKENTRY";		/* Define Request */
	theTargetForm.submit();							/* Submit Form */

}

/**********************************************************************************/

/* Checkout Stage 2 Server Time Ajax Page Update */

function updateCheckoutPageServerTime() {

	var submitUrl=checkoutAjaxPath;
     	var xmlHttpReq = false;
    	var thisAjaxRequest =this;

	var theTargetInput=document.getElementById("CHECKOUTPAGESERVERTIME");

    	if (window.XMLHttpRequest) {
        	thisAjaxRequest.xmlHttpReq = new XMLHttpRequest(); 				/* Fire Fox */
    	} else {
		if (window.ActiveXObject) {
        		thisAjaxRequest.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); 	/* IE */
		}
    	}
	
	if(thisAjaxRequest.xmlHttpReq) {

		thisAjaxRequest.xmlHttpReq.open('POST', submitUrl, true);
    		thisAjaxRequest.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

		var submitString="CHECKOUTREQUEST=UPDATECHECKOUTPAGESERVERTIME";
  
		self.xmlHttpReq.onreadystatechange = function() {
        		if (self.xmlHttpReq.readyState == 4) {
				theTargetInput.value=self.xmlHttpReq.responseText;
        		}
    		}
    
		thisAjaxRequest.xmlHttpReq.send(submitString);
	}

}

/**********************************************************************************/

/* Checkout Scroll To Postion Cache And Retrieval */

function cacheScreenPostion() {
	var theTargetInput=document.getElementById("CHECKOUTSCROLLPOSITIONCACHE");
	theTargetInput.value=document.body.scrollTop;
}

function scrollToScreenPostion(screenPosition) {

	if(!isNaN(screenPosition)) {
		document.body.scrollTop=screenPosition;
	}
	
}

/**********************************************************************************/

/* Checkout Proceed To Payment */

function checkoutProceedToPayment() {
	var theTargetForm=document.getElementById("CHECKOUTDELIVERYFORM");
	var theTargetRequestInput=document.getElementById("CHECKOUTREQUEST");

	theTargetRequestInput.value="CHECKOUTPAYMENTMAIN";		/* Define Request */
	theTargetForm.submit();						/* Submit Form */
}


/**********************************************************************************/

/* Checkout Back From Order Review */

function checkoutBackFromOrderReview() {

	var theTargetForm=document.getElementById("CHECKOUTDELIVERYFORM");
	var theTargetRequestInput=document.getElementById("CHECKOUTREQUEST");

	theTargetRequestInput.value="CHECKOUTDELIVERYSUMMARYBACK";	/* Define Request */
	theTargetForm.submit();						/* Submit Form */

}


/**********************************************************************************/

/* Checkout Update Payment Details */

function checkoutUpdateDatePaymentDetails() {
	var theTargetForm=document.getElementById("CHECKOUTPAYMENTFORM");
	var theTargetRequestInput=document.getElementById("CHECKOUTREQUEST");

	theTargetRequestInput.value="CHECKOUTUPDATEPAYMENTDETAILS";		/* Define Request */
	theTargetForm.submit();							/* Submit Form */
}

/**********************************************************************************/

/* Remove Gift Voucher From Basket Payment */

function checkoutRemoveGiftVoucher(voucherLineId) {

	var theTargetForm=document.getElementById("CHECKOUTPAYMENTFORM");
	var theTargetRequestInput=document.getElementById("CHECKOUTREQUEST");
	var theTargetRequestDataInput=document.getElementById("CHECKOUTREQUESTDATA");

	theTargetRequestInput.value="CHECKOUTREMOVEGIFTVOUCHER";	/* Define Action */
	theTargetRequestDataInput.value=voucherLineId;			/* Define Gift Voucher To Remove */
	theTargetForm.submit();						/* Submit Form */	

}

/**********************************************************************************/

/* CV2 Information PopUp */

function showccbaCV2InformationPopup(direction) {

	var theTargetDiv=document.getElementById("CCBACV2INFORMATIONDIV");

	if(direction==0) {
		theTargetDiv.style.display="none";
	} else {
		theTargetDiv.style.display="";
	}

}

/**********************************************************************************/

/* Checkout Payment Form Validate */

function checkoutSubmitPaymentDetails() {
	var theTargetForm=document.getElementById("CHECKOUTPAYMENTFORM");
	var theTargetRequestInput=document.getElementById("CHECKOUTREQUEST");
	var theTargetPadderDiv=document.getElementById("CCBAPAYMENTPADDER");	

	var theBackFromPaymentButton=document.getElementById("CHECKOUTBACKFROMPAYMENTBUTTON");
	var theCheckoutSubmitPaymentDetailsButton=document.getElementById("CHECKOUTSUBMITPAYMENTDETAILSBUTTON");

	theBackFromPaymentButton.disabled=true;			theBackFromPaymentButton.value="Please Wait";	
	theCheckoutSubmitPaymentDetailsButton.disabled=true;	theCheckoutSubmitPaymentDetailsButton.value="Please Wait";

	/* Display Please Wait Div */

 
  if(typeof SYSTEMMESSAGEDIV !="undefined") {
    var theSystemMessageDiv=document.getElementById("SYSTEMMESSAGEDIV");
    theSystemMessageDiv.style.display="none";
  }

	var theTargetDiv=document.getElementById("CCBAPAYMENTDIV");
	var theTargetInput=document.getElementById("CCBAPAYMENTDIVDISPLAYMODE");
	
	showccbaCV2InformationPopup(0);
	checkoutCCBAPleaseWaitAnimation();
	theTargetDiv.style.display='';
	theTargetInput.value="1";
	scrollToScreenPostion(0);

	theTargetRequestInput.value="CHECKOUTPAYMENTFORMVALIDATE";		/* Define Action */
	theTargetForm.submit();

	theTargetForm.style.display="none";		/* Hide Form */
	theTargetPadderDiv.style.display="";		/* Unhide Payment Padder - IE Fix */
	/* Clear Form Entries To Prevent Visibility On Back Button */

	window.setTimeout("checkoutClearPaymentForm()",500);
}


/**********************************************************************************/

/* Checkout Clear Payment Form Entries */

function checkoutClearPaymentForm() {

	var theTargetForm=document.getElementById("CHECKOUTPAYMENTFORM");

	var theTargetInputToClearArray=new Array;
	theTargetInputToClearArray[0]="CHECKOUTPAYMENTCARDTYPE";
	theTargetInputToClearArray[1]="CHECKOUTPAYMENTCARDSTARTMONTH";
	theTargetInputToClearArray[2]="CHECKOUTPAYMENTCARDSTARTYEAR";
	theTargetInputToClearArray[3]="CHECKOUTPAYMENTCARDISSUENUMBER";
	theTargetInputToClearArray[4]="CHECKOUTPAYMENTCARDNAME";
	theTargetInputToClearArray[5]="CHECKOUTPAYMENTCARDNUMBER";
	theTargetInputToClearArray[6]="CHECKOUTPAYMENTCARDEXPIRYMONTH";	
	theTargetInputToClearArray[7]="CHECKOUTPAYMENTCARDEXPIRYYEAR";	
	theTargetInputToClearArray[8]="CHECKOUTPAYMENTCARDSECURITYNUMBER";	
	theTargetInputToClearArray[9]="CHECKOUTPAYMENTCARDBILLINGID";

	for (var i=0; i<theTargetInputToClearArray.length; i++) {
		var theTargetInput=document.getElementById(theTargetInputToClearArray[i]);
		theTargetInput.value="";
	}	
}

/**********************************************************************************/

/* Checkout CCBA Please Wait Animation */

var theCurrentPleaseWaitElement=-1;

function checkoutCCBAPleaseWaitAnimation() {

	theCurrentPleaseWaitElement++;

	if(theCurrentPleaseWaitElement >10) {
		theCurrentPleaseWaitElement=0;
	}

	for (var i=0; i<=10; i++) {
		theTargetElement=document.getElementById("CCBAPLEASEWAITANIMATIONELEMENT_"+i);
	
		if(i==theCurrentPleaseWaitElement) {
			theTargetElement.src="/images/global/please_wait_white_ball.gif";
		} else {
			theTargetElement.src="/images/global/please_wait_blue_ball.gif";
		}
	}

	setTimeout('checkoutCCBAPleaseWaitAnimation()',100);
}


/**********************************************************************************/

/* Checkout Back From Payment */

function checkoutBackFromPayment() {

	var theTargetForm=document.getElementById("CHECKOUTPAYMENTFORM");
	var theTargetRequestInput=document.getElementById("CHECKOUTREQUEST");

	theTargetRequestInput.value="ORDERREVIEW";		/* Define Action */
	theTargetForm.submit();
}


/**********************************************************************************/

/* Checkout Add New Billing Address */

function checkoutAddNewBillingAddress() {
	var theTargetForm=document.getElementById("CHECKOUTPAYMENTFORM");
	var theTargetRequestInput=document.getElementById("CHECKOUTREQUEST");

	theTargetRequestInput.value="ADDNEWBILLINGADDRESS";		/* Define Request */
	theTargetForm.submit();						/* Submit Form */

}

/**********************************************************************************/

/* Checkout Cancel Add New Billing Address */

function checkoutCancelAddNewBillingAddress() {

	var theTargetForm=document.getElementById("CHECKOUTADDRESSFORM");
	var theTargetRequestInput=document.getElementById("CHECKOUTREQUEST");

	theTargetRequestInput.value="CHECKOUTPAYMENTMAINBACK";	/* Define Request */
	theTargetForm.submit();					/* Submit Form */
}

/**********************************************************************************/

/* Create Customer Account At End Of Checkout Process */

function checkoutCreateCustomerAccount() {

	var theTargetForm=document.getElementById("CHECKOUTORDERCONFIRMATIONFORM");
	var theTargetRequestInput=document.getElementById("CHECKOUTREQUEST");

	theTargetRequestInput.value="CHECKOUTCREATECUSTOMERACCOUNT";	/* Define Action */
	theTargetForm.submit();						/* Submit Form */

}

/**********************************************************************************/

/* Hide Add To Basket Confirmation */

function hideAddToBasketConfirmation() {
	var theAddToBasketConfirmationAnchorDiv=document.getElementById("ADDTOBASKETCONFIRMATION");
	theAddToBasketConfirmationAnchorDiv.style.display='none';
}


/**********************************************************************************/

/* Populate Commidea 3DS Frame */

function populate3DSFrame(the3DSContent) {
        var theTargetIframe = document.getElementById("COMMIDEA3DSIFRAME");
       	var theTargetIframeContent = theTargetIframe.contentDocument;
	if (theTargetIframeContent == undefined || theTargetIframeContent == null) {
        				 theTargetIframeContent = theTargetIframe.contentWindow.document;
	}
	theTargetIframeContent.open();
        theTargetIframeContent.write(the3DSContent);
        theTargetIframeContent.close();

}

/**********************************************************************************/

/* Show MDA Help Text */

function showCcbaMdaHelpText() {
	var theTargetDiv=document.getElementById("CCBAMDAHELPTEXT");
	theTargetDiv.style.display="";
}

/**********************************************************************************/


