function SubmitirPage(prmt, forma)
{   
    if (valida_campos(forma)) {
		 	  forma.TipoAccion.value = prmt;
		forma.submit();	
	}
}
function EnviarPage(prmt, pForm)
{	pForm.TipoAccion.value = prmt;
	pForm.submit();}


function Lock_Field(campo) {
  //var isIE = document.all?true:false;
  var _ret = true;
  tipo_campo = campo.name;		   		// obtiene el nombre del control.
  tp_camp = tipo_campo.substring(0,1);  // obtiene el primer caracter del nombre del control.
  tp_camp = tp_camp.toUpperCase();
  //alert(window.event.keyCode)
  // si se pulso ENTER, pasa el focus al siguiente campo (control) 
  // y deja formateado a miles si se trata de un campo de montos
   if ( window.event.keyCode == 13) {  
	   if ( tp_camp == 'M' ) {
		   //campo.value = asMoney(campo.value);
	   }
	   campo = tabOnEnter(campo, event);
   }
   //if ( tp_camp == 'M' ) {
       //alert(campo.value)
	//   campo.value = asMoney(campo.value);
  //} 
  if (campo.disabled==false) {
  switch (tp_camp) {
		 // validacion numerica
		 case 'N' : if (window.event.keyCode < 48 || window.event.keyCode > 57) {   //no setrata de un numero
					   window.event.keyCode = 0;
					  _ret = false;
					} break;
		 case 'T' : if (window.event.keyCode < 48 || window.event.keyCode > 57) {   //no setrata de un numero
					   window.event.keyCode = 0;
					  _ret = false;
					} break;
		 // validacion de campos fecha en formato AA/MM
		 case 'F' : if (window.event.keyCode < 48 || window.event.keyCode > 57) {   //no setrata de un numero
					   window.event.keyCode = 0;
					  _ret = false;
					} break;															
		 // validacion de montos (con el punto decimal)
		 case 'M' : if (window.event.keyCode < 46 || window.event.keyCode == 47  || window.event.keyCode > 57 ) {   //no setrata de un numero o del punto decimal		 
					   window.event.keyCode = 0;
					  _ret = false;					  
					} 
					// validacion para que solo permita digitar una solo vez el punto(.)
	                if (window.event.keyCode == 46 ){ // el punto decimal(.)
					   if (InStr(campo.value,'.')>=0) {
					        window.event.keyCode = 0;
						 }
					}
					break;
		 // validacion de alfabéticos y la barra espaciadora
		 case 'A' : 
//		 alert(window.event.keyCode);
		 if ((window.event.keyCode < 32 || window.event.keyCode > 32 ) && (window.event.keyCode < 65 || window.event.keyCode > 90 ) && (window.event.keyCode < 97 || window.event.keyCode > 122 ) && (window.event.keyCode!= 241) && (window.event.keyCode!=209)  ) {
					   window.event.keyCode = 0;
					  _ret = false;
					} break;
		 // validacion de montos (con el punto decimal)
		/* case 'U' : if (window.event.keyCode < 46 || window.event.keyCode == 47  || window.event.keyCode > 57 ) {   //no setrata de un numero o del punto ecimal
					   window.event.keyCode = 0;
					  _ret = false;
					} break; */
   }// End switch
   //------------------------
   if (tp_camp == 'T') { // numeros de tarjetas separadas de 4 en 4 ejem: 1223-2323-4444-5567
	   if ((campo.value.length==4) || (campo.value.length==9) || (campo.value.length==14) || (campo.value.length==19) || (campo.value.length==24)) {
		  campo.value=campo.value + '-';
	   }   
   }
   //------------------------
   if (tp_camp == 'F') { // fecha en formato AA/MM ejem: 03/12
	   if ((campo.value.length==2) || (campo.value.length==5)) {
		  campo.value=campo.value + '/';         //               99/99/9999
	   } 
   }
   //------------------------   
   /*if (tp_camp == 'U') { // numeros de tarjetas separadas de 4 en 4 ejem: 1223-2323-4444-5567
	   if ((campo.value.length==3) || (campo.value.length==7) || (campo.value.length==11)) {
		  campo.value=campo.value + ',';
	   }   
   }*/
   //------------------------   
   }
   return (_ret); 
}

