
/**************************************************************************

Funciones Generales de IriNET-V

**************************************************************************/

function abrirVinculo (prefijo, valor, sufijo){
    var url = prefijo + valor + sufijo;
    window.open(url,'PopUp');    
 }

function IsNumeric(sText){

	/**************************************************
	
	Funcion equivalente a la funcion 'IsNumeric' de
	Visual Basic.
	
	**************************************************/

	var ValidChars = "-0123456789,.";
	var IsNumber=true;
	var Char;
	//Un texto vacio no sera numerico.
	if (sText==null) {
		IsNumber = false;
	} else {
		//Comprobacion de si los distintos 
		//caracteres son numericos o no.
		for (i = 0; i < sText.length && IsNumber == true; i++) 
		   { 
		   Char = sText.charAt(i); 
		   if (ValidChars.indexOf(Char) == -1) 
		      {
		      IsNumber = false;
		      }
		   }
	}
	return IsNumber;
}

function IsNumeric2(sText){

	/**************************************************
	
	Funcion equivalente a la funcion 'IsNumeric' de
	Visual Basic.
	
	**************************************************/

	var ValidChars = "0123456789";
	var IsNumber=true;
	var Char;
	//Un texto vacio no sera numerico.
	if (sText==null) {
		IsNumber = false;
	} else {
		//Comprobacion de si los distintos 
		//caracteres son numericos o no.
		for (i = 0; i < sText.length && IsNumber == true; i++) 
		   { 
		   Char = sText.charAt(i); 
		   if (ValidChars.indexOf(Char) == -1) 
		      {
		      IsNumber = false;
		      }
		   }
	}
	return IsNumber;
}
//---------------------------------------------------------------------//
function Right(str, n){

	/**************************************************
	
	Funcion de proceso equivalente a la funcion 'Right'
	de Visual Basic.
	
	**************************************************/
	
	if (n <= 0)
		return "";
	else if (n > String(str).length)
		return str;
	else {
		var iLen = String(str).length;
		//Esto no da resultados en FireFox:
		//return String(str).substring(iLen, iLen - n);
		return String(str).substring(iLen - n, iLen);
	}
}
//---------------------------------------------------------------------//
function Left(str, n){

	/**************************************************
	
	Funcion de proceso equivalente a la funcion 'Left'
	de Visual Basic.
	
	**************************************************/

	if (n <= 0)     
		return "";
	else if (n > String(str).length)   
		return str;                
	else 
		return String(str).substring(0,n);
}
//---------------------------------------------------------------------//
function Len(str){

	/**************************************************
	
	Funcion de proceso equivalente a la funcion 'Len'
	de Visual Basic.
	
	**************************************************/

	return str.length
}
//---------------------------------------------------------------------//
function Replace(Expresion, FindString, ReplaceString){

	/**************************************************
	
	Funcion equivalente a la funcion 'Replace' de
	Visual Basic.
	
	**************************************************/

	return Expresion.replace(FindString, ReplaceString)

}
//---------------------------------------------------------------------//
function Trim(TRIM_VALUE){

	/**************************************************
	
	Funcion equivalente a la funcion 'Trim' de
	Visual Basic.
	
	**************************************************/

	if(TRIM_VALUE.length < 1){
		return"";
	}
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);
	if(TRIM_VALUE==""){
		return "";
	} else {
		return TRIM_VALUE;
	}
}
//---------------------------------------------------------------------//
function RTrim(VALUE){

	/**************************************************
	
	Funcion equivalente a la funcion 'RTrim' de
	Visual Basic.
	
	**************************************************/

	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	if(v_length < 0){
		return"";
	}
	var iTemp = v_length -1;
	while(iTemp > -1){
		if(VALUE.charAt(iTemp) == w_space){
		} else {
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp-1;
	}
	return strTemp;
}
//---------------------------------------------------------------------//
function LTrim(VALUE){

	/**************************************************
	
	Funcion equivalente a la funcion 'LTrim' de
	Visual Basic.
	
	**************************************************/

	var w_space = String.fromCharCode(32);
	if(v_length < 1){
		return"";
	}
	var v_length = VALUE.length;
	var strTemp = "";
	var iTemp = 0;
	while(iTemp < v_length){
		if(VALUE.charAt(iTemp) == w_space){
		} else {
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	}
	return strTemp;
}
//---------------------------------------------------------------------//
function Mid(s, n, c){

	/**************************************************
	
	Funcion equivalente a la funcion 'Mid' de
	Visual Basic.
	
	**************************************************/
	
	var numargs=Mid.arguments.length;
	
	// Si solo se pasan los dos primeros argumentos
	if(numargs<3)
		c=s.length-n+1;
		
	if(c<1)
		c=s.length-n+1;
	if(n+c >s.length)
		c=s.length-n+1;
	if(n>s.length)
		return "";
		
	return s.substring(n-1,n+c-1);
}
//---------------------------------------------------------------------//
function InStr(n, s1, s2){
	// Devuelve la posicion de la primera ocurrencia de s2 en s1
	// Si se especifica n, se empezara a comprobar desde esa posicion
	// Sino se especifica, los dos parametros seran las cadenas
	var numargs=InStr.arguments.length;
	
	if(numargs<3)
		return n.indexOf(s1)+1;
	else
		return s1.indexOf(s2, n)+1;
}
//---------------------------------------------------------------------//
function GetMapFrame(){

	/**************************************************
	
	Recupera el Frame en el que se encuentra el mapa.
	
	**************************************************/
	
	if(window.document.getElementById('MapFrame')!=null){	        
		return window.document.getElementById('MapFrame').contentWindow;		
	}
	if(window.parent.document.getElementById('MapFrame')!=null){
		return window.parent.document.getElementById('MapFrame').contentWindow;		
	}
	
	if(window.parent.parent.document.getElementById('MapFrame')!=null){
		return window.parent.parent.document.getElementById('MapFrame').contentWindow;		
	}
	
	return null;
}


function GetBusquedasFrame(){

	/**************************************************
	
	Recupera el Frame en el que se encuentran las busquedas.
	
	**************************************************/
	
	if(window.document.getElementById('if_busquedas')!=null){	      
		return window.document.getElementById('if_busquedas').contentWindow;	
		
	}
	if(window.parent.document.getElementById('if_busquedas')!=null){
		return window.parent.document.getElementById('if_busquedas').contentWindow;		
	}
	
	if(window.parent.parent.document.getElementById('if_busquedas')!=null){
		return window.parent.parent.document.getElementById('if_busquedas').contentWindow;		
	}
	
	return null;
}

function GetAnalisisFrame(){

	/**************************************************
	
	Recupera el Frame en el que se encuentran los analisis.
	
	**************************************************/
	
	if(window.document.getElementById('if_analisis')!=null){	      
		return window.document.getElementById('if_analisis').contentWindow;	
		
	}
	if(window.parent.document.getElementById('if_analisis')!=null){
		return window.parent.document.getElementById('if_analisis').contentWindow;		
	}
	
	if(window.parent.parent.document.getElementById('if_analisis')!=null){
		return window.parent.parent.document.getElementById('if_analisis').contentWindow;		
	}
	
	return null;
}

function GetLeyendaFrame(){

	/**************************************************
	
	Recupera el Frame en el que se encuentran las leyendas.
	
	**************************************************/
	
	if(window.document.getElementById('if_leyenda')!=null){	      
		return window.document.getElementById('if_leyenda').contentWindow;	
		
	}
	if(window.parent.document.getElementById('if_leyenda')!=null){
		return window.parent.document.getElementById('if_leyenda').contentWindow;		
	}
	
	if(window.parent.parent.document.getElementById('if_leyenda')!=null){
		return window.parent.parent.document.getElementById('if_leyenda').contentWindow;		
	}
	
	return null;
}

//---------------------------------------------------------------------//
function GetInfExtendidaFrame(){

	/**************************************************
	
	Recupera el Frame en el que se encuentra el frame de resultados.
	
	**************************************************/	
	if(window.document.getElementById('infExtendidaFrame')!=null){
		return window.document.getElementById('infExtendidaFrame').contentWindow;		
	}
	if(window.parent.document.getElementById('infExtendidaFrame')!=null){
		return window.parent.document.getElementById('infExtendidaFrame').contentWindow;		
	}
	
	if(window.parent.parent.document.getElementById('infExtendidaFrame')!=null){
		return window.parent.parent.document.getElementById('infExtendidaFrame').contentWindow;		
	}
	return null;
}

//---------------------------------------------------------------------//
function GetResultadosFrame(){

	/**************************************************
	
	Recupera el Frame en el que se encuentra la pagina
	de resultados.
	
	**************************************************/
	if(window.document.getElementById('if_resultados')!=null)
		return window.document.getElementById('if_resultados').contentWindow;
	if(window.parent.document.getElementById('if_resultados')!=null)
		return window.parent.document.getElementById('if_resultados').contentWindow;
			
	return null;
}

//---------------------------------------------------------------------//
function getMensajeFrame(){

	/**************************************************
	
	Recupera el Frame en el que se encuentra la barra
	de estado.
	
	**************************************************/
	if(window.document.getElementById('MensajeFrame')!=null){
		return window.document.getElementById('MensajeFrame').contentWindow;
	}
	if(window.parent.document.getElementById('MensajeFrame')!=null){
		return window.parent.document.getElementById('MensajeFrame').contentWindow;
	}
		
	//Gorka - Para la ventana de mostrarRouting
	if (window.parent.parent.document != null){
	    return window.parent.parent.document.getElementById('MensajeFrame').contentWindow;
	}
	
	
	//Gorka - Para la ventana de imprimirPdf
	var ventanaPadre = window.opener;
	var ventanaAbuelo = window.opener.parent;	
		
	if(ventanaAbuelo.document.getElementById('MensajeFrame')!=null){	    
		return ventanaAbuelo.document.getElementById('MensajeFrame').contentWindow;
	}
	
	return null;
}
//---------------------------------------------------------------------//
function PonEstado(strEstado){

	/**************************************************
	
	Introduce el texto indicado en la barra de estado.
	
	**************************************************/

	var mensajeFrame = getMensajeFrame(); 	
	if(mensajeFrame!=null)
		mensajeFrame.document.getElementById('mensajesestado').innerHTML = strEstado;
	
}
//---------------------------------------------------------------------//
    var bCTRL
    var bALT

    function document_onkeydown(e) {
	    if(window.event) // IE 
	    { 
		    tecla = window.event.keyCode 
	    } 
	    else if(e.which) // Netscape/Firefox/Opera 
	    { 
		    tecla = e.which 
	    } 
		if(tecla==17) bCTRL=true;
		if(tecla==18) bALT=true;
		if (tecla==80 && bCTRL && bALT){
			var MapFrame=GetMapFrame()
			MapFrame.parent.CierraPopUps3('remotelogin.aspx?' + MapFrame.document.getElementById('vista').value ,'PopUp','toolbar=no,location=no,status=yes,menubar=no,scrollbars=no,resizable=no,top=' + ((self.screen.height-100)/2) + ',left=' + ((self.screen.width-440)/2) + ',width=440,height=100');
		}
    }
    function document_onkeyup(e) {
        var tecla
	    if(window.event) // IE 
	    { 
		    tecla = window.event.keyCode 
	    } 
	    else if(e.which) // Netscape/Firefox/Opera 
	    { 
		    tecla = e.which 
	    } 
		if(tecla==17) bCTRL=false;
		if(tecla==18) bALT=false;
    }
    
    document.onkeydown = document_onkeydown;
    document.onkeyup = document_onkeyup;
//---------------------------------------------------------------------//

function onlyNumbers(e,source,alowDecimals) 
{ 
	var keynum;
	var keychar;
	var numcheck;

	if(window.event) // IE 
	{ 
		keynum = e.keyCode 
	} 
	else if(e.which) // Netscape/Firefox/Opera 
	{ 
		keynum = e.which 
	} 
	keychar = String.fromCharCode(keynum) 
	switch (keychar)
		{
		case '-':
			{
			//Anade el simbolo negativo si no hay uno ya.
			if(!InStr(source.value,"-")){
				source.value=keychar+source.value;
			}
			break
		}
		case ',':
			{
			if(alowDecimals!=false){
			    //Anade un separador decimal (si habia uno ya lo elimina).
			    if(InStr(source.value,",")||InStr(source.value,".")){
				    return false;
			    } else {
				    if(window.event) // IE 
	                { 
		                e.keyCode = 46;
	                } 
	                else if(e.which) // Netscape/Firefox/Opera 
	                { 
		                e.which = 46;
	                } 
				    return true;
			    }
			} else {
			    return false;
			}
			break
		}
		case '.':
			{
			if(alowDecimals!=false){
			    //Anade un separador decimal (si habia uno ya lo elimina).
			    if(InStr(source.value,",")||InStr(source.value,".")){
				    return false;
			    } else {
				    return true;
			    }
			} else {
			    return false;
			}
			break
		}
		default: 
			{
			//Si es un valor numerico lo anade.
			if(IsNumeric2(keychar)) return true; //source.value=source.value+keychar;
			break
		}
	}
	if(keynum==8||keynum==13||keynum==undefined)
		//Los caracteres "indefinidos" se permitiran de forma general.
		//El caracter  8 se permite por ser un borrado de un caracter.
		//El caracter 13 se permite por ser un retorno de carro.
		return true;
	else
		return false; 
} 
//---------------------------------------------------------------------//

/**************************************************************************

Funciones de validacion y conversion de fechas

**************************************************************************/


function CheckDate(dateStr){ 
	//El formato "estandar" de fecha sera el espanol.
	var strFormat = 'dd/mm/yyyy'; 
	if(!isDate(dateStr)){		
		return false; 
	} 
 	return FormatDate(dateStr,strFormat); 	
} 

function isDate(DateToCheck){
	if(DateToCheck==""){return true;}
	var m_strDate = FormatDate(DateToCheck);
	if(m_strDate==""){
		return false;
	}
	var m_arrDate = m_strDate.split("/");
	var m_DAY = m_arrDate[0];
	var m_MONTH = m_arrDate[1];
	var m_YEAR = m_arrDate[2];
	if(m_YEAR.length > 4){return false;}
	m_strDate = m_MONTH + "/" + m_DAY + "/" + m_YEAR;
	var testDate=new Date(m_strDate);
	if(testDate.getMonth()+1==m_MONTH){
		return true;
	}else{
		return false;
	}
}

function FormatDate(DateToFormat,FormatAs){
	if(DateToFormat==""){return"";}
	if(!FormatAs){FormatAs="dd/mm/yyyy";}

	var strReturnDate;
	FormatAs = FormatAs.toLowerCase();
	DateToFormat = DateToFormat.toLowerCase();
	var arrDate
	var arrMonths = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	var strMONTH;
	var Separator;

	while(DateToFormat.indexOf("st")>-1){
		DateToFormat = DateToFormat.replace("st","");
	}

	while(DateToFormat.indexOf("nd")>-1){
		DateToFormat = DateToFormat.replace("nd","");
	}

	while(DateToFormat.indexOf("rd")>-1){
		DateToFormat = DateToFormat.replace("rd","");
	}

	while(DateToFormat.indexOf("th")>-1){
		DateToFormat = DateToFormat.replace("th","");
	}

	if(DateToFormat.indexOf(".")>-1){
		Separator = ".";
	}

	if(DateToFormat.indexOf("-")>-1){
		Separator = "-";
	}


	if(DateToFormat.indexOf("/")>-1){
		Separator = "/";
	}

	if(DateToFormat.indexOf(" ")>-1){
		Separator = " ";
	}

	arrDate = DateToFormat.split(Separator);
	DateToFormat = "";
	for(var iSD = 0;iSD < arrDate.length;iSD++){
		if(arrDate[iSD]!=""){
			DateToFormat += arrDate[iSD] + Separator;
		}
	}
	DateToFormat = DateToFormat.substring(0,DateToFormat.length-1);
	arrDate = DateToFormat.split(Separator);

	if(arrDate.length < 3){
		return "";
	}

	var DAY = arrDate[0];
	var MONTH = arrDate[1];
	var YEAR = arrDate[2];

	if(parseFloat(arrDate[1]) > 12){
		DAY = arrDate[1];
		MONTH = arrDate[0];
	}

	if(parseFloat(DAY) && DAY.toString().length==4){
		YEAR = arrDate[0];
		DAY = arrDate[2];
		MONTH = arrDate[1];
	}


	for(var iSD = 0;iSD < arrMonths.length;iSD++){
		var ShortMonth = arrMonths[iSD].substring(0,3).toLowerCase();
		var MonthPosition = DateToFormat.indexOf(ShortMonth);
		if(MonthPosition > -1){
		MONTH = iSD + 1;
			if(MonthPosition == 0){
			DAY = arrDate[1];
			YEAR = arrDate[2];
			}
		break;
		}
	}

	var strTemp = YEAR.toString();
	if(strTemp.length==2){

		if(parseFloat(YEAR)>40){
		YEAR = "19" + YEAR;
		}
		else{
		YEAR = "20" + YEAR;
		}

	}

	if(parseInt(MONTH)< 10 && MONTH.toString().length < 2){
		MONTH = "0" + MONTH;
	}
	if(parseInt(DAY)< 10 && DAY.toString().length < 2){
		DAY = "0" + DAY;
	}
	switch (FormatAs){
		case "dd/mm/yyyy":
			return DAY + "/" + MONTH + "/" + YEAR;
		case "mm/dd/yyyy":
			return MONTH + "/" + DAY + "/" + YEAR;
		case "dd/mmm/yyyy":
			return DAY + " " + arrMonths[MONTH -1].substring(0,3) + " " + YEAR;
		case "mmm/dd/yyyy":
			return arrMonths[MONTH -1].substring(0,3) + " " + DAY + " " + YEAR;
		case "dd/mmmm/yyyy":
			return DAY + " " + arrMonths[MONTH -1] + " " + YEAR;	
		case "mmmm/dd/yyyy":
			return arrMonths[MONTH -1] + " " + DAY + " " + YEAR;
	}

	return DAY + "/" + strMONTH + "/" + YEAR;

}

//---------------------------------------------------------------------//
function comprobarLongitud(campo, longitudCampo){

	/**************************************************
	
	Comprueba que el texto pasado no excede la longitud pasada
	
	**************************************************/
	if (campo.length > longitudCampo){
	    return false;
	}else{
	    return true;
	}
}
