// check the name
function isValidname(str) 
{
	var name = document.getElementById("name").value;
	if(""==name)
	{
		//document.getElementById("name_text").style.color="#FF0000";
		document.getElementById('name').focus();
		return false;
	}
	else
	{
		document.getElementById("name_text").style.color="#000000";
		return true;
	}
}

// check comment
function isValidcomment(str) 
{
	var comment = document.getElementById("comment").value;
	if(""==comment)
	{
		//document.getElementById("comment_text").style.color="#FF0000";
		document.getElementById('comment').focus();
		return false;
	}
	else
	{
		document.getElementById("comment_text").style.color="#000000";
		return true;
	}
}

function XMLHTTPObject() {
    var xmlhttp=false;
    //If XMLHTTPReques is available
    if (XMLHttpRequest) {
        try {xmlhttp = new XMLHttpRequest();}
        catch(e) {xmlhttp = false;}
    } else if(typeof ActiveXObject != 'undefined') {
	//Use IE's ActiveX items to load the file.
        try {xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");} 
        catch(e) {
            try {xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");}
            catch(E) {xmlhttp = false;}
        }
    } else  {
        xmlhttp = false; //Browser don't support Ajax
    }
    return xmlhttp;
}
var http = new XMLHTTPObject();




//This function will be called when the form is submited
function saveData() 
{
	var probs = 0;
	if (isValidanswer() == false) 
	{
		document.getElementById("answer_text").style.color="#FF0000";
		probs = probs + 1;
	}
	if (isValidcomment() == false) 
	{
		document.getElementById("comment_text").style.color="#FF0000";
		probs = probs + 1;
	}
	if (checkemail() == false) 
	{
		document.getElementById("email_text").style.color="#FF0000";
		probs = probs + 1;
	}
	if (isValidname() == false) 
	{
		document.getElementById("name_text").style.color="#FF0000";
		probs = probs + 1;
	}
		
	if (probs == 0) // check for problems with the fields
	{ 
      // if the answer is correct, proceed
	   	var name = document.getElementById("name").value;
    	var comment = document.getElementById("comment").value;
    	var email = document.getElementById("email").value;
		var domain = document.getElementById("domain").value;
		var answer = document.getElementById("answer").value;
	   //By calling this file, we have sent the data.
    	http.open("GET","send_data.php?comment=" + comment + "&email=" + email + "&name=" + name + "&domain" + domain + "&return=confirm",true);
    
    	http.onreadystatechange = function() 
		{
        	if(http.readyState == 4) 
			{
            	if(http.status == 200) 
				{
                	var result = http.responseText;
                	var msg = ""
                	if(result == '1') 
					{
                    	//Display the thank you message.
                    	msg = "Thank you for your interest - we will look into your comment as soon as possible.";
						//alert("Thank you for your interest - we will look into your comment as soon as possible.");
                	} else 
					{
                   	 msg = "There was an error sending the data : " . result;
                	}
                	document.getElementById("form_elements").innerHTML = msg;
            	}
        	}
    	}
    http.send(null);
    
    return false;//Prevent the form from being submited
  }
  // ** END **
  	error_message = "Some of the form data is incorrect. " + probs + " problems found. Please double check and resubmit";
   	
	alert(error_message);
	//document.getElementById('heading_text').innerHTML = error_message;
	return false;
   
}

function init() {
    if(http) {//If the browser supports Ajax functions
        document.getElementById("feedback_form").onsubmit = saveData; //The saveData function is attached to the submit action of the form.
    }
}
window.onload = init; //The 'init' function will be called when the page is loaded.
