vsval1="";
thispage="shoppingbasket.htm";
nextpage="shoppingbasket.htm";
vscartresponse="has been added to your cart.";

function priceFormat(value) 
{
	return value.toFixed(2);
}


function displayCart() {
	afval=0;
	orderref='';
	alldata = '';

 	if (GetCookie('Cart')){
 		alldata = GetCookie('Cart');
 		//alert(alldata);
 		//SetCookie('Cart','');
 		
    }
 	else{
 		alldata = '';
 	}
	alltotal = 0;
	listcount = 0;
	vscount = 0;
	
    document.writeln('<TABLE width=100% border=0 cellspacing=1 cellpadding=2 bgColor=#80a080 cols=5>');
 	for (var i = 0; i <= alldata.length; i++) {
        if (alldata.substring(i,i+1) == ']') {
  			listcount=listcount+1;
  	    }
 	}
 	if (listcount > 0){
	   document.writeln('<TR><td nowrap width=70%><font face="Arial" size=2 color="FFFFFF"><b>Items </TD><TD valign="bottom" width=1 colspan=1><font face="Arial" size=2 color="FFFFFF"><b>Qty</TD><TD valign="bottom" width=1 colspan=1><font face="Arial" size=2 color="FFFFFF"><b>Each</TD><td valign="bottom" width=1 colspan=1 align=left><font face="Arial" size=2 color="FFFFFF"><b>Total</TD><TD valign="bottom" width=1 colspan=1 align=center><font face="Arial" size=2 color="FFFFFF"><b>Remove</TD></TR>');
    }else {
        document.writeln('<TR><TD colspan=5><font face="Arial" size=2 color="FFFFFF"><b>Empty</TD></TR>');
 	}
	listcount = 0;
 	for (var i = 0; i <= alldata.length; i++) {
  		if (alldata.substring(i,i+1) == '[') {
  			listbegin = i+1;
  		} else if (alldata.substring(i,i+1) == ']') {
  			listfinish = i;
  			listtotal = 0;
  			vscount = 0;
  			listtotal = (eval(vsprice*vsamount));
  			liststore = listtotal * 100;
  			alltotal = alltotal + listtotal;
  			listcount=listcount+1;

            listtotal=priceFormat(listtotal);
   			if (!vsprice){
                vsprice=0;listtotal=0;
            }

			vsshowdel='<a href="javascript:clearSlot('+listcount+')"><image src=images/X.gif border=0></a>';
   
  			remcolumn='<td align=center colspan=1><font face="Arial" size=2 color="000000">'+vsshowdel+'</td>'

            qtyinput='<INPUT size=3 value='+vsamount+' onblur="AmendCart()" name="qty'+listcount+'">'
            qtycolumn='<td colspan=1><font face="Arial" size=2 color="000000">'+qtyinput+'</td>'

   			document.writeln('<tr bgColor="FFFFFF"><td><font face="Arial" size=2 color="000000">'+vsobject+'</td>'+qtycolumn+'<td colspan=1 nowrap><font face="Arial" size=2 color="000000">'+vsprice+'</td><td colspan=1 align=right nowrap><font face="Arial" size=2 color="000000">'+listtotal+'</td>'+remcolumn+'</tr>');
        }
        else if (alldata.substring(i,i+1) == '#') {
  			vscount = vscount + 1;
            if (vscount == 2) {vsobject = alldata.substring(listbegin, i);}
            if (vscount == 3) {vsamount = alldata.substring(listbegin, i);}   
            if (vscount == 4) {vsprice = alldata.substring(listbegin, i);}
            listbegin = i+1;
        }
 	}
	
	if (listcount > 0) {
 		document.writeln('<tr><td colspan=3 align=right><font face="Arial" size=2 color="FFFFFF"><b>Order Total: £</b></td><td align=right><font face="Arial" size=2 color="FFFFFF"><b>'+priceFormat(alltotal)+'</b></td></tr></table>');
    } else {
		document.writeln('<tr><td colspan=5>&nbsp;</td></tr></TABLE>');
 	}
}

