var itWin = null;
function popIt(service) {
	var width = 300;
	var height = 300;
	var filename = "/services/popup.it." + service + ".html";
	itWin = window.open(filename, "itWin", "width=" + width + ",height=" + height + ",toolbars=no,scrollbars=no,scrolling=no");
}


var mailWin = null;
function popMail(service) {
	var width	= 300;
	var height = 300;
	var filename = "/services/popup.mail." + service + ".html";
	mailWin = window.open(filename, "mailWin", "width=" + width + ",height=" + height + ",toolbars=no,scrollbars=no,scrolling=no");
}


var courierWin = null;
function popCourier(service) {
	var width = 300;
	var height = 300;
	var filename = "/services/popup.courier." + service + ".html";
	courierWin = window.open(filename, "courierWin", "width=" + width + ",height=" + height + ",toolbars=no,scrollbars=no,scrolling=no");
}


var transitWin = null;
function popStdTransitTime() {
	var width	= 300;
	var height = 400;
	var filename = "/tools/popup.std-transit-time.html";
	transitWin = window.open(filename, "transitWin", "width=" + width + ",height=" + height + ",toolbars=no,scrollbars=no,scrolling=no");
}

var ratesWin = null;
function popRates(zone) {
	var width	= 300;
	var height = 400;
	var filename = "/rates/popup.rates." + zone + ".html";
	ratesWin = window.open(filename, "ratesWin", "width=" + width + ",height=" + height + ",toolbars=no,scrollbars=no,scrolling=no");
}

var quickquoteWin = null;
function popQuickQuote() {
	var width	= 300;
	var height = 375;
	var filename = "/rates/popup.rates.quick-quote.html";
	quickquoteWin = window.open(filename, "quickquoteWin", "width=" + width + ",height=" + height + ",toolbars=no,scrollbars=no,scrolling=no");
}

var mapWin = null;
function popMap(timezone) {
	var tz = (timezone) ? true : false;
	var width	= 627;
	var height = 442;
	var filenameFlash = (tz) ? "/tools/popup.map.tz.html" :"/tools/popup.map.html";
	var filenamePlain = "/getflash.html";
	if (detectFlash()) {
		mapWin = window.open(filenameFlash, "mapWin", "width=" + width + ",height=" + height + ",toolbars=no,scrollbars=no,scrolling=no");
	} else {
		window.location.href = filenamePlain;
	}
}

function popMapTz() {
	var width	= 627;
	var height = 442;
	var filenameFlash = "/tools/popup.map.tz.html";
	var filenamePlain = "/getflash.html";
	if (detectFlash()) {
		mapWin = window.open(filenameFlash, "mapWin", "width=" + width + ",height=" + height + ",toolbars=no,scrollbars=no,scrolling=no");
	} else {
		window.location.href = filenamePlain;
	}
}

var CURRENT_UNITS_IMPERIAL = true;
var numDecimalPlaces = 2;
var roundingMultiplier = Math.pow(10,numDecimalPlaces);

// for dimensional weight calc
function calcWeight() {
	var f = document.forms["dimweight"];

	if (f.units[0].checked) {
		f.dest[0].value = 194;
		f.dest[1].value = 166;
	} else {
		f.dest[0].value = 7000;
		f.dest[1].value = 6000;
	}

	f.length.value = isNaN(parseFloat(f.length.value)) ? "" : parseFloat(f.length.value);
	f.width.value = isNaN(parseFloat(f.width.value)) ? "" : parseFloat(f.width.value);
	f.height.value = isNaN(parseFloat(f.height.value)) ? "" : parseFloat(f.height.value);

	if ( isNaN(parseFloat(f.length.value)) || isNaN(parseFloat(f.width.value)) || isNaN(parseFloat(f.height.value)) ) { f.weight.value = ""; return; }
	var temp = (parseFloat(f.length.value) * parseFloat(f.width.value) * parseFloat(f.height.value));
	temp = (f.dest[0].checked) ? temp/f.dest[0].value : temp/f.dest[1].value;
	f.weight.value = Math.round(temp *roundingMultiplier)/roundingMultiplier;
}

