
function popup(URL,name,w,h,resize) {
	if (resize=="") {resize=="no";}
	window.open(URL,name,'width='+w+',height='+h+',scrollbars=yes,menubar=no,resizable='+resize);
}


var menuBase = '';
var menuItems = new Array();
var menuText  = new Array();
var menuURL  = new Array();
var menuLevel  = new Array();
var inPage = 0;
var headerPrefix = '<h2><a href="index.html">';
var headerSuffix = '</a></h2>';

function writeMenu() {
	document.write(headerPrefix + menuText[0] + headerSuffix);
	document.write('<ul>');
	for (i=0;i<menuItems.length;i++) {
		var menuIndex = menuItems[i];
		var indicator = 'arrow';
		var width = '11';
		var size = '';
		if (menuLevel[menuIndex]=='sub'){
			indicator = 'chevron';
			width = '8';
		}
		if (menuLevel[menuIndex]=='subsub'){
			indicator = 'chevron';
			width = '8';
			size = '_small';
		}
		if (menuIndex==inPage) {
			document.write('<li class="' + menuLevel[menuIndex] + ' submenu_in"><img src="../imgs/icn_'+indicator+'_brown_nav'+size+'.gif" style="width:'+width+'px;height:8px;" alt="-&gt;" />' + menuText[menuIndex] + '</li>');
		}
		else {
			if (menuURL[menuIndex].indexOf("EXTERNAL") == -1) {
				document.write('<li class="' + menuLevel[menuIndex] + '"><img src="../imgs/icn_'+indicator+'_blue_nav'+size+'.gif" style="width:'+width+'px;height:8px;" alt="-&gt;" /><a href="' + menuBase + menuURL[menuIndex] + '" class="small">' + menuText[menuIndex] + '</a></li>');
			}
			else {
				document.write('<li class="' + menuLevel[menuIndex] + '"><img src="../imgs/icn_'+indicator+'_blue_nav'+size+'.gif" style="width:'+width+'px;height:8px;" alt="-&gt;" /><a href="' + menuURL[menuIndex].replace("EXTERNAL", "") + '" class="small" target="_blank">' + menuText[menuIndex] + '</a></li>');
			}
		}
	} 
	document.write('</ul>');
}

var producttype = "";
var usps2006_flats = new Array("0.97","1.14","1.31","1.48","1.65","1.82","1.99","2.16","2.33","2.50","2.67","2.84","4.60","4.60","4.60");
var upsmi2006_flats = new Array("0.90","1.05","1.21","1.37","1.53","1.68","1.83","1.94","2.09","2.24","2.39","2.47","3.56","3.66","3.74");
var usps2007_flats = new Array("1.00","1.17","1.34","1.51","1.68","1.85","2.02","2.19","2.36","2.53","2.70","2.87","4.70","4.70","4.70");
var upsmi2007_flats = new Array("0.93","1.08","1.24","1.40","1.56","1.76","1.86","1.97","2.12","2.27","2.42","2.50","3.71","3.81","3.89");
var piece_flats = new Array();
var percent_flats = new Array();

var usps2006_parcels = new Array("1.30","1.47","1.64","1.81","1.98","2.15","2.32","2.49","2.66","2.83","3.00","3.17","4.60","4.60","4.60");
var upsmi2006_parcels = new Array("1.24","1.40","1.56","1.72","1.88","2.04","2.19","2.34","2.50","2.62","2.76","2.89","3.66","3.77","3.82");
var usps2007_parcels = new Array("1.34","1.51","1.68","1.85","2.02","2.19","2.36","2.53","2.70","2.87","3.04","3.21","4.70","4.70","4.70");
var upsmi2007_parcels = new Array("1.28","1.44","1.60","1.76","1.93","2.09","2.25","2.41","2.57","2.72","2.86","2.99","3.91","4.02", "4.07");
var piece_parcels = new Array();
var percent_parcels = new Array();

function populatefields(type) {
	switch (type) {
		case "flats":
			for (i=2;i<=16;i++) {
				piece_flats[i-2] = usps2007_flats[i-2] - usps2006_flats[i-2];
				percent_flats[i-2] = piece_flats[i-2]/usps2006_flats[i-2];

				document.getElementById("usps2006_"+i).innerHTML = "$"+roundit(parseFloat(usps2006_flats[i-2]));
				document.getElementById("upsmi2006_"+i).innerHTML = "$"+roundit(parseFloat(upsmi2006_flats[i-2]));
				document.getElementById("usps2007_"+i).innerHTML = "$"+usps2007_flats[i-2];
				document.getElementById("upsmi2007_"+i).innerHTML = "$"+roundit(upsmi2007_flats[i-2]);
				document.getElementById("piece_"+i).innerHTML = "$"+roundit(piece_flats[i-2]);
				document.getElementById("percent_"+i).innerHTML = roundit(percent_flats[i-2]*100,0)+"%";
			}
			producttype = "flats";
			break;
		case "parcels" :
			for (i=2;i<=16;i++) {
				piece_parcels[i-2] = usps2007_parcels[i-2] - usps2006_parcels[i-2];
				percent_parcels[i-2] = piece_parcels[i-2]/usps2006_parcels[i-2];

				document.getElementById("usps2006_"+i).innerHTML = "$"+roundit(parseFloat(usps2006_parcels[i-2]));
				document.getElementById("upsmi2006_"+i).innerHTML = "$"+roundit(parseFloat(upsmi2006_parcels[i-2]));
				document.getElementById("usps2007_"+i).innerHTML = "$"+usps2007_parcels[i-2];
				document.getElementById("upsmi2007_"+i).innerHTML = "$"+roundit(upsmi2007_parcels[i-2]);
				document.getElementById("piece_"+i).innerHTML = "$"+roundit(piece_parcels[i-2]);
				document.getElementById("percent_"+i).innerHTML = roundit(percent_parcels[i-2]*100,0)+"%";
			}
			producttype = "parcels";
			break;
	}
}