function AmendCart()
{
    wantItList = null;
	listcount = 0;
 	itemelement = 0;
 	itembegin = 0;
 	itemstring = null;
 	code = null;
 	qty = null;
 	price = null;
 	refresh = 0;
     
     for (var i = 0; i <= alldata.length; i++) {
  		//Start of item
        if (alldata.substring(i,i+1) == '[') 
        {
  			itembegin = i+1;
  			itemstring = null;
  			itemnumber = null;
  			numberstring = null;
  			pricestring = null;
  		} 
  		//End of item
        else if (alldata.substring(i,i+1) == ']') {
  			listfinish = i;
  			listcount=listcount+1;
   			itemelement = 0;
   			
   			var tempqty = eval('document.BuyForm.qty'+(listcount));
   			if (tempqty.value != 0)
   			{
                if(wantItList == null)
                {
                    wantItList = '['+itemstring+']';
                }
                else
                {
                    wantItList = wantItList+'['+itemstring+']';
                }
            }
  		}
  		
        else if (alldata.substring(i,i+1) == '#') {
  			itemelement = itemelement + 1;
            if (itemelement == 1) {
                code = alldata.substring(itembegin, i);
                itemstring = alldata.substring(itembegin,i+1);
            }
            if (itemelement == 2) {
                itemstring = itemstring + alldata.substring(itembegin,i+1);
            }
            if (itemelement == 3) {
                var tempqty = eval('document.BuyForm.qty'+(listcount+1));
                qty = alldata.substring(itembegin,i);
				if(isNaN(tempqty.value) == true){
					alert(tempqty.value + " is not a valid quantity");
					eval('document.BuyForm.qty'+(listcount+1)+'.value=numberstring');
					itemstring = itemstring + alldata.substring(itembegin,i+1);
				}
				else{
					if(qty != tempqty.value){
						refresh = 1;
					}					
					itemstring = itemstring + tempqty.value + '#';
					qty = tempqty.value;
				}
            }   
            if (itemelement == 4) {
                price = GetQuantityPrice(code, qty);
                itemstring = itemstring + price + '#';
            }
            itembegin= i+1;
        }      
 	}
	SetCookie('Cart',wantItList);
    if (refresh){
	    location = nextpage;
	}
}

function clearSlot(datnumber) {
	wantItList = null;
	listcount = 0;
 	for (var i = 0; i <= alldata.length; i++) {
  		if (alldata.substring(i,i+1) == '[') {
  			listbegin = i+1;
  		} else if (alldata.substring(i,i+1) == ']') {
  			listfinish = i;
  			listcount=listcount+1;
   			if (listcount != datnumber) {
   				wantItList = wantItList+'['+alldata.substring(listbegin, listfinish)+']';
   			}
  		}
 	}
	SetCookie('Cart',wantItList);
	if (thispage){location = thispage;}else{window.location.reload();}
}

function ValidateString(mystring)
{
    var newstring = '';
    for(var i=0; i<mystring.length; i++){
        if(mystring.substring(i,i+1) == '#'){
            newstring = newstring + '%23';
        }
        else{
            newstring = newstring + mystring.substring(i,i+1);
        }
    }
    return(newstring);
}

function AmendCustomer() {
	CustomerDetails = null;
	CustomerDetails = ValidateString(document.BuyForm.name.value) + "#" 
	CustomerDetails	= CustomerDetails + ValidateString(document.BuyForm.companyname.value) + "#"
	CustomerDetails	= CustomerDetails + ValidateString(document.BuyForm.address.value) + "#"
	CustomerDetails	= CustomerDetails + ValidateString(document.BuyForm.postcode.value) + "#"
	CustomerDetails	= CustomerDetails + document.BuyForm.country.value + "#" 
	CustomerDetails	= CustomerDetails + ValidateString(document.BuyForm.ecvat.value) + "#" 
	CustomerDetails	= CustomerDetails + ValidateString(document.BuyForm.telephone.value) + "#" 
	CustomerDetails	= CustomerDetails + ValidateString(document.BuyForm.email.value) + "#"
	CustomerDetails	= CustomerDetails + document.BuyForm.M_advert.value + "#"

	SetCookie('Customer',CustomerDetails);
	SetCookie('Country',document.BuyForm.country[document.BuyForm.country.selectedIndex].text);

	CustomerDetails = null;
	CustomerDetails	= ValidateString(document.BuyForm.M_shipping_name.value) + "#"
	CustomerDetails	= CustomerDetails + ValidateString(document.BuyForm.M_shipping_companyname.value) + "#"
	CustomerDetails	= CustomerDetails + ValidateString(document.BuyForm.M_shipping_address.value) + "#"
	CustomerDetails	= CustomerDetails + ValidateString(document.BuyForm.M_shipping_postcode.value) + "#"
	CustomerDetails	= CustomerDetails + ValidateString(document.BuyForm.M_shipping_telephone.value) + "#"
	CustomerDetails	= CustomerDetails + ValidateString(document.BuyForm.M_shipping_country.value) + "#"
				
	SetCookie('Delivery',CustomerDetails);
	if(document.BuyForm.M_shipping_country.selectedIndex != 0)
		SetCookie('DCountry',document.BuyForm.country[document.BuyForm.M_shipping_country.selectedIndex].text);
	else
		SetCookie('DCountry',document.BuyForm.country[document.BuyForm.country.selectedIndex].text);
}

