//By: Chris Campbell
//Created: May 20, 2005
//Last Modified: June 27th, 2005
//www.particletree.com


window.onload = init;

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 http = GetXmlHttpObject();//don't worry about this

function init(){
  attachFormHandlers();
  setInitialVisibility();

}

function showHide(elementID, parentValue){
  if(parentValue=="no"){
   //document.getElementById(elementID).style.visibility="hidden";
   document.getElementById(elementID).style.display="none";
  }else{
   document.getElementById(elementID).style.display="block";
   //document.getElementById(elementID).style.visibility="visible";

  }
  //document.getElementById(otherName).style.border="1px solid #eee";

}

function setInitialVisibility(){
var form = document.getElementById('form1')

	if (document.getElementsByTagName)//make sure were on a newer browser
	{
		var objInput = document.getElementsByTagName('input');
		for (var iCounter=0; iCounter<objInput.length; iCounter++){
      if (objInput[iCounter].name.substring(0,4) == "text" || objInput[iCounter].name == "Submit" || objInput[iCounter].name == "Restart")
    {
    }else{
    sVal = objInput[iCounter].value; //get value inside of input field
	sVal = sVal.toString();
	if (objInput[iCounter].checked){
  sRules = objInput[iCounter].className.split(' '); // get all the rules from the input box classname

  for (var iiCounter=4; iiCounter<sRules.length; iiCounter++){
       //alert(sRules[iiCounter]);
       var ulName = "ul_"+sRules[iiCounter];
       showHide(ulName, sVal);


  }//end for dependancies loop
}//end if checked

   }//end if right type of input
  }//end for loop (all inputs)
  }//right type of browser

}//function

function attachFormHandlers()
{
	var form = document.getElementById('form1') 

	if (document.getElementsByTagName)//make sure were on a newer browser
	{
		var objInput = document.getElementsByTagName('input');
		for (var iCounter=0; iCounter<objInput.length; iCounter++)
		if (objInput[iCounter].name.substring(0,4) == "text")
    {
     //alert(objInput[iCounter].name);
     objInput[iCounter].onchange = function(){return validateMe(this);} //attach the onchange to each input field
     // objInput[iCounter].onfocus = function(){return validateMe(this);} //attach the onchange to each input field

     }else if(objInput[iCounter].name == "Submit" || objInput[iCounter].name == "Restart")
    {
     //alert(objInput[iCounter].name);
     //objInput[iCounter].onchange = function(){return validateMe(this);} //attach the onchange to each input field

     }else{
     objInput[iCounter].onfocus = 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].onchange = 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) {

	sVal = objInput.value; //get value inside of input field
	sVal = sVal.toString();
  //alert("in validate me with " + sVal);
	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.
  //alert("the rules are " +  sRules);
  for (var iCounter=4; iCounter<sRules.length; iCounter++){
    var ulName = "ul_"+sRules[iCounter];
      // alert("showhiding " + ulName);
       showHide(ulName, sVal);

  }


  //get surlyes&sreuired=notrequied&typecheck=none&gshow=newscmsmsg
	//sends the rules and value to the asp page to be validated
	//alert(sUrl + (sVal) + "&sRequired=" + (sRequired) + "&sTypeCheck=" + sTypeCheck + "&gShow=" + gShow);
	http.open("GET", sUrl + (sVal) + "&sRequired=" + (sRequired) + "&sTypeCheck=" + sTypeCheck + "&gShow=" + gShow, 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
      //alert(gShow);
    //whatever the variable glo_show's (usermsg for example) innerHTML holds, is now whatever  was returned by the asp page. 
    	document.getElementById(gShow).innerHTML = "";
      var sValues = new Array(); sValues[0]=""; sValues[1]="";  sValues[2]="";
     sValues = sResults[0].split(";;");
		document.getElementById(gShow).appendChild(document.createTextNode(sValues[0]));
     document.getElementById("pricingTable").innerHTML=(sValues[1]);
     //document.getElementById("QuoteParams").innerHTML=(sValues[2]);
     //document.getElementById("text_email").value="thisParam[0]";//(thisParam[1]).toString();

     updateForm(sValues[2]);
    }
    //http.readyState =0;
}

function updateForm(quoteParams){
  aQuoteParams = new Array();
  aQuoteParams = quoteParams.split(" ");
  //alert("in function");
  for (var iCounter=0; iCounter<aQuoteParams.length; iCounter++)
  {
    thisParam = aQuoteParams[iCounter].split(":");
    
    var element = document.getElementById(thisParam[0]);
    //alert (element);
    if(element){
     if (element.name == "Submit" || element.name == "Restart"){

     } else if (element.name == "sitetypes")
     { element.value=thisParam[1];//(thisParam[1]).toString();


    }else if (element.name.substring(0,4) == "text")
    {
     // alert(element.name + " " + thisParam[1]);
       element.value=thisParam[1];//(thisParam[1]).toString();
       /*sRules = element.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.
        */
        /*for (var iCounter=4; iCounter<sRules.length; iCounter++){
        var ulName = "ul_"+sRules[iCounter];

        showHide(ulName, sVal);

        }//end for*/

    }else{
     // alert(element.name + " " + thisParam[1]);
   //element.innerHTML=(thisParam[1]);
   if(thisParam[1]=="no"){
     document.getElementById(thisParam[0]+"2").checked=true;
     document.getElementById(thisParam[0]+"1").checked=false;
     
   }else if(thisParam[1]=="yes"){
     document.getElementById(thisParam[0]+"2").checked=false;
     document.getElementById(thisParam[0]+"1").checked=true;
   }
   sRules = document.getElementById(thisParam[0]+"2").className.split(' ');
   //for (var iCounter=4; iCounter<sRules.length; iCounter++){
   //     var ulName = "ul_"+sRules[iCounter];
        if(sRules[4]){
          //alert("updateing " + sRules[4]);
 //document.getElementById("text_email").value="ul_"+sRules[4];
         showHide("ul_"+sRules[4], thisParam[1]);
        }


    //    }

    }//end inner if//end inner if

    }//end if element
    /*if (document.getElementById(thisParam[0]).name.substring(0,4) == "text")
    {
    //just update the value directly
    document.getElementById(thisParam[0]).innerHTML=(thisParam[1]);
    }else if (document.getElementById(thisParam[0]).name == "Submit" || objInput[iCounter].name == "Restart")
    { //do nothing with these
    }else{
      //set to yes or no, find the relevant radio with this id for yes or no
      sVal = document.getElementById(thisParam[0]+"1").value; //get value inside of input field
	    sVal = sVal.toString();
	if (objInput[iCounter].checked){

  }
    }*/


    }

}


function validate()
{
var tables; 

tables = document.getElementsByTagName('td')

	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
			if (tables[i].innerHTML == 'Thank You' || 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 stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 {
 document.getElementById("pricingTable").innerHTML=xmlHttp.responseText
 //document.quoteform.numPages.value="2";
 }
}

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;
}
