
// phone validation:   var phoneRE = /^\(\d\d\d\) \d\d\d-\d\d\d\d$/;
// phone validation:   var phoneRE = /^\(\d{3}\) \d{3}-\d{4}$/;

// SSN (proper format only) : ^\d{3}-\d{2}-\d{4}$
// SSN (with or without dashes) : ^(\d{3}-\d{2}-\d{4})|(\d{3}\d{2}\d{4})$

/*
 * hasData() : Data Validation
 * @param string, number - Make sure some type of data has been entered
 * @return boolean : True if data has been given. It is False otherwise.
 * 
 * @TESTED AND WORKING
*/

function hasData(data) {
	if (data === "") {
		return false;	
	} else {
		return true;	
	}
}


/*
 * isAlphaOnly() : Alpha Chars Validation
 * @param string : A string only
 * @return boolean : True if the string contains alphabetical chars only. It is False otherwise.
 * 
 * @TESTED AND WORKING
*/

function isAlphaOnly(data) {
	var regEx = /([0-9][^a-zA-Z])|([^a-zA-Z][0-9])$/;
	if (regEx.test(data)) {
		return false;	
	} else {
		return true;	
	}
}


/*
 * isNumericOnly() : Alpha Chars Validation
 * @param string : A string only
 * @return boolean : True if the string contains numeric chars only. It is False otherwise.
 * 
 * @TESTED AND WORKING
*/

function isNumericOnly(data) {
	var regEx = /([^0-9])$/;
	//var regEx = /([a-zA-Z][^0-9])|([^0-9][a-zA-Z])$/;
	if (regEx.test(data) || data == "") {
		return false;	
	} else {
		return true;	
	}
}

/*
 * isAlphaNumeric() : Alpha Chars Validation
 * @param string : A string only
 * @return boolean : True if the string contains numeric chars only. It is False otherwise.
 * 
 * @TESTED AND WORKING
*/

function isAlphaNumeric(data) {
//	var regEx = /^([^a-z]{1,})|([^A-Z]{1,})([^0-9]{1})$/;
	//var regEx = /([^a-zA-Z0-9])([a-zA-Z0-9*][^{0,3}])$/;
	var regEx = /^([\w]{3,})$/;
	
	if (regEx.test(data)) {
		return false;	
	} else {
		return true;	
	}
}


/*
 * isEmail() : Email Validation
 * @param string : A string only
 * @return boolean : True if the string contains an '@', '.', and between 2 to 4 chars after period. It is False otherwise.
 * 
 * @TESTED AND WORKING
*/

function isEmail(data) {
	var regEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!regEx.test(data)) {
		return false;	
	} else {
		return true;	
	}
}


/*
 * isPhoneNumer() : Email Validation
 * @param string : A string only
 * @return boolean : True if the string contains two '-', and proper phone format of ###-###-####. It is False otherwise.
 * 
 * @TESTED AND WORKING
*/

function isDashedPhoneNumber(data) {
	var regEx = /^([0-9]{3})+(\-)+([0-9]{3})+(\-)+([0-9]{4})$/;
	if (!regEx.test(data)) {
		return false;	
	} else {
		return true;	
	}
}

/*
 * isPhoneNumer() : Email Validation
 * @param string : A string only
 * @return boolean : True if the string contains two '.', and proper phone format of ###-###-####. It is False otherwise.
 * 
 * @TESTED AND WORKING
*/

function isDottedPhoneNumber(data) {
	var regEx = /^([0-9]{3})+(\.)+([0-9]{3})+(\.)+([0-9]{4})$/;
	if (!regEx.test(data)) {
		return false;	
	} else {
		return true;	
	}
}


/*
 * isRadioChecked() : Radio Button (Single or Group) Validation
 * @param string : A string only
 * @return boolean : True if selectedCount is equal to 1. It is False otherwise.
 * 
 * @TESTED AND WORKING
*/

function isRadioChecked(target) {
	var selectedCount = -1;
	
	for (var i=0; i<target.length; i++) {
		if (target[i].checked == true) {
			selectedCount = 1;
		}
	}
	
	if (selectedCount != 1) {
		return false;
	} else {
		return true;	
	}
}

