/**
* write back to the document: using document.layers for ns and document.all for IE.
*	layerWrite(int id of the element to change, String nested reference to an element, String HTML to write to document)
*/
function layerWrite(id,nestref,html)
{
	ns4 = (document.layers)? true:false;
	ie4 = (document.all)? true:false;
	
	// alert("ie4 = " + ie4 +"  ns4 = " + ns4);
	
	if( ns4 )
	{
		// alert("This is Netscape: Use lyr = document.layers[" + id + "].document");
		if( nestref ) var lyr = eval('document.' + nestref + '.document.' + id + '.document')
		else var lyr = document.layers[id].document
		lyr.open()
		lyr.write(html)
		lyr.close()
	}
	else if( ie4 )
	{ 
		// alert("This is IE: Use document.all["+ id + "].innerHTML");
		document.all[id].innerHTML = html
	}
}


/**
* Called when hovering over links in the Navigation Bar.
*
*	Checks browser Type:
*		If Netscape then:
*			loop through the link array and turn off all links that
*			were on previously. Then turn on
*/
function linkOver(num)
{
	// If Netscape then go through all links in the array and call linkOut if link[i][3] is true.
	// Element 3 is the 'LinkOver' status element: indicates that the element is red already, supposedly.
	if( ns4 )
	{
		for ( var i = 0;i < link.length;i++ )
			{ if( link[i][3] == true ) linkOut(i) }
	}
	
	// Set the current link element 'LinkOver' status flag to true
	link[num][3] = true
	
	// Check for 'CurrentNavElement' by referencing link element 4.
	// If it's true then set the class to 'dredb'
	// Otherwise just hilight it red: 'dred'
    // layerWrite(object element ID, nested reference to element, HTML to write to layer) 
	if( link[num][4] == true )
		{ layerWrite(link[num][0],null,'<a class="dredb" href="' + link[num][1] + '" onMouseOut="linkOut(' + num + ')">' + link[num][2] + '</a>') }
	else
		{ layerWrite(link[num][0],null,'<a class="dred" href="' + link[num][1] + '" onMouseOut="linkOut(' + num + ')">' + link[num][2] + '</a>') }
}


function linkOut(num)
{
	link[num][3] = true
	if( link[num][4] == true )
		{ layerWrite(link[num][0],null,'<a class="dredb" href="' + link[num][1] + '" onMouseOver="linkOver(' + num + ')">' + link[num][2] + '</a>') }
	else
		{ layerWrite(link[num][0],null,'<a class="gray" href="' + link[num][1] + '" onMouseOver="linkOver(' + num + ')">' + link[num][2] + '</a>') }
}


function pageLoadSelectOn()
{
	/*
	This function fires onLoad of the window object and determines
	which navigation element to hilight based on the currently loaded 
	view in the database.
	*/
	var loc; 	    // complete URL of the loaded page as a String
	var viewLoaded;	// Notes View loaded in the webpage
	var idx0;	    // Initial Index of "nsf/"  Test this for -1 to see if URL is good
	var idx1;	    // Starting Index for Substring extraction
	var idx2;	    // Ending Index for Substring extraction
	var navArea;	// What Navigation area are we in based on the view that was loaded?
	var num;	    // Number for indexing array.
	
	// Get the Current View string out of the current URL
	loc = document.location.toString();

	idx0 = loc.indexOf("nsf/");
	idx1 = idx0 + 4;	
	idx2 = loc.indexOf("?");

	// If the '?' isn't found then indexOf() returns -1, 
	// change this to prevent an error with substring()
	if( idx2 == -1 )
		{ idx2 = loc.length; }
	viewLoaded = loc.substring(idx1,idx2); 
	viewLoaded = viewLoaded.toLowerCase();
	
	// alert("viewLoaded = " + viewLoaded);

	// Determine what Navigation element to hilight
	
	// alert("region");
	if( viewLoaded.indexOf("region") != -1 )
		{ num = 3; }

	// alert("products");
	else if( viewLoaded.indexOf("product") != -1 )
		{ num = 2; }

	// alert("opps");
	else if( viewLoaded == "webjobs_x" )
		{ num = 5; }

	// Let the function fail without warning if not applicable to the page.
	// alert("Neither 'region' nor 'product' was found in:\n" + viewLoaded);
	// alert("bailed!");
	else
		{ return false; }

	link[num][4] = true;
	layerWrite(link[num][0],null,'<a class="dredb" href="' + link[num][1] + '" onMouseOut="linkOut(' + num + ')">' + link[num][2] + '</a>');
}


