
window.onload = attachFormHandlers;

var gShow; //variable holding the id where feedback will be sent to.
var sUrl = "formvalidation.php?validationtype=ajax&val=";//url is the page which will be processing all of the information.  it is important to make sure validationtype is ajax
var gErrors = 0; //number of errors is set to none to begin with
//var xmlHttp = getHTTPObject();//don't worry about this
var xmlHttp;


function attachFormHandlers()
{ 
    var form = document.getElementById('form1')
	
	var objInput11 = document.getElementById('first');
	objInput11.onblur = function(){return validateMe(this);} 
	var objInput12 = document.getElementById('last');
	objInput12.onblur = function(){return validateMe(this);} 
	var objInput13 = document.getElementById('title');
	objInput13.onblur = function(){return validateMe(this);} 
	var objInput14 = document.getElementById('address');
	objInput14.onblur = function(){return validateMe(this);} 
	var objInput15 = document.getElementById('city');
	objInput15.onblur = function(){return validateMe(this);} 
	var objInput16 = document.getElementById('number');
	objInput16.onblur = function(){return validateMe(this);} 
	var objInput17 = document.getElementById('state');
	objInput17.onblur = function(){return validateMe(this);} 
	var objInput18 = document.getElementById('phone');
	objInput18.onblur = function(){return validateMe(this);} 
	var objInput19 = document.getElementById('hospital');
	objInput19.onblur = function(){return validateMe(this);} 
	var objInput10 = document.getElementById('email');
	objInput10.onblur = function(){return validateMe(this);} 
	/*if (document.getElementsByTagName)//make sure were on a newer browser
	{
		var objInput = document.getElementsByTagName('input');
		var objInput1 = document.getElementsByTagName('select');
		for (var iCounter=0; iCounter<objInput.length; iCounter++)
		objInput[iCounter].onblur = function(){return validateMe(this);} //attach the onchange to each input field
		for (var iCounter=0; iCounter<objInput1.length; iCounter++)
		objInput1[iCounter].onblur = function(){return validateMe(this);} //attach the onchange to each select field
	}  */
//	form.onsubmit = function(){return validate();}	
}




/*validateMe is the function called with onblur each time the user leaves the input box
passed into it is the value entered, the rules (which you could create your own), and the id of the area the results will show in*/
function validateMe(objInput) {
    
	sVal = objInput.value; //get value inside of input field
	
	sRules = objInput.className.split(' '); // get all the rules from the input box classname
	sRequired = sRules[1]; // determines if field is required or not
	sTypeCheck = sRules[2]; //typecheck are additional validation rules (ie. email, phone, date)
    gShow = sRules[3]; //gShow is the td id where feedback is sent to.
    
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  } 
	//sends the rules and value to the asp page to be validated
	xmlHttp.open("GET", sUrl + (sVal) + "&sRequired=" + (sRequired) + "&sTypeCheck=" + sTypeCheck, true);
	xmlHttp.onreadystatechange = handleHttpResponse; 	// handle what to do with the feedback 
	xmlHttp.send(null);  
}

function handleHttpResponse() {
	//if the process is completed, decide to do with the returned data
	if (xmlHttp.readyState == 4) 
  	{
		
   		sResults = xmlHttp.responseText.split(","); //results is now whatever the feedback from the asp page was
		//whatever the variable glo_show's (usermsg for example) innerHTML holds, is now whatever  was returned by the asp page. 
		document.getElementById(gShow).innerHTML = "";
		document.getElementById(gShow).innerHTML = sResults[0];
  	}
}


function validate()
{
var tables; 

tables = document.getElementsByTagName('td')
gErrors = 0;

	for (i=0; i<tables.length; i++)//loop through all the <td> elements 
	{
		// if the class name of that td element is rules check to see if there are error warnings
		if (tables[i].className == "rules")
		{
			//if there is a thank you or its blank then it passes
//			
			var pattern = /Checkmark2.png/;
			if (pattern.test(tables[i].innerHTML) || tables[i].innerHTML == '' )
			{
				tables[i].style.color = '#000000';//the color is changed to black or stays black
			}
			else
			{
				gErrors = gErrors + 1; //the error count increases by 1
				tables[i].style.color = '#ff0000';//error messages are changed to red
			}
		}
	}
	
	
	if (gErrors > 0)
	{
		//if there are any errors give a message
		alert ("Please make sure all fields are properly completed.  Errors are marked in red!");
		gErrors = 0;// reset errors to 0
		return false;
	}
	else return true;

}


/*function getHTTPObject() {
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
//	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
//		try 
//		{
//			xmlhttp = new XMLHttpRequest();
//		} catch (e) {
//		xmlhttp = false;
//		}
//	}
//	return xmlhttp;
//} 

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;
} 