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 stay@capevictoria.co.za");
                return false;
            }
        }
    }

    xmlHttp.onreadystatechange = function ()
    {
        if (xmlHttp.readyState < 4)
        {
            document.getElementById("loadmsg").innerHTML = '<img src="images/loading.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"><b>Your message was successfully sent!</b></span><br /><br />';
							 
							 document.getElementById("name").value = "";
							 document.getElementById("type").value = "";
							 document.getElementById("email").value = "";
							 document.getElementById("cnumber").value = "";
							 document.getElementById("type").selectedIndex = 0;
							 document.getElementById("arrival_day").selectedIndex = 0;
							 document.getElementById("arrival_month").selectedIndex = 0;
							 document.getElementById("arrival_year").selectedIndex = 0;
							 document.getElementById("departure_day").selectedIndex = 0;
							 document.getElementById("departure_month").selectedIndex = 0;
							 document.getElementById("departure_year").selectedIndex = 0;
							 document.getElementById("room1").selectedIndex = 0;
							 document.getElementById("room2").selectedIndex = 0;
							 document.getElementById("guests").value = "";
							 document.getElementById("requests").value = "";
						}
						else if(xmlHttp.responseText == "False")
						{						   
							 document.getElementById("loadmsg").innerHTML = '<span class = "red"><b>There are outstanding fields that are still require valid information before you can send this message!</b></span><br /><br />';
						}
						else
						{						   
							 document.getElementById("loadmsg").innerHTML =  xmlHttp.responseText;
						}
        }
    }


    var strContactFields = "";
		
		str = document.getElementById("name").value.replace(/&/g,"***amp***");
	  str = str.replace(/=/g,"***eql***");
	  str = str.replace(/\+/g,"***pls***");
		strContactFields += "?name="+str;
		
		str = document.getElementById("type").value.replace(/&/g,"***amp***");
	  str = str.replace(/=/g,"***eql***");
	  str = str.replace(/\+/g,"***pls***");
		strContactFields += "&type="+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("cnumber").value.replace(/&/g,"***amp***");
	  str = str.replace(/=/g,"***eql***");
	  str = str.replace(/\+/g,"***pls***");
		strContactFields += "&cnumber="+str;
				
		str = document.getElementById("arrival_day").value.replace(/&/g,"***amp***");
	  str = str.replace(/=/g,"***eql***");
	  str = str.replace(/\+/g,"***pls***");
		strContactFields += "&arrival_day="+str;		

		str = document.getElementById("arrival_month").value.replace(/&/g,"***amp***");
	  str = str.replace(/=/g,"***eql***");
	  str = str.replace(/\+/g,"***pls***");
		strContactFields += "&arrival_month="+str;	
		
		str = document.getElementById("arrival_year").value.replace(/&/g,"***amp***");
	  str = str.replace(/=/g,"***eql***");
	  str = str.replace(/\+/g,"***pls***");
		strContactFields += "&arrival_year="+str;	
						
		str = document.getElementById("departure_day").value.replace(/&/g,"***amp***");
	  str = str.replace(/=/g,"***eql***");
	  str = str.replace(/\+/g,"***pls***");
		strContactFields += "&departure_day="+str;				

		str = document.getElementById("departure_month").value.replace(/&/g,"***amp***");
	  str = str.replace(/=/g,"***eql***");
	  str = str.replace(/\+/g,"***pls***");
		strContactFields += "&departure_month="+str;
		
		str = document.getElementById("departure_year").value.replace(/&/g,"***amp***");
	  str = str.replace(/=/g,"***eql***");
	  str = str.replace(/\+/g,"***pls***");
		strContactFields += "&departure_year="+str;
		
		str = document.getElementById("room1").value.replace(/&/g,"***amp***");
	  str = str.replace(/=/g,"***eql***");
	  str = str.replace(/\+/g,"***pls***");
		strContactFields += "&room1="+str;

		str = document.getElementById("room2").value.replace(/&/g,"***amp***");
	  str = str.replace(/=/g,"***eql***");
	  str = str.replace(/\+/g,"***pls***");
		strContactFields += "&room2="+str;
		
		str = document.getElementById("guests").value.replace(/&/g,"***amp***");
	  str = str.replace(/=/g,"***eql***");
	  str = str.replace(/\+/g,"***pls***");
		strContactFields += "&guests="+str;
		
		str = document.getElementById("requests").value.replace(/&/g,"***amp***");
	  str = str.replace(/=/g,"***eql***");
	  str = str.replace(/\+/g,"***pls***");
		strContactFields += "&requests="+str;		
				
    xmlHttp.open ("GET", "sendbooking.asp" + strContactFields, true);
		xmlHttp.send (null);
}
