//validaciones generales
function disableRightClick(e)
{
  if(!document.rightClickDisabled) // initialize
  {
    if(document.layers) 
    {
      document.captureEvents(Event.MOUSEDOWN);
      document.onmousedown = disableRightClick;
    }
    else document.oncontextmenu = disableRightClick;
    return document.rightClickDisabled = true;
  }
  if(document.layers || (document.getElementById && !document.all))
  {
		if (document.implementation.hasFeature("MouseEvents","2.0")) {
      return false;
		}
    if (e.which!=1||e.button!=1)
    {
      e.which = 1;
			e.button = 1;
      return false;
    }
  }
  else
  {
    return event.ctrlKey;
  }
}
//disableRightClick();


function isNumberFloat(inputString) {
  return (!isNaN(inputString)) ? true : false;
}

/*function checkNumero(){
	if (document.all) {
		// manejo de eventos para Explorer
		var char0 = "0".charCodeAt(0);
		var char9 = "9".charCodeAt(0);
		//alert(window.event.keyCode);
		var offsetNumKeyPad = 48;
		if ((window.event.keyCode >= char0 && window.event.keyCode <= char9) || (window.event.keyCode >= parseInt(char0+offsetNumKeyPad) && window.event.keyCode <= parseInt(char9+offsetNumKeyPad)) || window.event.keyCode == 13 || window.event.keyCode == 8 || window.event.keyCode == 9 || window.event.keyCode == 46)
			return true;
		window.event.returnValue = "";
		return false;
	}
}*/

function checkNumero(e) {
	var char0 = "0".charCodeAt(0);
	var char9 = "9".charCodeAt(0);
	//alert(window.event.keyCode);
	//var offsetNumKeyPad = 48;
	var whichKey;
	if (document.all) {
		whichKey = window.event.keyCode;
	}
	else {
		whichKey = e.which;
	}
	//alert(whichKey)
	//if ((whichKey >= char0 && whichKey <= char9) || (whichKey >= parseInt(char0+offsetNumKeyPad) && whichKey <= parseInt(char9+offsetNumKeyPad)) || whichKey == 13 || whichKey == 8 || whichKey == 9 || whichKey == 46) {
	if ((whichKey >= char0 && whichKey <= char9) || whichKey == 13 || whichKey == 8 || whichKey == 9 || whichKey == 46) {
		return true;
	}
	if (document.all) {
		window.event.returnValue = "";
	}
	else {
		e.preventDefault();
	}
	return false;
}

function quitarCaracteres (inputString, removeChar)
{
  var returnString = inputString;
  if (removeChar.length)
  {
    while('' + returnString.charAt(0) == removeChar)
    {
      returnString = returnString.substring(1,returnString.length);
    }
    while('' + returnString.charAt(returnString.length - 1) == removeChar)
    {
      returnString = returnString.substring(0,returnString.length - 1);
    }
  }
  return returnString;
}
function removeLeadingAndTrailingChar (inputString, removeChar) 
{
	var returnString = inputString;
	if (removeChar.length)
	{
	  while(''+returnString.charAt(0)==removeChar)
		{
		  returnString=returnString.substring(1,returnString.length);
		}
		while(''+returnString.charAt(returnString.length-1)==removeChar)
	  {
	    returnString=returnString.substring(0,returnString.length-1);
	  }
	}
	return returnString;
}

function trimSpaces(inputString) {
	return removeLeadingAndTrailingChar(inputString,' ');
}

function rechazarBrowsers() {
	myUserAgent   = navigator.userAgent.toLowerCase();
	if ( (myUserAgent.indexOf('opera')>= 0) || 
			 (myUserAgent.indexOf('msie')<0 && myUserAgent.indexOf('firefox')<0) ||
			 (myUserAgent.indexOf('msie')> 0 && (!document.all || myUserAgent.substr(myUserAgent.indexOf('msie')+5,1) < 5)) ||
		 	 (document.layers) ) {
		top.location = "/soporte/navegadorNoSoportado.asp";
	}
}

