

 
function settime () {
  var curtime = new Date();
  var curhour = curtime.getHours();
  var curmin = curtime.getMinutes();
  var cursec = curtime.getSeconds();
  var time = "";
  time = (curhour) + ":" +
         (curmin < 10 ? "0" : "") + curmin + ":" +
         (cursec < 10 ? "0" : "") + cursec;

  document.getElementById('hora').innerHTML = time;
};

function popup (pagina,w,h) {
	var newWindow;
	newWindow=window.open(pagina,'_blank','width='+w+',height='+h+', titlebar=0, toolbar=0, menubar=0, location=0,directories=0,status=0,scrollbars=1,resizable=1'); 
	newWindow.focus();
};


String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

function validar(campo, valor, tipo, cmin, nmin, nmax, objeto) {
	//campo; texto del campo, valor: valor del campo, tipo: '','correo','numero', cmin: longitud minima, nmin,nmax rango numerico
	valor=valor.trim();	
	if(nmin==undefined) nmin=0;
	if(nmax==undefined) nmax=-1;

	//alert(valor+'  '+valor.length+'  '+cmin);
	if(valor.length<cmin) { alert('Introduce un dato válido para \''+campo+'\''); return false; }
	if(tipo=='correo') {
		regex=/^\s*[^,]{1,}\@[^,]{1,}\.\w{2,3}\s*\,?\s*$/;
		if(!regex.test(valor)) { alert('Introduce un email válido para \''+campo+'\''); return false; }
	}
	if(tipo=='numero') {		
		regex=/^\d{1,}$/;
		if((valor<nmin || valor>nmax) && nmax>=0) { alert('Introduce un dato válido para \''+campo+'\''); return false;  }
		if(!regex.test(valor)) { alert('Introduce un dato válido para \''+campo+'\''); return false; }
		if(valor<nmin && nmax==-1) { alert('Introduce un dato válido para \''+campo+'\''); return false; }
		
	}
	if(tipo=='fecha') {
		regex=/^\d{2}\/\d{2}\/\d{4}$/;
		if(!regex.test(valor)) { alert('Introduce una fecha válida para \''+campo+'\''); return false; }
		if(!isDate(valor)) { alert('Introduce una fecha válida para \''+campo+'\''); return false; }
		eval(objeto+'=\''+valor+'\'');		
	}
	if(tipo=='moneda') {
		if (valor == '' || valor.toString().match(/^[-]?\d*\,?\d{0,2}$/)) {
			if(valor=='') {}
			else if(valor.indexOf(',')==-1) { eval(objeto+'=\''+valor+',00\''); valor=eval(objeto); }
			else if(valor.indexOf(',')==valor.length-1 && valor.length>0) { eval(objeto+'=\''+valor+'00\''); valor=eval(objeto); }
			else if(valor.indexOf(',')==valor.length-2 && valor.length>0) { eval(objeto+'=\''+valor+'0\''); valor=eval(objeto); }
		} else {
			alert('Introduzca un dato válido para \''+campo+'\'. Use el separador \',\' (coma) para los céntimos y sólo 2 posiciones decimales'); 
			return false; 
		}
	}
	if(tipo=='porciento') {
		if (valor == '' || valor.toString().match(/^[-]?\d*\,?\d{0,4}$/)) {
			if(valor=='') {}
			else if(valor.indexOf(',')==-1) { eval(objeto+'=\''+valor+',00\''); valor=eval(objeto); }
			else if(valor.indexOf(',')==valor.length-1 && valor.length>0) { eval(objeto+'=\''+valor+'00\''); valor=eval(objeto); }
			else if(valor.indexOf(',')==valor.length-2 && valor.length>0) { eval(objeto+'=\''+valor+'0\''); valor=eval(objeto); }
		} else {
			alert('Introduzca un dato válido (0 - 99,99) para \''+campo+'\'. Use el separador \',\' (coma) para los decimales (4 posiciones decimales máximo)'); 
			return false; 
		}
	}
	
	return true;
}

function validar_moneda (cantidad, campo, texto)	{
	return validar(texto,cantidad,'moneda',0,0,-1,campo);
}

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 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
}


function number_format (number, decimals, dec_point, thousands_sep) { //como el de PHP
	number=parseFloat(number.replace(',','.'));
	var n = number, prec = decimals;
 
    var toFixedFix = function (n,prec) {
        var k = Math.pow(10,prec);
        return (Math.round(n*k)/k).toString();
    };
 
    n = !isFinite(+n) ? 0 : +n;
    prec = !isFinite(+prec) ? 0 : Math.abs(prec);
    var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
    var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;
 
    var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;
 
    var abs = toFixedFix(Math.abs(n), prec);
    var _, i;
 
    if (abs >= 1000) {
        _ = abs.split(/\D/);
        i = _[0].length % 3 || 3;
 
        _[0] = s.slice(0,i + (n < 0)) +
              _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
        s = _.join(dec);
    } else {
        s = s.replace('.', dec);
    }
 
    var decPos = s.indexOf(dec);
    if (prec >= 1 && decPos !== -1 && (s.length-decPos-1) < prec) {
        s += new Array(prec-(s.length-decPos-1)).join(0)+'0';
    }
    else if (prec >= 1 && decPos === -1) {
        s += dec+new Array(prec).join(0)+'0';
    }
    return s;
}