// ****************************************************************
// Preprinted.js
// 5/30/07 -- CEA, SFA
// This series of script is used with the ShopPreprinted.asp
// To validate the form as well as populate the form objects
// including dynamic changes to quantites, formats etc, as changes are made.
// ****************************************************************


function onValidatePreprinted(thisform)
{

	var strLine = new Array();

	var i, ii;
	var iMoved
	var bError = false;
	
	var red		= '#FF0000';
	var black	= '#000000';
	
	var a;



	ChangeColor("CoLine1Static", black);
	ChangeColor("BankLine1Static", black);
	ChangeColor("StartingNumberStatic", black);
	ChangeColor("FaxNumberStatic", black);


	var bReorder = false;


	a = document.getElementById("PreviousPreprintedOrders");

	if(a) {
		var index = a.selectedIndex;
		i = a.options.item(index).value

		if (i == -2)
			bReorder = true;

	}


	 
	if(thisform.bReorder && thisform.bReorder.checked)
		bReorder = true;



	if(bReorder == false) { 

		if(thisform.CoLine1.value == "") {
			bError = true;	
			ChangeColor("CoLine1Static",red);
		}
		
		if(thisform.BankLine1.value == "") {
			bError = true;
			ChangeColor("BankLine1Static",red);
		}
	

		if(thisform.StartingNumber.value == "" || parseInt(thisform.StartingNumber.value) <= 0) {
			bError = true;	
			ChangeColor("StartingNumberStatic",red);
		}
	

		if(thisform.FaxNumber.value == "") {
			bError = true;	
			ChangeColor("FaxNumberStatic",red);
		}
	
	}


	if(thisform.RoutingNumber.value == "" || IsValidABA(thisform.RoutingNumber.value) == false) {
		bError = true;	
		ChangeColor("RoutingNumberStatic",red);
	}
	else 
		ChangeColor("RoutingNumberStatic", black);



	if(thisform.AccountNumber.value == "") {
		bError = true;	
		ChangeColor("AccountNumberStatic",red);
	}
	else 
		ChangeColor("AccountNumberStatic", black);





	// Move Company and Banking lines up, if blank lines exist above completed lines.
	strLine[0] = thisform.CoLine1.value;
	strLine[1] = thisform.CoLine2.value;
	strLine[2] = thisform.CoLine3.value;
	strLine[3] = thisform.CoLine4.value;
	strLine[4] = thisform.CoLine5.value;

	
	iMoved = 0;
	for(i=1;i<4;i++)
	{
		if(strLine[i] == "")
		{
			for(ii = i; ii < 4; ii++)
			{
				if(strLine[ii+1] != "")
				{
					strLine[ii] = strLine[ii+1];
					strLine[ii+1] = "";
					iMoved = i;
				}
			}
			if(iMoved == i)
				i--;					
		}

	}


	//Redraw the input fields of the form if we moved the data.
	if(iMoved) {
		thisform.CoLine1.value = strLine[0];
		thisform.CoLine2.value = strLine[1];
		thisform.CoLine3.value = strLine[2];
		thisform.CoLine4.value = strLine[3];
		thisform.CoLine5.value = strLine[4];
	}




	strLine[0] = thisform.BankLine1.value;
	strLine[1] = thisform.BankLine2.value;
	strLine[2] = thisform.BankLine3.value;
	strLine[3] = thisform.BankLine4.value;


	// Now move the Banking Info.
	iMoved = 0;
	for(i=1;i<3;i++)
	{
		if(strLine[i] == "")
		{
			for(ii = i; ii < 3; ii++)
			{
				
				if(strLine[ii+1] != "")
				{
					strLine[ii] = strLine[ii+1];
					strLine[ii+1] = "";
					iMoved = i;
				}
			}
			if(iMoved == i)
				i--;					
		}
	}

	if(iMoved) {
		thisform.BankLine1.value = strLine[0];
		thisform.BankLine2.value = strLine[1];
		thisform.BankLine3.value = strLine[2];
		thisform.BankLine4.value = strLine[3];
	}



	var qty = 0;
	a = document.getElementById("CheckQTY");
	index = a.selectedIndex;
	if (index >= 0)
		qty = parseInt(a.options.item(index).text);
		

	if (qty == 0)  {

		a = document.getElementById("DepositQTY");
		index = a.selectedIndex;
		
		if (index >= 0) 
			qty = parseInt(a.options.item(index).text);


		if (qty == 0)  {

			a = document.getElementById("EnvQTY");
			index = a.selectedIndex;
			if (index >= 0)
				qty = parseInt(a.options.item(index).text);


			if(qty <= 0) {
				alert("Please select a quanity of Checks, Deposit Tickets, or Envelopes.");
				return false;
			}

		}
	}



	if(bError == true) {

		alert("Please complete and/or verify the items shown in red.");
		return false;
	}


	return 1;





}


