var noverif;
noverif=false;
var miKey,miEvent;

function JDSPostImporte(objeto) {
		objeto.value=JDSReplace(objeto.value,",",".");
		objeto.value=JDSStrDouble(JDSDouble(objeto.value));
}
function JDSDouble(cad) {
	var res=parseFloat(cad);
	if(isNaN(res)) {
			return 0.0;
	} else {
		return res;
	}
}
function JDSInt(cad) {
	var res=parseInt(cad);
	if(isNaN(res)) {
			return 0;
	} else {
		return res;
	}
}
function JDSStrDouble(valor) {
		return JDSReplace((""+(JDSRound(valor,2)).toFixed(2)),".",",");
}
function JDSRound(cad,idec) {
		var res=cad;
		for (var i=0;i<idec;i++) res=res*10;
		res=Math.round(res);
		for (var i=0;i<idec;i++) res=res/10;
		return res;
}

// ***************** MASCARAS INPUT ************************
function JDSKbdImportes(evento) {
var key;
	if(!evento) evento = window.event;
	if (evento.keyCode) {
		key = evento.keyCode;
	} else if (evento.which) {
		key = evento.which;
	}
	if (noverif) return true;
	if (key < 32 && key!=13) {
	} else if (key >= 48 && key <= 57) {
	} else if (key == 46) {
	} else if (key == 44) {
			if (evento.keyCode) evento.keyCode = 46;
	} else if (key == 45) {
	} else {
		if (evento.keyCode) {
			evento.keyCode = 0;
			evento.cancelBubble=true;
			evento.returnValue=false;
		} else {
			evento.which= 0;
		}
	}
}
function JDSKbdInt(evento) {
var key;
	if(!evento) evento = window.event;
	if (evento.keyCode) {
		key = evento.keyCode;
	} else if (evento.which) {
		key = evento.which;
	}
	if (key < 32) {
	} else if (key >= 48 && key <= 57) {
	} else {
		if (evento.keyCode) {
			evento.keyCode = 0;
			evento.cancelBubble=true;
			evento.returnValue=false;
		} else {
			evento.which= 0;
		}
	}
}
function JDSKbdFecha(evento) {
var key;
	if(!evento) evento = window.event;
	if (evento.keyCode) {
		key = evento.keyCode;
	} else if (evento.which) {
		key = evento.which;
	}
	if (key < 32) {
	} else if (key >= 48 && key <= 57) {
	} else if (key == 45) {
	} else if (key == 47) {
		if (evento.keyCode) evento.keyCode=45;
	} else {
		if (evento.keyCode) {
			evento.keyCode = 0;
			evento.cancelBubble=true;
			evento.returnValue=false;
		} else {
			evento.which= 0;
		}
	}
}
function JDSKbdDoble(evento) {
var key;
	if(!evento) evento = window.event;
	if (evento.keyCode) {
		key = evento.keyCode;
	} else if (evento.which) {
		key = evento.which;
	}
	if (key==13) {
		key=9;
  } else if (key < 32) {
	} else if (key >= 48 && key <= 57) {
	} else if (key == 46) {
	} else if (key == 44) {
    miEvent.keyCode = 46;
	} else if (key == 45) {
	} else {
		if (evento.keyCode) {
			evento.keyCode = 0;
			evento.cancelBubble=true;
			evento.returnValue=false;
		} else {
			evento.which= 0;
		}
	}
}

// ***************** VALIDACIONES MASCARAS ************************

// ENTERO
function JDSValidaEntero(objeto,oblig) {
	if (noverif) return true;
	var res=objeto.value;
	if (res=="" ) {
			if (oblig==1) {
				noverif=true;
				objeto.value="";
				objeto.focus();
				alert("El dato es obligatorio");
				noverif=false;
				return false;
			} else {
				return true;
			}
	} else {
		if (!JDSValInt(res)) {
			noverif=true;
			objeto.value="";
			objeto.focus();
			alert("Número no válido");
			noverif=false;
			return false;
		}
	}
}

// FECHA
function JDSValidaFecha(objeto,oblig) {
	if (noverif) return true;
	var res=objeto.value;
	if (res=="") {
			if (oblig==1) {
				noverif=true;
				objeto.value="";
				objeto.focus();
				alert("El dato es obligatorio");
				noverif=false;
				return false;
			} else {
				return true;
			}
	} else {
		if (!isFechaValida(res)) {
			noverif=true;
			objeto.value="";
			objeto.focus();
			alert("Fecha no válida");
			noverif=false;
			return false;
		}
	}
}

