function showBasket() {
	new Ajax.Updater('basket', '/class/plugins/minicart.php?shopURL='+shopURL, {asynchronous:true, evalScripts:true});
}
function addToBasket() {
	$('processing').show();
if($('option2')) {
	new Ajax.Updater('basket', '/class/plugins/minicart.php?etype=add&productID='+productID+'&optionID='+$('option2').value+'&shopURL='+shopURL, {asynchronous:true, evalScripts:true});
	}
else if($('option1')) {
	new Ajax.Updater('basket', '/class/plugins/minicart.php?etype=add&productID='+productID+'&optionID='+$('option1').value+'&shopURL='+shopURL, {asynchronous:true, evalScripts:true});
	}
else {
	new Ajax.Updater('basket', '/class/plugins/minicart.php?etype=add&productID='+productID+'&shopURL='+shopURL, {asynchronous:true, evalScripts:true});
	}
}
function plusBasket(productID, optionID) {
	$('processing').show();
	new Ajax.Updater('basket', '/class/plugins/minicart.php?etype=minus&productID='+productID+'&optionID='+optionID+'&shopURL='+shopURL, {asynchronous:true, evalScripts:true});
}
function minusBasket(productID, optionID) {
	$('processing').show();
	new Ajax.Updater('basket', '/class/plugins/minicart.php?etype=plus&productID='+productID+'&optionID='+optionID+'&shopURL='+shopURL, {asynchronous:true, evalScripts:true});
}
function isNumeric(input) {
   return (input - 0) == input && input.length > 0;
}
function updatePrice1() {
	var price=option1[$('option1').value]['price'];
	var discount=option1[$('option1').value]['discount'];
	$('price').innerHTML=(price-discount).toFixed(2);
	if(discount>0) {
		if($('wasPrice')) {
			$('wasPrice').innerHTML=price;
			$('wasPriceWrapper').show();
		}
		if($('savePrice')) {
			$('savePrice').innerHTML=discount;
			$('savePriceWrapper').show();
		}
	} else {
		$('savePriceWrapper').hide();
		$('wasPriceWrapper').hide();
	}	
}
function updateStock1() {
	var stock=option1[$('option1').value]["stock"];
	coptionID=option1[$('option1').value]["id"];
	if(stock<1) {
		$('basketAdd').disabled="disabled";
		$('stock').innerHTML='Out of stock';
	} else {
		$('basketAdd').disabled=false;
		$('stock').innerHTML=stock;
	}
}
            
function updateStock2() {
	var stock=option2[$('option1').value][$('option2').value]['stock'];
	coptionID=option2[$('option1').value][$('option2').value]['id'];
	if(stock<1) {
		$('basketAdd').disabled="disabled";
		$('stock').innerHTML='Out of stock';
	} else {
		$('basketAdd').disabled=false;
		$('stock').innerHTML=stock;
	}
}

