var freelanthropyUrl = "http://www.freelanthropy.com/secretbuilders/download.php?id=";
var playPage = null;
var toolbarId = null;
function checkSid(){
	var xmlHttp = new_xmlHttp()
		 			
	xmlHttp.onreadystatechange = function(){
	if((xmlHttp.readyState == 4)){														
		var resp =  xmlHttp.responseText;							
			if(resp != "sidok"){
				document.location = "register.html";
			}						  				
		}
	}	
		 			
 	var url = server + "/WordServlet" + "?cmd=checksid" 
		+ "&cache=" + Math.random();			
		sendGetRequest(xmlHttp, url, true);	
	}
				
	function chechSubmit() {
		if (document.getElementById("school_list").value == "empty"){
			return;
		}
		resetSelect('tree_list');
		var charity = getCharityName("school_list");		
		playPage = "play.jsp?&charity=" 
			+ charity
			+ "&flash=money"
		letThemChoose();			
	}			
				
	function treeSubmit(){
		var charity = getCharityName("tree_list");
		var animType = "money"
		if(charity == "Arbor Day Foundation" 
			|| charity == "Trees for the Future"){
			animType = "tree"
		}
		resetSelect('school_list');
		playPage = "play.jsp?flash=" + animType 
		+ "&charity="					
		+ charity;
		letThemChoose();				
	}
	
	function resetSelect(elemetnId){
		document.getElementById(elemetnId).selectedIndex = 0;		
	}
			
	function letThemChoose(){
		document.getElementById("ch_div").style.visibility = "visible";
	}
			
	function playNow(){
		window.location = playPage;
	}	
	
	function downloadToolbar(){
		if(!checkBrowser()){
			alert("We're sorry but the browser you are using is currently unsupported");
			return;
		}
		if(toolbarId){
			openToolbarInstr();
		}else{
			alert("We don't have toolbar id for this charity!");
		}		
	}
	
	function doDownload(popup){
		popup.close();
		window.location = freelanthropyUrl + toolbarId;
	}
	
	function getToolbarUrl(){
		return freelanthropyUrl + toolbarId;
	}
	
	function getCharityName(chType){
		var chty = document.getElementById(chType).value;		
		var arr = chty.split(",");
		if(arr.length > 2){
			toolbarId = arr[arr.length-1];
			var index = chty.lastIndexOf(",");
			return chty.substring(0, index);			
		}else{
			toolbarId = null;
		}
		return chty;
	}	
	
	function checkBrowser(){
		var browser = navigator.userAgent.toLowerCase();
		return browser.indexOf("firefox") != -1 || browser.indexOf("msie") != -1;  
	}
	
	function openToolbarInstr(){
		popup = window.open ("htm/toolBar.htm", 
			"instr","location=1,status=0,scrollbars=1,width=500,height=600");
		popup.moveTo(200,200);
		popup.prt = this;
	}
	function closePopup(popup){
		popup.close();
	}	
			
