var Q,PL,PH,A,S,C,W,D;   // Global variable declaration
var notcomplete = "Please complete all of the information in the form above.";
var moreinfo    = "Your postage cannot be calculated without additional information. Please call us.";
var weightc     = "Please call us for rates on pieces 3.3oz. or greater.";

function solve(form)	{
    Q  =  form.Q.value;  // The Quantity of the mailing
    PL =  0;             // The postage low
    PH =  0;             // The postage high
    A  =  form.AV.value;  // Non-profit status
    S  =  form.SV.value;  // The size of the mailing
    C  =  form.CV.value;  // The class of the mailing
    W  =  form.WV.value;  // The weight of the mailing
    D  =  form.DV.value;  // Delivery area

	// Check for Postcard Exception
	if ((S=='S3') && (W!='W1')) {
		alert("Postcards must be 1 oz. or less.");
		W = 'W1';
		form.W[0].checked = true;
		form.W[1].checked = false;
		form.W[2].checked = false;
		form.W[3].checked = false;
		form.W.value = W;
		form.WV.value = W;
		}
	if ((Q=="null") || (S=="null") || (C=="null") || (W=="null") || (D=="null")) {// || (A=="null")
		form.message.value = notcomplete;
	} else if (C=='C3') {
		form.message.value = moreinfo;
	} else if (W=='W4') {
		form.message.value = moreinfo;
	} else if (Q <= 0) {
		form.message.value = notcomplete;
	} else if (Q > 0) {
		QuickPostit(form);
	} else {
		form.message.value = notcomplete;
	}
}

function QuickPostit(form)	{
	// Check for Quantity Exceptions
	if ((C=='C2') && (Q<500)) {
		alert("In order to get Presorted First Class rates, you must have at least 500 pieces.\r \rYour selection has been changed to First Class.");
		C = 'C1';
		form.C[0].checked = true;
		form.C[1].checked = false;
		form.C.value = C;
		form.CV.value = C;
		return;
		}
	if ((C=='C4') && (Q<200)) {
		alert("In order to get Standard (formerly 3rd class bulk) rates, you must have at least 200 pieces.\r \rYour selection has been changed to First Class.");
		C = "C1";
		form.C[0].checked = true;
		form.C[1].checked = false;
		form.C.value = C;
		form.CV.value = C;
		return;
		}
	// Calculate Acording to Mail Classification
	if (C=='C3') { // All Periodical require additional information
		form.message.value="Your postage cannot be calculated without additional information.  Please call us. 3";
		return;
	} else if ((A=='A1') && (C=='C4')) { // Non-Profits Rates Only Apply when Non-Profit is Checked and Classification is Standard
		if (Non_Profit(form)=="error") {return}
	} else if (C=='C4') {
		if (Profit(form) == "error") {return}
	} else if (C=='C2') {
		if (Presorted(form)=="error") {return}
	} else if (C=='C1') {
		if (First(form)=="error") {return}
	} else {
		form.message.value="Mail classification error.";
		return;
	}
	form.PL.value=PL;
	form.PH.value=PH;
	windowOpen(form);
}

/* First Class Formulas */
function First(form)	{
	if (S=='S1') {
		if (W=='W1') {
			PL='.37';
			PH='.37';
		} else if (W=='W2') {
			PL='.60';
			PH='.60';
		} else if ((W=='W3') || (W=='W4')) {
			PL='0.0';
			PH='0.0';
			form.message.value=weightc;
			return "error";
		} else {
			alert("Mail weight error.");
		}	
	} else if (S=='S2') {
		if (W=='W1') {
			PL='.49';
			PH='.49';
		} else if (W=='W2') {
			PL='.60';
			PH='.60';
		} else if ((W=='W3') || (W=='W4')) {
			PL='0.0';
			PH='0.0';
			form.message.value=weightc;
			return "error";
		} else {
			alert("Mail weight error.");
		}
	} else if (S=='S3') {
		PL='.23';
		PH='.23';
	} else {
		form.message.value="Size of Piece Error.";
		return "error";
	}
}	

/* Presorted First Class Formulas */
function Presorted(form)	{
	if (S=='S1') {
		if (W=='W1') {
			PL='.278';
			PH='.309';
		} else if (W=='W2') {
			PL='.508';
			PH='.539';
		} else if ((W=='W3') || (W=='W4')) {
			PL='0';
			PH='0';
			form.message.value=weightc;
			return "error";
		} else {
			form.message.value="Mail weight error.";
			return "error";
		}	
	} else if (S=='S2') {
		if (W=='W1') {
			PL='.357';
			PH='.396';
		} else if (W=='W2') {
			PL='.527';
			PH='.566';
		} else if ((W=='W3') || (W=='W4')) {
			PL='0';
			PH='0';
			form.message.value=weightc;
			return;
		} else {
			alert("Mail weight error.");
			return "error";
		}
	} else if (S=='S3') {
		PL='.176';
		PH='.194';
	} else {
		form.message.value="Size of Piece Error.";
		return "error";
	}
}	

