// JavaScript Document
/*** FUNCIONES PARA VALIDAR DATOS ***/
/*** APLICAN LOS ESTILOS DEFINIDOS EN VALIDATOR.CSS**/
var wrong_validator_message = 'Some of the data you submitted is missing or incomplete. Please review your submition and try again.';
var required_validator_message = '* Required';
var invalid_validator_message = '* Invalid';
var numeric_validator_message = '* Must be numeric';
var match_validator_message = '* Mismatch';


function noValidator(validador)
{
	document.getElementById(validador).className = 'noValidator'; 
}

function wrongValidator(validador)
{
	document.getElementById(validador).className = 'wrongValidator'; 
}

/* USADO EN EL ONFOCUS DEL CONTROL */
function onfocusValidator(control)
{
	document.getElementById(control).focus();
}

function dateValidator(control,validador)
{
	var date = document.getElementById(control).value;
	date = date.split('/'); 
	date = date[1]+'/'+date[0]+'/'+date[2]; //cambiar a m/d/Y si no esta
	
	var test1 = (/^\d{1,2}\/?\d{1,2}\/\d{4}$/.test(date));

	date = date.split('/');
	
	d = new Date(date[2],date[0]-1,date[1]) //m/d/Y
	test2 = (1*date[0]==(d.getMonth()+1) && 1*date[1]==d.getDate() && 1*date[2]==d.getFullYear())
	
	if (!test1 || !test2) 
	{
		document.getElementById(validador).className = 'wrongValidator'; 
		document.getElementById(validador).innerText = invalid_validator_message;
		return false;
	}
	else
	{
		document.getElementById(validador).className = 'rightValidator';
		document.getElementById(validador).innerText = '';
		return true;
	}
}

function validateDate(obj){
	//en lugar del calendario d/m/Y
	date=obj.value
	if (/[^\d/]|(\/\/)/g.test(date))  {obj.value=obj.value.replace(/[^\d/]/g,'');obj.value=obj.value.replace(/\/{2}/g,'/'); return }
	if (/^\d{2}$/.test(date)){obj.value=obj.value+'/'; return }
	if (/^\d{2}\/\d{2}$/.test(date)){obj.value=obj.value+'/'; return }
	if (!/^\d{1,2}\/\d{1,2}\/\d{4}$/.test(date)) return
}

/* USADO EN EL ONBLUR DEL CONTROL PARA VALIDAR CONTROLES VACIOS*/
function emptyValidator(control,validador)
{
	
	if(document.getElementById(control).value == '')
	{
		document.getElementById(validador).className = 'wrongValidator'; 
		document.getElementById(validador).innerText = required_validator_message;
		return false;
	}
	else
	{
		document.getElementById(validador).className = 'rightValidator';
		document.getElementById(validador).innerText = '';
		return true;
	}
}

function birthdayValidator(control1,control2,control3,validador)
{
	if(document.getElementById(control1).selectedIndex <= 0 || document.getElementById(control2).selectedIndex <= 0 || document.getElementById(control3).selectedIndex <= 0)
	{
		document.getElementById(validador).className = 'wrongValidator';
		document.getElementById(validador).innerText = required_validator_message;
		return false;
	}
	else
	{
		document.getElementById(validador).className = 'rightValidator';
		document.getElementById(validador).innerText = '';
		return true;
	}
}

/* USADO EN EL ONBLUR DEL CONTROL PARA VALIDAR COMBOS SIN SELECCIONAR*/
function selectValidator(control,validador)
{
	
	if(document.getElementById(control).selectedIndex <= 0)
	{
		document.getElementById(validador).className = 'wrongValidator'; /*RED*/
		document.getElementById(validador).innerText = required_validator_message;
		return false;
	}
	else
	{
		document.getElementById(validador).className = 'rightValidator'; /*GREEN*/
		document.getElementById(validador).innerText = '';
		return true;
	}
}

/* USADO EN EL ONBLUR DEL CONTROL PARA VALIDAR COMBOS SIN SELECCIONAR SIN --Seleccione--*/
function selectValidatorNOSEL(control,validador)
{
	
	if(document.getElementById(control).selectedIndex < 0)
	{
		document.getElementById(validador).className = 'wrongValidator'; /*RED*/
		document.getElementById(validador).title = required_validator_message;
		return false;
	}
	else
	{
		document.getElementById(validador).className = 'rightValidator'; /*GREEN*/
		document.getElementById(validador).title = '* OK';
		return true;
	}
}

/* USADO EN EL ONBLUR DEL CONTROL PARA VALIDAR QUE EL PASS REPETIDO SEA IGUAL AL INGRESADO*/
function passwordValidator(controlReferencia,control,validador)
{
	if(document.getElementById(controlReferencia).value !='')
	{
		if(document.getElementById(controlReferencia).value != document.getElementById(control).value)
		{
			document.getElementById(validador).className = 'wrongValidator';
			document.getElementById(validador).innerText = match_validator_message;
			return false;
		}
		else
		{
			document.getElementById(validador).className = 'rightValidator';
			document.getElementById(validador).innerText = '';
			return true;
		}
	}
	else
	{
		document.getElementById(validador).className = 'wrongValidator';
		document.getElementById(validador).innerText = required_validator_message;
		return false;
	}
}

