
function currencyChange($currencyCode){
	new Ajax.Request('/currencies/change_currency/'+$currencyCode,
	{
		method: 'post',
		onSuccess: function(transport){
			//change the currency symbol and prices
			window.location.reload();
		}
	});
}


 

 
 function calculatePrice(addToCheckout){
	 	$form = $('orderForm');

	//get the chosen fabric (Front) - only one fabric for all over covers
	$swatchID =  $('fabricCode').value;//getRadioFieldValue('allOverCoverFabric');
	$fabricDescription = $('fabricDescription').value;
	$swatchName = $fabricDescription;
	
	//console.debug($swatchID);
	//get the chosen fit option - simple or standard fit
	$fitOption =  getRadioFieldValue('fitType')-1;
	$fitOptionText =  getRadioFieldId('fitType');
	if($fitOption==-1) $fitOption = 0;
	//$fitOptionText = ($fitOption == 0) ? 'Standard Fit' : 'Premier Fit';
	
	//how many covers
	$sofaQTY = ($('sofaCoverAmount') ) ? $('sofaCoverAmount').value : 0;
	$chairQTY = $form['chairCoverAmount'].value;
	
	$currentCount =	parseInt($('basketCount').innerHTML);
	
	$chairType = ($('chairType')) ? $('chairType').value : '';
	$productType = ($chairType == 'Dining Chair') ? 3 : 2;
	$productType = ($chairType == 'Loose Cushion Cover') ? 4 : $productType;
	//console.debug($sofaQTY+' '+$chairQTY);
	sofaUpdateObject = {'currentCount' : $currentCount , 'qty' : $sofaQTY , 'product' : 1, 'productDescription' : 'Sofa Cover' , 'fabric' : $fabricDescription , 'swatch' : $swatchID, 'swatchid' : $swatchID, 'swatchName' : $swatchName , 'fitOption' : parseInt($fitOption)+1, 'fitOptionText' : $fitOptionText};
	if($sofaQTY !=0) $currentCount++;
	chairUpdateObject = {'currentCount' : $currentCount , 'qty' : $chairQTY , 'product' : $productType, 'productDescription' : $chairType , 'fabric' : $fabricDescription , 'swatch' : $swatchID, 'swatchid' : $swatchID, 'swatchName' : $swatchName , 'fitOption' : parseInt($fitOption)+1, 'fitOptionText' : $fitOptionText};

	$combinedUpdateObject = [sofaUpdateObject , chairUpdateObject];
	//console.debug($allOverCoverLine);
	
	//setAllOverCoverOptionList($allOverCoverLine);
	
	checkoutSave = (addToCheckout=='undefined') ? false : addToCheckout;
	ajaxGetPrice($combinedUpdateObject,checkoutSave);

 }
 
 function ajaxGetPrice($combinedUpdateObject,checkoutSave){
	 //console.debug($combinedUpdateObject);
	 pars = 'array='+$combinedUpdateObject.toJSON();
	if(checkoutSave == true){ 
		pars += '&save=true';
	}
	
		 new Ajax.Request('/checkout/getPrice', {method:'post', parameters : pars , onSuccess : function(transport) { 
														
			 prices = eval('(' + transport.responseText + ')');
			 $conversionRate = $('conversionRate').value;
			 $currencySymbol = $('currencySymbol').value;
			 prices.discountPrice = Math.floor(prices.discountPrice);
			  //console.debug('here');
			 if($('fullPrice')) $('fullPrice').innerHTML = $currencySymbol+Math.round(prices.fullPrice * $conversionRate);
			
			 $('priceFigure').innerHTML = '<strong>'+$currencySymbol+Math.round(prices.discountPrice * $conversionRate)+'</strong>';
			 if(checkoutSave == true){ addPriceToCheckoutTotal(prices.discountPrice);
				new Ajax.Updater('stage4basketSummary', '/checkout/stage4BasketSummary');
			 }
				if(checkoutSave == true){ 
					writeBasketLink($combinedUpdateObject[0]);
					writeBasketLink($combinedUpdateObject[1]);
				}																					 
			 
			 
			//$('checkoutLink').style.display = 'auto';
		 } 
		});
		}

 
 function writeBasketLink(updateObject){
 		//current count shown in the basket panel
		$currentCount =	parseInt($('basketCount').innerHTML);	
		if(updateObject.qty == 0) return $currentCount;
		
		$qty = updateObject.qty ;
		$productDescription = updateObject.productDescription;
		$fabric = updateObject.fabric;
		
		
		$stringKey = ''+updateObject.product+updateObject.swatchid+(parseInt(updateObject.fitOption)-1);
		
		if($('orderLine'+$stringKey)){
			//line esists do just increment the qty on it

			$oldQty = parseInt($('orderDescription'+$stringKey).innerHTML);
			$qty = parseInt($qty)+$oldQty;
			$('orderDescription'+$stringKey).innerHTML = $qty+' x '+$productDescription+' - '+$fabric;
			
		}
		else{
		$orderLine = new Element('div', {'id' : 'orderLine'+$stringKey , 'class' : 'orderLine'});
		$deleteIcon = 	new Element('div' , {'class' : 'orderDelete'});
		//$currentCount = $currentCount+1;
		
		//$deleteIcon.innerHTML = '<a href="/checkout/remove_line?ajax=false&index='+(parseInt($currentCount)+1)+'" title="Click To Remove" onClick="shopRemoveLine(\''+(parseInt($currentCount)+1)+'\'); return false;" ><img src="/images/deleteIcon.png" /></a>'
		$orderLine.insert($deleteIcon);
		//$orderLine.innerHTML += '<span id="orderDescription'+$stringKey+'">'+$qty+' x '+$productDescription+' - '+$fabric+'</span></div>';
		$('basketOrder').insert($orderLine);
		}
		
		window.scroll(0,0);
		new Effect.Pulsate('orderLine'+$stringKey);
		$currentCount++;
		
		$('basketCount').innerHTML = $currentCount;
		
		//$('checkoutLink').style.display = 'none';
		return $currentCount++;
 }

 function callUpdateBasket(updateObject){
	
		pars = 'array='+updateObject.toJSON();
		new Ajax.Request('/checkout/update/',
		{
			method:'post',
			parameters: pars,
			onSuccess: function(transport){
				price = transport.responseText;
				conversionRate = ($('conversionRate')) ? $('conversionRate').value : 1;
				currencySymbol = ($('currencySymbol')) ? $('currencySymbol').value : 1;
				$('priceValue').innerHTML = currencySymbol+Math.round(price*conversionRate);
				new Ajax.Updater('stage4basketSummary', '/checkout/stage4BasketSummary');
			}
		});
	
 }

 
 function shopRemoveLine($key){
	 //console.debug($key);
	 new Ajax.Request('/checkout/remove_line?index='+$key, {method:'get', onSuccess : function(transport) { 
		//console.debug($('orderLine'+$key));
		$('orderLine'+$key).remove();
		//$currentPrice = $('priceValue').innerHTML.replace('&pound;','');
		//$currentPrice = $currentPrice.replace('£','');
		//console.debug('price is '+$currentPrice);
		$('priceValue').innerHTML = '&pound;'+transport.responseText;
		$('basketCount').innerHTML = parseInt($('basketCount').innerHTML)-1;													
		if($checkout!='undefined') window.location.replace("/checkout/showCheckout");
		else{
			new Ajax.Updater('stage4basketSummary', '/checkout/stage4BasketSummary');
		}
				 } 
	});
	 
	 //alert($key);
 }
 
