/* 
 * Brothers-All-Natural WebUtils Javascript Library
 *
 * Copyright (c) 2009 Brothers International Food Corporation
 *
*/

/* ************************************************************************************************
 * InitTabs - Initializes the tab controls
 *	FadeEffect: $("#fc-ban").tabs({ fx: { opacity: 'toggle', duration: 'fast'}});
 * ************************************************************************************************/
function InitTabs() {
	//- Dont use the tab-switching-fade-effect with IE, looks horrible
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
		$("#shopban").tabs();
	} else {
		$("#shopban").tabs({ fx: { opacity: 'toggle', duration: 'fast'}});
	}
}

/* ************************************************************************************************
 * InitNavAccordion - Set the active (open) container in the accordion based on the current page
 * ************************************************************************************************/
function InitNavAccordion() {
	var urlArray = new Array();
	urlArray = location.href.split("/");
	if ((urlArray[urlArray.length-1].search(/ShopFC/)) == 0) {
		$("#leftnav").accordion({ header: "h3", autoHeight: false, collapsible: true, active: 0 });
	} else if ((urlArray[urlArray.length-1].search(/ShopFO/)) == 0) {
		$("#leftnav").accordion({ header: "h3", autoHeight: false, collapsible: true, active: 1 });
	} else if ((urlArray[urlArray.length-1].search(/ShopPC/)) == 0) {
		$("#leftnav").accordion({ header: "h3", autoHeight: false, collapsible: true, active: 2 });
	} else if ((urlArray[urlArray.length-1].search(/ShopRetail/)) == 0) {
		$("#leftnav").accordion({ header: "h3", autoHeight: false, collapsible: true, active: 3 });
	} else {
		$("#leftnav").accordion({ header: "h3", autoHeight: false, collapsible: true, active: false });
	}
}

/* ************************************************************************************************
 * InitTextBoxes - Initialize text boxes that use the jQuery LabelOver text box hint method
 * ************************************************************************************************/
function InitTextBoxes() {
	$('login').labelOver('over');
	$('label.textboxhint').labelOver('over');
}

/* ************************************************************************************************
 * InitDialog - Inits anything to be using as jquery dialog
 * ************************************************************************************************/
function InitDialog() {
	$('#invoiceDialog').dialog({
		autoOpen: false,
		width: 670,
		position: 'top',
		buttons: { "Print": function() { $('#invoiceDialog').printElement({ pageTitle: 'Brothers-All-Natural Order Details' }); } }
	});
	
	$('#orderDetails').dialog({
		autoOpen: false,
		width: 670,
		position: 'top',
		buttons: { "Print": function() { $('#orderDetails').printElement({ pageTitle: 'Brothers-All-Natural Order Details' }); }}
	});

	$('#addrDialog').dialog({
		autoOpen: false,
		width: 500,
		height: 385,
		position: 'top',
		modal: true,
		title: 'Add/Edit Addresses'
	});
	
	$('#ccDialog').dialog({
		autoOpen: false,
		width: 440,
		height: 240,
		position: 'top',
		modal: true,
		title: 'Add/Edit Credit Card'
	});
	
	$('#confirmDialog').dialog({
		autoOpen: false,
		title: "Please confirm...",
		modal: true
	});
	
	$('#busyMsg').dialog({ autoOpen: false, title: 'Working...'});
	$('#genericMsg').dialog({ autoOpen: false, title: 'Brothers-All-Natural...'});
}

/* ************************************************************************************************
 * InitDatePickers
 * ************************************************************************************************/
function InitDatePickers() {

	//- Donation request form
	$('#donateStartDate').datepicker({minDate: +30});
}

/* ************************************************************************************************
 * InitValidation - Initializes forms using jQuery-Validate
 * ************************************************************************************************/
