/**************************************
	Filename:	validate.js
	Author:	Chris Campbell
			www.particletree.com
	Modified:	03.04.2008
			www.squeezeoflime.com
**************************************/

/***************************************************
		GLOBAL VARIABLES
***************************************************/
//var url = "form_check.php?validation_type=ajax&val=";//url is the page which will be processing all of the information.  it is important to make sure validation_type is ajax
var feedback_id;				// DECLARE variable for form feedback
var http = get_http_object();//don't worry about this
var num_errors = 0; 			// INITIALIZE number of errors to zero
var url = "../Scripts/form_validate.php?validation_type=ajax";	// INITIALIZE the url of the AJAX script
var change = '';

/***************************************************
		ATTACH_FORM_HANDLERS
	input:	none
	output:	none
	function:	attaches dynamic validation to 
		form elements, including the form itself.
***************************************************/
function attach_form_handlers() {
	var form = document.getElementById('form1');
	if(form) {
		if(document.getElementsByTagName) {								// VERIFY we have a capable browser
			var obj = document.getElementsByTagName('input');					// GRAB all possible tags
			for (var icount=0; icount < obj.length; icount++)				// LOOP through all tags
				obj[icount].onblur = function(){ return validate_me(this); }	// --ATTACH our dynamic validation to each tag
		} // END if
		form.onsubmit = function(){ return validate_whole(); }				// ATTACH validation to the form
	}
} // END attach_form_handlers


/***************************************************
		VALIDATE_ME
	input:	the element
	output:
	function:	called from onBlur on each of the
		form input elements (thanks to A_F_H)
***************************************************/
function validate_me(obj) {
	
	obj_value			= obj.value; 				// GET our field value
	
	obj_rules 		= obj.className.split(' ');	// GRAB all the class names (aka rules) from the object
	is_required 		= obj_rules[1];			// --is it required?
	type_check		= obj_rules[2]; 			// --what kind of additional check is needed? (if any)
	feedback_id		= obj_rules[3];			// --where we send it to

	if(type_check == "checkbox") {
		if(obj.checked) {}
		else { obj_value = ''; }
	}
	
	if(type_check == "password") { change = obj_value; }
	if(type_check == "confirm") { obj_value += "&val2="+change; }

	var parameters = "val=" + (obj_value) + "&is_required=" + (is_required) + "&type_check=" + (type_check);
	http.open("POST", url, true);
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.onreadystatechange = handle_http_response;
	http.send(parameters);
} // END validate_me

/***************************************************
		handle_http_response
	input:	the element
	output:
	function:	handles HTTP response and decides
		what to do with it
***************************************************/
function handle_http_response() {
	if (http.readyState == 4) {
		// we might want to check for server response == 200
  		var results = http.responseText.split(",");	// GET and SPLIT validation results
		document.getElementById(feedback_id).innerHTML = "";
		document.getElementById(feedback_id).appendChild(document.createTextNode(results[0]));
  	}
}

/***************************************************
		VALIDATE_WHOLE
	input:	none
	output:	true | false
	function:	validates form for submission
***************************************************/
function validate_whole() {
	var inputs = document.getElementsByTagName('input');					// GRAB all possible tags
	var box;
	var exception = '';
	var num_errors = 0;
	for (var icount=0; icount < inputs.length; icount++) {				// LOOP through all tags
		var rules = inputs[icount].className.split(' ');
		if(rules[1] == "required" && inputs[icount].value == "") {
			document.getElementById(rules[3]).innerHTML = "Required";
		} else if(rules[2] == "checkbox" && inputs[icount].checked == false) {
			document.getElementById(rules[3]).innerHTML = "Required";
			box = inputs[icount].id;
			exception = rules[3];
		} else {}
	}
		
	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 == 'Valid' || tables[i].innerHTML == '' ) {
				tables[i].style.color = '#000000';//the color is changed to black or stays black
			} else if (tables[i].id == exception) {
				if(document.getElementById(box).checked == true) {
					tables[i].style.color = '#000000';//the color is changed to black or stays black
					tables[i].innerHTML = '';//the color is changed to black or stays black
				} else {
					num_errors = num_errors + 1; //the error count increases by 1
					tables[i].style.color = '#ff0000';//error messages are changed to red
				}
			} else {
				num_errors = num_errors + 1; //the error count increases by 1
				tables[i].style.color = '#ff0000';//error messages are changed to red
			}
		}
	}
		
	if (num_errors > 0) {	// IF we found errors send error message
		alert("Please make sure all fields are properly completed.  Errors are marked in red!");
		num_errors = 0;	// --RESET errors to 0
		return false;
	} else { return true; }

}

/***************************************************
		request_HTTP
	input:	
	output:	xmlhttp object
	function:	gets a valid xmlhttp object
***************************************************/
function get_http_object() {
	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;
}