var website = 0;
var season;
var juniors = false;
var schools = false;

// push for IE5 (from www.quirksmode.org)
function Array_push()
{
	var A_p = 0
	for (A_p = 0; A_p < arguments.length; A_p++)
	{
		this[this.length] = arguments[A_p]
	}
	return this.length;
}
// pop for IE5 
function Array_pop()
{
	lastElement = this[this.length-1];
	this.length = Math.max(this.length-1,0);
	return lastElement;
}

function showHide( id, flag )
{
	var obj = document.getElementById(id);
	if( obj != null )
		obj.style.display = flag?"":"none";
}

function parseSearchString( search )
{
	var commandList = {};
	var i, tokens, cmdList = search.split("&");
	if( cmdList && cmdList.length > 0 )
	{
		for( i=0; i<cmdList.length; i++ )
		{
			tokens = cmdList[i].split("=");
			if( tokens && tokens.length == 2 )
				commandList[tokens[0].toLowerCase()] = tokens[1].toLowerCase();
		}
	}
	return commandList;
}

function deepCopy( obj )
{
	var o;
	if( obj != null && typeof(obj) == "object" )
	{
		if( obj.length == null )
		{
			o = {};
			for( var x in obj )
			{
				o[x] = deepCopy( obj[x] );
			}
		}
		else
		{
			o = [];
			for( var i=0; i<obj.length; i++ )
			{
				o.push( deepCopy( obj[i] ) );
			}
		}
	}
	else
		o = obj;
	return o;
}

function replaceAll( str, findStr, replaceStr )
{
	if( str == null ) return null;
	var index = str.indexOf( findStr );
	while( index != -1 )
	{
		str = str.substr(0,index) + replaceStr + str.substr(index+findStr.length);
		index = str.indexOf( findStr, index+replaceStr.length );
	}
	return str;
}

function toDateTime(dateObj, dateOnly)
{
	var o = [];
	if( dateObj != null )
	{
		o.push( formatInt(dateObj.getDate(),2) );
		o.push( "/" );
		o.push( formatInt(dateObj.getMonth()+1,2) );
		o.push( "/" );
		o.push( dateObj.getFullYear() );
		if( dateOnly != true )
		{
			o.push( " " );
			o.push( formatInt(dateObj.getHours(),2) );
			o.push( ":" );
			o.push( formatInt(dateObj.getMinutes(),2) );
		}
	}
	return o.join("");
}

function getFrame( frameID )
{
	var obj = document.getElementById(frameID);
	if( obj == null )
		return null;
	return obj.contentWindow;
}

function GetTeamName( teamID )
{
	var teamList = parent.getTeamList();
	var teamObj = teamList[teamID];
	return teamObj == null ? "" : teamObj.clubName + " " + teamObj.teamName;
}

function objectLength( obj )
{
	var count = 0;
	for( var key in obj )
		count++;
	return count;
}

function GenerateMailHref(HrefObj, ref, Subject ){
	if( Subject == null ) {
		HrefObj.href="mail"+"to:" + em[ref] + "@" + ISP[ref];
	} else {
		HrefObj.href="mail"+"to:" + em[ref] + "@" + ISP[ref] + '?subject=' + Subject;
	}
}


// CODE FROM BROWSERDETECT.JS
// ==========================

// Card Status Flags
var CardNotEntered = 0;
var CardSubmittedFlag = 1;
var CardRejectedFlag = 2;
var CardReSubmittedFlag = 3;
var CardEnteredByAdminFlag = 4;
var CardConfirmedFlag = 5;

// Fixture Status Values
var FixtureNotSet = 0;
var FixtureCancelled = 1;
var FixtureSet = 2;
var ScoreOnlyEntered = 3;
var PartialCardEntered = 4;
var FullCardEntered = 5;
	
// Walkover State Values
var NoWalkover = 0;
var HomeWalkover = 1;
var AwayWalkover = 2;
var VoidMatch = 3;

// Competition Types
var ctMens = 1;
var ctLadies = 2;
var ctMixed = 3;
var ctMedley = 4;

// System Setting Types
var SystemSettingTypeInt = 0;
var SystemSettingTypeFloat = 1;
var SystemSettingTypeText = 2;
var SystemSettingTypeDate = 3;
var SystemSettingTypeBit = 4;
var SystemSettingTypeMemo = 5;
var SystemSettingTypeFK = 6;
var SystemSettingTypePerm = 7;

	// Match Card Actions
var MatchCardActionEnterFixture = 1;
var MatchCardActionEditFixture = 2;
var MatchCardActionCancelFixture = 3;
var MatchCardActionEnterScore = 4;
var MatchCardActionEditScore = 5;
var MatchCardActionDeleteScore = 6;
var MatchCardActionEnterCard = 7;
var MatchCardActionEditCard = 8;
var MatchCardActionRejectCard = 9;
var MatchCardActionConfirmCard = 10;
var MatchCardActionDeleteCard = 11;
var MatchCardActionUnconfirmCard = 12;