function InitValidation() {

	//- Manage Account: General Settings
	$('#frmAccountSettings').validate({
		rules: {
			FirstName: { required: true },
			LastName: { required: true },
			Email: { required: true, email: true }
		}
	});
	
	//- Manage Account: Change Password
	$('#frmChangePassword').validate({
		rules: {
			OldPassword: { required: true },
			Password: { required: true, minlength: 8, maxlength: 14 },
			DuplicatePassword: { required: true, minlength: 8, maxlength: 14, equalTo: "#Password" }
		}
	});
	
	//- Manage Account: Credit Cards
	$('#functAddEditCC').validate({
		rules: {
			Description: { required: true },
			Type: { required: true },
			Number: { creditcard2: function(){ return $('#Type').val(); } },
			ExpireMonth: { required: true },
			ExpireYear: { required: true }
		}
	});
	
	$('#editAddr').validate();
	$('#loginform').validate();

	//- Checkout: Create Account
	$('#createacctform').validate({
		rules: {
			createEmail: { required: true, email: true },
			createPW: { required: true, minlength: 8, maxlength: 14 } ,
			recreatePW: { required: true, equalTo: "#createPW", minlength: 8, maxlength: 14 }
		}
	});
	
	//- Checkout: Shipping & Billing
	$('#chkout-sb').validate();

	//- Unsubscribe form
	$('#frmUnsubscribe').validate({
		rules: {
			emailaddr: { required: true, email: true }
		}
	});
	
	//- Generic Add/Edit Address Form
	$('#frmAddEditAddr').validate({
		rules: {
			FirstName: { required: true },
			LastName: { required: true },
			Country: { required: true },
			AddressLine1: { required: true },
			City: { required: true },
			State: { required: true },
			Zip: { required: true },
			Phone: { required: true, digits: true },
			Email: { required: true, email: true }
		}
	});
	
	//- Generic Add/Edit Credit Card
	$('#frmAddEditCC').validate({
		rules: {
			Description: { required: true },
			Type: { required: true },
			Number: { creditcard2: function(){ return $('#Type').val(); } },
			ExpireMonth: { required: true },
			ExpireYear: { required: true }
		}
	});
	
	//*** Contacu Us Forms ***//
	//- Donation Request Form
	$('#frmContactUs1').validate({
		rules: {
			donateName: { required: true },
			donateEmail: { required: true, email: true },
			donatePhone: { required: true },
			donateCompany: { required: true },
			donateTaxID: { required: true },
			donateSite: { required: true },
			donateAddr: { required: true },
			donateStartDate: { required: true },
			donateAttend: { required: true },
			donateDescr: { required: true },
			donateProducts: { required: true },
			donateCompany: { required: true }
		}
	});
	
	//- Retail / Wholesale Opportunities
	$('#frmContactUs2').validate({
		rules: {
			retailName: { required: true },
			retailEmail: { required: true, email: true },
			retailPhone: { required: true },
			retailCompany: { required: true }
		}
	});
	
	//- On-Line Ordering Problems
	$('#frmContactUs4').validate({
		rules: {
			orderName: { required: true },
			orderEmail: { required: true, email: true }
		}
	});
	
	//- Password / Account Problems
	$('#frmContactUs5').validate({
		rules: {
			acctName: { required: true },
			acctEmail: { required: true, email: true }
		}
	});
	
	//- Other Topic
	$('#frmContactUs6').validate({
		rules: {
			otherName: { required: true },
			otherEmail: { required: true, email: true }
		}
	});
}

/* ************************************************************************************************
 * jsValidateForm - Validates page-base forms (replacing InitValidation)
 * ************************************************************************************************/
function jsValidateForm (option) {

	switch (option) {
		case "CreateRetailAcct":
			$('#createretailacct').validate({
				rules: {
					contactCompanyName: { required: true },
					contactFirstName: { required: true },
					contactLastName: { required: true },
					contactEmail: { required: true, email: true }
				}
			});
			break;
			
		case "PromoteAcct":
			$('#promoteacct').validate({
				rules: {
					promoteCompanyName: { required: true },
					promoteEmail: { required: true, email: true },
					promotePhone: { digits: true },
					promoteFax: { digits: true }
				}
			});
			break;
			
		case "DemoteAcct":
			$('#demoteacct').validate({
				rules: {
					promoteEmail: { required: true, email: true }
				}
			});
			break;
	}
}