// DOBLE
function JDSValidaDoble(objeto,oblig) {
	if (noverif) return true;
	var res=objeto.value;
	if (res=="" ) {
			if (oblig==1) {
				noverif=true;
				objeto.value="";
				objeto.focus();
				alert("El dato es obligatorio");
				noverif=false;
				return false;
			} else {
				return true;
			}
	} else {
		if (!JDSValDouble(res)) {
			noverif=true;
			objeto.value="";
			objeto.focus();
			alert("Número no válido");
			noverif=false;
			return false;
		}
	}
}

// CAMPO OBLIGATORIO
function JDSValidaObligatorio(nombreCampo,mensaje) {
	var res=eval(nombreCampo+".value");
	if (res=="" ) {
		eval(nombreCampo+".focus()");
		alert("El campo ["+mensaje+"] es obligatorio");
		return false;
	}
}


// FUNCIONES INTERNAS AUXILIARES
function JDSValInt(cad) {
		return /^\d+$/.test(cad)
}
function JDSValDouble(cad) { 
	return /^\d+(\.\d+)?$/.test(cad) 
}
function JDSReplace(streng, soeg, erstat)
{ var st = streng;
  if (soeg.length == 0)
     return st;
  var idx = st.indexOf(soeg);
  while (idx >= 0)        
  {  st = st.substring(0,idx) + erstat + st.substr(idx+soeg.length);
     idx = st.indexOf(soeg);
  }
  return st;
}
function JDSStrzero(valor,longit) {
var cad=valor+"";
	while (cad.length<longit) {
		cad="0"+cad;
	}
	return cad;
} 
function JDSToken(xCadena, xtoken) {
	if (xCadena == null) {
		return "";
	}
	if (xCadena.length == 0) { 
		return "";
	}
	var i=xCadena.indexOf(xtoken);
	var xRes="";
	if (i == -1) {
		xRes=xCadena;
	} else if (i == 0) {
		xRes="";
	} else {
		xRes=xCadena.substring(0,i);
	}
	return xRes;
}
function JDSTokenRemove(xCadena, xtoken) {
	if (xCadena == null) { 
		return "";
	}
	if (xCadena.length == 0) { 
		return "";
	}
	var i=xCadena.indexOf(xtoken);
	if (i == -1) {
		return "";
	} else {
		xCadena=xCadena.substring(i+xtoken.length);
	}
	return xCadena;
}



function MayHavePoint(S) { return /^\d+(\.\d+)?$/.test(S) }

function GoodDecimal(S) { return /^\d+\.\d+$/.test(S) }
function MayHavePoint(S) { return /^\d+(\.\d+)?$/.test(S) }


// 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 isFechaValida(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 (dtStr==null) {
		return true;
	}
	if (dtStr=="") {
		return true;
	}
	
	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("El formato debe ser : dd-mm-yyyy")
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("El mes no es válido")
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("El día no es válido")
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("El año no es válido")
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("La fecha no es válida")
		return false;
	}
	return true;
}
	function CharToDate(dtStr) {
		var pos1=dtStr.indexOf("-");
		var pos2=dtStr.indexOf("-",pos1+1);
		var strDay=dtStr.substring(0,pos1);
		var strMonth=dtStr.substring(pos1+1,pos2);
		var strYear=dtStr.substring(pos2+1);
		if (strDay.substring(0,1)=="0") strDay=strDay.substring(1);
		if (strMonth.substring(0,1)=="0") strMonth=strMonth.substring(1);
		var f1=new Date(parseInt(strYear),parseInt(strMonth)-1,parseInt(strDay),0,0,1);
		//alert(f1);
		return f1;
	}

function trim(inputString) {
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
}
function replaceAll (streng, soeg, erstat)
{ var st = streng;
  if (soeg.length == 0)
     return st;
  var idx = st.indexOf(soeg);
  while (idx >= 0)        
  {  st = st.substring(0,idx) + erstat + st.substr(idx+soeg.length);
     idx = st.indexOf(soeg);
  }
  return st;
}

function miescape(cad) {
	cad=escape(cad);
	return replaceAll(cad,"+","%2B");
}

function SetFoco(objeto) {
	objeto.select();
}

function SetFoco(objeto) {
	objeto.select();
}

function AbreVentana(xw,xh,xt,xl,xdir,ventana,sc,rs) {
	var tools="toolbar=no,location=no,directories=no,status=no,menubar=no,resizable="+rs+",scrollbars="+sc+",width="+xw+",height="+xh+",top="+xt+",left="+xl+"')";
	var xwin=window.open("",ventana,tools);
	document.Datos.action=xdir;
	document.Datos.target=ventana;
	document.Datos.submit();
	document.Datos.target="";
	document.Datos.action="";
	xwin.focus();
}

