
var selectedList;
var availableList;
var xmlhttp;
var isLoaded = false;

function createListObjects(){
	var sPath = window.location.pathname;
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	//alert(sPage);
	if(sPage == 'news_post_files.php'){
		availableList = document.getElementById("newFiles");
	    selectedList = document.getElementById("currentFiles");
	} else {
	    availableList = document.getElementById("newPlayers");
	    selectedList = document.getElementById("currentPlayers");
    }
    isLoaded=true;
}

 function delAttribute(){
 if(isLoaded == false)
	createListObjects();
   var selIndex = selectedList.selectedIndex;
   if(selIndex < 0)
      return;
   availableList.appendChild(
      selectedList.options.item(selIndex))
   selectNone(selectedList,availableList);
   setSize(availableList,selectedList);
}
function addAttribute(){
if(isLoaded == false)
	createListObjects();
   var addIndex = availableList.selectedIndex;
   if(addIndex < 0)
      return;
   selectedList.appendChild( 
      availableList.options.item(addIndex));
   selectNone(selectedList,availableList);
   setSize(selectedList,availableList);
}

//function setTop(top){
//	document.getElementById
//      (\'someLayer\').style.top = top;
//}
function setLayerTop(lyr,top){
	lyr.style.top = top;
}

function setSize(list1,list2){
    list1.size = getSize(list1);
    list2.size = getSize(list2);
}

function selectNone(list1,list2){
    list1.selectedIndex = -1;
    list2.selectedIndex = -1;
    addIndex = -1;
    selIndex = -1;
}

function getSize(list){
    var len = list.childNodes.length;
    var nsLen = 0;
    //nodeType returns 1 for elements
    for(i=0; i<len; i++){
        if(list.childNodes.item(i).nodeType==1)
            nsLen++;
    }
    if(nsLen<2)
        return 2;
    else
        return nsLen;
}

function delAll(){
if(isLoaded == false)
	createListObjects();
    var len = selectedList.length -1;
    for(i=len; i>=0; i--){
        availableList.appendChild(selectedList.item(i));
    }
    selectNone(selectedList,availableList);
    setSize(selectedList,availableList);
    
}

function addAll(){
if(isLoaded == false)
	createListObjects();
    var len = availableList.length -1;
    for(i=len; i>=0; i--){
        selectedList.appendChild(availableList.item(i));
    }
    selectNone(selectedList,availableList);
    setSize(selectedList,availableList);
    
}

function showSelected(){
    var optionList = 
       document.getElementById
      ("selectedOptions").options;
    var data = '';
    var len = optionList.length;
    for(i=0; i<len; i++){
            data += ',';
        data += optionList.item(i).value;
    }
    alert(data);
}

function post(teamid) {
if(isLoaded == false)
	createListObjects();
xmlhttp=null;

//if (document.getElementById("id") != null){
//	rosterName = document.getElementById("rosterName").value;
//}

// code for Mozilla, etc.
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
// code for IE
else if (window.ActiveXObject)
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttp!=null)
  {
	var optionList = 
	   document.getElementById
	  ("currentPlayers").options;
	var data = '';
	var names = '';
	var len = optionList.length;
	for(i=0; i<len; i++){
		data += optionList.item(i).id;
		data += ',';
		names +=optionList.item(i).text + ', ';
	}
	data = data.substring(0,data.length -1);
	names = names.substring(0,names.length -2);

  xmlhttp.open("POST","team_players.php",false);
	xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');		
	xmlhttp.send("id="+teamid+"&players="+data );
	alert("Players set to: " + names);
	//alert(xmlhttp.responseText);
	window.location = "team.php?id=" + teamid;	
  }
else
  {
  alert("Your browser does not support XMLHTTP.");
  }

}

