// JavaScript Document
/********************************************************
**	shortcut getElementById
*********************************************************/

$(function(){
	// Opens sign in area
	$('#activatesignin').click(function() {
		$('#signin').fadeIn('slow');
		$('#signinusername').focus();
	});
	
	// Closes sign in area
	$('#closesignin').click(function() {
		$('#signin').fadeOut('slow');
	});
	
	$('.datepicker').datePicker({clickInput:true, startDate:'01/01/1996'});
});

function init() {
	if(document.getElementById('cardno')) {
		document.getElementById('cardno').onkeyup = function () {
			if(document.getElementById('cardno').value!="") {
				validateCC(document.getElementById('cardno').value);
			}
		}
	}
	if(document.getElementById('parentproduct')) {
		if(document.getElementById('parentproduct').value!="" && document.getElementById('childlicense').value==""){
			getLicense(document.getElementById('parentproduct').value);		
		}
	}
	if(document.getElementById('savingstotal')){
		updatePrice(document.getElementById('zone').value);
	}

	if(document.getElementById('find_us')) {
		document.getElementById('find_us').onchange = function () {
			if(document.getElementById('find_us').value=="other") {
					document.getElementById('find_us_detail').style.display='block';
			}
			else
			{
					document.getElementById('find_us_detail').style.display='none';
			}
		}
	}
	
	if(document.getElementById('changecustomertype')) {
		document.getElementById('changecustomertype').onclick = function () {
			if(document.getElementById('typeeducational')){
				document.getElementById('typeeducational').style.display="block";
			}
			if(document.getElementById('typebusiness')){
				document.getElementById('typebusiness').style.display="block";
			}
			if(document.getElementById('typepersonal')){
				document.getElementById('typepersonal').style.display="block";
			}
			if(document.getElementById('educationallogged')){
				document.getElementById('educationallogged').style.display="block";
			}
			if(document.getElementById('businesslogged')){
				document.getElementById('businesslogged').style.display="block";
			}
			if(document.getElementById('personallogged')){
				document.getElementById('personallogged').style.display="block";
			}
		}
	}
	
	if(document.getElementById('customertypeclose')){
		document.getElementById('customertypeclose').onclick = function () {
			if(document.getElementById('typeeducational')){
				document.getElementById('typeeducational').style.display="none";
			}
			if(document.getElementById('typebusiness')){
				document.getElementById('typebusiness').style.display="none";
			}
			if(document.getElementById('typepersonal')){
				document.getElementById('typepersonal').style.display="none";
			}
			if(document.getElementById('educationallogged')){
				document.getElementById('educationallogged').style.display="none";
			}
			if(document.getElementById('businesslogged')){
				document.getElementById('businesslogged').style.display="none";
			}
			if(document.getElementById('personallogged')){
				document.getElementById('personallogged').style.display="none";
			}
		}
	}
	
	if(document.getElementById('currencyclose')){
		document.getElementById('currencyclose').onclick = function () {
			if(document.getElementById('currency')){
				document.getElementById('currency').style.display="none";
			}
			if(document.getElementById('currencyusd')){
				document.getElementById('currencyusd').style.display="none";
			}
			if(document.getElementById('currencyeur')){
				document.getElementById('currencyeur').style.display="none";
			}
			if(document.getElementById('currencylogged')){
				document.getElementById('currencylogged').style.display="none";
			}
			if(document.getElementById('currencyusdlogged')){
				document.getElementById('currencyusdlogged').style.display="none";
			}
			if(document.getElementById('currencyeurlogged')){
				document.getElementById('currencyeurlogged').style.display="none";
			}
		}
	}
	
	if(document.getElementById('accountRePassword')){
			document.getElementById('accountPassword').onkeyup = function () {
				document.getElementById('validateAccountPassword').style.display = 'block';
				if((document.getElementById('accountPassword').value).length<=5){
						document.getElementById('validateAccountPassword').style.background = 'url(/images/cross.gif)';
				}
				else {
					document.getElementById('validateAccountPassword').style.background = 'url(/images/tick.gif)';
				}
			}
			document.getElementById('accountRePassword').onkeyup = function () {
				document.getElementById('validateAccountRePassword').style.display = 'block';
				if(document.getElementById('accountRePassword').value!=document.getElementById('accountPassword').value){
						document.getElementById('validateAccountRePassword').style.background = 'url(/images/cross.gif)';
				}
				else {
					document.getElementById('validateAccountRePassword').style.background = 'url(/images/tick.gif)';
				}
			}
	}
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }


