//<!--
//	SHARED ONLINE ORDER FORM CODE
// www.egadgetfactory.com - online order form
// @author: ben baker
// @date: 24-01-08
//
// IE check
var EXPLORER = ( document.all );
//
// total cost 
var total = 0;
//
//	refreshes cost if page refreshed and options left checked
//
function refreshCost(){
	// ref to checkbox
	var el;
	for (var i=0;i<prod_arr.length;i++ ){
		el = document.getElementById( prod_arr[ i ].id );
		if( el == null ) break;
		if( el.checked )el.onclick();
	}
}
// Round to 2 decimal places
function to2dp( n ){
	ans = n * 1000
	// convert answer to string
	ans = Math.round(ans /10).toString();
	// length of amount string
	len = ans.length;
	// get hold of major part of cost
	var maj = ans.substring(0,len-2);
	// get hold of minor part of cost
	var min = ans.substring(len-2,len);
	// clear answer var
	ans="";
	// check if major is empty/nothing
	if( maj == "" ) maj = 0;
	// add major to amount
	ans += maj;
	// if the minor is not nothing add it to amount
	if( min > 0 )ans += "." + min
	// return answer to 2 dp
	return ans
}
//
//	Checks for decimal place when adding tax to total cost
//  @note returns string
//
function checkForDecimal( tot, tax ){
	// get the tax to 2 decimal places
	var a_as_str = to2dp( tax );
	// set total amoutn with tax var
	var tot_with_tax = tot;
	// if there is a decimal place then split the tax
	if (a_as_str.indexOf(".")!=-1){
		// get pounds and pence
		var arr = a_as_str.split( "." );
		// check if no pounds, then set to zero
		//if( arr[ 0 ] == "" ) arr[ 0 ] = 0;
		// add pounds to total
		tot_with_tax += parseInt( arr[ 0 ] );
		// add pence to string if greater than zero
		//if( arr[ 1 ] > 0 )
		tot_with_tax = String( tot_with_tax ) + "." + String( arr[ 1 ] );
	}else{
		// just add tax as it's pounds
		tot_with_tax + tax;
	}
	// return total with tax as string
	return tot_with_tax;
}
/*
 * Calculate an amount minus tax
 */
function calcTax( amount ){
	//alert(getVatAmount(amount))
	// calculate Tax
	var incTax = checkForDecimal( amount, getVatAmount(amount) );
	// rounded
	return incTax;
}
//
//	get amount of VAT of total
// needs to return to 2 dp
//
function getVatAmount(amount){
	// return Math.floor( ( TAX_AMOUNT / 100 ) * amount )
	return ( TAX_AMOUNT / 100 ) * amount;
}
//
//	returns a product basd on it's id
//
function getProduct( id ){
	// loop produtcs
	for( var i = 0; i < prod_arr.length; i++ ){
		if( id == prod_arr[ i ].id ) return prod_arr[ i ];
	}
	// if no product found return null
	return null;
}
//
function addToTotal( boo, val){
	var amount = boo ?  val : -val;
	// check it will be above or equal to zero
	if( ( total + amount ) >= 0) total += amount;
	// set cost text
	setCostText();
}
 /* checkbox onChange method
 * 
 */
function checkboxChange( checkbox ){
	// get hold of product data
	var prod = getProduct( checkbox.id );
	//
	//	if it's a package that has an alternate cost option 
	//
	if ( prod.type == "package" && prod.value2 != null ){
		// if it's checked add cost - else - remove cost
		checkbox.checked ? addSoftwareCostToPackage( checkbox.id ) : removePackageCost( checkbox );
	}else{
		addToTotal( checkbox.checked, prod.value );
	}
}
//
//	remove the cost of software from a package
//
function removePackageCost( checkbox ){
	// remove package cost 
	addToTotal( false, checkbox.curr_cost );
}
//
//	add the cost of software from a package
//
function addSoftwareCostToPackage( id, calledByRad ){
	var chkbx = document.getElementById( id );
	var rads = document.getElementsByName( id + "_group" );
	var prod = getProduct( id );
	var r,curr_val;
	//
	// if called by radio button remove previous cost
	if( calledByRad ) addToTotal( false, chkbx.curr_cost );
	// loop radio buttons
	for(var i=0;i<rads.length;i++){
		// point at radio button
		r = rads[ i ];
		// if the last radio button is pressed (the more expensive option)
		if( r.checked ){
			// see if it's the more expensive cost
			curr_val = r.value == "Activote for PowerPoint" ? prod.value2 : prod.value;
			// store reft to current cost
			chkbx.curr_cost = curr_val;
			break;
		}
	}
	// add cost of package
	addToTotal( true, curr_val );
}
//
// gets product cost from URL
//
function getProdsFromURL(){
	var add = new String( location );
	var hasProdInURL = add.indexOf("?");
	if(hasProdInURL==-1)return;
	var qPos = add.indexOf("?") + 5;
	var id = add.substring( qPos, qPos+8 );
	// get product element
	var prodEl = document.getElementById( id );
	// if no element return
	if(prodEl==null) return;
	// check product checkbox
	prodEl.checked = true;
	//
	// if it's not a package go to details section
	window.location.hash = id;
	//document.form.cont_nam.focus();
}
//
//	called by software radio buttons
//
function packageSoftwareChange( id ){
	var chkbx = document.getElementById( id );
	// if the radio is checked ad cost of software
	if( chkbx.checked )addSoftwareCostToPackage( id ,true );
}
/*
 * Checks whether of not is integer, if not adds a zero to the end
 */
