/*
    Contact form.
	File contents copyright 2009 by David Summer.
*/
   
   /* Set the focus on the first field in the form */
   function Init()
   {
        document.getElementById('yourNameInput').focus();
   }

   /*
        Send the email message via Ajax.
   */
   function sendEmail(theForm)
   {
        url = "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 == "button")
            {
                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('contactMessageToUser');
            messageElement.style.color = emailSucessColor;
            messageElement.innerHTML = theForm.elements['yourNameInput'].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].type == "button")
                {
                    theForm.elements[i].value = '';
                }
            }
        } 
   }

   /*
        Perform form validation.
   */
   function validateForm()
   { 
        var ret = true;
        var errorColor = '#b74326';
        var correctColor = '#000000';
        
        // Name, email and comments are required fields
        var nameInput = document.getElementById('yourNameInput');
        var emailInput = document.getElementById('emailAddressInput');
        var commentsInput = document.getElementById('commentsInput');
        var inputField = new Array(nameInput, emailInput, commentsInput);
        
        var name = document.getElementById('yourName');
        var email = document.getElementById('emailAddress');
        var comments = document.getElementById('comments');
        var textField = new Array(name, email, 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;
        }
        
        // check that a subject was selected
        var subjectInput = document.getElementById('subjectInput');
        var subject = document.getElementById('subject');
        if(subjectInput.selectedIndex == 0)
        {
            subject.style.color = errorColor;
            if(ret == true)
                subjectInput.focus();
            ret = false;
        }
        else
        {
             subject.style.color = correctColor;
        }
         
        if(ret == false)    // erase any former success message
        {
            var messageElement = document.getElementById('contactMessageToUser');
            messageElement.innerHTML = '';
            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;
   }