function displaySubMenus(elem)
{
	var subMnus = document.getElementById(elem);
	if (subMnus.style.display == 'none')
	{
		subMnus.style.display = 'block';
	}
	else
	{
		subMnus.style.display = 'none';
	}
}

function processLogin()
{
	var pWord = document.frmLogin.pLogin.value;
	if (pWord != '')
	{
		document.frmLogin.submit();
	}
}

function processLogout()
{
	document.frmLogin.submit();
}

/***********************************************************************************************************************************************************************
Function Name: 			CheckNumberField(numtype)
Description:			Combines serveral types of field verification routines into one function.  Designed to be called in an onKeyPress option in an <input> field.
Function Parameters: 	numtype =	1 = straight numeric
									2 = straight numeric with decimal point only
									3 = straight numeric with decimal point and hyphen
									4 = straight numeric with hyphen and parenthesis
									5 = straight numeric with hyphen only
									6 = straight numeric with slash key for dates
									9 = straight numeric with colon for times
Syntax:						onKeyPress="javascript:return CheckNumberField(numtype);"
***********************************************************************************************************************************************************************/
// This routine only returns numeric information with appropriate supporting keystrokes.  Call with an onKeyPress([number type]);
function CheckNumberField(numtype)
{
	var key = window.event.keyCode;
	var checknum = numtype;
	if ((key > 47 && key < 58) || (key == 39) || (key == 37) || (key == 35) || (key == 36) || (key == 46) || (key == 8))
	{   // if key pressed is 0 thru 9, right or left arrow, delete, end, home, backspace let it through.
		return;
	}
	if ( checknum !== 1 )
	{
		if ( checknum == 2 )
		{
			if ( key == 46 )
			{   // let a '.' let it through
				return;
			}
		}
		if ( checknum == 3 )
		{
			if ( key == 46 || key == 45 )
			{   // let a '.' or a '-' let it through
				return;
			}
		}
		if ( checknum == 4 )
		{
			if ( key == 45 || key == 40 || key == 41 || key == 32)
			{   
				return;
			}
		}
		if ( checknum == 5 )
		{
			if ( key == 45 )
			{   // let a '-' through only
				return;
			}
		}
		if ( checknum == 6 )
		{
			if ( key == 47 )
			{   // let a '/' through only
				return;
			}
		}
		if ( checknum == 9 )
		{
			// lower case letters 97-'a', 112-'p' and 109-'m'
			if (key == 97 || key == 109 || key == 112)
			{
				return;
			}
			// UPPER case letters 65-'A', 80-'P' and 77-'M'
			if (key == 65 || key == 80 || key == 77)
			{
				return;
			}			
			// 58 - colon, 32 - spacebar
			if ( key == 58 || key == 32 )
			{
				return;
			}
		}
	}
	window.event.returnValue = null;
}

// Declaring valid date character, minimum year and maximum year
var dtCh = "/";
var tmCh = ":";
var curr_date = new Date();
var minYear = curr_date.getFullYear();
var maxYear = minYear + 1;

