
// constantes

js_alta = 'alta';
js_baja = 'baja';
js_modificacion = 'modificacion';

// lugar

//js_lugar = 'CASA';
//js_lugar = 'DESARROLLO';
js_lugar = '';

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

// Valida Fechas

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("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		// alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		// alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		// alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		// alert("Please enter a valid date")
		return false
	}
	
return true
}

// Valida Horas

function esUnaHora(tiempo)
{
	// En lista_t tenemos un array con los tiempos. lista_t[0] = minutos, lista_t[1] = segundos. 
	lista_t = tiempo.split(":");
	
	// si distinto de 2 es que no tenemos el formato correcto.
	if (lista_t.length != 3)
	{
		return false;
	}
	
	// comprobamos las horas.
	if ( lista_t[0] == "" )
	{
		return false;
	}
	if ( isNaN(lista_t[0]) )
	{
		return false;
	}
	else if ( (parseInt(lista_t[0]) < 0) || (parseInt(lista_t[0]) >23) )
	{
		return false;
	}
	if(!isInteger(lista_t[0]))
	{
		return false;
	}
	
	// comprobamos los minutos.
	if ( lista_t[1] == "" )
	{
		return false;
	}
	if ( isNaN(lista_t[1]) )
	{
		return false;
	}
	if ( (parseInt(lista_t[1]) < 0) || (parseInt(lista_t[1]) >59) )
	{
		return false;
	}
	if(!isInteger(lista_t[1]))
	{
		return false;
	}
	
	
	// comprobamos los segundos.
	if ( lista_t[2] == "" )
	{
		return false;
	}
	if ( isNaN(lista_t[2]) )
	{
		return false;
	}
	if ( (parseInt(lista_t[2]) < 0) || (parseInt(lista_t[2]) >59) )
	{
		return false;
	}
	if(!isInteger(lista_t[2]))
	{
		return false;
	}
	
	return true;
}

function esFechaHora(fechaHora)
{
	lista_f = fechaHora.split(' ');
	
	// en lista_f[0] debemos tener la fecha y lista_f[1] la hora
	if (lista_f.length != 2)
	{
		return false;
	}
	return isDate(lista_f[0]) && esUnaHora(lista_f[1]);
}

function esSabado(fecha)
{	
	lista_fecha = fecha.split("/");
	
	dia = parseInt(lista_fecha[0]);
	mes = parseInt(lista_fecha[1] - 1);
	ano = parseInt(lista_fecha[2]);
	
	fechaAux = new Date(ano, mes, dia);
	
	diaSemana = fechaAux.getDay();
	
	if (diaSemana == 6)
		return true;
	else
		return false;
}

function isArray(a) 
{
	return isObject(a) && a.constructor == Array;
}

// Comprueba si es un numero y si es mayor que cero
function enteroMayor(campo, valor)
{
	if (campo == "")
	{
		return false;
	}
	if(!isInteger(campo))
	{
		return false;
	}
	if (parseInt(campo) <= valor)
	{
		return false;
	}
	
	return true;
}

function esFloatPositivo(campo)
{
	if (campo == "")
		return false;
	
	if ( isNaN(campo) )
		return false;
	
	if (parseFloat(campo) < 0)
		return false;
		
	return true;
}

function esFloatMayor(campo, valor)
{
	if (campo == "")
		return false;
	
	if ( isNaN(campo) )
		return false;
	
	if (parseFloat(campo) <= valor)
	{
		return false;
	}
		
	return true;
}

function funcXor(valor1, valor2)
{
	if (valor1)
		return !valor2;
	else
		return valor2;
}

function verFotoPopUp(foto)
{
	// MM_openBrWindow(foto,'ggwin','status=yes,menubar=yes,width=592,height=480');
	if (js_lugar == 'CASA')
		MM_openBrWindow('/transporte3/imageGG.php?img=' + foto,'ggwin','status=yes,menubar=yes,scrollbars=yes');
	else if(js_lugar == 'DESARROLLO')
		MM_openBrWindow('/v2/imageGG.php?img=' + foto,'ggwin','status=yes,menubar=yes,scrollbars=yes');
	else
		MM_openBrWindow('/imageGG.php?img=' + foto,'ggwin','status=yes,menubar=yes,scrollbars=yes');
	// window.open('/geographica/imageGG.php?img=' + foto,'ggwin','status=yes,menubar=yes,scrollbars=yes');
}

function validaTelefono(telef)
{
	var i;
	var c;
	
   for (i = 0; i < telef.length; i++)
   {   
        // Check that current character is number.
        c = telef.charAt(i);
        if ( ((c >= "0") && (c <= "9"))
        		|| (c == "-") || (c == "(") || (c == ")") || (c == "/") )
        	return true;
        else
        	return false;
    }
    // All characters are numbers.
    return true;
}

function numCamposVacios(valor1, valor2)
{
	res = 0;
	if (valor1 == "")
		res++;
	if (valor2 == "")
		res++;
	
	return res;
}

function documentWrite(cadena)
{
	document.write(cadena);
}

function trim(s) 
{ 
	return s.replace( /^\s*/, "" ).replace( /\s*$/, "" ); 
	// a = this.replace(/^s+/, ‘’); 
	//return a.replace(/s+$/, ‘’); 
} 