  	
  	
function cartKeyPress(ev, idForm)
	{
	var keynum='';
	if(window.event) // IE
		{
		keynum = ev.keyCode;
		}
	else if(ev.which) // Netscape/Firefox/Opera
		{
		keynum = ev.which;
		}
		
	if (keynum == 13)
		{
	 	cartFrmRecalculate(idForm);
	 	return false;
	 	}
	return true;	 	
  	}


function cartFrmSubmitAction (idForm, strAction)
	{
	if (!cartQuantityFocus(idForm, '')) return;  // quantity field failed validation
	
	var frm = document.getElementById(idForm);
	frm.param_action.value=strAction;
	frm.submit();
	}
	
function cartFrmCheckout(idForm)
	{
	if (!cartQuantityFocus(idForm, '')) return;  // quantity field failed validation

	frm = document.getElementById(idForm);
	frm.action=document.getElementById('checkout_url').value;
	frm.submit();
	}

function cartFrmGoogleCheckout(idForm)
	{
	if (!cartQuantityFocus(idForm, '')) return;  // quantity field failed validation

	setUrchinInputCode(pageTracker);
	
	frm = document.getElementById(idForm);
	frm.action=document.getElementById('checkout_url').value;
	frm.submit();
	}
	
	
	
function cartFrmRecalculate(idForm)
	{
	if (!cartQuantityFocus(idForm, '')) return;  // quantity field failed validation

	if (document.getElementById('param_recalculate').value == '1')
		{
		document.getElementById(idForm).submit();
		}
	}

function cartFrmDelete(idForm)
	{
	if (!cartQuantityFocus(idForm, '')) return;  // quantity field failed validation
	
	// add check that a delete value was selected

	document.getElementById('param_delete').value = '1';
	document.getElementById(idForm).submit();
	}

function cartFrmClear(idForm)
	{
	
	document.getElementById('param_clear').value = '1';
	document.getElementById(idForm).submit();
	}

	
var idQuantityFocus='';

function cartQuantityFocus(idForm, inputID)
	{
	if (idQuantityFocus != '' && idQuantityFocus != inputID)
		{
		if(!cartCheckQuantity(idForm, idQuantityFocus))	
			{
			document.getElementById(idQuantityFocus).focus();
			return false;
			}
		document.getElementById('param_recalculate').value = '1';
		}
		
	idQuantityFocus = inputID;
	return true;
	}
	
function cartCheckQuantity(idForm, inputID)
	{
	var frm = document.getElementById(idForm);
	var elem = document.getElementById(inputID);
	var decimals = (document.getElementById('param_decimals').value == '1');
	
	if (decimals)
		{
		if (elem.value == '' || isNaN(elem.value) || elem.value < 0)
			{
			alert(arrLanguage[0]+elem.value+arrLanguage[1]);
			return false;
			}
		
		}
	else
		{
		if (elem.value == '' || isNaN(elem.value) || Math.round(elem.value) != elem.value || elem.value < 0)
			{
			alert(arrLanguage[0]+elem.value+arrLanguage[2]);
			return false;
			}
		}
	
	return true;
	}
	

// CHECKOUT FORM FUNCTIONS //////////////////////////////////////////////////////////

var bShippingChange = false;
var dbTax1Rate = 0;
var dbTax2Rate = 0;

function apiShippingChange()
		//if shipping type is not fedex return
		//
		//set shippingChangeFlag
		//disable 'submit' button
		//enable 'calculate shipping' button (on click=apiShippingCall())
	{
		if (shippingType != 'fedex' && shippingType != 'ups')
			return;
		bShippingChange=true;
		
		document.frmCheckout.param_button.style.display='none';
		document.frmCheckout.param_recalculate.style.display='block';
		
		document.getElementById('a_update_shipping').style.display='block';
		document.getElementById('sp_shipping').style.display='none';

	}