/* USADO EN EL ONBLUR DEL CONTROL PARA VALIDAR QUE EL PASS REPETIDO SEA IGUAL AL INGRESADO*/
function passwordValidatorNuevo(controlReferencia,control,validador,nuevo)
{
	if(document.getElementById(controlReferencia).value !='')
	{
		if(document.getElementById(controlReferencia).value != document.getElementById(control).value)
		{
			document.getElementById(validador).className = 'wrongValidator'; /*RED*/
			document.getElementById(validador).title = match_validator_message;
			return false;
		}
		else
		{
			document.getElementById(validador).className = 'rightValidator'; //GREEN
			document.getElementById(validador).title = '* OK';
			return true;
		}
	}
	else
	{
		if(nuevo)
		{
			document.getElementById(validador).className = 'wrongValidator'; /*RED*/
			document.getElementById(validador).title = required_validator_message;
			return false;
		}
		else
		{
			document.getElementById(validador).className = 'rightValidator'; //GREEN
			document.getElementById(validador).title = '* OK';
			return true;
		}
	}
}

/* USADO EN EL ONBLUR DEL CONTROL PARA VALIDAR CONTROLES NUMERICOS*/
function numberValidator(control,validador,valempty)
{
	if(valempty && document.getElementById(control).value == '')
	{
		document.getElementById(validador).className = 'wrongValidator';
		document.getElementById(validador).innerText = required_validator_message;
		return false;
	}
	else if(document.getElementById(control).value != '')
	{
		if (isNaN(document.getElementById(control).value)) 
	  	{ 
            document.getElementById(validador).className = 'wrongValidator'; /*RED*/
			document.getElementById(validador).innerText = numeric_validator_message;
			return false;
     	}
	  	else
	 	{ 
           	document.getElementById(validador).className = 'rightValidator'; /*GREEN*/
			document.getElementById(validador).innerText = '';
			return true;
      	} 
	}
}

/* USADO EN EL ONBLUR DEL CONTROL PARA VALIDAR QUE UN CHECBOX ESTE CHECKEADO*/
function checkValidator(control,validador)
{
	if(!document.getElementById(control).checked)
	{
		document.getElementById(validador).className = 'wrongValidator'; /*RED*/
		/*document.getElementById(validador).innerText = wrongmsg;*/
		return false;
	}
	else
	{
		document.getElementById(validador).className = 'noValidator'; 
		/*document.getElementById(validador).innerText = rightmsg;*/
		return true;
	}
}


/* USADO EN EL ONBLUR DEL CONTROL PARA VALIDAR EMAIL*/
function emailValidator(control,validador,valempty)
{
	if(valempty && document.getElementById(control).value == '')
	{
		document.getElementById(validador).className = 'wrongValidator';
		document.getElementById(validador).innerText = required_validator_message;
		return false;
	}
	else if(document.getElementById(control).value != '')
	{
		var emailID = document.getElementById(control);
		
		if (emailCheck(emailID.value)==false)
		{
			document.getElementById(validador).className = 'wrongValidator'; 
			document.getElementById(validador).innerText = invalid_validator_message;
			return false;
			
		}
		else
		{
			document.getElementById(validador).className = 'rightValidator'; 
			document.getElementById(validador).innerText = '';
			return true;
		}
	}
}

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) 
	{
		/* Too many/few @'s or something; basically, this address doesn't
	even fit the general mould of a valid e-mail address. */
	
	/*alert("Email address seems incorrect (check @ and .'s)");*/
	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("Ths username contains invalid characters.");*/
		return false;
	   	}
	}
	
	for (i=0; i<domain.length; i++) {
	if (domain.charCodeAt(i)>127) {
	/*alert("Ths domain name contains invalid characters.");*/
	return false;
	   }
	}

	// See if "user" is valid 
	if (user.match(userPat)==null) {
	// user is not valid
	/*alert("The username doesn't seem to be valid.");*/
	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("Destination IP address is invalid!");*/
			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("The domain name does not seem to be valid.");*/
		return false;
		   }
	}


	if (checkTLD && domArr[domArr.length-1].length!=2 && 
	domArr[domArr.length-1].search(knownDomsPat)==-1) {
	/*alert("The address must end in a well-known domain or two letter " + "country.");*/
	return false;
	}

	if (len<2) {
	/*alert("This address is missing a hostname!");*/
	return false;
	}

	// If we've gotten this far, everything's valid!
	return true;
}

/*** VALIDADORES ESPECIALES **/
/* USADO EN EL ONBLUR DEL CONTROL PARA VALIDAR QUE ALGUN RADIOBUTTON ESTE CHECKEADO*/
function radioDiscountValidator(validador)
{
	if(!document.form.discount[0].checked && !document.form.discount[1].checked )
	{
		document.getElementById(validador).className = 'wrongValidator'; /*RED*/
		document.getElementById(validador).title = required_validator_message;
		return false;
	}
	else
	{
		document.getElementById(validador).className = 'rightValidator'; 
		document.getElementById(validador).title = '* OK';
		return true;
	}
}

/* USADO EN EL ONBLUR DEL CONTROL PARA VALIDAR UN IMPORTE SEA MAYOR QUE 0*/
function amountValidator(control,validador)
{
	if(document.getElementById(control).value=="" || document.getElementById(control).value==0)
	{
		document.getElementById(validador).className = 'wrongValidator'; /*RED*/
		document.getElementById(validador).title = '* El Importe total debe ser mayor que 0';
		return false;
	}
	else
	{
		document.getElementById(validador).className = 'rightValidator'; 
		document.getElementById(validador).title = '* OK';
		return true;
	}
}
