//--Copyright © 1999-2007, A Plus Consultants, LLC, All Rights Reserved.
//--No part of this script may be used for any purpose without prior
//--written permission. Contact richard@aplusconsultants.com.
/*
	Common JavaScript functions.
	Created: 01/03/2004

	Functions:
	askDel()				(moved from stores.cfm)
	bSaveFileName(oFile, oName)
	bSetChangedCheck(oForm, sElement, sChanged)
	bSetChangedRadio(oForm, sElement, sChanged)
	CalcSalePrice(oForm, sSalePrice, nDiscount, sBasePrice)
	CheckSome(oForm, sElement, sAction)
	DittoSet(sForm, sField)
	DittoCopyAll(sForm, sFieldList)
	DittoSetAll(sForm, sFieldList)
	DittoViewAll(sForm, sFieldList)
	doAddress(sOrder)
	doCloseOrder(sOrder)
	doComment(sOrder)
	doEmail(sOrder)
	doInvoice(sOrder)
	doOrder(sColumn, sDir, sForm)	(moved from archivelist_tb.cfm)
	doReopenOrder(sOrder)
	doShipComplete(sOrder, sFormName)
	doShowShipping(sOrder)
	doSKUFilter()
	doSubmitEngrave()
	doSubmitOrder(iOrder, sFormName)
	FocusOn(sObject)		set focus to the window or object.
	OpenPop(sURL, sTarget)
	selectRow(mT, sWindow)	(moved from searchlist.cfm)
	showPopup(sMsg)			(moved from various pages)
	showWait(bShow)			(moved from archivelist_tb.cfm)
*/
var ww = null;

function askDel() {
	return confirm("WARNING!\n\nYou're about to delete a store. Any users associated to this store need to be changed as well!\n\nIf you wish to continue, click OK. Click Cancel to return to the previous screen.");
}

function bSaveFileName(oFile, oName) {
/*
	We need to preserve the name of the uploaded file for comparison.
*/
	var sFile = oFile.value;
	sFile = sFile.substr(sFile.lastIndexOf("\\") + 1, sFile.length);
	//	Condition added because of problem when leaving upload file name blank.
	if (sFile != "") {
		oName.value = sFile;
	}
}

function bSetChangedCheck(oForm, sElement, sChanged) {
	var oElement = oForm[sElement];
	var ii = 0;
	var iCnt = 0;
	var sChangedCheck = "";
	for (ii = 0; ii < oElement.length; ii++) {
		if (oElement[ii].type == "checkbox") {
			if (oElement[ii].checked != oElement[ii].defaultChecked) {
				sChangedCheck += "," + oElement[ii].value;
				iCnt++;
			}
		}
	}
	sChangedCheck = sChangedCheck.substr(1, sChangedCheck.length);
	oForm[sChanged].value = sChangedCheck;
//	alert(sChangedCheck);
/*	if (iCnt == 0) {
		alert("You haven't made any changes.");
		return false;
	} */
	alert(iCnt + " box(es) have changed.");
	return true;
}

function bSetChangedRadio(oForm, sElement, sChanged) {
//	var oElement = oForm[sElement];
	var ii = 0;
	var iCnt = 0;
	var sChangedCheck = "";
	var sElName = "";
	for (ii = 0; ii < oForm.elements.length; ii++) {
//alert(oForm.elements[ii].name);	
		if (oForm.elements[ii].type == "radio" && sElName != oForm.elements[ii].name) {
			if (oForm.elements[ii].checked != oForm.elements[ii].defaultChecked) {
				sElName = oForm.elements[ii].name;
				var ar = sElName.split("_");
				sChangedCheck += "," + ar[1];
				iCnt++;
			}
		}
	}
	sChangedCheck = sChangedCheck.substr(1, sChangedCheck.length);
	oForm[sChanged].value = sChangedCheck;
//	alert(sChangedCheck);
/*	if (iCnt == 0) {
		alert("You haven't made any changes.");
		return false;
	} */
	alert(iCnt + " button(s) have changed.");
	return true;
}

