function printError(fieldName,msg) {
	alert("MISSING VALUE:\n" + msg);
	fieldName.focus();
}

function isBlank(textField,msg) {
	if (textField.value == "") {
		printError(textField,msg);
		return true;
	} 
	else { return false; }
}

function isDate(textField,msg) {
	if (CheckDateField(textField)) {
		return true;
	} 
	else 
	{ printError(textField,msg); return false;  }
}

function optionNotSelected(optionField,msg) {
	if ((optionField.value == "blank") || (optionField.value == "0")) {
		printError(optionField,msg);
		return true;
	} 
	else { return false; }
}

function radioNotSelected(radioField,msg) {
	var selected;
	for (i=0; i<radioField.length; i++) {
		if (radioField[i].checked==true)
		selected=true;
	}	  
	if (selected == true) {
		return false;
	}
	else {
		printError(radioField[0],msg);
		return true;
	}   
}

function radioSelection(radioField) {
	var selected;
	for (i=0; i<radioField.length; i++) {
		if (radioField[i].checked==true)
			selected=radioField[i].value;
	}	  
	return selected;   
}

function checkPhoneNumber(phoneNum) {
	//check for zero or one open parenthesis \(?, followed by three digits \d{3}, 
	//followed by zero or one close parenthesis \)?, followed by one dash, forward slash,
	//or decimal point and when found, remember the character ([-\/\.]), followed by three digits \d{3},
	//followed by the remembered match of a dash, forward slash, or decimal point \1, followed by four digits \d{4}
	re = /\(?\d{3}\)?([-\/\.])\d{3}\1\d{4}/;
	ok = re.exec(phoneNum);
	if (!ok) {
	   window.alert (phoneNum +" isn't a phone number with area code.")
	}
}

function checkEmailAddress(emailAddress) {
}

function verify_customer_info(f) {   
	/*if (optionNotSelected(f.employee,"Employee")) return false;
	if (optionNotSelected(f.store,"Store")) return false;
	if (radioNotSelected(f.address_type,"Address Type")) return false;
	if (isBlank(f.ship_first_name,"First Name")) return false;
	if (isBlank(f.ship_last_name,"Last Name")) return false;
	if (isBlank(f.ship_address1,"Shipping Address")) return false;
	if (isBlank(f.ship_city,"City")) return false;
	if (optionNotSelected(f.ship_state,"State")) return false;
	if (isBlank(f.ship_zip,"Zip Code")) return false;
	if (isBlank(f.home_phone,"Home Phone Number")) return false;*/
	if (f.f_Email.value != f.Email_c.value) {
		printError(f.Email_c,"Email Addresses Don't Match");
		return false;
	}
	/*if (optionNotSelected(f.payment_type,"Payment Type")) return false;
	if (optionNotSelected(f.order_type,"Order Type")) return false;
	//check all the billing fields if the same as shipping checkbox isn't checked
	if (!f.same_as_shipping.checked) {
		if (isBlank(f.bill_first_name,"First Name")) return false;
		if (isBlank(f.bill_last_name,"Last Name")) return false;
		if (isBlank(f.bill_address1,"Billing Address")) return false;
		if (isBlank(f.bill_city,"City")) return false;
		if (optionNotSelected(f.bill_state,"State")) return false;
		if (isBlank(f.bill_zip,"Zip Code")) return false;
	}*/
	return true;
}

function verify_billing_info(f) {
	return true;
}

function verify_order_info(f) {   
	if (optionNotSelected(f.artist,"Artist")) return false;
	if (optionNotSelected(f.image,"Image")) return false;
	if (optionNotSelected(f.frame,"Frame")) return false;
	if ((f.frame.options[f.frame.selectedIndex].text == "Metal") || (f.frame.options[f.frame.selectedIndex].text == "Wood")) {
		if (optionNotSelected(f.description,"Description")) return false;
	}
	if (radioNotSelected(f.glazing,"Glazing Yes/No")) return false;
	if (radioSelection(f.glazing) == "yes") {
		if (radioNotSelected(f.glaze_type,"Glaze Type")) return false;
	}
	if (radioNotSelected(f.matting,"Matting Yes/No")) return false;
	if (radioSelection(f.matting) == "yes") {
		if (optionNotSelected(f.mat_num,"Matting: How Many")) return false;
		if (radioNotSelected(f.mat_to_image,"Mat to Image Yes/No")) return false;
	}
	if (isBlank(f.item_cost,"Item Cost")) return false;
	return true;
}