function calculate() {
	var f = document.quote;
	var rates = new Array(
		new Array(23,3.5,3.0,2.5),
		new Array(27,5.5,5.0,4.5),
		new Array(29,5.5,5.0,4.5),
		new Array(35,7.0,6.5,6.0),
		new Array(37,8.0,7.5,7.0),
		new Array(57,9.0,8.5,8.0),
		new Array(65,9.0,8.5,8.0)
		);

	var documentSurcharge = (f.document[1].checked) ? 25 : 0;
	var weight = (parseFloat(f.weight.value) <= 0.5) ? Math.floor(parseFloat(f.weight.value)) : Math.ceil(parseFloat(f.weight.value));
	var zone = f.country.options[f.country.selectedIndex].value;

	if (zone == "A") { 
		zone = 0;
	} else if (zone == "B") {
		zone = 1;
	} else if (zone == "C") {
		zone = 2;
	} else if (zone == "D") {
		zone = 3;
	} else if (zone == "E") {
		zone = 4;
	} else if (zone == "F") {
		zone = 5;
	} else if (zone == "G") {
		zone = 6;
	} else return false;

	var total = 0;

	if (weight < 0) {
		f.weight.value = 0;
		f.total.value = 0;
		return false;

	} else if (weight > 50) {
		total = ( (weight - 50) * rates[zone][3] ) + (25 * rates[zone][2]) + (25 * rates[zone][1]) + rates[zone][0];

	} else if (weight > 25) {
		total = ((weight - 25) * rates[zone][2]) + (25 * rates[zone][1]) + rates[zone][0];

	} else if (weight > 0) {
		total = (weight * rates[zone][1]) + rates[zone][0];

	} else if (weight == 0) { 
		total = rates[zone][0];

	}

	f.total.value = Math.round((total + documentSurcharge) * roundingMultiplier) / roundingMultiplier;

}


function switchUnits() {
	var conversionFactor = 2.54;
	var f = document.forms["dimweight"];

	if (f.units[0].checked && !CURRENT_UNITS_IMPERIAL) {
		f.length.value = isNaN(parseFloat(f.length.value)) ? "" : f.length.value / conversionFactor;
		f.width.value = isNaN(parseFloat(f.width.value)) ? "" : f.width.value / conversionFactor;
		f.height.value = isNaN(parseFloat(f.height.value)) ? "" : f.height.value / conversionFactor;
		CURRENT_UNITS_IMPERIAL = true;
	} else if (f.units[1].checked && CURRENT_UNITS_IMPERIAL) {
		f.length.value = isNaN(parseFloat(f.length.value)) ? "" : f.length.value * conversionFactor;
		f.width.value = isNaN(parseFloat(f.width.value)) ? "" : f.width.value * conversionFactor;
		f.height.value = isNaN(parseFloat(f.height.value)) ? "" : f.height.value * conversionFactor;
		CURRENT_UNITS_IMPERIAL = false;
	}
}


function closeAndGoto(url) {
	top.window.opener.location.href = url;
	top.window.close();
}


var addrBookWin = null;
function editAddressBook() {
	var width	= 300;
	var height = 400;
	var filename = "/tools/popup.address-book.html";
	addrBookWin = window.open(filename, "addrBookWin", "width=" + width + ",height=" + height + ",toolbars=no,scrollbars=no,scrolling=no");
}



// flash detection stuff
var detectableWithVB = false;
var pluginFound = false;

function detectFlash() {
	if (navigator.plugins){
		if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
			var isVersion2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
			var flashDescription = navigator.plugins["Shockwave Flash" + isVersion2].description;
			var flashVersion = parseInt(flashDescription.charAt(flashDescription.indexOf(".") - 1));
			pluginFound = flashVersion == 6;
			}
		}

	if (!pluginFound && detectableWithVB) pluginFound = detectActiveXControl('ShockwaveFlash.ShockwaveFlash.6');
	return pluginFound;
	}


// VBScript block for MSIE Windows
if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1)) {
	document.writeln('<script language="VBscript">');

	document.writeln('\'do a one-time test for a version of VBScript that can handle this code');
	document.writeln('detectableWithVB = False');
	document.writeln('If ScriptEngineMajorVersion >= 2 then');
	document.writeln('  detectableWithVB = True');
	document.writeln('End If');

	document.writeln('\'this next function will detect most plugins');
	document.writeln('Function detectActiveXControl(activeXControlName)');
	document.writeln('  on error resume next');
	document.writeln('  detectActiveXControl = False');
	document.writeln('  If detectableWithVB Then');
	document.writeln('     detectActiveXControl = IsObject(CreateObject(activeXControlName))');
	document.writeln('  End If');
	document.writeln('End Function');

	document.writeln('</scr' + 'ipt>');
}