MSIE = (navigator.appName=="Microsoft Internet Explorer")?-1:0;
linkt1 = -1; linkyr = -1;

function showfollowing(showitm){
 sho=2; hid=1;
 if(showitm == 1){sho=1;hid=2;}
 document.getElementById('disrdscores'+hid).style.display = 'none'
 document.getElementById('disrdscores'+sho).style.display = 'block'
 
}

function scoresover(event){
 if (MSIE) src = window.event.srcElement;
 else src = event.target;
 if (src.tagName != "TR")src = src.parentNode;
 if (src.tagName != "TR")src = src.parentNode;
 if (src.tagName != "TR")src = src.parentNode;
 src.style.background = '#dfe5cb';
}

function scoresout (event){
 if (MSIE) src = window.event.srcElement;
 else src = event.target;
 if (src.tagName != "TR")src = src.parentNode;
 if (src.tagName != "TR")src = src.parentNode;
 if (src.tagName != "TR")src = src.parentNode;
 src.style.background = 'none';
}

function findPos(obj) {
	var curleft = curtop = 0;

	if (obj.offsetParent) {
	do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	return [curleft,curtop];
	}
}

function showheadtohead (t1,t2){
 hr='?option=com_content&view=article&id=112:head-to-head&catid=34:all-time&Itemid=56&qry=1&t1=' + t1 + '&t2=' + t2;
 window.location.href= hr
 
}

function removeItems(originalArray, itemsToRemove) {
 var j;
 for (var i = 0; i < itemsToRemove.length; i++) {
	j = 0;
	while (j < originalArray.length) {
		if (originalArray[j] == itemsToRemove[i]){
			originalArray.splice(j, 2);
		} 
		else j++;
	}
 }
 return originalArray;
}


function writeatsform(){
InitContext()
if (atsform=="") return

qryitem = new Array ("fmqtr", "Fm Qtr", "t1","Team 1", "t2", "Team 2", "home","Home", "away", "Away", "inyr", "IN Year", "yrfm", "FM Year", "yrto", "TO Year", "inmo", "IN Month", "fmmo", "FM Month", "tomo", "TO Month",  "day", "ON Day", "dn", "Day/Night", "inrd", "IN Rd", "rdfm", "FM Rd", "rdto", "TO Rd", "inat", "In/At", "gnd", "Ground", "trl","Led/Trl");


if(atsform=="default"){
	formrem = new Array ('fmqtr');
	qryitem = removeItems(qryitem, formrem);
}
else if(atsform=="use"){
	qryitem = formrem;
}
else {
	if(formrem!="")qryitem = removeItems(qryitem, formrem);
}

txc =  "<img alt='Copy' style='cursor:pointer;' title='Copy Filters' onclick='copyfilter()' src='afl/site/imgs/copy.png' onmouseover='this.style.background = \"#cccccc\"' onmouseout='this.style.background = \"\"' /> ";
txc += "<a  onmouseover='this.style.background = \"#cccccc\"' onmouseout='this.style.background = \"\"' id='filterhelp' style='float:right' title='View Filter FAQ' href='?option=com_content&view=article&id=118:using-filters&catid=31:general&Itemid=41'></a> ";
txc += "<img alt='Paste' style='cursor:pointer;' title='Paste Filters' onclick='pastefilter()' src='afl/site/imgs/paste.png'  onmouseover='this.style.background = \"#cccccc\"' onmouseout='this.style.background = \"\"' />";
document.getElementById('atsform').innerHTML = txc;

tx1 = "Add Filter : ";
tx2 = "<select id='addaqry' onchange='addaqryitem()'><option value=''>....</option>";

for(x=0;x<qryitem.length;x+=2){
 tx2 += "<option value = '" + qryitem[x] + "'>" + qryitem[x+1] + "</option>";
}
tx2 += "</select>";

addqry (tx1, tx2, "");

document.getElementById("filterhelp").innerHTML = "<img alt='help' src='afl/site/imgs/help.png' />";

cururl = cururl.replace("&amp;","&");
abc = cururl.match(/qry=1&(.*)/g)
itm = RegExp.$1;
itms = itm.split("&")

if(formdef!=""){
 formdef = formdef.replace(/&amp;/g,"&");
 itms2 = formdef.split("&")
 if(itms2.length > 0){
	for(xx=0; xx<itms2.length; xx++){
		z = itms2[xx].split("=");
		addaqryitem(z[0],z[1]);		
	}
 }
}

if(itms.length > 0 && itm!=""){
	for(xx=0; xx<itms.length; xx++){
		z = itms[xx].split("=");
		addaqryitem(z[0],z[1]);	
	}
}


}