//----------------------------------------------------------------------
//Objetivo: Si llega un 6º parametro le aplica el scrollbars a la ventana. 
//----------------------------------------------------------------------
function OpenWinPopup(pagina,x,y,z,w) { 
var numargs = OpenWinPopup.arguments.length;
   //alert(numargs);
   if (numargs >= 5 ) {
      window.open(pagina, 'WinPopup','width='+x+',height='+y+',top='+z+',left='+w+',status,scrollbars');
   } else {
      window.open(pagina, 'WinPopup','width='+x+',height='+y+',top='+z+',left='+w+',status,noscrollbars');
   }
}

function OpenWinMenuPopup(pagina,x,y,z,w) {
	window.open(pagina, 'WinPopup','width='+x+',height='+y+',top='+z+',left='+w+',menubar =yes, resizable=1');
}


//Desactiva BackSpace
function checkKey() {
	var BACKSPACE = 8;
	if(window.event.keyCode == BACKSPACE) {
		window.event.keyCode = 0;
	}

}

function select(field) {
  field.select();
  field.focus();
}

function winConsulta(page) 
{
window.open(page, 'winConsulta', 'width=530,height=500,top=70,scrollbars=0');
}

function windows(page) 
{
window.open(page, 'Winname', 'width=580,scrollbars=1,height=500,top=10');
}

function windows2(page) 
{
window.open(page, 'Winname', 'width=800,scrollbars=1,height=600,resizable,top=10,left=150');
}

function Solofun_Submitir(forma,Pagina)
	{
	forma.opc.value="C";
	forma.action=Pagina;
	forma.submit();
}

function Submit_Form(forma)
{   
    if (valida_campos(forma)) {
//	alert(forma.name);	forma.action = document.all.txtform.value + '.asp';	alert(forma.action);
	forma.submit();	
	}
}

function valida_campos(form){
  var valido;
  var no_campo;
  var tp_camp;
  
  valido=true;
//	  alert(form.name);
  for (var e = 0; e < form.elements.length; e++) { // se recorre todos los campos que existan en el formulario
	  tipo_campo = form.elements[e].name;		   // obtiene el nombre del control.
	  tp_camp = tipo_campo.substring(0,1);         // obtiene el primer caracter del nombre del control.
	  //tp_camp=tp_camp.toUpperCase();
	  //alert(tipo_campo + tp_camp + form.elements[e].disabled);
	  if (form.elements[e].disabled==false) { // valida solo los controles que no estan desabilitados
	  switch (tp_camp) {
				 // validacion alfabetica o alfanumerica
				 case 'A' : if (form.elements[e].value.length==0) {
							  valido=false;
						   }  break;
				 // validacion alfanumerica
				 case 'X' : if (form.elements[e].value.length==0) {
							  valido=false;
						   }  break;						   
				 // validacion numerica
				 case 'N' : if (form.elements[e].value.length==0 || isNaN(form.elements[e].value) || parseFloat(form.elements[e].value) < 0) {
							  valido=false;
						   }  break;
				 // validacion numerica para montos
				 case 'M':  if (form.elements[e].value.length==0 || parseFloat(form.elements[e].value) < 0) {					   						   
							  valido=false;
						   }  break;
	   } // End switch
	   } //end if
	   if (valido==false) {
		  alert("Este Campo es requerido, favor ingrese un valor.");
		  form.elements[e].select();
		  form.elements[e].focus();
		  break;  // break del for
	   }
   }// End for

   return valido;
}// End Function

function tabOnEnter(field, evt)
{
    var keyCode = document.layers ? evt.which : document.all ? evt.keyCode : evt.keyCode;
    var nextfield;
    if (keyCode != 13)
        return true;
    else {
        nextfield = getNextElement(field);
		while (nextfield.disabled == true || nextfield.type == 'hidden') {
			field = nextfield;
        	nextfield = getNextElement(field);
		}
		nextfield.focus();
        return false;
    }
}

// --------------------------------------------------------------------------------------------------------
//  OBTIENE EL SIGUIENTE ELEMENTO EN UN FORMULARIO
// --------------------------------------------------------------------------------------------------------
function getNextElement(field)
{
    var form = field.form;
    for (var e = 0; e < form.elements.length; e++)
        if (field == form.elements[e])
            break;
        return form.elements[++e % form.elements.length];
}


//Funciones añadidos el 30 de Septiembre por Anl. Rosa Moreira
function LCase(Value) {
  return Value.toString().toLowerCase();
}

