function termsandconditions()
{
	openWindow("staticviewer.php?cmd=tac", "cc_tac", 640, 480, ", scrollbars, resizable");
}

function updateProductPricingOptionChange(selectId)
{
	sourceSelectBox=eval("document.addtobasket.product_option_"+selectId);
	sourceDiv=document.getElementById("product_option_div_price_"+selectId);

	// assign new source div price
	optionId=sourceSelectBox.selectedIndex;
	dataIndex=getIndexFromValue(productPriceIndex, selectId);
	selectedIndex=sourceSelectBox.selectedIndex;
	optionPrice=(productPriceData[dataIndex][selectedIndex]);
	priceStr=getMoneyStr(optionPrice);
	optionPriceData=replaceStr(optionPriceTemplate, "{PRODUCT_JAVASCRIPT_TEMPLATE_OPTION_PRICE}", priceStr);
	sourceDiv.innerHTML=optionPriceData;

	// do pricing re-calculations
	updateProductFinalPrice();
}

function updateProductFinalPrice()
{
	itemPriceDiv=document.getElementById("product_item_price_div");
	finalPriceDiv=document.getElementById("product_final_price_div");

	// calculate new final price
	price=parseFloat(productBasePrice);
	for(i=0; i<productPriceIndex.length; i++)
	{
		optionId=productPriceIndex[i];
		selectBox=eval("document.addtobasket.product_option_"+optionId);
		selectedIndex=selectBox.selectedIndex;
		price+=parseFloat(productPriceData[i][selectedIndex]);
	}

	priceStr=getMoneyStr(price);
	priceData=replaceStr(itemPriceTemplate, "{PRODUCT_JAVASCRIPT_TEMPLATE_ITEM_PRICE}", priceStr);
	itemPriceDiv.innerHTML=priceData;

	quantitySelectBox=document.addtobasket.quantity;
	quantity=parseFloat(quantitySelectBox.options[quantitySelectBox.selectedIndex].value);

	priceStr=getMoneyStr(price*quantity);
	priceData=replaceStr(finalPriceTemplate, "{PRODUCT_JAVASCRIPT_TEMPLATE_FINAL_PRICE}", priceStr);
	finalPriceDiv.innerHTML=priceData;
}

function productImageCycle(direction)
{
	// change image
	totalCycles=productImageIdData.length;
	productImageCycleIndex=(productImageCycleIndex+direction)%totalCycles;
	if(productImageCycleIndex<0) productImageCycleIndex=Math.abs(totalCycles+productImageCycleIndex)%totalCycles;
	resourceId=productImageIdData[productImageCycleIndex];
	resourceFilename="resources/resource_"+resourceId+".jpg";
	size=getImageSizeToMax(resourceFilename, 200, 300);
	// replace image data and link data
	imageData=productImageTemplate;
	imageData=replaceStr(imageData, "{PRODUCT_JAVASCRIPT_IMAGE_ID}", resourceId);
	imageData=replaceStr(imageData, "{PRODUCT_JAVASCRIPT_IMAGE_FILENAME}", resourceFilename);
	imageData=replaceStr(imageData, "{PRODUCT_JAVASCRIPT_IMAGE_WIDTH}", size[0]);
	imageData=replaceStr(imageData, "{PRODUCT_JAVASCRIPT_IMAGE_HEIGHT}", size[1]);
	document.getElementById("product_image").innerHTML=imageData;
	// change image cycle data text
	cycleData=replaceStr(productImageCycleTemplate, "{PRODUCT_JAVASCRIPT_TEMPLATE_IMAGE_CURRENT_ID}", productImageCycleIndex+1);
	document.getElementById("product_image_cycle_div").innerHTML=cycleData;
}

function productImageOnload()
{
	for(i=0; i<productImageIdData.length; i++)
	{
		resourceId=resourceId=productImageIdData[i];
		filename="resources/resource_"+resourceId+".jpg";
		temp=new Image();
		temp.src=filename;
	}
}

function getIndexFromValue(list, value)
{
	for(_index=0; _index<list.length; _index++)
		if(list[_index]==value)
			return _index;
	return false;
}

function replaceStr(text, oldValue, newValue)
{
	re=new RegExp(oldValue, 'gi');
	return text.replace(re, newValue);
}

function getMoneyStr(value)
{
	value=(Math.round((value-0)*100))/100;
	if(value==Math.floor(value)) return value+".00";
	else if((value*10)==Math.floor(value*10)) return value+'0';
	else return value;
}

function updatebasketquantities()
{
	document.updatebasketquantities.submit()
}

// IMAGE UTILS
function getSizeToMax(w, h, maxWidth, maxHeight)
{
	changed=false;
	if(w>maxWidth)
	{
		h=(maxWidth*h)/w;
		w=maxWidth;
		changed=true;
	}
	if(h>maxHeight)
	{
		w=(maxHeight*w)/h;
		h=maxHeight;
		changed=true;
	}
	return Array(w, h, changed);
}

