//alert('funciones.js');
var rutaNombreAplicacion=appContext;
function deshabilitarInstitucion(valor, forma, nombre1, nombre2, nombre3,
		nombre4) {
	if (document[forma].country.value == 'MEX') {
		if (valor == '03') {
			document[forma][nombre1].value = '0';
			document[forma][nombre1].disabled = true;
			document[forma][nombre2].value = '0';
			document[forma][nombre2].disabled = true;
			document[forma][nombre3].value = '0';
			document[forma][nombre3].disabled = true;
			document[forma][nombre4].value = '0';
			document[forma][nombre4].disabled = true;
		} else {
			document[forma][nombre1].disabled = false;
			document[forma][nombre2].disabled = false;
			document[forma][nombre3].disabled = false;
			document[forma][nombre4].disabled = false;
		}
	}

}
function addfav()
   {
   if (document.all)
      {
      window.external.AddFavorite
      ("http://www.siicyt.gob.mx/","SIICYT")
      }
   }
// UTILIZADA EN CVU Y VIECYT PM
function compararpassword(nomforma, elemento1, elemento2) {
	// alert("cont1="+document[nomforma].elements[1].value+"con2="+document[nomforma].elements[2].value);
	if (document[nomforma][elemento1].value == document[nomforma][elemento2].value)
		return true;
	return false;
}

function Redirect(direccion) {
	document.location = direccion;
}

//UTILIZADA EN BUZON DE NECESIDADES Y PROYECTOS
function validarClasificacion(nombreForma, nombreCampo, menje) {
	if (window.document[nombreForma][nombreCampo].value == 0) {
		return false;
	}
	return true;
}


//UTILIZADA EN EL JSP OLVIDO PASSWORD
function confirmaEnvioPassword(forma) {

	if (!validateUsuarioForm(forma))
		return false;

	if (confirm("¿Esta seguro de continuar?")) {
		return true;
	}
	return false;
}

function confirmaAccion(mensaje, nombreForma) {

	if (confirm(mensaje)) {
		return true;
		// window.document[nombreForma].submit();
	}

	return false;
}

//REPORTES CVU
function resetCuadrosLista(objeto) {
	objeto.selectedIndex = 0;
}



// ****BUSCADOR******************************/

function ConfirmarInformacion() {
	// alert(this.document.BusquedaForm.palabraClave.value);
	if (window.document.BusquedaForm.palabraClave.value == null
			|| window.document.BusquedaForm.palabraClave.value == "") {
		if (confirm("El despliegue de esta informacion puede tardar varios minutos le sugerimos la opción [Ir] del buscador para seleccionar criterios de busqueda.Desea continuar aun con la busqueda?")) {
			window.document.BusquedaForm.submit();
		}
	} else
		window.document.BusquedaForm.submit();
}

function validarAlgunDatoSeleccionado(form) {

	result = false;

	for (i = 0; i < form.elements.length; i++) {
		if (form.elements[i].type == 'checkbox'
				&& form.elements[i].checked == true)
			result = true;
	}

	if (!result)
		alert("No existen elementos seleccionados");

	return result;
}

function selecciona(form) {
	for (i = 1; i < form.elements.length; i++) {
		if (form.elements[i].type == 'checkbox') {
			if (form.elements[i].checked == true)
				form.elements[i].checked = false;
			else
				form.elements[i].checked = true;
		}
	}
}

var formRadiosSeleccion = function(){
	this.criterioDerecho=0;
	this.criterioIzquierdo=0;
	this.promocion=0;
	this.tipo=0;
	this.tipoPresentacion=0;
}

var seleccionRadios = new formRadiosSeleccion();