function apiShippingCall(bAlertError)
		//if shipping type is not fedex return
		//
		//ajax call
		//handler is apiShippingReturn()
	{
		if (shippingType != 'fedex' && shippingType != 'ups')
			return;
			
		if (!apiShippingValidate(bAlertError))
			return;

		strUrl = thisurl+"inc/shipping.php";
		strUrl += "?param_shipping_city=" + encodeURI(document.getElementById('param_shipping_city').value);
		strUrl += "&param_shipping_zip=" + encodeURI(document.getElementById('param_shipping_zip').value);
		strUrl += "&param_shipping_province=" + encodeURI(document.getElementById('param_shipping_province').value);
		strUrl += "&param_shipping_country=" + encodeURI(document.getElementById('param_shipping_country').value);
		
		if (shippingType == 'ups' || shippingType == 'fedex')
			strUrl += "&param_shipping_method_id=" + encodeURI(document.getElementById('param_shipping_method_id').value);
			
		xmlHttp = fncjsGlobalHttpRequest();
		xmlHttp.onreadystatechange=apiShippingReturn;
		xmlHttp.open("GET", strUrl ,true);
		xmlHttp.send(null);
	}
	
function apiShippingValidate(bAlertError)
	{
	 var flag_alert = 'ok';
	 var str_alert = arrLanguage[3]+'\n';
	 if('' == fncjsGlobalTrim(document.frmCheckout.param_shipping_city.value))
		{ 
		flag_alert = 'LotanShakked'; str_alert += arrLanguage[4]+'\n'; 
		}
	 if('' == fncjsGlobalTrim(document.frmCheckout.param_shipping_zip.value))
		{ 
		flag_alert = 'LotanShakked'; str_alert += arrLanguage[5]+'\n'; 
		}
	 if('' == fncjsGlobalTrim(document.frmCheckout.param_shipping_country.value))
		{ 
		flag_alert = 'LotanShakked'; str_alert += arrLanguage[6]+'\n'; 
		}
	 if(document.frmCheckout.param_shipping_province.length > 1 && '' == fncjsGlobalTrim(document.frmCheckout.param_shipping_province.value))
		{ 
		flag_alert = 'LotanShakked'; str_alert += arrLanguage[7]+'\n'; 
		}
	if (shippingType == 'ups' || shippingType == 'fedex')
		{
		if('' == fncjsGlobalTrim(document.getElementById('param_shipping_method').value))
			{ 
			flag_alert = 'LotanShakked'; 
			str_alert += arrLanguage[9]+'\n'; 
			}
		}


	 if ('LotanShakked' == flag_alert)
	 	{ 
	 	if (bAlertError)
	 		alert(str_alert); 
	 	return false;
	 	}
	 else 
	 	return true;
	}
	
function apiShippingReturn()
		//if shipping type is not fedex return
		//
		//update the shipping price
		//unset shippingChange flag
		//calculateTotals()
	{
		
		if (xmlHttp.readyState != 4) return;
		
		if (shippingType != 'fedex' && shippingType != 'ups')
			return;
			
		if (isNaN(xmlHttp.responseText))
			{
			apiShippingChange();
			alert(xmlHttp.responseText);
			return;
			}
			
		bShippingChange = false;
		flShippingPrice = parseFloat(xmlHttp.responseText);
		document.getElementById('param_shipping_price').value = flShippingPrice;
		document.getElementById('sp_shipping_price').innerHTML = 
			formatFixed2(flShippingPrice);

		document.getElementById('a_update_shipping').style.display='none';
		document.getElementById('sp_shipping').style.display='block';

		document.frmCheckout.param_button.style.display='block';
		document.frmCheckout.param_recalculate.style.display='none';

		calculateTotals();
		
		window.location='#a_ecommerce_top';

	}
	
function countryChange(strCountry) 
	{
	var strProvince;
	var strDivProvince;
	
	switch (strCountry)
		{
		case 'param_billing_country':
			strProvince = 'param_billing_province';
			strDivProvince = 'div_billing_province';
			break;

		case 'param_shipping_country':
			strProvince = 'param_shipping_province';
			strDivProvince = 'div_shipping_province';
			switch (shippingType)
				{
				case 'fedex':
				case 'ups':
					populateShippingMethod();
					break;
				}
			break;
			
		default:
			return;
			break;
		}
		
	cmsbuffetPopulateProvince(strCountry, strProvince);
	
	if (document.getElementById(strProvince).length <= 1)
		document.getElementById(strDivProvince).style.display='none';
	else
		document.getElementById(strDivProvince).style.display='block';
		
	if (strCountry == 'param_shipping_country')
		{
		apiShippingChange();
		
		if (document.getElementById(strProvince).length <= 1 || shippingType == 'country_flat')
			countryTax();	
		}
	}
	