/*
 * isBoxChecked() : Checkboxn (Single only) Validation
 * @param string : A string only - formName to validate
 * @param string : A string only - Checkbox in the formName to validate
 * @return boolean : True if the checkbox in the correct form is checked
 * 
 * @TESTED AND WORKING
*/

function isBoxChecked(target) {
    if (target.checked == false) {
		return false;
	} else {
		return true;	
	}
}

/*
 * dropdownHasValue(val) : Dropdown (Single only) Validation
 * @param string : A string only - drowpdown id to validate
 * @return boolean : True if the dropdown has a value and/or is not "null"
 * 
 * @TESTED AND WORKING
*/

function dropdownHasValue(target) {
	if (target.options[target.selectedIndex].value == null || target.options[target.selectedIndex].value == "null" || target.options[target.selectedIndex].value == "") {
		return false;
	} else {
		return true;	
	}
}

/*
 * isStringInCountRange(dataToCount, countToMatch) : Make sure the data has more than dataToCount 
 * @param string : A string only
 * @param array : A array only - a range [min, max] the char count must fall within or be equal too either min or max.
 * @return boolean : True if selectedCount is equal to 1. It is False otherwise
 * 
 * @TESTED AND WORKING
*/

function isStringInCountRange(dataToCount, numberRange) {
	var charCount = Number(dataToCount.length);
	
	if (charCount <= numberRange[0] || charCount >= numberRange[1]) {
		return false;
	} else {
		return true;
	}
}


/*
 * isNumInCountRange(dataToCount, numberRange) : Make sure the data has no more than the selected count 
 * @param tring/number : A string or number
 * @param array : A array only - a range [min, max] the number must fall within or be equal too either min or max.
 * @return boolean : True if selectedCount is equal to 1. It is False otherwise
 * 
 * @TESTED AND WORKING
*/

function isMaxLength(dataToCount, numberRange) {
	if (String(Number(dataToCount)).length != String(numberRange)) {
		return false	
	} else {
		return true;
	}
}

function isNumInCountRange(dataToCount, numberRange) {
	if (isNumericOnly(dataToCount)) {
		var charCount = String(Number(dataToCount)).length;
	} else {
		var charCount = Number(dataToCount.length);
	}
	
	if (Number(charCount) <= numberRange[0] || Number(charCount) >= numberRange[1]) {
		return false;
	} else {
		return true;
	}
}



/*
 * 
 * 
 * 
 * 
 * 
*/


var showGroupContents = function(target) { 
	if (document.getElementById(target).style.display == "block") {
		document.getElementById(target).style.display = "none";
	} else if (isBoxChecked(document.getElementById(target))) {
		document.getElementById(target).style.display = "block";
	}
}

var resetSelector = function(target, index) {
	document.getElementById(target).selectedIndex = index;
}

var clearFields = function(target, menu) {
	
	var clear = function(int) {
		document.getElementById("presentationDoc" + int).selectedIndex = 0;
		document.getElementById("presentationDesc" + int).value = "";
	}
	
	if (fieldsTotal != null) {
		if (fieldsRequested < fieldsTotal.length) {
			for (var i = 1; i < fieldsTotal.length; i++) { 
				if (!dropdownHasValue(document.getElementById(menu))) {
					clear(i);
				} else if (i > (fieldsRequested+1)) {
					clear(i);
				}
			}
		}
	}
}

var clearFields2 = function(target, menu) {
	
	var clear = function(int) {
		document.getElementById("seriesMonth" + int).selectedIndex = 0;
		document.getElementById("seriesDay" + int).selectedIndex = 0;
		document.getElementById("seriesYear" + int).selectedIndex = 0;
		document.getElementById("seriesTitle" + int).value = "";
	}
	
	if (fieldsTotal2 != null) {
		if (fieldsRequested2 < fieldsTotal2.length) {
			for (var i = 1; i < (fieldsTotal2.length+1); i++) {
				if (!dropdownHasValue(document.getElementById(menu))) {
					clear(i);
				} else if (i > (fieldsRequested2+1)) {
					clear(i);	
				}
			}
		}
	}
}