function onFontChange(ListBox)
{


	if(!ListBox)
		return;

	var index = ListBox.selectedIndex;

	var a = ListBox.options;


	var strFontFace = a.item(index).text;
	

	//var a = document.getElementById("CoLine1Static");
	//a.style.fontWeight="bold";										-- This works.
	//a.style.fontStyle="italic";										-- This works.
	//a.style.fontFamily="Arial Narrow, Charlesworth, Times New Roman"; -- This works
	//a.style.fontFamily="Arial Narrow";								-- This works.
	//a.style.fontFamily="Charlesworth";								-- This works.


	ChangeFontFamily("CoLine1Static", strFontFace);
	ChangeFontFamily("CoLine2Static", strFontFace);
	ChangeFontFamily("CoLine3Static", strFontFace);
	ChangeFontFamily("CoLine4Static", strFontFace);
	ChangeFontFamily("CoLine5Static", strFontFace);


	ChangeFontFamily("CoLine1", strFontFace);
	ChangeFontFamily("CoLine2", strFontFace);
	ChangeFontFamily("CoLine3", strFontFace);
	ChangeFontFamily("CoLine4", strFontFace);
	ChangeFontFamily("CoLine5", strFontFace);

	
}


function onFontWeightChange(Item)
{


	if(!Item)
		return;


	var weight = "normal";

	if(Item.checked)
		weight = "bold";
		

	ChangeFontWeight("CoLine1Static", "bold");
	ChangeFontWeight("CoLine1", "bold");
	

	if(Item.name == "bBoldCoLine2") {
		ChangeFontWeight("CoLine2Static", weight);
		ChangeFontWeight("CoLine2", weight);

	}


}


// Did not want to use radio buttons, but cannot allow both check boxes to be checked,
// So unclick the other if it is clicked.

function NotBoth(Item)
{

	var bChecked = Item.checked;

	if(bChecked) {

		var itemPartner = "bLogoOnFile";

		if(Item.name == "bLogoOnFile") 
			itemPartner = "bAddLogo";
		
		var a = document.getElementById(itemPartner);

		a.checked = false;

	}
}







// The discount brackets have been loaded into a document level array by the .asp code
// We can now access this array to determine the dicount level.

function GetDiscount(qty, index)
{


	if (index < 0 || index > 2)
		index = 1;			



	if(!document.Discounts)
		return 0;						// Not yet loaded.

	var minimum = document.Discounts[index][0];

	if (qty < minimum) 
		return 0;

	

	var j;
	var end = document.Discounts[index].length;
	

	for (j=0;j<end;++j) {

		if (qty <= document.Discounts[index][j*2+1])
			return document.Discounts[index][j*2+2];

	}

	return 0;

}



// Formats a value into 2 decimal places.
function FormatDollars(number)
{

	var fAmount = parseInt((number+.005) * 100);

	if (isNaN(fAmount))
		return "0.00";


	var sAmount = fAmount.toString();
	
	var iLen = sAmount.length;

	var sReturn = sAmount.substr(0, iLen-2);
	sReturn+= ".";
	sReturn += sAmount.substr(iLen-2,2);


	return sReturn;
	


}