function updatePrice2() {
	var price=option2[$('option1').value][$('option2').value]['price'];
	var discount=option2[$('option1').value][$('option2').value]['discount'];
	$('price').innerHTML=(price-discount).toFixed(2);
	if(discount>0) {
		if($('wasPrice')) {
			$('wasPrice').innerHTML=price;
			$('wasPriceWrapper').show();
		}
		if($('savePrice')) {
			$('savePrice').innerHTML=discount;
			$('savePriceWrapper').show();
		}
	} else {
		$('savePriceWrapper').hide();
		$('wasPriceWrapper').hide();
	}	
}
function updateOptions(optionID) {
	var elSel = $('option2');
	var i;
	for (i = elSel.length - 1; i>=0; i--) {
		elSel.remove(i);
		}
	for (var optionKey in option2[optionID]) {
		if(isNumeric(optionKey)) {
			  var elOptNew = document.createElement('option');
			  elOptNew.text = option2[optionID][optionKey]['name'];
			  elOptNew.value = option2[optionID][optionKey]['id'];
				try {
					elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
				  }
				  catch(ex) {
					elSel.add(elOptNew); // IE only
				  }
			}
		}
	if(showStock=='1') { updateStock2(); }
	updatePrice2();
}
function validate() {
	if($('country').value=='United States') { var elements=["name", "email", "address1", "town", "state", "postcode", "card_number", "cvv"]; }
	else if($('country').value=='Canada') { var elements=["name", "email", "address1", "town", "province", "postcode", "card_number", "cvv"]; }
	else { var elements=["name", "email", "address1", "town", "county", "postcode", "card_number", "cvv"]; }
var currentTime = new Date();
var month=currentTime.getMonth();
var year=currentTime.getYear();
if (year>100&&year<2000) { year=(year-100); if (year<10) { year='0'+year; } }
if (year>2000) { year=(year-2000); if (year<10) { year='0'+year; } }
var error=0;
for(i=0;i<elements.length;i++) {
	if ($(elements[i]).value=='') {
		if(elements[i]=='state') { $('ccounty').className='redalert'; }
		else if(elements[i]=='province') { $('ccounty').className='redalert'; }
		else { $('c'+elements[i]).className='redalert';  }
		error=error+1;
	} else {
		if(elements[i]=='state') { $('ccounty').className=''; }
		else if(elements[i]=='province') { $('ccounty').className=''; }
		else { $('c'+elements[i]).className='';  }
		}
	}	
if ($('card_number').value.length<16 || isNaN($('card_number').value)){ 
	error=error+1;
	$('ccard_number').className='redalert';
	} else {
	$('ccard_number').className='';	
	}	
if ($('cvv').value.length<3 || isNaN($('cvv').value)){  
	error=error+1;
	$('ccvv').className='redalert';	
	} else {
	$('ccvv').className='';	
	}

if ($('shippingTrue').checked==true) {
	if($('shipcountry').value=='United States') { var shipelements=["shipname", "shipaddress1", "shiptown", "shipstate", "shippostcode"]; }
	else if($('shipcountry').value=='Canada') { var shipelements=["shipname", "shipaddress1", "shiptown", "shipprovince", "shippostcode"]; }
	else { var shipelements=["shipname", "shipaddress1", "shiptown", "shipcounty", "shippostcode"]; }
	for(i=0;i<shipelements.length;i++) {
	if ($(shipelements[i]).value=='') {
		if(shipelements[i]=='shipstate') { $('cshipcounty').className='redalert'; }
		else if (shipelements[i]=='shipprovince') { $('cshipcounty').className='redalert'; }
		else { $('c'+shipelements[i]).className='redalert'; }
		error=error+1;
	} else {
		if(shipelements[i]=='shipstate') { $('cshipcounty').className=''; }
		else if (shipelements[i]=='shipprovince') { $('cshipcounty').className=''; }
		else { $('c'+shipelements[i]).className=''; }
		}
	}
}
if ($('exp_month').value<=month&&$('exp_year').value<=year) {
	$('exp').className='redalert';
	error=error+1;
	} else {
	$('exp').className='';
	}
var emailerror=0;
var at="@"
var dot="."
var str = $('email').value;
var lat=str.indexOf(at)
var lstr=str.length
var ldot=str.indexOf(dot)
if (str.indexOf(at)==-1){
	emailerror=emailerror+1;
}
if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
   emailerror=emailerror+1;
}
if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	emailerror=emailerror+1;
}
if (str.indexOf(at,(lat+1))!=-1){
	emailerror=emailerror+1;
}
if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
   emailerror=emailerror+1;
}
if (str.indexOf(dot,(lat+2))==-1){
   emailerror=emailerror+1;
}	
if (str.indexOf(" ")!=-1){
   emailerror=emailerror+1;
}
if (emailerror>0) {
	$('cemail').className='redalert';
	error=error+1;
	} else {
	$('cemail').className='';
	}
if (($('start_month').value=='00'||$('start_year').value=='00')&&$('issue').value==''&&$('card_type').value=='Maestro') {
	$('start').className='redalert';
	$('cissue').className='redalert';
	error=error+1;
	} else {
	$('start').className='';
	$('cissue').className='';
	}	
