/*
*************************************************
	list.js - v2.0.1

 	JavaScript functions for pages that use the
 	SDCProduct component
*************************************************

*** Note ***
Refresh or reopen browser if these scripts are updated; file is cached by browser!!

*** 01.08.29 [SJN] ***
	- Created this file, moved functions into here from list.asp

*** 01.11.02 [BWM] ***
	- Added a couple new functions
	- Cleanup
	
*** 01.11.04 [BWM] ***
	- Split out page-specific functions to psl.js and cart.js
	- v2.0
	
*** 01.11.15 [BWM] ***
	- Split psl_UpdateQuantity()
	- Improve error handling
*/

function goto_URL(object) {
	if (object) {
		self.location.href = object.options[object.selectedIndex].value;
	}

	return false;
}

function psl_BuyAll() {
	var oForm;
	var sArray = '';
	var iCount = 0;
	var iArCount = 0;

	if (document.frmCOMData.txtListLen) {
		iCount = document.frmCOMData.txtListLen.value;
	}

	if (iCount == 0) {
		alert('No items on this list can be added to your cart.');
	} else {
		for (var i = 0; i < iCount; i++) {
			if (eval('document.forms["frmProduct' + i + '"]')) {
				oForm = document.forms['frmProduct' + i];
				
				sArray += '||' + oForm.ProductID.value + '|' + oForm.Qty.value;
				iArCount++;
			}
		}
		
		if (sArray.length == 0) {
			alert('No items on this list can be added to your cart.');
		} else { 
			if (confirm('Click OK to add all of these items to your cart.')) {
				sArray = iArCount + sArray;
				
				//top.ShopCart.location.href = 'cart_display.asp?Action=Add'
										   + '&ProductArray=' + escape(sArray);
										   
				oForm = document.forms['frmBuyAll'];
				oForm.ItemsToAdd.value = sArray;
				oForm.submit();
				
			}
		}
	}
	
	return false;	
}

function checkNum(fldName) {
	var space = /[1234567890]/;
	var fldValue
	var lchar
	
	fldValue = eval(fldName).value;
	lchar = fldValue.charAt((fldValue.length) - 1);
	if(lchar.search(space) == -1) {
		eval(fldName).value = eval(fldName).value.substring(0, (eval(fldName).value.length) - 1);
	}
}

function psl_OnBuyImg(strBuy, strNum){
	var itemQty;
	itemQty = eval("document.frmProduct" + strNum + ".Qty").value;
	if (itemQty == "0"){
		if (strBuy == "frmDetail"){
			document[strBuy].src = "/images/btn/btn_buyCart.gif";
		}else{
			document[strBuy].src = "/images/btn/btn_Buy.gif";
		}
	}else{
		if (strBuy == "frmDetail"){
			document[strBuy].src = "/images/btn/btn_buyInCart.gif";
		}else{
			document[strBuy].src = "/images/btn/btn_inCart.gif";
		}
	}
	psl_OnBuy("frmProduct" + strNum);
}

function psl_OnBuy(strForm){
	var strURL;
	strURL = '/cart_display.asp';
	strURL = strURL + '?ProductID=' + eval('document.' + strForm + '.ProductID.value');
	strURL = strURL + '&Qty=' + eval('document.' + strForm + '.Qty.value');
	parent.ShopCart.location.replace(strURL);
}

function psl_BuyChecked(){
	var oForm;
	var sArray = '';
	var iCount = 0;
	var iCountChecked = 0;
	var formCounter = '';

	if(document.frmCOMData.length > 1){
		formCounter = document.frmCOMData.length;
		iCount = document.frmCOMData[formCounter-1].txtListLen.value;
	}else{
		if (document.frmCOMData.txtListLen){
			iCount = document.frmCOMData.txtListLen.value;
		}
	}

	for (var i = 0; i < iCount; i++) {
		if (eval('document.forms["frmProduct' + i + '"]')) {
			oForm = document.forms['frmProduct' + i];

			if (oForm.chkSelect) 
				if (oForm.chkSelect.checked)
				{
					sArray += '||' + oForm.ProductID.value + '|' + oForm.Qty.value;
					iCountChecked++;
				}
		}
	}

	if (iCountChecked == 0) {
		alert('No items are selected.');
	} else {
		sArray = iCountChecked + sArray;
		
		//top.ShopCart.location.href = 'cart_display.asp?Action=Add&'
								   + 'ProductArray=' + escape(sArray);
		
		oForm = document.forms['frmBuyAll'];
		oForm.ItemsToAdd.value = sArray;
		oForm.submit();
	}
	
	return false;
}

