// JavaScript Document
var wrong_validator_message = 'Some required information is missing or incomplete. Please correct your entries and try again.';

/* Put backgound text on input controls */
function defaultInputText(text,input)
{
	if(document.getElementById(input).value == '')
		document.getElementById(input).value = text;
}

function resetInputText(text,input)
{
	if(document.getElementById(input).value == text)
		document.getElementById(input).value = '';
}


function noValidator(validador)
{
	document.getElementById(validador).className = 'noValidator'; 
}

function onfocusValidator(control)
{
	document.getElementById(control).focus();
	//document.getElementById(control).className = 'onfocusValidator'; 
}

function setValidator(lbl,validador)
{
	if(lbl!='')
	{
		document.getElementById(validador).className = 'wrongValidator'; 
		document.getElementById(validador).innerText = lbl;
		return false;
	}
	else
	{
		document.getElementById(validador).className = 'rightValidator';
		document.getElementById(validador).innerText = '';
		return true;
	}
}


function dateValidator(control,validador)
{
	var date = document.getElementById(control).value;
	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])
	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 Date';
		return false;
	}
	else
	{
		document.getElementById(validador).className = 'rightValidator';
		document.getElementById(validador).innerText = '';
		return true;
	}
}

function usernameValidator(control,validador,valempty)
{
	var JSValor = document.getElementById(control).value;
	JSRegExp =/^[a-z\d]{1,100}$/i; 	
	
	if(valempty && (JSValor == ''))
	{
		document.getElementById(validador).className = 'wrongValidator';
		document.getElementById(validador).innerText = 'Required';
		return false;
	}
	if(JSRegExp.test(JSValor)) 
	{
		document.getElementById(validador).className = 'rightValidator'; 
		document.getElementById(validador).innerText = '';
		return true;
	}
	else
	{
		document.getElementById(validador).className = 'wrongValidator'; 
		document.getElementById(validador).innerText = 'String can not contain symbols and spaces';
		return false;
	} 
}


function birthdayValidator(ctlMonth,ctlDay,ctlYear,minAge,validador)
{
	if((document.getElementById(ctlMonth).value == '' || document.getElementById(ctlMonth).value == 0) && (document.getElementById(ctlDay).value == '' || document.getElementById(ctlDay).value == 0) && (document.getElementById(ctlYear).value == '' || document.getElementById(ctlYear).value == 0))
	{
		document.getElementById(validador).className = 'wrongValidator'; 
		document.getElementById(validador).innerText = 'Birthday is Required';
		return false;	
	}
	else if(document.getElementById(ctlMonth).value == '' || document.getElementById(ctlMonth).value == 0)
	{
		document.getElementById(validador).className = 'wrongValidator'; 
		document.getElementById(validador).innerText = 'Month is Required';
		return false;	
	}
	else if(document.getElementById(ctlDay).value == '' || document.getElementById(ctlDay).value == 0)
	{
		document.getElementById(validador).className = 'wrongValidator'; 
		document.getElementById(validador).innerText = 'Day is Required';
		return false;	
	}
	else if(document.getElementById(ctlYear).value == '' || document.getElementById(ctlYear).value == 0)
	{	
		document.getElementById(validador).className = 'wrongValidator'; 
		document.getElementById(validador).innerText = 'Year is Required';
		return false;	
	}
	else if(minAge > 0 && minAge != '')
	{
		var birth_date = new Date(document.getElementById(ctlMonth).value+'/'+document.getElementById(ctlDay).value+'/'+document.getElementById(ctlYear).value);
		var today = new Date();
		var years_old = parseInt((today -birth_date)/365/24/60/60/1000);
		
		if(years_old<minAge)
		{
			document.getElementById(validador).className = 'wrongValidator'; 
			document.getElementById(validador).innerText = 'Must be older than '+minAge+' years old';
			return false;
		}
	}
	else
	{
		document.getElementById(validador).className = 'rightValidator';
		document.getElementById(validador).innerText = '';
		return true;
	}
}