if (error==0) {	
		if($('country').value=='United States') { $('inputProvince').remove(); $('inputCounty').remove(); }
		else if($('country').value=='Canada') { $('inputState').remove(); $('inputCounty').remove(); }
		else { $('inputState').remove(); $('inputProvince').remove(); }
		if ($('shippingTrue').checked==true) {
			if($('shipcountry').value=='United States') { $('inputShipProvince').remove(); $('inputShipCounty').remove(); }
			else if($('shipcountry').value=='Canada') { $('inputShipState').remove(); $('inputShipCounty').remove(); }
			else { $('inputShipState').remove(); $('inputShipProvince').remove(); }
		}
		return true;
	} else { 
		return false; 
	}
}

function selectPayment(type) {
	$('pp').hide();
	$('cc').hide();
	$('ccship').hide();
	Effect.SlideDown($(type));
}

function selectShipping(type) {
	if (type=='') {
		$('ccship').hide();
		}
	else {
		Effect.Appear($(type));
		}
}
function updateShipping(newCountry) {
	new Ajax.Updater('checkoutBasket', '/class/plugins/checkoutBasket.php?etype=shipping&country='+newCountry, {asynchronous:true, evalScripts:true});
}
function updateCounty() {
	if($('country').value=='United States') { $('state').show(); $('county').hide(); $('province').hide(); $('countyLabel').innerHTML='State'; }
	else if($('country').value=='Canada') { $('state').hide(); $('county').hide(); $('province').show(); $('countyLabel').innerHTML='Province'; } 
	else { $('county').show(); $('state').hide(); $('province').hide(); $('countyLabel').innerHTML='County'; }
	if($('shipcountry').value=='United States') { $('shipstate').show(); $('shipcounty').hide(); $('shipprovince').hide(); $('shipcountyLabel').innerHTML='State'; } 
	else if($('shipcountry').value=='Canada') { $('shipstate').hide(); $('shipcounty').hide(); $('shipprovince').show(); $('shipcountyLabel').innerHTML='Province';}
	else { $('shipcounty').show(); $('shipstate').hide(); $('shipprovince').hide(); $('shipcountyLabel').innerHTML='County'; }
}

function getBasket() {
	new Ajax.Updater('checkoutBasket', '/class/plugins/checkoutBasket.php', {asynchronous:true, evalScripts:true});
}

function plusBasketCheckout(productID, optionID) {
	$('qty_'+productID+'_'+optionID).innerHTML='<img src="/oncms/master_assets/images/icons/ajax-loader.gif" border="0" />';
	new Ajax.Updater('checkoutBasket', '/class/plugins/checkoutBasket.php?etype=minus&productID='+productID+'&optionID='+optionID+'&shopURL='+shopURL, {asynchronous:true, evalScripts:true});
}
function minusBasketCheckout(productID, optionID) {
	$('qty_'+productID+'_'+optionID).innerHTML='<img src="/oncms/master_assets/images/icons/ajax-loader.gif" border="0" />';
	new Ajax.Updater('checkoutBasket', '/class/plugins/checkoutBasket.php?etype=plus&productID='+productID+'&optionID='+optionID+'&shopURL='+shopURL, {asynchronous:true, evalScripts:true});
}
function populateForm(queryString, formName) {
    var params = queryString.toQueryParams();
    Object.keys(params).each(function(key) {
        Form.Element.setValue($(formName)[key], params[key]);
    });
}
function noStock(productID, optionID) {
	alert('current pid:'+cproductID+' current oid:'+coptionID+'  ||   productID: '+productID+' optionID: '+optionID);
	if((optionID>0)&&(productID==cproductID)&&(optionID==coptionID)) {
		$('basketAdd').disabled="disabled";	
	} else if((optionID<1)&&(productID==cproductID)) {
		$('basketAdd').disabled="disabled";	
	} else {
		$('basketAdd').disabled=false;	
	}
}
function showImage(URL) {
	$('productImage').src=URL;
}