/*
gpVisible
checkDate
checkShortDate
checkPhone
checkZip
checkEmail
confirmEmail
checkSize
visibility
checkRadio
notFirstIndex
checkNumeric
*/

//helper function. since we ask, in nearly every case, if the grandparent object is visible (display == "") this is an easier way of doing it
function gpVisible(htmlObj){
	return htmlObj.parentNode.parentNode.style.display == "";
}

//The date must appear delimited by '/' or '-' and in M[M] D[D] YY[YY] format
function checkDate(htmlObj){
	if( gpVisible(htmlObj) )
		return htmlObj.value.match(/[0-9]{1,2}[\/-]{1}[0-9]{1,2}[\/-]{1}[0-9]{2,4}/);
	return true;
}

//The date must appear delimited by '/' or '-' and in M[M] YY[YY] format
function checkShortDate(htmlObj){
	if( gpVisible(htmlObj) )
		return htmlObj.value.match(/[0-9]{1,2}[\/-]{1}[0-9]{2,4}/);
	return true;
}

//Make sure the phone number is 10 or 11 digits long
function checkPhone(htmlObj){
	if( gpVisible(htmlObj) ){
		var temp = htmlObj.value.replace(/[-\(\)\s]/g,"");
		return temp.match(/[0-9]{10,11}/);
	}
	return true;
}

//Zip codes should be five or nine numbers long
function checkZip(htmlObj){
	if( gpVisible(htmlObj) ){
		//added this check 10/12/06 because Canadians don't use the same zip code type that the US does, ergo, if they are not applying from the US, don't force them to put in a US zip code
		if(document.getElementById('country') && document.getElementById('country').value == "USA"){
			var temp = htmlObj.value.replace(/[\s-]/g,"");
			return temp.length == 5 || temp.length == 9;
		}
		else return true;
	}
	else return true;
}

//Email must contain '@' and a '.' with text in between
function checkEmail(htmlObj){
	if( gpVisible(htmlObj) )
		return htmlObj.value.match(/[\w\.%-]+@[\w\.%-]+\.[a-zA-Z\.]{2,4}/);
	return true;
}

//Must match the email field and be more than 0 characters (if they're both 0, this should still throw an error)
function confirmEmail(htmlObj,htmlObj2){
	if( gpVisible(htmlObj) )
		return htmlObj.value.length > 5 && htmlObj.value == htmlObj2.value;
	return true;
}

//Just evaluates the length of the string to make sure text is present
function checkSize(htmlObj,minlen){
	if( gpVisible(htmlObj) )
		return htmlObj.value.length >= minlen;
	return true;
}

//Modifies the visibility of a tag. The status must be either '' or 'none'
function visibility(htmlObj,status){
	if(document.getElementById(htmlObj))
		document.getElementById(htmlObj).style.display = status;
}

//Checks to make sure that one of the radio buttons in a particular group has been selected
function checkRadio(htmlObj){
	if( gpVisible(htmlObj) ){
		var kids = htmlObj.childNodes;	//Creates an array of the nodes one level deeper
		var flag = false;	//Has a checked radio button been found yet?
		for(var i = 0; i < kids.length; i++){
			if(kids[i].nodeName == "LABEL"){	//All input elements are inside a label tag
				for(var j = 0; j < kids[i].childNodes.length; j++){
					if (kids[i].childNodes[j].nodeName == "INPUT" && kids[i].childNodes[j].checked){
						flag = true;
						break;
					}
				}
			}
		}
		return flag;
	}
	else return true;
}

//Confirms whether someone really wants to select an entry term other than Fall
/*
function confirmTerm(htmlObj) {
	//first, make sure a radio button was selected for a term
	checkRadio(htmlObj);
	//then if a term is selected, throw a confirm box if the term isn't Fall
	//.....ummm, this is where someone (Mike) needs to actually understand what is going on with validation for inforeq.....
}
*/


/*
function validateVisible(htmlObj){
	var grandparent = htmlObj.parentNode.parentNode;
	var see = grandparent.style.display;
	alert("Node name: " + grandparent.nodeName + "\nNode value: " + grandparent.nodeType);
	alert("The visibility for " + htmlObj.id + " is " + see);
}
*/

function notFirstIndex(htmlObj){
	if( gpVisible(htmlObj) )
		return htmlObj.selectedIndex != 0;
	return true;
}

function checkNumeric(htmlObj){
	if( gpVisible(htmlObj) )
		return htmlObj.value.replace(/[^0-9\.]/g,'').length > 0;
	return true;
}

function notify(){
	alert("Note: Please be sure you intended to select an entry term other than Fall Term. For instance, if you graduate in May or June, you are most likely interested in starting college in the fall.");
}