function psl_CheckAll(){
	var iCount = 0;
	if (document.frmCOMData.txtListLen){
		iCount = document.frmCOMData.txtListLen.value;
	}

	for (var i = 0; i < iCount; i++){
		if (eval('document.forms["frmProduct' + i + '"]')){
			if (document.forms['frmProduct' + i].chkSelect){
				document.forms['frmProduct' + i].chkSelect.checked = 1;
			}
		}
	}
}

function psl_CopyChecked() {
	var oForm;
	var sArray = '';
	var iCount = 0;
	var iCountChecked = 0;

	if (document.frmCOMData.txtListLen) {
		iCount = document.frmCOMData.txtListLen.value;
	}

	for (i = 0; i < iCount; i++) {
		if (eval('document.forms["frmProduct' + i + '"]')) {
			oForm = document.forms['frmProduct' + i];
		
			if (oForm.chkSelect) 
				if (oForm.chkSelect.checked) 
				{
					iCountChecked++;
					sArray += '||' + oForm.ProductID.value + '|' + oForm.Qty.value;
				}
		}
	}
	
	if (iCountChecked == 0) {
		alert('No items are selected.');
	} else {
		sArray = iCountChecked + sArray;
		psl_PopupOpen('list_popup.asp?action=addto&refer=list&ProductArray=' + escape(sArray));
	}

	return false;
}

function psl_DeleteList() {
	var oList;
	
	if (document.frmUDL.lstUDL) {
		oList = document.frmUDL.lstUDL;

		if (oList.selectedIndex == -1) {
			alert('Please select a custom shopping list to delete.');
		} else {
			var iListID = oList.options[oList.selectedIndex].value;
			var sListName = '';
			
			if (document.frmASPData.txtListName) {
				sListName = '"' + document.frmASPData.txtListName.value + '" ';
			}
							
			if (confirm('Click OK to delete your ' + sListName + 'list.')) { 
				window.location.href = 'list.asp?action=deletelist&listid=' + iListID; 
			}
		}
	}

	return false;
}

function psl_PopupList(i) {
	var oProduct;
	var sURL;
	
	if (eval('document.forms["frmProduct' + i + '"]')) {
		oProduct = document.forms['frmProduct' + i];
		
		sURL = 'list_popup.asp?action=addto'
			 + '&productid=' + oProduct.ProductID.value
			 + '&qty=' + oProduct.Qty.value;
			 
		psl_PopupOpen(sURL);
	}

	return false;
}

function psl_PopupList_Direct(productid, qty) {
	var wPopup;
	var sURL;
	
	sURL = 'list_popup.asp?action=addto&productid=' + productid + '&qty=' + qty;
	
	wPopup = window.open(sURL, 'listpopup', 'width=400,height=375,scrollbars=yes,resizable=no');
	wPopup.focus();
}

function psl_PopupNewList() {
	psl_PopupOpen('list_popup.asp?action=create&refer=list');

	return false;
}

function psl_PopupOpen(url) {
	var wPopup;
	
	wPopup = window.open(url, 'listpopup', 'width=400,Height=375,scrollbars=yes,resizable=no');
	wPopup.focus();

	return false;
}

function psl_PopupRenameList() {
	var oList;
	
	if (document.frmUDL.lstUDL) {
		oList = document.frmUDL.lstUDL;

		if (oList.selectedIndex == -1) {
			alert('Please select a custom shopping list to rename.');
		} else {
			var iListID = oList.options[oList.selectedIndex].value;

			psl_PopupOpen('list_popup.asp?action=rename&refer=list&listid=' + iListID);
		}
	}
	
	return false;	
}

