var message, fieldtofocus, passed, problemMsg, creditCardMsg;

// Master function that checks all the pieces 
function isMailReady() {
	passed = true;  
	fieldtofocus = "";
	message ="Please review the following required fields: \n";
	problemMsg = "";
	creditCardMsg = "";
	
// Checks to see that something has been ordered
	var result = countQty();
		//alert("result is: " + result);
	if (result)  {
		passed = false;
		message += "- - Select at least one Gift Pack \n";
		fieldtofocus = document.orderinfo.GiftQty1;
	}
	
// Collects the billing information
// However, bypass this section if it is a repeat order
	if (document.orderinfo.skipBilling.value == "no")  {
		if (document.orderinfo.FirstName.value == "") {
    		message += "- - Billing First Name \n"; 
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.orderinfo.FirstName};
		}
	
		if (document.orderinfo.CustName.value.length < 2)  {
			message += "- - Billing Last Name less than 2 characters \n"; 
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.orderinfo.CustName};
		}
					
    	if (document.orderinfo.zAddr1.value == "") {
    		message += "- - Billing Address \n";
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.orderinfo.zAddr1};
    	}
	
    	if (document.orderinfo.zCity.value == "") {
    		message += "- - Billing City \n";
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.orderinfo.zCity};
		}
	
		if (document.orderinfo.zRegion.options[document.orderinfo.zRegion.selectedIndex].value == "none")  {
			message += "- - Billing State \n";
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.orderinfo.zRegion};
		}
	
		var element = document.orderinfo.zPostalCode.value;
		if (element.length < 5) {
    		message += "- - Billing Zip needs at least 5 characters \n";
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.orderinfo.zPostalCode};
		}
		else  {
			var result = isAllNumeric(element);
			if (result)  {
				message += "- - Billing Zip" + messageSub;
				passed = false;
				if (fieldtofocus == "")  {fieldtofocus = document.orderinfo.zPostalCode};
			}
		}
	
		var element = document.orderinfo.Email;
		var result = isEmail(element);
		if (result)  {
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.orderinfo.Email};
		}
	
		var element = document.orderinfo.zPhone1.value + document.orderinfo.zPhone2.value + document.orderinfo.zPhone3.value;
			//alert(element);
		if (element.length < 10)  {
			message += "- - Billing phone is missing some numbers \n";
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.orderinfo.zPhone1};
		}
		else {
			var result = isPhone(element);
			if (result)  {
				passed = false;
				if (fieldtofocus == "")  {fieldtofocus = document.orderinfo.zPhone1};
			}
		}
	
		var element = document.orderinfo.CcNum;
		var result = isCreditCard(element);
		if (result)  {
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.orderinfo.CcNum};
		}
	
		if (document.orderinfo.expmonth.options[document.orderinfo.expmonth.selectedIndex].value == "none")  {
			message += "- - Expiration month \n";
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.orderinfo.expmonth};
		}
	
		if (document.orderinfo.expyear.options[document.orderinfo.expyear.selectedIndex].value == "none")  {
			message += "- - Expiration year \n";
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.orderinfo.expyear};
		}
	}

	// Testing for credit card expiration date is correct
	var Today = new Date();
	var CcMonth = Today.getMonth() + 1;
	var CcYear = Today.getYear();
	var billMonth = parseInt(document.orderinfo.expmonth.options[document.orderinfo.expmonth.selectedIndex].value, 10);
	// NS and IE handle time differently so we futz around abit...
	var billYear = document.orderinfo.expyear.options[document.orderinfo.expyear.selectedIndex].value;
	billYear = billYear.substring(2);
	billYear = parseInt(billYear, 10);
	
	if (CcYear > 2000)  {
		CcYear = CcYear - 2000; }
	else  {
		CcYear = CcYear - 100  }
		
		//alert("billMonth: " + billMonth + "   billYear: " + billYear);
		//alert("CcMonth: " + CcMonth + "   CcYear: " + CcYear);
	var monthDiff = billMonth - CcMonth;
	var yearDiff = billYear - CcYear;
		//alert("monthDiff: " + monthDiff + "  yearDiff: " + yearDiff);
	if (yearDiff < 1)  {
		if (monthDiff < 0)  {
			creditCardMsg = "\n Credit Card Date has expired.";
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.orderinfo.expmonth};
		}
	}
// End of Billing information
	