function getPrice(qty, parentid, childid, type, customertype){
	var xmlHttp;
	var childid = childid.substr(4);
	try{  
	// Firefox, Opera 8.0+, Safari  
		xmlHttp=new XMLHttpRequest();  
	}	catch (e){  
	// Internet Explorer  
	try
    {    
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    
		}
  catch (e)
    {    
			try
     	{      
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      
			}
    	catch (e)
      {      
				alert("Your browser does not support AJAX!");      
				return false;      
			}    
		}  
	}
  xmlHttp.onreadystatechange=function(){
    if(xmlHttp.readyState==4){
			var brokenstring=xmlHttp.responseText.split(" ");
			document.getElementById(customertype+'_price_'+childid).innerHTML=brokenstring[0]+' '+brokenstring[1];
			if(brokenstring[2]>=1){
				document.getElementById(customertype+'_saving_'+childid).innerHTML="Saving of "+brokenstring[0]+brokenstring[2];
			}
			else
			{
				document.getElementById(customertype+'_saving_'+childid).innerHTML=''
			}
		}
  }
  xmlHttp.open("GET",'/action/getprice.php?parentid='+parentid+'&childid='+childid+'&qty='+qty,true);
  xmlHttp.send(null);
}

function getLicense(id){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	var ids = id.split("___");
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		i=0;
		if(ajaxRequest.readyState == 4){
			document.getElementById('childlicense').options.length = 0;
			var longstring = ajaxRequest.responseText;
			var brokenstring = longstring.split("zzz");
			bsl = brokenstring.length-2;
			while(bsl>=i){
				var license = brokenstring[i].split("zz");
				document.getElementById('childlicense').options[i] = new Option(license[1]);
				document.getElementById('childlicense').options[i].className="option";
				document.getElementById('childlicense').options[i].value=license[0];
				i++;
				
			}
			document.getElementById('productbox').src="../productimages/"+ids[2]+"_box_large.jpg";
			document.getElementById('productname').innerHTML=ids[0];
			if(!id){
				document.getElementById('productbox').src="../images/text_please_select_a_product.gif";
				document.getElementById('productname').innerHTML='Please select a product';
			}
		}
		ajaxComplete();
	}
	ajaxRequest.open("GET", "/purchase/action/getlicense.php?parentid="+ids[1], true);
	ajaxRequest.send(null);
}

function ajaxComplete(){
	var childlicense = document.getElementById('childlicense');
	for(var i = 0; i<childlicense.length; i++) {
		if(childlicense.options[i].value==chosen) {
			childlicense.options[i].selected=true;
			chosen = '';
		}
	}
}

function show(id, src) {
		document.getElementById('singlearea').style.display='none';
		document.getElementById('multiarea').style.display='none';
		document.getElementById(id+'area').style.display='block';
		document.getElementById('tabsingle').src='images/tab_single_off.gif';
		document.getElementById('tabmulti').src='images/tab_multi_off.gif';
		document.getElementById('tab'+id).src=src;
}

function testVATNumber (vatno, zone) {
	if (checkVATNumber(vatno)) {
    vatno = checkVATNumber(vatno)
   	document.getElementById('vatvalid').style.display="inline"
		document.getElementById('vatinvalid').style.display="none"
		if(zone==2) {
			document.getElementById('vat_exempt').value='true';
		}
		else
		{
			document.getElementById('vat_exempt').value='false';
		}
  }  
  else 
	{
		document.getElementById('vatinvalid').style.display="block"	
		document.getElementById('vatvalid').style.display="none"
		document.getElementById('vat_exempt').value='false';
	}
	updatePrice(zone);
}

