// Converters (TOOLS)

function rnd(pnum,pdec){
	power=Math.pow(10,pdec);
	return Math.round(pnum*power)/power;
}

function tempConvert(inform) 
{
	// variables taken from the input form
	var fahrenheit = parseFloat(inform.fahrenheit.value);
	var celsius = parseFloat(inform.celsius.value);

	if(inform.celsius.value == "" && inform.fahrenheit.value == "")
		{
			alert("Please fill out the unit from which you wish to convert.");
		}
	else
		{
		//F-conversion
		if (inform.celsius.value == "") 
			{
				celsius = (fahrenheit-32)*(5/9);
				inform.celsius.value = rnd(celsius,2);
			} 
		else 
			{
			//C-conversion
			if (inform.fahrenheit.value == "") 
				{
					inform.fahrenheit.value = rnd(celsius*(9/5) + 32,2);
				}
			}
		}
}
function tempReset(inform) 
{
	inform.fahrenheit.value = "";
	inform.celsius.value = "";
}

function distConvert(inform) 
{
	// variables taken from the input form
	var meters = parseFloat(inform.meters.value);
	var feet = parseFloat(inform.feet.value);
	var centimeters = parseFloat(inform.centimeters.value);
	var inches = parseFloat(inform.inches.value);
	var yards = parseFloat(inform.yards.value);
	
	if(inform.meters.value == "" && inform.feet.value == "" && inform.centimeters.value == "" && inform.inches.value == "" && inform.yards.value == "")
		{
			alert("Please fill out the unit from which you wish to convert.");
		}
	else
		//F-conversion
		if (inform.meters.value == "" && inform.feet.value != "") 
			{
				meters = feet*0.3048;
				inform.meters.value = rnd(meters,3);
				centimeters = feet*30.48;
				inform.centimeters.value = rnd(centimeters,3);
				inches = feet*12;
				inform.inches.value = rnd(inches,3);
				yards = feet/3;
				inform.yards.value = rnd(yards,3);
			} 
		else 
			{
		//M-conversion
		if (inform.feet.value == "" && inform.meters.value != "") 
			{
				inform.feet.value = rnd(meters/0.3048,3);
				centimeters = meters*100;
				inform.centimeters.value = rnd(centimeters,3);
				inches = centimeters/2.54;
				inform.inches.value = rnd(inches,3);
				yards = meters/0.9144;
				inform.yards.value = rnd(yards,3);
			}
}
	
	//IN-conversion
	if (inform.centimeters.value == "" && inform.inches.value != "") {
		centimeters = inches*2.54;
		inform.centimeters.value = rnd(centimeters,3);
		meters = centimeters/100;
		inform.meters.value = rnd(meters,3);
		feet = meters/0.3048;
		inform.feet.value = rnd(feet,3);
		yards = meters/0.9144;
		inform.yards.value = rnd(yards,3);
	}
	else {
	//CM-conversion
		if (inform.inches.value == "" && inform.centimeters.value != "") {
			inform.inches.value = rnd(centimeters/2.54,3);
			meters = centimeters/100;
			inform.meters.value = rnd(meters,3);
			inform.feet.value = rnd(meters/0.3048,3);
			yards = meters/0.9144;
			inform.yards.value = rnd(yards,3);
		}
	}
	//YD-conversion
	if (inform.yards.value != "") {
		meters = yards*0.9144;
		centimeters = meters*100;
		feet = yards*3;
		inches = feet*12;
		inform.centimeters.value = rnd(centimeters,3);
		inform.inches.value = rnd(inches,3);
		inform.meters.value = rnd(meters,3);
		inform.feet.value = rnd(feet,3);
	}
}

function distReset(inform) {
	inform.meters.value = "";
	inform.feet.value = "";
	inform.centimeters.value = "";
	inform.inches.value = "";
	inform.yards.value = "";
}