function CalcSalePrice(oForm, sSalePrice, nDiscount, sBasePrice) {
/*
	This function computes a sale price for all the elements on a
	form based on the discount percentage and the type of base price
	to calculate on.
	
	It expects two or more sets of prices to be available on the form.
	One price must the sale price element for placing the calculation
	in and the others are the base prices.
	
	The form element names might be:
		sSalePrice:	PriceSale_nnnnn
		sBasePrice:	PriceRegular_nnnnn
					PriceMSRP_nnnnn

	Parameters:
	oForm		: The form object of the calling form.
	sSalePrice	: The sale price element string prefix.
	nDiscount	: A percentage discount to reduce a base price by.
	sBasePrice	: The base price element string prefix.
*/
	var ar = "";
	var arBasePrice = oForm[sBasePrice];
	var ii = 0;
	var nCalcPrice = 0;
	var oEl;
	var sName = "";

	for (ii = 0; ii < arBasePrice.length; ii++) {
		if (arBasePrice[ii].checked) {
			sBasePrice = arBasePrice[ii].value;
		}
	}
	if (nDiscount == "" || parseFloat(nDiscount, 10) == "NaN") {
		alert("Invalid discount.");
		return;
	}
	nDiscount = 1 - (nDiscount / 100);
	for (ii = 0; ii < oForm.elements.length; ii++) {
		if (oForm.elements[ii].type == "text") {
			oEl = oForm.elements[ii];
			sName = oEl.name;
			if (sName.substr(0, sSalePrice.length + 1) == sSalePrice + "_") {
				//	Split out the product identifier to build the
				//	name of the base field.
				ar = sName.split("_");
				nCalcPrice = oForm[sBasePrice + "_" + ar[1]].value;
				oEl.value = parseInt(nCalcPrice * nDiscount * 100, 10) / 100;
			}
		}
	}
}

function CheckSome(oForm, sElement, sAction) {
/*
	This selects or removes the selection from an array of form
	objects. The three actions are:
	All - select all items in the array.
	None - remove selection from all items in the array.
	Reverse - Toggle selected items off and unselected items on.
	It only works for the checkbox type, but it could be extended
	to work with the select-multiple type as well.
*/
//	alert(oForm.name + "," + sElement + "," + sAction);
	var oElement = oForm[sElement];
	var ii = 0;
	var iCnt = 0;
	for (ii = 0; ii < oElement.length; ii++) {
		if (oElement[ii].type == "checkbox") {
			if (sAction == "All") {
				oElement[ii].checked = true;
				iCnt++;
			}
			if (sAction == "None") {
				oElement[ii].checked = false;
			}
			if (sAction == "Reverse") {
				if (oElement[ii].checked) {
					oElement[ii].checked = false;
				}
				else {
					oElement[ii].checked = true;
					iCnt++;
				}
			}
		}
	}
	alert(iCnt + " box(es) are now selected.");
}

function DittoSet(sForm, sField) {
/*
	Set's the value of a form element to a value that has been
	stored in another element.

	The hidden "Ditto" fields store the value of the input and
	select controls.
	
	Other thoughts on handling this include:
		Writing out a JavaScript array and reading back from it.
		Setting the elements in a separate form.
		Reading all values form a sinlge hidden field.
*/
	var ii = 0;
	var oForm = document.forms[sForm];
	var oTgt = oForm[sField];
	var sDitto = sField + "Ditto";
	var sVal = oForm[sDitto].value;
	
	//	Handle the different object types.
	if (oTgt.type == "text") {
		//	Overwrite.
		oTgt.value = sVal;
	}
	else if (oTgt.type == "textarea") {
		//	Append.
		if (oTgt.value == "") {
			oTgt.value = sVal;
		}
		else {
			if (sVal == "") {
				oTgt.value = sVal;
			}
			else {
				oTgt.value += "\n" + sVal;
			}
		}
	}
	else if (oTgt.type == "select-one") {
		//	Select.
		for (ii = 0; ii < oTgt.options.length; ii++) {
			if (oTgt.options[ii].value == sVal) {
				oTgt.selectedIndex = ii;
			}
		}
	}
}