function psl_QuickAdd_Clean(sQuickAdd) {
	// Remove spaces and comma combinations and junk entry, end up with
	// comma delimited numbers.  Goal is to handle any bizarre way of
	// keying entries.
	//
	// Example: ',, ,,,,  111,  222,,,333  ,  ,, 444,,, '
	// Becomes: '111,222,333,444'
	
	sQuickAdd = sQuickAdd.replace(/   /g, ' ');
	sQuickAdd = sQuickAdd.replace(/  /g, ' ');
	sQuickAdd = sQuickAdd.replace(/ /g, ' ');
	sQuickAdd = sQuickAdd.replace(/ , /g, ',');
	sQuickAdd = sQuickAdd.replace(/ ,/g, ',');
	sQuickAdd = sQuickAdd.replace(/, /g, ',');
	sQuickAdd = sQuickAdd.replace(/ /g, ',');
			
	// A few extra replaces to cleanup really bad comma entries.  Change
	// later to a do-loop with replaces.		
	sQuickAdd = sQuickAdd.replace(/,,,,/g, ',');	
	sQuickAdd = sQuickAdd.replace(/,,,/g, ',');	
	sQuickAdd = sQuickAdd.replace(/,,/g, ',');	
	sQuickAdd = sQuickAdd.replace(/,,/g, ',');
	sQuickAdd = sQuickAdd.replace(/,,/g, ',');

	// If first char is a delimiter, remove it, or the split function
	// adds a blank element.
	if (sQuickAdd.charAt(0) == ',') {
		sQuickAdd = sQuickAdd.substring(1, sQuickAdd.length);
	}

	// If last char is a delimiter, remove it, or the split function adds a blank element.
	if (sQuickAdd.charAt(sQuickAdd.length - 1) == ',') {
		sQuickAdd = sQuickAdd.substring(0, sQuickAdd.length - 1);
	}

	// Split into array to determine count
	var arQuickAdd = sQuickAdd.split(',');
			
	// Replace delimiter with pipes and quantity default
	sQuickAdd = sQuickAdd.replace(/,/g, '|1||');
	sQuickAdd = arQuickAdd.length + '||' + sQuickAdd + '|1';
			
	return sQuickAdd;
}

function psl_QuickAdd_Clear() {
	var oQuickAdd;
	
	if (document.frmQuickAdd.txtQuickAdd) {
		oQuickAdd = document.frmQuickAdd.txtQuickAdd;
	
		if (oQuickAdd.value == 'Enter item number(s)') {
			oQuickAdd.value = '';
		}
	}
		
	return false;
}

function psl_RefreshPDLMain() {
	if (document.frmPDL) {
		document.frmPDL.submit();
	}

	return false;
}

function psl_RefreshUDLMain() {
	// Intended only for list.asp page use, NOT list_popup.asp use

	if (document.frmUDL) {
		document.frmUDL.submit();
	}

	return false;
}

function psl_RefreshUDL_Parent() {
	// Intended only for list.asp page use, NOT list_popup.asp use

	if (document.frmUDL_Parent) {
		document.frmUDL_Parent.submit();
	}

	return false;
}

function psl_UnCheckAll() 
{
	var iCount = 0;

	if (document.frmCOMData.txtListLen) 
	{
		iCount = document.frmCOMData.txtListLen.value;
	}

	for (var i = 0; i < iCount; i++) 
	{
		if (eval('document.forms["frmProduct' + i + '"]')) 
		{
			if (document.forms['frmProduct' + i].chkSelect) 
			{
				document.forms['frmProduct' + i].chkSelect.checked = 0;
			}
		}
	}
}

function RemoveWindows() {
	// Flags window as being unloaded so any call-back popups don't error out
	// if this window has been closed before the popup gets closed.
	
	if (window.wPopup && window.wPopup.open && !window.wPopup.closed) {
		window.wPopup.opener = null;
	}
}

function ws_Popup(strType) {
	var strURL

	strURL = 'ws_popup.asp?MsgType=' + strType

	wWSPopup = window.open(strURL,'wspopup','width=600,Height=470,scrollbars=yes,resizable=no');
	wWSPopup.focus();
}

function cig_Popup(strType) {
	var strURL

	strURL = 'cig_popup.asp?MsgType=' + strType

	wWSPopup = window.open(strURL,'cigpopup','width=600,Height=470,scrollbars=yes,resizable=no');
	wWSPopup.focus();
}

function checkAll(pdtCount){
	var oForm;
	for (var i = 0; i < pdtCount; i++){
		if (eval('document.forms["frmProduct' + i + '"]')) {
			oForm = document.forms['frmProduct' + i];
			if (oForm.chkSelect){
				oForm.chkSelect.checked = 1;
			}
		}
	}
}