/* HTHML editor */
function editorValidator(control,validador)
{
	if(control == '')
	{
		document.getElementById(validador).className = 'wrongValidator'; 
		document.getElementById(validador).innerText = 'Required';
		return false;
	}
	else
	{
		document.getElementById(validador).className = 'rightValidator';
		document.getElementById(validador).innerText = '';
		return true;
	}
}

/* EMPTY field */
function emptyValidator(control,validador)
{
	if(document.getElementById(control).value == '')
	{
		document.getElementById(validador).className = 'wrongValidator'; 
		document.getElementById(validador).innerText = 'Required';
		return false;
	}
	else
	{
		document.getElementById(validador).className = 'rightValidator';
		document.getElementById(validador).innerText = '';
		return true;
	}
}

/* SELECT */
function selectValidator(control,validador)
{
	
	if(document.getElementById(control).selectedIndex <= 0)
	{
		document.getElementById(validador).className = 'wrongValidator';
		document.getElementById(validador).innerText = 'Required';
		return false;
	}
	else
	{
		document.getElementById(validador).className = 'rightValidator';
		document.getElementById(validador).innerText = '';
		return true;
	}
}

/* PASSWORD */
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 = 'Mismatch';
			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';
		return false;
	}
}

/* NUMBER */
function numberValidator(control,validador,valempty)
{
	if(valempty && (document.getElementById(control).value == '' || document.getElementById(control).value == 0))
	{
		document.getElementById(validador).className = 'wrongValidator';
		document.getElementById(validador).innerText = 'Required';
		return false;
	}
	else if (isNaN(document.getElementById(control).value)) 
	{ 
		document.getElementById(validador).className = 'wrongValidator'; 
		document.getElementById(validador).innerText = 'Numeric.';
		return false;
	}
	else
	{
		document.getElementById(validador).className = 'rightValidator'; 
		document.getElementById(validador).innerText = '';
		return true;
	}
}

/* PHONE */
function phoneValidator(control,validador,valempty)
{
	var num = document.getElementById(control).value;
	
	if(valempty && (document.getElementById(control).value == '' || document.getElementById(control).value == 0))
	{
		document.getElementById(validador).className = 'wrongValidator';
		document.getElementById(validador).innerText = 'Required';
		return false;
	}
	else if (isNaN(document.getElementById(control).value)) 
	{ 
		document.getElementById(validador).className = 'wrongValidator'; 
		document.getElementById(validador).innerText = 'Numeric';
		return false;
	}
	else if(num.length!=10)
	{ 
		document.getElementById(validador).className = 'wrongValidator'; 
		document.getElementById(validador).innerText = 'Must have 10 digits';
		return false;
	}
	else
	{
		document.getElementById(validador).className = 'rightValidator'; 
		document.getElementById(validador).innerText = '';
		return true;
	}
}

/* ZIP */
function zipValidator(control,validador,valempty)
{
	var num = document.getElementById(control).value;
	
	if(valempty && (document.getElementById(control).value == '' || document.getElementById(control).value == 0))
	{
		document.getElementById(validador).className = 'wrongValidator';
		document.getElementById(validador).innerText = 'Required';
		return false;
	}
	else if (isNaN(document.getElementById(control).value)) 
	{ 
		document.getElementById(validador).className = 'wrongValidator'; 
		document.getElementById(validador).innerText = 'Numeric';
		return false;
	}
	else if(num.length!=5)
	{ 
		document.getElementById(validador).className = 'wrongValidator'; 
		document.getElementById(validador).innerText = 'Must have 5 digits';
		return false;
	}
	else
	{
		document.getElementById(validador).className = 'rightValidator'; 
		document.getElementById(validador).innerText = '';
		return true;
	}
}

/* EMAIL */
function emailValidator(control,validador,valempty)
{
	if(valempty && document.getElementById(control).value == '')
	{
		document.getElementById(validador).className = 'wrongValidator';
		document.getElementById(validador).innerText = 'Required';
		return false;
	}
	else
	{
		var emailID = document.getElementById(control);
		
		if (emailCheck(emailID.value)==false)
		{
			document.getElementById(validador).className = 'wrongValidator'; 
			document.getElementById(validador).innerText = 'Invalid';
			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;
}