function minLength(inputString,inputLength)
{
  return (inputString.length >= inputLength);
}
  
function onlyNumbers(inputString)
{
  var searchForNumbers = /\D/
  return (!searchForNumbers.test(inputString));
} 

function verificarTextArea(control, cantMaxima) {
	var escritos = eval(control.value.length);
	var dif = eval(escritos - cantMaxima);
	if (dif > 0) {
		var value = control.value.substr(0,cantMaxima);
		control.value = value;
	}
}

function mostrarDisponiblesTextArea(control, cantMaxima, textoDisponibles) {
	var escritos = eval(control.value.length);
	var disponibles = eval(cantMaxima - escritos);
	if (disponibles < 0) {
		var dif = eval(escritos - cantMaxima);
		var value = control.value.substr(0,cantMaxima);
		control.value = value;
		disponibles = 0;
	}
	textoDisponibles.value = disponibles;
}

function emailCheck (emailStr) {

/* The following variable tells the rest of the function whether or not
to verify that the address ends in a two-letter country or well-known
TLD.  1 means check it, 0 means don't. */

var checkTLD=1;

/* The following is the list of known TLDs that an e-mail address must end with. */

var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|cc|gob)$/;

/* The following pattern is used to check if the entered e-mail address
fits the user@domain format.  It also is used to separate the username
from the domain. */

var emailPat=/^(.+)@(.+)$/;

/* The following string represents the pattern for matching all special
characters.  We don't want to allow special characters in the address. 
These characters include ( ) < > @ , ; : \ " . [ ] */

var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

/* The following string represents the range of characters allowed in a 
username or domainname.  It really states which chars aren't allowed.*/

var validChars="\[^\\s" + specialChars + "\]";

/* The following pattern applies if the "user" is a quoted string (in
which case, there are no rules about which characters are allowed
and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
is a legal e-mail address. */

var quotedUser="(\"[^\"]*\")";

/* The following pattern applies for domains that are IP addresses,
rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
e-mail address. NOTE: The square brackets are required. */

var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

/* The following string represents an atom (basically a series of non-special characters.) */

var atom=validChars + '+';

/* The following string represents one word in the typical username.
For example, in john.doe@somewhere.com, john and doe are words.
Basically, a word is either an atom or quoted string. */

var word="(" + atom + "|" + quotedUser + ")";

// The following pattern describes the structure of the user

var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

/* The following pattern describes the structure of a normal symbolic
domain, as opposed to ipDomainPat, shown above. */

var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

/* Finally, let's start trying to figure out if the supplied address is valid. */

/* Begin with the coarse pattern to simply break up user@domain into
different pieces that are easy to analyze. */

var matchArray=emailStr.match(emailPat);

if (matchArray==null) {

/* Too many/few @'s or something; basically, this address doesn't
even fit the general mould of a valid e-mail address. */

alert("La dirección de email parece ser incorrecta\nVerifique (@) y (.)");
return false;
}
var user=matchArray[1];
var domain=matchArray[2];

// Start by checking that only basic ASCII characters are in the strings (0-127).

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;
   }
}

// See if "user" is valid 

if (user.match(userPat)==null) {

// user is not valid

alert("El nombre de usuario no parece ser válido.");
return false;
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
host name) make sure the IP address is valid. */

var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {

// this is an IP address

for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
alert("La dirección IP es inválida!");
return false;
   }
}
return true;
}

// Domain is symbolic name.  Check if it's valid.
 
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;
   }
}

/* domain name seems valid, but now make sure that it ends in a
known top-level domain (like com, edu, gov) or a two-letter word,
representing country (uk, nl), and that there's a hostname preceding 
the domain or country. */

if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1) {
alert("La dirección debe terminar en un dominio conocido\no código de país de dos letras.");
return false;
}

// Make sure there's a host name preceding the domain.

if (len<2) {
alert("Esta dirección no tiene un nombre de host!");
return false;
}

// If we've gotten this far, everything's valid!
return true;
}
