<!--hide this script from non-javascript-enabled browsers

var CHAR_CURRENCY  = [ '$', '&euro;', '&pound;' ];
/* var LOCAL_TAX = [ 0,17.5, 17.5]; */
var LOCAL_TAX = [ 0,15.0, 15.0];

var USA=0;	 
var EU=1;
var UK=2;
var WORLD=3;

var cost=0;
var total=0;
var tax=0;
var validVat=0; 

var CD = "0010"

 
function CheckNumber( ob )
{
	var pattern = /^[0-9]*$/;
	if(pattern.test(ob.value)==false)
	{
		ob.value = 0;
	}
	UpdateForm();
}
	

function getCursorPos(obj )
{
/*
   if (typeof obj.selectionStart != "undefined")
        return obj.selectionStart;
    
    if (document.selection != "undefined")
    {
	    var range = document.selection.createRange().duplicate(); 
	    var s = 0; 

	    while ( range.moveStart("character", -1) != 0 )
		    s++;
	    return s;
	}
*/
} 

function ValidateUsername( obj )
{
    //getCursorPos( obj )
	
	var str = obj.value;
	var ok = " .@-_QWERTYUIOPASDFGHJKLZXCVBNM()[]1234567890qwertyuiopasdfghjklzxcvbnm";
	var strn = "";
	var l = str.length;

	for(i=0; i < l ;i++)
	{
		if (ok.indexOf(str.charAt(i))<0)
		{
			if (i+1 != l)
				strn += " ";
		} 
		else
			strn += str.charAt(i);
	}
	if (strn!=str)
		obj.value = strn;
}

function CheckUsername( )
{
    var nm = document.buy.username
	ValidateUsername(nm);
	if (nm.value.length > 4)
		return true;

	alert("Please enter a valid username");
	return false;
}



function RoundMoney(valIn)
{
	var val = Math.round(valIn * 1000);
	if ((val %10) >= 5)
	{
		val /= 10; 
		val ++;
	}
	else
		val /= 10;

	return 	Math.floor(val/100) + '.' + 
			Math.floor((val/10) % 10) + 
			Math.floor(val % 10);
}

function GetCurrency(country)
{
	if (document.buy.currency.value == "Not Set")
	{
		var c = (!country || country >2) ? 0 : country;
		return c;
	}
	return document.buy.currency.value;
}

function GetCountry()
{
	var buy = document.buy;
	if (buy.country.length != undefined)
	{
		for(var i = 0; i < 4; i++) 
			if(buy.country[i].checked) 
				return buy.country[i].value;
	}
	else
		return buy.country.value;
	
	return 0;
}

function ChangeState()
{
	var buy = document.buy;
	var s=buy.state.selectedIndex;
	if(s==0)
		buy.VAT.value = "";
	else 
		buy.VAT.value=buy.state[s].value;
}

    
function UpdateForm()
{
	var buy = document.buy;
	var country = GetCountry();
	var currency = GetCurrency( country );
	
	//alert( "country " + country + " currency " + currency )
	var c = CHAR_CURRENCY[currency];
	var discount = 0;
	var backup =0;
	var products = data.length;
	cost = 0;
	
	for (var i in data)
	{
		var price = data[i][2][currency] / 100;
		var priceD = data[i][1][currency] / 100;
		var str = ""
		if (price != priceD) // have we got a discount ?
			str = "<s>" + c + RoundMoney( priceD ) + "</s> &nbsp; "						 
			
		document.getElementById("P_" + i).innerHTML = str + c + RoundMoney(price);
	
		var q = document.getElementById("Q_" + i).value;		
		if (q > 0)		
		{
			if (i != CD)
			{
				var ct = price * q
				cost += ct;
				discount += (priceD * q) - ct;
			}
			else 
				backup = price * q;
		}
	}

	// enable or disable the backup cd
	document.getElementById("Q_" + CD).disabled=cost ? false : true;		
	if (!cost)	
		document.getElementById("Q_" + CD).value = 0; 
	else
		cost += backup;

	document.getElementById("vatsection").style.display = (country==EU) ? "block" : "none";
		
	validVat = (country==EU) && checkVATNumber( buy.VAT.value, 0 ); 
    
	tax = ((country==USA) || (country==WORLD) || validVat) ? 0 : (LOCAL_TAX[currency] * cost / 100);
	
    cost = RoundMoney(cost)
	tax  = RoundMoney(tax);
	total = RoundMoney(parseFloat(cost) + parseFloat(tax));
    
    document.buy.taxCost.value = tax * 100;
	document.buy.totalCost.value = total * 100;

	document.getElementById("costTotal").innerHTML = c + cost;
	document.getElementById("discountTotal").innerHTML = c + RoundMoney(discount);
	document.getElementById("taxTotal").innerHTML = c + tax;
	document.getElementById("total").innerHTML = c + total;
		
}


function Buy()
{
	var country = GetCountry();
	
	if (!cost)
		alert("Please select something to purchase");
	else  if (CheckUsername())
	{
        document.buy.submit()	
	}
}

function ConfirmBuy()
{
    if (document.buyWP)
       document.buyWP.submit()
}

// stop hiding -->

