var request;
var dest;

function processStateChange(){
    if (request.readyState == 4){
        contentDiv = document.getElementById(dest);
        if (request.status == 200){
            response = request.responseText;
            contentDiv.innerHTML = response;
        } else {
            contentDiv.value = "Error: Status "+request.status;
        }
    }
}

function loadHTML(URL, destination){
    dest = destination;
    if (window.XMLHttpRequest){
        request = new XMLHttpRequest();
        request.onreadystatechange = processStateChange;
        request.open("GET", URL, true);
        request.send(null);
    } else if (window.ActiveXObject) {
        request = new ActiveXObject("Microsoft.XMLHTTP");
        if (request) {
            request.onreadystatechange = processStateChange;
            request.open("GET", URL, true);
            request.send();
        }
    }
}

function str_ireplace ( search, replace, subject ) {
    var i;
    if(!(replace instanceof Array)){
        replace=new Array(replace);
        if(search instanceof Array){//If search    is an array and replace    is a string, then this replacement string is used for every value of search
            while(search.length>replace.length){
                replace[replace.length]=replace[0];
            }
        }
    }
    if(!(search instanceof Array))search=new Array(search);
    while(search.length>replace.length){//If replace    has fewer values than search , then an empty string is used for the rest of replacement values
        replace[replace.length]='';
    }
    if(subject instanceof Array){//If subject is an array, then the search and replace is performed with every entry of subject , and the return value is an array as well.
        for(k in subject){
            subject[k]=str_replace(search,replace,subject[k]);
        }
        return subject;
    }
    for(var k=0; k<search.length; k++){
        reg = new RegExp(search[k], 'gi');
        subject = subject.replace(reg, replace[k]);
    }
    return subject;
}
	
function explode(inputstring, separators, includeEmpties) {
    inputstring = new String(inputstring);
    separators = new String(separators);

    if(separators == "undefined") {
        separators = " :;";
    }

    fixedExplode = new Array(1);
    currentElement = "";
    var count = 0;

    for(x=0; x < inputstring.length; x++) {
        char = inputstring.charAt(x);
        if(separators.indexOf(char) != -1) {
            if ( ( (includeEmpties <= 0) || (includeEmpties == false)) && (currentElement == "")) {
            } else {
                fixedExplode[count] = currentElement;
                count++;
                currentElement = "";
            }
        }	else {
            currentElement += char;
        }
    }

    if (( ! (includeEmpties <= 0) && (includeEmpties != false)) || (currentElement != "")) {
        fixedExplode[count] = currentElement;
    }
    return fixedExplode;
}
	
function isdate(date) {
    arr_date = explode(date, "-");
    day = arr_date[0];
    month = arr_date[1] - 1;
    year = arr_date[2];
    mydate = new Date(year, month, day, '00', '00', '00');
    //alert(day+"-"+month+"-"+year+" "+mydate.getDate()+"-"+mydate.getMonth()+"-"+mydate.getFullYear());
    if((day == mydate.getDate())&&(month == mydate.getMonth())&&(year == mydate.getFullYear())&&(year > 1753)) {
        return(true);
    } else {
        return(false);
    }
}
	
function istime(time) {
    arr_time = explode(time, ":");
    hour = arr_time[0];
    min = arr_time[1];
    if (arr_time.size() < 3) sec = '00'; else sec = arr_time[2];
    mytime = new Date('01','01','2001',hour, min, sec);
    if((hour==mytime.getHours())&&(min==mytime.getMinutes())&&(sec==mytime.getSeconds())) {
        return(true);
    } else {
        return(false);
    }
}
	
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/,"");
}
	
function update_menu() {
    new Ajax.Request('/index.php?ajax=update_menu', {
        method: 'post',
        onSuccess: function(transport) {
            $('adm_menu').innerHTML = transport.responseText;
        }
    });
}
	
function update_cart() {
    new Ajax.Request('/index.php?ajax=update_cart', {
        method: 'post',
        onSuccess: function(transport) {
            $('module_cart_menu').innerHTML = transport.responseText;
        }
    });
}
	
function update_attention() {
    new Ajax.Request('/index.php?ajax=update_attention', {
        method: 'post',
        onSuccess: function(transport) {
            $('module_attention').innerHTML = transport.responseText;
        }
    });
}
	
function delete_user(id) {
    if (confirm('Этот пользователь будет удален?')) {
        grayOut(true);
        new Ajax.Request('/index.php?ajax=function&action=delete_user&id='+id, {
            method: 'post',
            onSuccess: function(transport) {
                grayOut(false);
                $('main_content').innerHTML = transport.responseText;
            }
        });
}
}
function enterPress(event, button) {
    if((event.keyCode == 0xA)||(event.keyCode == 0xD)) {
        button.click();
    }
}
function tr_ch_color(target, background, text) {
    target.style.background = background;
    target.style.color = text;
}

function var_dump(element, limit, depth) {
    depth =	depth?depth:0;
    limit = limit?limit:1;
    returnString = '<ol>';
    for(property in element)
    {
        //Property domConfig isn't accessable
        if (property != 'domConfig')
        {
            returnString += '<li><strong>'+ property + '</strong> <small>(' + (typeof element[property]) +')</small>';
            if (typeof element[property] == 'number' || typeof element[property] == 'boolean')
                returnString += ' : <em>' + element[property] + '</em>';
            if (typeof element[property] == 'string' && element[property])
                returnString += ': <div style="background:#C9C9C9;border:1px solid black; overflow:auto;"><code>' +
                element[property].replace(/</g, '&amp;lt;').replace(/>/g, '&amp;gt;') + '</code></div>';
            if ((typeof element[property] == 'object') && (depth < limit))
                returnString += var_dump(element[property], limit, (depth + 1));
            returnString += '</li>';
        }
    }
    returnString += '</ol>';
    if(depth == 0)
    {
        winpop = window.open("", "","width=800,height=600,scrollbars,resizable");
        winpop.document.write('<pre>'+returnString+ '</pre>');
        winpop.document.close();
    }
    return returnString;
}

function postToURL(path, params, method) {
    method = method || "post"; // Set method to post by default, if not specified.

    // The rest of this code assumes you are not using a library.
    // It can be made less wordy if you use one.
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    for(var key in params) {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", key);
        hiddenField.setAttribute("value", params[key]);

        form.appendChild(hiddenField);
    }

    document.body.appendChild(form);
    form.submit();
}
