//class Ajax
function Ajax()
{
	//initialice variables
	this.preloader_image = '/plantillas/imagenes/ajax-loader.gif';
	this.preloader_container = null;
}

Ajax.prototype.preloader_show = function()
{
	if (this.preloader_container != null)
	{
		document.getElementById(this.preloader_container).innerHTML = "<img src='" + this.preloader_image + "'>";
	}
}

Ajax.prototype.preloader_hide = function()
{
	if (this.preloader_container != null)
	{
		document.getElementById(this.preloader_container).innerHTML = "";
	}
}

Ajax.prototype.initialize = function()
{
	xmlhttp = false;
	try 
	{
	    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} 
	catch (e) 
	{
	    try 
	    {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	    } 
	    catch (E) 
	    {
		xmlhttp = false;
	    }
	}
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') 
	{
	    xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

Ajax.prototype.ondone = null;

Ajax.prototype.get = function(url)
{
	var instance = this;
	instance.preloader_show();		
	var xmlhttp = this.initialize();

	var async = (this.ondone != null);
	xmlhttp.open("GET", url, async);
	if (async == true)
	{
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
			{
				instance.ondone(xmlhttp.responseText);
				instance.preloader_hide();					
			}
		}
	}
	xmlhttp.send(null);
}


Ajax.prototype.post = function(url, params)
{
	var instance = this;
	instance.preloader_show();	
	var xmlhttp = this.initialize();

	var async = (this.ondone != null);
	xmlhttp.open("POST", url, async);
	if (async == true)
	{
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
			{
				instance.ondone(xmlhttp.responseText)
				instance.preloader_hide();	
			}
		}
	}
	
	var body = "";
	for (var key in params)
		body += key + "=" + params[key] + "&";
	
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  	xmlhttp.setRequestHeader("Content-length", body.length);
  	xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.send(body);
}

function getDictioranyFormsParams(formName)
{
	var formulario = document.forms[formName];

	var params = {};
	for(var i=0; i < formulario.elements.length; i++)
	{
		if (!(formulario.elements[i].type == "checkbox") || formulario.elements[i].checked)
		{
			params[formulario.elements[i].name] = formulario.elements[i].value;
		}
	}
	return params;
}



function ejecutarScript(pagina)
{
	var xmlhttp = false;
	var async = true;
	try 
	{
	    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} 
	catch (e) 
	{
	    try 
	    {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	    } 
	    catch (E) 
	    {
		xmlhttp = false;
	    }
	}

	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') 
	{
	    xmlhttp = new XMLHttpRequest();
	}
	pagina = pagina;
	xmlhttp.open("GET", pagina, async);
	if (async == true)
	{
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
		{
		    eval(xmlhttp.responseText);
		}
	}
	}
	xmlhttp.send(null);
}


function ejecutar(pagina, idcontainer) 
{
	var xmlhttp = false;
	var async = true;
	if (idcontainer == null)
	{async = false;}
	try 
	{
	    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} 
	catch (e) 
	{
	    try 
	    {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	    } 
	    catch (E) 
	    {
		xmlhttp = false;
	    }
	}

	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') 
	{
	    xmlhttp = new XMLHttpRequest();
	}
	var obj = document.getElementById(idcontainer);
	pagina = pagina;
	xmlhttp.open("GET", pagina, async);
	if (async == true)
	{
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
			{
				obj.innerHTML = xmlhttp.responseText;
			}
		}
	}
	xmlhttp.send(null);
}


function ejecutarPost(formulario_name, idcontainer) 
{
	var xmlhttp = false;
	var async = false;
	if (idcontainer != null)
	{
		async = true;
		var container = document.getElementById(idcontainer)
	}
	try 
	{
	    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} 
	catch (e) 
	{
	    try 
	    {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");	
	    } 
	    catch (E) 
	    {
		xmlhttp = false;
	    }
	}

	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') 
	{
	    xmlhttp = new XMLHttpRequest();
	}
	
	formulario = document.forms[formulario_name];
	var body = "";
	for(var i=0; i < formulario.elements.length; i++)
	{
		if (!(formulario.elements[i].type == "checkbox") || formulario.elements[i].checked)
		{
			body += formulario.elements[i].name + '=' + formulario.elements[i].value + '&'
		}
	}
	
	xmlhttp.open("POST", formulario.action, async);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  	xmlhttp.setRequestHeader("Content-length", body.length);
  	xmlhttp.setRequestHeader("Connection", "close");
  	
	if (async == true)
	{
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
			{
				container.innerHTML = xmlhttp.responseText;
			}
		}
	}
	xmlhttp.send(body);
}


function Request(function_name, opt_argv)
{
    // If optional arguments was not provided, create it as empty
    if (!opt_argv)
        opt_argv = new Array();
    // Find if the last arg is a callback function; save it
    var callback = null;
    var len = opt_argv.length;
    if (len > 0 && typeof opt_argv[len-1] == 'function') {
        callback = opt_argv[len-1];
        opt_argv.length--;
    }
    var async = (callback != null);
    // Encode the arguments in to a URI
    var query = 'action=' + encodeURIComponent(function_name);
    for (var i = 0; i < opt_argv.length; i++) {
        var key = 'arg' + i;
        //var val = JSON.stringify(opt_argv[i]);
        var val = opt_argv[i];
        query += '&' + key + '=' + encodeURIComponent(val);
    }
    query += '&time=' + new Date().getTime(); // IE cache workaround

    // See http://en.wikipedia.org/wiki/XMLHttpRequest to make this cross-browser compatible
    var xmlhttp = new XMLHttpRequest();

    // Create a 'GET' request w/ an optional callback handler 
    xmlhttp.open('GET', '/rpc?' + query, async);
    
    if (async) {
        xmlhttponreadystatechange = function() {
            if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                var response = null;
                try {
                    response = JSON.parse(xmlhttp.responseText);
                } catch (e) {
                    response = xmlhttp.responseText;
                }
                callback(response);
            }
        }
    }
    // Make the actual request
    xmlhttp.send(null);
}

function InstallFunction(obj, name)
{
    obj[name] = function() { Request(name, arguments); }
}

function $(id)
{
    return document.getElementById(id);
}

var server = {};


function show_hide()
{
	var _ul = document.getElementById("sublocations")
	if (_ul.style.display == "none")
	{
		_ul.style.display = "block";
		document.getElementById("img_switcher").src = "./plantillas/imagenes/arrowUp.gif";
	}
	else
	{
		_ul.style.display = "none";
		document.getElementById("img_switcher").src = "./plantillas/imagenes/arrowDown.gif";
	}
	ejecutar("/gestionubicaciones?ac=mostrarocultar&m=" + _ul.style.display, null);
}
