function numbersonly(myfield, e, dec)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27) )
   return true;

// numbers
else if ((("0123456789").indexOf(keychar) > -1))
   return true;

// decimal point jump
else if (dec && (keychar == "."))
   {
   myfield.form.elements[dec].focus();
   return false;
   }
else
   return false;
}

function LTrim(str)
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      var j=0, i = s.length;
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;
      s = s.substring(j, i);
   }
   return s;
}

function RTrim(str)
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);
   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      var i = s.length - 1;       // Get length of string
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;
      	s = s.substring(0, i+1);
   }

   return s;
}
function Trim(str)
{
   return RTrim(LTrim(str));
}


var nav4 = window.Event ? true : false;
function acceptNum(evt){	
var key = nav4 ? evt.which : evt.keyCode;	
return (key <= 13 || (key >= 48 && key <= 57));
}

function ValidaFecha (dia,mes,ano,oblig){
	Error=true;
	if (oblig==true){
		if (dia=="" || mes=="" || ano=="")
			Error=false;
	}
	if ((mes>12) || (dia>31))
			{Error=false;}
	if (((mes==4)||(mes==6)||(mes==9)||(mes==11)) && (dia==31)){
			Error=false;}
	if ((mes==2) && (dia>28)){
			Error=false;}
	resto=ano%4;
	
	if ((resto==0) && (mes==2) && (dia==29)){
			Error=true;}
	if (ano<1880)
		{Error=false;}
	return Error;
}

function ValidaHora (hora,minuto,oblig){
	Error=true;
	if (oblig==true){
		if (hora=="" || minuto=="")
			{Error=false;}
	}
	if (hora>25 || minuto >60)
		{Error=false;}
	return Error;
}

function validarmail(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		return false
	 }
	
	 if (str.indexOf(" ")!=-1){
		return false
	 }

	 return true					
}

	

function dateDiff(p_Interval, p_Date1, p_Date2, p_firstdayofweek, p_firstweekofyear){
	if(!isDate(p_Date1)){return "invalid date: '" + p_Date1 + "'";}
	if(!isDate(p_Date2)){return "invalid date: '" + p_Date2 + "'";}

	var dt1 = new Date(p_Date1);
	var dt2 = new Date(p_Date2);
   	//get ms between dates (UTC) and make into "difference" date
	var iDiffMS = dt2.valueOf() - dt1.valueOf();
	var dtDiff 	= new Date(iDiffMS);

	// calc various diffs
	var nYears 			= dtDiff.getUTCFullYear()-1970;
	var nMonths 		= dtDiff.getUTCMonth() + (nYears!=0 ? nYears*12 : 0);
	var nQuarters 		= parseInt(nMonths/3);
	var nWeeks 			= parseInt(iDiffMS/1000/60/60/24/7);
	var nDays			= parseInt(iDiffMS/1000/60/60/24);
	var nHours 			= parseInt(iDiffMS/1000/60/60);
	var nMinutes 		= parseInt(iDiffMS/1000/60);
	var nSeconds		= parseInt(iDiffMS/1000);
	var nMilliseconds 	= iDiffMS;

	// return requested difference
	var iDiff = 0;		
	switch(p_Interval.toLowerCase()){
		case "yyyy": return nYears;
		case "q": return nQuarters;
		case "m": return nMonths;
		case "y": 		// day of year
		case "d": return nDays;
		case "w": return nDays;
		case "ww":return nWeeks;		// week of year	// <-- inaccurate, WW should count calendar weeks (# of sundays) between
		case "h": return nHours;
		case "n": return nMinutes;
		case "s": return nSeconds;
		case "ms":return nMilliseconds;	// millisecond	// <-- extension for JS, NOT available in VBScript
		default: return "invalid interval: '" + p_Interval + "'";
		}
}

function isDate(p_Expression){
	return !isNaN(new Date(p_Expression));
}
