// Open links in new windows
// xhtml compliant links in new windows
function externalLinks() {
if (!document.getElementsByTagName) return;

var anchors = document.getElementsByTagName("a");
for (var i=0; i<anchors.length; i++) {
var anchor = anchors[i];
var relvalue = anchor.getAttribute("rel");

if (anchor.getAttribute("href")) {
var external = /external/;
var relvalue = anchor.getAttribute("rel");
if (external.test(relvalue)) { anchor.target = "_blank"; }
}
}
}

// Validate Contents (just ensure dangerous goods confirm is checked)
function validateContents() {
// alert('hello');
	var fTF = document.getElementById('fTF');
	fConfirm=fTF.confirmnodangerousgoods.checked;
	if(fConfirm==false) {
		alert("Please confirm your parcel contains no dangerous items.");
		return false;
	}
}

// Validate Your Parcel Form
function validateYPF() {
//	alert('hello');
	var yPF = document.getElementById('yPF');
	fWeight=yPF.weight.value;
	fLength=yPF.length.value;
	fWidth=yPF.width.value;
	fHeight=yPF.height.value;
	fUnits=yPF.units.value;
//	fContents=yPF.contents.value; // don't bother validating
	if (
	(fWeight=='') || 
	(fLength=='') || 
	(fWidth=='') || 
	(fHeight=='')) {
		alert('Please complete all fields');
		//event.returnValue=false;
		return false;
	}
	// check numbers are valid
	if (
	isNaN(parseFloat(fWeight)) || 
	isNaN(parseFloat(fLength)) || 
	isNaN(parseFloat(fWidth)) || 
	isNaN(parseFloat(fHeight))) {
		alert('Please ensure you have entered valid numbers in the weight and dimensions fields');
		//event.returnValue=false;
		return false;
	}
	// normal weight limit calc
	if (fWeight>=29.91) {
		alert('Sorry, we can only deliver parcels up to 29.9kg');
		//event.returnValue=false;
		return false;	
	}
	// max dimensions limit calc
	if(fUnits=='inches') {
		fLength=fLength*2.54;
		fWidth=fWidth*2.54;
		fHeight=fHeight*2.54;
	}
	var dDim = parseFloat(fLength) + parseFloat(fHeight*2) + parseFloat(fWidth*2);
	//alert(dDim);
	if (dDim>300 || fLength>150 || fHeight>150 || fWidth>150) {
		alert('Sorry, your parcel dimensions are too large. We can deliver parcels up to 150cm in any dimension, or up to 300cm across length and girth combined, to most countries. We recommend you either try to split your parcel into smaller parcels (if possible!), or select a specialist delivery service. We recommend Mail Boxes Etc. as they can also handle large, valuable or awkward items. Visit www.mbe.co.uk');
		return false;
	}	
	// dimensional weight limit calc
	var dWeight = (fLength*fWidth*fHeight)/5000
	if (dWeight>=29.9) { 
		alert('Sorry, your parcel dimensions are too large for delivery via our services. We recommend you either try to split your parcel into smaller parcels (if possible!), or select a specialist delivery service.');
		return false;
	}
	// no negative or zero values!!
	if (
	(fWeight<=0) || 
	(fLength<=0) || 
	(fWidth<=0) || 
	(fHeight<=0)) {
		alert('Please ensure all weight and dimensions data is greater than zero.');
		//event.returnValue=false;
		return false;
	}
	
}

// Validate Sender Details Form
function validateSender() {
//	alert('hello');
	var fTF = document.getElementById('fTF');
	fName=fTF.name.value;
	fAddress1=fTF.address1.value;
	fPostcode=fTF.postcode.value;
	fCounty=fTF.county.value;
	fTel=fTF.tel.value;
	emailID=fTF.email;
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please enter your email address");
		emailID.focus();
		return false;
	}
	if (echeck(emailID.value)==false){
		emailID.value="";
		emailID.focus();
		return false;
	}

	if (
	(fName=='') || 
	(fAddress1=='') || 
	(fPostcode=='') || 
	(fCounty=='') || 
	(fTel=='')) {
		alert('Please complete all fields (only Address 2 is optional)');
		return false;
	}
}