function countryTax()  
	{
	var bUseShipping;
	var iCountry = document.getElementById('param_shipping_country').value;
	var dbTaxable = document.getElementById('param_total_taxable').value;
	var dbSubtotal = parseFloat(document.getElementById('sp_subtotal').innerHTML);
	var dbWeight = parseFloat(document.getElementById('param_total_weight').value);
	
	switch (shippingType)
		{
		case 'state_price':
		case 'state_weight':
		case 'state_flat':
		case 'country_flat':
			bUseShipping=true;
			break;
		default:
			bUseShipping=false;
			break;
		}
	
	dbTax1Rate = 0;
	dbTax2Rate = 0;
	var dbShipping = 0;

	for (var iCount = 0; iCount < arr_cmsbuffet_country.length; ++iCount)
		{
		if ( arr_cmsbuffet_country[iCount].countrycode == iCountry)
			{
			if (bUseShipping)
				dbShipping = parseFloat(arr_cmsbuffet_country[iCount].countryshipping);
			dbTax1Rate = arr_cmsbuffet_country[iCount].countrytax1;
			dbTax2Rate = arr_cmsbuffet_country[iCount].countrytax2;
			
			break;

			}
		}


	switch (shippingType)
		{
		case 'state_price':
			dbShipping = dbShipping*dbSubtotal/100.;
			break;
		case 'state_weight':
			dbShipping = dbShipping*dbWeight;
			break;
		default:
			break;
		}

	if (bUseShipping)
		{
		document.getElementById('sp_shipping_price').innerHTML = formatFixed2(dbShipping);
		document.getElementById('param_shipping_price').value = dbShipping;
		}
		
	calculateTotals();
	}
	
function calculateTotals()
	{
	if (bShippingChange)
		{
		document.getElementById('sp_total').innerHTML = "0.00";
		document.getElementById('param_subtotal').value = 0;
		document.getElementById('param_total').value = 0;
		document.getElementById('sp_shipping_price').innerHTML = "0.00";
		document.getElementById('param_shipping_price').value = 0;
		document.getElementById('sp_tax_price').innerHTML = 0;
		document.getElementById('param_tax1_price').value = 0.;
		document.getElementById('param_tax2_price').value = 0.;
		return;
		}
	
	var bUseShipping;
	switch (shippingType)
		{
		case 'state_price':
		case 'state_weight':
		case 'state_flat':
		case 'country_flat':
		case 'fedex':
		case 'ups':
			bUseShipping=true;
			break;
		default:
			bUseShipping=false;
			break;
		}

	var dbSubtotal = parseFloat(document.getElementById('sp_subtotal').innerHTML);
	var dbWeight = parseFloat(document.getElementById('param_total_weight').value);
	var dbShipping = 0;
	
	if (bUseShipping)
		dbShipping = parseFloat(document.getElementById('param_shipping_price').value);

	var dbTaxable = document.getElementById('param_total_taxable').value;
	var dbTax1 = Math.round((parseFloat(dbTaxable)+parseFloat(dbShipping)) * (parseFloat(dbTax1Rate)/100.) * 100.) / 100.;
	var dbTax2 = Math.round((parseFloat(dbTaxable)+parseFloat(dbShipping)) * (parseFloat(dbTax2Rate)/100.) * 100.) / 100.;

	document.getElementById('sp_tax_price').innerHTML = formatFixed2(dbTax1 + dbTax2);
	document.getElementById('param_tax1_price').value = dbTax1;
	document.getElementById('param_tax2_price').value = dbTax2;
	
	var dbTotal = dbSubtotal + dbShipping + dbTax1 + dbTax2;

	document.getElementById('sp_total').innerHTML = formatFixed2(dbTotal);
	document.getElementById('param_subtotal').value = dbSubtotal;
	document.getElementById('param_total').value = dbTotal;
	}
	