/* For Profit Standard Formulas - A2 is checked */
function Profit(form)	{
	if ((S=='S1') || (S=='S3')) {
		if (D=='D1') {
			PL='.145';
			PH='.186';
		} else if (D=='D2') {
			PL='.190';
			PH='.219';
		} else {
			form.message.value="Distribution area error.";
			return "error";
		}
	} else if (S=='S2') {
		if (D=='D1') {
			PL='.168';
			PH='.274';
		} else if (D=='D2') {
			PL='.261';
			PH='.300';
		} else {
			form.message.value="Distribution area error.";
			return "error";
		}
	} else {
		form.message.value="Size of Piece error.";
		return "error";
	}
}	

/* Non-Profit Standard Formulas - A1 is checked */
function Non_Profit(form)	{
	if ((S=='S1') || (S=='S3')) {
		if (D=='D1') {
			PL='.085';
			PH='.110';
		} else if (D=='D2') {
			PL='.114';
			PH='.144';
		} else {
			form.message.value="Distribution area error.";
			return "error";
		}
	} else if (S=='S2') {
		if (D=='D1') {
			PL='.100';
			PH='.163';
		} else if (D=='D2') {
			PL='.166';
			PH='.189';
		} else {
			form.message.value="Distribution area error.";
			return "error";
		}
	} else {
		form.message.value="Size of Piece error.";
		return "error";
	}
}	

function windowOpen(form)	{
	i = 1;
	L = dollarscents(Q*PL);
	H = dollarscents(Q*PH);
	if (L >= 1000000) { L = addcomma2(L); }
	if (H >= 1000000) { H = addcomma2(H); }
	if (L >= 1000) { L = addcomma1(L); }
	if (H >= 1000) { H = addcomma1(H); }
	
	if (L==H) {
		form.message.value = "Your estimated postage for this mailing is $" + L + ".";
	} else {
		form.message.value = "Your estimated postage for this mailing should fall between $" + L + " and $" + H + ".";
	}
}

function addcomma2(n) {
	n = n.toString();
	l = n.length;
	return (n.substring(0,l-9) + "," + n.substring(l-9,l-6) + "," + n.substring(l-6,l));
}

function addcomma1(n) {
	n = n.toString();
	l = n.length;
	return (n.substring(0,l-6) + "," + n.substring(l-6,l));
}

function dollarscents(money) {
	dollars = parseInt(money);
	cents = parseInt((money-dollars)*100);
	if (cents < 10) { cents = "0" + cents }
	return dollars + "." + cents;
}

function changeNP(form) {
	if (form.AV.value=='A1') {
			C='C5';		
			form.C[0].checked = false;
			form.C[1].checked = false;
			form.C[2].checked = false;
			form.C[3].checked = false;
			form.C[4].checked = true;
			form.C.value = C;
			form.CV.value = C;
	}
}

function checkNP(form) {
	if (form.C[4].checked) {
		alert("In order to get Non-Profit rates, you must be \rauthorized by the U.S.P.S. to mail at Non-Profit Rates.");
	}
}

function checkNP0(form) {
	if ((form.C[4].checked) && (form.A[1].checked)) {
		alert("In order to get Non-Profit rates, you must be authorized by the U.S.P.S. to mail at Non-Profit Rates.\r \rIf you are authorzied, please check your answer to the Non-Profit authorization question above.");
		C='C1';		
		form.C[0].checked = true;
		form.C[1].checked = false;
		form.C[2].checked = false;
		form.C[3].checked = false;
		form.C[4].checked = false;
		form.C.value = C;
		form.CV.value = C;
		A='A2';		
		form.A[0].checked = false;
		form.A[1].checked = true;
		form.A.value = A;
		form.AV.value = A;
	}
	if ((form.A[0].checked) && (form.C[4].checked==false)) {
		alert("You are authorized to mail at Non-Profit Rates.  Your Mail Classification should be Non-Profit.");
			C='C5';		
			form.C[0].checked = false;
			form.C[1].checked = false;
			form.C[2].checked = false;
			form.C[3].checked = false;
			form.C[4].checked = true;
			form.C.value = C;
			form.CV.value = C;
	}
}