function IsValidABA(aba_)
{


	if(aba_ == "" || aba_.length != 9)
		return false;


	var value, total=0;
	var j;	

	for(j=0; j< 8; j++) {		
		
		value = parseInt(aba_.charAt(j));			// Extract numeric value of each digit

		if(value < 0 || value > 9)	// Non numeric char.
			return false;
			
		
		if(j==0||j==3||j==6)
			value = value * 3;
		else if(j==1||j==4||j==7)
			value = value * 7;
		
		total+= value;
	}


	// Get the modulus remainder of the computed value, this is the "Check" digit
	var remainder = 10 -(total % 10);
	if(remainder == 10)
		remainder = 0;

	return remainder == parseInt(aba_.charAt(8));			// the calculated check digit should now be the last digit
}



// The CCode, URL and Images are all encoded in the value of each option of the checkFormats list box.

function GetCCode(index)
{

	
	var a = document.getElementById("CheckFormat");
	
	if(index<0)	
		index = a.selectedIndex;


	if (index >= 0) {

		var ccode = a.options.item(index).value;

		if(ccode != "") {
			
			index = ccode.indexOf("|");					// The extended description page and small image url are separated by a pipe.

			if(index >= 0)
				return ccode.substr(0,index);
			else
				return ccode;
		}

	}
	return null;					

}




// The product URL is the 2nd value between pipes (ccode|URL|IMAGE)
function GetProductURL()
{

	var a = document.getElementById("CheckFormat");
	index = a.selectedIndex;

	if (index >= 0) {

		var strURL = a.options.item(index).value

		
		if(strURL != "") {
			
			index = strURL.indexOf("|");					// The extended description page and small image url are separated by a pipe.

			if(index >= 0) {
				strURL = strURL.substr(index+1,999);
				index = strURL.indexOf("|");				
			}

			if(index >= 0)
				return strURL.substr(0,index);
			else
				return strURL;
		}

	}
	return "shopcontent.asp?type=features";					// if no extended description, then just show them the features pages.

}



function GetProductSmallImageURL()
{

	var a = document.getElementById("CheckFormat");
	index = a.selectedIndex;

	if (index >= 0) {

		var strURL = a.options.item(index).value;

		
		if(strURL && strURL != "") {


			index = strURL.indexOf("|");					// The extended description page and small image url are separated by a pipe.

			if(index >= 0) {
				strURL = strURL.substr(index+1,999);
				index = strURL.indexOf("|");				
			}
		
			
			index = strURL.indexOf("|");					// The extended description page and small image url are separated by a pipe.

			if(index >= 0)
				return strURL.substr(index+1,999);
			
		}

	}
	return "";					

}




function OpenProductURL()
{


	var a = document.getElementById("CheckFormat");
	index = a.selectedIndex;


	

	if (index >= 0) {

		var strURL = a.options.item(index).value;
		

		if(strURL != "") {
			
			index = strURL.indexOf("|");					// The extended description page and small image url are separated by a pipe.

			if(index >= 0) {
				strURL = strURL.substr(index+1,999);
				index = strURL.indexOf("|");				


			}


			if(index >= 0)
				strURL =  strURL.substr(0,index);
	

			strURL = "window.open('" + strURL + "','','menubar=no,toolbar=no,location=no,status=no,directories=no,copyhistory=no,resizable=yes,scrollbars=yes width=800,height=600')"

			eval(strURL);
			return true;

		}


	}
	alert("Please select a product from the drop down list");
	return "#";	

}



function OpenCompatVendorURL()
{


	var ccode = GetCCode(-1);

	if (ccode) {

		var strURL = "./shopDisplayCompatvendors.asp?ccode=" + ccode;

		strURL = "window.open('" + strURL + "','','menubar=no,toolbar=no,location=no,status=no,directories=no,copyhistory=no,resizable=yes,scrollbars=yes width=400,height=600')"

		eval(strURL);
		return true;


	}
	alert("Please select a product from the drop down list");
	return "#";	


}


