//* Written By TB McCoy, Copyright 1996, Highland Marketing. Maintained by Matthew Ward - 1999-2007. setzer@hiland.com
//* script determines NDG Postage Cost

// define variables and initialize

var endFlag=0;				//* flag for exit if boundary conditions exceeded*//
var goof=0;						//* flag to see if invalid input
var wtEnv=0;					//* envelope weight*//
var size=''	;				//* envelope size class*//
var num=0;					//* number of pieces inputed by user*//
var wtPiece=0;				//* weight of each piece to be mailed*//
var wt811=5.5;				//* weight of 8 1/2 by 11 bond paper*//
var wt1117=11;				//* weight of 11 by 17 bond paper*//
var wtbrc=5.5	;			//* weight of business reply card*//
var wtreply9=5.5;		//* weight of #9 reply envelope*//
var postage=0;				//* bulk postage cost*//
var postageStr				//*convert postage amnt to string*//
var fcp=0;					//* first class postage rate per piece*//
var fcpTotal=0;			//* total first class postage cost for mailing*//
var savings=0;				//* difference between first class and NDG*//
var msg0='';					//* output message*//
var msg1=''	;				//* output message*//
var msg2=''	;				//* output message*//
var msg3=''	;				//* output message*//
var msg4=''	;				//* output message*//
var num811=0;				//* number of 8 1/2 by 11 per piece*//
var num1117=0;				//* number of 11 by 17 per piece*//
var numbrc=0;				//* number of brc per piece*//
var numreply9=0;			//* number of #9 envelopes per piece*//
var envTypeIdx=0;		//* index of envelope selection dropdown list*//
var dollarStr				//*conversion of num to char with correct decimal places*//

var admailSL = 0.48;
var admailSLinc = 0.0023;
var admailOS = 0.63;
var admailOSinc = 0.0028;

var lmail30 = 0.61;
var lmail50 = 1.05;
var lmail100 = 1.29;
var lmail200 = 2.10;
var lmail300 = 2.95;
var lmail400 = 3.40;
var lmail500 = 3.65;

//**  SUBROUTINES  **//

function mainLine(number,Env,b8x11,b11x17,br,reply){
endFlag=0;	
goof=0;

//examine form data

num=document.ghostMail.numPieces.value;
num811=document.ghostMail.bond8x11.value;
num1117=document.ghostMail.bond11x17.value;
numbrc=document.ghostMail.brc.value;
numreply9=document.ghostMail.reply9.value;
envTypeIdx=document.ghostMail.Envelope.selectedIndex;

//* stop some potential silliness*//

if (num811==""){
		num811=0}
		
if (num1117==""){
		num1117=0}

if (numbrc==""){
		numbrc=0}

if (numreply9==""){
		numreply9=0}

if((num=="")||(num<1)||(num811<0)||(num1117<0)||(numbrc<0)||(numreply9<0)){
		alert("Invalid Input - try again")
		endFlag=1;
		goof=1;}

//determine size class and envelope weight
if(endFlag==0){
	sizeCat()}

//determine total weight of mailing piece
if(endFlag==0){
	wtPerPiece(num811,num1117,numbrc,numreply9)}

//	determine total cost of NDG Mailing
if(endFlag==0){
		totalCost(num)}


//determine total cost of first class mailing
if (endFlag==0){
		cf1stClass()}

//Massage Collected Data

if (endFlag==0){
	savings=fcpTotal-postage
	savings=Math.round(100*savings)/100
	dollarDeci(savings)
	savingsStr=dollarStr
	
        if (savings<=0){
	msg0='<h2>In This Case, Choose Lettermail</h2>';
        }
	if (savings>0){
	msg0='<h2>In This Case, Choose Addressed Admail</h2>';
        }
		
	msg1="<p>Based on a mailing of " + num + " pieces, each with an estimated  weight of " + wtPiece + " grams, the postage cost on an NDG, Addressed Admail mailing would be $" + postageStr + "</p>"

	msg2="<p>Using Lettermail, postage costs would be $" + fcpTotalStr + ".</p>" 
	if (savings<=0){
			
	msg3="<p>In this case, it is better to send the mail as Lettermail.</p>"
	}
	if (savings>0){
	
	msg3='<p><b>By using NDG, Addressed Admail you save <em style="font-style: normal; font-weight: bold; color: red;">$'+ savingsStr +"</em> in direct postage costs.</b></p><p>With LCP, Addressed Admail you could save <b>significantly more.</b></p>"}
	//msg4='<form><input type="button" VALUE="  OK  " onClick="javascript: displayNone(' + "'" + 'extraBox' + "'" + ')"><h5 style="margin-top: 1em;">Click OK to Exit</h5></form></center>';
	
	}
	if (endFlag!=0){
		msg0="";		
		msg1="<p>According to the information you've entered, your mailing piece will weigh</p>";
        msg2="<p><em style=\"font-style: normal; font-weight: bold; color: red;\">" + wtPiece + " grams</em>.</p>";
		msg3="<p><b>Since Lettermail cannot exceed 500 grams, a comparison cannot be made.</b></p>";
		//msg4='<form><input type="button" VALUE="  OK  " onClick="javascript: displayNone(' + "'" + 'extraBox' + "'" + ')"><h5 style="margin-top: 1em;">Click OK to Exit</h5></form></center>';
	}  
displayMsg(msg0,msg1,msg2,msg3,msg4)
}
function sizeCat(){
	size=""
	wtEnv=0	
	if (envTypeIdx==0){
			wtEnv=6
			size="SL"}
	if (envTypeIdx==1){  
			wtEnv=11
			size="SL"}
	if (envTypeIdx==2){
			wtEnv=16
			size="OS"}
}