function Checkout() {
	var errorstring = '';
	var errornum = 0;

	if(document.BuyForm.name.value == ''){
		errorstring = errorstring + "Name";
		errornum=errornum+1;
	}
	if(document.BuyForm.address.value == ''){
		if (errornum != 0){
			errorstring = errorstring +", ";
		}
		errornum=errornum+1;
		errorstring = errorstring + "Address";
	}
	if(document.BuyForm.postcode.value == ''){
		if (errornum != 0){
			errorstring = errorstring +", ";
		}
		errornum=errornum+1;
		errorstring = errorstring + "Postcode";
	}
	if(document.BuyForm.country.value == ''){
		if (errornum != 0){
			errorstring = errorstring +", ";
		}
		errornum=errornum+1;
		errorstring = errorstring + "Country";
	}
	if(document.BuyForm.email.value == ''){
		if (errornum != 0){
			errorstring = errorstring +", ";
		}
		errornum=errornum+1;
		errorstring = errorstring + "Email";
	}

	if(alltotal == 0){
        alert("There is nothing in your shopping basket !");
    }
    else if(errornum == 1){
		alert(errorstring + " field required");	
	}
	else if(errornum > 1){
		alert(errorstring + " fields required");	
	}
    else if((document.BuyForm.M_shipping_address.value !='') && (document.BuyForm.M_shipping_postcode.value =='')){
        alert("Please supply a postcode for the delivery address.")
    }
    else if((document.BuyForm.M_shipping_address.value !='') && (document.BuyForm.M_shipping_country.value =='')){
        alert("Please supply a country for the delivery address.")
    }
    else if(ECVatCheck() == true)
    {
	    //alert("AVIT Research Ltd is closed between 9th and 14th August 2010. Orders placed during this period will be processed on Monday 16th August. Sorry for any inconvenience this may cause.");
		location = "checkout.htm";		
	}
			
}

function ECVatCheck()
{
    var ECVat = document.BuyForm.ecvat.value;
    var Country = document.BuyForm.country.value;
    if(ECVat == '')
        return true;
    ECVat = ECVat.replace(Country,'');
    if((Country == 'AT') && ((ECVat.charAt(0).toUpperCase() != 'U') || (ECVat.length != 9)))
    {
        alert("Invalid VAT number\nexpected format:\nU23456789");
        return false;
    }
    if((Country == 'BE') && (ECVat.replace(' ', '').replace(' ', '').length != 9))
    {
        alert("Invalid VAT number\nexpected format:\n123 456 789");
        return false;
    }
    if((Country == 'CY') && (ECVat.length !=9))
    {
        alert("Invalid VAT number\nexpected format:\n12345678X");
        return false;
    }
    if((Country == 'CZ') && ((ECVat.length <8) || (ECVat.length > 13)))
    {
        alert("Invalid VAT number\nexpected format:\n12345678 or\n123456789 or\n1234567890");
        return false;
    }
    if((Country == 'DK') && (ECVat.replace(' ', '').replace(' ', '').replace(' ', '').length != 8))
    {
        alert("Invalid VAT number\nexpected format:\n12 34 56 78");
        return false;
    }
    if((Country == 'EE') && (ECVat.length != 9))
    {
        alert("Invalid VAT number\nexpected format:\n123456789");
        return false;
    }
    if((Country == 'FI') && (ECVat.length != 8))
    {
        alert("Invalid VAT number\nexpected format:\n12345678");
        return false;
    }
    if((Country == 'FR') && (ECVat.replace(' ', '').length != 11))
    {
        alert("Invalid VAT number\nexpected format:\n12 345678901 or\nX2 345678901 or\n1X 345678901 or\nXX 345678901");
        return false;
    }
    if((Country == 'DE') && (ECVat.length != 9))
    {
        alert("Invalid VAT number\nexpected format:\n123456789");
        return false;
    }
    if((Country == 'GR') && ((ECVat.replace('EL', '').length != 8) && (ECVat.replace('EL', '').length != 9)))
    {
        alert("Invalid VAT number\nexpected format:\n012345678");
        return false;
    }
    if((Country == 'HU') && (ECVat.length != 8))
    {
        alert("Invalid VAT number\nexpected format:\n12345678");
        return false;
    }
    if((Country == 'IE') && (ECVat.length != 8))
    {
        alert("Invalid VAT number\nexpected format:\n1234567X or\n1X34567X");
        return false;
    }
    if((Country == 'IT') && (ECVat.length != 11))
    {
        alert("Invalid VAT number\nexpected format:\n12345678901");
        return false;
    }
    if((Country == 'LV') && (ECVat.length != 11))
    {
        alert("Invalid VAT number\nexpected format:\n12345678901");
        return false;
    }
    if((Country == 'LT') && ((ECVat.length != 9) && (ECVat.length != 11))) 
    {
        alert("Invalid VAT number\nexpected format:\n123456789 or\n123456789012");
        return false;
    }
    if((Country == 'LU') && (ECVat.length != 8))
    {
        alert("Invalid VAT number\nexpected format:\n12345678");
        return false;
    }
    if((Country == 'MT') && (ECVat.length != 8))
    {
        alert("Invalid VAT number\nexpected format:\n12345678");
        return false;
    }
    if((Country == 'NL') && (ECVat.length != 12))
    {
        alert("Invalid VAT number\nexpected format:\n123456789B12");
        return false;
    }
    if((Country == 'PL') && (ECVat.length != 10))
    {
        alert("Invalid VAT number\nexpected format:\n1234567890");
        return false;
    }
    if((Country == 'PT') && (ECVat.length != 9))
    {
        alert("Invalid VAT number\nexpected format:\n123456789");
        return false;
    }
    if((Country == 'SI') && (ECVat.length != 8))
    {
        alert("Invalid VAT number\nexpected format:\n12345678");    
        return false;
    }
    if((Country == 'SK') && ((ECVat.length != 9) && (ECVat.length != 10)))
    {
        alert("Invalid VAT number\nexpected format:\n123456789 or\n1234567890");
        return false;
    }
    if((Country == 'ES') && (ECVat.length != 9))
    {
        alert("Invalid VAT number\nexpected format:\nX23456789 or\n12345678X or\nX2345678X");
        return false;
    }
    if((Country == 'SE') && (ECVat.length != 12))
    {
        alert("Invalid VAT number\nexpected format:\n123456789001");
        return false;
    }
    return true;
}