/* ************************************************************************************************
 * jsValidateDialog - Validates pop-up dialog forms (replacing InitValidation)
 * ************************************************************************************************/
function jsValidateDialog (frmDialog) {
	switch (frmDialog) {
		case "frmAddrDialog":
			$('#frmAddrDialog').validate({
				rules: {
					FirstName: { required: true },
					LastName: { required: true },
					Country: { required: true },
					AddressLine1: { required: true },
					City: { required: true },
					State: { required: true },
					Zip: { required: true },
					Type: { required: true },
					Phone: { required: true, digits: true },
					Email: { required: true, email: true }
				}
			});
			break;
			
		case "frmCCDialog":
			$('#frmCCDialog').validate({
				rules: {
					Description: { required: true },
					Type: { required: true },
					Number: { creditcard2: function(){ return $('#Type').val(); } },
					ExpireMonth: { required: true },
					ExpireYear: { required: true }
				}
			});
			break;
	}
}

/* ************************************************************************************************
 * jsShowDialogForm - Used to call GetDialogForm to display model dialogs
 * ************************************************************************************************/
function jsShowDialogForm(dialogForm, optParam1, optParam2) {

	jsBusyMsg(1);	

	if (typeof optParam1 == "undefined") { optParam1 = 0; }
	if (typeof optParam2 == "undefined") { optParam2 = 0; }

	switch (dialogForm) {
	
		case "frmAddrDialog":
			$.post('../lib/GetDialogForm.php', { form: dialogForm, masterAcct: optParam1, editAcct: optParam2 }, function(data) {
				$('#addrDialog').html(data);
				$('#addrDialog').dialog('open');
				$('#FirstName').focus();
				jsBusyMsg(0);
			});
		break;
		
		case "frmCCDialog":
			$.post('../lib/GetDialogForm.php', { form: dialogForm, masterAcct: optParam1, editAcct: optParam2 }, function(data) {
				$('#ccDialog').html(data);
				$('#ccDialog').dialog('open');
				$('#Description').focus();
				jsBusyMsg(0);
			});
		break;
		
		case "frmConfirmDialog":
			$('#confirmDialog').html('<div class="pad-20t txt-center font-11">Are you <i><b><span class="color-red">sure</span></b></i> that you want to remove this record?</div>');
			$('#confirmDialog').dialog('option', 'buttons', {
				"Yes": function() { window.location.href = optParam1; },	
				"No": function() { $('#confirmDialog').dialog('close'); }
			});
			$('#confirmDialog').dialog('open');
			jsBusyMsg(0);
		break;
		
	}
}
 
/* ************************************************************************************************
 * jsShowHomePageSection - Called from the left navigation options on the home page
 * ************************************************************************************************/
function jsShowHomePageSection(page) {
	$('#home').hide();
	$('#fc').hide();
	$('#fo').hide();
	$('#disney').hide();
	$('#pc').hide();
	$('#os').hide();
	$('#fc').show();
	
	var hidepage = $('#fc');
	hidepage.hide();
		
	$(page).show();
}


/* ************************************************************************************************
 * jsPopUp - Launches a pop-up window using the provided parameters
 * ************************************************************************************************/
function jsPopUp(url,winWidth,winHeight) {
	options = 'width=' + winWidth + ',height=' + winHeight + ',location=no,menubars=no,titlebar=no,scrollbars=yes,status=no,resizable=yes';
	window.open(url, '', options);
}

/* ************************************************************************************************
 * jsPointer - changes the cursor to a pointer for mouseover events
 * ************************************************************************************************/
function jsPointerOver(divID) {
	var divName = "#" + divID;
	$(divName).addClass('pointer');
}

function jsPointerOut(divID) {
	var divName = "#" + divID;
	$(divName).removeClass('pointer');
}
 
/* ************************************************************************************************
 * scMouseOver/scMouseOut - Events for the Shopping Cart button in the left navigation area
 * ************************************************************************************************/
function scMouseOver() {
	$('#scbtn').addClass('pages-lnav-cart-mover');
};

function scMouseOut() {
	$('#scbtn').removeClass('pages-lnav-cart-mover');
};

