
window.onload = attachFormHandlers2;

var gShow2; //variable holding the id where feedback will be sent to.
var sUrl2 = "formvalidation2.php?validationtype2=ajax&val2=";//url is the page which will be processing all of the information.  it is important to make sure validationtype is ajax
var gErrors2 = 0; //number of errors is set to none to begin with
//var xmlHttp = getHTTPObject();//don't worry about this
var xmlHttp2;



function attachFormHandlers2()
{
	var form2 = document.getElementById('form2') 
	
	var objInput21 = document.getElementById('first2');
	objInput21.onblur = function(){return validateMe2(this);} 
	var objInput22 = document.getElementById('last2');
	objInput22.onblur = function(){return validateMe2(this);} 
	var objInput23 = document.getElementById('title2');
	objInput23.onblur = function(){return validateMe2(this);} 
	var objInput24 = document.getElementById('address2');
	objInput24.onblur = function(){return validateMe2(this);} 
	var objInput25 = document.getElementById('city2');
	objInput25.onblur = function(){return validateMe2(this);} 
	var objInput26 = document.getElementById('number2');
	objInput26.onblur = function(){return validateMe2(this);} 
	var objInput27 = document.getElementById('state2');
	objInput27.onblur = function(){return validateMe2(this);} 
	var objInput28 = document.getElementById('phone2');
	objInput28.onblur = function(){return validateMe2(this);} 
	var objInput29 = document.getElementById('hospital2');
	objInput29.onblur = function(){return validateMe2(this);} 
	var objInput20 = document.getElementById('email2');
	objInput20.onblur = function(){return validateMe2(this);}
	var objInput200 = document.getElementById('country2');
	objInput200.onblur = function(){return validateMe2(this);} 
	
	form2.onsubmit = function(){return validate2();} //attach validate() to the form
	
}

/*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 validateMe2(objInput2) {
    
	sVal2 = objInput2.value; //get value inside of input field
	
	sRules2 = objInput2.className.split(' '); // get all the rules from the input box classname
	sRequired2 = sRules2[1]; // determines if field is required or not
	sTypeCheck2 = sRules2[2]; //typecheck are additional validation rules (ie. email, phone, date)
    gShow2 = sRules2[3]; //gShow is the td id where feedback is sent to.
    
	xmlHttp2=GetXmlHttpObject2();
	if (xmlHttp2==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  } 
	//sends the rules and value to the asp page to be validated
	xmlHttp2.open("GET", sUrl2 + (sVal2) + "&sRequired2=" + (sRequired2) + "&sTypeCheck2=" + sTypeCheck2, true);
  
	xmlHttp2.onreadystatechange = handleHttpResponse2; 	// handle what to do with the feedback 
	xmlHttp2.send(null);  
}


function handleHttpResponse2() {
	//if the process is completed, decide to do with the returned data
	if (xmlHttp2.readyState == 4) 
  	{
		
   		sResults2 = xmlHttp2.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(gShow2).innerHTML = sResults2[0];
  	}
}


function validate2()
{
var tables2; 

tables2 = document.getElementsByTagName('td')

	for (i=0; i<tables2.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 (tables2[i].className == "rules2")
		{
			//if there is a thank you or its blank then it passes
			//if (tables2[i].innerHTML == '<img src="img/Checkmark2.png">' || tables2[i].innerHTML == '' )
			var pattern = /Checkmark2.png/;
			if (pattern.test(tables2[i].innerHTML) || tables2[i].innerHTML == '' )
			{
				tables2[i].style.color = '#000000';//the color is changed to black or stays black
			}
			else
			{
				gErrors2 = gErrors2 + 1; //the error count increases by 1
				tables2[i].style.color = '#ff0000';//error messages are changed to red
			}
		}
	}
		
	//doucment.write(gErrors);
	if (gErrors2 > 0)
	{
		//if there are any errors give a message
		alert ("Please make sure all fields are properly completed.  Errors are marked in red!");
		gErrors2 = 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 GetXmlHttpObject2()
{
var xmlHttp2=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp2=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp2=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp2=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp2;
} 


