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
}

function getDateValue(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
	}
	
	var d = new Date(year, month - 1, day);
	return d.getTime();
}




// miguel

var valor_anterior = "";
var campo_anterior_valido = true;
var campo_anterior = null;

function validar_fecha(campo) {	
	if (campo.value.trim().length > 0) {
		campo_anterior = campo;
		var pos1=campo.value.indexOf("/");
		if (pos1==-1) {
			if (campo.value.length==6) {
				campo.value=campo.value.substring(0,2)+"/"+campo.value.substring(2,4)+"/20"+campo.value.substring(4);
		  	} else if (campo.value.length==8) {
	campo.value=campo.value.substring(0,2)+"/"+campo.value.substring(2,4)+"/"+campo.value.substring(4);
		  	}
		}
		if (!esDate(campo.value)) {
			campo.value = "";
			window.focus();
			campo.focus();
			return false;
		}
	}
	return true;
}


// 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 validar_doble(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.replace(/\,/, "."));
			campo.value=s;	
		}
	}
}

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 omplir_select(imput, sql) {

	var f = function(data) {
		imput.options.length = 0;
	
		for (var i = 0; i < data.length; i++) {		
			imput.options[imput.options.length] = new Option(data[i].valors[1], data[i].valors[0]);
		}	
	
	} 
	
	imput.options.length = 0;
	imput.options[imput.options.length] = new Option('Cargando...', '');

	CommonsHandler.getSql(f, sql);
	
}

function marcar_todos(quinform, nom) {
	
		if (quinform[nom].length) {
			var primermarcat = false;
			for (var i = 0; i < quinform[nom].length; i++) {
				if (i == 0) primermarcat = quinform[nom][i].checked;
				quinform[nom][i].checked = !primermarcat;
			}
		} else {
			quinform[nom].checked = !quinform[nom].checked;
		}
	
}

function validar_email(campo) {	
	if (campo.value.trim().length > 0) {
		campo_anterior = campo;
		if (!isEmail(campo.value)) {
			alert('Debe indicar una dirección de correo válida...');
			campo.value = "";
			window.focus();
			campo.focus();
		}
	}
}
