/*=============================================

	CARDINAL MARITIME
	--------

	File		:	common.js
	Started		:	20/11/2007
	By			:	Jim Neath	


=============================================*/

function change(id)
{
	// forms
	var to = document.getElementById('expQuote');
	var from = document.getElementById('impQuote');
	
	// links
	var to_link = document.getElementById('exp_link');
	var from_link = document.getElementById('imp_link');
	
	if (id == 'expQuote')
	{
		to.style.display = '';
		from.style.display = 'none';
		to_link.className = 'active';
		from_link.className = '';
	}
	else
	{
		from.style.display = '';
		to.style.display = 'none';	
		from_link.className = 'active';
		to_link.className = '';
	}
}	

startList = function() 
{	
	if (document.all && document.getElementById) 
	{
		// ul element
		root = document.getElementById('navlist');
		
		// find below dropdowns
		selects = document.getElementsByTagName('select');
		
		for (i = 0; i < root.childNodes.length; i++) 
		{
			node = root.childNodes[i];
			
			if (node.nodeName.toLowerCase() == 'li') 
			{
				node.onmouseover = function() 
				{
					this.className += ' over';
					
					for (i = 0; i < selects.length; i++)
					{
						if (selects[i].className == 'hide')
						{
							selects[i].style.visibility = 'hidden';
						}
					}
				}
				
				node.onmouseout = function() 
				{
					this.className = this.className.replace(' over', '');
					
					for (i = 0; i < selects.length; i++)
					{
						if (selects[i].className == 'hide')
						{
							selects[i].style.visibility = '';
						}
					}
				}
			}
		}
	}
}

window.onload = startList;

/*=============================================
	added by Steve
=============================================*/

// Gets executed every time the page is loaded
function getObj(name)
{
 if (document.getElementById)
 {
   return document.getElementById(name);
 }
 else if (document.all)
 {
   return document.all[name];
 }
 else if (document.layers)
 {
   return document.layers[name];
 }
 else return false;
}

function openWin(theURL, winName, features)
{
	window.open(theURL, winName, features);
}

function selShipDate(shipDate, frmName) {
	
	x = getObj(frmName);
	
	if (shipDate == "none") {
		x.shippingDate.value = "No specified date";
	}
	else {
		x.shippingDate.value = shipDate;
	}
	return true;
	
}

function subForm(frmName) {
	x = getObj(frmName);
	x.submit();
}

function validChar(fVal) {
 var valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@ "
 var ok = "yes";
 var temp;
 for (var i=0; i<fVal.length; i++) {
   temp = "" + fVal.substring(i, i+1);
   if (valid.indexOf(temp) == "-1") ok = "no";
 }
 if (ok == "no") {
   return false;
 }
 else return true;
}

function validNum(fVal) {
 var valid = "0123456789 ()."
 var ok = "yes";
 var temp;
 for (var i=0; i<fVal.length; i++) {
   temp = "" + fVal.substring(i, i+1);
   if (valid.indexOf(temp) == "-1") ok = "no";
 }
 if (ok == "no") {
   return false;
 }
 else return true;
}

function chkupdfrm(name) {
	var x = getObj(name);
	var errors = false;
	var errortxt = "There were some errors with the form you just tried to submit.\n\nPlease check the messages below and make the requested corrections.";
	if (x.upd_pass.checked) {
		if (x.cur_password.value == ""){
			errors = true;
			errortxt += "\nYou need to enter your current password.";
		}
		if (!(x.new_pass1.value == x.new_pass2.value)) {
			errors = true;
			errortxt += "\nThe new password you have typed in does not match up. Please ensure the password you type into the two boxes is identical.";
		}
		if ((x.new_pass1.value == "") || (x.new_pass2.value == "")) {
			errors = true;
			errortxt += "\nYou need to enter your new password into both boxes, making sure you type the same password both times.";
		}
	}
	if (x.first_name.value == "") {
		errors = true;
		errortxt += "\nYou have not entered your first name.";
	}
	if (x.last_name.value == "") {
		errors = true;
		errortxt += "\nYou have not entered your last name.";
	}
	if (x.company.value == "") {
		errors = true;
		errortxt += "\nYou have not entered your company name.";
	}
	if (x.tel.value == "") {
		errors = true;
		errortxt += "\nYou have not entered your telephone number.";
	}
	if (errors) {
		alert (errortxt);
		return false;
	}
	else return true;
}

