function validateDeliveryCalendarLaunch(url, chosenPostCode, postcode, df, defaultDespatchType){
	if(postcode==""){
		alert("Please enter a postcode so that we can establish \nwhen we can deliver this product to you.")
		return false;
	}
	else if(!isPostcode(postcode)){
		alert("Please enter a valid postcode.")
		return false;
	} else {
		if (chosenPostCode!="") {
			if (chosenPostCode != postcode) {
				alert("The postcode must match the postcode entered for previous products which you have added to the basket.")
				return false;
			}
		}
		openDeliveryCalendar(url, postcode, df, defaultDespatchType)
	}
}

function updateDeliveryCalendar(url, postcode, df, defaultDespatchType, callFrom, basketProductCode, chosenDate){
	var left = screen.width - 540;

	if(df == 'FEENIX') {
		window.open(url + '?postcode=' + postcode + '&warehouseCode=' + df + "&ddt=" + defaultDespatchType + "&callFrom=" + callFrom + "&basketProductCode=" + basketProductCode + "&chosenDate=" + chosenDate,'deliveryCalendar','width=210,height=420,top=186,left='+left+',scrollbars=no')
	}
	else {
		window.open(url + '?postcode=' + postcode + '&warehouseCode=' + df + "&ddt=" + defaultDespatchType + "&callFrom=" + callFrom + "&basketProductCode=" + basketProductCode + "&chosenDate=" + chosenDate,'deliveryCalendar','width=210,height=300,top=186,left='+left+',scrollbars=no')
	}
}

function openDeliveryCalendar(url, postcode, df, defaultDespatchType){
	var left = screen.width - 540;

	if(df == 'FEENIX') {
		window.open(url + '?postcode=' + postcode + '&warehouseCode=' + df + "&ddt=" + defaultDespatchType,'deliveryCalendar','width=210,height=420,top=186,left='+left+',scrollbars=no')
	}
	else {
		window.open(url + '?postcode=' + postcode + '&warehouseCode=' + df + "&ddt=" + defaultDespatchType,'deliveryCalendar','width=210,height=300,top=186,left='+left+',scrollbars=no')
	}
}

function validateATB(f){
	if (f.productCode.value =="") {
		alert('Please select which product you wish to purchase from the list of options')
		return false;
	}
	if (f.checkRequiredDate.value=="Y") {
		if ((f.requiredDate.value=="")||(f.despatchType.value=="")){
			alert('In order to add this product to your basket please choose a Delivery Date')
			return false;
		}
		else{
			return true;
		}
	}
}

function updateProductCode(f,v) {
	f.productCode.value = v
	el = getCheckedValue(document.forms['MultipleAddToBasket'].elements['attributeCode'])
//	f.currentchoice.value = attributeCodes[el]
	updateDiv(attributeCodes[el])
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == 'undefined')
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			//return radioObj[i].value;
			return i
		}
	}
	return "";
}


function updateDiv(productDesc) {
	if (document.layers) {
		lyr = document.layers["currentChoice"].document;
		lyr.open();
		lyr.write(productDesc);
		lyr.close();
	}
	else if (navigator.appName == "Microsoft Internet Explorer") {
		document.all["currentChoice"].innerHTML = productDesc;
	}
	else if (document.getElementById) {
		document.getElementById('currentChoice').innerHTML = productDesc;
	}
//	else if (document.all) {
//		document.all["currentChoice"].innerHTML = productDesc;
//	}
}

function getStyleObject(objectId) {
  // checkW3C DOM, then MSIE 4, then NN 4.
  //
  if(document.getElementById && document.getElementById(objectId)) {
	return document.getElementById(objectId).style;
   }
   else if (document.all && document.all(objectId)) {  
	return document.all(objectId).style;
   } 
   else if (document.layers && document.layers[objectId]) { 
	return document.layers[objectId];
   } else {
	return false;
   }
}

function changeObjectVisibility(objectId, newVisibility) {
    // first get the object's stylesheet
    var styleObject = getStyleObject(objectId);

    // then if we find a stylesheet, set its visibility
    // as requested
    //
    if (styleObject) {
    	if (newVisibility == 'hidden') {
    		//styleObject.visibility = newVisibility;
    		styleObject.display = "none";
			}
    	if (newVisibility == 'visible') {
    		//styleObject.visibility = newVisibility;
    		styleObject.display = "block";
			}
			//styleObject.visibility = newVisibility;
			return true;
  	} else {
			return false;
    }
}