/* *********************************************************
     * You may use this code for free on any web page provided that 
     * these comment lines and the following credit remain in the code.
     * Floating Div from http://www.javascript-fx.com
     ******************************************************** 
	 
<div id="divTopLeft"     style="position:absolute">
<!-- Start - put your content here --->
<b>Your Content Here</b>
<!-- End   - put your content here --->
</div>
*/


var ns = (navigator.appName.indexOf("Netscape") != -1);
var d = document;
function JSFX_FloatDiv(id, sx, sy)
{
	var el=d.getElementById?d.getElementById(id):d.all?d.all[id]:d.layers[id];
	var px = document.layers ? "" : "px";
	window[id + "_obj"] = el;
	if(d.layers)el.style=el;
	el.cx = el.sx = sx;el.cy = el.sy = sy;
	//var x = Position.cumulativeOffset($('col3'))[0];
	//var x =  '200';
	el.sP=function(x,y){if(!$('scrollLimiter') ||  y < Position.cumulativeOffset($('scrollLimiter'))[1] ){ this.style.left=$('col3').offsetLeft+30+px;this.style.top=y+px;} 
};

	el.floatIt=function()
	{
		var pX, pY;
		pX = (this.sx >= 0) ? 0 : ns ? innerWidth : 
		document.documentElement && document.documentElement.clientWidth ? 
		document.documentElement.clientWidth : document.body.clientWidth;
		pY = ns ? pageYOffset : document.documentElement && document.documentElement.scrollTop ? 
		document.documentElement.scrollTop : document.body.scrollTop;
		if(this.sy<0) 
		pY += ns ? innerHeight : document.documentElement && document.documentElement.clientHeight ? 
		document.documentElement.clientHeight : document.body.clientHeight;
		this.cx += (pX + this.sx - this.cx)/8;this.cy += (pY + this.sy - this.cy)/8;
		this.sP(this.cx, this.cy);
		setTimeout(this.id + "_obj.floatIt()", 40);
	}
	calculatePrice();
	return el;
}
