var req = null;

var READY_STATE_UNINITIALIZED = 0;
var READY_STATE_LOADING = 1;
var READY_STATE_LOADED = 2;
var READY_STATE_COMPLETE = 4;



function loadXMLDoc(url,post)
{
	if (window.XMLHttpRequest) // branch for native XMLHttpRequest object
		req = new XMLHttpRequest();
	else if (window.ActiveXObject) // branch for IE/Windows ActiveX version
		req = new ActiveXObject("Microsoft.XMLHTTP");

	document.body.style.cursor = 'wait';

	if (req)
	{
		//req.overrideMimeType('text/xml');
		req.onreadystatechange = processReqChange;
		if ( post )
		{
			req.open("POST", url, true);
			req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			//req.setRequestHeader('Accept-Charset', 'windows-1251,utf-8;q=0.7,*;q=0.7');
			req.send(post);
		}
		else
		{
			req.open("GET", url, true);
			req.send(null);
		}
	}
	else
		alert("There was a problem communicating with XMLHttpRequest object in your browser!");
}



function processReqChange()
{
	try
	{
		if (req.readyState == READY_STATE_COMPLETE) // only if req shows "complete"
		{
			if (req.status == 200) // only if "OK"
			{
				document.body.style.cursor = 'auto';
				// ...processing statements go here...
				//response = req.responseXML.documentElement;
				//method = response.getElementsByTagName('method')[0].firstChild.data;
				//result = response.getElementsByTagName('result')[0].firstChild.data;
				var p = eval(req.responseText);
				method = p.method;
				result = p.result;
				if(method != 'noop' ) {eval(method+'(result)');}
			}
			else
			{
				document.body.style.cursor = 'auto';
				alert("There was a problem retrieving the XML data:\n" + req.statusText);
			}
		}
	}
	catch( e )
	{
		document.body.style.cursor = 'auto';
		alert('Критическая ошибка: ' + e.description);
	}
}

function noop(el){}


var ajax_Form;
var ajax_InProgress;
var ajax_Complete;
var ajax_Error;

var dlgName='';

function dlgStart(name){
	dlgName = name;
	if ( Browser.version.IE ) {
		$('#overallOpacityWrapper').css('filter','alpha(opacity=30)');
		$('#'+dlgName+'Dialog').css('filter','alpha(opacity=100)');
	} else {
		$('#overallOpacityWrapper').css('opacity','.3');
		$('#'+dlgName+'Dialog').css('opacity','1');
	}
	if(dlgName!='') dlgOpenForm();
}	

function dlgFinish(){
	dlgClose();
	if ( Browser.version.IE ) {
		$('#overallOpacityWrapper').css('filter','alpha(opacity=100)');
	} else {
		$('#overallOpacityWrapper').css('opacity','1');
	}
	$('#'+dlgName+'Dialog').css('visibility','hidden');
}

function dlgOpenForm(){
	$('#dlgForm_'+dlgName).css('display','block');
	$('#dlgWaiting_'+dlgName).css('display','none');
	$('#dlgComplete_'+dlgName).css('display','none');
	$('#dlgError_'+dlgName).css('display','none');
}

function dlgWaiting(){
	$('#dlgForm_'+dlgName).css('display','none');
	$('#dlgWaiting_'+dlgName).css('display','block');
	$('#dlgComplete_'+dlgName).css('display','none');
	$('#dlgError_'+dlgName).css('display','none');	
}

function dlgClose(){
	$('#dlgForm_'+dlgName).css('display','none');
	$('#dlgWaiting_'+dlgName).css('display','none');
	$('#dlgComplete_'+dlgName).css('display','block');
	$('#dlgError_'+dlgName).css('display','none');	
	
}

function dlgError(){
	$('#dlgForm_'+dlgName).css('display','none');
	$('#dlgWaiting_'+dlgName).css('display','none');
	$('#dlgComplete_'+dlgName).css('display','none');
	$('#dlgError_'+dlgName).css('display','block');	
}





function ajax_Init(dlg_name)
{
	ajax_Form = document.getElementById('ajax_Form_'+dlg_name).style;
	ajax_InProgress = document.getElementById('ajax_InProgress_'+dlg_name).style;
	ajax_Complete = document.getElementById('ajax_Complete_'+dlg_name).style;
	ajax_Error = document.getElementById('ajax_Error_'+dlg_name).style;
}


function ajaxTaskForm()
{
	ajax_Form.display = 'block';
	ajax_InProgress.display = 'none';
	ajax_Complete.display = 'none';
	ajax_Error.display = 'none';
}

function ajaxTaskInProgress()
{
	ajax_Form.display = 'none';
	ajax_Error.display = 'none';
	ajax_Complete.display = 'none';
	ajax_InProgress.display = 'block';
}

function ajaxTaskComplete()
{
	ajax_Form.display = 'none';
	ajax_InProgress.display = 'none';
	ajax_Error.display = 'none';
	ajax_Complete.display = 'block';
}

function ajaxTaskError()
{
	ajax_Form.display = 'none';
	ajax_InProgress.display = 'none';
	ajax_Complete.display = 'none';
	ajax_Error.display = 'block';
}

function reqJSON(url, success, error) {

    var CallParams = {};
    CallParams.type = "GET";//params.Method || "POST";
    CallParams.url = url;
    CallParams.processData = true;
    CallParams.dataType = "json";
    if (success) {
		CallParams.success = success;
	}	
    if (error) {
        CallParams.error = error;
    }
    $.ajax(CallParams);
} 

function postJSON(url, params, success, error) {
    var CallParams = {};
    CallParams.type = "POST";//params.Method || "POST";
    CallParams.url = url;
    CallParams.processData = true;
    CallParams.data = params;
    CallParams.dataType = "json";
    CallParams.success = success;
    if (error) {
        CallParams.error = error;
    }
    $.ajax(CallParams);
} 