// global variables
var LastScript;
var LastAction;
var ThisSession;
var AjaxEnabled = false;
var LoggedIn = false;
var premiumPrinted=true;
var over2kgPrinted=true;
window.onload = function() {
	prelodImages();
	return true;
}
window.onunload = function() {
	if ( !premiumPrinted ) {
		alert('You must print the SpeedTrack consignment notes\nbefore leaving this page');
		history.go(0);
	}
	if ( !over2kgPrinted ) {
		alert('You must print the Customs Invoices\nbefore leaving this page');
		history.go(0);
	}
}
function iif(cond,first,last) {
	if ( cond )
		return first;
	else
		return last;
}
function UpdateGlobals(script,action,session) {
	LastScript = script;
	LastAction = action;
	ThisSession = session;
}
function PrintLabels(url,form_name) {
	if ( document.forms[form_name].LabelPos ) {
		url = url+'&LabelPos='+escape(document.forms[form_name].LabelPos.value);
	}
	url = url+'&TopMargin='+escape(document.forms[form_name].TopMargin.value);
	url = url+'&LeftMargin='+escape(document.forms[form_name].LeftMargin.value);
	url = url+'&VertSpace='+escape(document.forms[form_name].VertSpace.value);
	url = url+'&HorizSpace='+escape(document.forms[form_name].HorizSpace.value);
	PopupWindow(url,600,400,0,0,'Y','Y');
}
function CalcLabelSheets(labels) {
	var start_label = document.form1.LabelPos.value;
	var first_sheet_labels = 4-start_label+1;
	var remainder = labels - first_sheet_labels;
	var sheets = 1;
	if ( remainder > 0 ) {
		// there are labels to be printed on successive pages
		sheets = Math.floor(remainder/4);
		if (remainder%4>0) {
			sheets++;
		}
		sheets++;
	}
	document.getElementById('sheets').innerHTML=sheets;
}
function SendPassword() {
	if ( document.login_form.var_Email.value.indexOf('@') == -1 ) {
		alert('You must enter your Email Address first');
	} else {
		// send email via AJAX
		var params = 'a=3&s='+document.login_form.s.value+'&ID1='+document.login_form.var_Email.value;
		AJAX_runScript('account.php',params,null,'S');
	}
}
function SubmitParcelForm() {
	// creates a validation string for
	// the parcel form on the create shipment page
	var str = "'parcel_form'";
	str += ",'var_Weight','Weight','RinRange0.01:20'";
	str += ",'var_Length','Length','RinRange0.1:150'";
	str += ",'var_Width','Width','RinRange0.1:150'";
	str += ",'var_Height','Height','RinRange0.1:150'";
	str += ",'var_Description','Customs Description','R'";
	str += ",'var_ApproximateValue','Customs Approx. Value','RisNum:1.00'";
	str += ",'var_Country','Country','mustSelect:XXX'";
	if ( document.parcel_form.var_Insured.checked || document.parcel_form.var_Weight.value > 2 || document.parcel_form.var_Premium.checked ) {
		str += ",'var_Name','Send To Name','R','var_Address1','Address 1','R','var_City','City','R'";
		form_validate_msg = '\n- Parcels over 2.0Kg and Covered parcels must have address fields.';
		if ( document.parcel_form.var_Insured.checked ) {
			str += ",'var_InsuredValue','Covered Value','RisNum'";
			if ( document.parcel_form.var_InsuredValue.value == '' ) {
				form_validate_msg += '\n- Covered parcels must have a covered value.';
			}
		}
	}
	if ( document.parcel_form.var_Premium.checked ) {
		str += ",'var_Email','Recipient Email Address','RisEmail'";
		form_validate_msg += '\n- SkippyTracker parcels need the recipient\'s email address so that they can receive a tracking code after the parcel has been procesed.';
		if ( !document.parcel_form.var_ReadPremiumTerms.checked ) {
			str += ",'var_ReadPremiumTerms','Read SkippyTracker Terms of Use','isChecked'";
			form_validate_msg += '\n- For SkippyTracker parcels you must acknowledge that you have read the Terms of Use.';
		}
	}
	if ( document.parcel_form.var_ProofOfPostage.checked ) {
		if ( str.indexOf('var_Email') == -1 ) {
			str += ",'var_Email','Recipient Email Address','RisEmail'";
		}
		if ( str.indexOf('var_Name') == -1 ) {
			str += ",'var_Name','Send To Name','R','var_Address1','Address 1','R','var_City','City','R'";
		}
		form_validate_msg += '\n- Proof of Postage parcels need the recipient\'s address and email address so that they can receive notification after the parcel has been procesed.';
	}
	if ( ValidateForm(str) ) {
		document.parcel_form.submit();
	}
}
function DoInsuredValue() {
	if ( document.parcel_form.var_Insured.checked ) {
		document.getElementById('ivalue').style.visibility="visible";
		document.parcel_form.var_InsuredValue.focus();
	} else {
		document.getElementById('ivalue').style.visibility="hidden";
		document.parcel_form.var_InsuredValue.value='0.00';
	}
	return true;
}
function SubmitConfirmationEmail(form_name) {
	// creates a validation string for
	// the parcel form on the create shipment page
	var str = "'"+form_name+"'";
	str += ",'email','Recipient Email','RisEmail'";
	if ( ValidateForm(str) ) {
		document.forms[form_name].submit();
	}
}
function SubmitNonReceiptEmail() {
	// creates a validation string for
	// the parcel form on the create shipment page
	var str = "'track2'";
	str += ",'email','Recipient Email','RisEmail'";
	if ( ValidateForm(str) ) {
		document.track2.submit();
	}
}
function SubmitNonReceiptLetterEmail() {
	// submits to the non-receipt letter email
	document.track2.submit();
}
function RateCalc() {
	var weight = document.form1.var_Weight.value;
	if ( weight <= 0 ) {
		alert('Weight must be greater than zero');
		document.getElementById("rate").innerHTML="";
		document.getElementById("msg").innerHTML="";
		document.form1.var_Weight.focus();
	} else if ( weight > 20 ) {
		alert('Maximum weight is 20 Kg');
		document.getElementById("rate").innerHTML="";
		document.getElementById("msg").innerHTML="";
		document.form1.var_Weight.focus();
	} else {
		document.getElementById("error").innerHTML="";
		var params = 'a=1&s='+document.form1.s.value+'&var_Weight='+document.form1.var_Weight.value+'&var_CountryCode='+document.form1.var_CountryCode.value;
		AJAX_runScript('rate_calculator.php',params,null,'E');
	}
}
function LargeParcelCalc() {
	var weight = document.form5.var_Weight.value;
	if ( weight <= 0 ) {
		alert('Weight must be greater than zero');
		document.getElementById("largerate").innerHTML="";
		document.getElementById("largemsg").innerHTML="";
		document.form5.var_Weight.focus();
	} else if ( weight > 20 ) {
		alert('Maximum weight is 20 Kg');
		document.getElementById("largerate").innerHTML="";
		document.getElementById("largemsg").innerHTML="";
		document.form5.var_Weight.focus();
	} else {
		document.getElementById("largeerror").innerHTML="";
		var params = 'a=6&s='+document.form5.s.value+'&var_Weight='+document.form5.var_Weight.value+'&var_CountryCode='+document.form5.var_CountryCode.value+'&var_Length='+document.form5.var_Length.value+'&var_Width='+document.form5.var_Width.value+'&var_Height='+document.form5.var_Height.value;
		AJAX_runScript('rate_calculator.php',params,null,'E');
	}
}
function VolWeightCalc() {
	document.getElementById("verror").innerHTML="";
	document.getElementById("vweight").innerHTML="";
	if ( document.form3.var_Length.value > 150 || document.form3.var_Length.value < 0.1 ) {
		alert('Length must be between 0.1 and 150 cm');
	} else if ( document.form3.var_Width.value > 150 || document.form3.var_Width.value < 0.1 ) {
		alert('Width must be between 0.1 and 150 cm');
	} else if ( document.form3.var_Height.value > 150 || document.form3.var_Height.value < 0.1 ) {
		alert('Height must be between 0.1 and 150 cm');
	} else {
		var params = 'a=3&s='+document.form3.s.value+'&var_Length='+document.form3.var_Length.value+'&var_Width='+document.form3.var_Width.value+'&var_Height='+document.form3.var_Height.value;
		AJAX_runScript('rate_calculator.php',params,null,'E');
	}
}
function InsuranceCalc() {
	var val = document.form2.var_InsuredValue.value;
	if ( val <= 0 ) {
		alert('Insured Value must be greater than zero');
		document.getElementById("irate").innerHTML="";
		document.getElementById("imsg").innerHTML="";
		document.form2.var_InsuredValue.focus();
	} else {
		document.getElementById("ierror").innerHTML="";
		var params = 'a=2&s='+document.form2.s.value+'&var_InsuredValue='+document.form2.var_InsuredValue.value+'&var_CountryCode='+document.form2.var_CountryCode.value;
		AJAX_runScript('rate_calculator.php',params,null,'E');
	}
}
function TermsBoxChecked(sForm,sField) {
	if ( document.forms[sForm].elements[sField].checked ) {
		return true;
	}
	alert('You must indicate that you have read the Terms and Conditions first');
	return false;
}
function prelodImages() {
	if (document.images) {
		var i;
		i = new Image();
		i.src = "../images/spb-gold-left.gif";
		i = new Image();
		i.src = "../images/spb-gold-fill.gif";
		i = new Image();
		i.src = "../images/spb-gold-right.gif";
		i = new Image();
		i.src = "../images/spb-silver-left.gif";
		i = new Image();
		i.src = "../images/spb-silver-fill.gif";
		i = new Image();
		i.src = "../images/spb-silver-right.gif";
		i = new Image();
		i.src = "../images/spb-green-left.gif";
		i = new Image();
		i.src = "../images/spb-green-fill.gif";
		i = new Image();
		i.src = "../images/spb-green-right.gif";

		i = null;
	}
}
function DisplayRatesList(s,r) {
	PopupWindow('../rate_calculator.php?a=5&s='+s+'&r='+r+'&w='+document.form4.var_Weight.value,600,600,0,0,'Y','Y','N','N','Y','N');
}
function DisplayFutureRates(s,r) {
	PopupWindow('future_rates.php?a=2&s='+s+'&r='+r+'&w='+document.form1.var_Weight.value,600,600,0,0,'Y','Y','N','N','Y','N');
}
function checkMaxCover() {
	if ( Number(document.parcel_form.var_InsuredValue.value) > 500 ) {
		alert('Maximum Loss/Damage cover is $500.00');
		document.parcel_form.var_InsuredValue.value='500.00';
		return false;
	}
	return true;
}
function checkCalcMaxCover() {
	if ( Number(document.form2.var_InsuredValue.value) > 500 ) {
		alert('Maximum Loss/Damage cover is $500.00');
		document.form2.var_InsuredValue.value='500.00';
		return false;
	}
	return true;
}
function activateDimensions(istrue) {
	if ( istrue ) {
		AJAX('ajax_functions.php?a=1&s='+ThisSession+'&activate=Y',true,'POST');
	} else {
		AJAX('ajax_functions.php?a=1&s='+ThisSession+'&activate=N',true,'POST');
	}
	return false;
}