function postNewsPost(postid) {
if(isLoaded == false)
	createListObjects();
xmlhttp=null;

//if (document.getElementById("id") != null){
//	rosterName = document.getElementById("rosterName").value;
//}

// code for Mozilla, etc.
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
// code for IE
else if (window.ActiveXObject)
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttp!=null)
  {
	var optionList = 
	   document.getElementById
	  ("currentFiles").options;
	var data = '';
	var names = '';
	var len = optionList.length;
	for(i=0; i<len; i++){
		data += optionList.item(i).id;
		data += ',';
		names +=optionList.item(i).text + ', ';
	}
	data = data.substring(0,data.length -1);
	names = names.substring(0,names.length -2);

  xmlhttp.open("POST","news_post_files.php",false);
	xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');		
	xmlhttp.send("id="+postid+"&files="+data );
	alert("Files set to: " + names);
	//we simply close this window when we are done!!
	//alert(xmlhttp.responseText);
	//refresh the calling page
	opener.location.reload();
	window.close();
	window.location = "news_post.php?id=" + postid;	
  }
else
  {
  alert("Your browser does not support XMLHTTP.");
  }

}








//lvayner.. this is a copy n remake of the above to fit the user.php page
function postUser(userid) {
if(isLoaded == false)
	createListObjects();
xmlhttp=null;

//if (document.getElementById("team_id") != null){
//	rosterName = document.getElementById("rosterName").value;
//}

// code for Mozilla, etc.
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
// code for IE
else if (window.ActiveXObject)
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttp!=null)
  {
	var optionListLeague = 
	   document.getElementById
	  ("LeagueUser").options;
	var leaguedata = '';
	var leaguename = '';
	var len = optionListLeague.length;
	for(i=0; i<len; i++){
		if(optionListLeague.item(i).selected == true){
			leaguedata += optionListLeague.item(i).id;
			leaguedata += ',';
			leaguename += optionListLeague.item(i).text;
			leaguename += ',';
		}

	}
	leaguedata = leaguedata.substring(0,leaguedata.length -1);
	leaguename = leaguename.substring(0,leaguename.length -1);
	
	var optionListDivision = 
	   document.getElementById
	  ("DivisionUser").options;
	var divisiondata = '';
	var divisionname= '';
	var len = optionListDivision.length;
	for(i=0; i<len; i++){
		if(optionListDivision.item(i).selected == true){
			divisiondata += optionListDivision.item(i).id;
			divisiondata += ',';
			divisionname += optionListDivision.item(i).text;
			divisionname += ',';
		}

	}
	divisiondata = divisiondata.substring(0,divisiondata.length -1);
	divisionname = divisionname.substring(0,divisionname.length -1);
	
	var optionListTeam = 
	   document.getElementById
	  ("TeamUser").options;
	var teamdata = '';
	var teamname = '';
	var len = optionListTeam.length;
	for(i=0; i<len; i++){
		if(optionListTeam.item(i).selected == true){
			teamdata += optionListTeam.item(i).id;
			teamdata += ',';
			teamname += optionListTeam.item(i).text;
			teamname += ',';
		}

	}
	teamdata = teamdata.substring(0,teamdata.length -1);
	teamname = teamname.substring(0,teamname.length -1);

  xmlhttp.open("POST","user.php",false);
	xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');		
	xmlhttp.send("id="+userid+"&isAssignmentUpdate=1&LeagueUser="+leaguedata+"&DivisionUser="+divisiondata+"&TeamUser="+teamdata );
	alert('User updated with the following assingments \n\r-------------------------------------------------------\n\r'+'Leagues: ' + leaguename + '\n\r' + 'Divisions: ' + divisionname + '\n\r' + 'Teams: ' + teamname);
	//alert(xmlhttp.responseText);
	//window.location = "team_players.php?team_id=" + teamid;	
  }
else
  {
  alert("Your browser does not support XMLHTTP.");
  }

}