function UCase(Value) {
  return Value.toString().toUpperCase();
}

function Len(Expression) {
  return Expression.toString().length;
}

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function Mid(Str, Start, Length) {
  if (Length < 1) {
    alert("Invalid Length argument\n\nMid function (js2vb.js)"); return "";
  }
  if (Start < 0) {
    alert("Invalid Start argument\n\nMid function (js2vb.js)"); return "";
  }
  return Str.substring(Start, Start + Length);
}

function InStr(Start, String1, String2, Compare) {
  if (Start < 1) {
    alert("Invalid Start argument\n\nInStr function (js2vb.js)"); return "";
  }
  if (Start > Len(String1)) return 0;
  if (Len(String2) == 0) return Start;
  if (Compare == 1) {String1 = LCase(String1); String2 = LCase(String2);}
  if (Start > 1) {
    var index = Right(String1, Len(String1) - Start + 1).indexOf(String2)
    if (index == -1) {return 0;} else {return index + Start;}
  } else {
    return String1.indexOf(String2) + 1
  }
}

function InStrRev(StringCheck, StringMatch, Start, Compare) {
  if (Start == 0 || Start < -1) {
    alert("Invalid Start argument\n\nInStrRev function (js2vb.js)");
	return "";
  }
  if (Len(StringMatch) == 0) return Start;
  if (Compare == 1) {
    StringCheck = LCase(StringCheck); StringMatch = LCase(StringMatch);
  }
  if (Start > 1) {
    return Left(StringCheck, Start).lastIndexOf(StringMatch) + 1;
    } else {
    return StringCheck.lastIndexOf(StringMatch) + 1;
  }
}

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);
   }
	//alert('LTrim: ' + typeof(s));
   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);
   }

	//alert('RTrim: ' + typeof(s));
   return s;
}

//Trim
function Trim(s) 
{
  while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
  {
    s = s.substring(1,s.length);
  }
  while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
  {
    s = s.substring(0,s.length-1);
  }
  return s;
}

function IsNull(Expression) {
  return (Expression == null);
}

function IsEmpty(Expression) {
  return (Expression.toString().length == 0);
}

function IsObject(Expression) {
  return (typeof Expression == "object");
}

function IsArray(VarName) {
  return (VarName.constructor.toString().indexOf("Array") == -1);
}

function IsDate(Expression) {
  var test = new Date(Date.parse(Expression));
  return !(isNaN(test.getYear()));
}

function ValidaNumero(){
	if (window.event.keyCode < 48 || window.event.keyCode > 57) { 
		   window.event.keyCode = 0;
		   return;
	}
}

function canAddCharacter(textarea, maxChars) 
{ 
     if(typeof(textarea.onkeypress.arguments[0]) != 'undefined') 
          var keyCode = textarea.onkeypress.arguments[0].keyCode; 
     else 
     { 
          if(document.selection.createRange().text.length != 0) return true; 
          var keyCode = event.keyCode; 
     } 

     var allowedChars = new Array(8, 37, 38, 39, 40, 46);     //Backspace, delete and arrow keys 
     for(var x=0; x<allowedChars.length; x++) if(allowedChars[x] == keyCode) return true; 

     if(textarea.value.length < maxChars) return true; 

     return false; 
}

//function BuscarItems(Nodo,select2)
function BuscarItems()
{
//alert(Nodo);alert(select2);
var i=0;var myEle;
for (i=document.all.CmbPerfil.options.length;i >=0;i--)
	document.all.CmbPerfil.options[i]=null; 

//alert(document.all.CL_FilasNodo.value);
//alert(document.all.hidCod.value);
//alert(document.all.hidDes.value);

if (document.all.CL_FilasNodo.value==0){
	  myEle = document.createElement("option") ;
	  myEle.value = document.all.hidCod.value;
	  myEle.text = document.all.hidDes.value; 
	  document.all.CmbPerfil.add(myEle);
   }

//alert(document.all.hidCod.length);
for (i=0;i<document.all.hidCod.length;i++)
	if (document.all.hidNod[i].value == Nodo) {
	  myEle = document.createElement("option") ;
	  myEle.value = document.all.hidCod[i].value;
	  myEle.text = document.all.hidDes[i].value; 
	  document.all.CmbPerfil.add(myEle);
	  if (myEle.value==select2) {
		 myEle.selected="true"
	  }
	}
return;
}
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
