/* 
replaceSubstring("hellothere", "l", "x") = "hexxothere"
replaceSubstring("this is a string", "is", "x") = "thx x a string"
replaceSubstring("Here is a long string", "", "xxx") = "Here is a long string"
replaceSubstring("Here is a long string", " ", "") = "Hereisalongstring"
' The slash is a literal character, so "\\" is a single slash
replaceSubstring("\\mysubdir\\mydatabase.nsf", "\\", "/") = "/mysubdir/mydatabase.nsf"
' If the string is "literal \s\w characters", it must be represented literally like "literal \\s\\w characters"
replaceSubstring("literal \\s\\w characters", "\\", "\\\\") = "literal \\s\\w characters"
replaceSubstring("Getting rid of unwanted words", "unwanted", "unneeded") = "Getting rid of unneeded words"
*/

function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function



function fnUnformatNumber(obj,input,output){
	obj.value = replaceSubstring(obj.value,input,output)
}

//se hace referencia de la siguiente manera onKeyPress="return acceptNum(event);"
var nav4 = window.Event ? true : false;
function acceptNum(evt){ 
	// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57 
	var key = evt.keyCode; 
	return (key <= 13 || (key >= 48 && key <= 57));
}
function acceptText(evt){ 
	// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57 
	var key = evt.keyCode; 
	return (key <= 13 || (key >= 65 && key <= 90) || (key >= 97 && key <= 122));
}

function acceptNumText(evt) {
	// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57 
	var key = evt.keyCode;
	return (key <= 13 || (key >= 48 && key <= 57) || (key >= 65 && key <= 90) || (key >= 97 && key <= 122));
}

//onKeyPress="return validaIngresoRut(event);"
function validaIngresoRut(evt) {
	var key = evt.keyCode;
	return ((key >= 48 && key <= 57) || (key == 107 || key == 75) || (key == 45 || key == 46) || (key == 8));
}

function validarEmail(valor) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)){
   		//alert("La direcci?n de email " + valor    + " es correcta.") 
   		return (true);
  	} else {
   		alert("La direccion de email es incorrecta");
   		return (false);
  	}
}

//valida rut
function checkRutField(rut, input)
{
	var tmpstr = "";
	for ( i=0; i < rut.length ; i++ )
		if ( rut.charAt(i) != ' ' && rut.charAt(i) != '.' && rut.charAt(i) != '-' )
			tmpstr = tmpstr + rut.charAt(i);
	rut = tmpstr;
	largo = rut.length;
// [VARM+]
	tmpstr = "";
	for ( i=0; rut.charAt(i) == '0' ; i++ );
		for (; i < rut.length ; i++ )
			tmpstr = tmpstr + rut.charAt(i);
	rut = tmpstr;
	largo = rut.length;
	
	//alert(rut);
	
// [VARM-]
	if ( largo < 2 )
	{
		alert("Debe ingresar el rut completo");
		input.focus();
		input.select();
		return false;
	}
	for (i=0; i < largo ; i++ )
	{
		if ( rut.charAt(i) != "0" && rut.charAt(i) != "1" && rut.charAt(i) !="2" && rut.charAt(i) != "3" && rut.charAt(i) != "4" && rut.charAt(i) !="5" && rut.charAt(i) != "6" && rut.charAt(i) != "7" && rut.charAt(i) !="8" && rut.charAt(i) != "9" && rut.charAt(i) !="k" && rut.charAt(i) != "K" )
		{
			alert("RUT invalido");
			input.focus();
			input.select();
			return false;
		}
	}
	var invertido = "";
	for ( i=(largo-1),j=0; i>=0; i--,j++ )
		invertido = invertido + rut.charAt(i);
	var drut = "";
	drut = drut + invertido.charAt(0);
	drut = drut + '-';
	cnt = 0;
	for ( i=1,j=2; i<largo; i++,j++ )
	{
		if ( cnt == 3 )
		{
			//drut = drut + '.';
			drut = drut + '';
			j++;
			drut = drut + invertido.charAt(i);
			cnt = 1;
		}
		else
		{
			drut = drut + invertido.charAt(i);
			cnt++;
		}
	}
	invertido = "";
	for ( i=(drut.length-1),j=0; i>=0; i--,j++ )
		invertido = invertido + drut.charAt(i);
	input.value = invertido;
	if ( checkDV(rut, input, rut) )
		return true;
	return false;
}


function checkDV(crut, input, rut)
{
	largo = crut.length;
	if ( largo < 2 )
	{
		alert("Debe ingresar el rut");
		input.focus();
		input.select();
		return false;
	}
	if ( largo > 2 )
		rut = crut.substring(0, largo - 1);
	else
		rut = crut.charAt(0);
	dv = crut.charAt(largo-1);
	checkCDV(dv, input);
	if ( rut == null || dv == null )
		return 0;
	var dvr = '0';
	suma = 0;
	mul = 2;
	for (i= rut.length -1 ; i >= 0; i--)
	{
		suma = suma + rut.charAt(i) * mul;
		if (mul == 7)
			mul = 2;
		else
			mul++;
	}
	res = suma % 11;
	if (res==1)
		dvr = 'k';
	else if (res==0)
		dvr = '0';
	else
	{
		dvi = 11-res;
		dvr = dvi + "";
	}
	if ( dvr != dv.toLowerCase() )
	{
		alert("Rut incorrecto");
		input.focus();
		input.value = "";
		return false;
	}
	return true;
}


function checkCDV(dvr, input)
{
	dv = dvr + "";
	if ( dv != '0' && dv != '1' && dv != '2' && dv != '3' && dv != '4' && dv != '5' && dv != '6' && dv != '7' && dv != '8' && dv != '9' && dv != 'k'  && dv != 'K')
	{
		alert("Debe ingresar un digito verificador valido");
		input.focus();
		input.select();
		return false;
	}
	return true;
}
