/**
 *   Copyright (c) 2005 Concise Web Design Limited.
 *       All Rights Reserved
 *
 *   This work is an unpublished work and contains confidential, proprietary,
 *   and trade secret information of Concise Web Design Limited.  Access to this
 *   work is restricted.  No part of this work may be used, practiced, performed,
 *   copied, distributed, reproduced, revised, modified, translated,
 *   abridged, condensed, expanded, collected, compiled, linked, recast,
 *   transformed, adapted, or reverse engineered without the prior written
 *   consent of Concise Web Design Limited.  Any use or exploitation of this work
 *   without express authorization could subject the perpetrator to criminal
 *   and civil liability.
 *
 *   Author: David Simpson
 */
  
/**
*	@param myObj The form field object
*
*	Pass the price (value) of a pulldown to it's associated charge field
*/
function updateCharge(myObj){
	frm = myObj.form;
	frm.elements[myObj.name + " Charge"].value = myObj[myObj.selectedIndex].value;
}
/**
*	@param object myObj The form field object
*	@param String[] myArray The form field object
*
*	Pass the price (value) of a pulldown's charge array to it's associated charge field
*/
function doPrice(myField,myArray){
	if (myField.form.elements[0].value == "insert")
	{
		document.getElementById("product-price").innerHTML = "&#163;" + formatAsMoney(myArray[myField.selectedIndex]);
		document.getElementById("product-price-discount").innerHTML = "&#163;" + formatAsMoney(myArray[myField.selectedIndex]*discount_multiplier);
		if (document.getElementById("currConv"))
		{
			if (document.getElementById("currConv").innerHTML != '')
			{
				getExchangeRatesForProduct();
			}	
		}
	} else 	
	{
		myField.form.elements[getFormIndex(myField)+1].value = formatAsMoney(myArray[myField.selectedIndex]);
	}
} 
function doRadioPrice(myField,myValue){
	document.getElementById(myField).value = formatAsMoney(myValue);
} 
function doTextareaPrice(myField,myValue){
	if (myField.value == ""){
		myField.form.elements[getFormIndex(myField)+1].value = "0.00";
	} else {
		myField.form.elements[getFormIndex(myField)+1].value = formatAsMoney(myValue);
	}
}
function formatAsMoney(mnt) {
	// Format a number to 2 decimal places
    mnt -= 0;
    mnt = (Math.round(mnt*100))/100;
    return (mnt == Math.floor(mnt)) ? mnt + '.00' 
              : ( (mnt*10 == Math.floor(mnt*10)) ? 
                       mnt + '0' : mnt);
} 
/**
*	@param object input The form field object
*	@return int formIndex
*
*	Find the index of the current form object
*/
function getFormIndex(input) {
	var index = -1, i = 0, found = false;
	while (i < input.form.length && index == -1) {
		if (input.form[i] == input) {
			index = i;
			break;
		} else {
			i++;
		}
	}
	return index;
}

function openWin(theURL,winName,features){
	window.open(theURL,winName,"scrollbars=auto,resizable=yes,"+features);
}
function popupCalPHP(fieldName,dateFormat) {
	window.open(
		"popup_calendar.php?fieldName="+fieldName+"&date_format="+dateFormat,
		"popupCalendar",
		"width=260,height=240"
	);	
}
function popupPickVal(myVal,fieldID){
	window.opener.document.getElementById(fieldID).value = myVal;
	window.opener.document.getElementById(fieldID).focus();
	self.close();
}
function showProductAttributes(myFrmField, attrID){
	openWin(
		"popup_product_attributes.php?ID="+attrID+"&returnField="+myFrmField,
		"popupAttr",
		"scrollbars=auto,resizable=yes,width=600,height=450"
	);
}
// 2006-06-19 DAS
function toggle(channel, state){
	// create IDs based on channel name
	var More = channel + 'More';
	var MoreText = channel + 'MoreText';
	var Less = channel + 'Less';
	if (state == "more"){
	  // hide More button; show More text and Less button
	  // 'inline' not 'block' so More/Less buttons on same line as parent text
	  document.getElementById(More).style.display = 'none';
	  document.getElementById(MoreText).style.display = 'inline';
	  document.getElementById(Less).style.display = 'inline';
	} else {
	  // hide Less button and More text; show More button
	  document.getElementById(Less).style.display = 'none';
	  document.getElementById(MoreText).style.display = 'none';
	  document.getElementById(More).style.display = 'inline';
	}
}

/**
 * 2006-03
 */
function updatePulldownImg (myObj,myArray){
	var id = "img_"+myObj.id; // The id of the associated image
	var thisUrl = "./"+myArray[myObj.selectedIndex];
	var thisStyle = "#fff url("+thisUrl+") no-repeat";
	document.getElementById(id).src = thisUrl;
//	document.getElementById(id).style.background = thisStyle;
} 
/**
 * 2006-05-17
 */
function updatePulldownWording (item_id,myObj,myArray){
	var id = "img_"+myObj.id; // The id of the associated image
	var thisUrl = "./wording_gif.php?order_item_id="+item_id+"&font="+myArray[myObj.selectedIndex];
	if (myObj.selectedIndex == 0){
		thisUrl = "./images/blank.gif";
	}
	// alert(thisUrl);
	document.getElementById(id).src = thisUrl;
	document.getElementById(id).style.border = "1px solid #ccc";
} 

function getExchangeRates(amountToConvert,outputTo)
{	
	$(outputTo).ajaxStart(function(){
	   $(this).empty();
	   $(this).append('<span class="loading">Loading...</span>');
	 });
	$.ajax({
	   type: "GET",
	   url: "/catalog/currencyconverter.php",
	   data: "amount=" + amountToConvert,
	   success: function(msg){
	     $(outputTo).empty();
	     $(outputTo).append( msg );
	   },
	   error: function(msg){
	     $(outputTo).empty();
	     $(outputTo).append( 'Sorry. Cannot connect to currency convertor website.' );
	   }
	 });
}
function getExchangeRatesForProduct()
{
	 getExchangeRates($('#product-price-discount').html().substring(1),'#currConv');
}
function getExchangeRatesForCartTotal()
{
	 getExchangeRates($('#cart-gross-total').html(),'#currConv');
} 


/*

// Fix to allow use of images in label nodes
// from http://snook.ca/archives/javascript/using_images_as/
// TODO change this to a working jquery version
window.onload = function()
{
	if(document.all && navigator.appVersion.indexOf("MSIE")>-1 && navigator.appVersion.indexOf("Windows")>-1)
	{
		var a = document.getElementsByTagName("label");
		for(var i=0,j=a.length;i<j;i++)
		{
			if(a[i].hasChildNodes && a[i].childNodes.item(0).tagName == "img")
			{
				a[i].onclick = function()
				{
					var e = document.getElementById(this.htmlFor);
					switch(e.type)
					{
						case "radio": e.checked|=1;break;
						case "checkbox": e.checked=!e.checked;break;
						case "text": case "password": case "textarea": e.focus(); break;
					}
				}
			}
		}
	}
}

*/
