var ifv = false, ifvmsg = "";
function sendMail ()
{
    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 ("Your browser does not support AJAX. Please e-mail us at enquiries@capevillarentals.com");
                return false;
            }
        }
    }

    xmlHttp.onreadystatechange = function ()
    {
        if (xmlHttp.readyState < 4)
        {
            document.getElementById("loadmsg").innerHTML = '<img src="images/bar2.gif" alt="Sending..." align = "left" /> &nbsp; <span style = "font-size:110%;">Sending Message...</span> <br /><br />';
        }
				
				if (xmlHttp.readyState == 4)
        {
						if(xmlHttp.responseText == "True")
						{						
						   document.getElementById("loadmsg").innerHTML = '<span class = "green">Your message was successfully sent!</span><br /><br />';
							 
							 for(var i = 0; i < document.getElementById("contact").childNodes.length; i++)
							 {
    							 if((document.getElementById("contact").childNodes[i].children[1].type == "text" && document.getElementById("contact").childNodes[i].children[1].tagName == "INPUT") || document.getElementById("contact").childNodes[i].children[1].tagName == "TEXTAREA")
    							 {
    									 document.getElementById("contact").childNodes[i].children[1].value = "";
    							 }
							 }
							 
						}
						else
						{						   
							 document.getElementById("loadmsg").innerHTML = '<span class = "red">There are outstanding fields that still require valid information before you can send this message!</span><br /><br />';
						}
        }
    }


    var strContactFields = "";
		
		str = document.getElementById("name").value.replace(/&/g,"***amp***");
	  str = str.replace(/=/g,"***eql***");
	  str = str.replace(/\+/g,"***pls***");
		strContactFields += "?name="+str;
		
		if(document.getElementById("email").value.indexOf("@") != -1 && document.getElementById("email").value.indexOf(".") != -1 )
		{
    		str = document.getElementById("email").value.replace(/&/g,"***amp***");
    	  str = str.replace(/=/g,"***eql***");
    	  str = str.replace(/\+/g,"***pls***");
    		strContactFields += "&email="+str;	   
		}
		
		str = document.getElementById("telephone").value.replace(/&/g,"***amp***");
	  str = str.replace(/=/g,"***eql***");
	  str = str.replace(/\+/g,"***pls***");
		strContactFields += "&telephone="+str;
				
		str = document.getElementById("subject").value.replace(/&/g,"***amp***");
	  str = str.replace(/=/g,"***eql***");
	  str = str.replace(/\+/g,"***pls***");
		strContactFields += "&subject="+str;
				
		str = document.getElementById("message").value.replace(/&/g,"***amp***");
	  str = str.replace(/=/g,"***eql***");
	  str = str.replace(/\+/g,"***pls***");
		strContactFields += "&message="+str;
				
    xmlHttp.open ("GET", "sendmail.asp" + strContactFields, true);
		xmlHttp.send (null);
}

