/* Javascript support for Woodland Tree Foundation pages */

// $Id: $

// TODO: move the mailer functions into a separate file

/*
* Toggles the checkboxes for Board Members
* Uses the list of Board Member ids stored in the 'board' hidden field
* @param controlId - the id of the checkbox that the user clicks to toggle the Board checkboxes
*/
function boardCheck(controlId) {
	var boardVal = document.getElementById('board').value;
	boardVal = boardVal.split(",");
	var i;
	var checkboxId;
	for (i=0; i < boardVal.length; i++) {
		checkboxId = 'cb' + boardVal[i];
		var checkState = ""; 
		if (document.getElementById(controlId).checked) { 
			checkState = 'checked'; 
		} 
	
		document.getElementById(checkboxId).checked = checkState;
	}
}


/**
 * confirms that mailer form is ready to submit 
 */
function checkMailer() {
	var subject = document.getElementById('subject').value;
	if ((subject == undefined) || (subject == "")) {
		alert('You must have a subject');
		return false;
	}
	
	var message = document.getElementById('msgBody').value;
	if ((message == undefined) || (message == "")) {
		alert('You must have a message body');
		return false;
	}
	var cbLength = document.forms[0].send.length;
	var i;
	var validCb = false;
	for (i = 0; i < cbLength; i++) {
		if (document.forms[0].send[i].checked == true) {
			validCb = true;
		}
	}
	if (validCb == false) {
		alert('You must select at least one recipient');
		return false;
	}

	var sendOk = confirm('Are you sure you want to send this message to the selected recipients?\nSubject:' + subject + '\nMessage:' + message);
	
	if (sendOk) {
		return true;
	}
	else {
		alert('message cancelled');
		return false;
	}
	return false;
}


/**
 * sizes the menu and content divs to the same height
 * @param {} div1
 * @param {} div2
 * @return 
 * @type 
 */
function setDivs(div1, div2) {
	var oneHeight = document.getElementById(div1).offsetHeight;
	var twoHeight = document.getElementById(div2).offsetHeight;
	if (oneHeight > twoHeight) {
		document.getElementById(div2).style.height = oneHeight + 'px';
	}
	else {
		document.getElementById(div1).style.height = twoHeight + 'px';
	}
}


function setToPattern(target, pattern, overlay) {
	var patternHeight = document.getElementById(pattern).offsetHeight;
	var targetHeight = document.getElementById(target).offsetHeight;
	var overlayHeight = document.getElementById(overlay).offsetHeight;
	var newHeight = patternHeight /*- overlayHeight*/;
	newHeight = newHeight + 'px';
//	alert('patternHeight is ' + patternHeight + ' and target is ' + targetHeight + ' and overlay is ' + overlayHeight);
	document.getElementById(target).style.height = newHeight;
//	alert('Now  target is ' + document.getElementById(target).style.height);
}

/** 
 * create empty error string;
 */
var error='';


/**
 * Array of field names used for validation
 */

var signupIdList = new Array('fname','lname','email');

/**
 * validation check when form submitted
 * @param form - the form being validated
 * @param list - array of field ids to validate
 */
function checkForm(form, list) {
	var fieldName="";
	var fieldValue = "";
	var validString="";

	error = '';
	var i = 0;
	for (i = 0; i < list.length; i++) {  
		fieldId = document.getElementById(list[i]);
		fieldName = fieldId.name;
		fieldValue = fieldId.value;
		if ((form == 'signup') && (fieldName == 'lname')) checkName(fieldName,fieldValue);
		if ((form == 'signup') && (fieldName=='fname')) checkName(fieldName,fieldValue);
		if (fieldName=='email') validEmail(fieldValue)
	}
	
	if (error.length) {
		alert( error);
		subOk.value = 0;
		error = "";
		return false;
	}

	return true;
}



/**
 * Checks User name for invalid chars
 * @param fieldName - the name of the field being validated
 * @param inputVal - the value to validate
 */
function checkName(fieldName,inputVal){
	//*********************************************************************************/
	// following rgx_name works as follows: If there are multiple names, all names but the last are 
	// evaluated by the expression within the () parens.  These names can be separated by any of the specified punctuation
	// or  a space, but the only punctuation allowed at the end is a period. Single initials with or without periods are allowed.
	// typically, the last part of the expression (after the *) handle's the last name, but in a case like O'Neal or Herrera-Smith,
	// the first part of the name is validated by the parenthetical expression (including the ' or - ) and the rest of the name is 
	// handled by the rest of the expression.
	/***********************************************************************************/
	//rgx_name='([A-Za-z]+([\-\'\_\s]|\.\s))*[A-Za-z]+\.?'
	var rgx_name = '([A-Za-z]+([\-\'\_\s]|\.\s))*[A-Za-z]+\.?';
	eval('var validString=/^' + rgx_name + '$/');  
	//if match failed
	if (inputVal.search(validString) == -1) {
		error += "Incorrectly formatted  Name in the " + fieldName + " field. \n No punctuation allowed except for hyphens (-), apostrophes ('), periods(.) and underscores (_).\n "
	}
}



/**
 * Check for a valid e-mail address
 * @param email - the address to validate
 */ 
 function validEmail(email) {
 		var i = 0;
    	var invalidChars = " /:,;"
    	var loc_error = 0;	    	
    	if ((!email)||(email == '')) return;    // user left email field blank so nothing to validate
  
     //Check for invalid characters in email address
     for (i=0; i<invalidChars.length; i++) {
        badChar = invalidChars.charAt(i)
        if (email.indexOf(badChar,0) > -1) loc_error++
        }
  
     //Verify that email includes a "@"
     atPos = email.indexOf("@",1)
	  //exception for VMACS type addresses
		if (atPos == -1) { 
     		loc_error++;
     		 //save for use w/VMACS type email specifiers
	 		var validString = /^[a-z]{3,20}$/
	 		if (email.search(validString)== -1) {
				loc_error++;
			}
			else {
			// email address is a valid 'short VMACS type' address
				return;
			}
	 	}
  		else {
	     //Verify that there is only one "@"
	     if (email.indexOf("@",atPos+1) > -1) loc_error++
	  
	     //Check for a "." after the "@"
	     periodPos = email.indexOf(".",atPos)
	     if (periodPos == -1) loc_error++
	  
	     //Check for at least 2 characters after the "."
	     if (periodPos+3 > email.length) loc_error++
	     if (loc_error > 0) error += 'Email address is not formatted correctly\n';
		}
} //End function validEmail
   




