function ajax_request(url, data, type, method, callback, c_params, loading, l_params){
    if(!type){
        type = false;
    }
    if(!method){
        method = "post";
    }
    var xmlhttp;
    if (window.XMLHttpRequest){
        xmlhttp = new XMLHttpRequest();
    } else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (type == true){
        xmlhttp.onreadystatechange = function(){
            if (loading && xmlhttp.readyState == 1){
                eval(loading + "(l_params)");
            }
            if (callback && xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                eval(callback + "(xmlhttp.responseText, c_params)");
            }
        }
    }
    if(method == "get"){
        url += "?" + data;
        data = "";
    }
    xmlhttp.open(method, url, type);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send(data);
    if(type == false){
        return xmlhttp.responseText;
    } else {
        return true;
    }
}

function http_build_query(data, separator, name) {  
    var key = 0, i = 0, tmp_arr = new Array();
    if (!separator) {
        separator = "&";
    }
    for (key in data) {
        if(data[key] instanceof Array){
            tmp_arr[i++] = http_build_query(data[key], separator, key);
        } else {
            var key_2;
            if (!name){
                key_2 = key;
            } else {
                key_2 = name;
            }
            tmp_arr[i++] = key_2 + '=' + data[key];
        }
    }
    return tmp_arr.join(separator);
} 

function submit_form(elem){
    var arr = new Array();
    for(var i = 0; i < elem.form.elements.length; i++){
        arr[elem.form.elements[i].name] = elem.form.elements[i].value;
    }
    var data = http_build_query(arr);
    ajax_request("/page/uploads/php/ajax_contact_form.php", data, true, 
        "post", "submit_form_callback", null, "submit_form_loading");
}

function submit_form_loading(){
    document.getElementById("contactform").style.display = "none";
    document.getElementById("loading").style.display = "";
}

function submit_form_callback(result){ 
    document.getElementById("loading").style.display = "none";
    document.getElementById("contactform").style.display = "none";
    if(result == 1){    
        document.getElementById("success").style.display = "";
    } else {
        document.getElementById("error").style.display = "";
    }
}
