function callback_List(res)
{
	if (res.value) {
		el = res.context;
		if(document.all) {
			ele = document.all[el];
		} else {
			ele = document.getElementById(el);
		}
		ele.innerHTML = res.value;
		AjaxUnLoading();
	} else {
		alert('error al procesar solicitud');
		AjaxUnLoading();
	}
}
function callback_SaveAnomalia(res)
{
	if (res.value) {
		el = res.context;
		window.open('/admin/upload.php?anomalia_id='+res.value,'upload','width=400,height=300');
		AjaxUnLoading();
	} else {
		alert('error al procesar solicitud');
		AjaxUnLoading();
	}
}

function ListOrder(myclass, form, order, page, field, value, el) {
	try	
	{
		AjaxLoading();
		MyMethods.ListOrder(myclass, form, order, page, field, value, callback_List, el);
	}
	catch(e)
	{
		alert("Error: " + e.description);
	}
}

function ListAnomalias(myclass, el) {
	try	
	{
		AjaxLoading();
		MyMethods.ListAnomalias(myclass, callback_List, el);
	}
	catch(e)
	{
		alert("Error: " + e.description);
	}
}

function GetAnomalia(myclass, proyecto_id, anomalia_id, el) {
	try	
	{
		AjaxLoading();
		MyMethods.GetAnomalia(myclass, proyecto_id, anomalia_id, callback_List, el);
	}
	catch(e)
	{
		alert("Error 1: " + e.description);
	}
}

function NewAnomalia(myclass, el) {
	try	
	{
		AjaxLoading();
		MyMethods.NewAnomalia(myclass, callback_List, el);
	}
	catch(e)
	{
		alert("Error 1: " + e.description);
	}
}

function SaveAnomalia(myclass, anomalia_id, proyecto_id, tipo_id, estado_id, creador, origen_id, causa_id, ejecutor, aprobador, anom_modulo, anom_resumen, anom_detalle, anom_respuesta, anom_gravedad, anom_prioridad, anom_estimada, anom_fuente, metadata, el) {
	try	
	{
		AjaxLoading();
		MyMethods.SaveAnomalia(myclass, anomalia_id, proyecto_id, tipo_id, estado_id, creador, origen_id, causa_id, ejecutor, aprobador, anom_modulo, anom_resumen, anom_detalle, anom_respuesta, anom_gravedad, anom_prioridad, anom_estimada, anom_fuente, metadata, callback_SaveAnomalia, el);
	}
	catch(e)
	{
		alert("Error 1: " + e.description);
	}
}

function PutFile(myclass, file) {
	try	
	{
		AjaxLoading();
		MyMethods.PutFile(myclass, file, callback_List, el);
	}
	catch(e)
	{
		alert("Error 1: " + e.description);
	}
}

function callback_Elements(res)
{
	el = res.context;
	if (res.value) {
		fillObjects(res.value, el);
	} else {
    	selectClear(el);
	//	alert('error al procesar solicitud');
	}
}

var dfirst_text = "Todas";
var first_text = dfirst_text;
function Elements(myclass, value, ele)
{
	var d = null;
	try	
	{
	    if (value != 0){
			MyMethods.Elements(myclass, value, callback_Elements, ele);
	    } else {
	    	selectClear(ele);
		}
	}
	catch(e)
	{
		alert("Error: " + e.description);
	}
}


function getComunas(value, ele)
{
	var d = null;
	try
	{
	    if (value != 0){
			MyMethods.Elements('comuna', value, callback_Elements, ele);
	    } else {
	    	selectClear(ele);
		}
	}
	catch(e)
	{
		alert("Error: " + e.description);
	}
}


function getCategoria(value, ele)
{
	var d = null;
	try
	{
	    if (value != 0){
			MyMethods.Elements('categoria', value, callback_Elements, ele);
	    } else {
	    	selectClear(ele);
		}
	}
	catch(e)
	{
		alert("Error: " + e.description);
	}
}


function callback_ZonabImg(res)
{
	if (res.value) {
		el = res.context;
		el.src = '/admin/uploads/'+res.value;
	} else {
		alert('error al procesar solicitud');
	}
}
function getZonabImg(myclass, value, ele)
{
	var d = null;
	try
	{
	    if (value != 0){
			MyMethods.getPropertyValue(myclass, 'zonab_archivodef', value, callback_ZonabImg, ele);
	    } else {
			ele.src = 'images/sinimagen.gif';
		}
	}
	catch(e)
	{
		alert("Error: " + e.description);
	}
}

//Esconde las regiones y las comunas cuando el país no es chile
function hideshowregioncomuna(oo){
    v = oo.options[oo.selectedIndex].value;
    var objcomuna = document.getElementById("comuna_id");
    if (!objcomuna){
		var objcomuna = document.getElementById("regi_comuna");
		var objformcomuna = oo.form.regi_comuna;
	} else {
        var objformcomuna = oo.form.comuna_id;
	}
    var objregion = document.getElementById("region_id");
    if (!objregion){
		var objregion = document.getElementById("regi_region");
		var objformregion = oo.form.regi_region;
	} else {
	    var objformregion = oo.form.region_id;
	}
    
    if(v != 42){
//		oo.form.regi_comuna.style.display='none';oo.form.regi_region.style.display='none';
		selectClear(objformregion);
		first_text = "Seleccione una Comuna";
		selectClear(objformcomuna);
		objcomuna.style.display='none';objregion.style.display='none';
	} else {
//		oo.form.regi_comuna.style.display='block';oo.form.regi_region.style.display='block';
		objcomuna.style.display='block';objregion.style.display='block';
	}
}

function callback_Referrer(res)
{
	return true;
}

function saveReferrer(banner_id,ele){
    MyMethods.callClassMethod('banner','updateHits',banner_id,callback_Referrer,ele);
    MyMethods.callClassMethod('referrer','saveReferrer',banner_id,callback_Referrer,ele);
}

function fillSelectFromArray(selectobj, array) {

	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;
		}
	}
	first_text = dfirst_text;
}

// 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) {
	aDatas = _unserializeArray(data);
	selectClear(select);
	fillSelectFromArray(select, aDatas);
	return true;
}

function selectClear(lst){
  lst.options.length = 0;
  opt = new Option(''+first_text+'', '0');
  lst.options[lst.options.length] = opt;
}



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 = 'block';
	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';
	}
}

function AjaxSubmitForm(form) 
{
	for(element in document.elements)
	{
		//
	}
}