function DittoCopyAll(sForm, sFieldList) {
/*
	This reads the current values of the form fields and copies it
	into the hidden "Ditto" fields and then displays the newly set
	values.

	The hidden "Ditto" fields store the value of the input and
	select controls.
*/
	var ar = sFieldList.split(",");
	var ii = 0;
	var jj = 0;
	var oForm = document.forms[sForm];
	var oSrc = oForm[0];	//	First form field object.
	var oTgt = oForm[0];	//	First form field object.
	var sShow = "NEW SETTINGS:\n";
	var sVal = "";
	var sTab = "";

	//	Force a wider dialog window by padding with tabs.

	sShow += "===============\t\t\t\t\t\t\n";
	for (ii = 0; ii < ar.length; ii++) { 
		oSrc = oForm[ar[ii]];
		oTgt = oForm[ar[ii] + "Ditto"];
		//	Load current value into the ditto field.
		if (oSrc.type == "text" || oSrc.type == "textarea") {
			sVal = oSrc.value;
			oTgt.value = oSrc.value;
		}
		else if (oSrc.type == "select-one") {
			//	Select.
			sVal = oSrc.options[oSrc.selectedIndex].value;
			oTgt.value = sVal;
		}
		//	Add another tab for short names to format the display.
		sTab = (ar[ii].length > 7) ? "" : "\t";
		sShow += ar[ii] + ":\t" + sTab + sVal + "\n";
	}
	alert(sShow);
}

function DittoSetAll(sForm, sFieldList) {
/*
	This calls the DittoSet function for each "Ditto" field in the
	list. It is intended, generally, for use on an empty form.
*/
	var ar = sFieldList.split(",");
	var ii = 0;
	for (ii = 0; ii < ar.length; ii++) { 
		DittoSet(sForm, ar[ii]);
	}
	alert(ar.length + " values were set.");	
}

function DittoViewAll(sForm, sFieldList) {
/*
	Display all the current values of the "Ditto" fields in the list.

	The hidden "Ditto" fields store the value of the input and
	select controls.
*/
	var ar = sFieldList.split(",");
	var ii = 0;
	var oForm = document.forms[sForm];
	var sShow = "CURRENT SETTINGS:\n";
	var sTab = "";

	//	Force a wider dialog window by padding with tabs.
	sShow += "===============\t\t\t\t\t\t\n";
	for (ii = 0; ii < ar.length; ii++) { 
		//	Add another tab for short names to format the display.
		sTab = (ar[ii].length > 7) ? "" : "\t";
		sShow += ar[ii] + ":\t" + sTab + oForm[ar[ii] + "Ditto"].value + "\n";
	}
	alert(sShow);
}

function doAddress(sOrder) {
	window.showModalDialog("orderaddress.cfm?K=" + sOrder, "", "dialogWidth=550px; dialogHeight=210px; center:yes; status:no; scroll:yes; help:no");
}

function doCloseOrder(sOrder) {
	if (confirm("Close Order\n\nThis will close the order, regardless of whether or not it was shipped. To continue, press OK. Press cancel to return.")) {
		document.location.href = "orderclose.cfm?K=" + sOrder;
	}
}

function doComment(sOrder) {
	var W = 550, H = 320;
	L = (screen.width - W) / 2;
	T = (screen.height - H) / 2;
	oWin = window.open("ordercommentedit.cfm?K=" + sOrder, "CommentWin", "width=" + W + ",height=" + H + ",left=" + L + ",top=" + T + ",scrollbars=yes,status=no");
	oWin.focus();
}

function doEmail(sOrder) {
	var W = 593, H = 488;
	L = (screen.width - W) / 2;
	T = (screen.height - H) / 2;
	oWin = window.open("ordermail.cfm?K=" + sOrder, "EmailWin", "width=" + W + ",height=" + H + ",left=" + L + ",top=" + T + ",scrollbars=yes,status=no");
	oWin.focus();
}

function doInvoice(sOrder) {
//	Removed PrintedTicket setting.
//	document.op.action="setstatus.cfm?OrderId=" + sOrder + "&Status=PrintedTicket";
//	document.op.submit();
	oWin = window.open("invoiceshow.cfm?K=" + sOrder, "InvoiceWin", "");
	oWin.focus();
}

function doOrder(sColumn, sDir, sForm) {
	oForm = document.forms[sForm];

	oForm["SortOrder"].value = sColumn;
	oForm["SortDirection"].value = sDir;
	oForm.submit();
/*
	self.location.href = sForm + 'list_tb.cfm?orderby=' + sColumn + '&dir=' + sDir;
//	showWait(true);
	top.workspace[sForm + "list"].location.href = sForm + 'list.cfm?orderby=' + sColumn + '&dir=' + sDir;
*/
}

