/*
    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.
*/
   
    /* check Captcha answer with the server */
    function checkCaptchaAnswer()
    {  
        url = "dsAudioCaptchaSubmitDemo.php";
        params = "userAnswer=" + document.getElementById('userAnswer').value;
        var ret = makeServerCall(url, params);
        return (ret == 1) ? true : false;
    }

   /*
        Perform form validation.
   */
   function validateForm()
   { 
        // remove the mp3 player from the page
        dsSoundComplete();

        // check Captcha via Ajax
        captchaInput = document.getElementById('captchaInput');
        var captcha = document.getElementById('captcha');
        var correctCaptcha = checkCaptchaAnswer();
        var response = document.getElementById('responseToAnswer');
        var userInput = document.getElementById('userAnswer').value;
        
        if(!correctCaptcha)
        {        
            response.style.color = '#8e2d48';
            response.innerHTML = 'The answer: "' + userInput + '" is not correct.'; 
        }
        else
        {
            response.style.color = '#256134';
            response.innerHTML = 'The answer: "' + userInput + '" is correct.';
        }
        
        // in any case reset the Captcha and clear the input
        var captchaImage = document.getElementById('captchaImage');
        captchaImage.src = "../contact/dsCaptchaImage.php?b=" + Math.random();
        var captchaInput = document.getElementById('userAnswer');
        captchaInput.value = "";    
        
        document.getElementById('userAnswer').focus();
   }
   
   /*
        Callback the mp3 player uses when the sound is done playing.
        Remove the mp3 player from the page.
   */
   function dsSoundComplete()
   {
        var playerElement = document.getElementById('flashPlayerWraper');
        playerElement.innerHTML = "<div id='flascontent2' style='display:none;'>not seen</div>";
   } 
   
   /*
        Dynamically load the mp3 player
   */
   function loadPlayer()
   {         
        var curID = 'flashPlayerWraper';
        var playerElement = document.getElementById(curID);

        playerElement.innerHTML = "<div id='flascontent2' style='display:none;'>not seen</div>";

        var flashvars =
        {
            file:   "../contact/dsPlaySound.php",
            as:     '1'
        };

        var params =
        {
            swliveconnect:      'true',
            allowScriptAccess:  'sameDomain'
        };

        var attributes =
        {
            name:   'captchaPlayer',
            id:     'captchaPlayer'
        };

        var versionStr = "9.0";
        var hasMinPlayerVersion = swfobject.hasFlashPlayerVersion(versionStr);

        if (hasMinPlayerVersion == false) 
        {
            playerElement = document.getElementById(curID);
            playerElement.innerHTML = '<div id="flashcontent2"><a href="http://get.adobe.com/flashplayer/" target="_new">Get Flash player</a> to use the audio Captcha</div>';
        }
        else
        {
            swfobject.embedSWF('../contact/captchaplayer.swf', 'flascontent2', '0', '0', '9', false, flashvars, params, attributes);	
        }                      
    }

    /* 
        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;
   }