// ******************************************************************************************************
// P R O D U C T    F U N C T I O N S
/*
Version History		: [x.xx] - <Developer Initials> - <dd/mm/yyyy> - <Brief Description>
**********************************************************************************************************
[1.00] - Initial Build
[1.10] - DB - 23/12/2008 - Case ID:08-10013 - Ordering multiple virtual gifts
[1.11] - PJR - 15/10/2009 - CaseID:2009-10-5229 Desc: Shop improvements for October (revised) Replacing 2009-09-5177
[1.12] - PJR - 02/11/2009 - CaseID:09-11734 Desc: Shop improvements for October - Bug fixes
*/


// ----------------------------------------------------------------------------- ToggleAdditionalInformation()

function ToggleAdditionalInformation() {
	var oAdditionInfoBarStatus = getObject('oAdditionInfoBarStatus');
	var oAdditionInfoBarImg = getObject('oAdditionInfoBarImg');
	var oAdditionInfo = getObject('oAdditionInfo');
	if(oAdditionInfoBarStatus.innerHTML == 'close') {
		oAdditionInfoBarStatus.innerHTML = 'open';
		oAdditionInfoBarImg.src = '/public/images/product/arrow_closed1.gif';
		oAdditionInfoBarImg.alt = 'open';
		oAdditionInfo.style.display = 'none';
	} else {
		oAdditionInfoBarStatus.innerHTML = 'close';
		oAdditionInfoBarImg.src = '/public/images/product/arrow_open1.gif';
		oAdditionInfoBarImg.alt = 'close';
		oAdditionInfo.style.display = 'block';
	}
}

function AdditionalImageError(oThis, sSmallImgpath, sLargeImgpath) {
	var oAdditionalImgLink = getObject('oAdditionImage');
	oAdditionalImgLink.href = sLargeImgpath + '/noimage.gif';
	oThis.src=sSmallImgpath + '/noimage.gif';
}


function MainImageError(oThis, sSmallImgpath, sLargeImgpath) {
	var oMainImgLink = getObject('oMainImage');
	oMainImgLink.href = sLargeImgpath + '/noimage.gif';
	oThis.src=sSmallImgpath + '/noimage.gif';
}

function OpenEmailWin(sTarget, sSiteUrl, sProductName) {
		var sPage = sSiteUrl + '/pages/emailafriend/emailafriend.asp?sURL=' + sTarget + '&sName=' + sProductName;
		var c_win = window.open(sPage,'Email','location=no,scrollbars=no,resizable=no,width=500,height=490,left=100,top=10');
		c_win.focus();
}

function ValidateAndSubmit() {
	var oForm		= document.forms['frmEmailAFriend'];
	var sEmailTo	= oForm.elements['sEmailTo'].value;
	var sEmailFrom	= oForm.elements['sEmailFrom'].value;
	var sMsg		= oForm.elements['sMsg'].value;

	var strMsg = '';
	if (!CheckEmail(sEmailFrom)){strMsg += 'Please enter a valid email address for you.\n'};
	if (!CheckEmail(sEmailTo)){strMsg += 'Please enter a valid email address for your friend.\n'};
	if (sMsg.length == 0){strMsg += 'Please enter a message for your friend.\n'};

	//Submit form if there are no error messages to display to the user.
	if(strMsg.length > 0){
		alert(strMsg);
	}else{
		oForm.submit();
	}
}

// changing the images when the user clicks on standard image.
function ChangeViewsImage(productid,divid,imagename) {
	var tarr = imagename.split("/");
	var timg = tarr[tarr.length -1];
	var oMainImage;
	var thumbImage;
	
	if(HM_DOM)
			{	
				oMainImage = document.getElementById(productid );
				thumbImage = document.getElementById(divid);
			}
			else
			{	
				oMainImage = eval('document.all.' + productid );
				thumbImage = eval('document.all.' + divid);
			}
			
			var mytool_array=oMainImage.src.split("/");
			var tempimg = mytool_array[mytool_array.length -1];
			
			oMainImage.src = '/public/pictures/products/standarD/' + timg;
			//thumbImage.src = '/public/pictures/products/Additional/small/' + tempimg ;
			
	}