thelink = "";
showx = -1;

function addaqryitem(){
 itmv = document.getElementById('addaqry')
 itm = itmv.value

 itmval = "abc";
 if(arguments.length>1){
	itm = arguments[0];
	itmval = arguments[1];
 }



 switch(itm){

	case "t1":
		tx1 = "Team 1 : ";
		tx2 = "<select name='t1'><option value=''></option>";
		for(x=0; x < teams.length; x+=2){

		 xtxt=""; if(itmval==teams[x])xtxt="SELECTED ";
		 tx2 += "<option " + xtxt + "value='" + teams[x] + "'>" + teams[x+1] + "</option>";
		}
		tx2 += "</select>";
		addqry (tx1, tx2);
		break;

	case "t2":
		tx1 = "Team 2 : ";
		tx2 = "<select name='t2'><option value=''></option>";
		for(x=0; x < teams.length; x+=2){
		 xtxt=""; if(itmval==teams[x])xtxt="SELECTED ";
		 tx2 += "<option " + xtxt + "value='" + teams[x] + "'>" + teams[x+1] + "</option>";
		}
		tx2 += "</select>";
		addqry (tx1, tx2);
		break;

	case "home":
		tx1 = "Home : ";
		tx2 = "<select name='home'><option value=''></option>";
		for(x=0; x < teams.length; x+=2){
		 xtxt=""; if(itmval==teams[x])xtxt="SELECTED ";
		 tx2 += "<option " + xtxt + "value='" + teams[x] + "'>" + teams[x+1] + "</option>";
		}
		tx2 += "</select>";
		addqry (tx1, tx2);
		break;

	case "away":
		tx1 = "Away : ";
		tx2 = "<select name='away'><option value=''></option>";
		for(x=0; x < teams.length; x+=2){
		 xtxt=""; if(itmval==teams[x])xtxt="SELECTED ";
		 tx2 += "<option " + xtxt + "value='" + teams[x] + "'>" + teams[x+1] + "</option>";
		}
		tx2 += "</select>";
		addqry (tx1, tx2);
		break;

	case "inyr":
		tx1 = "IN Year : ";
		tx2 = "<select name='inyr'><option value=''></option>";
		for(x=minyr; x <= maxyr; x++){
		 xtxt=""; if(itmval==x)xtxt="SELECTED ";
		 tx2 += "<option " + xtxt + "value='" + x + "'>" + x + "</option>";
		}
		tx2 += "</select>";
		addqry (tx1, tx2);
		break;

	case "yrfm":
		tx1 = "FM Year : ";
		tx2 = "<select name='yrfm'><option value=''></option>";
		for(x=minyr; x <= maxyr; x++){
		 xtxt=""; if(itmval==x)xtxt="SELECTED ";
		 tx2 += "<option " + xtxt + "value='" + x + "'>" + x + "</option>";
		}
		tx2 += "</select>";
		addqry (tx1, tx2);
		break;

	case "yrto":
		tx1 = "TO Year : ";
		tx2 = "<select name='yrto'><option value=''></option>";
		for(x=minyr; x <= maxyr; x++){
		 xtxt=""; if(itmval==x)xtxt="SELECTED ";
		 tx2 += "<option " + xtxt + "value='" + x + "'>" + x + "</option>";
		}
		tx2 += "</select>";
		addqry (tx1, tx2);
		break;

	case "inmo":
		tx1 = "IN Month : ";
		mths = new Array ('Mar','Apr','May','Jun','Jul','Aug','Sep','Oct')
		tx2 = "<select name='inmo'><option value=''></option>";
		for(x=0; x < mths.length; x++){
		 xtxt=""; if(itmval==x+3)xtxt="SELECTED ";
		 tx2 += "<option " + xtxt + "value='" + (x+3) + "'>" + mths[x] + "</option>";
		}
		tx2 += "</select>";
		addqry (tx1, tx2);
		break;

	case "fmmo":
		tx1 = "FM Month : ";
		mths = new Array ('Mar','Apr','May','Jun','Jul','Aug','Sep','Oct')
		tx2 = "<select name='fmmo'><option value=''></option>";
		for(x=0; x < mths.length; x++){
		 xtxt=""; if(itmval==x+3)xtxt="SELECTED ";
		 tx2 += "<option " + xtxt + "value='" + (x+3) + "'>" + mths[x] + "</option>";
		}
		tx2 += "</select>";
		addqry (tx1, tx2);
		break;

	case "tomo":
		tx1 = "TO Month : ";
		mths = new Array ('Mar','Apr','May','Jun','Jul','Aug','Sep','Oct')
		tx2 = "<select name='tomo'><option value=''></option>";
		for(x=0; x < mths.length; x++){
		 xtxt=""; if(itmval==x+3)xtxt="SELECTED ";
		 tx2 += "<option " + xtxt + "value='" + (x+3) + "'>" + mths[x] + "</option>";
		}
		tx2 += "</select>";
		addqry (tx1, tx2);
		break;

	case "day":
		tx1 = "ON Day : ";
		days = new Array ('Sun','Mon','Tue','Wed','Thur','Fri','Sat')
		tx2 = "<select name='day'><option value=''></option>";
		for(x=0; x < days.length; x++){
		 xtxt=""; if(itmval==x+1)xtxt="SELECTED ";
		 tx2 += "<option " + xtxt + "value='" + (x+1) + "'>" + days[x] + "</option>";
		}
		tx2 += "</select>";
		addqry (tx1, tx2);
		break;

	case "dn":
		tx1 = "Day/Night : ";
		days = new Array ('Day','Night')
		tx2 = "<select name='dn'><option value=''></option>";
		for(x=0; x < days.length; x++){
		 xtxt=""; if(itmval==x)xtxt="SELECTED ";
		 tx2 += "<option " + xtxt + "value='" + x + "'>" + days[x] + "</option>";
		}
		tx2 += "</select>";
		addqry (tx1, tx2);
		break;


	case "gnd":
		tx1 = "Ground : ";
		tx2 = "<select name='gnd'><option value=''></option>";
		for(x=0; x < grounds.length; x+=2){
		 xtxt=""; if(itmval==grounds[x])xtxt="SELECTED ";
		 tx2 += "<option " + xtxt + "value='" + grounds[x] + "'>" + grounds[x+1] + "</option>";
		}
		statearr = new Array ('in Vic', 'in NSW', 'in QLD', 'in ACT', 'in SA', 'in NT', 'in WA', 'in Tas'); 
		for(x=0; x<statearr.length; x++){
		 xtxt=""; if(itmval==x+1001)xtxt="SELECTED ";
		 tx2 += "<option " + xtxt + "value='" + (1001+x) + "'>" + statearr[x] + "</option>";
		}
		tx2 += "</select>";
		addqry (tx1, tx2);
		break;

	case "inrd":
		tx1 = "IN Rd : ";
		tx2 = "<select name='inrd'><option value=''></option>";
		for(x=0; x < rounds.length; x+=2){
		 xtxt=""; if(itmval==rounds[x])xtxt="SELECTED ";
		 tx2 += "<option " + xtxt + "value='" + rounds[x] + "'>" + rounds[x+1] + "</option>";
		}
		tx2 += "</select>";
		addqry (tx1, tx2);
		break;

	case "rdfm":
		tx1 = "FM Rd : ";
		tx2 = "<select name='rdfm'><option value=''></option>";
		for(x=0; x < rounds.length; x+=2){
		 xtxt=""; if(itmval==rounds[x])xtxt="SELECTED ";
		 tx2 += "<option " + xtxt + "value='" + rounds[x] + "'>" + rounds[x+1] + "</option>";
		}
		tx2 += "</select>";
		addqry (tx1, tx2);
		break;

	case "rdto":
		tx1 = "TO Rd : ";
		tx2 = "<select name='rdto'><option value=''></option>";
		for(x=0; x < rounds.length; x+=2){
		 xtxt=""; if(itmval==rounds[x])xtxt="SELECTED ";
		 tx2 += "<option " + xtxt + "value='" + rounds[x] + "'>" + rounds[x+1] + "</option>";
		}
		tx2 += "</select>";
		addqry (tx1, tx2);
		break;

	case "inat":
		inata = new Array ("at 1/4 time", "at 1/2 time", "at 3/4 time", "at Full time", "in 2nd Qtr", "in 3rd Qtr", "in 4th Qtr", "in 2nd Half", "in last 3 Qtrs");
		tx1 = "in/at : ";
		tx2 = "<select name='inat'><option value=''></option>";
		for(x=0; x < inata.length; x++){
		 xtxt=""; if(itmval==x)xtxt="SELECTED ";
		 tx2 += "<option " + xtxt + "value='" + x + "'>" + inata[x] + "</option>";
		}
		tx2 += "</select>";
		addqry (tx1, tx2);
		break;

	case "fmqtr":
		inata = new Array ("fm 1/4 time", "fm 1/2 time", "fm 3/4 time");
		tx1 = "From Qtr : ";
		tx2 = "<select name='fmqtr'><option value=''></option>";
		for(x=0; x < inata.length; x++){
		 xtxt=""; if(itmval==x)xtxt="SELECTED ";
		 tx2 += "<option " + xtxt + "value='" + x + "'>" + inata[x] + "</option>";
		}
		tx2 += "</select>";
		addqry (tx1, tx2);
		break;

	case "trl":
		inata = new Array ("led at 1/4t", "led at 1/2t", "led at 3/4t", "trl at 1/4t", "trl at 1/2t", "trl at 3/4t","draw at 1/4t", "draw at 1/2t", "draw at 3/4t", "draw at FT");
		tx1 = "Led/Trl : ";
		tx2 = "<select name='trl'><option value=''></option>";
		for(x=0; x < inata.length; x++){
		 xtxt=""; if(itmval==x)xtxt="SELECTED ";
		 tx2 += "<option " + xtxt + "value='" + x + "'>" + inata[x] + "</option>";
		}
		tx2 += "</select>";
		addqry (tx1, tx2);
		break;

  }
 itmv.value = "";

}

