//Assumes jquery is loaded
function validate() {
    /* Prepare and Send data to the server */
	var contactForm = document.getElementById('contact');
    var i, max = contactForm.elements.length;
    var formData = [];
    for (i = 0; i < max; ++i) {
        if (contactForm.elements[i].type == 'checkbox') {
            formData.push(contactForm.elements[i].name + "=" + contactForm.elements[i].checked);
        } else {
            formData.push(contactForm.elements[i].name + "=" + contactForm.elements[i].value);
        }
    }
    $.ajax({
        type:"POST",
        url:"./inc/contact.php",
        data:formData.join("&"),
        success:function(data) {
            var result = eval('(' + data + ')');
            //Generic reset of captcha elements
            $("#captchaRepeat_error").hide();
            // hide all error messages
            $(".error_msg").hide();
            //Check result and proceed accordingly
            if (parseInt(result.code) > 0) {
                max = contactForm.elements.length;
                for (i = 0; i < max; ++i) {
                    if (contactForm.elements[i].type == "select") {
                        contactForm.elements[i].selectedIndex = 0;
                    }
                    else if (contactForm.elements[i].type != "button" && contactForm.elements[i].type != "hidden") {
                        contactForm.elements[i].value = "";
                    }
                }
                if ($(".fill").length) {
                    window.scrollTo(0, 0);
                    $(".fill").load("./thanks.php?contact=" + result.contactId + "&source=" + $("#source").val());
                    if (typeof(pageTracker) == "object" && typeof(pageTracker._trackPageview) == "function") {
                        pageTracker._trackPageview("./thanks.php");
                    }
                }
                else {
                    alert("Thanks, someone will follow up with you soon.");
                }
            }
            else {
                // assign errors
                if (parseInt(result.code) == 0 && typeof(result.fields) == "object") {
                    for(field in result.fields) {
                        $("#" + result.fields[field] + "_error").show();
                    }
                    // position page at first error
                    var errors = $(".error_msg");
                    $.each(errors, function(){
                        var fields = this.id.split("_");
                        if (fields[0]) {
                            if ($.inArray(fields[0], result.fields) >= 0) {
                                window.scrollTo(0, $("#" + this.id).offset().top);
                                return false;
                            }
                        }
                    });
                } else {
                    alert("Sorry, we weren't able to process your application at the moment, try again soon.");
                }	           
            }
        },
        error:function() {
            alert("Sorry, we weren't able to send your mesage at the moment, try again soon.");
        }
    });
}

