
/*

		Funciones de propósito general en Javascript
		============================================

*/


//Declaración de variables de ámbito global
var dtCh= "/";
var minYear=1900;
var maxYear=2100;
var whitespace = " \t\n\r";


//Devuelve la posición X (left) de un objeto, incluso si  está dentro de otro
function findPosX(obj) 
{ 
var curleft = 0; 
if (obj.offsetParent) 
{ 
while (obj.offsetParent) 
{ 
curleft += obj.offsetLeft 
obj = obj.offsetParent; 
} 
} 
else if (obj.x) 
curleft += obj.x; 
return curleft; 
} 

//Devuelve la posición Y (top) de un objeto, incluso si  está dentro de otro
function findPosY(obj) 
{ 
var curtop = 0; 
if (obj.offsetParent) 
{ 
while (obj.offsetParent) 
{ 
curtop += obj.offsetTop 
obj = obj.offsetParent; 
} 
} 
else if (obj.y) 
curtop += obj.y; 
return curtop; 
} 

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
}

//Devuelve true si la cadena corresponde a una fecha.
function isDate(dtStr){
	if(dtStr.length!=10) {
		return false;
	}
	
	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 || dtStr.length!=10){
		alert("El formato de fecha ha de ser: dd/mm/aaaa")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Por favor, indique 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, indique un día válido")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Por favor, indique un año entre "+minYear+" y "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Por favor, indique una fecha correcta")
		return false
	}
	return true
}

//Devuelve true si todos los caracteres son números
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;
}

//Devuelve true si corresponde a una hora hh:mm
function esHora(s)
{
	var i;
	var c;
	
	if (s.length!=5)
		{
		return false;
		}
	else
		{  
		c = s.charAt(2);
		if (c!=":")
			{
		 	return false;
			}
		else
			{
			for (i = 0; i < s.length; i++)
				{   
				// Check that current character is number.
				c = s.charAt(i);
				if (i!=2)
					{
					if (((c < "0") || (c > "9"))) return false;
					}
				}
			}
		}
    // All characters are numbers.
    return true;
}

//Devuelve true si la cadena de caracteres corresponde a un email
function isEmail (s)
{
    if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
    if (isWhitespace(s)) return false;
    var i = 1;
    var sLength = s.length;
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

//Devuelve true si hemos indicado la cadena vacía
function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}


function isWhitespace (s)
{   var i;
    if (isEmpty(s)) return true;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        // si el caracter en que estoy no aparece en whitespace,
        // entonces retornar falso
        if (whitespace.indexOf(c) == -1) return false;
    }
    return true;
}

function trim(str){
	if(str!=null) return str.replace(/^\s*|\s*$/g,"");
	else return "";
}

//Limpiar el principio de cadena de espacios y caracteres de control.
function ltrim(cadena){
	
		while ((cadena.length>0) && (cadena.charAt(0)==" " || cadena.charAt(0)=="\r" || cadena.charAt(0)=="\n" || cadena.charAt(0)=="\t" || cadena.charAt(0)=="\b" || cadena.charAt(0)=="\f"))
		{
			cadena=cadena.substring(1);
		}
	return cadena
}

//Abre la página indicada en la misma ventana
function abrirUrl(url)
{
window.top.location.href = url;
}

//Abre la pagina indicada en otra venta pop up
//Ejemplo de opciones:
//opciones= "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, width=508, height=365, top=85, left=140";

function abrirVentana (pagina, opciones) {
window.open(pagina,"",opciones);
}
