var minQuantityArray = new Array;

function openPopupFull(url, height, width)
{
	var optionString = "resizable=yes,scrollbars=yes,location=yes,toolbar=yes,width=" + width + ",height=" + height;
	window.open(url, '', optionString);
}

function openPopupNone(url, height, width)
{
	var optionString = "resizable=yes,scrollbars=yes,location=no,toolbar=no,width=" + width + ",height=" + height;
	window.open(url, '', optionString);
}


// hide/display the div for choosing gift card options
function showGiftCardDiv(state) {
	if (state == 1) {
		document.getElementById('giftCardDiv').style.display = "block";
	} else if (state == 0) {
		document.getElementById('giftCardDiv').style.display = "none";	
	}

}

// hide/display the div for choosing customization options
function showCustomizeProductDiv(state) {
	if (state == 1) {
		document.getElementById('customizeProductDiv').style.display = "block";
	} else if (state == 0) {
		document.getElementById('customizeProductDiv').style.display = "none";	
	}

}

// check the product "add to bag" form for proper definition of gift card
function checkGiftCardOptions() {
	var errorMessage = "";

	if (document.info.giftCard[1].checked) // "yes" to gift card selected
	{
		var giftCardSelected = 0;

		if (document.info.giftCardID.length) {  // there is more than one option, so it is an array
			for (var i=0; i < document.info.giftCardID.length; i++) {
				if (document.info.giftCardID[i].checked) {
					giftCardSelected = 1;
					break;	
				}
			}
		} else {
			if (document.info.giftCardID.checked) {
				giftCardSelected = 1;
			}
		}
		
		if (!giftCardSelected) {
			errorMessage = errorMessage + "Please select a gift card style.\n";
		}
	}

	if (errorMessage != "") {
		alert(errorMessage);
		return false;
	}
	
	return true;
}

// check the product "add to bag" form for proper definition of customizations
function checkCustomizationOptions() {
	var errorMessage = "";

	if (document.info.customizeProduct) {
		if (document.info.customizeProduct[1].checked) // "yes" to customization selected
		{
			var customizeOptionSelected = 0;
			
			if (document.info.customizationID.length) {  // there is more than one option, so it is an array
				for (var i=0; i < document.info.customizationID.length; i++) {
					if (document.info.customizationID[i].checked) {
						customizeOptionSelected = 1;
						break;	
					}
				}
			} else {
				if (document.info.customizationID.checked) {
					customizeOptionSelected = 1;
				}
			}
			
			if (!customizeOptionSelected) {
				errorMessage = errorMessage + "Please specify a customization selection.\n";
			}
		}
		
		if (errorMessage != "") {
			alert(errorMessage);
			return false;
		}
	}	
	return true;
}

// determines which customization options to have disabled/enabled depending on what the selected quantity is
function customizationMinQuantities(passedQuantity) {
	var quantityValue = 0;
	

	if (!passedQuantity) {	// quantity is passed from the editCustomizationOptions page only
		quantityValue = document.info.quantity.value;
	} else { 
		quantityValue = passedQuantity;
	}
	
	if (document.info.customizationID.length) {  // there is more than one option, so it is an array 
		for (var i = 0; i < document.info.customizationID.length; i++) {
			if (quantityValue < minQuantityArray[i]) {
				document.info.customizationID[i].checked = false;
				document.info.customizationID[i].disabled = true;
			} else {
				document.info.customizationID[i].disabled = false;
			}
		}
	}
	else { // only one option, not an array
		if (quantityValue < minQuantityArray[0]) { 
			document.info.customizationID.checked = false;
			document.info.customizationID.disabled = true;
		} else { 
			document.info.customizationID.disabled = false;
		}
	}
}

function showInputs(type){
	if (type == 'T') {
		document.getElementById('logoInputDiv').style.display = "none";
		document.getElementById('textInputDiv').style.display = "block";
		document.getElementById('instructionsInputDiv').style.display = "block";
	} else if (type == 'LT') {
		document.getElementById('logoInputDiv').style.display = "block";
		document.getElementById('textInputDiv').style.display = "block";
		document.getElementById('instructionsInputDiv').style.display = "block";		
	}
}

// the error that is written out when updating the quantities in a cart and one quantity was 
// attempted to be changed below the minimum required for the selected customization option.
function errorDiv(product, minQ) { 
	document.write("<P>");
	document.write("<div id='errorUpdatingCartDiv'>");
	document.write("<div id='errorHeader'>Error Updating Cart</div>");
	document.write("<P>You cannot update the quantity of product '" + product + "' because the customization option you have selected for this product requires a minimum quantity of " + minQ + " products ordered.");
	document.write("<P>To lower the quantity, you must first change or remove the customization option.");
	document.write("<P><a href='cart.cfm'>OK</a>");
	document.write("</div>");
}

// rewrite of indexOf so that it ignores case
function IgnoreCaseIndexOf(mainString, str) { 
   var s1 = mainString.toLowerCase(); 
   var t1 = str.toLowerCase(); 
  
   return s1.indexOf(t1); 
}

// determine which payment option div to show
function showPaymentTypeDiv(theType) { 
	if (theType == 1) { 
		document.getElementById("creditCardDiv").style.display = "none";
	}
	else {
		document.getElementById("creditCardDiv").style.display = "block";
	}
}

function checkCheckoutInputs() {
	var errorMessage = "";

	if (document.info.paymentType) {
		if (!document.info.paymentType[0].checked && !document.info.paymentType[1].checked) {
			errorMessage = errorMessage + "Please select a payment type.\n";
		} else if (document.info.paymentType[0].checked) { // purchase order option checked.  Make sure it is filled out.
			//if (document.info.purchaseOrderNumber.value == "") {
			//	errorMessage = errorMessage + "Please enter a purchase order number.\n";	
			//}
		} else if (document.info.paymentType[1].checked) {  // credit card option checked.  Make sure fields are filled out.
			if (document.info.cardholderName.value == "") {
				errorMessage = errorMessage + "Please enter the credit card holder's name.\n";	
			}

			if (document.info.cardNumber.value == "") {
				errorMessage = errorMessage + "Please enter the credit card number.\n";	
			}
		}
	}

	
	if (errorMessage == "")
		return true;
	else {
		alert(errorMessage);
		return false;
	}
}