﻿var Ajax_xh;

function Ajax_String(url, request, async, fncReceived)
{
	Ajax_Init();

	if (async && fncReceived != null)
	{
		Ajax_xh.onreadystatechange = function()
		{
			if (Ajax_xh.readyState == 4)//done
			{
				fncReceived(Ajax_xh.responseText);
			}
		}
	}

	Ajax_xh.open("POST", url, async);
	Ajax_xh.setRequestHeader("content-type", "application/x-www-form-urlencoded");
	Ajax_xh.send(request);

	if (async == false)
		return Ajax_xh.responseText;
}
function Ajax_DBUpdate(root, database, table, field, type, value, criteria)
{
    Ajax_Init();

    var data = "d=" + database + "&t=" + table + "&f=" + field + "&dt=" + type + "&v="
        + value + "&c=" + criteria;

    Ajax_xh.open("POST", root + "util/ajax/dbupdate.ashx", false);
	Ajax_xh.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

	Ajax_xh.send(data);
}
function Ajax_DBDelete(root, table, criteria, fncReceived)
{
    Ajax_Init();

    if (fncReceived != null)
    {
        Ajax_xh.onreadystatechange = function()
        {
            if (Ajax_xh.readyState == 4)//done
            {
                fncReceived(Ajax_xh.responseText);
            }
        }

        Ajax_xh.open("POST", root + "util/ajax/dbdelete.ashx", true);
    }
    else
    {
        Ajax_xh.open("POST", root + "util/ajax/dbdelete.ashx", false);
    }

	Ajax_xh.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    var data = "t=" + table + "&c=" + criteria;

	Ajax_xh.send(data);
}
function Ajax_DBExecute(root, database, sproc, parameters, fncReceived)
{
    Ajax_Init();

    if (fncReceived != null)
    {
        Ajax_xh.onreadystatechange = function()
        {
            if (Ajax_xh.readyState == 4)//done
            {
                fncReceived(Ajax_xh.responseText);
            }
        }

        Ajax_xh.open("POST", root + "util/ajax/dbexecute.ashx", true);
    }
    else
    {
        Ajax_xh.open("POST", root + "util/ajax/dbexecute.ashx", false);
    }

	Ajax_xh.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    var data = "d=" + database + "&s=" + sproc + "&p=" + parameters;

	Ajax_xh.send(data);
}
function Ajax_RunCode(root, func, parameters, fncReceived)
{
    Ajax_Init();

    if (fncReceived != null)
    {
        Ajax_xh.onreadystatechange = function()
        {
            if (Ajax_xh.readyState == 4)//done
            {
                fncReceived(Ajax_xh.responseText);
            }
        }

        Ajax_xh.open("POST", root + "utils/runcode.ashx", true);
    }
    else
    {
        Ajax_xh.open("POST", root + "utils/runcode.ashx", false);
    }

	Ajax_xh.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    var data = "f=" + func + "&p=" + ((parameters == null) ? "" : parameters);

	Ajax_xh.send(data);
}
function Ajax_RunCodeReturn(root, func, parameters)
{
    Ajax_Init();

    Ajax_xh.open("POST", root + "util/ajax/runcode.ashx", false);
	Ajax_xh.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	Ajax_xh.send("f=" + func + "&p=" + parameters);

	return Ajax_xh.responseText;
}
function Ajax_DBSetting(root, classname, object, settingtype, value)
{
    Ajax_Init();

    var data = "cn=" + classname + "&o=" + object + "&st=" + settingtype + "&v=" + value;

    Ajax_xh.open("POST", root + "util/ajax/dbsetting.ashx", true);
	Ajax_xh.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

	//xh.onreadystatechange = ListenToData;
	Ajax_xh.send(data);
}
function Ajax_Init()
{
	Ajax_xh = Sarissa.getXmlHttpRequest();
}
function Ajax_LogOutput(responseText)
{

}