function isInteger(s)
{
	var i;
    for (i = 0; i < s.length; i++)
	{   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
	{   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year)
{
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) 
{
	for (var i = 1; i <= n; i++) 
	{
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(elem,dtStr)
{
	var regex = /^([0]?[1-9]|[1][0-2])[\/]([0]?[1-9]|[1|2][0-9]|[3][0|1])[\/]([0-9]{4})$/;
	if(dtStr!="") 
	{
		if(!regex.test(dtStr)) 
		{
			errors = "The date format should be MM/DD/YYYY";
			alert(errors);
			elem.style.backgroundColor = '#ffde01';
			elem.focus();
			return false;
		}
		else
		{
			var pos1 = dtStr.indexOf(dtCh);
			var pos2 = dtStr.indexOf(dtCh,pos1+1);
			var strMonth = dtStr.substring(0,pos1);
			var strDay = dtStr.substring(pos1+1,pos2);
			var strYear = dtStr.substring(pos2+1);
			var year = parseInt(strYear);
			if (year < minYear)
			{
				alert("Please enter a year equal to or greater than "+minYear);
				elem.style.backgroundColor = '#ffde01';
				elem.focus();
				return false;
			}
		}
	}
	else
	{
		//alert('You need to enter a date.');	
		//elem.style.backgroundColor = '#ffde01';
		//elem.focus();
		//return false;
	}
	return true;
}

function verifyTime(elem)
{
	var regex = /^([1-9]|1[0-2]|0[1-9]){1}(:[0-5][0-9] [aApP][mM]){1}$/;
	if (elem.value != '')
	{
		if(!regex.test(elem.value)) 
		{
			alert("The time format should be HH:MM AM or PM\nThe hour should be 1-12 and the minute 00-59");
			elem.focus();
			return false;
		}	
	}
}

function isTime(elem,tmStr,ampm,elemconv) 
{
	//var regex = /^([1-9]|1[0-2]|0[1-9]){1}(:[0-5][0-9] [aApP][mM]){1}$/;
	var regex = /^([1-9]|1[0-2]|0[1-9]){1}(:[0-5][0-9]){1}$/;
	if(tmStr!="") 
	{
		if(!regex.test(tmStr)) 
		{
			alert("The time format should be HH:MM\nThe hour should be 1-12 and the minute 00-59");
			elem.style.backgroundColor = '#ffde01';
			elem.focus();
			return false;
		}
		else
		{
			var pos1 = tmStr.indexOf(tmCh);
			var strHour = tmStr.substring(0,pos1);
			var strMinute = tmStr.substring(pos1+1,tmStr.length);
			var hour = parseInt(strHour);
			if (ampm == 'PM' && hour != 12)
			{
				hour = hour + 12;
				elemconv.value = hour+':'+strMinute;
			}
			else
			{
				elemconv.value = elem.value;
			}
		}
	}
	else
	{
		alert('You need to enter a time.');
		elem.style.backgroundColor = '#ffde01';
		elem.focus();
		return false;
	}
	return true;
}

function convertText(cellvalue,options,rowObject)
{
	return convertSQLtoRead(cellvalue);
}

function convertSQLtoRead(stringSQL)
{
	if (typeof stringSQL === "undefined")
	{
	}
	else
	{
		stringSQL = stringSQL.replace(/\[a\]/g, "\&");
		stringSQL = stringSQL.replace(/\[c\]/g, "\,");	
		stringSQL = stringSQL.replace(/\[eq\]/g, "\=");
		stringSQL = stringSQL.replace(/\[sc\]/g, "\;");
		stringSQL = stringSQL.replace(/\[sp\]/g, " ");
		stringSQL = stringSQL.replace(/\[lb\]/g, "\#");
		stringSQL = stringSQL.replace(/\[as\]/g, "\*");
		stringSQL = stringSQL.replace(/\[pc\]/g, "\%");
	
		//ASCII Convertion 
		stringSQL = stringSQL.replace(/\&\#39\;/g, "\'");
		stringSQL = stringSQL.replace(/\&\#34\;/g, "\"");
		stringSQL = stringSQL.replace(/\&quot\;/g, "\"");	
		stringSQL = stringSQL.replace(/\&\#60\;/g, "\<");
		stringSQL = stringSQL.replace(/\&lt\;/g, "\<");		
		stringSQL = stringSQL.replace(/\&\#62\;/g, "\>");
		stringSQL = stringSQL.replace(/\&gt\;/g, "\>");		
	
		stringSQL = stringSQL.replace(/\[sq\]/g, "\'");
		stringSQL = stringSQL.replace(/\[dq\]/g, "\"");
		stringSQL = stringSQL.replace(/\[lt\]/g, "\<");
		stringSQL = stringSQL.replace(/\[gt\]/g, "\>");			
		return stringSQL;
	}
}

function convertSQLtoWrite(stringSQL)
{	
	//ASCII Convertion 
	stringSQL = stringSQL.replace(/\&\#39\;/g, "[sq]");
	stringSQL = stringSQL.replace(/\&\#34\;/g, "[dq]");
	stringSQL = stringSQL.replace(/\&quot\;/g, "[dq]");	
	stringSQL = stringSQL.replace(/\&\#60\;/g, "[lt]");
	stringSQL = stringSQL.replace(/\&lt\;/g, "[lt]");		
	stringSQL = stringSQL.replace(/\&\#62\;/g, "[gt]");
	stringSQL = stringSQL.replace(/\&gt\;/g, "[gt]");		

	stringSQL = stringSQL.replace(/\&/g, "[a]");
	stringSQL = stringSQL.replace(/\,/g, "[c]");
	stringSQL = stringSQL.replace(/\'/g, "[sq]");
	stringSQL = stringSQL.replace(/\"/g, "[dq]");
	stringSQL = stringSQL.replace(/\=/g, "[eq]");
	stringSQL = stringSQL.replace(/\;/g, "[sc]");
	stringSQL = stringSQL.replace(/ /g, "[sp]");
	stringSQL = stringSQL.replace(/\</g, "[lt]");
	stringSQL = stringSQL.replace(/\>/g, "[gt]");	
	stringSQL = stringSQL.replace(/\#/g, "[lb]");
	stringSQL = stringSQL.replace(/\*/g, "[as]");
	stringSQL = stringSQL.replace(/\%/g, "[pc]");
	return stringSQL;
}

function setAnchorLinks()
{
	//on the admin pages anchor tags are created by inputting "anchor=X" where X is the name of the anchor
	//we then loop through all links that have "anchor=" in the href and set its name attribute to its href after removing the anchor=, which leaves us with X
	//we also remove the href attribute so link ends up looking like <a name="X"></a>
	$('pre a[href^="anchor="]').each(function(){
		$(this).attr('name',$(this).attr('href').replace('anchor=',''));
		$(this).removeAttr('href');
	});
}

function setLinkTarget()
{
	//the below statement will force any link within a pre tag to open in a new window if the href is pointing to http://
	//hopefully this will open internal links within the same window and external links in a new window
	$('pre a[href^="http://"],pre a[href^="https://"]').attr('target','_blank');
	$('div a[href^="http://"],div a[href^="https://"]').attr('target','_blank');
	$('blockquote a[href^="http://"],blockquote a[href^="https://"]').attr('target','_blank');
	
	//the below statement will change the color of links created by the jwysiwyg from it's default black to the red used throughout the site
	$('pre p a,pre li a,pre div a,pre blockquote a').css('color','#C00720');
}

function setFontFamily()
{
	$('pre p,pre blockquote,pre div').css('fontFamily','Verdana, Arial, Helvetica, sans-serif');
	$('pre p,pre blockquote,pre div').css('fontSize','11px');
}

function invalid()
{
	var invLog = document.getElementById('invalidLogin');
	if (invLog)
	{
		invLog.style.visibility = 'visible';	
	}
}

function maskPassword(cellvalue,options,rowObject)
{
	var myFormat = '';
	for (var i = 0, len = cellvalue.length; i < len; i++)
	{
		myFormat += cellvalue[i].replace(cellvalue[i],'*');
	}
	return myFormat;
}

function rowHighlighter()
{
	$('table tr').mouseover(function(){
		$(this).css('background-color','#E5F3FF');
	});
	$('table tr').mouseout(function(){
		$(this).css('background-color','#FFFFFF');
	});
}

//BELOW FUNCTIONS ARE USED FOR MULTIPLE EVENT SUBMISSIONS ON THE PRACTICE,TOURNAMENT and TRAINING ADMIN PAGES
function populateTeamSelection(team)
{
	var selTeam = document.getElementById('selTeam');
	if (selTeam)
	{
		var teamArr = team.split(';');
		selTeam.options.length = 0;
		$(teamArr).each(function()
		{
			var teamArr2 = this.split(':');
			selTeam.options[selTeam.options.length] = new Option(teamArr2[1],teamArr2[0]);	
		});
		selTeam.options[0].selected = true;
	}
}

function populateLocationSelection(location)
{
	var selLocation = document.getElementById('selLocation');
	var locationArr = location.split(';');
	selLocation.options.length = 0;
	$(locationArr).each(function()
	{
		var locationArr2 = this.split(':');
		selLocation.options[selLocation.options.length] = new Option(convertSQLtoRead(locationArr2[1]),locationArr2[0]);	
	});
	selLocation.options[0].selected = true;
}

function verifyValues(pg)
{
	//var proceed = true;
	//var dt = isDate($('#txtEventDate'),$('#txtEventDate').val());
	
	if (pg == 'training')
	{
		var desc = $('#txtDescription').val();
		if (desc == '')
		{
			alert('You need to enter a description for this event.');
			return false;	
		}
	}
	$('#selEventDate option').each(function(){
		$(this).attr('selected','selected');
	});
	var dt = $('#selEventDate').val();
	if (dt == '' || dt == null)
	{
		alert('You need to select a date.');
		return false;	
	}
	var tm = $('#selTeam').val();
	if (tm == '' && pg != 'training')
	{
		alert('You need to select a team.');
		return false;
	}	
	var loc = $('#selLocation').val();
	if (loc == '')
	{
		alert('You need to select a location.');
		return false;
	}	
	var dur = $('#txtDuration').val();
	if (dur == '')
	{
		alert('You need to input a duration.');
		return false;
	}
	//if (proceed)
	//{
		submitMultiValues(pg);
	//}
}

function submitMultiValues(pg)
{
	var myRequestType = '';
	var myRequestType2 = '';
	var myTblGrid = '';
	
	if (pg == 'practice')
	{
		myRequestType = 'saveMultiPracticeSchedule';
		myRequestType2 = 'getPracticeSchedule';
		myTblGrid = '#tblPractices';
	}
	else if (pg == 'tournament')
	{
		myRequestType = 'saveMultiPracticeSchedule';
		myRequestType2 = 'getTournamentSchedule';
		myTblGrid = '#tblTournaments';
	}
	else if (pg == 'training')
	{
		myRequestType = 'saveMultiTrainingSchedule';
		myRequestType2 = 'getTrainingSchedule';
		myTblGrid = '#tblTraining';
	}
	
	var myValues = '';
	var myCnt = 0;
	$('.multi').each(function(){
		if (myCnt == 0)
		{
			myValues += $(this).val();
		}
		else
		{
			myValues += ';'+$(this).val();	
		}
		myCnt++;
	});
	$.ajax({
		type: "GET",
		url: 'Admin_Data_Helper.asp?requestType='+myRequestType+'&pValues='+myValues,
		dataType: '',
		success: function (data) 
		{
			$(myTblGrid).setGridParam({datatype:'xml',url:'Admin_Data_Helper.asp?requestType='+myRequestType2+'&txtDate1='+$('#txtDate1').val()+'&txtDate2='+$('#txtDate2').val()}).trigger("reloadGrid");
			$('#dialog').dialog('close');
		}
	});
}

function displayMultiEventBtn(xlin)
{
	if (xlin != '')
	{
		$('#btnOpener').show();
	}
}

function buildMultiForm(myTeamVar,myLocationVar,pg)
{
	var myOption = '';
	var myDisabled = '';
	if (pg == 'practice')
	{
		myOption = '<option value="1">Practice</option>';
		myDisabled = 'disabled="disabled"';
	}
	else if (pg == 'tournament')
	{
		myOption = '<option value="2">Tournament</option>';
		myDisabled = 'disabled="disabled"';
	}
	else if (pg == 'training')
	{
		myOption = '<option value="3">Training</option><option value="4">Camp</option><option value="5">Clinic</option><option value="6">Other</option>';
		myDisabled = '';
	}
	//This is used to create a popup form for multiple event entries.
	var myForm = '';
	var myVal = '';
	if (pg == 'practice' || pg == 'tournament')
	{
		if (pg == 'practice')
		{
			myVal = 'practice';	
		}

		myForm = '<table>'+
					'<tr>'+
						'<th>* Date:</th>'+
						'<td style="white-space:nowrap;">'+
							'<select name="selEventDate" id="selEventDate" multiple class="multi" size="12" style="min-width:100px; float:left;"></select>'+
							'<div id="datepicker" style="float:left; margin-left:5px;"></div>'+
							//'<input type="text" id="txtEventDate" name="txtEventDate" class="multi" onkeypress="CheckNumberField(6);" />'+
						'</td>'+
					'</tr>'+
					'<tr>'+
						'<th>Description:</th>'+
						'<td><input type="text" id="txtDescription" name="txtDescription" class="multi" value="'+myVal+'" /></td>'+
					'</tr>'+
					'<tr>'+
						'<th>Event Type:</th>'+
						'<td>'+
							'<select id="selEventType" name="selEventType" class="multi" '+myDisabled+'>'+myOption+'</select>'+
						'</td>'+
					'</tr>'+
					'<tr>'+
						'<th>* Team Name:</th>'+
						'<td><select id="selTeam" name="selTeam" class="multi" multiple></select></td>'+
					'</tr>'+
					'<tr>'+
						'<th>* Location:</th>'+
						'<td><select id="selLocation" name="selLocation" class="multi"></select></td>'+
					'</tr>'+
					'<tr>'+
						'<th>Start Time:</th>'+
						'<td><input type="text" id="txtStartTime" name="txtStartTime" class="multi" onkeypress="CheckNumberField(9);" /></td>'+
					'</tr>'+
					'<tr>'+
						'<th>End Time:</th>'+
						'<td><input type="text" id="txtEndTime" name="txtEndTime" class="multi" onkeypress="CheckNumberField(9);" /></td>'+
					'</tr>'+
					'<tr>'+
						'<th>Repetition:</th>'+
						'<td><select id="selRepetition" name="selRepetition" class="multi"><option value="0">one-time event</option><option value="1">daily</option><option value="7">weekly</option></select></td>'+
					'</tr>'+
					'<tr>'+
						'<th>Duration:</th>'+
						'<td><input type="text" id="txtDuration" name="txtDuration" class="multi" value="1" /></td>'+
					'</tr>'+
					'<tr>'+
						'<td colspan="2" align="right"><input type="button" id="btnAddMulti" name="btnAddMulti" value="submit" onclick="verifyValues(\''+pg+'\');" /></td>'+
					'</tr>'+
				'</table>';
	}
	else if (pg == 'training')
	{
		myForm = '<table>'+
					'<tr>'+
						'<th>* Date:</th>'+
						'<td style="white-space:nowrap;">'+
							'<select name="selEventDate" id="selEventDate" multiple class="multi" size="12" style="min-width:100px; float:left;"></select>'+
							'<div id="datepicker" style="float:left; margin-left:5px;"></div>'+
							//'<input type="text" id="txtEventDate" name="txtEventDate" class="multi" onkeypress="CheckNumberField(6);" />'+
						'</td>'+
					'</tr>'+
					'<tr>'+
						'<th>* Description:</th>'+
						'<td><input type="text" id="txtDescription" name="txtDescription" class="multi" /></td>'+
					'</tr>'+
					'<tr>'+
						'<th>Event Type:</th>'+
						'<td>'+
							'<select id="selEventType" name="selEventType" class="multi" '+myDisabled+'>'+myOption+'</select>'+
						'</td>'+
					'</tr>'+
					'<tr>'+
						'<th>Team Name:</th>'+
						'<td><select id="selTeam" name="selTeam" class="multi" multiple></select></td>'+
					'</tr>'+
					'<tr>'+
						'<th>* Location:</th>'+
						'<td><select id="selLocation" name="selLocation" class="multi"></select></td>'+
					'</tr>'+
					'<tr>'+
						'<th>Start Time:</th>'+
						'<td><input type="text" id="txtStartTime" name="txtStartTime" class="multi" onkeypress="CheckNumberField(9);" /></td>'+
					'</tr>'+
					'<tr>'+
						'<th>End Time:</th>'+
						'<td><input type="text" id="txtEndTime" name="txtEndTime" class="multi" onkeypress="CheckNumberField(9);" /></td>'+
					'</tr>'+
					'<tr>'+
						'<th>SignUpGenius #:</th>'+
						'<td><input type="text" id="txtSignUpGenius" name="txtSignUpGenius" class="multi" onkeypress="CheckNumberField(1);" /></td>'+
					'</tr>'+
					'<tr>'+
						'<th>Program ID:</th>'+
						'<td><input type="text" id="txtProgramID" name="txtProgramID" class="multi" onkeypress="CheckNumberField(1);" /></td>'+
					'</tr>'+
					'<tr>'+
						'<th>Club ID:</th>'+
						'<td><input type="text" id="txtClubID" name="txtClubID" class="multi" onkeypress="CheckNumberField(1);" /></td>'+
					'</tr>'+
					'<tr>'+
						'<th>News ID:</th>'+
						'<td><input type="text" id="txtNewsID" name="txtNewsID" class="multi" onkeypress="CheckNumberField(1);" /></td>'+
					'</tr>'+
					'<tr>'+
						'<th>Repetition:</th>'+
						'<td><select id="selRepetition" name="selRepetition" class="multi"><option value="0">one-time event</option><option value="1">daily</option><option value="7">weekly</option></select></td>'+
					'</tr>'+
					'<tr>'+
						'<th>Duration:</th>'+
						'<td><input type="text" id="txtDuration" name="txtDuration" class="multi" value="1" /></td>'+
					'</tr>'+
					'<tr>'+
						'<td colspan="2" align="right"><input type="button" id="btnAddMulti" name="btnAddMulti" value="submit" onclick="verifyValues(\''+pg+'\');" /></td>'+
					'</tr>'+
				'</table>';
	}

	$('#dialog').html(myForm).dialog({
		autoOpen: false,
		show: 'blind',
		hide: 'explode',
		width: 400,
		modal: true,
		close:function(event,ui){
			$('#selEventDate').find('option').remove();
			$('#txtStartTime').val('');
			$('#txtEndTime').val('');
			$('#txtDescription').val('');
		}
		//buttons:[{
			//text: 'submit2',
			//click: function(){verifyValues();}
		//}]
	});

	$('#btnOpener').click(function() {
		$('#dialog').dialog('open');
		$('#datepicker').datepicker({
			onSelect:function(dateText,inst){
				var proceed = true;
				$("#selEventDate option").each(function(){
					if(dateText == $(this).val())
					{
						proceed = false;
					}
				});
				if(proceed)
				{
					$('#selEventDate').append('<option value="'+dateText+'">'+dateText+'</option>');
				}
			}
		});
		populateTeamSelection(myTeamVar);
		populateLocationSelection(myLocationVar);
		$('#txtEventDate').datepicker();
		$('#txtStartTime').timepicker({showPeriod:true,showLeadingZero:true});
		$('#txtEndTime').timepicker({showPeriod:true,showLeadingZero:true});
		return false;
	});
}

//used to implement hover features on navigation menu
$(function() {
	$('#navmenu').supersubs({
		minWidth:12,
		maxWidth:27,
		extraWidth:1
	}).superfish();
});

//var heightArr = ["60:5'","61:5' 1\"","62:5' 2\"","63:5' 3\"","64:5' 4\"","65:5' 5\"","66:5' 6\"","67:5' 7\"","68:5' 8\"","69:5' 9\"","70:5' 10\"","71:5' 11\"","72:6'","73:6' 1\"","74:6' 2\""];
var heightVar = "60:5';61:5' 1\";62:5' 2\";63:5' 3\";64:5' 4\";65:5' 5\";66:5' 6\";67:5' 7\";68:5' 8\";69:5' 9\";70:5' 10\";71:5' 11\";72:6';73:6' 1\";74:6' 2\"";
var verticalVar = "24:2';25:2' 1\";26:2' 2\";27:2' 3\";28:2' 4\";29:2' 5\";30:2' 6\";31:2' 7\";32:2' 8\";33:2' 9\";34:2' 10\";35:2' 11\";36:3';37:3' 1\";38:3' 2\"";