//showing the zoom image when the user clicks on view enlarge image	
	function zoomImage(Prodid,ProdOptions) {

			var sHeight 	= '620px'
			var sWidth  	= '590px'
			var sLeft   	= '30px'
			var sTop    	= '30px'
			
			var sImage = getObject(Prodid).src 
			
			sImage = sImage.substring(sImage.indexOf('D/')+1,sImage.length)
			
			if(window.showModalDialog) {
				window.open('/pages/product/ImagePopup.asp?ImageName=' + sImage + "&Images=" + ProdOptions  , 'ImagePopup', 'width=' + sWidth + ', height=' + sHeight + ', left=' + sLeft + ', top=' + sTop + ', menubar=no, status=no, location=no, toolbar=no, scrollbars=no, resizable=no, modal=yes');
			} else {
				window.open('/pages/product/ImagePopup.asp?ImageName=' + sImage + "&Images=" + ProdOptions  , 'ImagePopup', 'width=' + sWidth + ', height=' + sHeight + ', left=' + sLeft + ', top=' + sTop + ', menubar=no, status=no, location=no, toolbar=no, scrollbars=no, resizable=no');
			}
		}	
		
		// Start [1.11] - upadte to variant selection functionality
		// selection of variant image
		function VariantSelection(sproductid,svariantid,simagefilename,scolour,sVarStock) {
			//alert('\n--productid:' + sproductid + '\n--svariantid:'+  svariantid + '\n--image:' + simagefilename + '\n--colour:' + scolour +'\n--sVarStock:' + sVarStock);
			//var oForm = document.forms['form_' + sproductid];			
			//oForm.elements['item0_variantid'].value = svariantid;
			//oForm.elements['item0_image_variant_filename'].value = simagefilename;
			//oForm.elements['item0_variant_option'].value = scolour;
			//oForm.elements['item0_variant_stock'].value = sVarStock;
			EnableBuyButton(sproductid);
		}
		// End [1.11]
		
		// Start [1.11]
		function giftVariantSelection(sproductid,svariantid,simagefilename,scolour,sVarStock,iVariantCount) {
			//alert('\n--productid:' + sproductid + '\n--svariantid:'+  svariantid + '\n--image:' + simagefilename + '\n--colour:' + scolour);
			var oForm = document.forms['form_' + sproductid];
			oForm.elements['item'+iVariantCount+'_variantid'].value = svariantid;
			oForm.elements['item'+iVariantCount+'_image_variant_filename'].value = simagefilename;
			oForm.elements['item'+iVariantCount+'_variant_option'].value = scolour;
			oForm.elements['item'+iVariantCount+'_variant_stock'].value = sVarStock;
			//oForm.elements['item1_qty'].value = oForm.elements['item0_qty'].value;
		}
		// End [1.11]
		