/* ************************************************************************************************
 * ToggleIt - Used to toggle the show/hide state of a div using the EASING jQuery plugin
 * ************************************************************************************************/
function ToggleIt(divID,timer) {
	var div = document.getElementById(divID);
	var divName = "#" + div.id;
	if (document.getElementById(divID).style.display == 'block') {
		$(divName).slideUp(timer);
	} else {
		$(divName).slideDown(timer);
	}
};

/* ************************************************************************************************
 * ShowIt - Forces a div to show using the EASING jQuery plugin
 * ************************************************************************************************/
function ShowIt(divID,timer) {
	var div = document.getElementById(divID);
	var divName = "#" + div.id;
	if (document.getElementById(divID).style.display !== 'block') {
		$(divName).slideDown(timer);
	}
}

/* ************************************************************************************************
 * HideIt - Forces a div to hide using the EASING jQuery plugin
 * ************************************************************************************************/
function HideIt(divID,timer) {
	var div = document.getElementById(divID);
	var divName = "#" + div.id;
	if (document.getElementById(divID).style.display == 'block') {
		$(divName).slideUp(timer);
	}
}

/* ************************************************************************************************
 * jsVarietyShowHide - Show/Hide div's (no easing)
 * ************************************************************************************************/
function jsVarietyShowHide(vGroup,selected) {

	// Product description div to show
	var descrDivID = 'descr' + vGroup + selected;
	var descrDiv = document.getElementById(descrDivID);
	var descrDivName = '#' + descrDiv.id;
	
	// Hide all product description div's
	for(var i=0; i<=5; i++) {
		strI = i+'';
		var divHide = '#' + 'descr' + vGroup + strI;
		$(divHide).hide();
	}
	
	// Show the selected product div
	$(descrDivName).show();
}

/* ************************************************************************************************
 * jsDisplaySwitcher - Used to toggle images/text for the display shopping section
 * ************************************************************************************************/
function jsDisplaySwitcher(categoryCode,toShow) {

	// Hide all product description and image div's
	for(var i=0; i<=8; i++) {
		strI = i+'';
		var divHideDescr = '#' + 'descr' + categoryCode + strI;
		var divHideImg = '#' + 'img' + categoryCode + strI;
		$(divHideDescr).hide();
		$(divHideImg).hide();
	}

	// Show the selected image div
	var imgDivID = 'img' + categoryCode + toShow;
	var imgDiv = document.getElementById(imgDivID);
	var imgDivName = '#' + imgDiv.id;
	//$(imgDivName).show({ fx: { opacity: 'toggle', duration: 'fast'}});
	$(imgDivName).fadeIn(800);
	
	// Show the selected product div
	var descrDivID = 'descr' + categoryCode + toShow;
	var descrDiv = document.getElementById(descrDivID);
	var descrDivName = '#' + descrDiv.id;
	$(descrDivName).show();
}

/* ************************************************************************************************
 * jsInventoryFilterShowHide
 * ************************************************************************************************/
function jsInventoryFilterShowHide(selected) {

	// Hide the Flavor Divs
	for(var i=0; i<5; i++) {
		strI = i+'';
		var divHide = '#' + 'flavor' + strI;
		$(divHide).hide();
	}
	
	// Hide the Sub-Category Divs
	for(var i=0; i<5; i++) {
		strI = i+'';
		var divHide = '#' + 'subcat' + strI;
		$(divHide).hide();
	}
	
	// Convert 'selected' to a string
	var strSelected = selected+'';
	
	// Flavor
	var flavorDivID = 'flavor' + strSelected;
	var flavorDiv = document.getElementById(flavorDivID);
	var flavorDivName = '#' + flavorDiv.id;
	
	$(flavorDivName).show();
	
	// SubCat
	if (selected == 1) {
		var subcatDivID = 'subcat' + strSelected;
		var subcatDiv = document.getElementById(subcatDivID);
		var subcatDivName = '#' + subcatDiv.id;
		$(subcatDivName).show();
	}

}