function onBillingShippingChange()
	{
	if (shippingType == 'none') 
		return;
	
	if (document.getElementById("param_same_as").checked)
		{

		document.getElementById("param_shipping_address1").value =
			document.getElementById("param_billing_address1").value ;

		document.getElementById("param_shipping_address2").value =
			document.getElementById("param_billing_address2").value ;

		document.getElementById("param_shipping_city").value =
			document.getElementById("param_billing_city").value ;

		document.getElementById("param_shipping_zip").value =
			document.getElementById("param_billing_zip").value ;

		document.getElementById("param_shipping_phone1").value =
			document.getElementById("param_billing_phone1").value ;

		document.getElementById("param_shipping_phone2").value =
			document.getElementById("param_billing_phone2").value ;


		document.getElementById("param_shipping_country").selectedIndex =
			document.getElementById("param_billing_country").selectedIndex ;
		countryChange('param_shipping_country');

		document.getElementById("param_shipping_province").selectedIndex =
			document.getElementById("param_billing_province").selectedIndex ;
		
		shippingDisable(true);
		}
	else
		{
		shippingDisable(false);

		document.getElementById("param_shipping_address1").value = "";

		document.getElementById("param_shipping_address2").value = "";

		document.getElementById("param_shipping_city").value = "";

		document.getElementById("param_shipping_zip").value = "";

		document.getElementById("param_shipping_phone1").value = "";

		document.getElementById("param_shipping_phone2").value = "";

		document.getElementById("param_shipping_country").selectedIndex = 0;
		
		countryChange('param_shipping_country');

		}
	provinceChange();
	}
	
function provinceChange()
	{
	var strProvince = (shippingType == 'none') ? 'param_billing_province' : 'param_shipping_province';
	
	if(document.getElementById(strProvince).length <= 1)
		return;
		
	var iProvince = document.getElementById(strProvince).value;
	
	var dbWeight = parseFloat(document.getElementById('param_total_weight').value);
	var dbTaxable = document.getElementById('param_total_taxable').value;
	var dbSubtotal = parseFloat(document.getElementById('sp_subtotal').innerHTML);
	
	switch (shippingType)
		{
		case 'state_price':
		case 'state_weight':
		case 'state_flat':
			bUseShipping=true;
			break;
		default:
			bUseShipping=false;
			break;
		}
	
	var dbShipping = 0;
	dbTax1Rate = 0;
	dbTax2Rate = 0;

	for (var iCount = 0; iCount < arr_cmsbuffet_province.length; ++iCount)
		{
		if ( arr_cmsbuffet_province[iCount].provincecode == iProvince)
			{
			if (bUseShipping)
				dbShipping = parseFloat(arr_cmsbuffet_province[iCount].provinceshipping);
			dbTax1Rate = arr_cmsbuffet_province[iCount].provincetax1;
			dbTax2Rate = arr_cmsbuffet_province[iCount].provincetax2;
			
			break;
			}
		}

	switch (shippingType)
		{
		case 'state_price':
			dbShipping = dbShipping*dbSubtotal/100.;
			break;
		case 'state_weight':
			dbShipping = dbShipping*dbWeight;
			break;
		default:
			break;
		}
	
			
	if (bUseShipping)
		{
		document.getElementById('sp_shipping_price').innerHTML = formatFixed2(dbShipping);
		document.getElementById('param_shipping_price').value = dbShipping;
		}
		
	if (shippingType == 'fedex' || shippingType == 'ups')
		apiShippingChange();
		
	calculateTotals();
	}


function shippingDisable(bValue)
	{
	if (shippingType == 'none') return;
	
	document.getElementById("param_shipping_address1").disabled = bValue;
	document.getElementById("param_shipping_address2").disabled = bValue;
	document.getElementById("param_shipping_city").disabled = bValue;
	document.getElementById("param_shipping_zip").disabled = bValue;
	document.getElementById("param_shipping_phone1").disabled = bValue;
	document.getElementById("param_shipping_phone2").disabled = bValue;
	document.getElementById("param_shipping_country").disabled = bValue;
	document.getElementById("param_shipping_province").disabled = bValue;
	}

function checkoutSubmit()
	{
	shippingDisable(false);
	fncjsLocalCheckform();
	
	// this statement is reached only if validation fails
	if (shippingType != 'none')
		shippingDisable(document.getElementById("param_same_as").checked);
	}


function formatFixed2(iNumber)
	{
	return '' + iNumber.toFixed(2);
	}

function checkoutOnLoad()
	{
	countryChange('param_billing_country');
	document.getElementById('param_billing_province').value=
		document.getElementById('param_billing_province_initial').value;
	
	if (shippingType == 'none') 
		{
		provinceChange();
		}
	else
		{
		countryChange('param_shipping_country');
		document.getElementById('param_shipping_province').value=
			document.getElementById('param_shipping_province_initial').value;

		provinceChange();

		if(document.getElementById("param_same_as").checked)
			shippingDisable(true);
		}
	}
	