function checkInteger( amount ){
	/*alert(amount)*/
	return (amount % 1!= 0)? amount + "0":amount;
}
// sets the cost text
function setCostText(){
	var total_s = document.getElementById("total_sub"); 
	var total_s2 = document.getElementById("total_sub2"); 
	// set total cost text - if it's an integer, if not add a zero - and calc tax on amount
	total_s.innerHTML = CURRENCY + total;
	if ( total_s2 != null ) total_s2.innerHTML = total_s.innerHTML;
	//
	var vat_el = document.getElementById( "total_vat" );
	var vat_el2 = document.getElementById( "total_vat2" );
	// if tax is set add it
	vat_el.innerHTML = CURRENCY + "" + to2dp( getVatAmount( total ) );
	if ( vat_el2 != null ) vat_el2.innerHTML = vat_el.innerHTML;
	//
	// get hold of total cost element
	var total_c = document.getElementById( "total_cost" );
	var total_c2 = document.getElementById( "total_cost2" );
	// set total cost
	total_c.innerHTML = CURRENCY + calcTax( total );
	if ( total_c2 != null ) total_c2.innerHTML = total_c.innerHTML;
	//
	// total amount put into hiddent tag for order form
	var hidden = document.getElementById( "hidCost" );
	hidden.value = calcTax( total ) + CURRENCY_LETTERS + " ( exc. VAT: " + checkInteger( total ) + CURRENCY_LETTERS + " )";
}
function checkProducts(){
	// ref to checkbox
	var el;
	for (var i=0;i<prod_arr.length;i++ ){
		el = document.getElementById( prod_arr[ i ].id );
		if( el.checked )return true;
	}
	return false;
}
/*
 *	Check order form
 *  @note: checkMailSafe() is in js_form-validate.js
 */
 function checkOrder( theForm ){
	if (!checkProducts()){
		alert("Please select a product to purchase by checking it's checkbox.");
		window.location.hash = "uk_form";
		return false;
	}
	// name
	if ( !checkMailSafe( theForm.cont_nam.value ) ){
		alert("Please enter your name");
		theForm.cont_nam.focus();
		return false;
	}
	// institution
	if ( !checkMailSafe( theForm.cont_ins.value ) ){
		alert("Please enter your school name");
		theForm.cont_ins.focus();
		return false;
	}
	// Address
	if ( !checkMailSafe( theForm.cont_add.value ) ){
		alert("Please enter your Address");
		theForm.cont_add.focus();
		return false;
	}
	// PostCode
	if ( !checkMailSafe( theForm.cont_pco.value ) ){
		alert("Please enter your postcode");
		theForm.cont_pco.focus();
		return false;
	}
	// number
	if ( !checkMailSafe( theForm.cont_num.value ) ){
		alert("Please enter your contact number");
		theForm.cont_num.focus();
		return false;
	}
	//email
	  if ( !checkMailSafe(  theForm.cont_ema.value) )
  {
     alert("Please enter a complete email address in the form: yourname@yourdomain.com");
    theForm.cont_ema.focus();
    return false;
  }
  
  if (!isEmailAddr(theForm.cont_ema.value))
  {
    alert("Please enter a complete email address in the form: yourname@yourdomain.com");
    theForm.cont_ema.focus();
    return false;
  }
   
  if (theForm.cont_ema.value.length < 3)
  {
     alert("Please enter a complete email address in the form: yourname@yourdomain.com");
    theForm.cont_ema.focus();
    return false;
  }
	// Authorised by:
	if ( !checkMailSafe( theForm.cont_aut.value ) ){
		alert("Please enter who is authorising this purchase");
		theForm.cont_aut.focus();
		return false;
	}
	// Purchase Order No:
	if ( !checkMailSafe( theForm.cont_ord.value ) ){
		alert("Please enter your purchase order number");
		theForm.cont_ord.focus();
		return false;
	}
	if(!theForm.chk_agreement.checked){
		alert("You must check the box to say that you agree to the e-gadget factory terms and conditions of sale.");
		theForm.chk_agreement.focus();
		return false;
	}

	// returns true if no error
	return true;
 }
 //-->