function EnableBuyButton(prodcode)
		{
			if(HM_DOM)
			{
				oElementb=document.getElementById('btnAdd' + prodcode);
				oElementc=document.getElementById('form_' + prodcode + 'link');
				// [1.11] oElementd=document.getElementById('btnAddtop' + prodcode);
				// [1.11] oElemente=document.getElementById('form_' + prodcode + 'linktop');
				
				
			}
			else
			{
				oElementb=eval('document.all.btnAdd' + prodcode);
				oElementc=eval('document.all.form_' + prodcode + 'link');
				// [1.11] oElementd=eval('document.all.btnAddtop' + prodcode);
				// [1.11] oElemente=eval('document.all.form_' + prodcode + 'linktop');
			}
				
			// [1.12] - need to call the new function (not the old one)
			oElementc.href = "javascript:addToBasket('" + prodcode +"')";
			// [1.11] oElemente.href = "javascript:addProduct('" + prodcode +"')";
			
			//oElementc.href = 'javascript:addtoBasketClick(\'' + prodcode + '\',\'' + prodcode + '\' )';
			
			// [1.11] - remove oButtond
			changeButton('', oElementb)
		}		
		
	// [1.11] - remove oButtond
	function changeButton(oButtona,oButtonb)
		{
			if (oButtona)
			{
				oButtona.disabled=false;
				oButtona.src='/public/images/buttons/select.gif'
				
			}
			if (oButtonb)
			{
				oButtonb.src='/public/images/buttons/addtobasket.gif'
				// [1.11] oButtond.src='/public/images/buttons/buy.gif'
			}
		}
		
	function addProduct(sproductid)
		{
			
			var oForm = document.forms['form_' + sproductid];
			
				if( parseInt(oForm.elements['item0_variant_stock'].value) <  parseInt(oForm.elements['item0_qty'].value))
				{
					oForm.elements['item0_warning'].value = oForm.elements['item0_variant_stock'].value;
					//oForm.elements['item0_warning'].value = "Changes were made to your basket because an item&#39;s stock level is insufficent." 
					if (oForm.elements['item0_stockbehaviour_on_zero'].value < 2 )	{
						alert("The availabe stock is " + ( oForm.elements['item0_variant_stock'].value) + ", Changes were made to your basket as item's stock level is insufficent.")
					}
					else
					{
						oForm.elements['item0_warning'].value = oForm.elements['item0_qty'].value;
					}
				} else
				{
					oForm.elements['item0_warning'].value = oForm.elements['item0_qty'].value;
				}
				oForm.submit();
			
		}		
		
	// Start [1.11]	
	function addToBasket(sproductid) {
			
		var oForm = document.forms['form_' + sproductid];
		var oVariantCount = getObject("variantCount");
		var bValid = false;
		for(var i=0;i<oVariantCount.value;i++) {
			var oVariantQty = getObject("item"+i+"_qty");
			if(oVariantQty) {
				if(oVariantQty.value > 0) {
					bValid = true;
					break;
				}
			}
		}
		
		if(bValid) {
			oForm.submit();
		} else {
			alert("Please ensure your quantity is greater than 0");
		}			
			
	}
		
	function updateQty(sElement) {
		var rIsNotNumeric = /[^0-9]/;
		var rIsNotZero = /[1-9]+/;
		var oElement = getObject(sElement);
		if(rIsNotNumeric.test(oElement.value)) {
			alert("Please ensure you enter a valid quantity.");
			oElement.value = '';
			oElement.focus();
		}
	}
	
	function decreaseQty(sQtyInput) {
		var oQtyInput = getObject(sQtyInput);
		if(oQtyInput.value == '') {
			oQtyInput.value = 0;
		} 
		
		if(oQtyInput.value > 0) {
			oQtyInput.value = parseInt(oQtyInput.value,10) - 1;	
		}
	}
	
	function increaseQty(sQtyInput) {
		var oQtyInput = getObject(sQtyInput);
		if(oQtyInput.value == '') {
			oQtyInput.value = 0;
		}		
		oQtyInput.value = parseInt(oQtyInput.value,10) + 1;	
	}	
	
	// End [1.11]		
			
		
	function CalTotal(iQty,sproductid)
		{
			//alert('sproductid:' + sproductid);
			var oFormVar = document.forms['form_' + sproductid]
			if (iQty.indexOf('.') > 0)
			{
				iQty=Math.round(iQty)
				oFormVar.elements['item0_qty'].value=iQty;
				// [1.10] Start of
				// If the free gift box exists - ensure the qty values are in synch with
				// the number of items the customer ordered.
				if (oFormVar.elements['item1_qty']!=null) {
					oFormVar.elements['item1_qty'].value = oFormVar.elements['item0_qty'].value;
				}
				// [1.10] end of 
				return true;
				
				return false;
			}
			if (isNaN(iQty))
			{
				alert("Quantity must be valid number greater than 0");
				oFormVar.elements['item0_qty'].value = 1;
				// [1.10] Start of
				// If the free gift box exists - ensure the qty values are in synch with
				// the number of items the customer ordered.
				if (oFormVar.elements['item1_qty']!=null) {
					oFormVar.elements['item1_qty'].value = oFormVar.elements['item0_qty'].value;
				}
				// [1.10] end of 
				return true;
				return false;

			}
			else if (iQty <= 0 )
			{
				alert("Quantity must be valid number greater than 0");
				oFormVar.elements['item0_qty'].value = 1;
				// [1.10] Start of
				// If the free gift box exists - ensure the qty values are in synch with
				// the number of items the customer ordered.
				if (oFormVar.elements['item1_qty']!=null) {
					oFormVar.elements['item1_qty'].value = oFormVar.elements['item0_qty'].value;
				}
				// [1.10] end of 
				return true;
				return false;
			}
			else
			{
				// [1.10] Start of
				// If the free gift box exists - ensure the qty values are in synch with
				// the number of items the customer ordered.
				if (oFormVar.elements['item1_qty']!=null) {
					oFormVar.elements['item1_qty'].value = oFormVar.elements['item0_qty'].value;
				}
				// [1.10] end of 
				return true;
			}
        }