// JavaScript Document

function modificar_img(obj,img) {
	obj.src = "/imagenes/"+img;
}

// Removes leading whitespaces
function ltrim(value) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function rtrim(value) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim(value) {
	
	return ltrim(rtrim(value));
	
}

/////////////////////////////////////////////////////////////////////////////////////////////////
// emailCheck: Comprueba que el string pasado cmo parametro se trata de un email correcto,     // 
// para ello verifica:																																				 //
//																																														 //
// 1.- Si el email tiene el formato user@dominio																							 //
// 2.- La existencia de caracteres no validos y																								 //
// 3.- Si la dirección de email está representada con una dirección IP Válida									 //
/////////////////////////////////////////////////////////////////////////////////////////////////

function emailCheck (emailStr) {
  
  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) {
    op = confirm("Atención, el EMAIL parece incorrecto. ¿Seguro que desea continuar?")
		if (op) return true
		else return false
  }
  var user=matchArray[1]
  var domain=matchArray[2]

	// Comprobamos si el nombre de usuario es valido
  if (user.match(userPat)==null) {
    alert("Error.- El nombre de usuario indicado en el EMAIL no es válido.")
  	return false
  }

  // Comprobamos que la dirección IP es válida */
  var IPArray=domain.match(ipDomainPat)
  if (IPArray!=null) {
    for (var i=1;i<=4;i++) {
      if (IPArray[i]>255) {
        alert("Error.- IP de destino indicada en el EMAIL es inválida.")
        return false
      }
    }
    return true
  }

  // Comprobamos que el dominio es válido */
  var domainArray=domain.match(domainPat)
  if (domainArray==null) {
    alert("Error.- El dominio indicado en el EMAIL parece no ser válido.")
    return false
  }

  var atomPat=new RegExp(atom,"g")
  var domArr=domain.match(atomPat)
  var len=domArr.length
	
  if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>4) {
      alert("Error.- La dicrección EMAIL debe tener 4 letras si es .info, 3 letras si es .com o 2 si es de algún pais.")
      return false
  }
	
  if (len<2) {
    var errStr="Error.- La dirección EMAIL es erronea."
    alert(errStr)
    return false
  }

  // La dirección de email ingresada es Válida
  return true;
}


/////////////////////////////////////////////////////////////////////////////////////////////////
// NumberFormat                                     																	         // 
//																																														 //																																			 //
// To use addSeparatorsNF, you need to pass it the following arguments:                        //
// nStr: The number to be formatted, as a string or number. No validation is done, so don't    //
// input a formatted number. If inD is something other than a period, then nStr must be passed //
// in as a string.																																						 //		
// inD: The decimal character for the input, such as '.' for the number 100.2									 //
// outD: The decimal character for the output, such as ',' for the number 100,2								 //
// sep: The separator character for the output, such as ',' for the number 1,000.2 						 //
/////////////////////////////////////////////////////////////////////////////////////////////////

function NumberFormat(nStr, inD, outD, sep) {
	nStr += '';
	var dpos = nStr.indexOf(inD);
	var nStrEnd = '';
	if (dpos != -1) {
		nStrEnd = outD + nStr.substring(dpos + 1, nStr.length);
		nStr = nStr.substring(0, dpos);
	}
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(nStr)) {
		nStr = nStr.replace(rgx, '$1' + sep + '$2');
	}
	return nStr + nStrEnd;
}