function delrow(event){
 if (MSIE) src = window.event.srcElement.parentNode.parentNode;
 else src = event.target.parentNode.parentNode;
 tbl = document.getElementById('qry')
 rown = src.rowIndex;
 tbl.deleteRow(rown)
}

function addqry(){
	c1 = arguments[0];
	c2 = arguments[1];
	tbl = document.getElementById('qry')
	var lastRow = tbl.rows.length;
	var row = tbl.insertRow(lastRow);

	var cellLeft = row.insertCell(0);
	cellLeft.setAttribute("width", "58px");

	cellLeft.innerHTML = c1;

	var cellLeft = row.insertCell(1);
	cellLeft.innerHTML = c2;

	//alert(arguments.length)

	var cellLeft = row.insertCell(2);
	if(arguments.length!=3){
		if(showx)cellLeft.innerHTML = "<b onclick='delrow(event)'>x</b>";
	}

	if(lastRow==1){
		document.getElementById('thelink').innerHTML = "GET INFO";
	}
}

function updatelink(){
 txt = getupdatedlink(1);
 document.getElementById('thelink').href = txt;

}

function getupdatedlink(v){
 b = document.getElementById("qry")
  a = b.getElementsByTagName("SELECT")
  ztxt2 = "&qry=1";
 ztxt = jqry.replace(/&amp;/g,"&");
 for (x=0;x<a.length;x++){
	elmn = a[x].name; elmv = a[x].value
	if(elmn!=""&&elmv!="") ztxt2 += "&" + elmn + "=" + elmv;
 }
 if(v==2) return ztxt2;
 else return ztxt + ztxt2;
}

