﻿
function postForm() {
    var randy = Math.random() * Date.parse(new Date());

    if (typeof pageTracker != "undefined")
        pageTracker._trackPageview("/ContactFormSent");

    var _tmpurl = "/Contact.aspx?postback=1&content=1&r=" + randy;
    $("loading").style.display = 'block';

    if (_tween)
        _tween.stop();
    _tween = Ekina.UI.Effects.Tween.alpha($("contents_holder"), 0, { duration: .5, func: Ekina.UI.Effects.Tween.Regular.Out });
    _tween.start();

    _tween.onTweenFinished = function() {

        _xmlhttp = null;
        if (window.XMLHttpRequest) {// code for all new browsers
            _xmlhttp = new XMLHttpRequest();
        }
        else if (window.ActiveXObject) {// code for IE5 and IE6
            _xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }

        if (_xmlhttp != null) {
            var name = $("name").value;
            var email = $("email").value;
            var enquiry = $("enquiry").value;
            var subscribe = $("subscribe").checked;
            _xmlhttp.onreadystatechange = form_posted;
            _xmlhttp.open("POST", _tmpurl, true);
            _xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            _xmlhttp.send("name=" + escape(name) + "&email=" + escape(email) + "&enquiry=" + escape(enquiry) + "&subscribe=" + escape(subscribe));
        }
    };

    return false;
}

function form_posted() {
    if (_xmlhttp.readyState == 4) { // 4 = "loaded"
        //if (_xmlhttp.status == 200) { // 200 = OK
        $("loading").style.display = 'none';
        $('contents_holder').innerHTML = _xmlhttp.responseText;
        _tween.stop();
        _tween = Ekina.UI.Effects.Tween.alpha($("contents_holder"), 100, { duration: .5, func: Ekina.UI.Effects.Tween.Regular.Out });
        _tween.start();
        _tween.onTweenFinished = null;
    }
//    else {
//        alert(_xmlhttp.responseText);
//        alert("Problem retrieving XML data");
//    }
}


function IsValidEmail(sEmail) {
    validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
    if (sEmail.search(validRegExp) == -1) {
        return false;
    }
    return true;
}

function validateForm() {

    var error = "";

    if (!IsValidEmail($f("email")))
        error += "\n - Email address is not valid";

    if ($f("name") == "")
        error += "\n - Name cannot be blank";

    if ($f("enquiry") == "")
        error += "\n - Enquiry cannot be blank";


    if (error != "") {
        alert("Please check the following and try again...\n" + error);
        return false;
    } else
        return true;
}