var waiting = false;
var funcQueue = new Array();
var funcLoop = false;
var xmlHttp=null;
function ajaxQCall(url, myForm, FuncToCall)
{
    var inputs = "";
    var elem = myForm.elements;
    for(var i = 0; i < elem.length; i++)
    {
        value = iValue(elem[i]);
        if(!value)
            continue;
        inputs += "&"+elem[i].name+"=";
        if(typeof value == 'string')
            inputs += escape(value)
        else if(typeof value == 'boolean')
            inputs += (value? "true":"false")
        else
            inputs+= value;
    }
    ajaxCall(url, inputs, FuncToCall);
}
function ajaxFCall(url, myForm, eInputs, FuncToCall)
{
    var inputs = "";
    var elem = myForm.elements;
    for(var i = 0; i < elem.length; i++)
    {
        value = iValue(elem[i]);
        if(!value)
            continue;
        inputs += "&"+elem[i].name+"=";
        if(typeof value == 'string')
            inputs += escape(value)
        else if(typeof value == 'boolean')
            inputs += (value? "true":"false")
        else
            inputs+= value;
    }
    eInputs += inputs;
    ajaxCall(url, eInputs, FuncToCall);
}
function ajaxCall(url, inputs, FuncToCall)
{
//alert(inputs);
    if (!FuncToCall)
        var func = function(){ waiting=false; };
    else
        var func = function(){
            FuncToCall();
            waiting=false;
        };
    var functionToRun = function()
    { runAjax(url, inputs, func) };
    funcQueue.push(functionToRun);
    if(!funcLoop)
    {
        funcLoop = true;
        callNextFunc();
    }
}
function callNextFunc()
{
    if (!waiting)
    {
        if(funcQueue.length > 0)
        {
            waiting = true;
            var func = funcQueue.shift();
            func();
            window.setTimeout("callNextFunc()", 5);
        }
        else
        {
            funcLoop = false;
            waiting = false;
        }
    }
    else
    { window.setTimeout("callNextFunc()", 5); }
    return;
}
function runAjax(url, inputs, finalCall)
{
    // Firefox, Opera 8.0+, Safari
    try
    { xmlHttp=new XMLHttpRequest(); }
    catch (e)
    {
        // Internet Explorer
        try
        { xmlHttp=new ActiveXObject('Msxml2.XMLHTTP'); }
        catch (e)
        { xmlHttp=new ActiveXObject('Microsoft.XMLHTTP'); }
    }
    if (xmlHttp==null)
    {
        alert ('Ajax Error!');
        return;
    }
//        url = url.replace("https", "http");
//alert(url);
    xmlHttp.open('POST', url, true);
    xmlHttp.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT');
    //xmlHttp.setRequestHeader('Content-Type','multipart/form-data; charset=UTF-8');
    xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
    xmlHttp.setRequestHeader('Content-length', inputs.length);
    xmlHttp.setRequestHeader('Connection', 'close');
    xmlHttp.onreadystatechange=function () { runFunction(finalCall) };
    if(inputs == "")
        inputs = null;
    xmlHttp.send(inputs);
}
function runFunction(objFn)
{
    if (xmlHttp.readyState==4 && xmlHttp.status == 200)
    {
        objFn();
        ajaxReplyJs();
    }
    else if (xmlHttp.readyState==4 && xmlHttp.status != 200)
    {alert("Ajax error "+xmlHttp.status); waiting=false;}
}
function ajaxReplyJs()
{
    if (document.getElementById('replyJs'))
    { eval(document.getElementById('replyJs').innerHTML); }
}
function isEmpty(field, text)
{
    if (field.value == "")
        field.value = text;
}
function editMe(field, text)
{
    if (field.value == text)
        field.value = "";
}
function fieldValue(field, text)
{
    if (field.value == text)
        return "";
    else
        return field.value;
}

        

