/*
function Check_ajax_pool()
function Http_request( str_url, str_params, str_return_action, str_error_action, n_action_late )
function Http_request_advanced( str_url, str_params, str_oncomplete_action, str_onerror_action, n_oncomplete_late, str_method, str_response_type )
function Initialize_HTTP_object()
function Close_HTTP_object()
function Http_request_data_arriving()
function Get_XML_object_from_text( str_XML )
function Get_form_query_string( obj_form )
function Submit_form_by_AJAX( obj_form, str_return_action, str_error_action, n_late, b_loading_animation, b_XML )
function Submit_form_by_AJAX_onComplete( str_form_id )
function Load_frame_HTML_by_AJAX( str_url, str_params, str_target_frame_id, str_action_oncompleted, n_action_late )
function Load_frame_HTML_by_AJAX_onComplete( str_HTML )
function Execute_HREF_by_Ajax( obj_href )
function Execute_HREF_by_Ajax_onComplete()
function XML_execute( obj_XML )
function Keep_session_active( b_staring )
*/

var m_obj_http;
var m_array_ajax_request = new Array();
var m_obj_ajax_response = new Object();
var m_b_display_ajax_error = false;

function Check_ajax_pool()
{
	var obj_array_request;
	if(m_array_ajax_request.length > 0)
	{
		if(!m_obj_http)
		{
			obj_array_request = m_array_ajax_request[0].split("#@#");
			m_array_ajax_request.shift();
			Http_request_advanced(obj_array_request[0], obj_array_request[1], obj_array_request[2], obj_array_request[3], obj_array_request[4], obj_array_request[5], obj_array_request[6]);
		}
		//Echo("Check_ajax_pool(" + m_array_ajax_request.length + ")");
		setTimeout("Check_ajax_pool();", 100);
	}
}

function Http_request( str_url, str_params, str_return_action, str_error_action, n_action_late )
{
	//Echo("Http_request: " + str_url + "?" + str_params + " --> " + str_return_action);
	return(Http_request_advanced(str_url, str_params, str_return_action, str_error_action, n_action_late, "POST", (str_return_action == "XML_execute") ? "XML" : "HTML"));
}

function Http_request_advanced( str_url, str_params, str_oncomplete_action, str_onerror_action, n_oncomplete_late, str_method, str_response_type )
{
	//Echo(str_url);Echo(str_params);Echo(str_oncomplete_action);Echo(str_onerror_action);Echo(n_oncomplete_late);Echo(str_method);Echo(str_response_type);
	var i;
	var b_result = false;
	var obj_array_request;
	var str_join_request;
	if(m_obj_http)
	{
		obj_array_request = new Array();
		obj_array_request.push(str_url); //[0]
		obj_array_request.push(str_params); //[1]
		obj_array_request.push(str_oncomplete_action); //[2]
		obj_array_request.push(str_onerror_action); //[3]
		obj_array_request.push(n_oncomplete_late); //[4]
		obj_array_request.push(str_method); //[5]
		obj_array_request.push(str_response_type); //[6]
		str_join_request = obj_array_request.join("#@#");
				
		for(i=0; i<m_array_ajax_request.length; i++)
		{
			if(m_array_ajax_request[i] == str_join_request)
			{
				m_array_ajax_request.splice(i, 1);
			}
		}
		m_array_ajax_request.push(str_join_request);
		if(m_array_ajax_request.length == 1)
		{
			setTimeout("Check_ajax_pool();", 1000);
		}
		b_result = true;
		//Echo("POOL:" + str_params);
	}
	else if(Initialize_HTTP_object())
	{
		//Echo(str_params);
		str_method	= (str_method == "")		? "POST"		: str_method;
		str_url		= (str_method == "POST")	? str_url		: str_url + "?" + str_params;
		str_params	= (str_method == "POST")	? str_params	: null;
		
		m_obj_ajax_response = new Object();  
		m_obj_ajax_response.OnComplete = str_oncomplete_action;
		m_obj_ajax_response.OnError = str_onerror_action;
		m_obj_ajax_response.OnCompleteLate = n_oncomplete_late;
		m_obj_ajax_response.responseType = str_response_type;
		m_obj_http.open(str_method, str_url, true);
		m_obj_http.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=iso-8859-1");
		if(str_params)
		{
			m_obj_http.setRequestHeader("Content-length", str_params.length);
		}
		m_obj_http.send(str_params);
		b_result = true;
	}
	return(b_result);
}

function Initialize_HTTP_object()
{
	var b_result = true;
	try
	{
		if		(window.XMLHttpRequest)	{	m_obj_http = new XMLHttpRequest();					} // Mozilla, Safari, ...
		else if (true)					{	m_obj_http = new ActiveXObject("Microsoft.XMLHTTP");} // For all other navigator
		else if	(window.ActiveXObject)	{	m_obj_http = new ActiveXObject("Msxml2.XMLHTTP"); 	} // IE
		m_obj_http.onreadystatechange = function() { Http_request_data_arriving(); };
	}
	catch(err) { b_result = false; }
	return(b_result);
}