function weightConvert(inform) {
	// variables taken from the input form
	var kgs = parseFloat(inform.kgs.value);
	var pds = parseFloat(inform.pds.value);

	if (inform.kgs.value == "" && inform.pds.value == "")
	{
		alert("Please fill out the unit from which you wish to convert.");		
	}	
	else
		//K-conversion
		if (inform.pds.value == "") 
		{
			pds = kgs*2.205;
			inform.pds.value = pds;
		} 
		else 
		{
		//P-conversion
		if (inform.kgs.value == "") 
		{
			inform.kgs.value = pds*0.4536
		}
	}
}

function weightReset(inform) {
	inform.kgs.value = "";
	inform.pds.value = "";
}

function velConvert(inform) {
	// variables taken from the input form
	var kmh = parseFloat(inform.kmh.value);
	var mph = parseFloat(inform.mph.value);
	var knots = parseFloat(inform.knots.value);
	var ms = parseFloat(inform.ms.value);
	
	//KMH-conversion
	if (inform.kmh.value != "" && inform.mph.value == "" && inform.knots.value == "" && inform.ms.value == "") {
		mph = kmh/1.609344; //1.6093474712522022
		inform.mph.value = mph;
		knots = kmh/1.852; //1.8519996918400246
		inform.knots.value = knots;
		ms = kmh/3.6; //0.2777778
		inform.ms.value = ms;
	}
	//MPH-conversion
	else if (inform.kmh.value == "" && inform.mph.value != "" && inform.knots.value == "" && inform.ms.value == "") {
		kmh = mph*1.609344;
		inform.kmh.value = kmh;
		knots = kmh/1.852;
		inform.knots.value = knots;
		ms = kmh/3.6;
		inform.ms.value = ms;
	}
	//KN-conversion
	else if (inform.kmh.value == "" && inform.mph.value == "" && inform.knots.value != "" && inform.ms.value == "") {
		kmh = knots*1.852;
		inform.kmh.value = kmh;
		mph = kmh/1.609344;
		inform.mph.value = mph;
		ms = kmh/3.6;
		inform.ms.value = ms;
	}
	//MS-conversion
	else if (inform.kmh.value == "" && inform.mph.value == "" && inform.knots.value == "" && inform.ms.value != "") {
		kmh = ms*3.6;
		inform.kmh.value = kmh;
		knots = kmh/1.852;
		inform.knots.value = knots;
		mph = kmh/1.609344;
		inform.mph.value = mph;
	}
	else {alert("Please fill out the unit from which you wish to convert.");}
}

function velReset(inform) {
	inform.kmh.value = "";
	inform.mph.value = "";
	inform.knots.value = "";
	inform.ms.value = "";
}

// END

//HOME PAGE VALIDATION TO REDIRECT TO HBL or CONTAINER ENQUIRY
	function go_query()
	{
		if (document.mainFrm.documentTxt.value != "")
		{
			if (document.mainFrm.documentList.value == 'bl_no')
			{
				window.location.href = 'fsltrack/hbldisplay.aspx?hbl_no='+document.mainFrm.documentTxt.value;
			}
			else if(document.mainFrm.documentList.value == 'awb_no')
			{
				window.location.href = 'fsltrack/mawb_display.aspx?mawb_no='+document.mainFrm.documentTxt.value;
			}
			else if(document.mainFrm.documentList.value == 'container_no')
			{
				window.location.href = 'fsltrack/Container/home_container_main.aspx?container_no='+document.mainFrm.documentTxt.value;
			}
		}
	}
	function ChkEnter()
	{
		if (document.mainFrm.documentTxt.value != "")
		{
			if(event.keyCode==13)
			{
				go_query();
			}
		}
	}
//END HOME PAGE VALIDATION


// ** Preload Function **

function preload() 
{
	if (!document.images) return;
	var ar = new Array();
	var arguments = preload.arguments;
	for (var i = 0; i < arguments.length; i++) 
	{
		ar[i] = new Image();
		ar[i].src = arguments[i];
	}
}

// ** End Preload **