function applyDiscount(code, parentid, childid, zone, eduDiscount){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var result = ajaxRequest.responseText;
			var first = result.substr(0,1);
			var percentage = result.substr(1);	
			if(result!="fail") {
				if(first=="p"){
					document.getElementById('discount_per').value = percentage*1;
				}
				else
				{
					document.getElementById('discount_amt').value = result*1;
				}
				document.getElementById('discountvalid').style.display="block"
				document.getElementById('discountinvalid').style.display="none"
			}
			else
			{
				document.getElementById('discount_amt').value = '';
				document.getElementById('discount_per').value = '';
				document.getElementById('discountvalid').style.display="none"
				document.getElementById('discountinvalid').style.display="block"
			}
			updatePrice(zone);
		}
	}
	ajaxRequest.open("GET", "/purchase/action/applyDiscounts.php?code="+code+"&parentid="+parentid+"&childid="+childid+"&eduDiscount="+eduDiscount, true);
	ajaxRequest.send(null);
}
function updatePostage(amount, zone){
		document.getElementById('postageprice').value=amount;
		updatePrice(zone);
}
function updatePrice(zone){
		var discount=0;
		//if(document.getElementById('premium_support')){ 
		//	if(document.getElementById('premium_support').checked){
		//		var support = document.getElementById('premium_support').value*1;
		//	}
		//}
		var post = document.getElementById('postageprice').value*1; 				// Postage Price
		var unitSub = document.getElementById('unit_sub_total').value*1;	// Pre Sub Total
		var save = document.getElementById('savingstotal').innerHTML*1;			// Savings Total 
		var dAmt = document.getElementById('discount_amt').value*1;					// Discount Amount
		var dPer = (document.getElementById('discount_per').value*1)/100;					// Discount Percentage
		//if(document.getElementById('premium_support')){ 
		//	if(document.getElementById('premium_support').checked){
		//		var support = document.getElementById('premium_support').value*1;
		//		var subTotal = post+unitSub+support;
		//	} else {
				var subTotal = post+unitSub;	
		//	}
		//}
		if (dPer!="") {
		 	discount = subTotal*dPer;
		}
		if(dAmt!="") {
			discount = dAmt;
		}
		subTotal = subTotal - discount;
		
		if(zone<=2) {
			var vat = (subTotal * 1.175)-subTotal;
		}
		
		if(zone==2 && document.getElementById('vat_exempt').value=='true') {
			vat = 0;
		}
		if(zone==2 && document.getElementById('vat_exempt').value=='false') {
			vat = (subTotal * 1.175)-subTotal;
		}
		if(!vat){
			var vat=0.00;
		}
		if(($('#special_vat_rate').val()*1) > 0){
			var vat = (subTotal * ($('#special_vat_rate').val()*1))-subTotal;
		}
		var total = subTotal+vat;
		
		document.getElementById('subtotal').innerHTML = (unitSub).toFixed(2);
		document.getElementById('final_subtotal').value = document.getElementById('subtotal').innerHTML;
		document.getElementById('savingstotal').innerHTML = discount.toFixed(2);
		document.getElementById('final_discount').value = document.getElementById('savingstotal').innerHTML;
		document.getElementById('vattotal').innerHTML = vat.toFixed(2);
		document.getElementById('final_vat').value = document.getElementById('vattotal').innerHTML;
		document.getElementById('paymenttotal').innerHTML = total.toFixed(2);
		document.getElementById('final_total').value = document.getElementById('paymenttotal').innerHTML;
}

	
function validateStudent(college, course, number){
	document.getElementById('validateCollege').style.display = 'block';
	document.getElementById('validateCourse').style.display = 'block';
	document.getElementById('validateStudentNumber').style.display = 'block';
	if(number.length<=3){
		document.getElementById('student_number').focus();
		document.getElementById('validateStudentNumber').style.background = 'url(/images/cross.gif)';
	} else {
		document.getElementById('validateStudentNumber').style.background = 'url(/images/tick.gif)';
	}
	if(course.length<=5){
		document.getElementById('validateCourse').style.background = 'url(/images/cross.gif)';
		document.getElementById('course').focus();
	} else {
		document.getElementById('validateCourse').style.background = 'url(/images/tick.gif)';
	}
	if(college.length<=5){
		document.getElementById('validateCollege').style.background = 'url(/images/cross.gif)';
		document.getElementById('college').focus();
	} else {
		document.getElementById('validateCollege').style.background = 'url(/images/tick.gif)';	
	}
}
function PadDigits(n, totalDigits) { 
		n = n.toString(); 
		var pd = ''; 
		if (totalDigits > n.length) { 
				for (i=0; i < (totalDigits-n.length); i++) { 
						pd += '0'; 
				} 
		} 
		return pd + n.toString(); 
} 

