String.prototype.trim = function() {

 // skip leading and trailing whitespace
 // and return everything in between
  var x=this;
  x=x.replace(/^\s*(.*)/, "$1");
  x=x.replace(/(.*?)\s*$/, "$1");
  return x;
}

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
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 esDate(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("Los formatos válidos para una fecha son : mmddyy, mmddyyyy o mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Por favor introduce un més válido")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Por favor introduce un día válido")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Por favor introduce 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 introduce una fecha válida")
		return false
	}
return true
}


// miguel

var valor_anterior = "";
var campo_anterior_valido = true;
var campo_anterior = null;

function validar_fecha(fecha) {
	if (fecha.trim().length > 0) {
		//campo_anterior = campo;
		var pos1=fecha.indexOf("/");
		if (pos1==-1) {
			if (fecha.length==6) {
				fecha=fecha.substring(0,2)+"/"+fecha.substring(2,4)+"/20"+fecha.substring(4);
		  	} else if (campo.value.length==8) {
	fecha=fecha.substring(0,2)+"/"+fecha.substring(2,4)+"/"+fecha.substring(4);
		  	}
		}
		if (!esDate(fecha)) {
			//campo.value = "";
			//window.focus();
			//campo.focus();
			return false;
		}
	}
	return fecha;
}


// isEmail (STRING s [, BOOLEAN emptyOK])
// 
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isEmail (s)
{   
	// there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

function esReal (s)
{   
	var r = parseFloat(s);
	if (isNaN(r)) return false;
	else return true;
}

function validar_email(campo) {	
	if (campo.value.trim().length > 0) {
		campo_anterior = campo;
		if (!isEmail(campo.value)) {
			campo.value = "";
			window.focus();
			campo.focus();
		}
	}
}

function validar_real(campo) {
	if (campo.value.trim().length > 0) {
		campo_anterior = campo;
		if (!esReal(campo.value)) {
			alert("Aquí debes introducir un valor numérico");
			campo.value = "";
			window.focus();
			campo.focus();
		} else {
			var s = "" + parseFloat(campo.value);
			campo.value=s.replace(/\./, ",");	
		}
	}
}

function esEntero (s)
{   
	var r = parseInt(s,10);
	if (isNaN(r)) return false;
	else return true;
}

function validar_entero(campo) {
	if (campo.value.trim().length > 0) {
		campo_anterior = campo;
		if (!esEntero(campo.value)) {
			alert("Aquí debes introducir un valor numérico sin decimales");
			campo.value = "";
			window.focus();
			campo.focus();
		} else {
			var s = "" + parseInt(campo.value,10);
			campo.value=s.replace(/\./, ",");	
		}
	}
}


function sumaMes(nDia, nMes, nAno, nSum){
	if(nDia.substring(0,1) == 0)
		nDia = nDia.substring(1,2);
	if(nMes.substring(0,1) == 0)
		nMes = nMes.substring(1,2);
	if(nAno.substring(0,1) == 0)
		nAno = nAno.substring(1,2);
    if (nSum >= 0)
		{ 
     	for (var i = 0; i < Math.abs(nSum); i++)
			{ 
      		if (nMes == 12)
				{ 
       			nMes = 1; 
       			nAno ++; 
      			} 
			else 
				nMes ++; 
     		} 
    	} 
	else 
		{ 
     	for (var i = 0; i < Math.abs(nSum); i++)
			{ 
      		if (nMes == 1)
				{ 
       			nMes = 12; 
       			nAno --; 
      			} 
			else 
				nMes --; 
     		} 
   		 }
    fecha = new Date(nAno,nMes - 1,nDia); 
	return fecha;
   } 

function sumaDia(nDia, nMes, nAno, nSum)
	{
	nDia = parseInt(nDia);
	nMes = parseInt(nMes);
	nAno = parseInt(nAno);
	nSum = parseInt(nSum);
	fecha = new Date(nAno,nMes - 1,nDia,10,00,00);
	nueva_fecha_milisegundos = fecha.getTime() + 86400000 * nSum;
	nueva_fecha = new Date(nueva_fecha_milisegundos);
	nDia2=nueva_fecha.getDate();
	nMes2=nueva_fecha.getMonth();
	nAno2=nueva_fecha.getFullYear();
	nueva_fecha = new Date(nAno2, nMes2, nDia2);
	return nueva_fecha;
	}

function dias_entre_fechas (nDia1, nMes1, nAno1, nDia2, nMes2, nAno2)
	{
	nDia = parseInt(nDia1);
	nMes = parseInt(nMes1);
	nAno = parseInt(nAno1);
	
	nDia = parseInt(nDia2);
	nMes = parseInt(nMes2);
	nAno = parseInt(nAno2);
		
	fecha1 = new Date(nAno1,nMes1-1,nDia1);
	fecha2 = new Date(nAno2,nMes2-1,nDia2);
	
	fecha1milisegundos = fecha1.getTime();
	fecha2milisegundos = fecha2.getTime();		

	diferenciamilisegundos = fecha2milisegundos - fecha1milisegundos;	
	
	dias = Math.round(diferenciamilisegundos / 86400000);	
	return dias;
	}

function validar_fechas_desde_hasta(fecha1, fecha2, objeto)
	{
	if(fecha1 > fecha2)
		{
		objeto.options[objeto.options.selectedIndex].selected = true;
		objeto.focus();
		return false;
		}
	else
		return true;
	}
	
function diasMesCal(mes,any)
	{
		var dias = 31;
		if ( mes == 3 || mes == 5 || mes == 8 || mes == 10 )      dias = 30;
		if ( mes == 1 && ( any / 4 ) != Math.floor ( any / 4 ) )  dias = 28;
		if ( mes == 1 && ( any / 4 ) == Math.floor ( any / 4 ) )  dias = 29;
		return dias;
	}
	
function fecha_ddmmaaaa(dia,mes,any)
{
	if (dia < 10)
	{
		dia = "0" + dia;
	}			
	if (mes < 10)
	{
		mes = "0" + mes; 
	}
	var fecha = dia + "/" + mes + "/" + any; 
	return fecha;
}

function totalmente_en_blanco(cadena)
{
	var nuevacadena = cadena.replace(/ /g,'');
	if (nuevacadena.length ==  0)
		return true;
	else
		return false;
}

function diasMesCal2(mes,any)
	{
		var dias = 31;
		if ( mes == 4 || mes == 6 || mes == 9 || mes == 11 )      dias = 30;
		if ( mes == 2 ) dias = 	daysInFebruary(any);	
		return dias;
	}