// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Project - Specific Client Functions
// ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

function Project_Tree_BeforeNodeSelected(oNode, iTreeProductType) {

	if (oNode.GetParentNode() == null && (parseInt(iTreeProductType) == 10032 || parseInt(iTreeProductType) == 10040))
		return false;
	return true;
}

function processOrders() {
	if (confirm('The selected orders will be set to "Processing" status (only orders in "Pending" status). Continue?'))
		return true;
	else {
		alert('Processing Cancelled');
		return false;
	}
}

function goCat(iCID) {
	document.location='/Category.aspx?iCID=' + iCID;
}

// Prevents bottom submit button from firing on mac/firefox (global)
var bSubmitFooter = true;

function SearchSubmit(sInput_ID) {

	var sSearch = document.getElementById(sInput_ID).value;
	
	if (sSearch.length < 3) {
		alert('Please enter a search criteria longer than 2 characters.')
		return false;
	} else if (sSearch == 'search') {
		alert('Please enter a search criteria.')
		return false;
	}
	document.location = '/ProductDisplay.aspx?iType=3&sSearch=' + sSearch;
}

function SearchEnter(e, oInput) {
	
	if (window.event) e = window.event;
	
	if (e.keyCode == 13) {
	
		SearchSubmit(oInput.id);
		e.cancelBubble = true;
		return false;
	}
	
	return true;
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function processOrders_OpenInvoices(sOrderString) {
		document.body.insertAdjacentHTML('beforeEnd', '<form target=_blank action="/Modules/Invoices.aspx" method=post>' +
		'<input type=hidden name="sOrderString" value="' + sOrderString + '"></form>');
		document.forms[document.forms.length - 1].submit();
}

function openInvoices(SQL, bAll) {
	var sPath = '/Modules/Invoices.aspx?sStatement=' + escape(SQL);
	if (bAll) sPath += '&bShowAll=1';
	winPopup(sPath, 700, 500, 50, 100);
}

function openPackingList(SQL) {
	winPopup('/Modules/Packing.aspx?sStatement=' + escape(SQL), 700, 500, 50, 100);
}

function openMailingLabels(SQL) {
	winPopup('/Modules/MailingLabels.aspx?sStatement=' + escape(SQL), 700, 500, 50, 100);
}

function openEmailSender(SQL) {
	winPopup('/Modules/EmailSender.aspx?sStatement=' + escape(SQL), 700, 500, 50, 100);
}

function winPopup(file, width, height, top, left) {	
	var str, oWin;
	str = ' width=' + width + ', height=' + height + ', top=' + top + ', left=' + left + ', resizable=1, scrollbars=1, toolbar=1, menubar=1';
	oWin = window.open(file, 'popup', str);
}

function CancelItem(sAction) {
	if (document.getElementById('OrderDetail_PID').value =='') { 
			alert('Please select an item first.'); 
			return false; 
	} else {
		if (confirm('This will ' + sAction + ' this item and credit the customer\'s account. Continue?')) {
			
			if (sAction == 'return') {
				var iReturnCount = parseInt(prompt('Select number of units to return'));
				if (iReturnCount > 0 && iReturnCount <= 10) {
					document.getElementById('OrderDetail_iReturnAmount').value = iReturnCount;
					return true;
				} else {
					alert('Please enter a number between 1 and 10');
					return false;	
				}
			} else
				return true;
		} else
			return false;
	}
}

function GiftCardAssign(iOrderID) {

	var iPID = document.getElementById('OrderDetail_PID').value;
	if (iPID =='') { 
			alert('Please select an item to assign the gift card to first.'); 
			return false; 
	} else {
	
		var iProceed = window.showModalDialog('/Modules/GiftCardAssigner.aspx?&iPID=' + iPID + '&iOrderID=' + iOrderID,
				"dialog_10046", 
				"dialogTop:250px; dialogLeft:250px; dialogHeight:200px; dialogWidth:500px; status:1; help:0; scroll:1");

		if (iProceed == -1) {
			top.document.location = '/UC/Login.aspx?sMsg=timeout'; 
			return false;
		} else if(iProceed > 0) {
			return true;
		} else
			return false;
		}
}

function paymentTerminal(iParentPID) {

	var iProceed = window.showModalDialog('/Modules/PaymentTerminal.aspx?&iOrderID=' + iParentPID,
				"dialog_10046", 
				"dialogTop:50px; dialogLeft:250px; dialogHeight:550px; dialogWidth:500px; status:1; help:0; scroll:1");

	if (iProceed == -1) {
		top.document.location = '/UC/Login.aspx?sMsg=timeout'; 
		return false;
	} else if(iProceed > 0) {
		return true;
	} else
		return false;
		
}

function modifyQuantity(iParentPID) {

	var iPID = document.getElementById('OrderDetail_PID').value;
	var iStatus = document.getElementById('OrderDetail_iStatus').value;
	
	if (iPID=='') { 
		alert('Please select an item first.'); 
		return false; 
	} else if (iStatus == '12036') {
		alert('This item has already been cancelled. You cannot modify its quantity.');
		return false;
	} else {
	
		if (confirm('Changing an item quantity will cause a credit/debit to the customer\'s account. Continue?')) {
		
			var iProceed = window.showModalDialog('Editor_Linked.aspx?sProductType=10005&iParentPID=' + iParentPID + '&iDialogDepth=1&iLinkType=1&bHideHeader=1&iPID=' + iPID + '&iAction=2',
				"dialog_10005", 
				"dialogTop:50px; dialogLeft:250px; dialogHeight:550px; dialogWidth:500px; status:0; help:0; scroll:1");

			if (iProceed == -1) {
				top.document.location = '/UC/Login.aspx?sMsg=timeout'; 
				return false;
			} else if(iProceed > 0) {
				return true;
			} else
				return false;
		} else return false;
	}
}

function addItem() {

	var iProceed = window.showModalDialog('Editor_Linked.aspx?sProductType=10007&iParentPID=0&iDialogDepth=1&iLinkType=2&sParentProductType=10004&bHideHeader=1&iPID=0&iAction=5',
			"dialog_10006", 
			"dialogTop:0px; dialogLeft:290px; dialogHeight:550px; dialogWidth:600px; status:0; help:0; scroll:1");

	if (iProceed == -1) {
		top.document.location = '/UC/Login.aspx?sMsg=timeout'; 
		return false;
	} else if(iProceed > 0) {
		document.getElementById('OrderDetail_PID').value = iProceed;
		return true;
	} else
		return false;
		
}

function OrderPID_Set(iPID) {
	if (document.getElementById('OrderDetail_PID')) {
		if (iPID != null && iPID != '')
			document.getElementById('OrderDetail_PID').value = iPID;
	}
}
function OrderStatus_Set(iStatus) {
	if (document.getElementById('OrderDetail_iStatus'))
		document.getElementById('OrderDetail_iStatus').value = iStatus;
}

	function ImageOver(oImage, iAction) {
		if (iAction ==0)
			oImage.style.borderColor = '#D3C3B3'
		else
			oImage.style.borderColor = '#A69787'
	}