/*
// ** lcl_sailing.aspx and exp_ebooking bring port name on top of list while typing **
	function filtery(pattern, list){
	
	if (!list.bak){
		list.bak = new Array();
		for (n=0;n<list.length;n++){
		list.bak[list.bak.length] = new Array(list[n].value, list[n].text);
		}
	}

	match = new Array();
	nomatch = new Array();
	for (n=0;n<list.bak.length;n++){
		if(list.bak[n][1].toLowerCase().indexOf(pattern.toLowerCase())!=-1){
		match[match.length] = new Array(list.bak[n][0], list.bak[n][1]);
		}else{
		nomatch[nomatch.length] = new Array(list.bak[n][0], list.bak[n][1]);
		}
	}

	for (n=0;n<match.length;n++){
		list[n].value = match[n][0];
		list[n].text = match[n][1];
	}
	for (n=0;n<nomatch.length;n++){
		list[n+match.length].value = nomatch[n][0];
		list[n+match.length].text = nomatch[n][1];
	}

	list.selectedIndex=-1;

	}
*/
	function receiptHandler()
	{
		document.lclFrm.receiptTxt.value = "";
		document.lclFrm.receiptTxt.value = document.lclFrm.originList.options[document.lclFrm.originList.selectedIndex].text;
	}
	function deliveryHandler()
	{
		document.lclFrm.deliveryTxt.value = "";
		document.lclFrm.deliveryTxt.value = document.lclFrm.destinationList.options[document.lclFrm.destinationList.selectedIndex].text;
	}
/*	function Load_Func()
	{
		document.lclFrm.receiptTxt.focus();
		document.lclFrm.originList.selectedIndex=-1;
		document.lclFrm.destinationList.selectedIndex=-1;
		document.lclFrm.receiptTxt.value='';
		document.lclFrm.deliveryTxt.value='';
	}*/

// End lcl_sailing.aspx **

// ** exp_ebooking.aspx selecting text from list and display in text box **

	function porHandler()
	{
		document.expeBookFrm.receiptTxt.value = "";
		document.expeBookFrm.receiptTxt.value = document.expeBookFrm.originList.options[document.expeBookFrm.originList.selectedIndex].text;
	}
	function fdcHandler()
	{
		document.expeBookFrm.deliveryTxt.value = "";
		document.expeBookFrm.deliveryTxt.value = document.expeBookFrm.destinationList.options[document.expeBookFrm.destinationList.selectedIndex].text;
	}
	
// End exp_ebooking.aspx **

// BookMark in Browser

		function addBookMark(MarkPage)
		{
			browser_version= parseInt(navigator.appVersion);
			browser_type = navigator.appName;

			if (browser_type == "Microsoft Internet Explorer" && (browser_version >= 4))
			{
				var url = MarkPage;
				var title = "FSL Sailing Schedule";
				window.external.AddFavorite(url,title);
			}
			else if (browser_type == "Netscape" && (browser_version >= 4))
			{
				popUp = open("", "displayWindow", "width=220,height=100,status=no,toolbar=no,menubar=no");
				popUp.document.open();
				popUp.document.write("<html><body><LINK href=http://www.freightsystems.com/freightsystems/FSL.css type=text/css rel=stylesheet>"
				+"<p align='center'>To Bookmark the new page<br>"
				+"please press CTRL+D<br>"
				+"after reaching the new address.<br><br>");
				popUp.document.write("<a href='javascript:self.close()'>");
				popUp.document.write("<b>Close</b></a>");
				popUp.document.write("</body></html>");
				popUp.document.close() ;
			}
		}
// End BookMark


// ************ General Checking *************

function isEmpty(str)
{
	if(str == "") 
	{
		return true;
	}
	return false;
}

function CheckForSpace(str) 
{
	var flag="no";
	loop:for(i=0;i<str.length;i++) {
		if(str.charAt(i) ==" ") {
			flag="yes";
		} else {
			flag="no";
			break loop;
		}
	}
	if(flag=="yes") {
		return true;
	} else {
		return false;
	}
}