var fieldsObj = null;
var fieldsRequested = null;
var fieldsTotal;
var addFields = function(target, menu) {	

	fieldsObj       = document.getElementById(target);
	fieldsTotal     = fieldsObj.getElementsByTagName("fieldset");
	fieldsRequested = (document.getElementById(menu).selectedIndex - 1) // subtract 1 to account for first null field in menu;
	
	document.getElementById(target).style.display = "block";
	
	var displayType;
	for (var i = 0; i < fieldsTotal.length; i++) {
		if (i <= (fieldsRequested)) {
			displayType = "block";
		} else {
			displayType = "none";
		}
		fieldsTotal[i].style.display = displayType;
	}	
}

var fieldsObj2 = null;
var fieldsRequested2 = null;
var fieldsTotal2;
var addFields2 = function(target, menu) {	

	fieldsObj2       = document.getElementById(target);
	fieldsTotal2     = fieldsObj2.getElementsByTagName("fieldset");
	fieldsRequested2 = (document.getElementById(menu).selectedIndex - 1) // subtract 1 to account for first null field in menu;
	
	fieldsObj2.style.display = "block";
	
	var displayType2;
	for (var i = 0; i < fieldsTotal2.length; i++) {
		if (i <= (fieldsRequested2)) {
			displayType2 = "block";
		} else {
			displayType2 = "none";
		}
		fieldsTotal2[i].style.display = displayType2;
	}	
}