function displayCustomer()
{
	listbegin = 0;
	listcount = 0;
	customerdetails = new String(GetCookie('Customer'));
		
 	for (var i = 0; i <= customerdetails.length; i++) {	  	
		if (customerdetails.substring(i,i+1) == '#') {
  			listcount = listcount + 1;
  			if(listcount==1){document.BuyForm.name.value = unescape(customerdetails.substring(listbegin, i));}
  			if(listcount==2){document.BuyForm.companyname.value = unescape(customerdetails.substring(listbegin, i));}
  			if(listcount==3){document.BuyForm.address.value = unescape(customerdetails.substring(listbegin, i));}		
  			if(listcount==4){document.BuyForm.postcode.value = unescape(customerdetails.substring(listbegin, i));}
 			if(listcount==5){document.BuyForm.country.value = customerdetails.substring(listbegin, i);}
 			if(listcount==6){document.BuyForm.ecvat.value = unescape(customerdetails.substring(listbegin, i));}
 			if(listcount==7){document.BuyForm.telephone.value = unescape(customerdetails.substring(listbegin, i));}
			if(listcount==8){document.BuyForm.email.value = unescape(customerdetails.substring(listbegin, i));}
			if(listcount==9){document.BuyForm.M_advert.value = customerdetails.substring(listbegin, i);}
  			listbegin = i+1;
  		}
 	}

	listbegin = 0;
	listcount = 0;
	customerdetails = new String(GetCookie('Delivery'));
	
 	for (var i = 0; i <= customerdetails.length; i++) {	  	
		if (customerdetails.substring(i,i+1) == '#') {
  			listcount = listcount + 1;
  			if(listcount==1){document.BuyForm.M_shipping_name.value = unescape(customerdetails.substring(listbegin, i));}
  			if(listcount==2){document.BuyForm.M_shipping_companyname.value = unescape(customerdetails.substring(listbegin, i));}
  			if(listcount==3){document.BuyForm.M_shipping_address.value = unescape(customerdetails.substring(listbegin, i));}			
  			if(listcount==4){document.BuyForm.M_shipping_postcode.value = unescape(customerdetails.substring(listbegin, i));}
 			if(listcount==5){document.BuyForm.M_shipping_telephone.value = unescape(customerdetails.substring(listbegin, i));}
 			if(listcount==6){document.BuyForm.M_shipping_country.value = customerdetails.substring(listbegin, i);}
  			listbegin = i+1;
  		}
 	}
 	if (GetCookie('Refresh') == '1'){
        DeleteCookie('Refresh');
        window.location.reload();
    }
 	
}

function ContinueShopping()
{
	location ="index.htm";
}
