/*
    Contact form including Captcha, using Ajax for validation.
	Part of the SummerSound integrated visual and audio captcha package.
	File contents copyright 2009 by David Summer.
*/

   /*
        Send the email message via Ajax.
   */
   function sendEmail(theForm)
   {
        url = "../contact/dsmail.php";
        params = "";
        // get the form elemnets for the POST
        for(i=0; i<theForm.elements.length; i++)
        {
            if(theForm.elements[i].type == "text" || theForm.elements[i].type == "textarea" || theForm.elements[i].type == "hidden")
            {
                params += theForm.elements[i].name + "=" + theForm.elements[i].value + "&";
            }
            else if(theForm.elements[i].type == "select-one")
            {
                params += theForm.elements[i].name + "=" + theForm.elements[i].options[theForm.elements[i].selectedIndex].text + "&";
            }
        }
        var ret = makeServerCall(url, params);
        // if successful, show success message
        if(ret == 1)
        {
            var emailSucessColor = '#000000';
            var messageElement = document.getElementById('weddingContactMessage');
            messageElement.style.color = emailSucessColor;
            messageElement.innerHTML = theForm.elements['firstNameInput'].value + ',<br/>Thank you, your email has been sent. <br/>David will contact you soon.';    
            // change the background color so the message is noticed
            messageElement.style.backgroundColor = '#b3cfee';
            
            // clear the input fields
            for(i=0; i<theForm.elements.length; i++)
            {
                if(theForm.elements[i].type == "text" || theForm.elements[i].type == "textarea")
                {
                    theForm.elements[i].value = '';
                }
            }
        }
   }

   /*
        Perform form validation.
   */
   function validateForm()
   { 
        // remove the mp3 player from the page
     //   dsSoundComplete();
   
        var ret = true;
        var errorColor = '#b74326';
        var correctColor = '#000000';
        
        // Name, email, date, time, location and comments are required fields
        var firstNameInput = document.getElementById('firstNameInput');
        var lastNameInput = document.getElementById('lastNameInput');
        var emailInput = document.getElementById('emailAddressInput');
        var dateInput = document.getElementById('dateInput');
        var timeInput = document.getElementById('timeInput');
        var locationInput = document.getElementById('locationInput');
        var commentsInput = document.getElementById('commentsInput');
        var inputField = new Array(firstNameInput, lastNameInput, emailInput, dateInput, timeInput, locationInput, commentsInput);
        
        var firstName = document.getElementById('firstName');
        var lastName = document.getElementById('lastName');
        var email = document.getElementById('emailAddress');
        var date = document.getElementById('date');
        var time = document.getElementById('time');
        var location = document.getElementById('location');                        
        var comments = document.getElementById('comments');
        var textField = new Array(firstName, lastName, email, date, time, location, comments);
        
        for(var i = 0; i < inputField.length; i++)
        {
            if (inputField[i].value==null||inputField[i].value=="")
            {
                textField[i].style.color = errorColor;
                if(ret == true)
                    inputField[i].focus();
                ret = false;
            }
            else
            {
                 textField[i].style.color = correctColor;
            }
        }
        
        // check for well formed email
        var apos = emailInput.value.indexOf("@");
        var dotpos = emailInput.value.lastIndexOf(".");
        if (apos < 1 || dotpos-apos < 2) 
        {
            email.style.color = errorColor;
            if(ret == true)
                emailInput.focus();
            ret = false;
        }
        
        // check phone
        var phoneInput = document.getElementById('phoneInput');
        var phone = document.getElementById('phone');
        if(phoneInput.value.length > 0 && phoneInput.value.length < 7)
        {
            phone.style.color = errorColor;
            if(ret == true)
                phoneInput.focus();
            ret = false;
        }
        else
        {
             phone.style.color = correctColor;
        }
         
        if(ret == false)    // erase any former success message
        {
            var messageElement = document.getElementById('weddingContactMessage');
            messageElement.innerHTML = 'Contact David Summer for more information on having beautiful solo flute music for your wedding ceremony, cocktail hour or other special occasion.';
            messageElement.style.backgroundColor = '';
        }
     
        return ret;
   }
   
    /* 
        Helper, make a call to the server 
    */
    function makeServerCall(url, params)
    {
        var xmlhttp;
    
        if (window.XMLHttpRequest)
        {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else if (window.ActiveXObject)
        {
            // code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        // 3rd param to false makes sync
        xmlhttp.open("POST",url,false);
        //Send the proper header information along with the request
        xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlhttp.setRequestHeader("Content-length", params.length);
        xmlhttp.setRequestHeader("Connection", "close");
        // send the request to the server 
        xmlhttp.send(params);
        var ret = xmlhttp.responseText;
        return ret;
   }