// Eliminar los espacios en blanco del principio y del fin
function trim(inputStr){
	var strTemp;
	strTemp = inputStr;

	// Quitar los espacios delanteros	
	var i = 0;
	inputStrLength = inputStr.length;
	oneChar = inputStr.charAt(i)
	while (oneChar == ' ' && i < inputStrLength){
		oneChar = inputStr.charAt(++i);
	}
	if (oneChar != ' '){
		strTemp = inputStr.substring(i, inputStrLength)
	}

	// Quitar los espacios de atrás
	i = strTemp.length - 1;
	oneChar = strTemp.charAt(i)
	while (oneChar == ' ' && i > 0){
		oneChar = strTemp.charAt(--i);
	}
	if (oneChar != ' '){
		strTemp = strTemp.substring(0, i + 1);
	}
	return strTemp;
}

function trimChar(inputStr, car){
	// elimina un caracter(car) de la cadena dada (inputStr)
	var strTemp;
	strTemp = inputStr;

	// Quitar los caracteres delanteros	
	i = 0;
	inputStrLength = inputStr.length;
	oneChar = inputStr.charAt(i)
	while (oneChar == car && i < inputStrLength){
		oneChar = inputStr.charAt(++i);
	}
	if (oneChar != car){
		strTemp = inputStr.substring(i, inputStrLength)
	}

	// Quitar los caracteres de atrás
	i = strTemp.length - 1;
	oneChar = strTemp.charAt(i)
	while (oneChar == car && i > 0){
		oneChar = strTemp.charAt(--i);
	}
	if (oneChar != car){
		strTemp = strTemp.substring(0, i + 1);
	}

	return strTemp;
}

function muestra_grupo( formulario, grupo, target ){
	formulario.grupo.value = grupo;
	formulario.action = target;
	formulario.submit();
}

function chequea_fecha( dia, mes, anyo ){
	if ( dia == -1 || mes == -1 || anyo == "" ) return false;
	if ( dia > 31 || mes > 12 ) return false;
	if ( ( mes == 4 || mes == 6 || mes == 9 || mes == 11 ) && ( dia == 31 ) ) return false;
	if ( mes == 2 && dia > 29 ) return false;
	if ( mes == 2 && dia == 29 && !es_bisiesto( anyo ) ) return false;
	return true;
}

//Es un número entero
function esEntero(inputStr){
	for (i = 0; i < inputStr.length; i++){
		oneChar = inputStr.charAt(i);
		if ( oneChar < "0" || oneChar > "9" ){
			return false;
		}
	}
	return true;
}

/**
 * DHTML validacion de fechas. 
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1800;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("El fomato de la fecha debe ser: dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Por favor, especifique un mes válido")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Por favor, especifique un día válido")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Por favor, especifique un año válido entre " + minYear+" y "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Por favor, especifique una fecha válida")
		return false
	}
	return true;
}

function replaceComilla(inputStr){
	// reemplaza el caracter comilla  por backslash+comilla en la cadena dada (inputStr)
	var newStr = "";
	var strTemp;
	strTemp = inputStr;

	//Quito los caracteres de en medio
	for (i=0; i < strTemp.length; i++)  {			
		ch = strTemp.substring(i, i+1);
		//alert("car:"+ch);
		if (ch == "'") {
			newStr += "\\'";
		} else newStr += ch; 
		
	}
	//replace("'","\\\'");
	return newStr;
}

function delComillas(inputStr){
	// reemplaza el caracter comilla  por backslash+comilla en la cadena dada (inputStr)
	var newStr = "";
	var strTemp;
	strTemp = inputStr;

	//Quito los caracteres de en medio
	for (i=0; i < strTemp.length; i++)  {			
		ch = strTemp.substring(i, i+1);
		//alert("car:"+ch);
		if (ch != "'" && ch != "\"") {
			newStr += ch; 
		}	
	}
	//replace("'","\\\'");
	return newStr;
}

function es_bisiesto( anyo ){
	if ( ( ( anyo % 4 == 0 ) && ( anyo % 100 != 0 ) ) || ( anyo % 400 == 0 ) ) return true;
	else return false;
}

function get_cookie(Name) {
//Get cookie routine by Shelley Powers 
  var search = Name + "="
  var returnvalue = "";
  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    // if cookie exists
    if (offset != -1) { 
      offset += search.length
      // set index of beginning of value
      end = document.cookie.indexOf(";", offset);
      // set index of end of cookie value
      if (end == -1) end = document.cookie.length;
      returnvalue=unescape(document.cookie.substring(offset, end))
      }
   }
  return returnvalue;
}

function existe_frame( nombre ) {
    for (var i=0;i<parent.frames.length;i++) {
         if (parent.frames[i].name == nombre){
            return true;
		}
    }
    return false;
}