// Herrinbone, Marble etc.
// If the style is changed, then the color may need to change as well.

function onCheckStyleChange()
{

	var i;

	var a = document.getElementById("CheckStyle");


	var index = a.selectedIndex;
	var strStyle = a.options.item(index).text;
	

	var aColors = new Array();

	if(strStyle == "Herringbone") {

		if (bIs3Up()) {

			aColors[0] = "Blue";
			aColors[1] = "Green";
			aColors[2] = "Burgundy";
			aColors[3] = "";

		}

		else {

			aColors[0] = "Blue";
			aColors[1] = "Green";
			aColors[2] = "Burgundy";
			aColors[3] = "Gray";
			aColors[4] = "Tan";
			aColors[5] = "";

		}


	}
	else if (strStyle == "Pastel Solid")
	{
		aColors[0] = "Green";
		aColors[1] = "Burgundy";
		aColors[2] = "Gray";
		aColors[3] = "Tan";
		aColors[4] = "";

	}
	else 
	{
		aColors[0] = "Blue";
		aColors[1] = "Green";
		aColors[2] = "Burgundy";
		aColors[3] = "Yellow";
		aColors[4] = "";

	}


	// Get the color list box.
	a = document.getElementById("CheckColor");
	index = a.selectedIndex;
	var strCurrentColor;					// Select 1st color if no selection.


	
	if (index >= 0)
		strCurrentColor = a.options.item(index).text;


	else if(!strCurrentColor) 
		strCurrentColor = readCookie("CheckColor");


	if(!strCurrentColor)
		strCurrentColor = aColors[0];		


	
	// Clear the list
	while(a.options.length) 
		a.options[0] = null;



	index = 0;			// incase we cannot re-select this color

	for(i=0; aColors[i] != ""; ++i) {

		a.options[i] = new Option(aColors[i]);
	
		//var oOption = document.createElement("OPTION");			// Not comptable with firefox.
		//oOption.text=aColors[i];
		//a.add(oOption);

		if(aColors[i] == strCurrentColor)  
			index = i;
		
		
	
	}

	a.selectedIndex = index;			// reselect the current selection if any.


	createCookie("CheckColor", a.options.item(index).text, 2);


	return true;




}



// Poplulate the qty check box, based on the type of check (1 up, 2 up, 3 up etc.)
// The prices are also shown and are calculated based on the discount table.

