var httpRequest;
//var httpPfRequest;

function validate()
{

var ret_bool = true;

if(document.getElementById('user_name').value == ''){document.getElementById('user_name').parentNode.style.backgroundColor = "#FFBDBD"; /*alert("Please enter your name!");*/ ret_bool = false;}
if(document.getElementById('user_company').value == ''){document.getElementById('user_company').parentNode.style.backgroundColor = "#FFBDBD"; /* alert("Please enter your company!");*/ ret_bool = false;}
if(document.getElementById('user_contact_number').value == ''){document.getElementById('user_contact_number').parentNode.style.backgroundColor = "#FFBDBD"; /* alert("Please enter your contact number!");*/ ret_bool = false;}
if(document.getElementById('user_email_address').value == ''){document.getElementById('user_email_address').parentNode.style.backgroundColor = "#FFBDBD"; /* alert("Please enter your email address!");*/ ret_bool = false;}
if(document.getElementById('user_text_message').value == ''){document.getElementById('user_text_message').parentNode.style.backgroundColor = "#FFBDBD"; /*alert("Please type in your message!");*/ ret_bool = false;}

if(!ret_bool){alert('Please correct the fields marked in red!'); return false;}

s_m();
//document.message_form.submit();
}

function get_xml_http_request_object()
{
var xml_http_request_object;

if(window.XMLHttpRequest)
{//Mozilla related
xml_http_request_object = new XMLHttpRequest();

if (xml_http_request_object.overrideMimeType) {
xml_http_request_object.overrideMimeType('text/xml'); //correct mime for some browsers
}//close if overrideMimeType

}
else if(window.ActiveXObject)
{//Internet Explorer related
try {xml_http_request_object = new ActiveXObject("Msxml2.XMLHTTP");}catch (e) {try {xml_http_request_object = new ActiveXObject("Microsoft.XMLHTTP");}catch (e) {}} 
}

if (!xml_http_request_object) {
alert('Cannot create XmlHTTPRequest!');return false;}
else
{return xml_http_request_object;}

}


function check_verification_code()
{
var v_code = document.getElementById('validation_code_field').value;
var url = "./verify_code.php?c="+v_code;
var xml_http_v_code_request =  get_xml_http_request_object();
xml_http_v_code_request.onreadystatechange = function() { check_v_code_xml(xml_http_v_code_request); };//state change function
xml_http_v_code_request.open('GET', url, true);//open channel
xml_http_v_code_request.send(null);//send request 
return false;
}


function check_v_code_xml(request)
{
var v_code_content = "";
if (request.readyState == 4) {// everything is good, the response is received
if (request.status == 200) { v_code_content = request.responseText; if(v_code_content == "true"){validate();}else{document.getElementById('validation_code_field').parentNode.style.backgroundColor = "#FFBDBD";alert("Please enter the correct verification code!"); return false;}} //close if status 200 block
else {v_code_content = 'Problem: '+request.status; return false;}
}//close if state 4
 else {}  // still not ready
}




function s_m() {

var d_site = message_data_handler_file;
var user_name = escape(document.message_form.user_name.value);
var user_company = escape(document.message_form.user_company.value);
var user_contact_number = escape(document.message_form.user_contact_number.value);
var user_email_address = escape(document.message_form.user_email_address.value);
var user_text_message = escape(document.message_form.user_text_message.value);
var v_code = escape(document.message_form.validation_code.value);
var mess_v = "";

mess_v = mess_v +"&user_name=" + user_name;
mess_v = mess_v +"&user_company=" + user_company;
mess_v = mess_v +"&user_contact_number=" + user_contact_number;
mess_v = mess_v +"&user_email_address=" + user_email_address;
mess_v = mess_v +"&user_text_message=" + user_text_message;
mess_v = mess_v +"&v_code=" + v_code;

if (window.XMLHttpRequest) { // Mozilla, Safari, ...
httpRequest = new XMLHttpRequest();
if (httpRequest.overrideMimeType) {
httpRequest.overrideMimeType('text/xml'); //correct mime for some browsers
}//close if overrideMimeType
}//close if HMLHttpRequest 
else if (window.ActiveXObject) { // IE
try {httpRequest = new ActiveXObject("Msxml2.XMLHTTP");}catch (e) {try {httpRequest = new ActiveXObject("Microsoft.XMLHTTP");}catch (e) {}} 
}//close if activex

if (!httpRequest) {
alert('Cannot create an XMLHTTP instance, Message Not Sent!');return false;}

//state change function
httpRequest.onreadystatechange = function() { upd_mess(httpRequest); };
//alert(d_site);
//open channel
httpRequest.open('POST', d_site, true);
//set posting content type
httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
//send request with content        
httpRequest.send(mess_v);
}





function upd_mess(request)
{
var page_content = "";

if (request.readyState == 4) {
// everything is good, the response is received
if (request.status == 200) {
page_content = request.responseText;
} //close if status 200 block
else {page_content = 'There was a problem with the request. '+request.status;}

setTimeout(function(){wait_a_while(page_content)},2000);

}//close if state 4
 else {
//document.getElementById('message_form_container').style.display = "none";
//document.getElementById('message_send_container').style.display = "block";
document.getElementById('message_send_container').innerHTML = '<div class=\'sending_message_block\'><br><br>Sending Message. Please Wait!<br><br><img src=\'./images/icn_sending_animated-blue.gif\'></div>';  }  // still not ready

}


function wait_a_while(page_content)
{document.getElementById('message_send_container').innerHTML = page_content;} 









