var historyData = new Array();
var ajaxRequestStarted = false;
var curWindowIDUrl = "";
function loadPage(pageName, cID, shortLink)
{
	if(ajaxRequestStarted == true) return;
	if(typeof cID == "undefined") cID = "content";
	ajaxRequestStarted = true;
	showPleaseWait();
	currentID = cID;
	
	var url = "commands_ajax.php?cmd=loadPage&page="+pageName + curWindowIDUrl;

	var tt = new AjaxRequest(url, GET_METHOD, loadPageHandler, null);

	var curHist = detectPageHistory();

	setHistory(curHist);
	tt.send();
	
	//if(typeof shortLink != "undefined") changeLocation("#" + shortLink);
	changeLocation("#" + pageName);
}

function loadPageHandler(resp)
{
	hidePleaseWait();
	ajaxRequestStarted = false;
	//alert("aa");
	get_obj(currentID).innerHTML = resp.responseText;
	//alert(document.getElementsByTagName("script"));
}

function submitAjaxForm(fid)
{
	if(ajaxRequestStarted) {
		alert("Please wait, until ajax form submit will finished.");
		return false;
	}
	
	showPleaseWait();
	if(typeof fid == "undefined") {
		alert("Invalid form");
		return false;
	}
	
	var frm = get_obj(fid);
	if(frm == null) {
		alert("Invalid form.");
		return false;
	}
//	if(!validateForm()) return false;
	retrieved = false;
	var txt = "";
	var req = new Array();
	var elem = frm.elements;
	var k = -1;
	for(var i=0;i<elem.length;i++) 
	{
		var el = elem[i];
		var elemType = el.type;
		var elemName = el.name;
		var elemID = el.id;
		var elemVal  = el.value;
		var elemText = el.text;
//		if(elemID != "" && elemName != "" && elemID != elemName) {
//			alert("Invalid element : " + elemID + " : " + elemName);
//		}

		if(elemName == "") continue;
		
		if(el.disabled == true) continue;
		
		data = "";
		if((elemType == "checkbox" || elemType == "radio") && el.checked)
		{
			data = elemVal;
		}
		
		if(elemType == "select-one"){
			lst = getSelectedOptionObj(el.id, true);
			//alert(elemType + " : " + el.id);
			//alert(lst);
			data = lst.join(",");
		}
		if(elemType == "select-multiple"){
			lst = getSelectedOptionObj(el.id, false);
			alert(elemType + " : " + el.id);
			alert(lst);
			if(lst == null || lst.length == 0) continue;
			data = lst.join(",");
		}
		if(elemType == "text"  || elemType == "password" || elemType == "hidden") {
			data = elemVal;
		}
		if(elemType == "textarea") {
			data = elemVal;	
		}
		//alert(elemName + " : " + elemType);
		if(data == "") continue;
		txt += elemName + " [ " + elemType + " ]  :::  " + data + "\n";
		req[++k] = elemName + "=" + encodeURIComponent(data);
	}
	if(txt == "") {
		// nothing to send
		return false;
	}
	
	req[++k] = "wid=" + encodeURIComponent(curWindowID);
	reqStr = req.join("&");
	reqUrl = "commands.php";
	
	ajaxRequestStarted = true;
	
	request = new AjaxRequest(reqUrl, "POST", formSubmitHandler, false, reqStr);

	headerNames = ["Content-Type", "Content-Length", "Connection"];
	headerValues = ["application/x-www-form-urlencoded", reqStr.length, "close"];
	request.setHeaders(headerNames, headerValues);

	request.send();
}

// es funckcian hetevum a ete submiti ardiunqum patasxan a galis responseText-um error message object, urem@ da
// artapatkervum a ekranin u return false
function formSubmitHandler(resp) 
{
	ajaxRequestStarted = false;
	hidePleaseWait();

	if(nextCallFunction != null) {
		eval(nextCallFunction);
		nextCallFunction = null;
	}
}




function setHistory(nm)
{
	if(nm.length == 0) return;
	historyData.push(nm);
}

function getHistory()
{
	if(historyData.length == 0) return "index";
	return historyData.pop()
}

// JavaScript Document


function ajaxSendCommand(command, action, cID, params) {
	showPleaseWait();
	if(params == undefined) params = ""; else params = "&" + params;

	reqUrl = "commands.php?cmd=ajax&command=" + command + "&action=" + action + params + curWindowIDUrl;

	var isJson = true;
	
	containerID = cID;
	request = new AjaxRequest(reqUrl, "GET", ajaxSendCommand_handler, isJson);
	request.send();
}

function ajaxSendCommand_handler(resp){
	hidePleaseWait();

	var err  = resp.ERROR;
	var data = resp.DATA;

	if(err != "" && err != "null" && typeof err != undefined) {
		alert(err);
	} else {
		if(data == "null" || data == null || data == undefined) {

		} else {
			if(data.html == undefined) {
				alert(showObj(data));
			} else {
				get_obj(containerID).innerHTML = data.html;
				if(data.script != null && data.script != "") {
					try {
						eval(data.script);
					}catch(e){
						alert(e);
					}
				}
			}
			
			
		}
		
	}
	
}


function loadHTML(pageName, cID, extParams) {
	showPleaseWait();
	if(typeof cID == "undefined" || cID == null) cID = "pageContent";

	reqUrl = "/ajax?cmd=loadPage&page=" + pageName + curWindowIDUrl;
	if(typeof extParams != "undefined" && extParams != null && extParams != "") reqUrl += "&" + extParams;

	var isJson = false;
	
	containerID = cID;
	request = new AjaxRequest(reqUrl, "GET", loadHTMLhandler, isJson);
	request.send();
}

function loadHTMLhandler(resp) {
	hidePleaseWait();
	get_obj(containerID).innerHTML = resp.responseText;
	if(typeof nextCallFunction != "undefined" && nextCallFunction != null) {
		eval(nextCallFunction);
		nextCallFunction = null;
	}
}


function SendCommand(cmd, act, cID, extParams) {
	showPleaseWait();
	if(typeof cID == "undefined" || cID == null) cID = "pageContent";

	reqUrl = "/j/?cmd=cmd&c=" + cmd + "&m=" + act + curWindowIDUrl;
	if(typeof extParams != "undefined" && extParams != null && extParams != "") reqUrl += "&" + extParams;

	var isJson = false;
	
	containerID = cID;
	request = new AjaxRequest(reqUrl, "GET", SendCommandHandler, isJson);
	request.send();
}

function SendCommandHandler(resp) {
	hidePleaseWait();
	//get_obj(containerID).innerHTML = resp.responseText;
	if(typeof nextCallFunction != "undefined" && nextCallFunction != null) {
		eval(nextCallFunction);
		nextCallFunction = null;
	}
}


function ajax_error_handler(msg, obj) {
	alert(msg);
	retrieveStarted = false;
	startDrillDown();
	disconnectFromDAS();
	if(typeof obj.responseText != "undefined" && obj.responseText != undefined && obj.responseText!=null && obj.responseText!="" ) {
		alert(obj.responseText);
	}
	if(typeof tm != "undefined" && tm != null) {
		clearInterval(tm);
		tm = null;
	}
	
	ajaxRequestStarted = false;
	hidePleaseWait(true);
}

function showPleaseWait(){}
function hidePleaseWait(){}