var detect = navigator.userAgent.toLowerCase();
var thestring;
var vBrowser;
var TRinline = "inline";
var TDinline = "inline";

var shortMonthList = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
var longMonthList = ["January","February","March","April","May","June","July","August","September","October","November","December"];

function BrowserDetect(Display)
{
	var OS, browser, version;

	if (checkIt('konqueror'))
	{
		browser = "Konqueror";
		OS = "Linux";
	}
	else if (checkIt('chrome')) browser = "Chrome"
	else if (checkIt('safari')) browser = "Safari"
	else if (checkIt('omniweb')) browser = "OmniWeb"
	else if (checkIt('opera')) browser = "Opera"
	else if (checkIt('webtv')) browser = "WebTV";
	else if (checkIt('icab')) browser = "iCab"
	else if (checkIt('msie')) browser = "Internet Explorer"
	else if (checkIt('firefox')) browser = "Firefox"
	else if (!checkIt('compatible'))
	{
		browser = "Netscape Navigator"
		version = detect.charAt(8);
	}
	else browser = "An unknown browser";
	if( browser == "Safari" )
	{
		location.href = "NoSafari.htm";
	}

	if (!version)
		version = detect.charAt(place + thestring.length);

	if (!OS)
	{
		if (checkIt('linux')) OS = "Linux";
		else if (checkIt('x11')) OS = "Unix";
		else if (checkIt('mac')) OS = "Mac"
		else if (checkIt('win')) OS = "Windows"
		else OS = "an unknown operating system";
	}

//	alert(browser + " " + version + " " + OS);
	if (Display)
		document.write(browser + " version " + version + " on " + OS);
	return(browser);
}

function checkIt(string)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

//	SOME OLD ROLLOVER.JS CODE
//	=========================

//  Turn off image
function turnOff(ImageName)
{
	var obj = document.getElementById(ImageName);
	if( obj )
		obj.className = "MenuOff";
}

// On mouse over, turn image on
function turnOver(ImageName)
{
	var obj = document.getElementById(ImageName);
	if( obj )
		obj.className = "MenuOn";
}

function initCommon()
{
	// Initialisation code
	if (typeof Array.prototype.push == "undefined")
	{
		Array.prototype.push = Array_push
	}
	if (typeof Array.prototype.pop == "undefined")
	{
		Array.prototype.pop = Array_pop;
	}
	vBrowser = BrowserDetect( 0 );
	if( vBrowser == "Internet Explorer" )
	{
		TRinline = "inline";
		TDinline = "inline";
	}
	else
	{
		TRinline = "table-row";
		TDinline = "table-cell";
	}
	if( typeof(document.getElementById) == "undefined" )
	{
		document.getElementById = function(id) {return document.all[id];}
	}
}

function GetVenue( teamID, venueList, teamList )
{
	var clubID = teamList[teamID].clubId;
	return venueList[clubID];
}

function addFilterObj( filterList, homeTeamID, awayTeamID, venueList, teamList, divID )
{
	var obj = filterList[homeTeamID];
	if( !obj )
		filterList[homeTeamID] = {};
	var venuePtr = null;
	if( venueList != null )
		venuePtr = GetVenue( homeTeamID, venueList, teamList );
	if( filterList[homeTeamID][awayTeamID] == null )
		filterList[homeTeamID][awayTeamID] = {homeTeamID:homeTeamID,awayTeamID:awayTeamID,fixture:null,venuePtr:venuePtr,index:-1,divID:divID};
}

function AddFixtureFiltersForDivision( filterList, div, venueList, teamList )
{
	for( var i=0; i<div.teams.length; i++ )
		for( var j=i+1; j<div.teams.length; j++ )
		{
			addFilterObj( filterList, div.teams[i], div.teams[j], venueList, teamList, div.divID );
			addFilterObj( filterList, div.teams[j], div.teams[i], venueList, teamList, div.divID );
		}
}

function AddFixtureFiltersForTeam( filterList, teamObj, venueList, teamList )
{
	if( teamObj )
	{
		var div = divisionList[teamObj.divID];
		for( var i=0; i<div.teams.length; i++ )
		{
			if( div.teams[i] != teamObj.teamID )
			{
				addFilterObj( filterList, teamObj.teamID, div.teams[i], venueList, teamList, teamObj.divID );
				addFilterObj( filterList, div.teams[i], teamObj.teamID, venueList, teamList, teamObj.divID );
			}
		}
	}
}

function getRoot()
{
	var topFrame = null;
	try
	{
		var root = this;
		if( root.ImTheDaddy == true )
			topFrame = root;
		while( root.parent != root )
		{
			if( root.ImTheDaddy == true )
			{
				topFrame = root;
				break;
			}
			root = root.parent;
		}
		if( root.ImTheDaddy == true )
			topFrame = root;
	}
	catch(e)
	{
		topFrame = null;
	}
	return topFrame;
}

function formatInt( num, digits )
{
	var text = "";
	for( var i=0; i<digits; i++ )
		text += "0";
	text += num;
	return text.substr(text.length-digits);
}