function validateWebinarRequestForm() {
    
	var errMsg = "";
	
	/*
	// Check: Requested Date
   	if (!dropdownHasValue(document.getElementById("requestedMonth")) ||
		!dropdownHasValue(document.getElementById("requestedDay")) ||
		!dropdownHasValue(document.getElementById("requestedYear"))) {
		 
		 errMsg += "Please provide your Requested Date" + "<br />";
	}
	
	// Check: Webminar Time T0 & From
   	if (!dropdownHasValue(document.getElementById("requestedStartHour")) ||
		!dropdownHasValue(document.getElementById("requestedStartMinute")) ||
		!dropdownHasValue(document.getElementById("requestedStartAMPM")) ||
		!dropdownHasValue(document.getElementById("requestedEndHour")) ||
		!dropdownHasValue(document.getElementById("requestedEndMinute")) ||
		!dropdownHasValue(document.getElementById("requestedEndAMPM"))) 
	{
	 	
		errMsg += "Please provide your Requested Time" + "<br />";
	}
	
	// Check: Webminar Alt Time T0 & From
	if(!dropdownHasValue(document.getElementById("requestedStartHour2")) ||
		!dropdownHasValue(document.getElementById("requestedStartMinute2")) ||
		!dropdownHasValue(document.getElementById("requestedStartAMPM2")) ||
		!dropdownHasValue(document.getElementById("requestedEndHour2")) ||
		!dropdownHasValue(document.getElementById("requestedEndMinute2")) ||
		!dropdownHasValue(document.getElementById("requestedEndAMPM2"))) 
	{
	 	
		errMsg += "Please provide your Requested Alternate Time" + "<br />";
	}
	
	// Check: Webminar Title
   	if (!hasData(document.getElementById("webinarTitle").value)) {
		 errMsg += "Please provide your Webinar Title" + "<br />";
	}
	
	// Check: Webminar Description
	if (!hasData(document.getElementById("webinarDescription").value)) {
		 errMsg += "Please provide your Webinar Description" + "<br />";
	}
	
	// Check: Webminar Target Audience
	if (!hasData(document.getElementById("webinarTargetAudience").value)) {
		 errMsg += "Please provide your Webinar Target Audience" + "<br />";
	}
	
	// Check: Requestor First Name
	if (!hasData(document.getElementById("requestorFirstName").value)) {
		 errMsg += "Please provide your First Name" + "<br />";
	}
	
	// Check: Requestor Last Name
	if (!hasData(document.getElementById("requestorLastName").value)) {
		 errMsg += "Please provide your Last Name" + "<br />";
	}
	
	// Check: Requestor Organization
	if (!hasData(document.getElementById("requestorOrganization").value)) {
		 errMsg += "Please provide your Oranization" + "<br />";
	}
	
	
	var validRequestorPhone = true;
	var validRequestorEmail = true;
	
	// Check: Requestor Phone
	if (!isNumericOnly(document.getElementById("requestorPhone").value) || 
		!isMaxLength(document.getElementById("requestorPhone").value, 3) ||
		!isNumericOnly(document.getElementById("requestorPhone2").value) || 
		!isMaxLength(document.getElementById("requestorPhone2").value, 3) ||
		!isNumericOnly(document.getElementById("requestorPhone3").value) || 
		!isMaxLength(document.getElementById("requestorPhone3").value, 4))
	{	
		validRequestorPhone = false;
	}
	
	// Check: Requestor Email
	if (!hasData(document.getElementById("requestorEmail").value) || !isEmail(document.getElementById("requestorEmail").value))
	{
		validRequestorEmail = false;
	}
	
	if(validRequestorPhone == false && validRequestorEmail == false)
	{
		errMsg += "Please provide your Phone Number or your E-mail address " + "<br />";
	}
	else if(!validRequestorPhone)
	{	
		errMsg += "Please provide your Phone Number" + "<br />";		
	}
	else if(!validRequestorEmail)
	{
		errMsg += "Please make sure your E-mail address that is properly formatted" + "<br />";
	}
	
	// Check: Requestor On Behalf Of For Validation
	if (isBoxChecked(document.getElementById("behalfOf"))) {
		
		// Check: On Behalf Of Name
		if (!hasData(document.getElementById("behalfName").value)) {
			 errMsg += "Please provide your the staff members Full Name" + "<br />";
		}
		
		// Check: On Behalf Of Organization
		if (!hasData(document.getElementById("behalfOrganization").value)) {
			 errMsg += "Please provide your the Organization" + "<br />";
		}
		// Check: On Behalf Of Phone
		if (!isNumericOnly(document.getElementById("behalfPhone").value) || 
			!isMaxLength(document.getElementById("behalfPhone").value, 3) ||
			!isNumericOnly(document.getElementById("behalfPhone2").value) || 
			!isMaxLength(document.getElementById("behalfPhone2").value, 3) ||
			!isNumericOnly(document.getElementById("behalfPhone3").value) || 
			!isMaxLength(document.getElementById("behalfPhone3").value, 4)) {
			
			errMsg += "Please provide the staff members Phone Number" + "<br />";
		}
		// Check: On Behalf Of Email
		if (!hasData(document.getElementById("behalfEmail").value) || !isEmail(document.getElementById("behalfEmail").value)) {
			 errMsg += "Please make sure the staff members E-mail address is properly formatted" + "<br />";
		}
	}
	
	// CHeck: Preregistration For Validation
	if (isBoxChecked(document.getElementById("preregistration"))) {
		// Check: Preregsration Date
		if (!dropdownHasValue(document.getElementById("preregisterMonth")) ||
			!dropdownHasValue(document.getElementById("preregisterDay")) ||
			!dropdownHasValue(document.getElementById("preregisterYear"))) {
			 
			 errMsg += "Please provide your Webinars Preregistration Date" + "<br />";
		}
	}
	*/
	
	// Check: Webinar Files Info
	/* 
	//JSG  9/1/2009 removed per clients request
	var presFilesCounter = 0;
	if (isBoxChecked(document.getElementById("presentation"))) {
		if (dropdownHasValue(document.getElementById("presentationFIleCount"))) {
			for (var i = 1; i < document.getElementById("presentationFIleCount").selectedIndex; i++) {
				if (!dropdownHasValue(document.getElementById("presentationDoc" + i)) || !hasData(document.getElementById("presentationDesc" + i).value)) {
					presFilesCounter++;
				}
			}
			if (presFilesCounter > 0) errMsg += "Please provide your Webinars File Information" + "<br />";
		}
	}
	
	*/
	/*
	// Check: Webinar Series Info
	var webSeriesCounter = 0;
	if (isBoxChecked(document.getElementById("series"))) {
		if (dropdownHasValue(document.getElementById("webinarSeriesCount"))) {
			for (var i = 1; i < (document.getElementById("webinarSeriesCount").selectedIndex+1); i++) {
				if (!dropdownHasValue(document.getElementById("seriesMonth" + i)) ||
					!dropdownHasValue(document.getElementById("seriesDay" + i)) ||
					!dropdownHasValue(document.getElementById("seriesYear" + i))) {
					webSeriesCounter++;
				}
			}
			if (webSeriesCounter > 0) errMsg += "Please provide your the information for your Recurring Seriess Presenations" + "<br />";
		}
	}
	*/
	
	if (!isBoxChecked(document.getElementById("termsAgreement"))) {
		errMsg += "You must agree to the Terms" + "<br />";							 
	}
	/*
	if (!hasData(document.getElementById("recaptcha_response_field").value) ) {
		errMsg += "Please enter the recaptcha words" + "<br />";							 
	}
	else if(!validateCaptcha()){
		errMsg += "Your captcha is incorrect. Please try again";
	}
	*/
	
	// Display the validation results
	if (errMsg) {
		jQueryModalFormError(errMsg + "&nbsp;<br />");
		//Recaptcha.reload();
		return false;
	} else {
		return true;
	}
}