function changeState(name) {
	var x = getObj('upd_pass');
	var y = getObj(name);
	if (x.options[x.selectedIndex].value == "1") {
		y.style.display='';
	}
	else y.style.display='none';
}

function chkEmail(email) {
	atPos = email.indexOf("@");
	stopPos = email.lastIndexOf(".");
	
	if (email == "") {
		return false;
	}
	
	if (atPos == -1 || stopPos == -1) {
		return false;
	}
	
	if (stopPos < atPos) {
		return false;
	}
	
	if (stopPos - atPos == 1) {
		return false;
	}
	return true;
}

function chkQuoteFrm(frmName) {
	var errors = false;
	var x = getObj(frmName);
	var errorTxt = "Sorry, you have filled out one or more of the form fields incorrectly.\n";
	
	if (!validNum(x.cWeight.value)) {
		errors = true;
		errorTxt += "Cargo Weight should be a number\n";
	}
	if (!validNum(x.cVol.value)) {
		errors = true;
		errorTxt += "Cargo Volume should be a number\n";
	}
	if (x.cWeight.value == "") {
		errors = true;
		errorTxt += "Please enter a Cargo Weight\n";
	}
	if (x.cVol.value == "") {
		errors = true;
		errorTxt += "Please enter a Cargo Volume\n";
	}
	if (x.id == "expQuote") {
		if ((x.town.value!="X")&&(x.depot.value!="X")) {
			errors = true;
			errorTxt += "Please select either a Collection Point or a Depot\n";
		}
		if ((x.town.value=="X")&&(x.depot.value=="X")) {
			errors = true;
			errorTxt += "Please select either a Collection Point or a Depot\n";
		}
		if (x.portID.value=="X") {
			errors = true;
			errorTxt += "Please select a Destination point\n";
		}
	}
	else if(x.id == "impQuote") {
		if ((x.itown.value=="X") && (x.delUKP.checked == false)) {
			errors = true;
			errorTxt += "Please select a Delivery Town or delivery to a UK Port\n";
		}
		if (x.portID.value=="X") {
			errors = true;
			errorTxt += "Please select an Origin point\n";
		}
	}
	if ((x.name.value=="")) {
		errors = true;
		errorTxt += "Please enter your name so we can send you a copy of your quote\n";
	}
	if (!chkEmail(x.email.value) || (x.email.value=="")) {
		errors = true;
		errorTxt += "Please enter your email address so we can send you a copy of your quote\n";
	}
	if (errors) {
		errorTxt += "Please change these details and click on the submit button again.";
		errors = false;
		alert(errorTxt);
		return false;
	}
	else return true;
}

function chkContactFrm(frmName) {
	var x = getObj(frmName);
	var errors = false;
	var message = "Sorry, you have filled out one or more of the form fields incorrectly.\n";
	
	if(!validChar(x.name.value) || (x.name.value=="")) {
		message += "The Name field can only contain the characters Aa-Zz and 0-9\n";
		errors = true;
	}
	if (!chkEmail(x.email.value) || (x.email.value=="")) {
		errors = true;
		message += "Please enter a valid email address\n";
		errors = true;
	}
	if(!validNum(x.tel.value)) {
		message += "The tel field can only contain a valid number\n";
		errors = true;
	}
	if(!validChar(x.msg.value) || (x.msg.value=="")) {
		message += "The message field can only contain the characters Aa-Zz and 0-9\n";
		errors = true;
	}
	if (errors) {
		message += "Please change these details and click on the submit button again.";
		errors = false;
		alert(message);
		return false;
	}
	else return true;
}

