function externalLinks(){
	for (var i=0; (a=document.getElementsByTagName('a')[i]);i++) {
          if (a.getAttribute("rel") == "external")
               a.target = "_blank";
	}
}

function navHover(){
	for (var i=0; li=document.getElementById('nav').getElementsByTagName('LI')[i];i++) {
		li.onmouseover = function(){
			this.className+= ' over';
		}
		li.onmouseout = function(){
			this.className = this.className.replace(' over','');
		}
	}
}

function validateInt(iString) {
    // no leading 0s allowed
    return (("" + parseInt(iString)) == iString);
};

function roundThis(x){
	return Math.round(x*100)/100;
}

function calculate(){

	var bw_pages_v = $("#bw_pages").val();
	var c_pages_v = $("#c_pages").val();
	var copies_v = $("#copies").val();
	var hard_bound_v = $("#hard_bound").val();
	var soft_bound_v = $("#soft_bound").val();
	var wiro_bound_v = $("#wiro_bound").val();
	var tmp1 = document.getElementById('extras').options.selectedIndex;
	var extras_v = document.getElementById('extras').options[tmp1].value;
	var tmp = document.getElementById('delivery').options.selectedIndex;
	var delivery_v = document.getElementById('delivery').options[tmp].value;

	if(validateInt(bw_pages_v) == false) { bw_pages_v = 0; }
	if(validateInt(c_pages_v) == false) { c_pages_v = 0; }
	if(validateInt(copies_v) == false) { copies_v = 1; }
	if(validateInt(hard_bound_v) == false) { hard_bound_v = 0; }
	if(validateInt(soft_bound_v) == false) { soft_bound_v = 0; }
	if(validateInt(wiro_bound_v) == false) { wiro_bound_v = 0; }

	var t_print = ((bw_pages_v * bw_pages_c) + (c_pages_v * c_pages_c)) * copies_v;

 if($("#next_day").get(0).checked == true) {
		hard_bound_c = hard_bound_1; 
		soft_bound_c = soft_bound_1; 
		wiro_bound_c = wiro_bound_1; 
	}
		if($("#two_days").get(0).checked == true) { 
		hard_bound_c = hard_bound_2; 
		soft_bound_c = soft_bound_2; 
		wiro_bound_c = wiro_bound_2; 
	}
	if($("#five_days").get(0).checked == true) {
		hard_bound_c = hard_bound_5; 
		soft_bound_c = soft_bound_5; 
		wiro_bound_c = wiro_bound_5; 
	}
	
	var discountValue = 100;
	if (parseInt(hard_bound_v) + parseInt(soft_bound_v) + parseInt(wiro_bound_v) >= 4) {
		discountValue = discountValue - discount; 
	} 

	var t_binding = (((hard_bound_v * hard_bound_c) + (soft_bound_v * soft_bound_c) + (wiro_bound_v * wiro_bound_c))/100)*discountValue;

	var t_extras = copies_v * eval(extras_v+'_c');

	var t_delivery = eval(delivery_v);

	var t_total = t_print + t_binding + t_extras + t_delivery;

	$("#total_print").val(roundThis(t_print).toFixed(2));
	$("#total_binding").val(roundThis(t_binding).toFixed(2));
	$("#total_extras").val(roundThis(t_extras).toFixed(2));
	$("#total_delivery").val(roundThis(t_delivery).toFixed(2));
	$("#total").val(roundThis(t_total).toFixed(2));

}

function billingAddress(x){
	var farr = new Array('_Building','_Street','_City','_State','_Postcode','_Country');
	
	if (x == true){
		for(var i=0;i<=farr.length;i++){
			$('#d'+farr[i]).val($('#b'+farr[i]).val());
		}
	} else {
		for(var i=0;i<=farr.length;i++){
			$('#d'+farr[i]).val('');
		}
	}
}

function checkCalculations(){
	if ($("#bw_pages").val() == ''){ $("#bw_pages").val(0);  }
	if ($("#c_pages").val() == ''){ $("#c_pages").val(0); }
	if ($("#copies").val() == ''){ $("#copies").val(1);  }
	if ($("#hard_bound").val() == ''){ $("#hard_bound").val(0); }
	if ($("#soft_bound").val() == ''){ $("#soft_bound").val(0); }
	if ($("#wiro_bound").val() == ''){ $("#wiro_bound").val(0); }
	
	if ((parseInt($("#hard_bound").val()) + parseInt($("#soft_bound").val()) + parseInt($("#wiro_bound").val())) != parseInt($("#copies").val())){
		alert("Number of copies dosn't match number of bindings");
		return false;
	}
	if ((parseInt($("#bw_pages").val()) + parseInt($("#c_pages").val())) <= 0){
		alert("Please enter number of pages");
		return false;
	}
}

function checkOrder(){
	var check = Array(
		'z_First_Name',
		'z_Last_Name',
		'z_Day_Time_Phone',
		'z_Email_Address',
		'b_Building',
		'b_Street',
		'b_City',
		'b_State',
		'b_Postcode',
		'b_Country',
		'f_upload',
		'd_Collection_Date',
		'd_Building',
		'd_Street',
		'd_City',
		'd_State',
		'd_Postcode',
		'd_Country'
	);
	
	for(i=0;i<check.length;i++){
		if($("#"+check[i]).val() == ''){
			alert("Please fill out all required fields. The cursor will be positioned in the first missing field.");
			$("#"+check[i]).get(0).focus();
			return false;
		}
	}	
}

function init(){
	if(arguments.callee.done)return;
	arguments.callee.done=true;
	externalLinks();
	/*@cc_on @*/
	/*@if (@_win32)
		navHover();
	/*@end @*/
	$("input").bind("keyup",function(){
		calculate();
	}).bind("change",function(){
		calculate();
	});
	$("select").bind("change",function(){
		calculate();
	});
	$("#d_Same_as_Billing_Address").bind("change",function(){
		billingAddress($("#d_Same_as_Billing_Address").get(0).checked);	
	});
	$("#calculateForm").bind("submit",function(){
		return checkCalculations();
	});
	$("#orderForm").bind("submit",function(){
		return checkOrder();
	});

};

if(document.addEventListener){document.addEventListener("DOMContentLoaded", init, null);}
/*@cc_on @*/
/*@if (@_win32)
  document.write('<script defer="defer" src="./js/javascript_ieo.js"><'+'/script>');
/*@end @*/
window.onload=init;