function hideShowDeliverySection(strProductCode, strDespatchFrom, strAvailableStock) {

	//alert("***" +strProductCode+ "***");
	//alert("***" +strDespatchFrom+ "***");

	if (strProductCode != '') {
		if (strDespatchFrom == "DHDO" || strDespatchFrom == "DHDO_VIA_FEENIX") {
			changeObjectVisibility("deliveryOptionsSpecificContentNoCalendar", "visible")
			changeObjectVisibility("deliveryOptionsSpecificContentCalendar", "hidden")
			changeObjectVisibility("deliveryOptionsAddToBasket", "visible")
			changeObjectVisibility("deliveryOptionsGeneralContent", "hidden")
			document.MultipleAddToBasket.productCode.value = strProductCode
			document.MultipleAddToBasket.requiredDate.value = ''
			document.MultipleAddToBasket.checkRequiredDate.value = 'N'
		}
		else {
			if(parseFloat(strAvailableStock) == 0) {
				changeObjectVisibility("deliveryOptionsSpecificContentNoCalendar", "hidden")
				changeObjectVisibility("deliveryOptionsSpecificContentCalendar", "hidden")
				changeObjectVisibility("deliveryOptionsAddToBasket", "hidden")
				changeObjectVisibility("deliveryOptionsGeneralContent", "visible")
				document.MultipleAddToBasket.productCode.value = ''
			}
			else {
				changeObjectVisibility("deliveryOptionsSpecificContentNoCalendar", "hidden")
				changeObjectVisibility("deliveryOptionsSpecificContentCalendar", "visible")
				changeObjectVisibility("deliveryOptionsAddToBasket", "visible")
				changeObjectVisibility("deliveryOptionsGeneralContent", "hidden")
				document.MultipleAddToBasket.productCode.value = strProductCode
				document.MultipleAddToBasket.checkRequiredDate.value = 'Y'
			}
		}
	}
	else {
		changeObjectVisibility("deliveryOptionsSpecificContentNoCalendar", "hidden")
		changeObjectVisibility("deliveryOptionsSpecificContentCalendar", "hidden")
		changeObjectVisibility("deliveryOptionsAddToBasket", "hidden")
		changeObjectVisibility("deliveryOptionsGeneralContent", "visible")
	}
}