function validateEmail(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
      return "invalid";
   } else {
			return "valid"; 
	 }
}

function validateConfirmForm(college, course, number) {
	if(document.getElementById('college')){
		document.getElementById('validateCollege').style.display = 'block';
		document.getElementById('validateCourse').style.display = 'block';
		document.getElementById('validateStudentNumber').style.display = 'block';
		var $return = true;
		if(number.length<=3){
			document.getElementById('student_number').focus();
			document.getElementById('validateStudentNumber').style.background = 'url(/images/cross.gif)';
			$return = false;
		} 
		if(course.length<=5){
			document.getElementById('validateCourse').style.background = 'url(/images/cross.gif)';
			document.getElementById('course').focus();
			$return = false;
		}
		if(college.length<=5){
			document.getElementById('validateCollege').style.background = 'url(/images/cross.gif)';
			document.getElementById('college').focus();
			$return = false;
		} 
		return $return;
	}
}

function validatePayment() {
	if(document.getElementById('cardnovalid').value=="1"){
		document.getElementById('vatvalid').style.display="block"
		document.getElementById('vatinvalid').style.display="none"
		var $return = true;
	}
	else
	{
		document.getElementById('vatvalid').style.display="none"
		document.getElementById('vatinvalid').style.display="block"
		var $return = false;
	}
	return $return;
}


function validatePurchaseProduct() {
		if(document.getElementById('parentproduct').value=="") {
			document.getElementById('parentvalid').style.display="none"
			document.getElementById('parentinvalid').style.display="block"
			var $return = false;
		}
		if(document.getElementById('childlicense').value=="") {
			document.getElementById('childvalid').style.display="none"
			document.getElementById('childinvalid').style.display="block"
			var $return = false;
		}
		return $return;
}

function updateStatus(status) {
	document.getElementById('declined_notes').style.display='none';
	document.getElementById('shipped_notes').style.display='none';
	document.getElementById('payment_notes').style.display='none';
	document.getElementById('not_complete_notes').style.display='none';
	if(status=="declined"){
		document.getElementById('declined_notes').style.display='block';
	}
	if(status=="shipped"){
		document.getElementById('shipped_notes').style.display='block';
	}	
	if(status=="not_complete"){
		document.getElementById('not_complete_notes').style.display='block';
	}	
	if(status=="Payment received"){
		document.getElementById('payment_notes').style.display='block';
	}
}


function nonstudent(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
		document.getElementById('educational_area').style.display=none
	}
	ajaxRequest.open("GET", "/purchase/action/nonStudent.php", true);
	ajaxRequest.send(null);
}
}


function getCustomer(name) {
	var ajaxRequest;  // The variable that makes Ajax possible!
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			document.getElementById('customer_list').style.display='block';	
			document.getElementById('customer_list').innerHTML = ajaxRequest.responseText;
		}
	}
	ajaxRequest.open("GET", "/webadmin/sale/action/getCustomer.php?name="+name, true);
	ajaxRequest.send(null);
}

function setCustomer(id, name) {
	document.getElementById('customerid').value = id;
	document.getElementById('customeridedit').value = id;
	document.getElementById('edit_customer').style.display = 'block';
	document.getElementById('customer_list').style.display='none';
	document.getElementById('find_customer').value = name;
	document.getElementById('addresses').style.display = 'none';
}

function hide(id){
	if(document.getElementById(id)){
		document.getElementById(id).style.display = 'none';
	}
}
function getItemCode(code, row) {
	var ajaxRequest;  // The variable that makes Ajax possible!
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			document.getElementById('item_code_list_'+row).style.display='block';	
			document.getElementById('item_code_list_'+row).innerHTML = ajaxRequest.responseText;
		}
	}
	ajaxRequest.open("GET", "/webadmin/sale/action/getItemCode.php?code="+code+"&row="+row, true);
	ajaxRequest.send(null);
}