function Close_HTTP_object()
{
	m_obj_ajax_response = null;
	m_obj_http = null;
}

function Http_request_data_arriving()
{
	if(m_obj_http)
	{
		if(m_obj_http.readyState == 4)
		{
		    if(m_obj_http.status == 200)
		    {
				if(m_obj_ajax_response.responseType == "XML")
				{
					m_obj_ajax_response.Return = (m_obj_http.responseXML) ? m_obj_http.responseXML : m_obj_http.responseText;
				}
				else
				{
					m_obj_ajax_response.Return = m_obj_http.responseText;
				}
				//Echo(m_obj_ajax_response.Return);
				if(m_obj_ajax_response.OnComplete)
				{
					if(m_obj_ajax_response.OnCompleteLate)
					{
						setTimeout("try { " + m_obj_ajax_response.OnComplete + "(m_obj_ajax_response.Return); } catch(err){ if(m_b_display_ajax_error) { alert('Error on OnComplete function executing...'); }} Close_HTTP_object();", m_obj_ajax_response.OnCompleteLate);
					}
					else
					{
						eval(m_obj_ajax_response.OnComplete + "(m_obj_ajax_response.Return);");
						Close_HTTP_object();
					}
				}
				else
				{
					Close_HTTP_object();
				}
			}
			else if(m_obj_http.status == 500)
			{
				if(m_b_display_ajax_error)
				{
					alert(m_obj_http.responseText);
				}
			    if(m_obj_ajax_response.OnError)
			    {
			        eval(m_obj_ajax_response.OnError + "(m_obj_http.responseText);");
			    }
				Close_HTTP_object();
			}
		}
	}
}

function Get_XML_object_from_text( str_XML )
{
	var obj_XML;
	var obj_parser;
	try
	{
		//Internet Explorer
		obj_XML = new ActiveXObject("Microsoft.XMLDOM");
		obj_XML.async = "false";
		obj_XML.loadXML(str_XML);
	}
	catch(err)
	{
		try
		{
			//Firefox, Mozilla, Opera, etc.
			obj_parser = new DOMParser();
			obj_XML = obj_parser.parseFromString(str_XML, "text/xml");
		}
		catch(err) { alert("Error on obj_XML creation") }
	}
	return(obj_XML);
}

function Submit_form_by_AJAX( obj_form, str_return_action, str_error_action, n_late, b_loading_animation, b_XML )
{
	var str_form_id = obj_form.id;
	var n_form_index = 1;
	var obj_image_loading = document.getElementById("image_loading_by_ajax");
	var obj_submit_button;
	var obj_coords;
	var obj_inputs = obj_form.getElementsByTagName("input");

	if(b_loading_animation)
	{
		//Search submit and hide submit and reset
		for(i=0; i<obj_inputs.length; i++)
		{
			if(obj_inputs[i].type == "submit")
			{
				obj_submit_button = obj_inputs[i];
			}
			if(obj_inputs[i].type == "submit" || obj_inputs[i].type == "reset")
			{
				obj_inputs[i].style.visibility = "hidden";
			}
		}

		//Search or create Id for the form
		if(str_form_id == "")
		{
			while(document.getElementById("Form" + n_form_index))
			{
				n_form_index++;
			}
			str_form_id = "Form" + n_form_index;
			obj_form.setAttribute("id", str_form_id);
		}
		
		//Add image loading over submit button
		if(obj_submit_button)
		{
			obj_coords = Get_object_absolute_coords(obj_submit_button);
			if(!obj_image_loading)
			{
				obj_image_loading = document.createElement("span");
				obj_image_loading.setAttributeNode(CreateAttribute("id", "image_loading_by_ajax"));
				obj_image_loading.setAttributeNode(CreateAttribute("class", "C_ICON_LOADING"));
				document.getElementsByTagName("body")[0].appendChild(obj_image_loading);
			}
			obj_image_loading.style.position = "absolute";
			obj_image_loading.style.left = (obj_coords.left + (obj_submit_button.clientWidth / 2) - 8) + "px";
			obj_image_loading.style.top = (obj_coords.top + (obj_submit_button.clientHeight / 2) - 8) + "px";
			obj_image_loading.style.display = "block";
		}
		str_return_action = "Submit_form_by_AJAX_onComplete('" + str_form_id + "');" + str_return_action;
	}
	return(Http_request_advanced(""+obj_form.action, "ajax=1&" + Get_form_query_string(obj_form), str_return_action, str_error_action, n_late, "POST", b_XML ? "XML" : "HTML"));
}