function uncheckAll(pdtCount){
	var oForm;
	for (var i = 0; i < pdtCount; i++){
		if (eval('document.forms["frmProduct' + i + '"]')) {
			oForm = document.forms['frmProduct' + i];
			if (oForm.chkSelect){
				oForm.chkSelect.checked = 0;
			}
		}
	}
}

function copyChecked(pdtCount) {
	var oForm;
	var sArray = '';
	var iCountChecked = 0;
	var newURL
	
	for (var i = 0; i < pdtCount; i++) {
		if (eval('document.forms["frmProduct' + i + '"]')) {
			oForm = document.forms['frmProduct' + i];
			if (oForm.chkSelect)
				if (oForm.chkSelect.checked){
					iCountChecked++;
					sArray += '||' + oForm.ProductID.value + '|' + oForm.Qty.value;
				}
		}
	}
	
	if (iCountChecked == 0) {
		alert('No items are selected.');
	} else {
		sArray = iCountChecked + sArray;
		newURL = '/list_popup.asp?action=addto&refer=list&ProductArray=' + escape(sArray);
		popUp(newURL,'400','374','0','0');
	}
}

function buyAll(pdtCount) {
	var oForm;
	var sArray = '';
	var iArCount = 0;

	if (pdtCount == 0) {
		alert('No items on this list can be added to your cart.');
	} else {
		for (var i = 0; i < pdtCount; i++) {
			if (eval('document.forms["frmProduct' + i + '"]')) {
				oForm = document.forms['frmProduct' + i];
				sArray += '||' + oForm.ProductID.value + '|' + oForm.Qty.value;
				iArCount++;
			}
		}
		
		if (sArray.length == 0) {
			alert('No items on this list can be added to your cart.');
		} else { 
			if (confirm('Click OK to add all of these items to your cart.')) {
				sArray = iArCount + sArray;
				oForm = document.forms['frmBuyAll'];
				oForm.ItemsToAdd.value = sArray;
				oForm.submit();
			}
		}
	}
}

function buyChecked(pdtCount){
	var oForm;
	var sArray = '';
	var iCountChecked = 0;

	for (var i = 0; i < pdtCount; i++){
		if (eval('document.forms["frmProduct' + i + '"]')) {
			oForm = document.forms['frmProduct' + i];
			if (oForm.chkSelect) 
				if (oForm.chkSelect.checked){
					sArray += '||' + oForm.ProductID.value + '|' + oForm.Qty.value;
					iCountChecked++;
				}
		}
	}

	if (iCountChecked == 0){
		alert('No items are selected.');
	}else{
		sArray = iCountChecked + sArray;
		oForm = document.forms['frmBuyAll'];
		oForm.ItemsToAdd.value = sArray;
		oForm.submit();
	}
}

function availAlert(reasonCd){
	if (reasonCd == "USDA"){
		alert("This product is made in Minnesota and has been\ncertified by the Minn. Dept. of Agriculture.\nBecause it contains meat, it must be certified by\nthe USDA for sale outside the state, a process that\nthis small, local company has not yet undertaken.\n\nWe apologize to our WI customers and hope this product\nwill be available to you soon.")
	}else if (reasonCd == "ZLIQ-1"){
		alert("We're sorry, but we are unable to deliver\nalcoholic products to your location.")
	}else if (reasonCd == "ZCIG-1"){
		alert("We're sorry, but we are unable to deliver\ntobacco products to your location.")
	}else if (reasonCd == "ZLIQ-2"){
		alert("We're sorry, but we are unable to deliver\nalcoholic products on your selected delivery date.\n\nIf you want to purchase this item, please\nselect a new delivery date and time.")
	}else if (reasonCd == "ZCIG-2"){
		alert("We're sorry, but we are unable to deliver\ntobacco products on your selected delivery date.\n\nIf you want to purchase this item, please\nselect a new delivery date and time.")
	}else if (reasonCd == "ZLIQ-4"){
		alert("We're sorry, but we are unable to deliver\nalcoholic products during your selected\ndelivery timeslot.\n\nIf you want to purchase this item, please\nselect a new delivery date and time.")
	}else if (reasonCd == "ZCIG-4"){
		alert("We're sorry, but we are unable to deliver\ntobacco products during your selected\ndelivery timeslot.\n\nIf you want to purchase this item, please\nselect a new delivery date and time.")
	}
}