function doReopenOrder(sOrder) {
	mResult = confirm("Reopen Order\n\nThis will reopen the order. To continue, press OK. Press cancel to return.");
	if (mResult == true) {
		document.op.action="setstatus.cfm?OrderId=" + sOrder + "&Status=Reopened";
		document.op.submit();
	}
}

function doShipComplete(sOrder, sFormName) {
/*
	Automate the process of assigning the shipping quanties for
	an order in orderpreview.cfm.
	06/04/2005: Fixed a bug that caused the overwriting of the
				hidden shipped quantity field.
*/
	var iComplete = 0;
	var ii = 0;
	var oForm = document.forms[sFormName];
	if (! confirm("You are about to ship all remaining items in the order. Are you sure?")) {
		return false;
	}
	for (ii = 0; ii < oForm.elements.length; ii++) {
		var oE = oForm[ii];
//		alert(oE.name);
		if (oE.name) {
			//	Take the number remaining to be shipped.
			if (oE.name.substr(0, 6) == "ToShip") {
				iComplete = oE.value;
//				alert(oE.name + "=" + oE.value);
			}
			//	Fill the number into the form.
//			if (oE.name.substr(0, 2) == "S_") { //-- Bug in code.
			if (oE.name.substr(0, 2) == "S_" && oE.type == "text") {
//				alert(oE.name + "=" + oE.value);
				oE.value = iComplete;
//				alert(oE.name + "=" + oE.value);
			}
		}
	}

	doSubmitOrder(sOrder, sFormName);
}

function doShowShipping(sOrder) {
	var W = 550, H = 173;
	L = (screen.width - W) / 2;
	T = (screen.height - H) / 2;
	oWin = window.open("ordershipping.cfm?K=" + sOrder, "ShippingWin", "width=" + W + ",height=" + H + ",left=" + L + ",top=" + T + ",scrollbars=yes,status=no");
	oWin.focus();
}

function doSKUFilter() {
	var lstSKU = prompt("Set a list of SKU's to filter by. Use commas to separate SKU's.", "");
	var oFrame = top.frames["workspace"].orderlist;
//	alert(oFrame.location.href);
	var sHref = oFrame.location.href;
	//	Remove any SKU search string from the href value.
	iPos = sHref.indexOf("&");
	if (iPos >= 0) {
		sHref = sHref.substr(0, iPos);
	}
	alert(sHref);
	if (lstSKU == "") {
		oFrame.location.href = sHref;
	}
	else {
		oFrame.location.href = sHref + "&SKU=" + lstSKU;
	}
//	alert("SKU Filter");
}

function doSubmitEngrave() {
	document.op.action="toengraver.cfm";
	document.op.submit();
}

function doSubmitOrder(iOrder, sFormName) {
/*
	Determine whether this is a partial shipment and, if yes, confirm
	that this is intentional in orderpreview.cfm.
	06/04/2005: Fixed a bug that compared the quantity and shipped
				amount for all rows. It should only compare incomplete
				rows and then only with the remaining quantity to ship.
*/
	var bOk = true;
	var mBackItems = "";
	var oForm = document.forms["op"];
	var sToShip = oForm["ToShip"].value;
	var arToShip = sToShip.split(",");
	for (ii = 0; ii < TotalItems; ii++) {
		var oShipped = oForm["S_" + ItemsDisplayed[ii]];
		//	If the shipped field is hidden, then it's completed.
		if (oShipped.type == "text") {
//			mDiscrepancy = oForm["SQ_" + ItemsDisplayed[ii]].value - oShipped.value;
			mDiscrepancy = arToShip[ii] - oShipped.value;
//			alert(oForm["SQ_" + ItemsDisplayed[ii]].name + ":" + oForm["SQ_" + ItemsDisplayed[ii]].value); 
//			alert("arToShip[" + ii + "]" + ":" + arToShip[ii] + " = " + oShipped.name + ":" + oShipped.value); 
//			alert("mDiscrepancy" + ":" + mDiscrepancy);
			if (mDiscrepancy != 0) {
				mBackItems = mBackItems + mDiscrepancy + " out of " + oForm["SQ_" + ItemsDisplayed[ii]].value + " pcs for item '" + ItemsDisplayed[ii] + "' [" + oForm["SD_" + ItemsDisplayed[ii]].value + "]\n";
			}
		}
	}
	if (mBackItems != "") {
		bOk = confirm("Shipping Discrepancy\n\nThe following items have not been shipped the amount ordered:\n\n" + mBackItems + "\nIf this is correct, press OK to continue. Press Cancel to make any required changes.");
	}
	if (bOk) {
		//	05/11/2005:	Allow for change of FormName when Finish button is pressed.
		if (arguments.length == 2) {
			oForm["FormName"].value = arguments[1];
		}
		oForm.action = "orderfinish.cfm?K=" + iOrder;
		oForm.submit();
	}
}