//lvayner.. 03-05-2008 For Sponsors
function postSponsor(sponsorid) {
if(isLoaded == false)
	createListObjects();
xmlhttp=null;

// code for Mozilla, etc.
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
// code for IE
else if (window.ActiveXObject)
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttp!=null)
  {
	var optionListDivision = 
	   document.getElementById
	  ("DivisionSponsor").options;
	var divisiondata = '';
	var divisionname= '';
	var len = optionListDivision.length;
	for(i=0; i<len; i++){
		if(optionListDivision.item(i).selected == true){
			divisiondata += optionListDivision.item(i).id;
			divisiondata += ',';
			divisionname += optionListDivision.item(i).text;
			divisionname += ',';
		}

	}
	divisiondata = divisiondata.substring(0,divisiondata.length -1);
	divisionname = divisionname.substring(0,divisionname.length -1);
	
	var optionListTeam = 
	   document.getElementById
	  ("TeamSponsor").options;
	var teamdata = '';
	var teamname = '';
	var len = optionListTeam.length;
	for(i=0; i<len; i++){
		if(optionListTeam.item(i).selected == true){
			teamdata += optionListTeam.item(i).id;
			teamdata += ',';
			teamname += optionListTeam.item(i).text;
			teamname += ',';
		}

	}
	teamdata = teamdata.substring(0,teamdata.length -1);
	teamname = teamname.substring(0,teamname.length -1);
	
	var wschecked = document.getElementById("WholeSite").checked;
	if (wschecked==true)
	{
		var wholesite = 1;
	}
	else
	{
		var wholesite = 0;
	}
	
	xmlhttp.open("POST","sponsor.php",false);
	xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');		
	xmlhttp.send("id="+sponsorid+"&isAssignmentUpdate=1&DivisionSponsor="+divisiondata+"&TeamSponsor="+teamdata+"&WholeSite="+wholesite);
	
	//alert('Sponsor updated with the following assingments \n\r-------------------------------------------------------\n\r'+'Leagues: ' + leaguename + '\n\r' + 'Divisions: ' + divisionname + '\n\r' + 'Teams: ' + teamname);
	//alert(xmlhttp.responseText);
	//window.location = "team_players.php?team_id=" + teamid;	
  }
else
  {
  alert("Your browser does not support XMLHTTP.");
  }

}







//for games
function postGame(orderby,lastview,frame,filter) {
xmlhttp=null;

// code for Mozilla, etc.
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
// code for IE
else if (window.ActiveXObject)
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttp!=null)
  {
	if(lastview == '0'){
		var optionGroupBy = 
		   document.getElementById
		  ("groupby").options;
	}else {
		var optionGroupBy = 
	   document.getElementById
	  ("groupby2").options;
	 }
	var groupby = '';
	var len = optionGroupBy.length;
	var t = optionGroupBy.text;
	for(i=0; i<len; i++){
		if(optionGroupBy.item(i).selected == true){
			var text = optionGroupBy.item(i).text;
			groupby += optionGroupBy.item(i).value;
		}
	}
  xmlhttp.open("POST","games.php",false);
	xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');		
	xmlhttp.send("orderby="+orderby+"&groupby="+groupby+"&lastview="+lastview+"&frame="+frame+"&filter="+filter);
	//alert('Groupby Set to ' + groupby);
	//alert(xmlhttp.responseText);
	//document.write(xmlhttp.responseText);
	window.location = "games.php?orderby="+ orderby +"&groupby="+groupby+"&lastview="+lastview+"&frame="+frame+"&filter="+filter;	
  }
else
  {
  alert("Your browser does not support XMLHTTP.");
  }

}
//-------------------------------------------------------------------------------------------------------
//for tourneys
function postTournament(orderby,lastview,frame,filter) {
xmlhttp=null;

// code for Mozilla, etc.
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
// code for IE
else if (window.ActiveXObject)
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttp!=null)
  {
	if(lastview == '0'){
		var optionGroupBy = 
		   document.getElementById
		  ("groupby").options;
	}else {
		var optionGroupBy = 
	   document.getElementById
	  ("groupby2").options;
	 }
	var groupby = '';
	var len = optionGroupBy.length;
	var t = optionGroupBy.text;
	for(i=0; i<len; i++){
		if(optionGroupBy.item(i).selected == true){
			var text = optionGroupBy.item(i).text;
			groupby += optionGroupBy.item(i).value;
		}
	}
  xmlhttp.open("POST","tournaments.php",false);
	xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');		
	xmlhttp.send("orderby="+orderby+"&groupby="+groupby+"&lastview="+lastview+"&frame="+frame+"&filter="+filter);
	//alert('Groupby Set to ' + groupby);
	//alert(xmlhttp.responseText);
	//document.write(xmlhttp.responseText);
	window.location = "tournaments.php?orderby="+ orderby +"&groupby="+groupby+"&lastview="+lastview+"&frame="+frame+"&filter="+filter;	
  }