function getItem(ourid, parentid, childid, row) {
	var ajaxRequest;  // The variable that makes Ajax possible!
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			document.getElementById('item_childid_'+row).value=childid;
			document.getElementById('item_code_list_'+row).style.display='none';	
			document.getElementById('item_code_'+row).value = ourid.toUpperCase();
			document.getElementById('item_description_'+row).value = ajaxRequest.responseText;
			document.getElementById('item_qty_'+row).focus();			
			document.getElementById('item_qty_'+row).value=1;
			getSubTotal(childid, document.getElementById('item_qty_'+row).value, document.getElementById('item_vat_'+row).value, row);
		}
	}
	ajaxRequest.open("GET", "/webadmin/sale/action/getItem.php?parentid="+parentid+"&childid="+childid, true);
	ajaxRequest.send(null);
}

function getSubTotal(childid, qty, vat, row) {
	var c_type = document.getElementById('customertype').value;
	var currency = document.getElementById('currencycode').value;
	var currencysymbol = "£";
	var customerid = document.getElementById('customerid').value;
	if(currency == 'eur'){
		currencysymbol = "€";
	}
	if(currency == 'usd'){
		currencysymbol = "$";
	}
	var ajaxRequest;  // The variable that makes Ajax possible!
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			document.getElementById('item_subtotal_'+row).value = currencysymbol+" "+ajaxRequest.responseText;
			var temp = 	(ajaxRequest.responseText*vat)-ajaxRequest.responseText;
			document.getElementById('item_vat_amount_'+row).value = temp.toFixed(2);
			var vatTemp = '';
			var total = '';
			var tempST = '';
			for(i=1; i<=2; i++){
				if(document.getElementById('item_vat_amount_'+i).value>=0.01){
					vatTemp = (vatTemp*1)+(document.getElementById('item_vat_amount_'+i).value*1);
					tempST = (document.getElementById('item_subtotal_'+i).value).substring(1);
					total = (total*1)+(tempST*1);
				}
			}
			document.getElementById('total_vat').value = currencysymbol+" "+vatTemp;
			document.getElementById('total').value = currencysymbol+" "+(total+vatTemp).toFixed(2);
			document.getElementById('totalpaid').value = currencysymbol+" "+(total+vatTemp).toFixed(2);
		}
	}
	ajaxRequest.open("GET", "/webadmin/sale/action/getSubTotal.php?customerid="+customerid+"&childid="+childid+"&vat="+vat+"&qty="+qty+"&c_type="+c_type+"&currency="+currency, true);
	ajaxRequest.send(null);
}

function IsNumeric(sText){
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++){ 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1){
         IsNumber = false;
      }
   }
   return IsNumber;
}

function isEmpty(code, row){
	if(code==''){
		var currency = document.getElementById('currencycode').value;
		var currencysymbol = "£";
		if(currency == 'eur'){
			currencysymbol = "€";
		}
		if(currency == 'usd'){
			currencysymbol = "$";
		}
		document.getElementById('item_code_list_'+row).style.display = 'none';
		document.getElementById('item_description_'+row).value='';	
		document.getElementById('item_subtotal_'+row).value='';
		document.getElementById('item_vat_amount_'+row).value='';
		document.getElementById('item_childid_'+row).value='';
		document.getElementById('item_qty_'+row).value='';
		var vatTemp = '0.00';
		var total = '0.00';
		var tempST = '0.00';
		
		for(i=1; i<=2; i++){
			if(document.getElementById('item_vat_amount_'+i).value>=0.01){
				vatTemp = (vatTemp*1)+(document.getElementById('item_vat_amount_'+i).value*1);
				tempST = (document.getElementById('item_subtotal_'+i).value).substring(1);
				total = (total*1)+(tempST*1)+(vatTemp*1);
			}
		}
		
		document.getElementById('total_vat').value = currencysymbol+vatTemp;
		total = (total*1).toFixed(2);
		document.getElementById('total').value = currencysymbol+((total+vatTemp)*1).toFixed(2);
		document.getElementById('totalpaid').value = currencysymbol+((total+vatTemp)*1).toFixed(2);
	}
}