function cambiaTipoInvestigador(obj) {
	if (obj.value == 1) // //PARA SNI
	{
		document.getElementById('SNI').style.display = '';
		document.getElementById('BECAS').style.display = 'none';
		document.getElementById('BECAS2').style.display = 'none';
		document.ReportesSeleccionCVU.promocion.disabled = false;
		document.getElementById('tipo_presentacion').value=1;
		document.getElementById('tipo_presentacion').disabled=false;
		document.getElementById('nuevos_criterios').style.display = '';

		for (var counter = 0; counter < document.ReportesSeleccionCVU.tipo_reporte.length; counter++) {
			document.ReportesSeleccionCVU.tipo_reporte[counter].disabled = false;
		}
		// document.ReportesSeleccionCVU.mes.disabled = true;
		// document.ReportesSeleccionCVU.tipo_beca.disabled = true;
	}

	if (obj.value == 4) // /PARA BECAS
	{
		document.getElementById('BECAS').style.display = '';
		document.getElementById('BECAS2').style.display = '';
		document.getElementById('SNI').style.display = 'none';
		document.getElementById('tipo_presentacion').value=1;
		document.getElementById('tipo_presentacion').disabled=false;
		document.getElementById('nuevos_criterios').style.display = '';

		document.ReportesSeleccionCVU.promocion.disabled = false;

		for (var counter = 0; counter < document.ReportesSeleccionCVU.tipo_reporte.length; counter++) {
			document.ReportesSeleccionCVU.tipo_reporte[counter].disabled = false;
		}
		// document.ReportesSeleccionCVU.mes.disabled = false;
		// document.ReportesSeleccionCVU.tipo_beca.disabled = false;
		document.ReportesSeleccionCVU.promocion.options.collection = "promocionesB";

	}

	if (obj.value == 10)// /PARA INVESTIGADORES TOTALES
	{
		document.getElementById('BECAS').style.display = 'none';
		document.getElementById('BECAS2').style.display = 'none';
		document.getElementById('SNI').style.display = 'none';
		document.getElementById('nuevos_criterios').style.display = 'none';
		document.getElementById('tipo_presentacion').value=1;
		document.getElementById('tipo_presentacion').disabled=true;

		document.ReportesSeleccionCVU.promocion.disabled = true;

		//document.ReportesSeleccionCVU.tipo_reporte[0].checked = true;
		document.ReportesSeleccionCVU.tipo_reporte.checked = true;
		for (var counter = 0; counter < document.ReportesSeleccionCVU.tipo_reporte.length; counter++) {
			if (document.ReportesSeleccionCVU.tipo_reporte[counter].value != 1)
				document.ReportesSeleccionCVU.tipo_reporte[counter].disabled = true;
		}
		// document.ReportesSeleccionCVU.mes.disabled = true;
		// document.ReportesSeleccionCVU.tipo_beca.disabled = true;
	}
}

function checarReporte(valor) {
	if (valor == 1) {
		document.ReporteProyectosForm.formatoSalida[0].disabled = true;
		document.ReporteProyectosForm.formatoSalida[1].disabled = true;
		document.ReporteProyectosForm.formatoSalida[2].disabled = true;
	} else {
		document.ReporteProyectosForm.formatoSalida[0].disabled = false;
		document.ReporteProyectosForm.formatoSalida[1].disabled = false;
		document.ReporteProyectosForm.formatoSalida[2].disabled = false;
	}
}

var waitPage = function (action){
	var div = document.getElementById('waitImage');
	if (div){
		if (action=='show'){
			div.style.display='';
		}
		else
			div.style.display='none';
	}		
}


function displayButton(button){
	if (button.type && button.type=='checkbox'){
		pressButton(button, button.checked);
	} else
		if (!buttonsBuscador[button.id]){
			pressButton(button, true);
		}
		else {
			pressButton(button, false);
		}
}

function pressButton(button, press){
	buttonsBuscador[button.id]=press;
	if (button.type && button.type=='checkbox'){
		button.checked=press;
	} else
		if (press)
			button.className='buttonBusqueda buttonPressed';
		else
			button.className='buttonBusqueda buttonUnpressed';
} 


function loadScript(url, callback){
    var script = document.createElement("script")
    script.type = "text/javascript";

    if (script.readyState){  //IE
        script.onreadystatechange = function(){
            if (script.readyState == "loaded" ||
                    script.readyState == "complete"){
                script.onreadystatechange = null;
                callback();
            }
        };
    } else {  //Others
        script.onload = function(){
            callback();
        };
    }

    script.src = url;
    document.getElementsByTagName("head")[0].appendChild(script);
}

function loadCss(url, callback){
    var script = document.createElement("link")
    script.type = "text/css";
	script.rel = "stylesheet";

    if (script.readyState){  //IE
        script.onreadystatechange = function(){
            if (script.readyState == "loaded" ||
                    script.readyState == "complete"){
                script.onreadystatechange = null;
                callback();
            }
        };
    } else {  //Others
        script.onload = function(){
            callback();
        };
    }

    script.href = url;
    document.getElementsByTagName("head")[0].appendChild(script);
}