function changeDetails(f) {
	var strURL, strResponse, strArray;
	var strFlag, strBreadCrumbDescription, strDescription, formattedUpliftPrice, formattedRapidDelivery
	var strStyleImage, strStyleZoomImage, formattedProductSpecification
	var strDescription1, strDescription2, formattedPrice, storePrice, youSavePrice
	var formattedDeliverySection, formattedBuyNowSection
	var strProductCode, strDespatchFrom, strDefaultDespatchType
	var strDrawerOption, formattedProductAccessories, formattedProductAlternate, formattedMessage, strAvailableStock
	var formattedProductBedSizeAlternate
	
	//--- escape any special characters in drawer option
	strDrawerOption = escape(f.drawerOption.value);

	//--- escape the plus symbol manually from drawer option -- escape function does not escape the plus symbol
	strDrawerOption = strDrawerOption.replace(new RegExp( "\\+", "g" ),"%2B");
	
	strURL = '';
	strURL = strRoot + "/products/changeDetails.asp";
	strURL += "?styleCode=" + f.styleCode.value;
	strURL += "&bedSize=" + f.bedSize.value;
	strURL += "&productCode=" + f.strProductCode.value;
	strURL += "&basePrice=" + f.basePrice.value;
	strURL += "&styleDescription=" + f.styleDescription.value;
	strURL += "&rapidDelivery=" + f.rapidDelivery.value;
	strURL += "&baseTension=" + f.baseTension.value;
	strURL += "&colour=" + f.colour.value;
	strURL += "&drawerOption=" + strDrawerOption;
	strURL += "&bedComfort=" + f.bedComfort.value;
	strURL += "&accessoryColour=" + f.accessoryColour.value;
	strURL += "&headboardMaterial=" + f.headboardMaterial.value;
	strURL += "&x=" + Math.random()
	
	strResponse = sendXmlHttpRequest(strURL);

	// Split the returned data by the "@@#@@" delimiter
	strArray = new Array();
	strArray = strResponse.split("@@#@@"); 

	strFlag = strArray[0];
	
	var imageZoomText;
	
	switch(strFlag) {
	case "1":
		strBreadCrumbDescription = strArray[1];
		strDescription1 = strArray[2];
		strDescription2 = strArray[3];
		formattedPrice = strArray[4];
		storePrice = strArray[5];
		youSavePrice = strArray[6];
		formattedRapidDelivery = strArray[7];
		strStyleImage = strArray[8];
		strStyleZoomImage = strArray[9];
		formattedProductSpecification = strArray[10];
		strDespatchFrom = strArray[11];
		strDefaultDespatchType = strArray[12];
		strProductCode = strArray[13];
		formattedProductAccessories = strArray[14];
		formattedProductAlternate = strArray[15];
		formattedMessage = strArray[16];
		strAvailableStock = strArray[17];
		formattedProductBedSizeAlternate = strArray[18];
		imageZoomText = strDescription1 + " " + strDescription2;
		// Write responses to layer
		MM_setTextOfLayer('breadCrumbLocationText','',strBreadCrumbDescription);
		MM_setTextOfLayer('productHeaderText','',strDescription1);
		MM_setTextOfLayer('productDescription','',strDescription2);
		MM_setTextOfLayer('productWebPriceContainer','',formattedPrice);
		MM_setTextOfLayer('productPricesContainer','',storePrice + youSavePrice);
		MM_setTextOfLayer('rapidDeliveryImageContainer','',formattedRapidDelivery);
		MM_setTextOfLayer('productImageContainer','',strStyleImage);
		MM_setTextOfLayer('productZoomLinkContainer','',strStyleZoomImage);
		MM_setTextOfLayer('productSpecificationInfoContainer','',formattedProductSpecification);
		MM_setTextOfLayer('productAccessoryContainer','',formattedProductAccessories);
		MM_setTextOfLayer('productAlternateContainer','',formattedProductAlternate);
		MM_setTextOfLayer('messageContainer','',formattedMessage);
		MM_setTextOfLayer('bedSizeAlternateContainer','',formattedProductBedSizeAlternate);

		document.MultipleAddToBasket.productCode.value = strProductCode
		document.MultipleAddToBasket.warehouseCode.value = strDespatchFrom
		document.MultipleAddToBasket.defaultDespatchType.value = strDefaultDespatchType

		if (strDespatchFrom == 'DHDO' || strDespatchFrom == 'DHDO_VIA_FEENIX') {
		
			document.MultipleAddToBasket.checkRequiredDate.value = 'N'
			document.MultipleAddToBasket.despatchType.value = strDefaultDespatchType
		}
		else {
			document.MultipleAddToBasket.checkRequiredDate.value = 'Y'
		}

		changeObjectVisibility("productDescription", "visible")
		changeObjectVisibility("productPricesContainer", "visible")

		if (formattedMessage == '') {
			changeObjectVisibility("messageContainer", "hidden")
		}
		else {
			changeObjectVisibility("messageContainer", "visible")		
		}

		if (formattedRapidDelivery == '') {
			changeObjectVisibility("rapidDeliveryImageContainer", "hidden")
		}
		else {
			changeObjectVisibility("rapidDeliveryImageContainer", "visible")		
		}

		hideShowDeliverySection(strProductCode, strDespatchFrom, strAvailableStock)

		break;
	case "2":
		strBreadCrumbDescription = strArray[1];
		strDescription = strArray[2];
		formattedUpliftPrice = strArray[3];
		formattedRapidDelivery = strArray[4];
		strStyleImage = strArray[5];
		strStyleZoomImage = strArray[6];
		formattedProductSpecification = strArray[7];

		// Write responses to layer
		MM_setTextOfLayer('breadCrumbLocationText','',strBreadCrumbDescription);
		MM_setTextOfLayer('productHeaderText','',strDescription);
		MM_setTextOfLayer('productDescription','','');
		MM_setTextOfLayer('productWebPriceContainer','',formattedUpliftPrice);
		MM_setTextOfLayer('productPricesContainer','','');
		MM_setTextOfLayer('rapidDeliveryImageContainer','',formattedRapidDelivery);
		MM_setTextOfLayer('productImageContainer','',strStyleImage);
		MM_setTextOfLayer('productZoomLinkContainer','',strStyleZoomImage);
		MM_setTextOfLayer('productSpecificationInfoContainer','',formattedProductSpecification);
		MM_setTextOfLayer('productAccessoryContainer','','');
		MM_setTextOfLayer('productAlternateContainer','','');
		MM_setTextOfLayer('messageContainer','','');
		MM_setTextOfLayer('bedSizeAlternateContainer','','');

		document.MultipleAddToBasket.productCode.value = ''

		changeObjectVisibility("productDescription", "hidden")
		changeObjectVisibility("productPricesContainer", "hidden")
		changeObjectVisibility("messageContainer", "hidden")

		if (formattedRapidDelivery == '') {
			changeObjectVisibility("rapidDeliveryImageContainer", "hidden")
		}
		else {
			changeObjectVisibility("rapidDeliveryImageContainer", "visible")		
		}

		hideShowDeliverySection('','','')

	  	break;    
	case "3":

		document.MultipleAddToBasket.productCode.value = ''

		hideShowDeliverySection('','','')
		
		changeObjectVisibility("attributeOptionsContainer", "hidden")
		changeObjectVisibility("noProductMessageContainer", "visible")
		
	  	break;    
	}
		$('a.imageZoom').attr("title", imageZoomText);
			$('a.imageZoom').zoomimage({opacity: 0.8, border: 20, centered: true, controls: true});

			  $("#tabsSection > ul").tabs();	
}