function getImageSizeToMax(filename, maxWidth, maxHeight)
{
	imageTemp=new Image();
	imageTemp.src=filename;
	return getSizeToMax(imageTemp.width, imageTemp.height, maxWidth, maxHeight);
}

function isImageLargerThan(filename, width, height)
{
	imageTemp=new Image();
	imageTemp.src=filename;
	return imageTemp.width>width || imageTemp.height>height;
}

function updateCheckoutShippingRegion(selectBox)
{
	postagePrice=checkoutPostagePriceData[selectBox.options.selectedIndex];
	basketWeightPrice=replaceStr(basketWeightPriceTemplate, "{CHECKOUT_JAVASCRIPT_TEMPLATE_BASKET_WEIGHT_PRICE}", getMoneyStr(postagePrice));
	document.getElementById("basket_weight_price_div").innerHTML=basketWeightPrice;
	totalOrderPrice=getMoneyStr(parseFloat(basketPrice)+parseFloat(postagePrice));
	totalOrderPrice=replaceStr(finalPriceTemplate, "{CHECKOUT_JAVASCRIPT_TEMPLATE_FINAL_PRICE}", getMoneyStr(totalOrderPrice));
	document.getElementById("final_price_div").innerHTML=totalOrderPrice;
}

function worldpay()
{
	paymentiframe.location="paymentpost.php?type=1&id="+checkout_id;
}

function paypal()
{
	paymentiframe.location="paymentpost.php?type=2&id="+checkout_id;
}

function confirmContact(form)
{
	var contactError="";
	if(form.title.value=="") contactError+="- Please enter your contact name\n";
	if(form.email.value=="") contactError+="- Please enter your email address\n";
	else if(!booleanIsEmail(form.email.value)) contactError+="- Please enter a valid email address\n";
	if(form.message.value=="") contactError+="- Please enter a message\n";
	if(contactError!="")
	{
		alert('The following errors have occured...\n\n'+contactError);
		return false;
	}
	return true;
}

function confirmAddToBasket(form)
{
	len=productBasketTextInputData.length;
	for(i=0; i<len; i++)
	{
		textInput=eval("document.addtobasket.product_option_"+productBasketTextInputData[i]);
		result=isSafeText(textInput.value, "Products option entries");
		if(result!="")
		{
			alert(result);
			return false;
		}
	}
	return true;
}

function isSafeText(str, text)
{
	if((str.indexOf('|')!=-1) || (str.indexOf('\\')!=-1))
		return text+" cannot contain the '|' or '\\' characters";
	return "";
}

function confirmCheckoutTACAgreed(form)
{
	if(!form.checkout_tac.checked)
	{
		alert("Please tick the checkbox to indicate acceptance of our Terms and Conditions");
		return false;
	}
	return true;
}

function emptybasket()
{
	document.location="index.php?cmd=doemptybasket";
}

function viewResource(id)
{
	filename="resources/resource_"+id+".jpg";
	params=", resizable";
	if(isImageLargerThan(filename, 800, 600)) params+=", scrollbars";
	openWindow("resourceviewer.php?id="+id, "rv", 10, 10, params);
}

function printCheckoutOrder(id)
{
	openWindow("printorder.php?id="+id, "po", 800, 600, ", scrollbars, status, menubar");
}


function relocateCategorySelection(selectBox)
{
	selectedIndex=selectBox.selectedIndex;
	category_id=selectBox.options[selectedIndex].value;
	window.location="index.php?cmd=viewcat&id="+category_id;
}

function relocateProductSelection(selectBox)
{
	selectedIndex=selectBox.selectedIndex;
	product_id=selectBox.options[selectedIndex].value;
	window.location="index.php?cmd=viewproduct&id="+product_id;
}


function showList() 
{ 
	window.name="main";
	window.open('http://www.alcaig.com/templates/tartanviewer3/A.html','viewer','resizable=no,width=400,height=500,scrollbars');
}

function showList2() 
{ 
	window.name="main";
	window.open('http://www.alcaig.com/templates/dancerstartans/dancerstartans.php','viewer','resizable=no,width=400,height=500,scrollbars');
}

function showList3() 
{ 
	window.name="main";
	window.open('http://www.alcaig.com/templates/clancrest/clancrest.php','viewer','resizable=no,width=400,height=500,scrollbars');
}

function showList4() 
{ 
	window.name="main";
	window.open('http://www.alcaig.com/templates/tartanviewer2/A.html','viewer','resizable=no,width=400,height=500,scrollbars');
}

function showList5() 
{ 
	window.name="main";
	window.open('http://www.alcaig.com/templates/cantleviewer/cantleviewer.php','viewer','resizable=no,width=450,height=500,scrollbars');
}