/* ************************************************************************************************
 * jsReportParamSwitcher
 * ************************************************************************************************/
function jsReportParamSwitcher(divToShow) {

	//- Hide all of the divs
	for(var i=0; i<5; i++) {
		strI = i+'';
		var divHide = '#rpt' + strI;
		$(divHide).hide();
	}
	
	var divShow = '#' + divToShow;
	$(divShow).show();
}

/* ************************************************************************************************
 * jsManageRetailAcctSwitcher
 * ************************************************************************************************/
function jsManageRetailAcctSwitcher(option) {

	$('#retailTestContact').hide();	
	$('#retailContact').hide();
	$('#retailConfig').hide();
	$('#retailCreate').hide();
	$('#retailPromote').hide();
	$('#retailDemote').hide();
		
	switch (option) {
		case 1:
			$('#retailTestContact').show();
			break;
		case 2:
			break;
		case 3:
			$('#retailPromote').show();
			break;
		case 4:
			$('#retailDemote').show();
			break;
	}
}

/* ************************************************************************************************
 * jsContactUsSwitcher
 * ************************************************************************************************/
function jsContactUsSwitcher() {

	//- Hide all of the divs
	for(var i=0; i<=10; i++) {
		strI = i+'';
		var divHide = '#divContactUs' + strI;
		$(divHide).hide();
	}
	
	//- Show the requested form...
	if (document.getElementById('contactOptions').value != '0') {
		var divShow = '#divContactUs' + document.getElementById('contactOptions').value;
		$(divShow).show();
	}
	
}

/* ************************************************************************************************
 * jsCorpAcctInfoCopy
 * ************************************************************************************************/
function jsCorpAcctInfoCopy(option) {

	//- Options:
	//	1: Ship-To Company Copy
	//	2: Ship-To Contact Copy
	//	3: Bill-To Company Copy
	//	4: Bill-To Contact Copy
	//	5: Ship-Addr -> Bill-Addr Copy
	
	switch(option) {
		case 1:
			document.getElementById('shipFirstName').value = document.getElementById('contactCompanyName').value;
			document.getElementById('shipMiddleName').value = "";
			document.getElementById('shipLastName').value = "";
			break;
		case 2:
			document.getElementById('shipFirstName').value = document.getElementById('contactFirstName').value;
			document.getElementById('shipMiddleName').value = document.getElementById('contactMiddleName').value;
			document.getElementById('shipLastName').value = document.getElementById('contactLastName').value;
			break;
		case 3:
			document.getElementById('billFirstName').value = document.getElementById('contactCompanyName').value;
			document.getElementById('billMiddleName').value = "";
			document.getElementById('billLastName').value = "";
			break;
		case 4:
			document.getElementById('billFirstName').value = document.getElementById('contactFirstName').value;
			document.getElementById('billMiddleName').value = document.getElementById('contactMiddleName').value;			
			document.getElementById('billLastName').value = document.getElementById('contactLastName').value;
			break;
		case 5:
			document.getElementById('billAddr1').value = document.getElementById('shipAddr1').value;
			document.getElementById('billAddr2').value = document.getElementById('shipAddr2').value;
			document.getElementById('billCountry').value = document.getElementById('shipCountry').value;
			document.getElementById('billCity').value = document.getElementById('shipCity').value;
			document.getElementById('billState').value = document.getElementById('shipState').value;
			document.getElementById('billPostalCode').value = document.getElementById('shipPostalCode').value;
			break;
		case 6:
			if (document.getElementById('billToShip').checked == 1) {
				document.getElementById('billFirstName').disabled = true;
				document.getElementById('billMiddleName').disabled = true;
				document.getElementById('billLastName').disabled = true;
				document.getElementById('billAddr1').disabled = true;
				document.getElementById('billAddr2').disabled = true;
				document.getElementById('billCountry').disabled = true;
				document.getElementById('billCity').disabled = true;
				document.getElementById('billState').disabled = true;
				document.getElementById('billPostalCode').disabled = true;
			} else {
				document.getElementById('billFirstName').disabled = false;
				document.getElementById('billMiddleName').disabled = false;
				document.getElementById('billLastName').disabled = false;
				document.getElementById('billAddr1').disabled = false;
				document.getElementById('billAddr2').disabled = false;
				document.getElementById('billCountry').disabled = false;
				document.getElementById('billCity').disabled = false;
				document.getElementById('billState').disabled = false;
				document.getElementById('billPostalCode').disabled = false;
			}
	}

}

