//----------------------------------------------------
// Javascript library

// validate text control
function isEmpty(control){
	if (trimSpaces(control.value) == "" ){
		control.focus();
		control.select();
		return true;
	}
	return false;
}

// remove leading and tailing a string
function trimSpaces(inputStr){
	var len=inputStr.length;
	var i=0;
	while(i<len && inputStr.charAt(i)==" ") i++;
	var j=len-1;
	while(j>=0 && inputStr.charAt(j)==" ") j--;
	if (i<=j) // not empty
		return inputStr.substring(i,j+1);
	else
		return "";
}

// validate the email
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 (emailStr=="") {
	 	alert("Please provide a contact Email Address")
		return false;
	}

	if (matchArray==null) {
	 	alert("Your Email Address seems incorrect.\nPlease check the formatting (@ and .)")
		return false;
	}
	var user=matchArray[1]
	if (user.match(userPat)==null) {
	    alert("The email username doesn't seem to be valid.")
	    return false;
	}

	return true;
}

// check the dropdown selection
function checkDropDown(control,input){
	if (control[control.selectedIndex].value==input){
		control.focus();
		return false;
	}
	return true;
}

// check the radio selection
function checkRadioButton(control){
	if (!(control[0].checked || control[1].checked)){
		control[0].focus();
		return false;
	}
	return true;
}

// open help window
function openHelp(vLink, vHeight, vWidth, vScrollbar)
{
	var sLink = (typeof(vLink.href) == 'undefined') ? vLink : vLink.href;

	if (sLink == '')
	{
		return false;
	}

	winDef = 'status=no,resizable=yes,scrollbars=no,toolbar=no,location=no,fullscreen=no,titlebar=yes,height='.concat(vHeight).concat(',').concat('width=').concat(vWidth).concat('scrollbars=').concat(vScrollbar).concat(',');
	winDef = winDef.concat('top=').concat((screen.height - vHeight)/2).concat(',');
	winDef = winDef.concat('left=').concat((screen.width - vWidth)/2);
	newwin = open('', '_blank', winDef);
	newwin.location.href = sLink;

	if (typeof(vLink.href) != 'undefined')
	{
		return false;
	}
}