// SHIPPING METHODS

function populateShippingMethod()
	{
	switch(shippingType)
		{
		case 'ups':
			populateShippingMethodUPS();
			break;
		case 'fedex':
			populateShippingMethodFedex();
			break;l
		}
	}
	
function onShippingMethodChange()
	{
	var elShippingMethod = document.getElementById('param_shipping_method');
	document.getElementById('param_shipping_method_name').value = 
		elShippingMethod.options[elShippingMethod.selectedIndex].text;
	document.getElementById('param_shipping_method_id').value = 
		elShippingMethod.options[elShippingMethod.selectedIndex].value;
	
	apiShippingCall(false);
	}
	
function populateShippingMethodUPS()
	{
	var iDestCountry = document.getElementById('param_shipping_country').value;
	if (iDestCountry == '')
		return;
		
	// get the country name of the destination country
	strDestCountry = arr_cmsbuffet_country[iDestCountry].countryname;
	var elemMethod = document.getElementById('param_shipping_method');
	elemMethod.length = 0 ;

	try
		{
		elemMethod.add(new Option('----', ''), null);
		}
	catch(ex) // IE is not compliant with the standard
		{
		elemMethod.add(new Option('----', ''));
		}
	
	var arrMethods = shippingMethodsForCountriesUPS(shipperCountry,iDestCountry);
	for(var i=0;i<arrMethods.length;++i)
		{
		try
			{
			elemMethod.add(new Option(arrMethods[i].value,	arrMethods[i].key), null);
			}
		catch(ex)  // IE is not compliant with the standard
			{
			elemMethod.add(new Option(arrMethods[i].value,	arrMethods[i].key));
			}
		}
	}
	
function shippingMethodsForCountriesUPS(strSource,strDest)
	{
	var arrOut = new Array();
	
	strInput = strSource+':'+strDest;
	for(var i=0; i < arrUPSCountry.length; ++i)
		{
		var strKey = '^'+arrUPSCountry[i].key.replace('*','.*')+'$';
		if (strInput.search(strKey) >= 0)
			{
			var strCodes = arrUPSCountry[i].value;
			var k = 0;
			for (var j=0; j < arrUPSName.length; ++j)
				{
				if (strCodes.search(arrUPSName[j].key) >= 0)
					{
					arrOut[k] = arrUPSName[j];
					++k;
					}				
				}
			return arrOut;
			}
		}
	return arrOut;
	}

function populateShippingMethodFedex()
	{
	
	var iDestCountry = document.getElementById('param_shipping_country').value;
	if (iDestCountry == '')
		return;
		
	// get the country name of the destination country
	strDestCountry = arr_cmsbuffet_country[iDestCountry].countryname;
	
	var elemMethod = document.getElementById('param_shipping_method');
	elemMethod.length = 0 ;
	try
		{
		elemMethod.add(new Option('----', ''), null);
		}
	catch(ex) // IE is not compliant with the standard
		{
		elemMethod.add(new Option('----', ''));
		}
	
	
	var arrMethods = shippingMethodsForCountriesFedex(shipperCountry,iDestCountry);
	
	for(var i=0;i<arrMethods.length;++i)
		{
		try
			{
			elemMethod.add(new Option(arrMethods[i].value,	arrMethods[i].key), null);
			}
		catch(ex) // IE is not compliant with the standard
			{
			elemMethod.add(new Option(arrMethods[i].value,	arrMethods[i].key));
			}
		}
	}
	

function shippingMethodsForCountriesFedex(strSource,strDest)
	{
	var arrOut = new Array();
	
	strInput = strSource+':'+strDest;
	for(var i=0; i < arrFedexCountry.length; ++i)
		{
		var strKey = '^'+arrFedexCountry[i].key.replace('*','.*')+'$';
		if (strInput.search(strKey) >= 0)
			{
			var strCodes = arrFedexCountry[i].value;
			var k = 0;
			for (var j=0; j < arrFedexName.length; ++j)
				{
				if (strCodes.search(arrFedexName[j].key) >= 0)
					{
					arrOut[k] = arrFedexName[j];
					++k;
					}				
				}
			return arrOut;
			}
		}
	return arrOut;
	}

	
function aArray(key,value)
	{
	this.key = key;
	this.value = value;
	}
	