function verify_shipping_cost(f) {
	if (isBlank(f.ship_cost, "Shipping Cost")) return false;
	return true;
}

function verify_approval_info(f) {
	if (optionNotSelected(f.fulfill_loc,"Fulfillment Location")) return false;
	//if (isBlank(f.auth_code, "CC Authorization Code")) return false;
	return true;
}

function verify_payment_info(f) {
	if (isBlank(f.amount, "Amount")) return false;
	if (!CheckDateField(f.date.value))
	{
		alert("Enter date in mm/dd/yyyy format.");
		f.date.focus();
		return false;
	}
	return true;
}

function verify_shipping_info(f) {
	if (optionNotSelected(f.shipper, "Shipper")) return false;
	if ((f.shipper.options[f.shipper.selectedIndex].text == "FedEx") || (f.shipper.options[f.shipper.selectedIndex].text == "UPS")) {
		if (isBlank(f.tracking_num, "Tracking Number")) return false;
	}
	return true;
}

function somethingselected(oList, flag){
	var curform = oList.form; // get the containing form  
	if(flag==1)
	{
		clearCombo(curform.image); // clear the downstream list
	}
	else
	{
		clearCombo(curform.description); // clear the downstream list
	}
	
	var newvalue = oList.name + "=" + oList.options[oList.selectedIndex].value;
	if(flag==1)
	{
		fillCombo(curform.image, newvalue); // fill the downstream list
	}
	else
	{
		fillCombo(curform.description, newvalue); // fill the downstream list
	}
}

function clearCombo(oList){
	for (var i = oList.options.length - 1; i >= 0; i--){
		oList.options[i] = null;
	}
	oList.selectedIndex = -1;
}

function fillCombo(oList, vValue){
	if (vValue != "" && assocArray[vValue]){
		var arrX = assocArray[vValue];
	 	// Create Please Select as first entry in dropdown.	
		oList.options[oList.options.length] = new Option("Please Select", "blank");
		for (var i = 0; i < arrX.length; i = i + 2){
		 oList.options[oList.options.length] = new Option(arrX[i + 1], arrX[i]);
		}
	} 
	else oList.options[0] = new Option("None found", "");
}

function verifyDelete(itemID, action){
	var conStr = "Are you sure you want to delete this item?";
	if(confirm(conStr))
		window.location="processOrder.php?itemID="+itemID+"&action="+action;
}

function enableGlazingTypeChoices(radioField) {
	for (i=0; i<radioField.length; i++) {
		radioField[i].disabled==false;
	}	  
}

function CheckDateField (DateVal) {
	var c;
	var mon;
	var day;
	var year;
	if (DateVal.length != 10) //64
		return false;
	c = (DateVal.charAt(2))
	if (c != "/")
		return false;
	c = (DateVal.charAt(5))
	if (c != "/")
		return false;		
	mon = (DateVal.substring(0,2));
	day = (DateVal.substring(3,5));
	year = (DateVal.substring(6,10));
	if (isNaN(mon) || isNaN(day) || isNaN(year))
		return false;
	if (!isBetween(mon, 1, 12))
		return false;
	if (!CheckDayInMonth(mon, day, year))
		return false;
	if(year < 1900)
		return false;
	return true;
}

function CheckDayInMonth(mon, day, year) {
	if ((mon == 1) || (mon == 3) || (mon == 5) || (mon == 7) || (mon == 8) || (mon == 10) || (mon == 12)) {
		if (!isBetween(day, 1,31)){
			return false;
		}
	}
	if ((mon == 4) || (mon == 6) || (mon == 9) || (mon == 11))  {
		if (!isBetween(day, 1,30)){
			return false;
		}
	}
	if (mon == 2) {
		if (!isBetween(day, 1,29)){
			return false;
		}
	}
	return true;
}

function isBetween(val, limit1, limit2) {
	var max = (limit1 > limit2) ? limit1 : limit2;
	var min = (limit1 < limit2) ? limit1 : limit2;

	if (val == max || val == min) {
		return true;}
	if (val > min && val < max){
		return true}
	return false;
}