function copyfilter(){
 txt = getupdatedlink(2)
 set_cookie ("filter",txt)
}

function pastefilter(){
 var x = get_cookie ( "filter" );
 if(x){
	abc = x.match(/qry=1&(.*)/g)
	itm = RegExp.$1;
	itms = itm.split("&")

	if(itms.length > 0 && itm!=""){
		for(xx=0; xx<itms.length; xx++){
			z = itms[xx].split("=");
			addaqryitem(z[0],z[1]);	
		}
	}
 }
}

//function set_cookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure )

function set_cookie ( name, value, exp, path, domain, secure )
{
  var cookie_string = name + "=" + escape ( value );

  if ( exp )
  {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+exp);
		
    //var expires = new Date ( exp_y, exp_m, exp_d );
    cookie_string += "; expires=" + exdate.toGMTString();
  }

  if ( path )
        cookie_string += "; path=" + escape ( path );

  if ( domain )
        cookie_string += "; domain=" + escape ( domain );
  
  if ( secure )
        cookie_string += "; secure";
  
  document.cookie = cookie_string;
}

function delete_cookie ( cookie_name )
{
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}
function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}


function setdefaultside(){
 a = document.getElementById('defside').value
 if(a == ""){
	delete_cookie ("defaultteam")
 }
 else {
	set_cookie ("defaultteam", a, 365)
 }


}