function Submit_form_by_AJAX_onComplete( str_form_id )
{
	var obj_form = document.getElementById(str_form_id);
	var obj_inputs = obj_form.getElementsByTagName("input");
	var obj_image_loading = document.getElementById("image_loading_by_ajax");
	var i;

	if(obj_image_loading)
	{
		obj_image_loading.style.display = "none";
	}
	
	for(i=0; i<obj_inputs.length; i++)
	{
		if(obj_inputs[i].type == "submit" || obj_inputs[i].type == "reset")
		{
			obj_inputs[i].style.visibility = "visible";
		}
	}
}

var m_str_target_frame_id = "";
var m_str_load_frame_HTML_oncompleted = "";

function Load_frame_HTML_by_AJAX(str_url, str_params, str_target_frame_id, str_action_oncompleted, n_action_late)
{
	var obj_frame = document.getElementById(str_target_frame_id);
	var n_width = obj_frame.clientWidth || obj_frame.offsetWidth;
	var n_height = obj_frame.clientHeight || obj_frame.offsetHeight;
	var str_loading_img = "C_ICON_LOADING";
	var str_loading_width = 16;
	if ((n_width > 140) && (n_height > 140)) {
		str_loading_img = "C_ICON_LOADING_66x66";
		str_loading_width = 66;
	}
	var n_padding_left = Math.round((n_width - str_loading_width) / 2);
	var n_padding_top = Math.round((n_height - str_loading_width) / 2);
	var n_padding_bottom = Math.round((n_height - str_loading_width) / 2);

	if (n_padding_top > 160)
	{
		n_padding_bottom = n_padding_bottom + n_padding_top - 50;
		n_padding_top = 50;
	}
	if(!n_action_late && n_action_late != 0)
	{
		n_action_late = 1000;
	}
	obj_frame.innerHTML = "<div style=\"padding: " + n_padding_top + "px " + n_padding_left + "px " + n_padding_bottom + "px " + n_padding_left + "px; text-align: center;\"><center><span class=\"" + str_loading_img + "\"></span></center></div>";
	return(Http_request(str_url, "ajax=1&" + str_params, "m_str_target_frame_id=\"" + str_target_frame_id + "\";m_str_load_frame_HTML_oncompleted=\"" + str_action_oncompleted + "\";Load_frame_HTML_by_AJAX_onComplete", "", n_action_late));
}
function Load_frame_HTML_by_AJAX_onComplete( str_HTML )
{
	if(str_HTML.indexOf("<?xml ") >= 0)
	{
		XML_execute(Get_XML_object_from_text(str_HTML));
	}
	else if(str_HTML.toLowerCase().indexOf("<html") >= 0)
	{
		document.location = document.location;
	}
	else
	{
		document.getElementById(m_str_target_frame_id).innerHTML = str_HTML;
		eval(m_str_load_frame_HTML_oncompleted);
	}
}

var m_obj_array_HREF_by_Ajax_element = new Array();

function Execute_HREF_by_Ajax( obj_href )
{
	var n_pos_question = obj_href.href.indexOf("?");
	var str_action = obj_href.href.substr(0, n_pos_question);
	var str_params = obj_href.href.substr(n_pos_question + 1);
	
	obj_href.setAttribute("previousInnerHTML", obj_href.innerHTML);
	obj_href.innerHTML = "<span class=C_ICON_LOADING></span>";
	m_obj_array_HREF_by_Ajax_element.push(obj_href);
	return(Http_request_advanced(str_action, "ajax=1&" + str_params, "Execute_HREF_by_Ajax_onComplete();XML_execute", "", 1000, "GET", "XML"));
}

function Execute_HREF_by_Ajax_onComplete()
{
	var obj_href = m_obj_array_HREF_by_Ajax_element[0];
	m_obj_array_HREF_by_Ajax_element.shift();
	
	obj_href.innerHTML = obj_href.getAttribute("previousInnerHTML");
	obj_href.setAttribute("previousInnerHTML", "");
}

