/*###############################################################################################
#	Start validateFields
#	abstract: The functions listed within this block make up a set of form validation routines
###############################################################################################*/

//Validate form fields

var NOT_EMPTY	= "NOT_EMPTY";
var NUMERIC	= "NUMERIC";
var EMAIL	= "EMAIL";
var DAY		= "DAY";
var MONTH	= "MONTH";
var YEAR	= "YEAR";
var SELBOX	= "SELBOX";
var RADIO	= "RADIO";
var LENGTH	= "LENGTH";

//Variable used to hold the last value of the iterator j in validateFields() before calling itself.
var jj = 0; 

/************************************************************************************************
*	function: validateFields()
*	abstract: Iterates through array of form objects and calls the appropriate validation
*		  routine.
*	parameter(s): myArray(array) - The array of form objects and corresponding alert messages.
************************************************************************************************/

function validateFields(myArray)
{
	for (j=0; j<myArray.length; j++)
	{
		switch(myArray[j][1]) 
		{	
			case "NOT_EMPTY" :	if (chkEmptyString(myArray[j][0]))	break; else { showAlert(myArray[j][2]); return false; }
			case "NUMERIC" :	if (chkNumeric(myArray[j][0]))		break; else { showAlert(myArray[j][2]); return false; }
			case "EMAIL" :		if (chkEmail(myArray[j][0]))		break; else { showAlert(myArray[j][2]); return false; }
			case "SELBOX" :		if (chkSelect(myArray[j][0]))		break; else { showAlert(myArray[j][2]); return false; }
			case "DAY" :		if (chkDay(myArray[j][0]))		break; else { showAlert(myArray[j][2]); return false; }
			case "MONTH" :		if (chkMonth(myArray[j][0]))		break; else { showAlert(myArray[j][2]); return false; }
			case "YEAR" :		if (chkYear(myArray[j][0]))		break; else { showAlert(myArray[j][2]); return false; }
		    	case "DATE" :		if (chkValidDate())			break; else { showAlert(myArray[j][2]); return false; }
			case "RADIO" :		if (chkRadio(myArray[j][0]))		break; else { showAlert(myArray[j][2]); return false; }
			case "LENGTH" :		if (chkLength(myArray[j][0]))		break; else { showAlert(myArray[j][2]); return false; }
			default :		break;
		} // end switch
	} // end for 

	j = jj;
	return true;
}

/************************************************************************************************
*	function: showAlert()
*	abstract: Shows the user an error message if they have not
*		  entered the correct information into the corresponding field.
*	parameter(s): sLabel(string). The alert message to show
************************************************************************************************/

function showAlert(sLabel)
{
	alert(sLabel);
}

/************************************************************************************************
*	function: chkEmptyString()
*	abstract: Checks to see if empty strings exist in a required field.
*	parameter(s): elementOffset(string). The name of the form field being checked.
************************************************************************************************/

function chkEmptyString(elementOffset)
{
	if (document.forms[0].elements[elementOffset].value=="")
	{
		document.forms[0].elements[elementOffset].focus()
		return false;
	}
	return true;
}

/***********************************************************************************************
*	function: chkSelect()
*	abstract: Checks to see if a value was selected in a select box.
*	parameter(s): elementOffset(string). The name of the form field being checked.
************************************************************************************************/

function chkSelect(elementOffset) 
{

	if (getSelectValue(document.forms[0].elements[elementOffset]) == "") 
	{
		document.forms[0].elements[elementOffset].focus();
		return false;
	}
	return true;
}

//Function is used by chkSelect().  This is done for Netscape browsers.
function getSelectValue(cmb)
{
	return (cmb.options[cmb.selectedIndex].value)
}	

/***********************************************************************************************
*	function: chkRadio()
*	abstract: Checks to see if a radio option was selected
*	parameter(s): elementOffset(string). The name of the form field being checked.
************************************************************************************************/

function chkRadio(elementOffset)
{
	var isChecked=0;

	for (i=0; i<document.forms[0].elements[elementOffset].length; i++)
	{
		(document.forms[0].elements[elementOffset][i].checked) ? isChecked++ : isChecked+=0;
	}

	if (isChecked<1)
		return false;
	else
		return true;
}