function quickjump(){
 a = document.getElementById('quickjump')
 txt = "<a title='View Quick Jump FAQs' style='float:right' href='?option=com_content&view=article&id=185:how-to-use-quick-jump&catid=31:general&Itemid=41'><img alt='help' src='afl/site/imgs/help.png' /></a>";
 txt += '<table id=\'qry\'><tr><td>Team: </td><td>'
 txt += "<select onchange='chgqj()' id='qlt1'><option value=''></option>";
 for(x=0; x < teams.length; x+=2){
  txt += "<option value='" + teams[x] + "'>" + teams[x+1] + "</option>";
 }
 txt += "</select></td></tr><tr><td>Year: </td><td>";

 txt += "<select  onchange='chgqj()' id='qlyr'><option value=''></option>";
 for(x=maxyr; x >= minyr; x--){
  txt += "<option value='" + x + "'>" + x + "</option>";
 }
 txt += "</select></td></tr><tr><td>Rd: </td><td>";
 txt += "<select onchange='chgqj()' id='qlrd'><option value=''></option>";
 for(x=0; x < rounds.length; x+=2){
  txt += "<option value='" + rounds[x] + "'>" + rounds[x+1] + "</option>";
 }
 txt += "</select>";


 txt += "</td></tr><tr><td></td><td><a id='quicklinkhref' title='' href='#'>GO!</a></td></tr></table>";

 txt += "<p style='text-align:center'><a href='?option=com_content&view=article&id=173&Itemid=65'>All Records</a> - <a href='?option=com_content&view=article&id=172&Itemid=62'>All Stats</a> - <a href='?option=com_content&view=article&id=174&Itemid=64'>All Misc</a></p>";


 var defteam = get_cookie ( "defaultteam" );


 txt += "<hr /><p>Default Side: ";
 txt += "<select onchange='setdefaultside()' style='padding:0' id='defside'><option value=''></option>";
 for(x=0; x < teams.length; x+=2){
  xtxt = ""; if(defteam == teams[x])xtxt = "SELECTED ";

  txt += "<option " + xtxt + "value='" + teams[x] + "'>" + teams[x+1] + "</option>";
 }
 txt += "</select></p>";


 a.innerHTML = txt;
}

function chgqj(){
	qt1 = document.getElementById('qlt1').value;
	qyr = document.getElementById('qlyr').value;
	qrd = document.getElementById('qlrd').value;
	thel = document.getElementById('quicklinkhref');

 if(qt1 != '' && qyr == '' && qrd == ''){
	thel.title = 'View Team Summary'
	thel.href = "?option=com_content&view=article&id=187&catid=34&Itemid=56&qry=1&t1=" + qt1 
	thel.innerHTML = 'GO!'
 }
 if(qt1 != '' && qyr != '' && qrd == ''){
	thel.title = "View Team's Season Summary"
	thel.href = "?option=com_content&view=article&id=138&catid=36&Itemid=56&qry=1&t1=" + qt1 + "&inyr=" + qyr
	thel.innerHTML = 'GO!'
 }
 if(qt1 != '' && qyr != '' && qrd != ''){
	thel.title = "View Match Summary"
	thel.href = "?option=com_content&view=article&id=160&catid=34&Itemid=56&qry=1&t1=" + qt1 + "&inyr=" + qyr + "&inrd=" + qrd
	thel.innerHTML = 'GO!'
 }
 if(qt1 == '' && qyr != '' && qrd == ''){
	thel.title = 'View Season Summary'
	thel.href = "?option=com_content&view=article&id=139:season-review&catid=36:individual-season&Itemid=56&qry=1&inyr=" + qyr
	thel.innerHTML = 'GO!'
 }
 if(qt1 == '' && qyr != '' && qrd != ''){
	thel.title = 'View Round Summary'
	thel.href = '?option=com_content&view=article&id=140:round-details&catid=34:all-time&Itemid=56&qry=1&inyr=' + qyr + '&inrd=' + qrd
	thel.innerHTML = 'GO!'
 }


}