else
  {
  alert("Your browser does not support XMLHTTP.");
  }

}
//-------------------------------------------------------------------------------------------------------


//to apply filters on game page
function filterGame(orderby,lastview,frame,filter){
xmlhttp=null;


// code for Mozilla, etc.
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
// code for IE
else if (window.ActiveXObject)
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttp!=null)
  {
  
   //some old junk for the other logic
   if(lastview == '0'){
		var optionGroupBy = 
		   document.getElementById
		  ("groupby").options;
	}else {
		var optionGroupBy = 
	   document.getElementById
	  ("groupby").options;
	 }
	var groupby = '';
	var len = optionGroupBy.length;
	var t = optionGroupBy.text;
	for(i=0; i<len; i++){
		if(optionGroupBy.item(i).selected == true){
			var text = optionGroupBy.item(i).text;
			groupby += optionGroupBy.item(i).value;
		}
	}
	
	var filterDivision = 
	   document.getElementById
	   ("divisionfilter").options;
	
	var filterTeam = 
	   document.getElementById
	   ("teamfilter").options;
/*	
	var filterSponsor = 
	   document.getElementById
	   ("sponsorfilter").options;
*/	
	var filterMonth = 
	   document.getElementById
	   ("monthfilter").options;
	   
	   
	   
	    
	var filter = '';
	var text = '';
	
	var len = filterDivision.length;
	for(i=0; i<len; i++){
		if(filterDivision.item(i).selected == true && filterDivision.item(i).value != ""){
			text += 'divisionid=' + filterDivision.item(i).value;
		}
	}
	
	var len = filterTeam.length;
	for(i=0; i<len; i++){
		if(filterTeam.item(i).selected == true && filterTeam.item(i).value != ""){
			if(text != ''){
				text += ' and ';
			}
			text += 'hometeamid=' + filterTeam.item(i).value + ' or teamidaway='+ filterTeam.item(i).value;
		}
	}
	
		if (filter!=''){	
		 	xmlhttp.open("POST","games.php",false);
			xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');		
			xmlhttp.send("orderby="+orderby+"&groupby="+groupby+"&lastview="+lastview+"&frame="+frame+"&filter="+text);
		//alert('Groupby Set to ' + groupby);
		//alert(xmlhttp.responseText);
			document.write(xmlhttp.responseText);
		//window.location = "games.php?orderby="+ orderby +"&groupby="+groupby+"&lastview="+lastview+"&frame="+frame+"&filter="+filter;	
		}else{
			xmlhttp.open("POST","games.php",false);
			xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');		
			xmlhttp.send("");
			document.write(xmlhttp.responseText);
		}
  }
else
  {
  alert("Your browser does not support XMLHTTP.");
  }


}







//for games.php to show tables one after another
function printDisplay(){
//remove the title header and pager before we print it.
		var c = document.getElementById('content');
		if(c == null){ //hit plan B
			c = document.getElementById('contentMain');
		}
		var h = document.getElementById('header');
		var menu = document.getElementById('menu');
		if(h){
			c.removeChild(h);
		}
		
		var p = document.getElementById('pager');
		if(p){
			c.removeChild(p);
			//p.style.visibility = 'hidden';
		}
		if(menu){
		var mainContent = document.getElementById('contentMain');
			if(mainContent)
				mainContent.removeChild(menu);
		}
		var allTables = document.getElementsByTagName("table");
		
		for (var i=1;i<=Math.ceil(allTables.length);i++)
		{
			var x = document.getElementById('page' + i);
			if(x){
				x.style.position = 'static';
				x.style.visibility = 'visible';
				x.style.zindex = -1;
				x.className= 'table';
			}
		}
	}
