function callback_PutResult(res)
{
	el = res.context;
	if(document.all) {
		ele = document.all[el];
	} else {
		ele = document.getElementById(el);
	}
	if (res.value) {
		ele.innerHTML = res.value;
		//setTimeout("AjaxUnLoading()", 1000);
	} else {
		ele.innerHTML = "";
		//setTimeout("AjaxUnLoading()", 1000);
	}
}


function ListOrder(myclass, form, order, page, field, value, el) {
	try	
	{
		AjaxLoading();
		MyMethods.ListOrder(myclass, form, order, page, field, value, callback_PutResult, el);
	}
	catch(e)
	{
		alert("Error: " + e.description);
	}
}

function callback_Destacada(res)
{
	if (res.value) {
		el = res.context;
		
		for (i=1; document.listador.elements['item['+ i +']']; i++)	{			
			if(document.all) {
				if(el.name!=document.all['imag_'+i].name)
				{
					document.all['imag_'+i].src = "images/check_off.gif";
					document.all['imag_'+i].className = "N";
				}
			} else {
				if(el.name!=document.getElementById('imag_'+i).name)
				{
					document.getElementById('imag_'+i).src = "images/check_off.gif";
					document.getElementById('imag_'+i).className = "N";
				}
			}
		}

		if(el.className == "S")
		{
			el.src = "images/check_off.gif";
			el.className = "N";
		}
		else
		{
			el.className = "S";
			el.src = "images/check_on.gif";
		}
	}
	else
	{
		alert('Ocurrió un error, no se pudo destacar la imagen seleccionada');
	}
	AjaxUnLoading();
}

function SetDestacada(myclass, imagen_id, ele)
{
	try	
	{
		AjaxLoading();
		activar = "S";
		if(ele.className == "S")
		{
			activar = "N";
		}
		MyMethods.SetDestacada(myclass, imagen_id, activar, callback_Destacada, ele);
	}
	catch(e)
	{
		alert("Error: " + e.description);
	}
}

function GetCompiledTemplate(myclass, method, prop_codigo, tipo_id, el)
{
	try	
	{
		//AjaxLoading();
		MyMethods.GetCompiledTemplate(myclass, method, prop_codigo, tipo_id, callback_PutResult, el);
	}
	catch(e)
	{
		alert("Error: " + e.description);
	}
}

function callback_Elements(res)
{
	el = res.context;
	if (res.value) {
		document.getElementById('spn_sector_id').style.display ='';
		fillObjects(res.value, el, 'Todos los sectores');
		AjaxUnLoading();
	} else {
		//alert('error al procesar solicitud');
		AjaxUnLoading();
		selectClear(el);
		document.getElementById('spn_sector_id').style.display ='none';
		el.options[el.options.length] = new Option('No requiere', 0);
	}
}

function Elements(myclass, _method, value, ele)
{
	var d = null;
	try	
	{
		AjaxLoading();
		MyMethods.Elements(myclass, _method, value, callback_Elements, ele);
	}
	catch(e)
	{
		alert("Error: " + e.description);
	}
}

function callback_ElementsAdmin(res)
{
	el = res.context;
	if (res.value) {
		fillObjects(res.value, el, 'No requiere');
		AjaxUnLoading();
	} else {
		//alert('error al procesar solicitud');
		AjaxUnLoading();
		selectClear(el);
		el.options[el.options.length] = new Option('No requiere', 0);
	}
}


function ElementsAdmin(myclass, _method, value, ele)
{
	var d = null;
	try	
	{
		AjaxLoading();
		MyMethods.Elements(myclass, _method, value, callback_ElementsAdmin, ele);
	}
	catch(e)
	{
		alert("Error: " + e.description);
	}
}

function fillSelectFromArray(selectobj, array, displayNull) {
	if(displayNull) 
	{	
		opt = new Option(displayNull, '0');
		selectobj.options[selectobj.options.length] = opt;
	}
	for(var i = 0; i < array.length; i++) {
		value = array[i]['value'];
		text = array[i]['text'];
		eval('text = ' + text + ";");
		opt = new Option(text, value);
		if (selectobj && value) {
			selectobj.options[selectobj.options.length] = opt;
		}
	}
}

// unserializeArray: deserializa el parametro param y devuelve un arreglo hash 
function _unserializeArray(param) {
	var i, j, text, value, aux;
	objects = param.split("|");
	var aObjects = new Array();
	for(i = 0; i < objects.length; i++) {
		vars = objects[i].split("&");
		var object = new Array();
		for (j = 0; j < vars.length; j++) {
			aux = vars[j].split("=");
			object[aux[0]] = aux[1];
		}
		aObjects[aObjects.length] = object;
	}
	return aObjects;
}

function fillObjects(data, select, displayNull) {
	aDatas = _unserializeArray(data);
	selectClear(select);
	fillSelectFromArray(select, aDatas, displayNull);
	return true;
}

function selectClear(lst){
  lst.options.length = 0;
}





var myDivAjax;
function AjaxLoading(mensaje)
{
	if(!mensaje) {
		mensaje = "CARGANDO...";
	}
	//alert(document.body.clientWidth);
	//myImageAjax = document.createElement("IMG");
	//myImageAjax.src = "/admin/images/html/loading.gif";

	ancho = (mensaje.length * 5)+'px';
	myDivAjax = document.createElement("DIV");
	myDivAjax.style.width = ancho;
	myDivAjax.height = "100px";
	//myDivAjax.style.overflow = "auto";
	myDivAjax.id = "myDivAjax";
	myDivAjax.style.position = "absolute";
	myDivAjax.style.right= 0; 
	myDivAjax.style.top= document.body.scrollTop;
	myDivAjax.style.display = '';
	myDivAjax.style.background = "red";
	myDivAjax.style.color = "#fff";
	myDivAjax.innerHTML = mensaje;
	myDivAjax.style.fontSize = "11px";
	
	//myDivAjax.appendChild(myImageAjax);
	document.body.appendChild(myDivAjax);
}
function AjaxUnLoading()
{
	if(typeof (myDivAjax)=="object")
	{
		myDivAjax.innerHTML = "LISTO";
		myDivAjax.style.display = 'none';
	}
}
