/* this function checks if a text input has a value */
function hasText(strFormName, strFieldName, strMessage) {
	var objTextInput = document.forms[strFormName][strFieldName];
	var retVal = false;
	
	if(!objTextInput.value.length) {
		if(strMessage) alert("Please enter a value for the " + strMessage + ".");
		objTextInput.focus();
	} else retVal = true;

	return retVal;
}

/* this function checks if a dropdown has a selection
	an empty string for the option value indicates no selection
*/
function hasSelection(strFormName, strFieldName, strMessage) {
	var objSelectBoxInput = document.forms[strFormName][strFieldName];
	var retVal = false;
	
	if(!objSelectBoxInput[objSelectBoxInput.selectedIndex].value.length) {
		if(strMessage) alert("Please select a " + strMessage + ".");
		objSelectBoxInput.focus();
	} else retVal = true;
	
	return retVal;
}

/* this function checks if a checkbox or radio button has been checked */
function isChecked(strFormName, strFieldName, strMessage) {
	var objCheckboxInput = document.forms[strFormName][strFieldName];
	var retVal = false;

	if	(!objCheckboxInput.length) { objCheckboxInput.focus(); retVal = objCheckboxInput.checked; }
	else {
		for (var i = 0; i < objCheckboxInput.length; i++) {
			if (objCheckboxInput[i].checked) { retVal = true; break; }
		}
		objCheckboxInput[0].focus();
	}
	
	if (!retVal && strMessage) alert("Please select a " + strMessage + ".");
	return retVal;
}


/* this function checks that a text input contains a valid email address */
function isValidEmail(strFormName, strEmailFieldName) {
	var objForm = document.forms[strFormName];
	var strEmail = objForm[strEmailFieldName].value;

	if(!isEmail(strEmail)) {
		alert("Please enter a valid Email Address.");
		objForm[strEmailFieldName].focus();
		return false;
	}
	return true;
}

/* this function checks that a text input contains a valid zip code */
function isValidZip(strFormName, strZipFieldName, strCountryFieldName) {
	var objForm = document.forms[strFormName];
	var strZip = objForm[strZipFieldName].value;
	var objCountry = objForm[strCountryFieldName];

	/* some pages will not have the country dropdown */
	if (!objCountry) { var strCountry = strCountryFieldName; }
	else { var strCountry = objCountry[objCountry.selectedIndex].value; }

	if(strCountry == "US" || strCountry == "United States") {
		if(!isInteger(strZip) || (strZip.length != 5 && strZip.length != 9)) {
			alert("Please enter a numeric 5 digit Zip Code.");
			objForm[strZipFieldName].focus();
			return false;
		}
	} else if(strCountry == "CA" || strCountry == "Canada" || strCountry == "CANAD") {
		var zipExp = /^[A-Za-z][0-9][A-Za-z][0-9][A-Za-z][0-9]$/i;
		if (!zipExp.test(strZip)) {
			alert("Please enter a valid Zip Code (ex: T2N0E6).");
			objForm[strZipFieldName].focus();
			return false;
		}
	}
	return true;
}
/* this function checks that a text input contains a valid credit card number */
function isValidCCNumber(strFormName, strCCNumberFieldName, strMessage) {
	var objForm = document.forms[strFormName];
	var strCCNumber = objForm[strCCNumberFieldName].value;

	if(!isCCNumber(strCCNumber)) {
		alert("Please enter a valid "  + strMessage + ".");
		objForm[strCCNumberFieldName].focus();
		return false;
	}
	return true;
}

/* this function checks that two select box inputs contain a valid credit card expiration date */
function isValidCCExpiration(strFormName, strCCExpMonthFieldName, strCCExpYearFieldName, strMessage) {
	var objForm = document.forms[strFormName];
	var objCCExpMonth = objForm[strCCExpMonthFieldName];
	var objCCExpYear = objForm[strCCExpYearFieldName];
	var intCCExpMonth = parseInt(objCCExpMonth[objCCExpMonth.selectedIndex].value, 10);
	var intCCExpYear = parseInt(objCCExpYear[objCCExpYear.selectedIndex].value, 10);

	if(!isCCExpiration(intCCExpMonth, intCCExpYear)) {
		alert("Please select a valid "  + strMessage + ".");
		objCCExpMonth.focus();
		return false;
	}
	return true;
}