/************************************************************************************************
*	function: chkNumeric()
*	abstract: Checks to see if empty strings exist and if user inputed a numeric 
*	          value in a required field.
*	parameter(s): elementOffset(string). The name of the form field being checked.
************************************************************************************************/

function chkNumeric(elementOffset)
{
	if (isNaN(document.forms[0].elements[elementOffset].value) || document.forms[0].elements[elementOffset].value=="")
	{
		document.forms[0].elements[elementOffset].focus();
		return false;
	}
	return true;
}

/************************************************************************************************
*	function: chkEmail()
*	abstract: Validates an email address entered by searching for a '.' and an '@'.
*	parameter(s): elementOffset(string). The name of the form field being checked.
************************************************************************************************/

function chkEmail(elementOffset)
{
	var str = document.forms[0].elements[elementOffset].value;
	var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
	var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
	if (!reg1.test(str) && reg2.test(str))
		return true;
	else
		return false;
}

/************************************************************************************************
*	function: chkDay()
*	abstract: Validates day.
*	parameter(s): elementOffset(string). The name of the form field being checked.	
************************************************************************************************/

function chkDay(elementOffset) 
{
	if(document.forms[0].elements[elementOffset].value != "")
	{
		if (document.forms[0].elements[elementOffset].value > 31 || document.forms[0].elements[elementOffset].value < 1 && document.forms[0].elements[elementOffset].value != "dd")
		{
			document.forms[0].elements[elementOffset].focus();
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		return true;
	}	
}

/************************************************************************************************
*	function: chkMonth()
*	abstract: Validates month.
*	parameter(s): elementOffset(string). The name of the form field being checked.
************************************************************************************************/

function chkMonth(elementOffset) 
{
	if(document.forms[0].elements[elementOffset].value != "")
	{
		if (!chkNumeric(elementOffset) || 
			document.forms[0].elements[elementOffset].value > 12 || 
			document.forms[0].elements[elementOffset].value < 1)
		{
			document.forms[0].elements[elementOffset].focus();
			return false;
		} 
		else 
		{
			return true;
		}
	}
	else
	{
		return true;
	}
}

/************************************************************************************************
*	function: chkYear()
*	abstract: Validates year.
*	parameter(s): elementOffset(string). The name of the form field being checked.	
************************************************************************************************/

function chkYear(elementOffset)
{
	if(document.forms[0].elements[elementOffset].value != "")
	{
		if (!chkNumeric(elementOffset))
		{
			document.forms[0].elements[elementOffset].focus();
			return false;
		} 
		else 
		{	
			if (document.forms[0].elements[elementOffset].value>1900)
			return true;
			else
			return false;
		}
	}
	else
	{
		return true;
	}	
}

/************************************************************************************************
*	function: chkLength()
*	abstract: Checks that a text area does not exceed a set amount of characters
*	parameter(s): elementOffset(string). The name of the form field being checked.	
************************************************************************************************/

function chkLength(elementOffset)
{
	var fieldVal = document.forms[0].elements[elementOffset].value
	if(fieldVal.length > TEXT_LENGTH)
		return false;
	else
		return true;
}

/*###############################################################################################
#	End validateFields
###############################################################################################*/


/*###############################################################################################
#	Start GetDateTime
#	abstract: n/a
###############################################################################################*/


var theDate;
var month;
var period;
var day;
var LAYER_NAME = "layerDate"

/************************************************************************************************
*	function: theDate()
*	abstract: Builds a date/time string to be written out to a layer
*	parameter(s): n/a
************************************************************************************************/

function theDate()
{

	var currentDate=new Date()

	var hour=currentDate.getHours()
	var min=currentDate.getMinutes()
	var sec=currentDate.getSeconds()
	var date=currentDate.getDate()

	switch(currentDate.getMonth())
	{
        	case 0:month="January";break;
        	case 1:month="February";break;
	        case 2:month="March";break;
        	case 3:month="April";break;
	        case 4:month="May";break;
	        case 5:month="June";break;
	        case 6:month="July";break;
        	case 7:month="August";break;
	        case 8:month="September";break;
        	case 9:month="October";break;
	        case 10:month="November";break;
        	case 11:month="December";break;
	}

	switch(currentDate.getDay())
	{
        	case 0:day="Sunday";break;
	        case 1:day="Monday";break;
        	case 2:day="Tuesday";break;
	        case 3:day="Wednesday";break;
	        case 4:day="Thursday";break;
	        case 5:day="Friday";break;
	        case 6:day="Saturday";break;
	}
	
	if(sec<10){sec="0"+sec}
	if(min<10){min="0"+min}

	if(hour>12){hour-=12;period="pm"} else {period="am"}
	if(currentDate.getHours()==12){period="pm"}
	if(currentDate.getHours()==24){period="am"}

	var theDate = ""+day+", "+month+" "+date+", "+hour+":"+min+":"+sec+ period
	
	writeLayerContent(theDate, LAYER_NAME)

	//window.setTimeout("theDate()",300)
}

/*###############################################################################################
#	End GetDateTime
###############################################################################################*/

/*###############################################################################################
#	Start WriteLayerContent
#	abstract: n/a
###############################################################################################*/

/************************************************************************************************
*	function: writeLayerContent()
*	abstract: Outputs text to the specified layer
*	parameter(s): theText(string) - text to be displayed in layer.
*		      layerName(string) - the name of the layer to which the content will be 
*					  written.
************************************************************************************************/

function writeLayerContent(theText,layerName)
{
	if(document.all)
		document.all[layerName].innerHTML = theText;
	else if(document.layers)
	{
		document.layers[layerName].document.open
		document.layers[layerName].document.write(theText);
		document.layers[layerName].document.close();
	}		
}

/*###############################################################################################
#	End WriteLayerContent
###############################################################################################*/

/*###############################################################################################
#	Start Rollovers
#	abstract: The group of functions used to swap images on and off
###############################################################################################*/


/************************************************************************************************
*	function: ImageObject()
*	abstract: Creates an Image Object and properties.
*	parameter(s): name(string) - the name of the image you wish to modify
************************************************************************************************/

function ImageObject(name)
{
	this.off = new Image;
	this.off.src = name + "_UP.jpg";
	this.on = new Image;
	this.on.src = name + "_RL.jpg";
}

/************************************************************************************************
*	function: rollover()
*	abstract: Swaps the specified image to its 'on' state
*	parameter(s): imageName(string) - the name of the image you wish to switch
************************************************************************************************/

function rollover(imageName)
{
	if (enabled) 
	{ 
		document[imageName].src = ImageObjects[imageName].on.src; 
	}
}


/************************************************************************************************
*	function: rollout()
*	abstract: Swaps the specified image to its 'off' state
*	parameter(s): imageName(string) - the name of the image you wish to switch
************************************************************************************************/

function rollout(imageName)
{
	if (enabled) 
	{ 
		document[imageName].src = ImageObjects[imageName].off.src; 
	}
}

/*###############################################################################################
#	End Rollovers
###############################################################################################*/


/************************************************************************************************
*	function: popWin(theURL, winName, width, height, otherfeatures)
*	abstract: opens a popup window
*	parameter(s): theURL(string) - the url you wish to open.
*		      winName(string) - the name you wish to assign to the window
*		      width(string) - the desired width of the popup window
*		      height(string) - the desired height of the popup window
*		      otherfeatures(string) - the list of window features
************************************************************************************************/

function popWin(theURL, winName, width, height, otherfeatures)
{
	if (document.all || document.layers)
	{
		w = screen.availWidth;
		h = screen.availHeight;
	}

	var popW = width, popH = height;
	var leftPos = (w-popW)/2, topPos = (h-popH)/2;

	if(otherfeatures=='')
		features=''
	else
		features="," + otherfeatures

	var winHandle = window.open(theURL,winName,'width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos + features);
}

function highlight(layername)
{
   if (document.all) document.all(layername).style.backgroundColor="#FA6800"; 
   else if (document.layers) document.layers[layername].bgColor='#FA6800' 
} 


function unhighlight(layername)
{
   if (document.all) document.all(layername).style.backgroundColor=""; 
   else if (document.layers) document.layers[layername].bgColor='' 
}

function deleteSeason()
{
	if(confirm("WARNING: This will erase all data associated with this Season.\nSelect Ok to Delete or Cancel to cancel."))
	{
		location.replace('deleteseason.asp?id='+seasonID);
	}
}