// Collecting delivery information
		//alert("deltobill is: " + document.orderinfo.deltobill.checked);
	if (document.orderinfo.deltobill.checked == false)  {
	// begin of delivery same as billing
	
		if (document.orderinfo.ShiptoName.value == "") {
    		message += "- - Delivery Name \n";
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.orderinfo.ShiptoName};
		}
		
		if (document.orderinfo.yAddr1.value == "") {
    		message += "- - Delivery Address \n";
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.orderinfo.yAddr1};
		}
		
		if (document.orderinfo.yCity.value == "") {
    	message += "- - Delivery City \n";
		passed = false;
		if (fieldtofocus == "")  {fieldtofocus = document.orderinfo.yCity};
		}

		if (document.orderinfo.yRegion.options[document.orderinfo.yRegion.selectedIndex].value == "none")  {
			message += "- - Delivery State \n";
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.orderinfo.yRegion};
		}
		
		var element = document.orderinfo.yPostalCode.value;
		if (element.length < 5) {
    		message += "- - Delivery Zip needs at least 5 characters \n";
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.orderinfo.yPostalCode};
		}
		else  {
			var result = isAllNumeric(element);
			if (result)  {
				message += "- - Delivery Zip" + messageSub;
				passed = false;
				if (fieldtofocus == "")  {fieldtofocus = document.orderinfo.yPostalCode};
			}
		}

		var element = document.orderinfo.yPhone1.value + document.orderinfo.yPhone2.value + document.orderinfo.yPhone3.value;
			//alert(element);
		if (element.length != 0)  {
			if (element.length < 10)  {
				message += "- - Delivery phone is missing some numbers \n";
				passed = false;
				if (fieldtofocus == "")  {fieldtofocus = document.orderinfo.zPhone1};
			}
			else {
				var result = isPhone(element);
				if (result)  {
					passed = false;
					if (fieldtofocus == "")  {fieldtofocus = document.orderinfo.zPhone1};
				}
			}
		}
			//alert("Message length is: " + document.orderinfo.Comments.value.length);
		if (document.orderinfo.Comments.value.length > 251)  {
			message += "- - Please limit message to 250 characters. \n";
			passed = false;
			if (fieldtofocus == "")  {fieldtofocus = document.orderinfo.Comments};
		}
		
	}
	else {
		if (document.orderinfo.ShiptoName.value != "" || document.orderinfo.yAddr1.value != "" || document.orderinfo.yCity.value != "")  {
		problemMsg = "\n You have checked the 'Delivery same as billing' box and have entered information in the 'Delivery' section of this form. Please uncheck the 'Delivery same as billing' box to send the gift to another person.";
		passed = false;
		if (fieldtofocus == "")  {fieldtofocus = document.orderinfo.deltobill};
		}
	}
	 // end of delivery same as billing

	if (passed == true)  {
		document.orderinfo.submit();
	}
	if (passed == false)  {
		fixFieldInfo();
	}
}

function fixFieldInfo() {
	alert(message + creditCardMsg + problemMsg);
	fieldtofocus.focus();
}

// Look to see if anything has been ordered 
function countQty()  {
	var giftQty = 0;
	var giftCount = 0;
	var numberRecords = document.orderinfo.numRecords.value;
	
	for (iCounter=1; iCounter <= numberRecords; iCounter++)  {
		giftCountRow = "GiftQty"+iCounter;
		giftCount = eval("document.orderinfo." + giftCountRow).value;
		
		var giftString = giftCount.toString();
		for (var i = 0; i < giftString.length; i++)  {
			oneChar = giftString.charAt(i);
			if (oneChar < "0" || oneChar > "9") {
				message += "- - A non-digit is in a Gift Pack Qty box \n";
				return true;
			}
		}

		giftCount = parseFloat(giftCount);
		if (giftCount > 0)  {
			giftQty = giftQty + giftCount;
		}
	}
	
	if (giftQty == 0)  {
		return true;
	}
}
	
// Do some validation on the Email side of things
function isEmail(element)  {
	var inputStr = element.value;
	if (inputStr.length < 9)  {
		message += "- - Email should be at least 9 characters \n";
		return true;
	}	
	var charA = inputStr.indexOf("@");
	if (charA == -1)  {
		message += "- - Email did not find the @ character \n";
		return true;
	}
	if (charA < 2 )  {
		message += "- - Email expected at least 2 characters before the @ character \n";
		return true;
	}
	var charP = inputStr.lastIndexOf(".");
	if (charP == -1)  {
		message += "- - Email expected to find the . character \n";
		return true;
	}
	if (charP != inputStr.length - 3 && charP != inputStr.length - 4)  {
		message += "- - Email ending . character not in correct position \n";
		return true;
	}
	if (charP < charA + 3)  {
		message += "- - Email should have at least 2 characters between the @ and . characters \n";
		return true;
	}
	return false;
}

// Checks to see if the phone is somewhat correct
function isPhone(element)  {
	var oneChar;
	var inputStr = element;  //already a value
		//alert ("phonelength: " + inputStr.length);
	/*  -- removed when phone number swiched to three input boxes 10/2/02 --
	if (inputStr.length < 12)  {
		message += "- - Phone should have 10 digits and two dashes \n";
		return true;	
	}
	if (inputStr.charAt(3) != "-" || inputStr.charAt(7) != "-")  {
		message += "- - Phone needs dashes between numbers \n";
		return true;
	}
	*/
	for (var i = 0; i < inputStr.length; i++)  {
		oneChar = inputStr.charAt(i);
		if (oneChar < "0" || oneChar > "9") {
			if (oneChar != "(" && oneChar != ")" && oneChar != " " && oneChar != "-")  {
				message += "- - Phone should be only numbers \n";
				return true;
			}
		}
	}
	return false;
}

// Does some validity checking on the credit cards
function isCreditCard(element)  {
	var oneChar;
	var inputStr = element.value;
	if (inputStr.length != 16)  {
		message += "- - Credit Card should have 16 digits \n";
		return true;
	}
	for (var i = 0; i < inputStr.length; i++)  {
		oneChar = inputStr.charAt(i);
		if (oneChar < "0" || oneChar > "9") {
			message += "- - Credit Card must have only digits \n";
			return true;
		}
	}
	var cardType = inputStr.charAt(0);
	if (cardType == 3)  {
		message += "- - Sorry, but we do not accept American Express \n";
		return true;
	}
	if (cardType != 4 && cardType != 5 && cardType != 6)  {
		message += "- - Card type not recognized\n - - - Expecting Discover, Visa or Master Card \n";
		return true;
	}
	return false;	
}

// Checks to see if value is all numerics
function isAllNumeric(element)  {
	var oneChar;
	var inputStr = element;
	for (var i = 0; i < inputStr.length; i++)  {
		oneChar = inputStr.charAt(i);
		if (oneChar < "0" || oneChar > "9") {
			messageSub = " must only be numeric \n";
			return true;
		}
	}
}