function chkShenzhenFrm(frmName) {
	var x = getObj(frmName);
	var errors = false;
	var message = "Sorry, you have filled out one or more of the form fields incorrectly.\n";
	
	if(!validChar(x.name.value) || (x.name.value=="")) {
		message += "The Name field can only contain the characters Aa-Zz and 0-9\n";
		errors = true;
	}
	if(!validChar(x.company.value) || (x.company.value=="")) {
		message += "The Company field can only contain the characters Aa-Zz and 0-9\n";
		errors = true;
	}
	if (!chkEmail(x.email.value) || (x.email.value=="")) {
		errors = true;
		message += "Please enter a valid email address\n";
		errors = true;
	}
	if(!validNum(x.tel.value)) {
		message += "The tel field can only contain a valid number\n";
		errors = true;
	}
	if (errors) {
		message += "Please change these details and click on the submit button again.";
		errors = false;
		alert(message);
		return false;
	}
	else return true;
}

function chkCreditFrm(frmName) {
	var x = getObj(frmName);
	var errors = false;
	var message = "Sorry, you have filled out one or more of the form fields incorrectly.\n";
	
	if(!validChar(x.name.value) || (x.name.value=="")) {
		message += "The Name field can only contain the characters Aa-Zz and 0-9 and is a required field\n";
		errors = true;
	}
	if (!chkEmail(x.email.value) || (x.email.value=="")) {
		errors = true;
		message += "Please enter a valid email address\n";
		errors = true;
	}
	if(!validNum(x.tel.value) || (x.tel.value=="")) {
		message += "The telephone number field can only contain a valid number and is a required field\n";
		errors = true;
	}
	if(!validChar(x.coname.value) || (x.coname.value=="")) {
		message += "The Company Name field can only contain the characters Aa-Zz and 0-9 and is a required field\n";
		errors = true;
	}
	if (errors) {
		message += "Please change these details and click on the submit button again.";
		errors = false;
		alert(message);
		return false;
	}
	else return true;
}

function chkRegFrm(frmName) {
	var x = getObj(frmName);
	var errors = false;
	var message = "Sorry, you have filled out one or more of the form fields incorrectly.\n";
	
	if(!validChar(x.name.value) || (x.name.value=="")) {
		message += "The Name field can only contain the characters Aa-Zz and 0-9\n";
		errors = true;
	}
	if (!chkEmail(x.email.value) || (x.email.value=="")) {
		errors = true;
		message += "Please enter a valid email address\n";
		errors = true;
	}
	if(!validNum(x.tel.value) || (x.tel.value=="")) {
		message += "The tel field can only contain a valid number\n";
		errors = true;
	}
	if(!validChar(x.teamname.value) || (x.teamname.value=="")) {
		message += "The teamname field can only contain the characters Aa-Zz and 0-9\n";
		errors = true;
	}
	if (errors) {
		message += "Please change these details and click on the submit button again.";
		errors = false;
		alert(message);
		return false;
	}
	else return true;
}
function chkEnqFrm(frmName) {
	var x = getObj(frmName);
	var errors = false;
	var message = "Sorry, you have filled out one or more of the form fields incorrectly.\n";
	
	if(!validChar(x.cId.value)) {
		message += "The username field can only contain the characters Aa-Zz and 0-9\n";
		errors = true;
	}
	if(!validChar(x.cPass.value)) {
		message += "The password field can only contain the characters Aa-Zz and 0-9\n";
		errors = true;
	}
	if (errors) {
		message += "Please change these details and click on the submit button again.";
		errors = false;
		alert(message);
		return false;
	}
	else return true;
}
function chkEnq2Frm(frmName) {
	var x = getObj(frmName);
	var errors = false;
	var message = "Sorry, you have filled out one or more of the form fields incorrectly.\n";
	
	if(!validChar(x.name.value)) {
		message += "The name field can only contain the characters Aa-Zz and 0-9\n";
		errors = true;
	}
	if(!validChar(x.coName.value)) {
		message += "The company name field can only contain the characters Aa-Zz and 0-9\n";
		errors = true;
	}
	if(!validChar(x.town.value)) {
		message += "The town field can only contain the characters Aa-Zz and 0-9\n";
		errors = true;
	}
	if(!validNum(x.tel.value)) {
		message += "The telephone field must be a number\n";
		errors = true;
	}
	if(!chkEmail(x.email.value)){
		message += "You have entered an invalid email address\n";
		errors = true;
	}
	if(x.remMe.checked){
		if(!validChar(x.cId.value)) {
			message += "The username field can only contain the characters Aa-Zz and 0-9\n";
			errors = true;
		}
		if(!validChar(x.cPass.value)) {
			message += "The password field can only contain the characters Aa-Zz and 0-9\n";
			errors = true;
		}
	}
	if (errors) {
		message += "Please change these details and click on the submit button again.";
		errors = false;
		alert(message);
		return false;
	}
	else return true;
}
function chkNewName(frmName) {
	var x = getObj(frmName);
	var errors = false;
	var message = "Sorry, you have filled out one or more of the form fields incorrectly.\n";
	
	if(!validChar(x.name.value)) {
		message += "The name field can only contain the characters Aa-Zz and 0-9\n";
		errors = true;
	}
	if (errors) {
		message += "Please change these details and click on the submit button again.";
		errors = false;
		alert(message);
		return false;
	}
	else return true;
}