function toHex( num )
{
	var hexDigits = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"];
	var output = [];
	do
	{
		output.push( hexDigits[num % 16] );
		num = Math.floor(num / 16);
	} while( num > 0 );
	return output.reverse().join("");
}

function showFractions( num )
{
	var intPart = Math.floor(num);
	var fracPart = num - intPart;
	if( fracPart == 0.5 )
	{
		return intPart+"&frac12;";
	}
	if( fracPart == 0.25 )
	{
		return intPart+"&frac14;";
	}
	if( fracPart == 0.75 )
	{
		return intPart+"&frac34;";
	}
	return num;
}

function formatMoney( num, showPound )
{
	var n = num.toFixed(2);
	if( showPound )
		return "£" + n;
	return n;
}

function dateSuffix( num )
{
	switch( num )
	{
		case 1: return "1st";
		case 2: return "2nd";
		case 3: return "3rd";
		default: return num+"th";
	}
}

function toLongDate(dateObj)
{
	if( dateObj != null )
	{
		return dateSuffix(dateObj.getDate()) + " " + longMonthList[dateObj.getMonth()] + " " + dateObj.getFullYear();
	}
	return "";
}

function toShortDate(dateObj)
{
	if( dateObj != null )
	{
		return formatInt(dateObj.getDate(),2) + " " + shortMonthList[dateObj.getMonth()] + " " + dateObj.getFullYear();
	}
	return "";
}

function toTime(dateObj)
{
	if( dateObj != null )
	{
		return formatInt(dateObj.getHours(),2) + ":" + formatInt(dateObj.getMinutes(),2);
	}
	return "";
}

function toInteger(str,defaultValue)
{
	var num = defaultValue;
	try
	{
		num = parseInt(str,10);
		if( isNaN(num) )
			num = defaultValue;
	}
	catch(e)
	{
		num = defaultValue;
	}
	return num;
}

function getType( obj )
{
	if( obj == null )
		return "object";
	var t = typeof(obj);
	switch( t )
	{
		case "number":
			var s = String(obj);
			if( s.indexOf(".") != -1 )
				itemType = "single";
			break;
		case "object":
			if( obj.getTime != null )
			{
				t = "date";
				break;
			}
			if( obj.sort != null && obj.length != null )
			{
				t = "array";
				break;
			}
			if( obj == true || obj == false )
				t = "boolean";
			break;
	}
	return t;
}

function formatDbItem( itemType, fieldValue, nullValue, displayFormat )
{
	var text;
	var output = [];
	if( fieldValue == null )
		output.push( nullValue==null ? "null" : nullValue );
	else
	{
		switch( itemType )
		{
			case "Text":
			case "Memo":
			case "text":
			case "string":
				if( !displayFormat )
					output.push( '"' );
				text = replaceAll(fieldValue,'"','\\"');
				text = replaceAll(text,'\n','\\n');
				text = replaceAll(text,'\r','\\r');
				output.push( text );
				if( !displayFormat )
					output.push( '"' );
				break;
			case "date":
			case "DateTime":
				if( !displayFormat )
					output.push( 'new Date("' );
				output.push( toDateTime(fieldValue) );
				if( !displayFormat )
					output.push( '")' );
				break;
			case "Single":
			case "single":
				output.push( fieldValue.toFixed(2) );
				break;
			default:
				output.push( fieldValue );
				break;
		}
	}
	return output.join("");
}

function mouseMove(ev)
{
	var obj = ev.srcElement;
	if( obj == null ) obj = ev.target;
	if( obj && getRoot )
	{
		var topFrame = getRoot();
		if( topFrame != null && topFrame.move )
			topFrame.move(ev);
	}
}

function onPageFocus( data )
{
	if( (this.editing != true && this.pageDataDirty == true && this.forcePopupFocus != true) || (data != null && data.login == true) )
	{
		window.location.reload( true );
	}
	else
	{
		if( this.onPopupFocus != null )
			this.onPopupFocus( data );
	}
	getRoot().setPageOptions( website, season, juniors, schools );
}

function pageInit( assertPageName )
{
	if( assertPageName != null )
		getRoot().assertMenu( assertPageName );
	if( this.init != null )
		this.init();
}

function urlEncode( str )
{
	var s = replaceAll( str, " ", "&nbsp;" );
	s = replaceAll( s, "&", "&amp;" );
	s = replaceAll( s, "£", "&pound;" );
	s = replaceAll( s, "½", "&frac12;" );
	s = replaceAll( s, "¼", "&frac14;" );
	s = replaceAll( s, "¾", "&frac34;" );
	return s;
}

function urlDecode( str )
{
	var s = replaceAll( str, "&nbsp;", " " );
	s = replaceAll( s, "&amp;", "&" );
	s = replaceAll( s, "&pound;", "£" );
	s = replaceAll( s, "&frac12;", "½" );
	s = replaceAll( s, "&frac14;", "¼" );
	s = replaceAll( s, "&frac34;", "¾" );
	return s;
}

initCommon();