function FocusOn(sObject) {
	/*
		Created: 01/03/2004
	*/
	//	First set the focus to the window.
	window.focus();
	if (arguments.length == 0) {
		return;
	}
	//	Then set focus to the specified object.
	oObj = eval(sObject);
	oObj.focus();
}

function OpenPop(sURL, sTarget) {
/*
	Pop-up the URL in the target window.
	In general, the window is not resizable, but for certain types
	of window content, we want to allow it.
*/
	var W = 500;
	var H = 300;
	var L = 100;
	var T = 100;
	var sAttr = ",scrollbars=yes";
	if (sTarget == "_document" || sTarget == "_email" || sTarget == "_help") {
		W = 600;
		H = 400;
		T = 50;
		if (sTarget == "_document") {
			sAttr += ",menubar=yes";	//	To allow for printing.
		}
		sAttr += ",resizable=yes,status=yes";
	}
	if (sURL == "") {
		alert("The web address is blank.");
	}
	else {
		window.open(sURL, sTarget, "width=" + W + ",height=" + H + ",left=" + L + ",top=" + T + sAttr);
	}
	return false;
}

function selectRow(mT, sWindow) {
	var oWin = parent;
//	var oWinSub = oWin[sWindow];
	var sBGColor = "LightGrey";	// "highlight"
	var sFGColor = "Navy";	// "highlighttext"
	if (bOrders) {
		if (document.getElementById) {
			if (mT.mSelected == null) {
				if (prevSelRow) {
					prevSelRow.replaceNode(prevSelStyle);
				}

				prevSelStyle = mT.cloneNode(true);
				prevSelRow = mT;

				mT.style.background = sBGColor;
				mT.style.color = sFGColor;
				mT.mSelected = true;

				if (bSearchArchive) {
					oWinSub.loadArchive(mT.id);
				}
				else {
//					oWinSub.loadOrder(mT.id);
					window.open(sWindow + '.cfm?K=' + mT.id, sWindow);

//	alert(sWindow);
//	return true;
				}

				clearTimeout(timerID);
			}
		}
		else {
			//	This makes it work, without hightlighting
			//	in some other browsers.
			mT.style.background = sBGColor;
			mT.style.color = sFGColor;
			if (bSearchArchive) {
				oWinSub.loadArchive(mT.id);
			}
			else {
				oWinSub.loadOrder(mT.id);
			}
		}
	}
}

function showPopup(sMsg) {
	if (sMsg != "") {
		alert(sMsg);
	}
}

function showWait(bShow) {
	if (bShow == true) {
		var W = 200, H = 100;
		L = (screen.width - W) / 2;
		T = (screen.height - H) / 2;
		ww = window.open("showwait.cfm","WaitWin","height=" + H + ",left=" + L + ",scrollbars=yes,status=no,top=" + T + ",width=" + W);
	} else {
		if (ww) {
			ww.close();
		}
	}
}
/*
Change History:
01/28/2004:	Removed PrintedTicket setting from doInvoice.
02/15/2004:	Moved showPopup() into this script.
			Moved askDel() into this script.
04/09/2005:	Changed doOrder to work outside of frame.
			Renamed from beops.js.
04/30/2005:	Added new function - bSetChangedCheck.
			Added function CheckSome from msc_util.js
05/01/2005:	Added new function - CalcSalePrice.
05/21/2005:	Added new function - bSetChangedRadio.
01/17/2006:	Added new function - DittoSet.
			Added new function - DittoSetAll.
03/23/2007:	Added new function - DittoCopyAll.
			Added new function - DittoViewAll.
*/