function onCheckFormatChange()
{


	var i;
	var strCurrent
	var iMultiplier = 500;
	var price = 0;
	var bPers = false;
	var ccode
	var iDiscountIndex = 0


	var a = document.getElementById("CheckFormat");
	index = a.selectedIndex;


	if (index >= 0) {

		strCurrent = a.options.item(index).text;
		strCurrent = strCurrent.toLowerCase();

		ccode = GetCCode(-1);

	
		if (strCurrent.indexOf("3 up") >= 0 || strCurrent.indexOf("3 - up") >= 0 || strCurrent.indexOf("3up") >= 0) {
			iMultiplier = 600;
			iDiscountIndex = 1;
		}


		if(strCurrent.indexOf("personal") >= 0)			// personal size checks do not have matching envelopes.
			bPers = true;

	
		index = strCurrent.indexOf("base price:");


		// The Price is contained within the name in the drop down list.
		if (index >= 0) {
			price = strCurrent.substr(index+12,10);
			price = parseFloat(price);

		}
		
	}

	

	strCurrent = "";

	a = document.getElementById("CheckQTY");
	index = a.selectedIndex;
	if (index >= 0)
		strCurrent = a.options.item(index).text;



	// When the qty is changed, a cookie is created, this allows the user to use the back button and not loose the qty they selected,
	// This is necessary since this select list is populated by the script, not ASP so the back button causes a complete repopulation of the
	// listbox, and no item will have been "Previously" selected.


	if(!strCurrent || strCurrent == "") 
		strCurrent = readCookie("CheckQTY");
	



	// Clear the list
	while(a.options.length) 
		a.options[0] = null;

	for(i= 0; i<=64; ++i) {
	

		var iQty = i * iMultiplier;


		// For Compatability with main program, the discount is applied to the discounted qty price, not the total.

		/*
		var total = iQty * price;;
		var discount = GetDiscount(iQty, iDiscountIndex);
		if (discount > 0) 
			total-= (total * discount);
		*/

		
		discountedPrice = price;
		var discount = GetDiscount(iQty, iDiscountIndex);
	
		if (discount > 0) {

		
			if(discount<1.0)
				discount=price*discount

			discountedPrice-=discount;
					
			// Must force to 4 decimal places for compatability with main program.
			// Note: This math forces compatablity with the main program -- the rounding here is based on the subtracted amount,
			// It should have been on the discount amount. -- And actually the discount should have been aplied to the extended total.

			discountedPrice *= 10000;
			discountedPrice = Math.round(discountedPrice);
			discountedPrice/=10000;

			
		}
		
		var total = iQty * discountedPrice;;
		
		var sQty = iQty + " for $" + FormatDollars(total);


		//var oOption = document.createElement("OPTION");		Not compatable with firefox -- replaced with line below.
		//oOption.text= sQty;
		//a.add(oOption);

		a.options[i] = new Option(sQty, iQty);

		if(iQty == parseInt(strCurrent))
			index = i;
			

	}

	if (index >= 0)
		a.selectedIndex = index;			// reselect the current selection if any.


	onSetSrcforSmallImage();

	ShowApplicableStyles(ccode)				// Marble is not available with all formats.


	ShowHideEnvelopes(bPers)

	return true;


}

// If the vendor is changed, then the formats available also change -- which can also change the quantity listbox
function onChangeVendor()
{


	var iFormats = 0;
	var iCurrentIndex = 0;
	var strCCode;
	
	var strCurrentVendor;
	var strCurrentFormat;



	a = document.getElementById("PreprintedVendors");
	index = a.selectedIndex;

	if (index >= 0) {
		strCurrentVendor = a.options.item(index).text;
		strCCode = a.options.item(index).value;							// All of the pre-printed ccodes that this vendor supports are encoded in the value object separated by pipes -- L80971XX14|L80641XX -- etc

	}




	a = document.getElementById("CheckFormat");
	index = a.selectedIndex;
	if (index >= 0)
		strCurrentFormat = a.options.item(index).text;


	//alert(strCurrentFormat);



	if (!document.aPreprintedVendors) {

		document.aPreprintedVendors = new Array();
		document.aPreprintedVendors[0] = new Array();								// The text
		document.aPreprintedVendors[1] = new Array();								// The CCODE|URL|IMAGE
				

		for (index = 0; index < a.options.length ; index++) {
		
			document.aPreprintedVendors[0][index] = a.options.item(index).text;
			document.aPreprintedVendors[1][index] = a.options.item(index).value;

		}
	}



	

	// Clear the list
	while(a.options.length) 
		a.options[0] = null;


	
			

	
	for (index = 0; index < document.aPreprintedVendors[0].length; ++ index) {


		var strValue = document.aPreprintedVendors[1][index];		// (Encoded) --- ccode|url|image.
		if(strValue != "") {
			var pos = strValue.indexOf("|");						// The ccode, extended description page and small image url are separated by a pipe.
			if(pos >= 0)
				strValue = strValue.substr(0,pos);
			
		}

		
		if (strCCode == "ALL" || strCCode.indexOf(strValue) >= 0) {			// Is the product code in the list of products for this vendor.

			var oOption = new Option(document.aPreprintedVendors[0][index], document.aPreprintedVendors[1][index]);
		
			a.options[iFormats] = oOption;
		
			if(oOption.text == strCurrentFormat) 
				iCurrentIndex = iFormats;

			iFormats++;
			
			
		}

	}

	a.selectedIndex = iCurrentIndex;		


	onCheckFormatChange();
	


}


