var xmlHttp

function writeSessionAuto(object_name)
{ 
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="writesession_auto.php"
url=url+"?OBJECT_NAME="+object_name;
var value = document.getElementById(object_name).value;
url=url+"&OBJECT_VALUE="+value;
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("POST",url,true)
xmlHttp.send("")
}
function writeSessionHome(object_name,validation_type)
{ 
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="writesession_home.php"
url=url+"?OBJECT_NAME="+object_name;
var value = document.getElementById(object_name).value;
url=url+"&OBJECT_VALUE="+value;
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("POST",url,true)
xmlHttp.send("")
}
function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 	xmlDoc=xmlHttp.responseXML;
 	var sessiondata = "sessionid: "+xmlDoc.getElementsByTagName('sessionid')[0].childNodes[0].nodeValue;
 	sessiondata = sessiondata+" sessionname: "+xmlDoc.getElementsByTagName('sessionname')[0].childNodes[0].nodeValue;
 	sessiondata = sessiondata+" objectname: "+xmlDoc.getElementsByTagName('objectname')[0].childNodes[0].nodeValue;
 	sessiondata = sessiondata+" objectvalue: "+xmlDoc.getElementsByTagName('objectvalue')[0].childNodes[0].nodeValue;
 	sessiondata = sessiondata+" sessionvalue: "+xmlDoc.getElementsByTagName('sessionvalue')[0].childNodes[0].nodeValue;
 	/*alert('Session write info - '+sessiondata); this is here for debugging*/
 
 } 
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}
function checkEmail (strng) {
		var error="";
		if (strng == "") {
   		 error = "You didn't enter an email address.\n";
   		 return error;
		}
	var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       error = "Please enter a valid email address.\n";
    }else {
			 //test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          error = "The email address contains illegal characters.\n";
       }
    }
		return error;    
}


// phone number - strip out delimiters and check for 10 digits

function checkPhone (strng) {
		var error = "";
		if (strng == "") {
   		 error = "Phone number must be filled in.\n";
   		 return error;
		}

		var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
    	 error = "The phone number contains illegal characters.\n";
    }
    if (!(stripped.length == 10)) {
			 error = "The phone number is the wrong length. Make sure you included an area code.\n";
    } 
		return error;
}


// password - between 6-8 chars, uppercase, lowercase, and numeral

function checkPassword (strng) {
	 	var error = "";
	  if (strng == "") {
  	 		error = "You didn't enter a password.\n";
  	 		return error;
	  }
	  var illegalChars = /[\W_]/; // allow only letters and numbers
 
	  if ((strng.length < 6) || (strng.length > 8)) {
  	 		error = "The password is the wrong length.\n";
	  }else if (illegalChars.test(strng)) {
  	 			 error = "The password contains illegal characters.\n";
	  }else if (!((strng.search(/(a-z)+/)) && (strng.search(/(A-Z)+/)) && (strng.search(/(0-9)+/)))) {
 	 			 		error = "The password must contain at least one uppercase letter, one lowercase letter, and one numeral.\n";
	  }  
	  return error;    
}    


		 // username - 4-10 chars, uc, lc, and underscore only.

function checkUsername (strng) {
		 var error = "";
		 if (strng == "") {
   	 		error = "You didn't enter a username.\n";
		 }


     var illegalChars = /\W/; // allow letters, numbers, and underscores
     if ((strng.length < 4) || (strng.length > 10)) {
       	error = "The username is the wrong length.\n";
     }else if (illegalChars.test(strng)) {
     			 error = "The username contains illegal characters.";
    } 
		return error;
}       


// non-empty textbox

function isEmpty(strng) {
			var error = "";
  		if (strng.length == 0) {
     	 		error = "The mandatory text area has not been filled in.\n"
  		 }
			 return error;	  
}

// was textbox altered

function isDifferent(strng) {
		var error = ""; 
  	if (strng != "Can\'t touch this!") {
     	 error = "You altered the inviolate text area.\n";
  	}
		return error;
}

// exactly one radio button is chosen

function checkRadio(checkvalue) {
	 var error = "";
   if (!(checkvalue)) {
       error = "Please check a radio button.\n";
    }
		return error;
}

// valid selector from dropdown list

function checkDropdown(choice) {
		var error = "";
    if (choice == 0) {
    error = "You didn't choose an option from the drop-down list.\n";
    }    
		return error;
}
function isAllDigits(strng){
	var length = strng.length;
	var allDigits = true;
	for (a = 0; a < length; a++){
		var char = strng[a];
		if (isNaN(parseInt(strng))){
			allDigits = false;
			continue;
		}
		
	}
	return allDigits;
}
function zipOK(strng){
    var zip = strng;
    var error = '';
    var result = true;
    
    if (zip == ''){
    	return "Must enter a zip code.\n";
    }
    if (zip.length == 5){
        result = isAllDigits(zip);
        if (result == false){
            error = "Invalid character in zip code.\n";
            return error;
        }
     }else{
        	if (zip.length == 10){
        		var result = isAllDigits(zip.substring(0,5));
        		if (result == true){
            		if (zip.substring(5,6) != "-"){
                		result = false;
                	}else{
                			result = isAllDigits(zip.substring(6,10));
                	}
            	}
        		if (result == false){
            		error = "Invalid character in zip code.\n";
            	}
        		return error;
        	 } else{
        			error = "Invalid zip code.\n";
        			return error;
        	}	 
     }
     return error;
 }
 
var dtCh= "/";
var minYear= new Date().getFullYear()-100;
var maxYear= new Date().getFullYear()-17;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInContainer(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var dateArray = dtStr.split(dtCh);
	if (dateArray.length < 3){
		return "The date format should be : mm/dd/yyyy\n";
	}
	var strMonth=dateArray[0];
	var strDay=dateArray[1];
	var strYear=dateArray[2];
	if (isAllDigits(strMonth) =='' || isAllDigits(strDay) == '' || isAllDigits(strYear) == ''){
		return "All values in the date field must be numbers.\n";
	}
	var error = '';
	strYr=strYear
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (strMonth.length<1 || month<1 || month>12){
		error = "Please enter a valid month\n";
		return error
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		error = "Please enter a valid day\n";
		return error
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		error = "Please enter a valid 4 digit year between "+minYear+" and "+maxYear+"\n";
		return error
	}
return error
}

function checkBirthdate(strng){
	var dt=strng
	if (dt == ''){
		return "Must supply a birthdate.\n"
	}
	return isDate(dt);
	
 }
