//By: Chris Campbell
//Created: May 20, 2005
//www.particletree.com

window.onload = attachFormHandlers;
var gShow; //variable holding the id where feedback will be sent to.
var sUrl = "validate/formvalidation.aspx?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; 
var http = getHTTPObject();

function attachFormHandlers()
{
    if (typeof(srtFormId) == 'undefined') return;
	//var form = document.getElementById(srtFormId);
    var form = document.forms[0];
	if (document.getElementsByTagName)//make sure we're on a newer browser
	{
		var objInput = document.getElementsByTagName('input');
		for (var iCounter=0; iCounter<objInput.length; iCounter++)
		objInput[iCounter].onblur = function(){return validateMe(this);} //attach the onchange to each input field
        
        var objInput = document.getElementsByTagName('textarea');
		for (var iCounter=0; iCounter<objInput.length; iCounter++)
		objInput[iCounter].onblur = function(){return validateMe(this);} //attach the onchange to each input field
		
		var objInput = 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
	}
	form.onsubmit = function(){return validate();} //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 validateMe(objInput) 
{
    document.getElementById('errormsg').innerHTML = "&nbsp;";
	sVal = objInput.value; //get value inside of input field
	sRules = objInput.className.split(' ');
	sValidate = sRules[0]; 
	sRequired = sRules[1]; 
	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.
	//sends the rules and value to the asp page to be validated
	
	if (sValidate == "validate")
	{
	    http.open("GET", sUrl + (sVal) + "&sRequired=" + (sRequired) + "&sTypeCheck=" + sTypeCheck, true);
	    http.onreadystatechange = handleHttpResponse; 	// handle what to do with the feedback 
	    http.send(null);  
	}
}
function handleHttpResponse() {
	//if the process is completed, decide to do with the returned data
	if (http.readyState == 4) 
  	{
  		sResults = http.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. 
      if(document.getElementById(gShow))
      {
  	    document.getElementById(gShow).innerHTML = "";
        document.getElementById(gShow).appendChild(document.createTextNode(sResults[0]));
      }
  	}
}
function validate()
{  
  var tags; 
  
   
  tags = document.getElementsByTagName('a')
	for (i=0; i<tags.length; i++)
	{
		if (tags[i].className == "error")
		{
			if (tags[i].innerHTML != '' && tags[i].innerHTML != ' ')
			{
				gErrors = gErrors + 1;
		  }
		}
	}
  for(i=0; i<arrRequired.length; i++)
  {
    if(document.getElementById(arrRequired[i]).value == "")
      gErrors = gErrors + 1;
  }
	if (gErrors > 0)
	{
    document.getElementById('errormsg').innerHTML = "Please make sure all fields are properly completed.";
		gErrors = 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;
}
//Adds code to the main article entry textarea
function addCode(strCode)
{
  document.getElementById('paragraph').value = document.getElementById('paragraph').value + strCode;
}