function onSetSrcforSmallImage()
{


	var img = document.getElementById("smallcheckimage");
	
	img.src = GetProductSmallImageURL();

	if(img) {

		// Change the product ID under the image.
		var ccode = GetCCode(-1);
		var producttext = document.getElementById("ProductCode");
		producttext.value = ccode;

	}


}


function onSaveListValueToCookie(Item)
{

	var index = Item.selectedIndex;
	var strValue = Item.options.item(index).text;
	var strName = Item.name;


	// write the selected qty to a cookie, since if they backup we need this value reset after the java script rebuilds the select list.
	if (strValue) 
		createCookie(strName,strValue,1);


}



function onChangePreviousOrders()
{


	var RecID;

	var a = document.getElementById("PreviousPreprintedOrders");
	var index = a.selectedIndex;

	if (index >= 0) 
		RecID = a.options.item(index).value;							// All of the pre-printed ccodes that this vendor supports are encoded in the value object separated by pipes -- L80971XX14|L80641XX -- etc


	var action = "shoppreprinted.asp?REC_ID=" + RecID;


	SubmitForm("preprinted", action);

}



// Herrinbone, Marble etc.
// If the style is changed, then the color may need to change as well.

function ShowApplicableStyles(ccode)
{

	var i;


	var a = document.getElementById("CheckStyle");
	var index = a.selectedIndex;
	var strStyle = a.options.item(index).text;


	
	
	// Clear the list
	while(a.options.length) 
		a.options[0] = null;


	index = 0;			// incase we cannot re-select this color

	
	var aStyles = new Array();


	if(ccode == "L8000314" || ccode == "L8097414") {			// 3 UP BUSSINESS SIZED CHECKS (UNLINED/LINED)
		aStyles[0] = "Herringbone";
		aStyles[1] = "Pastel Solid";
		aStyles[2] = "";
	}

	else if(ccode == "L8093514") {								// 3 UP PERSONAL.
		aStyles[0] = "Herringbone";
		aStyles[1] = "";
	}

	else {

		aStyles[0] = "Herringbone";								// ALL others.
		aStyles[1] = "Marble";
		aStyles[2] = "";
	}


	
	for(i=0; aStyles[i] != ""; ++i) {
	
		a.options[i] = new Option(aStyles[i]);
		
		if(aStyles[i] == strStyle)  
			index = i;
	
	} 

	a.selectedIndex = index;			// reselect the current selection if any.


	onCheckStyleChange();

	return true;


}




function ShowHideEnvelopes(bHide)
{

	var i;

	var a = document.getElementById("EnvelopesDiv");			// (CEA - 9/25/09 -- Not sure if this works -- ID's should be unique) There are 3 divisions named this. (the elements in the envelopes row)

	for (i=0;i<a.length; i++) {

		if(bHide) 
			a[i].style.visibility = "hidden";
		else 
			a[i].style.visibility = "visible";
	}

	
	if (bHide) {
		a = document.getElementById("ENVQTY");
		a.selectedIndex = 0;			// if we are hidden, be sure to reset the qty to zero (the 1st option)
	}

}



// Poplulate the qty check box, based on the type of check (1 up, 2 up, 3 up etc.)
// The prices are also shown and are calculated based on the discount table.

function bIs3Up()
{


	var strCurrent
	var a = document.getElementById("CheckFormat");
	var index = a.selectedIndex;


	if (index >= 0) {

		strCurrent = a.options.item(index).text;
		strCurrent = strCurrent.toLowerCase();

		if (strCurrent.indexOf("3 up") >= 0 || strCurrent.indexOf("3 - up") >= 0  || strCurrent.indexOf("3up") >= 0) 
			return 1;

	}

	return 0;


}