// function to deselect one of the form fields in the import form
function deSel(toDeSel, origin) {
	var x = getObj(toDeSel);
	if(origin == "delUKP") {
		x.options[0].selected = true;
	}
	else if(origin == "town" || origin == "itown") {
		x.checked = false;
	}
}

// function to deselect one of the form fields in the export form
function deSelExp(toDeSel, origin) {
	var x = getObj(toDeSel);
	if(origin == "town") {
		x.options[0].selected = true;
	}
	else if(origin == "depot") {
		x.options[0].selected = true;
	}
}

function doConversion() {
	var returnVal;
	var x = getObj('unitConverter');
	var initVal = x.originalVal.value;
	var convId = x.conversion.value;
	if (initVal == ""){
		alert("You must enter an initial value.");	
	}
	else{
		switch (convId) {
		case "1": // ft to m
			returnVal = roundNumber((initVal * 0.3048)) + "m";
			break;
		case "2": // in to mm
			returnVal = roundNumber((initVal * 25.4)) + "mm";
			break;
		case "3": // mm to in
			returnVal = roundNumber((initVal * 0.3094)) + "in";
			break;
		case "4": // m to ft
			returnVal = roundNumber((initVal * 3.281)) + "ft";
			break;
		case "5": // sq m to sq ft
			returnVal = roundNumber((initVal * 10.7639)) + "sq.ft";
			break;
		case "6": // sq ft to sq m
			returnVal = roundNumber((initVal * 0.0929)) + "sq.m";
			break;
		case "7": // cu ft to cu m
			returnVal = roundNumber((initVal * 0.0283)) + "cu.m";
			break;
		case "8": // cu m to cu ft
			returnVal = roundNumber((initVal * 35.315)) + "cu.ft";
			break;
		case "9": // l to cu ft
			returnVal = roundNumber((initVal * 0.0353)) + "cu.ft";
			break;
		case "10": // cu ft to l
			returnVal = roundNumber((initVal * 28.317)) + "l";
			break;
		case "11": // l to us gal
			returnVal = roundNumber((initVal * 0.2642)) + " US Gal";
			break;
		case "12": // us gal to l
			returnVal = roundNumber((initVal * 3.785)) + "l";
			break;
		case "13": // l to UK Gal
			returnVal = roundNumber((initVal * 0.22)) + " UK Gal";
			break;
		case "14": // uk gal to l
			returnVal = roundNumber((initVal * 4.5461)) + "l";
			break;
		case "15": // uk gal to us gal
			returnVal = roundNumber((initVal * 1.2001)) + " US Gal";
			break;
		case "16": // us gal to uk gal
			returnVal = roundNumber((initVal * 0.8327)) + " UK Gal";
			break;
		case "17": // kg to lb
			returnVal = roundNumber((initVal * 2.2046)) + "lb";
			break;
		case "18": // lb to kg
			returnVal = roundNumber((initVal * 0.4536)) + "kg";
			break;
		case "19": // long tonnes to tonnes
			returnVal = roundNumber((initVal * 1.01605)) + " Tonnes(2204.62 lb)";
			break;
		case "20": // tonnes to long tonnes
			returnVal = roundNumber((initVal * 0.9842)) + " Long Tons(1016.05 kg)";
			break;
		case "21": // bar to psi
			returnVal = roundNumber((initVal * 14.504)) + " PSI";
			break;
		case "22": // psi to bar
			returnVal = roundNumber((initVal * 0.06895)) + " Bar";
			break;
		case "23": // inches HG to psi
			returnVal = roundNumber((initVal * 0.4912)) + " PSI";
			break;
		case "24": // psi to inches HG
			returnVal = roundNumber((initVal * 2.036)) + " Inches HG";
			break;
		case "25": // kg/sq.cm to psi
			returnVal = roundNumber((initVal * 14.223)) + " PSI";
			break;
		case "26": // psi to kg/sq.cm
			returnVal = roundNumber((initVal * 0.0703)) + " kg/sq.cm";
			break;
		case "27": // kg/sq.cm to bar
			returnVal = roundNumber((initVal * 0.9807)) + " Bar";
			break;
		case "28": // bar to kg/sq.cm
			returnVal = roundNumber((initVal * 1.02)) + " kg/sq.cm";
			break;
		case "29": // kg/sq.cm to inches HG
			returnVal = roundNumber((initVal * 28.976)) + " Inches HG";
			break;
		case "30": // inches HG to kg/sq.cm
			returnVal = roundNumber((initVal * 0.0345)) + " kg/sq.cm";
			break;
		case "31": // deg f to deg c
			returnVal = roundNumber((5/9*(initVal - 32))) + " Deg C";
			break;
		case "32": // in to mm
			returnVal = roundNumber(((initVal * 9/5) + 32)) + " Deg F";
			break;
		
		default: returnVal = "unknown";		
		}
	}
	x.outputBox.value = returnVal;
}

