/**
* Copyright 2006 Mirica Mugurel mmugur81@yahoo.com
*/
var bDebugAjax = false;

//clasa rezultat //se defineste ca o functie in javascript
function sql_res() {
	this.bSuccess		= 0;
	this.sErrorMessage	= '';
	this.nFields		= 0;
	this.nRows			= 0;
	this.aResult		= new Array();
}

//var etg_path; definita inaintea includerii scriptului
var path = (typeof(etg_path) == "undefined")? '' : etg_path;

var req;
var fn_res;
var obj_res = new sql_res();

function mysql_query(sqlFile, qString, fnResult) {	
	//qString data for posting to file: sqlFile
	fn_res = fnResult;
	var url= path + sqlFile;
	qString += '&no_ses=1';//no session verification
	//alert('fn mysql_query: '+url+' | qString ' +  qString);
	if (window.XMLHttpRequest) { // Non-IE browsers
	    req = new XMLHttpRequest();
	    req.onreadystatechange = mysql_process_result;
	    try {
	        req.open("POST", url, true);
	    } catch (e) {
	        alert(e);
	    }
	    req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	    req.send(qString);
	 }
	 else if (window.ActiveXObject) { // IE
	    req = new ActiveXObject("Microsoft.XMLHTTP");
	    if (req){
	        req.onreadystatechange = mysql_process_result;
	        req.open("POST", url, true);
	        req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	        req.send(qString);
	    }
	}
}

function mysql_process_result() {
	//alert(req.readyState);
	if (req.readyState == 4) { // Complete
    	//alert(req.status);
    	//alert(req.statusText);
    	//alert(req.responseText);
	    if (req.status == 200) { // OK response

	        //prelucrez informatiile, linie cu linie separate prin '\\'
	        //alert(req.responseText);
	        var tmp = req.responseText.split('\\');
			if (bDebugAjax)
				alert('ajax:mysql_process_result: '+tmp);

	        obj_res.bSuccess		= (tmp[0] == '1');
	        obj_res.sErrorMessage	= tmp[1];
	        obj_res.nFields			= tmp[2];
	        obj_res.nRows			= tmp[3];

	        //matricea
	        obj_res.aResult = new Array();
	        for (i=4; i<tmp.length; i++) {
	        	tmp1 = tmp[i].split('|');
	        	obj_res.aResult[i-4] = new Array();
	            for (j=0; j<tmp1.length; j++){
	            	obj_res.aResult[i-4].push(tmp1[j]);
	            }
	        }

	        fn_res(obj_res);
	    } else {
            //alert("Error: " + req.statusText);
            obj_res.bSuccess = 0;
	    	obj_res.sErrorMessage = req.statusText;
            fn_res(obj_res);
	    }
	}
}


//#####################################################################################
//this function returns only HTML HTML
function ajax_openFile(sFile, qString, fnResult){
	//sFile tb. sa aibe cale absoluta, nu relativa !!!
	fn_res = fnResult;
	var url= path + sFile + '?' + qString;
	if (window.XMLHttpRequest) { // Non-IE browsers
	    req = new XMLHttpRequest();
	    req.onreadystatechange = ajax_process_result;
	    try {
	        req.open("GET", url, true);
	    } catch (e) {
	        alert(e);
	    }
	    req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	    req.send(qString);
	 }
	 else if (window.ActiveXObject) { // IE
	    req = new ActiveXObject("Microsoft.XMLHTTP");
	    if (req){
	        req.onreadystatechange = ajax_process_result;
	        req.open("GET", url, true);
	        req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	        req.send("");
	    }
	}
}

function ajax_process_result() {
	//alert(req.readyState);
	if (req.readyState == 4) { // Complete
    	//alert(req.status);
    	//alert(req.statusText);
    	//alert(req.responseText);
	    if (req.status == 200) { // OK response
	        //alert(req.responseText);

			//send the responsee to user function
	        fn_res(req.responseText);
	    } else {
            //alert("Error: " + req.statusText);
            //send error to client function
            fn_res(req.statusText);
	    }
	}
}

var fs_id_sel = '';
var fs_fnCallback = '';
var fs_bFirstnull = false;

function ajax_fillSelect(id_sel, sUrl, sWaitText, fnCallback, bFirstnull){
	//fnCallback is used to notify caller
	if (!getById(id_sel))
		return;
	if (!sWaitText)
		sWaitText = '(loading...)';
		
	//clear select
	ctr_sel = getById(id_sel);
	while (ctr_sel.length> 0) {
    	ctr_sel.remove(0);
	}
	ctr_sel.options.add(new Option (sWaitText, -1), -1);
	
	//break url
	aTmp = sUrl.split('?');
	sqlFile = aTmp[0];
	qString = (aTmp[1])? aTmp[1] : '';
	
	fs_id_sel = id_sel;
	fs_fnCallback = fnCallback;
	fs_bFirstnull = bFirstnull;
	
	//alert('fncall: mysql_query('+sqlFile+', '+qString+', ajax_fillSelect_process_result)');
	mysql_query(sqlFile, qString, ajax_fillSelect_process_result);
}

function ajax_fillSelect_process_result(obj_res){
	//alert('fs_id_sel = '+fs_id_sel);
	//alert('got something [success:'+obj_res.bSuccess+'] | result: '+obj_res.aResult);
	ctr_sel = getById(fs_id_sel);
    ctr_sel.remove(0);//remove sWaitText
	//alert(ctr_sel);
	
	if (fs_bFirstnull)
		ctr_sel.options.add(new Option (' * * * ', 0), -1);
	
	if (obj_res.bSuccess){
		for (i=0; i<obj_res.nRows; i++) {
			ctr_sel.options.add(new Option (obj_res.aResult[i][1], obj_res.aResult[i][0]), -1);
		}
	}
	
	tmp_sel = fs_id_sel;
	tmp_callBack = fs_fnCallback;
	fs_id_sel = '';
	fs_fnCallback = '';
	fs_bFirstnull = false;
	
	//notify caller
	if (tmp_callBack)
		eval(tmp_callBack+'("'+tmp_sel+'")');
	
}



function loadAjaxData(elementId, url){ 
	var req = null; 

	document.getElementById(elementId).innerHTML = "Se incarca...";
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		if (req.overrideMimeType) 
			req.overrideMimeType('text/html');
	} 
	else 
		if (window.ActiveXObject) {
			try {
				req = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e)
		{

			try {

				req = new ActiveXObject("Microsoft.XMLHTTP");

			} catch (e) {}

		}

	}
	req.onreadystatechange = function() { 
		document.getElementById(elementId).innerHTML = "Wait for server...";
		if (req.readyState == 4) {
			//alert('ready 4');
			if (req.status == 200) {
				//alert('ready 200 ' + document.getElementById(elementId) + ' raspuns '+req.responseText);
				document.getElementById(elementId).innerHTML  = req.responseText;	
			}
			else
				document.getElementById(elementId).innerHTML = "Eroare la incarcare (code " + req.status + " " + req.statusText + ")";
		} 
	}; 
	req.open("GET", url, true); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	req.send(null); 
} 