function calculate(type) {
	var valid=false;
	var usps2007cost_total = 0;
	var upsmi2007cost_total = 0;
	var savings_total = 0;
	switch (producttype) {
		case "flats":
			for (i=2;i<=16;i++) {
				var volume = document.getElementById("vol_"+i).value;
				if (!isNaN(volume)) {
					var usps2007cost = usps2007_flats[i-2]*volume;
					var upsmi2007cost = upsmi2007_flats[i-2]*volume;
					var savings = usps2007cost-upsmi2007cost;
					document.getElementById("resultvol_"+i).innerHTML = volume;
					document.getElementById("usps2007cost_"+i).innerHTML = "$"+roundit(usps2007cost);
					document.getElementById("upsmi2007cost_"+i).innerHTML = "$"+roundit(upsmi2007cost);
					document.getElementById("savings_"+i).innerHTML = "$"+roundit(savings);

					usps2007cost_total += parseFloat(usps2007cost);
					upsmi2007cost_total += parseFloat(upsmi2007cost);
					savings_total += parseFloat(savings);
					valid=true;
				}
				else {valid = notanumber();}
				if (!valid) {break;}
			}
			if (valid && usps2007cost_total == 0) {valid = novalues();}
			break;
		case "parcels" :
			for (i=2;i<=16;i++) {
				var volume = document.getElementById("vol_"+i).value;
				if (!isNaN(volume)) {
					var usps2007cost = usps2007_parcels[i-2]*volume;
					var upsmi2007cost = upsmi2007_parcels[i-2]*volume;
					var savings = usps2007cost-upsmi2007cost;
					document.getElementById("resultvol_"+i).innerHTML = volume;
					document.getElementById("usps2007cost_"+i).innerHTML = "$"+roundit(usps2007cost);
					document.getElementById("upsmi2007cost_"+i).innerHTML = "$"+roundit(upsmi2007cost);
					document.getElementById("savings_"+i).innerHTML = "$"+roundit(savings);

					usps2007cost_total += parseFloat(usps2007cost);
					upsmi2007cost_total += parseFloat(upsmi2007cost);
					savings_total += parseFloat(savings);
					valid=true;
				}
				else {valid = notanumber();}
				if (!valid) {break;}
			}
			if (valid && usps2007cost_total == 0) {valid = novalues();}
			break;
		default :
			document.getElementById("instructions").innerHTML = "<span class='error'>You must select a product type.</span>";
			document.getElementById("instructions").style["display"] = "block";
			document.getElementById("instructions").style["display"] = "block";
			document.getElementById("calculatorresults").style["display"] = "none";
			valid=false;
			break;
	}
	if (valid) {
		document.getElementById("instructions").style["display"] = "none";
		document.getElementById("calculatorresults").style["display"] = "block";

		document.getElementById("usps2007cost_total").innerHTML = "$"+roundit(usps2007cost_total);
		document.getElementById("upsmi2007cost_total").innerHTML ="$"+ roundit(upsmi2007cost_total);
		document.getElementById("savings_total").innerHTML = "$"+roundit(savings_total);

		document.getElementById("annual_usps").innerHTML = "$"+roundit(12*usps2007cost_total);
		document.getElementById("annual_upsmi").innerHTML ="$"+ roundit(12*upsmi2007cost_total);
		document.getElementById("annual_savings").innerHTML = "$"+roundit(12*savings_total);

		document.location = 'calculator.html#results';
	}
}

function novalues() {
	document.getElementById("instructions").innerHTML = "<span class='error'>Please enter at least one Monthly Volume value.</span>";
	document.getElementById("instructions").style["display"] = "block";
	document.getElementById("instructions").style["display"] = "block";
	document.getElementById("calculatorresults").style["display"] = "none";
	return false;
}
function notanumber() {
	document.getElementById("instructions").innerHTML = "<span class='error'>Please enter numbers only.</span>";
	document.getElementById("instructions").style["display"] = "block";
	document.getElementById("instructions").style["display"] = "block";
	document.getElementById("calculatorresults").style["display"] = "none";
	return false;
}
function roundit(invalue,decimals) {
	if (isNaN(invalue)) {return ""};
	var outvalue = (decimals!="")?(Number(invalue)).toFixed(2):(Number(invalue)).toFixed(decimals);
	var integer = outvalue.split(".")[0].toString();
	var remainder = (outvalue.split(".").length > 1)?"."+outvalue.split(".")[1].toString():"";
	if (decimals == 0 && integer == 0 && Math.abs(invalue) > .5) {integer = Math.round(invalue).toString();}

	if (integer.replace("-","").length > 3) {
		var sRegExp = new RegExp('(-?[0-9]+)([0-9]{3})');
		while (sRegExp.test(integer)) {
			integer = integer.replace(sRegExp,'$1,$2');
		}
	}
	outvalue = integer + remainder;
	return outvalue;
}

if (top.frames.length!=0)
{top.location=self.document.location;}