// HOTPEBBLE CMS - Copyright ©2006 Greg Smith, Hotpebble. All Rights Reserved.  This file remains the intellectual property of Greg Smith and may not be redistributed in whole or significant part.

function validate_Form(form_id, errors_id, admin_path){

	//Check that AJAX is supported
	var xmlHttp;
	try{
		//Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e){
		//Internet Explorer
		try{
	  		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  	}
		catch (e){
			try{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
		  	catch (e){
				alert("Please update your browser as it doesn't support AJAX functionality.");
				return false;
			}
		}
	}
	
	//AJAX is supported so continue
	//Function to get form values (from http://www.devarticles.com/c/a/XML/XML-in-the-Browser-Submitting-forms-using-AJAX/5/)
	function getFormValues(fobj){
		var str='';
		for(var i=0;i< fobj.elements.length;i++){
			str+=fobj.elements[i].name+'='+ escape(fobj.elements
	[i].value)+'&';
		}
		str=str.substr(0,(str.length-1));
		return str;
	}
	
	//Send to the server
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==3){
			document.getElementById(errors_id).style.display = 'block';
			document.getElementById(errors_id).innerHTML = 'validating...';
		}
		if(xmlHttp.readyState==4){
			//Fill the errors span with reported validation errors
			result = xmlHttp.responseText;
			if(result=='validates'){
				//validates
				document.getElementById(form_id).submit();
			}else{
				document.getElementById(errors_id).innerHTML = result;
			}
		}
	}
	var str = getFormValues(document.getElementById(form_id));
	xmlHttp.open("POST",admin_path+"/scripts/script_validate_form.php",true); //open the php doc and object
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8"); //might be useful
	xmlHttp.send("validation_type=ajax&"+str); //send the data from form to the php file
	return false;
	
}