/* this function checks if a string is an integer */
function isInteger(strValue) {
	var intExp = /^[0-9]+$/;

	return intExp.test(strValue);
}

/* this function checks if a string is a valid email address */
function isEmail(strEmail) {
	var emailExp = /^([A-Za-z0-9][A-Za-z0-9_-]*\.)*([A-Za-z0-9][A-Za-z0-9_-]*)+@([A-Za-z0-9][A-Za-z0-9_-]*\.)+[A-Za-z]{2,4}$/i;

	return emailExp.test(strEmail);
}

/* this function checks if a string is a valid credit card number */
function isCCNumber(strCCNumber) {
	var i, n, c, r, t;

	// reverse the string
	r = "";	
	for (i = 0; i < strCCNumber.length; i++) {
    	c = strCCNumber.charAt(i);
		if (c >= 0 && c <= 9) r = c + r;
	}

	if (strCCNumber.length != r.length) return false;

	// Loop through the string. 
	// Double Even posions, Leave Odd posiotions alone
	t = "";
	for (i = 0; i < r.length; i++)	{
		c = parseInt(r.charAt(i), 10);
   		if (i % 2 != 0)	c *= 2;
		t = t + c;
	}

	// Add up all the single digits in this string.
	n = 0;
	for (i = 0; i < t.length; i++) {
		c = parseInt(t.charAt(i), 10);
		n = n + c;
	}

	// If the resulting sum is an even multiple of ten (but not zero), the
	// card number is good.
	if (n != 0 && n % 10 == 0) return true;
	else return false;
}

/* this function checks if a month and year combination form a valid credit card number expiration date */
function isCCExpiration(intMonth, intYear) {
	var dtToday = new Date();
	var intTodayMonth = dtToday.getMonth() + 1;
	var intTodayYear = dtToday.getFullYear().toString();
	if (intYear.toString().length != 4) intTodayYear = parseInt(intTodayYear.substr(2, 2), 10);
	else intTodayYear = parseInt(intTodayYear, 10);

	if (intYear < intTodayYear) return false;
	else if (intYear == intTodayYear && intMonth < intTodayMonth) return false;
	
	return true;
}

function openWindow(strURL, strWindowName, numWinWidth, numWinHeight) {
    var strSizeSpec;
	var objSearchWindow;
	strSizeSpec='toolbar=0,location=0,directories=0,left=10,top=10,status=no,menubar=0,scrollbars=1,resizable=yes,width='+numWinWidth+',height='+numWinHeight;

	objSearchWindow = window.open(strURL,strWindowName,strSizeSpec);
	objSearchWindow.focus();

	return;
}

/* this function checks if two fields are holding identical inputs (good for email and password verification */
function verify(strFormName, strFieldNameA, strFieldNameB, strMessage) {
	var objTextInput = document.forms[strFormName][strFieldNameA];
	var StrA = document.forms[strFormName][strFieldNameA].value;
	var StrB = document.forms[strFormName][strFieldNameB].value;
	
	if(StrA != StrB) {
		if (strMessage) 
			alert(strMessage + " were not matching. Please check your " + strMessage);
		objTextInput.focus();
	} else return true;	
	
	return false;
}

/* */
function isValidPassword(strFormName, strPasswordFieldName) {
	var objPassInput = document.forms[strFormName][strPasswordFieldName];
	
	if (objPassInput.value.length < 4 || objPassInput.value.length > 8) {
			alert("Password must be 4 to 8 characters in length.");
		objPassInput.focus();
	} else return true;
	
	return false;
}