/* JavaScript trim() logic. */
// Removes leading whitespaces
function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

// Removes ending whitespaces
function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}

// Removes leading and ending whitespaces
function trim( value ) {
	return LTrim(RTrim(value));
}


//Autotab between the fields.
function autotab(original,destination){
	if (original.getAttribute && original.value.length==original.getAttribute("maxlength")){
		destination.focus();
	}
}

// JavaScript logic for checking alphabetic names
function checkName(receivedValue){
	var idealString="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
	for(var i=0;i<receivedValue.length;i++){
		var extractedChar=receivedValue.charAt(i);
		var matchingValue=idealString.indexOf(extractedChar);
		if(matchingValue=="-1")	{			
			return false;		
		}
	}
	return true;
}

// JavaScript logic for checking numbers
function checkNumber(receivedValue){
	var idealString="1234567890";
	for(var i=0;i<receivedValue.length;i++)	{
		var extractedChar=receivedValue.charAt(i);
		var matchingValue=idealString.indexOf(extractedChar);
		if(matchingValue=="-1"){			
			return false;		
		}
	}
	return true;
}

// JavaScript logic for checking email
function checkEmail(receivedValue){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(receivedValue))
		return true;
	else
		return false;
}

// JavaScript logic for selecting the checked radio button value.
function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
}

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends 

/**
 * JavaScript logic to reset form field values.
 */

/*function clearAll(){
      var frms = document.forms;
      for(var i = 0; i < frms.length; i++) { // for each form obj
            for(var j = 0; j < frms[i].length; j++) {
                  var input = frms[i][j]
                  inputType = input.type;
                  if(inputType == 'text') {
                        input.value = "";
                  }else if(inputType == 'radio'){
                        input.checked = false;
                  }else if(inputType == 'checkbox'){
                        input.checked = false;
                  }else if(inputType == 'select-one'){
                        input.selectedIndex = -1;
                  }else if(inputType == 'select-multiple'){
                        input.selectedIndex = -1;
                  }
            }
      }
}*/

function clearAll(form){
      var frms = document.forms;
      for(var i = 0; i < frms.length; i++) { // for each form obj
            for(var j = 0; j < frms[i].length; j++) {
                  var input = frms[i][j]
                  inputType = input.type;
                  if(inputType == 'text') {
                        input.value = "";
                  }else if(inputType == 'radio'){                        
                        if(form == "enrollment"){
                        	document.getElementsByName("maritalStatusGroup")[0].checked
                        	document.getElementsByName("contactGroup")[1].checked
                        }else{
                        	input.checked = false;
                        }
                  }else if(inputType == 'checkbox'){
                        input.checked = false;
                  }else if(inputType == 'select-one'){
                        input.selectedIndex = "";
                  }else if(inputType == 'select-multiple'){
                        input.selectedIndex = -1;
                  }
            }
      }
}

/**
* JavaScript logic to select a Radio Button or Check Box when its text is selected.
*/
function checkThis(field,index){
	document.getElementsByName(field)[index].checked=true;
}

/**
* JavaScript logic to get XMLHttpRequest object.
*/
function GetXmlHttpObject(){
	var req=null;
	try{    
		// Firefox, Opera 8.0+, Safari    
		req=new XMLHttpRequest();		
		if (req.overrideMimeType){
			req.overrideMimeType('text/xml');
		}
	}
	catch (e){    
		// Internet Explorer    
		try{      
			req=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e){      
			try{        
				req=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){        
				alert("Your browser does not support AJAX!");
				return false;        
			}      
		}    
	}
	return req;
}

/**
* JavaScript logic to determine the age.
*/

function checkAge(year,month,date){ 
var today = new Date();
var DOByear = year;
var DOBmonth = month;
var DOBdate = date;
var d =DOBmonth+"/"+DOBdate+"/"+DOByear;

d = d.split("/");
var birthYear = parseInt(d[2]); 
var currentYear = today.getFullYear();
if (birthYear >= currentYear || birthYear < 1900) {  // check valid year
return false;
}
var birthMonth = parseInt(d[0],10)-1;   // radix 10!
if (birthMonth <0 || birthMonth >11) {  // check valid month 0-11 
return false;
}
var birthDay = parseInt(d[1],10);   // radix 10!
var dim = daysInMonth(birthMonth+1,birthYear);
if (birthDay <1 || birthDay > dim) {  // check valid date according to month
return false;
}

var age = currentYear - birthYear;
var currentMonth = today.getMonth();
var currentDay = today.getDate();
if (birthMonth > currentMonth){
age = age - 1
}
// next birthday not yet reached

else if (birthMonth == currentMonth && currentDay < birthDay){
age = age - 1
}

//alert('You are ' + age + ' years old'); 
if (age < 18){
//alert ("You have to be 18 years old!");
return false;
}
}

function daysInMonth(month,year) {  // months are 1-12
var dd = new Date(year, month, 0);
return dd.getDate();
}