//emailvalidation(this,text)

//Checking if the content has the general syntax of an email.
//Optional parameters are:
//text--text that will show in an alertbox if content is illegal.

function isEmail(entered) {

    var reg_exp = /^[a-z][\w\.]*@[\w\.]+\.[a-z]{2,3}/i

    if (entered.value==null || entered.value=="")
      return true;
    else
    {
      if(reg_exp.test(entered.value)) {
          return true;
      }
      else {
          alert("Capture un e-mail válido!");
          entered.focus();
          entered.select();
          return false;
      }
    }
}




//valuevalidation(this,min,max,text,type)

//Checking if the content is a number in a limited area.
//Optional parameters are:
//min --minimum value allowed in the field.
//max --maximum value allowed in the field.
//text --text that will show in an alertbox if content is illegal.
//type --enter "I" if only integers are allowed.

function valuevalidation(entered, min, max, alertbox, datatype)
{
with (entered)
{
checkvalue=parseFloat(value);
if (datatype)
{smalldatatype=datatype.toLowerCase();
if (smalldatatype.charAt(0)=="i") {checkvalue=parseInt(value)};
}
if ((parseFloat(min)==min && checkvalue<min) || (parseFloat(max)==max && checkvalue>max) || value!=checkvalue)
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
}




//digitvalidation(this,min,max,text,type)

//Checking if the content has a certain number of digits.
//Optional parameters are:
//min --minimum number of digits allowed in the field.
//max --maximum number of digits allowed in the field.
//text --text that will show in an alertbox if content is illegal.
//type --enter "I" if only integers are allowed.


function digitvalidation(entered, min, max, alertbox, datatype)
{
with (entered)
{
checkvalue=parseFloat(value);
if (datatype)
{smalldatatype=datatype.toLowerCase();
if (smalldatatype.charAt(0)=="i")
{checkvalue=parseInt(value); if (value.indexOf(".")!=-1) {checkvalue=checkvalue+1}};
}
if ((parseFloat(min)==min && value.length<min) || (parseFloat(max)==max && value.length>max) || value!=checkvalue)
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
}



//emptyvalidation(this,text)

//Checking if the field is empty.
//Optional parameters are:
//text --text that will show in an alertbox if content is illegal.

function emptyvalidation(entered, alertbox)
{
with (entered)
{
if (value==null || value=="")
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
}

function validatecodigo(field) {
var valid = "-0123456789"
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("Entrada inválida!  Solamente numeros y guion(-) son permitidos!");
field.focus();
field.select();
   }
}

function validatefloat(field) {
var valid = ".0123456789"
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("Entrada inválida!  Solamente numeros y punto(.) son permitidos!");
field.focus();
field.select();
   }
}

function validateint(field) {
var valid = "0123456789"
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("Entrada inválida!  Solamente numeros son permitidos!");
field.focus();
field.select();
   }
}

function round(number,X) {
// REdondea el decimal number X posiciones del punto decimal, por default 2
X = (!X ? 2 : X);
return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}

function suma(field1, field2, fieldresult)
{
   fieldresult.value = round(field1.value * field2.value);
}

function sumaprods(f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,result)
{
  /*if(f1.value == "") f1.value = 0;
  if(f2.value == "") f2.value = 0;
  if(f3.value == "") f3.value = 0;
  if(f4.value == "") f4.value = 0;
  if(f5.value == "") f5.value = 0;
  if(f6.value == "") f6.value = 0;
  if(f7.value == "") f7.value = 0;
  if(f8.value == "") f8.value = 0;
  if(f9.value == "") f9.value = 0;
  if(f10.value == "") f10.value = 0;
  if(f11.value == "") f11.value = 0;
  if(f12.value == "") f12.value = 0;*/
  //result.value = parseInt(f1.value)+parseInt(f2.value)+parseInt(f3.value)+parseInt(f4.value)+ parseInt(f5.value)+parseInt(f6.value)+parseInt(f7.value)+parseInt(f8.value)+ parseInt(f9.value)+parseInt(f10.value)+parseInt(f11.value)+parseInt(f12.value);

  var fields = new Array();
  fields[0] = f1;
  fields[1] = f2;
  fields[2] = f3;
  fields[3] = f4;
  fields[4] = f5;
  fields[5] = f6;
  fields[6] = f7;
  fields[7] = f8;
  fields[8] = f9;
  fields[9] = f10;
  fields[10] = f11;
  fields[11] = f12;
 suma = 0;
  for (i=0; i<fields.length; i++)
  { if (fields[i].value != "")
  	suma = suma + parseInt (fields[i].value);
  } 
  result.value = suma;
  
}

function validatenum(field) {
var valid = "123456"
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("Entrada inválida!  Solamente numeros del 1 al 6 son permitidos!");
field.focus();
field.select();
   }
}

function validaterfc(field) {
var valid = "-0123456789ABCDEFGHIJKLMNÑOPQRSTUVWXYZabcdefghijklmnñopqrstuvwxyz";
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("Entrada inválida!  Solamente letras, numeros y guion(-) son permitidos!");
field.focus();
field.select();
   }
}

function validateurl(field) {
var valid = "-0123456789ABCDEFGHIJKLMNÑOPQRSTUVWXYZabcdefghijklmnñopqrstuvwxyz., ";
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("Entrada inválida!  Caracteres epeciales no permitidos");
field.focus();
field.select();
   }
}


function sumaArpillas(f1,f2,f3,f4,f5,f6,f7,result)
{
  /*if(f1.value == "") f1.value = 0;
  if(f2.value == "") f2.value = 0;
  if(f3.value == "") f3.value = 0;
  if(f4.value == "") f4.value = 0;
  if(f5.value == "") f5.value = 0;
  if(f6.value == "") f6.value = 0;
  if(f7.value == "") f7.value = 0;

  result.value = parseInt(f1.value)+parseInt(f2.value)+parseInt(f3.value)+parseInt(f4.value)+ parseInt(f5.value)+parseInt(f6.value)+parseInt(f7.value);*/

  var fields = new Array();
  fields[0] = f1;
  fields[1] = f2;
  fields[2] = f3;
  fields[3] = f4;
  fields[4] = f5;
  fields[5] = f6;
  fields[6] = f7;
  suma = 0;
  for (i=0; i<fields.length; i++)
  { if (fields[i].value != "")
  	suma = suma + parseInt (fields[i].value);
  } 
  result.value = suma;
}

function sumaCerdos(f1,f2,f3,f4,result)
{
  /*if(f1.value == "") f1.value = 0;
  if(f2.value == "") f2.value = 0;
  if(f3.value == "") f3.value = 0;
  if(f4.value == "") f4.value = 0;

  result.value = parseInt(f1.value)+parseInt(f2.value)+parseInt(f3.value)+parseInt(f4.value);*/
  var fields = new Array();
  fields[0] = f1;
  fields[1] = f2;
  fields[2] = f3;
  fields[3] = f4;
  suma = 0;
  for (i=0; i<fields.length; i++)
  { if (fields[i].value != "")
  	suma = suma + parseInt (fields[i].value);
  } 
  result.value = suma;
}

function keyUp(text)
   {
     var key = window.event.keyCode
	 var realkey = String.fromCharCode(window.event.keyCode)
	 if ((key!=37) && (key!=39) && (key!=32) && (key!=8))
	   text.value = text.value.toUpperCase();
   }