function XML_execute( obj_XML )
{
	var i;
	var obj_ROOT;
	var obj_node;
	var str_element_id;
	var str_value;
	var str_action;
	var str_action_tagname;

	if(typeof(obj_XML) == "string")
	{
		obj_XML = Get_XML_object_from_text(obj_XML);
	}
	if(typeof(obj_XML) == "object")
	{
		var obj_ROOT = obj_XML.getElementsByTagName("ROOT")[0];
		if(obj_ROOT)
		{
			for(i=0; i<obj_ROOT.childNodes.length; i++)
			{
				obj_node = obj_ROOT.childNodes[i];
				switch(obj_node.tagName.toUpperCase())
				{
					case "COMMAND":
						switch(obj_node.getElementsByTagName("CMD")[0].childNodes[0].nodeValue.toUpperCase())
						{
							case "JAVASCRIPT":	eval(unescape(obj_node.getElementsByTagName("SCRIPT")[0].childNodes[0].nodeValue));			break;
							case "MESSAGEBOX":	alert(unescape(obj_node.getElementsByTagName("TEXT")[0].childNodes[0].nodeValue));			break;
							case "REDIRECT":	document.location = obj_node.getElementsByTagName("URL")[0].childNodes[0].nodeValue;		break;
							case "REDIRECTTOP":	top.document.location = obj_node.getElementsByTagName("URL")[0].childNodes[0].nodeValue;	break;
							case "INNERHTML":	str_element_id = obj_node.getElementsByTagName("ELEMENTID")[0].childNodes[0].nodeValue;
												str_value = unescape(obj_node.getElementsByTagName("INNERHTML")[0].childNodes[0].nodeValue);
												if(document.getElementById(str_element_id))
												{
													document.getElementById(str_element_id).innerHTML = str_value;
												}
												break;
							case "INNERHTMLCHILD":
												str_element_id = obj_node.getElementsByTagName("ELEMENTID")[0].childNodes[0].nodeValue;
												str_child_tagname = obj_node.getElementsByTagName("CHILDTAGNAME")[0].childNodes[0].nodeValue;
												n_child_index = Val(obj_node.getElementsByTagName("CHILDINDEX")[0].childNodes[0].nodeValue);
												str_value = unescape(obj_node.getElementsByTagName("INNERHTML")[0].childNodes[0].nodeValue);
												if(document.getElementById(str_element_id))
												{
													document.getElementById(str_element_id).getElementsByTagName(str_child_tagname)[n_child_index].innerHTML = str_value;
												}
												break;
							case "ELEMENTDISPLAY":
												str_element_id = obj_node.getElementsByTagName("ELEMENTID")[0].childNodes[0].nodeValue;
												str_value = obj_node.getElementsByTagName("DISPLAY")[0].childNodes[0].nodeValue;
												if(document.getElementById(str_element_id))
												{
													document.getElementById(str_element_id).style.display = str_value;
												}
												break;
							case "ELEMENTDISPLAYCHILD":
												str_element_id = obj_node.getElementsByTagName("ELEMENTID")[0].childNodes[0].nodeValue;
												str_child_tagname = obj_node.getElementsByTagName("CHILDTAGNAME")[0].childNodes[0].nodeValue;
												n_child_index = Val(obj_node.getElementsByTagName("CHILDINDEX")[0].childNodes[0].nodeValue);
												str_value = obj_node.getElementsByTagName("DISPLAY")[0].childNodes[0].nodeValue;
												if(document.getElementById(str_element_id))
												{
													document.getElementById(str_element_id).getElementsByTagName(str_child_tagname)[n_child_index].style.display = str_value;
												}
												break;
							case "ELEMENTOPACITY":
												str_element_id = obj_node.getElementsByTagName("ELEMENTID")[0].childNodes[0].nodeValue;
												str_value = obj_node.getElementsByTagName("OPACITY")[0].childNodes[0].nodeValue;
												Set_object_opacity(document.getElementById(str_element_id), Val(str_value));
												break;
							case "QUESTION":	str_question = unescape(obj_node.getElementsByTagName("THEQUESTION")[0].childNodes[0].nodeValue);
												str_action_tagname = confirm(str_question) ? "ACTIONYES" : "ACTIONNO";
												str_action = unescape(obj_node.getElementsByTagName(str_action_tagname)[0].childNodes[0].nodeValue);
												if(str_action != "")
												{
													if(str_action.substr(0, 11) == "javascript:")
													{
														eval(str_action.substr(11));
													}
													else
													{
														document.location = str_action;
													}
												}
												break;
							case "CLASSNAME":
												str_element_id = obj_node.getElementsByTagName("ELEMENTID")[0].childNodes[0].nodeValue;
												str_value = obj_node.getElementsByTagName("CLASSNAME")[0].childNodes[0].nodeValue;
												document.getElementById(str_element_id).className = str_value;
												break;
							default: //alert(obj_node.getElementsByTagName("CMD")[0].childNodes[0].nodeValue.toUpperCase());
						}
						break;
				}
			}
		}
	}
}

var m_b_keep_session_active = false;

function Keep_session_active( b_staring )
{
	if(b_staring)
	{
		m_b_keep_session_active = true;
		setTimeout("m_b_keep_session_active=false;", 30 * 60 * 1000);
	}
	else
	{
		Http_request("/JobUP20/commands.asp", "Ajax=1&cmd=KeepSessionActive", "", "");	
	}
	if(m_b_keep_session_active)
	{
		setTimeout("Keep_session_active(false);", 4 * 60 * 1000);
	}
}