//for games.php to show only a given table
function printDisplayPage(page){
//for later processing
var tablesToHide = new Array();
//remove the title header and pager before we print it.
		var c = document.getElementById('content');
		if(c == null){ //hit plan B
			c = document.getElementById('contentMain');
		}
		var h = document.getElementById('header');
		var menu = document.getElementById('menu');
		if(h){
			c.removeChild(h);
		}
		
		var p = document.getElementById('pager');
		if(p){
			c.removeChild(p);
			//p.style.visibility = 'hidden';
		}
		if(menu){
		var mainContent = document.getElementById('contentMain');
			if(mainContent)
				mainContent.removeChild(menu);
		}
		var allTables = document.getElementsByTagName("table");
		
		
		//now for the tables
		var scheduleDiv = document.getElementById("schedule");
		
		var counter = 0;
		for (var i=1;i<=Math.ceil(allTables.length);i++)
		{
			var x = document.getElementById('page' + i);
			if(x){
				if(i == page){
					x.style.position = 'static';
					x.style.visibility = 'visible';
					x.style.zindex = -1;
					x.className= 'table';
				} else {
					tablesToHide[counter] = i;	
					counter++;				
				}
				
			}
			
		}
		for (var i=0;i<tablesToHide.length;i++){
			var x = document.getElementById('page' + tablesToHide[i]);
			if(scheduleDiv){
					scheduleDiv.removeChild(x);
			}
		}	
}

//call this function to create a printable page of an iframe called myIFRAME
function MakePrint(title){
	var loc = frames['myIFRAME'].location.href;
	if (loc.indexOf('games.php') > 0){
		printCurrentPage(title,loc);
	} else {
		title = title.replace(/&/g,'%26');
		loc = loc.replace(/&/g,'%26');
		window.open('print.php?title=' + title + '&url=' + loc);
	}
}

function printCurrentBox(title){
	title = title.replace(/&/g,'%26');
	window.open('print.php?title=' + title);
}
function printCurrentPage(title,url){
	if(url != null) {
		if(url.match('games.php') != null){
		//alert(url + '\nindex of ? = ' + url.indexOf('?'));
			if (url.indexOf('?') > 0){				
				url += '&print';				
			} else {
				url += '?print';				
			}
		}
		
		title = title.replace(/&/g,'%26');
		url = url.replace(/&/g,'%26');
		window.open('print.php?title=' + title + '&url=' + url);
	} else {
		window.open('print.php?title=' + title);
	}
}
function printCurrentPage(title,url,page){
	if(url != null) {
		if(url.match('games.php') != null){
		//alert(url + '\nindex of ? = ' + url.indexOf('?'));
			if (url.indexOf('?') > 0){				
				url += '&print';				
			} else {
				url += '?print';				
			}
			url += '&page=' + page;
		}
		
		title = title.replace(/&/g,'%26');
		url = url.replace(/&/g,'%26');
		window.open('print.php?title=' + title + '&url=' + url);
	} else {
		window.open('print.php?title=' + title);
	}
}

//for color schema
function SetColorValue(controlName){	
	showColorDiv('controls/colors.php?control' + controlName);
}

 var thisURL;
 function showColorDiv(passingControl) 
{
	var url = 'controls/colors.php';
	thisURL = url;
	if(url != ''){
	    // branch for native XMLHttpRequest object
	    if (window.XMLHttpRequest) {
	        req = new XMLHttpRequest();
	        req.onreadystatechange = processReqChange;
	        req.open("GET", url, true);
	        req.send(null);
	    // branch for IE/Windows ActiveX version
	    } else if (window.ActiveXObject) {
	        req = new ActiveXObject("Microsoft.XMLHTTP");
	        if (req) {
	            req.onreadystatechange = processReqChange;
	            req.open("GET", url, true);
	            req.send();
	        }
	    }
	}
	else{
		//the document was create from the session variable, so just print it;
		//var t = setTimeout("window.print()",100);
	}
}
function processReqChange() 
{
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
        	var newdiv=document.createElement("div")
        	newdiv.setAttribute('align','center');
        	newdiv.setAttribute('position','absolute');
        	newdiv.id = 'contentMain';
        	newdiv.name = 'contentMain';
			newdiv.innerHTML="<?php showPrintFrame('whatever',thisURL,'',800,500,1); ?>";
			//newdiv.appendChild(newtext) //append text to new div
			document.getElementById("body").appendChild(newdiv)

			
        	//var t = setTimeout("window.print()",100);
        } else {
            alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }
}