/*
 * ==================================================
 * Se establecen Constantes que se utilizarán en este 
 * JavaScript
 * ==================================================
 */
  
  var CONSTANTES_DEMANDA = 1;

  var TABLAS_LEMPRESA = -50;
  var TABLAS_TITULACIONES_VI = -19;
  var TABLAS_ESPECIALIDADES = -9;
  var TABLAS_NIVELESOCUPACION_VI = -21;
  var TABLAS_NIVELESOCUPACION_PRE = -70;
  var TABLAS_DNNUSUARIOS_FUNCWEB = 85;
  var TABLAS_USUARIOS = 84;
  

/*
 * ==================================================
 * Establecer Cookie
 * ==================================================
 */
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}


/*
 * ==================================================
 * Obtener Cookie
 * ==================================================
 */
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/*
 * ==================================================
 * Eliminar Cookie
 * ==================================================
 */
function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
};
  
   
  
/*
 * ==================================================
 * Obtiene el id de un campo por su nombre. 
 * Se utiliza porque el DotNetNuke le cambia los ID de una forma aleatoria 
 * ==================================================
 */
function ResuelveId(txt) {
 var i
    for(i=0;i<window.opener.document.forms[0].length;i++) {
      if (window.opener.document.forms[0].elements[i].id.indexOf(txt) != -1){   
             break;
        }
    }
 return window.opener.document.forms[0].elements[i].id;
}

/*
 * ==================================================
 * Obtiene el nombre de un campo por su nombre. 
 * Se utiliza porque el DotNetNuke le cambia los nombres de una forma aleatoria  
 * ==================================================
 */
function ResuelveNom(txt) {
 var i
    for(i=0;i<window.document.forms[0].length;i++) {
        if (window.document.forms[0].elements[i].name.indexOf(txt) != -1){
             break;
        }
    }  
 return window.document.forms[0].elements[i].name;
}

/*
 * ==================================================
 * Realiza la apertura del buscador con el filtro correspondiente
 * ==================================================
 */
function Buscador(idtabla, cod, desc, demof, dir) {  
    var texto
    var filtro
    var Mozilla = ((navigator.appName == "Netscape"));
    var IE4 = ((navigator.userAgent.indexOf("MSIE") != -1));
    
    if (IE4){
    
        if (idtabla == TABLAS_LEMPRESA) {
            filtro = document.getElementById(ResuelveNom('txtCodEmpresa')).value;
        }
 
        if (idtabla == TABLAS_TITULACIONES_VI) {
            filtro = document.getElementById(ResuelveNom('txtCodTitulacion')).value;
        }
    
        if (idtabla == TABLAS_ESPECIALIDADES) {
            filtro = document.getElementById(ResuelveNom('cmbFormaTipo')).value;
        }
        
        if (idtabla == TABLAS_NIVELESOCUPACION_VI) {
		if(demof == CONSTANTES_DEMANDA) {
			filtro = document.getElementById(ResuelveNom('txtCodTrabajo')).value;
		} 
		else {
            	filtro = document.getElementById(ResuelveNom('txtCodPuesto')).value;
		}
        }
        
        if (idtabla == TABLAS_NIVELESOCUPACION_PRE) {
            filtro = document.getElementById(ResuelveNom('txtCodTrabPref')).value;
        }
    
        texto= document.getElementById(ResuelveNom(desc)).value;
        
        window.open(dir + 'Buscador.aspx?demof='+demof+'&idtabla='+idtabla+'&cod='+cod+'&des='+desc+'&filtro='+filtro+'&texto='+texto+'','Buscador','toolbar=no,location=no,menubar=no,scrollbars=yes,directories=no,status=no,resizable=yes,width=470,height=250,left=360,top=360'); 
    
    }
    
	if (Mozilla) { 

	    
	    if (idtabla == TABLAS_LEMPRESA) {
            filtro = document.getElementById('Demandas1_txtCodEmpresa').value;
        }
        
        if (idtabla == TABLAS_TITULACIONES_VI) {
            filtro = document.getElementById('Demandas1_txtCodTitulacion').value;
        }
    
        if (idtabla == TABLAS_ESPECIALIDADES) {
            filtro = document.getElementById('Demandas1_cmbFormaTipo').value;
        }
	    
	    texto=document.getElementById("Demandas1_" + desc).value;
	    
	    window.open(dir + 'BuscadorFirefox.aspx?demof='+demof+'&idtabla='+idtabla+'&cod='+cod+'&des='+desc+'&filtro='+filtro+'&texto='+texto+'','Buscador','toolbar=no,location=no,menubar=no,scrollbars=yes,directories=no,status=no,resizable=yes,width=470,height=250,left=360,top=360'); 
	
	}
	
    
  
}

/*
 * ==================================================
 * Obtiene un parámetro del URL
 * ==================================================
 */
function getURLParam(strParamName){ 
    var strReturn = "";  
    var strHref = window.location.href; 
    if ( strHref.indexOf("?") > -1 ){    
        var strQueryString = strHref.substr(strHref.indexOf("?"));       
        var aQueryString = strQueryString.split("&");   
        for ( var iParam = 0; iParam < aQueryString.length; iParam++ ) {      
            if (aQueryString[iParam].indexOf(strParamName + "=") > -1 ) {
                    var aParam = aQueryString[iParam].split("=");        
                    strReturn = aParam[1];        
                    break;      
                }    
            }  
        } 
    return strReturn;
} 