/* ************************************************************************************************
 * jsClearCart - Intermediate function trigging the shopping cart to be cleared
 * ************************************************************************************************/
function jsClearCart() {
	$.post('../lib/ShoppingCart-Relay', { clear_cart: 1 }, function(data) {
		$('#sc').html(data);
	});
}

/* ************************************************************************************************
 * jsRemoveFromCart - Intermediate function trigging a specific item to be removed from the shopping cart
 * ************************************************************************************************/
function jsRemoveFromCart(prodCode) {
	$.post('../lib/ShoppingCart-Relay', { remove_item: 1, pc: prodCode }, function(data) {
		$('#sc').html(data);
	});
}

/* ************************************************************************************************
 * jsRefreshCart - Internediate function trigging all line totals/quantities to be updated
 *                 based on the current QTY textbox values
 * ************************************************************************************************/
function jsRefreshCart() {
	// get a list of all input controls in the shopping cart
	var children = document.getElementById('sc').getElementsByTagName('input');
	// Cycle through the controls looking for the QTY boxes and launch the recalc/refresh
	for (var i=0; i <= children.length; i++){
		if (children[i].name.search(/_qty/) > 0) {
			var aryItemQty = children[i].name.split('_');
			$.post('../lib/ShoppingCart-Relay', { refresh_cart: 1, pc: aryItemQty[0], qty: children[i].value }, function(data) {
				$('#sc').html(data);			
			});
		}
	}
}

/* ************************************************************************************************
 * jsAddToCart - Basically overrides the submit action from the order pages, passes the values to 
 *               the shoppping cart module and updates the cart
 * ************************************************************************************************/
function jsAddToCart(sender, isCYO) {
	var addIt = true;

	// Init the sender form name
	var sendingForm = 'frm' + sender;

	// Get the quantity requested
	var orderQty = document.forms[sendingForm].elements[sender + 'qty'].value;
	
	// Get the number of radio buttons on the current form
	var radioLength = document.forms[sendingForm].elements['packsize'].length;

	// Cycle through the radio buttons to find the selected one and get the value
	for (var i=0; i < radioLength; i++) {
		if (document.forms[sendingForm].elements['packsize'][i].checked) {
			var selectedValue = document.forms[sendingForm].elements['packsize'][i].value;
		}
	}
	
	// Parse the product value field that contains all of the product information
	//   0 - Title
	//   1 - ProductItemCode
	//   2 - Price
	var valArray = new Array();
	valArray = selectedValue.split('|');

	// Chooose-Your-Own Variety pack stuff
	if (isCYO == 1) {
		var aryDropDown = valArray[1] + 'MultipleVarietyData[]';
		var varietyDataLength = document.forms[sendingForm].elements[aryDropDown].length;
		for (var i=0; i < varietyDataLength; i++) {
			if (i == 0) {
				mvdList = document.forms[sendingForm].elements[aryDropDown][i].value;
			} else {
				mvdList = mvdList + '|' + document.forms[sendingForm].elements[aryDropDown][i].value;
			}
			var idx = document.forms[sendingForm].elements[aryDropDown][i].selectedIndex;
			if (document.forms[sendingForm].elements[aryDropDown][i].options[idx].text.search(/stock/i) > 0) { addIt = false; }
		}
	} else {
		mvdList = "";
	}
	
	if (addIt == false) {
		jsShowMsg('Sorry, one or more of the Choose-Your-Own items selected is out of stock and cannot be added to your shopping cart.');
		//alert("Sorry, one or more of the choose-your-own items is out of stock and cannot be added to your shopping cart");
	} else {
		// Use the jQuery POST method to call ShoopingCart.php via the relay agent, set the POST values and refresh the cart info
		$.post('../lib/ShoppingCart-Relay.php',{ add_item: 1, title: valArray[0], pc: valArray[1], price: valArray[2], qty: orderQty, mvdList: mvdList },function(data){
			$('#sc').html(data);
			ShowIt('sc',500);
		});
	}
	
	// Return false to override the default submit action
	return false;
}