function isAlphanumeric(str)
{
	var flag = "yes";
	var counter = 0;
	for(j=0;j<str.length;j++) {
		if(str.charAt(j) == ' ' || str.charAt(j) == '.') {
			counter++;
		}
	}
	if(counter>5) {
		return true;
	}
	for(i=0;i<str.length;i++) {
		if(!((str.charAt(i)>='A'&& str.charAt(i)<='Z')||(str.charAt(i)>='a'&& str.charAt(i)<='z') || str.charAt(i) =='.' || str.charAt(i)<=' ')) {
			flag = "no";		
		}
	}
	if(flag == "no") {
		return true;	
	}
	return false;
}

function NotEmail(str)
{
	var i = 1;
    var length = str.length;
	if(isEmpty(str))
	{
		return true;
	}

    // look for @
    while ((i < length) && (str.charAt(i) != "@"))
    { 
		i++;
    }
	if ((i >= length) || (str.charAt(i) != "@"))
	{
	
	return false;
	}	
    else i += 2;
	// look for .
    while ((i < length) && (str.charAt(i) != "."))
    {
		i++;
	}

    // there must be at least one character after the .
    if ((i >= length - 1) || (str.charAt(i) != ".")) 
	{
		return false;
	}
    else return true;
}

function ProperEmail(str)
{
	var flag="yes";
	var counter=0;
	var counter1=0;
	for(i=0;i<str.length;i++)
	{
	    if(str.charAt(i) == " ")
	    {
	      return true;
	    }
		if(str.charAt(i) == "@")
		{
		counter++;
		}		
	}
	if(counter>1)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function checkspecialcharinemail(str)
{
	var flag = "yes";
	var counter = 0;
	var x="'";
	var y="\/";
	var z="\\";
	for(j=0;j<str.length;j++)
	{
		if( str.charAt(j) == ',' || str.charAt(j) == '|' || str.charAt(j) == '"' || str.charAt(j) == '(' || str.charAt(j) == ')' || str.charAt(j) == x || str.charAt(j) == y || str.charAt(j) == z || str.charAt(j) == '>' || str.charAt(j) == '~' || str.charAt(j) == '`' || str.charAt(j) == '[' || str.charAt(j) == ']' || str.charAt(j) == '{' || str.charAt(j) == '}' || str.charAt(j) == ':' || str.charAt(j) == ';' || str.charAt(j) == '=' || str.charAt(j) == '+' || str.charAt(j) == '#' || str.charAt(j) == '?' || str.charAt(j) == '!' || str.charAt(j) == '$' || str.charAt(j) == '%' || str.charAt(j) == '^' || str.charAt(j) == '&' || str.charAt(j) == '*' || str.charAt(j) == '<')
		{
			return true;
		}
	}
	return  false;
}


// ************ End General Checking *************

//************** MAIN FUNCTIONS ****************

//Opens new window(s) from interem.aspx

function OpenWin1()
{
	window.open('../html/someone.aspx','interem1','toolbar=no,status=no,scrollbars=1,location=no,menubar=no,directories=no,width=540,height=400,top=100,left=120')
}
function OpenWin2()
{
	window.open('../html/professionals.aspx','interem2','toolbar=no,status=no,scrollbars=1,location=no,menubar=no,directories=no,width=540,height=400,top=100,left=120')
}
function OpenWin3()
{
	window.open('../html/simpler.aspx','interem3','toolbar=no,status=no,scrollbars=1,location=no,menubar=no,directories=no,width=540,height=400,top=100,left=120')
}
function OpenWin4()
{
	window.open('../html/personal.aspx','interem4','toolbar=no,status=no,scrollbars=1,location=no,menubar=no,directories=no,width=540,height=400,top=100,left=120')
}
function AboutWin1()
{
	window.open('../html/fslfacts.aspx','fsl1','toolbar=no,status=no,scrollbars=1,location=no,menubar=no,directories=no,width=540,height=400,top=100,left=120')
}
function AboutWin2()
{
	window.open('../html/inception.aspx','fsl2','toolbar=no,status=no,scrollbars=1,location=no,menubar=no,directories=no,width=540,height=400,top=100,left=120')
}
function AboutWin4()
{
	window.open('../html/careers.aspx','fsl4','toolbar=no,status=no,scrollbars=1,location=no,menubar=no,directories=no,width=540,height=400,top=100,left=120')
}

//Opens new window(s) from sailing_entry.aspx

function popUp(URL) 
{
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=no,scrollbars=1,location=no,statusbar=no,menubar=no,resizable=1,width=500,height=400,left=140,top=100');");
}

function CalpopUp(URL) 
{
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=no,status=no,scrollbars=1,location=no,menubar=no,directories=no,width=210,height=180,top=250,left=20');");
}

function OpenVesselMaster() 
{
	window.open('vessel_master.aspx','VeslMaster','toolbar=no,scrollbars=1,location=no,statusbar=no,menubar=no,resizable=1,width=500,height=400,left=140,top=100');
}

function OpenSailentryHelp() 
{
	window.open('sailentry_help.aspx','SailHelp','toolbar=no,scrollbars=1,location=no,statusbar=no,menubar=no,resizable=1,width=500,height=400,left=140,top=100');
}

function OpenContactHelp() 
{
	window.open('updatecontact_help.aspx','ContactHelp','toolbar=no,scrollbars=1,location=no,statusbar=no,menubar=no,resizable=1,width=500,height=400,left=140,top=100');
}

/*
function customerForm_Validate()
{
   if(isEmpty(document.customerForm.frm_Name.value))
	{
		alert("Name is required");
		document.customerForm.frm_Name.style.color = "red";
		document.customerForm.frm_Name.focus();
		return false;
	}
   if(isEmpty(document.customerForm.frm_Email.value))
	{
		alert("Email address is required");
		document.customerForm.frm_Email.style.color = "red";
		document.customerForm.frm_Email.focus();
		return false;
	}
	else if(!NotEmail(document.customerForm.frm_Email.value))
	{
		alert("Please enter a valid E-mail address of the form x@y.z");
		document.customerForm.frm_Email.style.color="red";
		document.customerForm.frm_Email.focus();
		return false;
	}
	else if(checkspecialcharinemail(document.customerForm.frm_Email.value))
	{
	    alert("No special characters are allowed in Email");
		document.customerForm.frm_Email.style.color="red";
		document.customerForm.frm_Email.focus();
		return false;
	}	
	else if(ProperEmail(document.customerForm.frm_Email.value))
	{
		alert("Please enter a valid E-mail address of the form x@y.z");
		document.customerForm.frm_Email.style.color="red";
		document.customerForm.frm_Email.focus();
		return false;
	}
   if(isEmpty(document.customerForm.frm_Suggestion.value))
	{
		alert("Please enter your suggestions/feedback");
		document.customerForm.frm_Suggestion.focus();
		return false;
	}	
	return true;
}

	function Open_BL_Win()
	{
		var URL
		URL = 'airexp_bl.aspx?'
		URL = URL +'&pol='+ document.AirexpeBookFrm.receiptTxt.value
		URL = URL +'&pod='+ document.AirexpeBookFrm.fdcTxt.value
		URL = URL +'&pckgs='+ document.AirexpeBookFrm.pkgsTxt.value
		URL = URL +'&weight='+ document.AirexpeBookFrm.grossweightTxt.value
		URL = URL +'&consignee='+ document.AirexpeBookFrm.consigneeTxt.value
		URL = URL +'&ppcc='+ document.AirexpeBookFrm.termsList.value
		URL = URL +'&dimensions='+ document.AirexpeBookFrm.dimensionsTxt.value
		URL = URL +'&description='+ document.AirexpeBookFrm.commodityTxt.value

		window.open(URL,'blinstruction','toolbar=no,status=no,scrollbars=1,location=no,menubar=no,resizable=yes,directories=no,width=580,height=400,top=60,left=100');
	}
*/
//************** END MAIN FUNCTIONS ****************