// Validate Sender Details Form
function validateEmailQuote() {
//	alert('hello');
	var fTF = document.emailquoteform;
	emailID=fTF.emailquote;
	
	// only validate email if has value
	if ((emailID.value!=null)||(emailID.value!="")){
		if (echeck(emailID.value)==false){
			emailID.value="";
			emailID.focus();
			return false;
		}
	}

	if (
	(fName=='') || 
	(fAddress=='') || 
	(fPostcode=='') || 
	(fTel=='')) {
		alert('Please complete all fields');
		return false;
	}
}

function echeck(str) {

		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1){
		   alert("Please enter a valid email address");
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
			alert("Please enter a valid email address");
			return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   	alert("Please enter a valid email address");
			return false;
		}

		if (str.indexOf(at,(lat+1))!=-1){
			alert("Please enter a valid email address");
			return false;
		}

		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
			alert("Please enter a valid email address");
			return false;
		}

		if (str.indexOf(dot,(lat+2))==-1){
			alert("Please enter a valid email address");
			return false;
		}
		
		if (str.indexOf(" ")!=-1){
			alert("Please enter a valid email address");
			return false;
		}

		return true;				
}


// Validate Recipient Details Form
function validateRecipient() {
//	alert('hello');
	var fTF = document.getElementById('fTF');
	fToName=fTF.toname.value;
	fToAddress1=fTF.toaddress1.value;
	fToPostcode=fTF.topostcode.value;
	fToType=fTF.totype.value;
	fToTel=fTF.totel.value;
	if (
	(fToName=='') || 
	(fToAddress1=='') || 
	(fToPostcode=='') || 
	(fToTel=='')) {
		alert('Please complete all fields');
		return false;
	} 
}

function validateDropoff() {
//	alert('Validating');
	var fTF = document.getElementById('fTF');
	if(fTF.dropoffon.selectedIndex==0 || fTF.store.selectedIndex==0) {
		alert('Please complete all fields');
		return false;
	}
}

function validateCollection() {
//	alert('Validating');
	var fTF = document.getElementById('fTF');
	if(fTF.pickupon.selectedIndex==0 || fTF.pickupaddress.value=='' || fTF.pickuppostcode.value=='' || fTF.pickuptel.value=='') {
		alert('Please complete all fields. Only \'Notes for pickup\' is optional.');
		return false;
	}
}

// Validate Payment Details Form
function validatePayment() {
//	alert('hello');
	var fTF = document.getElementById('fTF');
	fBillingFirstnames=fTF.BillingFirstnames.value;
	fBillingSurname=fTF.BillingSurname.value;
	fBillingAddress1=fTF.BillingAddress1.value;
	fBillingCity=fTF.BillingCity.value;
	fBillingPostCode=fTF.BillingPostCode.value;
	fBillingCountry=fTF.BillingCountry.value;
	fTerms=fTF.agreeterms.checked;
//	alert(fTerms);

	if (fBillingFirstnames=='' || fBillingSurname=='' || fBillingAddress1=='' || fBillingCity=='' || fBillingPostCode=='' || fBillingCountry=='') {
		alert('Please ensure you have completed all required fields.');
		return false;
	} else if (fTerms==false) { 
		alert('You must agree to the terms and conditions before you proceed.');
		return false;
	}

}

// Validate Payment Details Form
function validateRegistration() {
//	alert('hello');
	var fTF = document.getElementById('signupform');
	fName=fTF.name.value;
	fEmail=fTF.email.value;
	fPassword=fTF.new_password.value;
	fCPassword=fTF.confirm_password.value;

	if (fName=='' || fEmail=='' || fPassword=='' || fCPassword=='') {
		alert('Please ensure you have completed all required fields.');
		return false;
	} else if (fPassword!=fCPassword) { 
		alert('Please ensure your passwords match.');
		return false;
	}

}


function answer(obj) { 
	if(obj.className=='sel') { obj.className=''; } else { obj.className='sel'; }
}

function showTip(tip) {
	document.getElementById(tip).style.display='block';
}

function hideTip(tip) {
	document.getElementById(tip).style.display='none';
}

function getHelp(hlp) {
	switch(hlp) {
		case 'dropoff':
			document.getElementById('help-collect').style.display = 'none';
			document.getElementById('help-dropoff').style.display = 'block';
			break;
		case 'collection':
			document.getElementById('help-dropoff').style.display = 'none';
			document.getElementById('help-collect').style.display = 'block';
//			document.getElementById('help-wrap').innerHTML = '<div id="help-collection"><h2>Collection help</h2></div>';
			break;
	}
	Element.Appear('help-wrap');
}






$(document).ready(function() {
	externalLinks();

	
});
