//check empty
function empty(aTextField){
	return ((aTextField.value.length==0) || (aTextField.value==null)) ? true : false;
}

//change nav size
function adjustNavHeight(num){
	document.getElementById("bgc-navigation").style.height=num+"px";
}

//check email address
function emailValidate (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) {
		//alert("Email address seems incorrect (check @ and .'s)");
		return false;
	}

	var user=matchArray[1];
	var domain=matchArray[2];

	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;
			}
	}

	if (user.match(userPat)==null) {
		//alert("The username doesn't seem to be valid.");
		return false;
	}

	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				//alert("Destination IP address is invalid!");
				return false;
				}
		}
		return true;
	}
 
	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;
	}

	return true;
}		
//end email 

//return chosen checkbox node values as an array.
//accepts checkObj - checkbox object nodelist
function getCheckedValue(checkObj){
	checkArr = new Array();
	if(!checkObj)
		return "";
	var checkLength = checkObj.length;
	if(checkLength == 0){
		return "";
	}else{			
		for(var i = 0; i<checkLength; i++) {
			if(checkObj[i].checked) {
				checkArr.push(checkObj[i].value);
			}
		}
		return checkArr;
	}
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getRadioValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i<radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setRadioValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

//limit a checkbox set to num
//i.e. 5 checkboxes, set to 2, only 2 can be selected
//accepts - htmlElement: checkBox object, sent as this
//num - string
//formName - string
function limitSelection(htmlElement, num, formName){
	var elements = document[formName][htmlElement.name];
	var countChecked = new Number();
	for(i=0;i<elements.length;i++){
		if(elements[i].checked){
			countChecked++;	
		}
	}
	
	var disabler = (countChecked >= 2) ? true : false;		
	for(i=0;i<elements.length;i++){
		if(!elements[i].checked){
			elements[i].disabled =disabler;
		}
	}
}

//remove value from array
function removeItems(theArray, item) {
	for(i=0;i<theArray.length;i++){
		if(theArray[i]==item){
			theArray.splice(i,1);	
		}	
	}	
	return theArray;
}