function doVolCalc() {
	var returnVal;
	var x = getObj('volumeCalc');
	var width = x.width.value;
	var height = x.height.value;
	var depth = x.depth.value;
	var theunit = x.theunit.value;
	returnVal = width*height*depth ;
	returnVal = roundNumber(returnVal);
	returnVal += theunit;	
	
	x.outputBox.value = returnVal;
}

function roundNumber(rnum) {
	var rlength = 2; // The number of decimal places to round to
	if (rnum > 8191 && rnum < 10485) {
		rnum = rnum-5000;
		var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
		newnumber = newnumber+5000;
	} else {
		var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
	}
	return newnumber;
	/* In case you're wondering about the part of the function which subtracts and then adds 5000, 
	it's a workaround for a known JavaScript bug (JavaScript can't reliably round numbers between 8192 and 10484). */
}

var keypresscount = 0; // used in the function below
function chkDigits(theVal){
	keypresscount += 1;
	if ((theVal == 8) && (keypresscount > 8)){
		alert ("Your password can be no longer than 8 characters.\n\nPlease ensure your password is no more than 8 characters.\n\nThank You.");
	}
}

function chkLoginForm(frmName){
	var x = getObj(frmName);
	var errors = false;
	var message = "Sorry, you have not filled all of the form fields incorrectly.\n";
	
	if(x.username.value == "") {
		message += "You need to enter your username\n";
		errors = true;
	}
	if(x.password.value == "") {
		message += "You need to enter your password\n";
		errors = true;
	}
	if (errors) {
		message += "Please fill out all fields and click on the submit button again.";
		errors = false;
		alert(message);
		return false;
	}
	else return true;
}