/*
 * ==================================================
 * Obtiene los datos del gridview del popup del buscador
 * ==================================================
 */
function ObtieneDatosGridView(obj) {  
    
    var CodOrigen;
    var DesOrigen;
    var Mozilla = ((navigator.appName == "Netscape"));
    var IE4 = ((navigator.userAgent.indexOf("MSIE") != -1));   
   
   //Discriminamos segun el explorador
   if (IE4) { 
   
	Cod = obj.document.activeElement.parentElement.parentElement.firstChild.innerText;
	Des = obj.document.activeElement.parentElement.parentElement.firstChild.nextSibling.innerText;
   
   }
   
   if (Mozilla) { 
    
   Cod=obj.document.getElementById("grdbuscar").parentNode.parentNode.parentNode.firstChild.innerHTML;
   Des=obj.document.getElementById("grdbuscar").parentNode.parentNode.firstChild.nextSibling.innerHTML;
   
   }
      
  	
	//Obtenemos	los campos
	CodOrigen=window.opener.document.getElementById(ResuelveId(getURLParam('cod')));
	DesOrigen=window.opener.document.getElementById(ResuelveId(getURLParam('des')));
			
	//Colocamos los datos en los campos
	if(CodOrigen != undefined) CodOrigen.value=Cod;
	if(DesOrigen != undefined) DesOrigen.value=Des;
	    
	//Cerramos la ventana en la que estamos
	window.close();
	
}


/*
 * ==================================================
 * Realiza la apertura del buscador con el filtro correspondiente
 * ==================================================
 */
function BuscadorDemanda(idtabla, cod, desc, demof, dir) {  
    var texto
    var filtro
    var Mozilla = ((navigator.appName == "Netscape"));
    var IE4 = ((navigator.userAgent.indexOf("MSIE") != -1));
    
    if (IE4){
    
        if (idtabla == TABLAS_USUARIOS) {
            filtro = document.getElementById(ResuelveNom('TxtCurriApli')).value;
        }
 
        if (idtabla == TABLAS_DNNUSUARIOS_FUNCWEB) {
            filtro = document.getElementById(ResuelveNom('TxtUserWeb')).value;
        }
        
        texto= document.getElementById(ResuelveNom(desc)).value;
        
        window.open(dir + 'BuscadorDemandas.aspx?demof='+demof+'&idtabla='+idtabla+'&cod='+cod+'&des='+desc+'&filtro='+filtro+'&texto='+texto+'','Buscador','toolbar=no,location=no,menubar=no,scrollbars=yes,directories=no,status=no,resizable=yes,width=470,height=250,left=360,top=360'); 
    
    }
    
	if (Mozilla) { 

	    
	    if (idtabla == TABLAS_LEMPRESA) {
            filtro = document.getElementById('Demandas1_txtCodEmpresa').value;
        }
        
        if (idtabla == TABLAS_TITULACIONES_VI) {
            filtro = document.getElementById('Demandas1_txtCodTitulacion').value;
        }
	    
	    texto=document.getElementById("Demandas1_" + desc).value;
	    
	    window.open(dir + 'BuscadorFirefox.aspx?demof='+demof+'&idtabla='+idtabla+'&cod='+cod+'&des='+desc+'&filtro='+filtro+'&texto='+texto+'','Buscador','toolbar=no,location=no,menubar=no,scrollbars=yes,directories=no,status=no,resizable=yes,width=470,height=250,left=360,top=360'); 
	
	}
	
    
  
}


/*
 * ==================================================
 * Obtiene los datos del gridview del popup del buscador
 * ==================================================
 */
function ObtieneDatosGridDemandantes(obj) {  
    
    var CodOrigen;
    var DesOrigen;
    var Mozilla = ((navigator.appName == "Netscape"));
    var IE4 = ((navigator.userAgent.indexOf("MSIE") != -1));   
   
   //Discriminamos segun el explorador
   if (IE4) { 
    
	Cod = obj.document.activeElement.parentElement.parentElement.firstChild.innerText;
	Des = obj.document.activeElement.parentElement.parentElement.firstChild.nextSibling.nextSibling.innerText + ' ' +obj.document.activeElement.parentElement.parentElement.firstChild.nextSibling.nextSibling.nextSibling.innerText + ' ' + obj.document.activeElement.parentElement.parentElement.firstChild.nextSibling.nextSibling.nextSibling.nextSibling.innerText;
   
   }
   
   if (Mozilla) { 
    
   Cod=obj.document.getElementById("grdbuscar").parentNode.parentNode.parentNode.firstChild.innerHTML;
   Des=obj.document.getElementById("grdbuscar").parentNode.parentNode.firstChild.nextSibling.innerHTML;
   
   }
      
  	
	//Obtenemos	los campos
	CodOrigen=window.opener.document.getElementById(ResuelveId(getURLParam('cod')));
	DesOrigen=window.opener.document.getElementById(ResuelveId(getURLParam('des')));
	
	//Colocamos los datos en los campos
	if(CodOrigen != undefined) CodOrigen.value=Cod;
	if(DesOrigen != undefined) DesOrigen.value=Des;
	    
	//Cerramos la ventana en la que estamos
	window.close();
	
}
 