/* ************************************************************************************************
 * jsGetOrderDetails - Used in Manage Orders to print all order details in a queue
 * ************************************************************************************************/
 function jsGetOrderDetails (orderNumber, option, value) {
 
	// Options: 1-Order Number Search, 2: QueueName Search/Print
 
	jsBusyMsg(1);
	
	//- Make sure you have an empty div on the page called orderDetails or this wont work
	$.post('../lib/GetOrderDetails.php', { selectOption: option, selectValue: value } , function(data) {
		$('#orderDetails').html(data);
		if (orderNumber != 0) {
			$('#orderDetails').dialog('option', 'title' , 'Order Number ' + orderNumber);
			$('#orderDetails').dialog('open');
		} else {
			$('#orderDetails').printElement({ pageTitle: 'Brothers-All-Natural Order Details' }); 
		}
		jsBusyMsg(0);
	});

	return false;
 }
 
/* ************************************************************************************************
 * jsCaptureAndLabel - called from Manage Orders to capture and print shipping labels
 * ************************************************************************************************/
 function jsCaptureAndLabel(flagCapture, printOption) {

	jsBusyMsg(1);
 
	$.post('../lib/Shipping_CaptureAndLabel.php' , { Capture: flagCapture, printOption: printOption } , function(data) {
		//- The next 3 lines are production
		$('#orderLabel').hide();
		$('#orderLabel').html(data);
		$('#orderLabel').printElement({ pageTitle: 'Brothers-All-Natural Shipping Label' }); 
		
		//- The next 2 lines are for testing
		//$('#orderDetails').html(data);
		//$('#orderDetails').dialog('open');
		
		jsBusyMsg(0);
	});
	
	return false;
 }
 
/* ************************************************************************************************
 * jsSubmitOrder
 * ************************************************************************************************/
 function jsSubmitOrder(checkouttype, state) {
	jsBusyMsg(1,1);
	
	$.post('../lib/Checkout_SubmitOrder.php' , { CheckoutType: checkouttype, State: state } , function(data) {
		jsBusyMsg(0);
		window.location.href = "../pages/Checkout_Submitted.php?Result=" + data;
	});
	
	return false;
 }

/* ************************************************************************************************
 * jsPrintIt
 * ************************************************************************************************/
function jsPrintIt(divToPrint, docTitle) {
	var printDiv = document.getElementById(divToPrint);
	var printDivName = '#' + printDiv.id;
	$(printDivName).printElement({ pageTitle: docTitle });
}
 
/* ************************************************************************************************
 * jsBusyMsg
 * ************************************************************************************************/
 function jsBusyMsg(option, state) {
 
	if (typeof state == "undefined") { state = 0; }
	
	if (state > 0) { $('#busyMsg').dialog('option', 'modal',true); }
	
	if (option == 0) {
		$('#busyMsg').dialog('close');
	} else {
		$('#busyMsg').dialog('open');
	}
 }
 
/* ************************************************************************************************
 * jsShowMsg
 * ************************************************************************************************/
 function jsShowMsg(msg,style) {
	
	if (typeof style == "undefined") { style = 0; }
	
	switch (style) {
		case 0:
			$('#genericMsg').html('<div class="pad-10t font-11 color-blue txt-center"><p>' + msg + '</div>');
			break;
		
		case 1:
			$('#genericMsg').dialog('option', 'title', 'Order Error');
			$('#genericMsg').dialog('option', 'modal', true);
			$('#genericMsg').html('<div class="pad-5t font-11 color-red txt-center"><p>' + msg + '</div>');
			break;
		
	}
	$('#genericMsg').dialog('open');
 }

 /* ************************************************************************************************
 * jsDebug
 * ************************************************************************************************/
 function jsDebug(option) {
	alert(option);
 }
 