function modifyAppletView()
{
	if( document.view != null )
		{ view.height = (document.body.clientHeight * .59) }
}


function popup(f)
{
	if( f == "dimcalc.html" )
		{
			a = "DIMCalc";
			b = "height=380,width=420,scrollbars=0,resizable=1";
		}
	else if( f == "tconvert.html" )
		{
			a = "TimeZone";
			//b = "height=200,width=462,scrollbars=0,resizable=1";
			b = "height=240,width=494,scrollbars=0,resizable=1";
		}
	else
		{
			a = "Currency";
			b = "height=268,width=468,scrollbars=0,resizable=1";
		}
	window.open(f,a,b)
}


function loadpg(a,b)
{
	ie4 = (document.all)? true:false;
	if( ie4 )
		{ window.location.href = a; }
	else
		{ window.location.href = b; }
}

// Validating input if it is a valid integer value.
function isInteger(value) {
  return (parseInt(value) == value);
}

// DIMCALC FUNCTIONS
var i_m=194;

// calculates the volumetric weight.  rounds up to nearest pound / half-kilo.
function calcwght(x)
{	
	if(isInteger(x.qty.value))
	{
		vol = x.qty.value * x.length.value * x.width.value * x.height.value;
		volwgt = vol / x.div.value;
		y = volwgt - parseInt( volwgt );
		if( y <= .5 && 0 < y && (i_m == 6000 || i_m == 7012) )
			{ rnd=.5; }
		else
			{ rnd=1; }
		if( y == 0 ) rnd = 0;
		frtwght = volwgt - y + rnd;
		if( vol != 0 && (isNaN( frtwght ) || frtwght < 1 ) ) frtwght = 1;
		x.answer.value = parseFloat( frtwght );
		return 1;
	}
	else
	{
		alert(x.qty.value + " is not Integer.");
	}
}


// selects the conversion factor for domestic and international shipments
function dom_int(i)
{
	form = i.form;
	if( form.units[0].checked )
		{ unit = "imp"; }
	else
		{ unit = "met"; }
	type = i.value;
// if domesitc
	if(type == "dom")
	{
		if(unit == "imp")
			{ i_m = 194; }
		else
			{ i_m=7012; }
	}
// if international
	else
	{
		if(unit == "imp")
			{ i_m = 166; }
		else
			{ i_m = 6000; }
	}
	form.div.value = i_m;
	calcwght( form );
}


// changes measurement units - metric or inchs/pounds
function in_cm(i)
{
	form = i.form;
	if(form.pkgtype[0].checked)
		{ type = "dom"; }
	else
		{ type = "int"; }
	unit = i.value;
	conv = 1;
	if(unit == "imp")
	{
		lenunt = "graphics/in.gif";
		wgtunt = "graphics/lb.gif";
		givunt = "graphics/inLb.gif";
		givalt = "Dimweight is given in pounds";
		if(type == "dom")
			{ i_m = 194; }
		else
			{ i_m = 166; }
	}
	else
	{
		lenunt = "graphics/cm.gif";
		wgtunt = "graphics/kg.gif";
		givunt = "graphics/inKg.gif";
		givalt = "Dimweight is given in kilograms";
		if(type == "dom")
			{ i_m = 7012; }
		else
			{ i_m = 6000; }
	}
	document.images["LENIM"].src="" + lenunt;
	document.images["HIGIM"].src="" + lenunt;
	document.images["WIDIM"].src="" + lenunt;
	document.images["VOLIM"].src="" + wgtunt;
	document.images["GIVIN"].src="" + givunt;
	document.images["GIVIN"].alt="" + givalt;
	form.div.value = i_m;
	calcwght( form );
}