var waitProgress = function waitGadget(data){
    this.context = data.context;
    this.wait = undefined;
    this.id = !data.id ? 'wait' : data.id;
    this.progressBar = undefined;
    this.interval = !data.interval ? 1000 : data.interval;
    this.load = function(){
        if (!$('#waitImage' + this.id)[0]) {
            var wait = $('<div id="waitImage' + this.id + '" align="center" style="z-index: 200;display:none;position:absolute; width:80%; height:10%; top:250; left:100;">');
			wait.appendTo($('#body'));
            $('<IFRAME style="Z-INDEX:-1;WIDTH:360px;POSITION:absolute;HEIGHT:54px" src="" frameborder="0" style="">').appendTo(wait);
            $('<img src="' + this.context + '/images/cargador01.gif">').appendTo(wait);
            var div = $('<div id="progressbarholder' + this.id + '" style="width:300px;height:20px;background-color: white">');
            $('<div id="progressbarlabel' + this.id + '" style="position:absolute;float: left; text-align: center; width: 300px;"></div>').appendTo(div);
            $('<div id="progressbar' + this.id + '"></div>').appendTo(div);
            div.appendTo(wait);
            $('<button onclick="window[\''+this.id+'\'].cancel();return false;">Cancelar</button>').appendTo(wait);
            $("#progressbar" + this.id).progressbar({
                value: 0
            });
        }
        if ($('#waitImage' + this.id)[0]) {
            $('#progressbarlabel' + this.id).html('Total: 0%');
            $('#progressbar' + this.id).progressbar('option', 'value', 0);
            this.wait = $('#waitImage' + this.id)[0];
            window[this.id] = this;
            this.progressBar = $('#progressbar' + this.id);
            this.progressLabel = $('#progressbarlabel' + this.id);
        }
    }
    this.cancel = function(){
        var options = {
            dataType: 'json',
            type: 'GET',
            url: this.context + this.url,
            data: this.params.cancel,
            extra: this,
            error: function(xhr, r, err){
				this.extra.reset();
            },
            success: function(response, status){
				this.extra.reset();
                if (response) {
                    var r = eval(response)[0];
					alert(r.message)
                }
            }
        }
        $.ajax(options);
    }
    this.reset = function(){
		try {
			this.hide();
			clearInterval(this.timer);
			this.progressBar.progressbar('option', 'value', 0);
			this.progressLabel.html('Total: 0%');
			this.request = 0;
		} catch(e){}
    }
    this.update = function(data, action){
        if (data && data != '' && data.prc && data.prc >= 0) {
            if (data.prc > 100) 
                data.prc = 100;
            if (data.moduleprc && data.moduleprc > 100) 
                data.moduleprc = 100;
            this.progressBar.progressbar('option', 'value', data.prc);
            if (!data.module) 
                this.progressLabel.html('Total: ' + data.prc + '%');
            else 
                if (action) 
                    this.progressLabel.html('Total: ' + data.prc + '%, ' + action + ' ' + data.module + '(' + data.moduleprc + '%)...');
        }
    }
    this.url = data.url;
    this.params = data.params;
    this.busy = false;
    this.start = function(){
        this.show();
        this.timer = setInterval('window.' + this.id + '.query()', this.interval);
    }
    this.request = 0;
	this.process=undefined,
    this.query = function(){
        try {
            if (!this.busy) {
                this.busy = true;
                var options = {
                    dataType: 'json',
                    type: 'GET',
                    url: this.context + this.url,
                    data: this.params.status,
                    extra: this,
                    error: function(xhr, r, err){
                        this.extra.busy = false;
                    },
                    success: function(response, status){
                        this.extra.busy = false;
                        if (response) {
							var r;
							if (response.avancePrct)
								r={
									prc:response.avancePrct,
									status:response.status
								}
							else
                            	r = eval(response)[0];
							if (r) {
								this.extra.update(r);
								this.extra.request++;
								if (r && r.prc >= 100 || (r.status == 'done' && this.extra.request > 2)) {
									this.extra.reset();
								}
							}
                        }
						if (this.extra.process!=undefined){
							this.extra.process(response);
						}
                    }
                }
                $.ajax(options);
            }
        } 
        catch (e) {
            alert(e)
            return false;
        }
        finally {
        }
    }
    
    this.show = function(){
        if (this.wait) {
            this.wait.style.display = '';
        }
    }
    this.hide = function(){
        if (this.wait) {
            this.wait.style.display = 'none';
        }
    }
}

