// JavaScript Document

function onKeyPressNumbers(e)
{
	var key = window.event ? e.keyCode : e.which
	var keychar = String.fromCharCode(key)
	reg = /\d/;
	return reg.test(keychar)
}

function roundNumber(original_number) {
	var decimals =2
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}

function emailCheck (emailStr) {

	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);

	if (matchArray==null) {
		alert("Dirección email parece incorrecto (verifique @ y puntos)");
		return false;
	}

	var user=matchArray[1];
	var domain=matchArray[2];

	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
		alert("El nombre de usuario contiene caracteres inválidos.");
		return false;
		}
	}

	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
		alert("El nombre de dominio contiene caracteres inválidos.");
		return false;
		}
	}

	if (user.match(userPat)==null) {
		alert("El nombre de usuario no parece ser válido");
		return false;
	}

	var IPArray=domain.match(ipDomainPat);

	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
			alert("dirección IP es inválido!");
			return false;
			}
		}
		return true;
	}

	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;

	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
		alert("El nombre de dominio no parece ser válido.");
		return false;
		}
	}

	if (checkTLD && domArr[domArr.length-1].length!=2 &&
	domArr[domArr.length-1].search(knownDomsPat)==-1) {
	alert("la direccion de email debe terminar con un dominio correcto o con dos letras del pais");
	return false;
	}

	if (len<2) {
	alert("Dirección no tiene un Hostname!");
	return false;
	}

	return true;
}

function validateChars(word)
{
  if (window.RegExp)
  {
    var reg = new RegExp("^[0-9,a-z,A-Z,_,-]*$","g");
    if (!reg.test(word))
      return(false);
  }
  return true;
}

function valCharacters(f){
  if (window.RegExp)
  {
    var reg = new RegExp("^[^0-9]*$","g");
    if (!reg.test(f.elements['firstname'].value))
    {
      alert("El nombre debe contener solo letras.");
      f.elements['firstname'].focus();
      return(false);
    }
  }
  return true;
}
