/* 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 text input has a value */
function hasText2(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 if a checkbox or radio button has been checked - edited message*/
function isChecked2(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 check the " + 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, strMessage) {
	var objForm = document.forms[strFormName];
	var strZip = objForm[strZipFieldName].value;

	if(!isInteger(strZip) || (strZip.length != 5 && strZip.length != 9)) {
		alert("Please enter a numeric 5 or 9 digit " + strMessage + ".");
		objForm[strZipFieldName].focus();
		return false;
	}
	return true;
}

/* this function checks that a text input contains a valid zip code based on the country selected
	NOTE: strCountryFieldName can be either the name of the dropdown or a string for the value of the country
*/
function isValidZip(strFormName, strZipFieldName, strCountryFieldName, strMessage) {
	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 or 9 digit " + strMessage + ".");
			objForm[strZipFieldName].focus();
			return false;
		}
	} else if(strCountry == "CA" || strCountry == "Canada") {
		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 " + strMessage + " (ex: T2N0E6).");
			objForm[strZipFieldName].focus();
			return false;
		}
	} else {
		if (!strZip.length) {
			alert("Please enter a valid " + strMessage + ".");
			objForm[strZipFieldName].focus();
			return false;
		}
	}
	return true;
}
/* this function opens a popup window */
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 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 isValidInteger(strFormName, strFieldName, strMessage) {
	var objForm = document.forms[strFormName];
	var strNumber = objForm[strFieldName].value;

	if(!isInteger(strNumber)) {
		alert("Please enter a whole dollar " + strMessage + " (no commas, $, or decimal places).");
		objForm[strFieldName].focus();
		return false;
	}

	return true;
}

/* this function checks that a text input has been filled for a related radio button */
function radioHasText(strFormName, strRadioButtonName, strRadioButtonValue, strTextInputName, strMessage) {
	var objForm = document.forms[strFormName];
	var objTextInput = objForm[strTextInputName];
	var strSelectedRadioButtonValue = getRadioButton(strFormName, strRadioButtonName);
	var retVal = false;

	if(strSelectedRadioButtonValue ==  strRadioButtonValue && !objTextInput.value.length) {
		if(strMessage) alert("Please enter a value for the " + strMessage + ".");
		objTextInput.focus();
	} else retVal = true;

	return retVal;
}

/* this function returns the value of a selected radio button */
function getRadioButton (strFormName, strRadioButtonName) {
	var objForm = document.forms[strFormName];
	var objRadioButton = objForm[strRadioButtonName];

	for (var i = 0; i < objRadioButton.length; i++) {
		if (objRadioButton[i].checked) { return objRadioButton[i].value; }
	}
	
	return "";
}

function isZero (strValue) {
	var zeroExp = /^0+(\.0*)?$/;

	return zeroExp.test(strValue);
}

/* this function will check whether or not the donation amount is less than 10 */
function minAmount (strFormName, strFieldName){
	var objForm = document.forms[strFormName];
	var strNumber = objForm[strFieldName].value;
	
	if (strNumber < 10) {
		alert("We're sorry, but our system cannot process donations under $10.");	
		return true;
	}
	return false;
}