function validateCaptcha()
{
    challengeField = $("input#recaptcha_challenge_field").val();
    responseField = $("input#recaptcha_response_field").val();
	
    var myResponseText = $.ajax({
    type: "POST",
    url: "/ajax/recaptcha.cfm",
	data: ({recaptcha_challenge_field : challengeField, recaptcha_response_field : responseField}),
    async: false
    }).responseText;
	
	myResponseText = jQuery.trim(myResponseText);
	
	//return false;
    if(myResponseText == "{valid:true}")
    {
        return true;
    }
    else
    {
        return false;
    }
}

function validateVideoConferenceRequestForm() {
    
	var errMsg = "";
	
	/*
	// Check: Requested Date
   	if (!dropdownHasValue(document.getElementById("requestedMonth")) ||
		!dropdownHasValue(document.getElementById("requestedDay")) ||
		!dropdownHasValue(document.getElementById("requestedYear"))) {
		 
		 errMsg += "Please provide your Requested Date" + "<br />";
	}
	
	// Check: Webminar Time T0 & From
   	if (!dropdownHasValue(document.getElementById("requestedStartHour")) ||
		!dropdownHasValue(document.getElementById("requestedStartMinute")) ||
		!dropdownHasValue(document.getElementById("requestedStartAMPM")) ||
		!dropdownHasValue(document.getElementById("requestedEndHour")) ||
		!dropdownHasValue(document.getElementById("requestedEndMinute")) ||
		!dropdownHasValue(document.getElementById("requestedEndAMPM"))) 
	{
	 	
		errMsg += "Please provide your Requested Time" + "<br />";
	}
	
	// Check: Webminar Alt Time T0 & From
	if(!dropdownHasValue(document.getElementById("requestedStartHour2")) ||
		!dropdownHasValue(document.getElementById("requestedStartMinute2")) ||
		!dropdownHasValue(document.getElementById("requestedStartAMPM2")) ||
		!dropdownHasValue(document.getElementById("requestedEndHour2")) ||
		!dropdownHasValue(document.getElementById("requestedEndMinute2")) ||
		!dropdownHasValue(document.getElementById("requestedEndAMPM2"))) 
	{
	 	
		errMsg += "Please provide your Requested Alternate Time" + "<br />";
	}
	
	// Check: Webminar Title
   	if (!hasData(document.getElementById("videoConferencingTitle").value)) {
		 errMsg += "Please provide your Video Conference Title" + "<br />";
	}
	
	
	// Check: Webminar Description
	if (!hasData(document.getElementById("videoConferencingDescription").value)) {
		 errMsg += "Please provide your Video Conference Description" + "<br />";
	}
	
	// Check: Webminar Target Audience
	if (!hasData(document.getElementById("videoConferencingTargetAudience").value)) {
		 errMsg += "Please provide your Video Conference Target Audience" + "<br />";
	}
	
	// Check: Requestor First Name
	if (!hasData(document.getElementById("requestorFirstName").value)) {
		 errMsg += "Please provide your First Name" + "<br />";
	}
	
	// Check: Requestor Last Name
	if (!hasData(document.getElementById("requestorLastName").value)) {
		 errMsg += "Please provide your Last Name" + "<br />";
	}
	
	// Check: Requestor Organization
	if (!hasData(document.getElementById("requestorOrganization").value)) {
		 errMsg += "Please provide your Oranization" + "<br />";
	}
	
	// Check: Requestor Phone
	if (!isNumericOnly(document.getElementById("requestorPhone").value) || 
		!isMaxLength(document.getElementById("requestorPhone").value, 3) ||
		!isNumericOnly(document.getElementById("requestorPhone2").value) || 
		!isMaxLength(document.getElementById("requestorPhone2").value, 3) ||
		!isNumericOnly(document.getElementById("requestorPhone3").value) || 
		!isMaxLength(document.getElementById("requestorPhone3").value, 4)) {
		
		errMsg += "Please provide your Phone Number" + "<br />";
	}
	
	// Check: Requestor Email
	if (!hasData(document.getElementById("requestorEmail").value) || !isEmail(document.getElementById("requestorEmail").value)) {
		 errMsg += "Please make sure your E-mail address that is properly formatted" + "<br />";
	}
	
	
	// Check: Requestor On Behalf Of For Validation
	if (isBoxChecked(document.getElementById("behalfOf"))) {
		
		// Check: On Behalf Of Name
		if (!hasData(document.getElementById("behalfName").value)) {
			 errMsg += "Please provide your the staff members Full Name" + "<br />";
		}
		
		// Check: On Behalf Of Organization
		if (!hasData(document.getElementById("behalfOrganization").value)) {
			 errMsg += "Please provide your the Organization" + "<br />";
		}
		// Check: On Behalf Of Phone
		if (!isNumericOnly(document.getElementById("behalfPhone").value) || 
			!isMaxLength(document.getElementById("behalfPhone").value, 3) ||
			!isNumericOnly(document.getElementById("behalfPhone2").value) || 
			!isMaxLength(document.getElementById("behalfPhone2").value, 3) ||
			!isNumericOnly(document.getElementById("behalfPhone3").value) || 
			!isMaxLength(document.getElementById("behalfPhone3").value, 4)) {
			
			errMsg += "Please provide the staff members Phone Number" + "<br />";
		}
		// Check: On Behalf Of Email
		if (!hasData(document.getElementById("behalfEmail").value) || !isEmail(document.getElementById("behalfEmail").value)) {
			 errMsg += "Please make sure the staff members E-mail address is properly formatted" + "<br />";
		}
	}
	*/
	
	/*	
	// Check: Video Conference Files Info
	var presFilesCounter = 0;
	if (isBoxChecked(document.getElementById("presentation"))) {
		if (dropdownHasValue(document.getElementById("presentationFIleCount"))) {
			for (var i = 1; i < document.getElementById("presentationFIleCount").selectedIndex; i++) {
				if (!dropdownHasValue(document.getElementById("presentationDoc" + i)) || !hasData(document.getElementById("presentationDesc" + i).value)) {
					presFilesCounter++;
				}
			}
			if (presFilesCounter > 0) errMsg += "Please provide your Video Conferences File Information" + "<br />";
		}
	}
	*/
	if (!isBoxChecked(document.getElementById("termsAgreement"))) {
		errMsg += "You must agree to the Terms to send this request." + "<br />";							 
	}
	
	/*
	if (!hasData(document.getElementById("recaptcha_response_field").value) ) {
		errMsg += "Please enter the recaptcha words" + "<br />";							 
	}
	else if(!validateCaptcha()){
		errMsg += "Your captcha is incorrect. Please try again";
	}
	*/
	
	// Display the validation results
	if (errMsg) {
		jQueryModalFormError(errMsg + "&nbsp;<br />");
		//Recaptcha.reload();
		return false;
	} else {
		return true;
	}
}