function wtPerPiece(num811,num1117,numbrc,numreply9){
	wtPiece=0
	pieceRate=0
	baseRate=0
	overWtAdjust=0
	
	wtPiece= Math.round(wtEnv + num811*wt811 + num1117*wt1117 + numbrc*wtbrc + numreply9*wtreply9);

	if (wtPiece > 500){
	   //alert("Too Big for Bulk Mail:  Use Parcel Post or send as a Catalouge")
	   endFlag=1	
	}
	if (wtPiece > 100){
           size='OS';
	}

	if (size=='SL' && (wtPiece<=50)){
	   pieceRate = admailSL;
	}
	if (size=='SL' && (wtPiece>50)){	
           baseRate = admailSL;
           overWtAdjust = (wtPiece-50)*admailSLinc;
           pieceRate = baseRate + overWtAdjust;
	}

        if (size=='OS' && (wtPiece<=50)){
           pieceRate = admailOS;
	}

        if (size=='OS' && (wtPiece>50)){	
	   baseRate = admailOS;
           overWtAdjust = (wtPiece-50)*admailOSinc;
           pieceRate = baseRate + overWtAdjust;
	}

}

function totalCost(num){
postage=0



//ghost mail case
	if ((num<1000) && (size=='SL')){	
		postage=pieceRate*num + admailSL*(1000-num)}

	if ((num<1000) && (size=='OS')){	
		postage=pieceRate*num + admailOS*(1000-num)}

	if (num>=1000){
		postage=pieceRate*num
		}
postage=Math.round(100*postage)/100	
dollarDeci(postage)
postageStr=dollarStr

}

function dollarDeci(dollarVal){
//* get the right number of decimals*//

dollarStr=dollarVal +''	//* a wee cheat to convert num to char*//

	if((dollarStr.length-dollarStr.lastIndexOf("."))==2 && dollarStr.lastIndexOf(".")!=-1){
		dollarStr=dollarStr+'0'
}			
	if(dollarStr.lastIndexOf(".")==-1){
		dollarStr=dollarStr+'.00'}			
	
 }



function cf1stClass(){
	fcp=0
	fcpTotal=0	

	if (size=='SL'){
		
			if (wtPiece<=30){
				fcp = lmail30}
			if ((wtPiece>30)  && (wtPiece<=50)){
				fcp = lmail50}
			if ((wtPiece>50) && (wtPiece<=100)){
				fcp = lmail100}
			if ((wtPiece>100) && (wtPiece<=200)){
				fcp = lmail200}
			if ((wtPiece>200) && (wtPiece<=300)){
				fcp = lmail300}
			if ((wtPiece>300) && (wtPiece<=400)){
				fcp = lmail400}
			if ((wtPiece>400) && (wtPiece<=500)){
				fcp = lmail500}
			if (wtPiece>500){
				alert("Your Mailing must be sent as either Parcel of Catalogue")
				endFlag=1}
	}

	if (size=="OS"){
			if (wtPiece<=100){
				fcp = lmail100}
			if ((wtPiece>100) && (wtPiece<=200)){
				fcp = lmail200}
			if ((wtPiece>200) && (wtPiece<=500)){
				fcp = lmail500}
			if ((wtPiece>200) && (wtPiece<=300)){
				fcp = lmail300}
			if ((wtPiece>300) && (wtPiece<=400)){
				fcp = lmail400}
			if (wtPiece>500){
				alert("Your Mailing must be sent as either Parcel of Catalogue")
				endFlag=1}
	}
fcpTotal=fcp*num
fcpTotal=Math.round(fcpTotal*100)/100
dollarDeci(fcpTotal)
fcpTotalStr=dollarStr

}

function displayMsg(msg0,msg1,msg2,msg3,msg4) {
if (goof!=1){  		
  tempMsg = msg0 + msg1 + msg2 + msg3 + msg4;
  document.getElementById("dialog").innerHTML = tempMsg;
  $.fn.colorbox({width:"450px", inline:true, href:"#dialog"});
}
}