function getBillVAT(amount){
	document.getElementById('vat_amount').value = (amount-(amount/(document.getElementById('vat_code').value*1))).toFixed(2);
	document.getElementById('total_paid').value = amount;
}

// Updates the basket
$(function(){
	$('.childitem td.item_buy a').bind("click", function(){
		$.post("/action/addtobasket.php", { id: $(this).attr('id') });
		setTimeout('updateItems()', 100);
		setTimeout('getSubTotal()', 100);
	});
});
function getSubTotal(){
	$.post("/action/basketsubtotal.php", {}, function(data){ 
		$('#basket_subtotal').html(data);	
	});
}
function updateItems(){
	$.post("/action/basket_items.php", {}, 
		function(data){
		$('#sidebasket').html(data);
		$('#sideBasketArea').show("slow");
		removeBasket();
	});
}
function getDeletedSubTotal(){
	$.post("/action/basketsubtotal.php", {}, function(data){ 
			if(data == 0.00){
				$('#sideBasketArea').hide('slow');
			}
	 });
}

function removeBasket(){
	$('.basket_remove').bind("click", function(){
		var id = $(this).attr('id').substring(7);																			 
		if($('#qty_'+id).html() == '1'){
			$(this).parent().hide('slow');
		} else {
			var qty = $('#qty_'+id).html();
			$('#qty_'+id).html((qty*1) - 1);
		}
		$.post("/action/removefrombasket.php", { id: id });
		setTimeout('getDeletedSubTotal()', 100);
		setTimeout('getSubTotal()', 100);
	});
}
/*
// Hides the basket if there no items and shows if there are
$(function(){
	$.post("/action/basket_items.php", {}, 
		function(data){
			$('#sidebasket').html(data);
			$('#sideBasketArea').hide();
			if($('#sidebasket > ul > li').hasClass('basketrow')) {
				getSubTotal();
				$('#sideBasketArea').show();
			}
			removeBasket();
		});
});


$(function(){
	$('#checkout').hover(
		function() {
			$('#checkout img').css({'display':'block', 'margin-top': '5px'});
			$('#checkout img').animate({marginLeft: '140px'}, 200);
		},
		function(){
			$('#checkout img').css({'display':'none', 'margin-left': '136px'});
		}
	);
});
function updateCheckoutSubTotal(){
	$.post("/action/basketsubtotal.php", {}, function(data){ 
		$('#checkout_subtotal').html(data);
	});
}
function updateCheckoutItem(){
	var id = $('#checkout_item_id').attr('value');
	var qty = $('#checkout_item_qty').attr('value');
	if(!IsNumeric(qty)){
		qty = 1;
	}
	if($('#checkout_item_remove').attr('checked') || qty<=0){
		var remove = 1;
		$('#row_'+id).hide();
	} else {
		$('#qty_'+id).html(qty);
		var remove = 0;
	}
	$.post("/checkout/action/updateCheckoutItem.php", { id: id,  qty: qty, remove: remove });
	setTimeout('updateCheckoutSubTotal()', 100);
	tb_remove();
};
*/
$(function(){
	$('#free_postage').hide();
	$('#delivery_date').change(function(){
		var value = $('#delivery_date option:selected').text();
		var amount = $('#delivery_date option:selected').val()*1;
		if(amount <=3){
			optional_amount = 0;
		}
		$('#postage_price').html(amount);
		$('#postageprice').val(amount);
		if(amount>=3){
			$('#free_postage').hide('slow');
		} else {
			if(amount==0){
				$('#free_postage').show('slow');	
			} else {
				$('#free_postage').hide('slow');
			}
		}
		if($('#postage_price').html()==''){
			$('#postage_price').html('0.00');	
		}
		updatePostage(amount, 1);
		$('#delivery_date_hidden').val(value);
	});
});

function padDigits(n, totalDigits) 
{ 
		n = n.toString(); 
		var pd = ''; 
		if (totalDigits > n.length) 
		{ 
				for (i=0; i < (totalDigits-n.length); i++) 
				{ 
						pd += '0'; 
				} 
		} 
		return pd + n.toString(); 
} 
function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}