function delChk(){
	if(!confirm("Are you sure you want to delete this port from your list?")) return false;
}

function closeWindow() {
	window.open('','_parent','');
	window.close();
}
//javascript:window.open(’javascript:window.close();’,'_self’,'’);
function updnClose(){
	top.opener.location.reload(true);
	closeWindow();
}
function chkNewReg(frmName) {
	var x = getObj(frmName);
	var errors = false;
	var message = "Sorry, you have filled out one or more of the form fields incorrectly.\n";
	
	if((!validChar(x.first_name.value)) || (x.first_name.value=="")) {
		message += "You must enter a your first name.\nThis field can only contain the characters Aa-Zz and 0-9\n\n";
		errors = true;
	}
	if((!validChar(x.last_name.value)) || (x.last_name.value=="")) {
		message += "You must enter a your last name.\nThis field can only contain the characters Aa-Zz and 0-9\n\n";
		errors = true;
	}
	if(!chkEmail(x.email_address.value)){
		message += "You have entered an invalid email address\n";
		errors = true;
	}
	if(!validNum(x.tel.value)) {
		message += "The telephone field must be a number\n";
		errors = true;
	}
	if((!validChar(x.company.value)) || (x.company.value=="")) {
		message += "You must enter your company name. This field can only contain the characters Aa-Zz and 0-9\n\n";
		errors = true;
	}
	if(x.branch.value!=""){
		if(!validChar(x.branch.value)) {
			message += "The town field can only contain the characters Aa-Zz and 0-9\n\n";
			errors = true;
		}
	}
	if (errors) {
		message += "Please change these details and click on the submit button again.";
		errors = false;
		alert(message);
		return false;
	}
	else return true;
}

function selChk(){
	var x = getObj('portSelFrm');
	if(x.portID.value=="X") {
		alert("Please select a port");
		return false;
	}
	else return true;
}

function checknewsletter(){
	var x = getObj('unsubscribe');
	if(x.name.value=="") {
		alert("Please enter your name");
		return false;
	}
	if(x.email.value=="") {
		alert("Please enter your email address");
		return false;
	}
	else return true;
}

function checkcompreg(){
	var x = getObj('competition');
	var errors = false;
	var message = "Sorry, you have filled out one or more of the form fields incorrectly.\n";
	
	if(!validChar(x.name.value)) {
		message += "The name field can only contain the characters Aa-Zz and 0-9\n";
		errors = true;
	}
	if(!validNum(x.tel.value)) {
		message += "The telephone field must be a number\n";
		errors = true;
	}
	if(!chkEmail(x.email.value)){
		message += "You have entered an invalid email address\n";
		errors = true;
	}
	if(!validChar(x.company.value)) {
		message += "The name field can only contain the characters Aa-Zz and 0-9\n";
		errors = true;
	}
	if(x.name.value=="") {
		message += "Please enter your Name\n";
		errors = true;
	}
	if(x.email.value=="") {
		message += "Please enter your email address\n";
		errors = true;
	}
	if(x.tel.value=="") {
		message += "Please enter your telephone number\n";
		errors = true;
	}
	if(x.company.value=="") {
		message += "Please enter your Company Name\n";
		errors = true;
	}
	if (errors) {
		message += "Please change these details and click on the register button again.";
		errors = false;
		alert(message);
		return false;
	}
	else return true;
}

function popup(url) {
 params  = 'width='+screen.width;
 params += ', height='+screen.height;
 //params += ', height=500';
 params += ', top=0, left=0';
 params += ', fullscreen=yes';

 newwin=window.open(url,'windowname4', params);
 if (window.focus) {newwin.focus()}
 return false;
}