// JavaScript Document

addLoadEvent(contactForm);

function addLoadEvent(func) {
    // Allows multiple window.onload events
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

function contactForm() {
    // When the button is clicked, try to send the form
    var cFormButton = document.getElementById('subButton');
    cFormButton.onclick = function() {
      sending();
      return false;  
    } 
}

function pausecomp(millis) {
    //  Function to create a variable delay
    var date = new Date();
    var curDate = null;

    do { curDate = new Date(); }
    while(curDate-date < millis);
    } 

function sending() {
    // Change the stamp image and send the request
	document.getElementById('subButton').src = "images/images/sending.gif";
    // Take the focus away from the submit button and send the
    // AJAX request 
	document.getElementById('subButton').blur();
	new Ajax.Request("email_form_process.php", {
            method: 'post',
            postBody: "name="+$F("name")+"&email="+$F("email")+"&message="+$F("message"),
            onComplete: showResponse
		});
    }

function showResponse(req) {
    // Based upon the response from the server side script, respond
    $('errors').style.borderStyle= "solid";	   
    var res=/message was received/;
	  
    if(req.responseText.match(res))    {
        // Successful
   	    pausecomp(1000); // One second delay
	    document.getElementById('subButton').src = "images/images/sent.jpg";
	    document.getElementById('subButton').blur();
        $('errors').style.borderSize = "0px";
        $('errors').style.borderStyle = "none";
        $('errors').innerHTML = "";
	    success = 1;
        }
    else {
        // Unsuccessful
        pausecomp(1000); // One second delay
        document.getElementById('subButton').src = "images/images/stamp.gif";
        document.getElementById('subButton').blur();
        $('errors').style.borderColor = "red";
        $('errors').style.color = "red";
        $('errors').style.borderSize = "1px";
        $('errors').innerHTML = req.responseText;
        }   
}
