// Format values as currency - ie with two decimal places
// Includes fix for displaying the pound sign in text input boxes within php!!! Yeeehah!
function formatCurrency(num) {
	num = num.toString();
	if(isNaN(num)) {
		num = "0";
	}
//	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	pence = num%100;
	num = Math.floor(num/100).toString();
	if(pence<10) {
		pence = "0" + pence;
	}
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) {
		num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
	}
	var amount = (num + '.' + pence);
	var currSign = (String.fromCharCode(163));
	var currAmt = (currSign + amount);
	return (currAmt);
}

// Format values as currency - ie with two decimal places
function formatTwodecs(num) {
	num = num.toString();
	if(isNaN(num)) {
		num = "0";
	}
//	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	pence = num%100;
	num = Math.floor(num/100).toString();
	if(pence<10) {
		pence = "0" + pence;
	}
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) {
		num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
	}
	var amount = (num + '.' + pence);
	return (amount);
}


// autofill totals and discounts etc
function startCalc(){
	interval = setInterval("calc()",1);
}
function calc(){
	one = document.orderForm.qty_ravensong.value;
//	two = document.orderForm.qty_glittering.value;
//	three = document.orderForm.qtyTotal.value = (one * 1) + (two * 1);
	three = document.orderForm.qtyTotal.value = (one * 1);
	unitPrice = "10.00";
	document.orderForm.order_total.value = formatCurrency( (three * 1) * (unitPrice * 1) );
}
function stopCalc(){
	clearInterval(interval);
}

// accept ONLY numeric inputs
// calculate the ASCII code of the character entered
function CalcKeyCode(aChar) {
	var character = aChar.substring(0,1);
	var code = aChar.charCodeAt(0);
	return code;
}
function checkNumber(val) {
	var strPass = val.value;
	var strLength = strPass.length;
	var lchar = val.value.charAt((strLength) - 1);
	var cCode = CalcKeyCode(lchar);
	// Check if the keyed in character is a number
	if (cCode < 48 || cCode > 57 ) {
		var myNumber = val.value.substring(0, (strLength) - 1);
		val.value = myNumber;
	}
	return false;
}

// Postage layer scripts
var textMsg1 = "<table class=\"postageTable\"><tr><td>QUANTITY</td><td>UK</td><td>EUROPE</td><td>ROTW</td></tr><tr><td>1 CD</td><td>&#163;1.00</td><td>&#163;1.60</td><td>&#163;2.00</td></tr><tr><td>2 CD's</td><td>&#163;1.25</td><td>&#163;2.20</td><td>&#163;3.00</td></tr><tr><td>3 CD's</td><td>&#163;1.50</td><td>&#163;2.75</td><td>&#163;3.95</td></tr><tr><td>4 CD's</td><td>&#163;1.75</td><td>&#163;3.00</td><td>&#163;4.50</td></tr><tr><td colspan=4>To order 5 or more CD's please contact us to make special arrangements</td></tr></table>";
var textMsg2 = "";
var ie4 = (document.all) ? true : false;
var ns4 = (document.layers) ? true : false;
var ns6 = (document.getElementById && !document.all) ? true : false;
function hidelayer(lay) {
	if (ie4) {document.all[lay].style.visibility = "hidden";}
	if (ns4) {document.layers[lay].visibility = "hide";}
	if (ns6) {document.getElementById([lay]).style.display = "none";}
	writetolayer(lay,textMsg2);
}
function showlayer(lay) {
	if (ie4) {document.all[lay].style.visibility = "visible";}
	if (ns4) {document.layers[lay].visibility = "show";}
	if (ns6) {document.getElementById([lay]).style.display = "block";}
	writetolayer(lay,textMsg1);
}
function writetolayer(lay, txt) {
	if (ie4) {
		document.all[lay].innerHTML = txt;
	}
	if (ns4) {
		document[lay].document.write(txt);
		document[lay].document.close();
	}
	if (ns6) {
		over = document.getElementById([lay]);
		range = document.createRange();
		range.setStartBefore(over);
		domfrag = range.createContextualFragment(txt);
		while (over.hasChildNodes()) {
			over.removeChild(over.lastChild);
		}
		over.appendChild(domfrag);
	}
}
var isActive = "0";
function postageLayer() {
	if (isActive == "0") {
		showlayer('newlayer');
		isActive = "1";
	} else if (isActive == "1") {
		hidelayer('newlayer');;
		isActive = "0";
	}
}

// Strip leading zeros
var num,newTerm
function stripZeros(num){
	while (num.charAt(0) == "0") {
		newTerm = num.substring(1, num.length);
		num = newTerm;
	}
	if (num == "")
		num = "0";
	return num;
}

// form validation
function validate_form () {
	valid = true;
	if ( document.orderForm.qtyTotal.value == "0" ) {
		alert ( "Please enter the NUMBER OF COPIES of at least one of the Susan Raven CD's that you want to order." );
		document.forms.orderForm.qty_ravensong.focus();
		valid = false;
	} else if ( (document.orderForm.firstname.value == "") || (document.orderForm.firstname.value == "First Name")) {
		alert ( "Please enter your FIRST NAME so that we know who this order is for." );
		document.forms.orderForm.firstname.focus();
		valid = false;
	} else if ( (document.orderForm.lastname.value == "") || (document.orderForm.lastname.value == "Last Name")) {
		alert ( "Please enter your LAST NAME so that we know who this order is for." );
		document.forms.orderForm.lastname.focus();
		valid = false;
	} else if ( document.orderForm.email.value == "" ) {
		alert ( "Please enter your EMAIL ADDRESS so that we can reply to you to let you know the status of your order." );
		document.forms.orderForm.email.focus();
		valid = false;
	} else if ( document.orderForm.country.selectedIndex == 0 ) {
		alert ( "Please select a DELIVER TO COUNTRY from the list provided so that we know how much postage to add to your order." );
		document.forms.orderForm.country.focus();
		valid = false;
	}
	if (valid == true) {
		var $sellP = "10";
		if (document.orderForm.qty_ravensong.value != "") {
			document.orderForm.qty_ravensong.value = stripZeros(document.orderForm.qty_ravensong.value);
		}
		if (document.orderForm.qty_ravensong.value == "0") {
		    document.orderForm.qty_ravensong.value = "";
		}
//		if (document.orderForm.qty_glittering.value != "") {
//			document.orderForm.qty_glittering.value = stripZeros(document.orderForm.qty_glittering.value);
//		}
//		if (document.orderForm.qty_glittering.value == "0") {
//		    document.orderForm.qty_glittering.value = "";
//		}
		$total_qty = document.orderForm.qtyTotal.value;
		$total_amt = ($total_qty * $sellP);
		document.orderForm.order_total.value = formatTwodecs($total_amt);
	}
	return valid;
}

// prepare the hidden fields to go
function prepFormvals() {
	var $sellP = "10";
	$total_qty = document.orderForm.qtyTotal.value;
	$total_amt = ($total_qty * $sellP);
	document.orderForm.order_total.value = formatTwodecs($total_amt);
}
