
//this function is now incorporated into the main ajaxform function
function validate() {
	if (document.form1.name.value.length < 1) {
	alert("Please enter your name");
	return false;
	}
	
	
	
	if (document.form1.name.value=="anon") {
	alert("Sorry, we no longer accept comments from anon. Please use a real name!");
	return false;
	}
	
	if (document.form1.name.value=="Anon") {
	alert("Sorry, we no longer accept comments from anon. Please use a real name!");
	return false;
	}
	
	if (document.form1.comments.value.length < 10) {
	alert("Sorry your comments are too short!");
	return false;
	}
	
	return true;
}

function displayLoading(element) {
	while (element.hasChildNodes()) {
		element.removeChild(element.lastChild);
	}
	var image = document.createElement("img");
	image.setAttribute("src","loading.gif");
	image.setAttribute("alt","Loading...");
	element.appendChild(image);
}

function fadeUp(element,red,green,blue) {
	if (element.fade) {
		clearTimeout(element.fade);
	}
	element.style.backgroundColor = "rgb("+red+","+green+","+blue+")";
	if (red == 255 && green == 255 && blue == 255) {
		return;
	}
	var newred = red + Math.ceil((255 - red)/10);
	var newgreen = green + Math.ceil((255 - green)/10);
	var newblue = blue + Math.ceil((255 - blue)/10);
	var repeat = function() {
		fadeUp(element,newred,newgreen,newblue)
	};
	element.fade = setTimeout(repeat,100);
}


function ajaxform(thisform,formhandler)
{
	
	if (document.form1.name.value.length < 1) {
	alert("Please enter your name");
	return false;
	}
	
	if (document.form1.name.value=="anon") {
	alert("Sorry, we no longer accept comments from anon. Please use a real name!");
	return false;
	}
	
	if (document.form1.comments.value.length < 10) {
	alert("Sorry your comments are too short!");
	return false;
	}

	
	//set up a variable for the comments div
	var stu = document.getElementById("comments_ajax");
	
	//replace the div with a loading image - NEED TO CHECK THIS IS WORKING!
	//displayLoading(stu);
	
    //General Purpose Ajax form submitter.
    //Written by Carl(bag) @ Thybag.co.uk

    // Set up data variable
    var formdata = "";

    // Set up Ajax request variable
    try {xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");}  catch (e) { alert("Error: Could not load page.");}

    // Loop through form fields
    for (i=0; i < thisform.length; i++)
    {
         //Build Send String
         if(thisform.elements[i].type == "text"){ //Handle Textbox's
                  formdata = formdata + thisform.elements[i].name + "=" + escape(thisform.elements[i].value) + "&";
         }else if(thisform.elements[i].type == "textarea"){ //Handle textareas
                  formdata = formdata + thisform.elements[i].name + "=" + escape(thisform.elements[i].value) + "&";
         }else if(thisform.elements[i].type == "checkbox"){ //Handle checkbox's
                 formdata = formdata + thisform.elements[i].name + "=" + thisform.elements[i].checked + "&";
         }else if(thisform.elements[i].type == "radio"){ //Handle Radio buttons
                  if(thisform.elements[i].checked==true){
                     formdata = formdata + thisform.elements[i].name + "=" + thisform.elements[i].value + "&";
                  }
         }else{
                  //finally, this should theoretically this is a select box.
                  formdata = formdata + thisform.elements[i].name + "=" + escape(thisform.elements[i].value) + "&";
         }
    }

    //Send Ajax Request
    xmlhttp.onreadystatechange = function(){
               //Check page is completed and there were no problems.
               if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
                      //What to do once the form is submitted - to inform the user.
                      //alert("Your form has been successfully submitted");
					  
					  //replace the div (currently showing the loading image) with the response from the php script
					  stu.innerHTML = xmlhttp.responseText;
					  
					  //fade div from yellow
					  fadeUp(stu,255,255,153);

               }
    }
    //Make connection
    xmlhttp.open("POST", formhandler);
    //Set Headers
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    //Send data
    xmlhttp.send(formdata);
    //stops form from submitting normally
    return false;
}