/*
    Contact form.
	File contents copyright 2009-2010 by David Summer.
*/
   
   var thankYouMessage = ", <br/> <p align='justify' class='text'>Thank you very much for contacting us. \
   We appreciate the fact that you have taken the time to fill out the website form. <br><br> \
    It will now be much easier for us to understand how we can help you (or your son or daughter) become the pianist you really want to be.<br><br> \
    You can expect to hear from us by email within 24 hours. This will take us to the next step in getting to know each other.<br><br> \
If you have any additional questions, please feel free to email us at: <a href='mailto:scheduling@edmascari.com'>scheduling@edmascari.com</a> \
    <br><br>Once again, many thanks for your interest. \
  I am looking forward to helping you as you learn to play the music you love.<br> \
  <br> \
  Regards,<br> \
  Ed Mascari<br> \
  <img src='../images/semnatura_ed.jpg' width='150' height='71'></p>";

   
   /* Set the focus on the first field in the form */
   function Init()
   {
        document.getElementById('yourName').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 == "checkbox" && theForm.elements[i].checked)
            {
                params += theForm.elements[i].name + "=" + theForm.elements[i].value + "&";
            }
            else if(theForm.elements[i].type == "radio" && theForm.elements[i].checked)
            {
                params += theForm.elements[i].name + "=" + theForm.elements[i].value + "&";
            }
        }
        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 = '<span class="text">' + theForm.elements['yourName'].value + "</span>" + thankYouMessage;    
            window.location.hash="contactMessageToUser"; 
            // 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 = '';
                }
                else if(theForm.elements[i].type == "checkbox")
                {
                    theForm.elements[i].checked = false;
                }
            } 
        } 
   }

   /*
        Perform form validation.
   */
   function validateForm()
   { 
        var ret = true;
        var errorColor = '#b74326';
        var correctColor = '#252525';
            
        // name, city, state, email and phone are required fields
        var inputField = new Array(document.getElementById('yourName'), document.getElementById('city'), document.getElementById('state'), document.getElementById('email'), document.getElementById('phone'));
        var textField = new Array(document.getElementById('yourNameLabel'), document.getElementById('cityLabel'), document.getElementById('stateLabel'), document.getElementById('emailLabel'), document.getElementById('phoneLabel'));
        
        for(var i = 0; i < inputField.length; i++)
        {
            if (inputField[i].value==null||inputField[i].value=="")
            {
                textField[i].style.color = errorColor;
                textField[i].style.fontWeight = 'bold';
                if(ret == true)
                    inputField[i].focus();
                ret = false;
            }
            else
            {
                 textField[i].style.color = correctColor;
                 textField[i].style.fontWeight = 'normal';
            }
        }
        
        // check for well formed email
        var emailInput = document.getElementById('email');
        var emailLabel = document.getElementById('emailLabel');
        var apos = emailInput.value.indexOf("@");
        var dotpos = emailInput.value.lastIndexOf(".");
        if (apos < 1 || dotpos-apos < 2) 
        {
            emailLabel.style.color = errorColor;
            emailLabel.style.fontWeight = 'bold';
            if(ret == true)
                emailInput.focus();
            ret = false;
        }
        
        // check phone
        var phoneInput = document.getElementById('phone');
        var phoneLabel = document.getElementById('phoneLabel');
        if(phoneInput.value.length < 7)
        {
            phoneLabel.style.color = errorColor;
            phoneLabel.style.fontWeight = 'bold';
            if(ret == true)
                phoneInput.focus();
            ret = false;
        }
        else
        {
             phoneLabel.style.color = correctColor;
             phoneLabel.style.fontWeight = 'normal';
        }
         
        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;
   }
