
var f_index = -1;
var s_index = -1;

// launches link in new page sized 600 x 450
function Start(page) {
	OpenWin = this.open(page, "CtrlWindow", "toolbar=yes,location=no,status=no,menubar=yes,directories=no,scrollbars=yes,resizable=yes,width=600,height=450");
}

//Returns a random integer between min and max
//Using Math.round() will give you a non-uniform distribution!
function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1) + min);
}

function nextItem(which, index){
	
	if (which == "funFact"){
		if (index == -1)
			index = default_fact_index;
		
		if ((index + 1) <= (Facts.length -1))
			f_index = index + 1;
		else
			f_index = 0;
		
		text = Facts[f_index];
		
	}
	else if (which == "statistics"){
		if (index == -1)
			index = default_stats_index;
		
		if ((index + 1) <= (Stats.length -1))
			s_index = index + 1;
		else
			s_index = 0;
		
		text = Stats[s_index];
		
	}
	
	var d = document.getElementById(which);
		d.innerHTML = text;
	
}


function makeActive(tab){
	
	//grab reference to both menus and the tab images
	sport_menu  = document.getElementById("sportsmenu");
	topic_menu  = document.getElementById("topicsmenu");
	sports_tab  = document.getElementById("sports_tab");
	topics_tab  = document.getElementById("topics_tab");
	
	//swap out the images and display the correct select box
	//as welll as clear any selections made on now hidden element
	if (tab == "topic"){
		sport_menu.style.display = "none";
		topic_menu.style.display = "inline";
		topics_tab.src = "/img/topictab_selected.gif";
		sports_tab.src = "/img/sporttab_active.gif";
		sport_menu.selectedIndex = -1;
	}
	else if (tab == "sport"){
		sport_menu.style.display = "inline";
		topic_menu.style.display = "none";
		topics_tab.src = "/img/topictab_active.gif";
		sports_tab.src = "/img/sporttab_selected.gif";
		topic_menu.selectedIndex = -1;
	}
	
}

function clearSearch(which){
	
	//grab all the form elements
	if (which == "top"){
		key  = document.getElementById("SearchKeywords1");
		img  = document.getElementById("imagesonly");
	}
	else{
		sport_menu  = document.getElementById("sportsmenu");
		topic_menu  = document.getElementById("topicsmenu");
		key = document.getElementById("SearchKeywords2");
		img  = document.getElementById("imagesonly2"); 
		showActiveTab(true, false);
		sport_menu.selectedIndex = -1;
		topic_menu.selectedIndex = -1;
	}
	
	//clear all the values for the appropriate section
	//of the form
	key.value = "";	
	img.checked = false;
	removeError();
	
}


function setBoxFocus(){	
	//for comment on image and order image page, set focus to comment field
	if(document.getElementById('comments')){
		document.getElementById('comments').focus();
	}
	
	//if we showed them an error message kill it
	removeError();

}

function removeError(){
	
	//delete it if it exists otherwise do nothing
	if(isPresent('err_message')){
		//grab its parent
		var err = $('err_target');
		var message = $('err_message');
		err.removeChild(message);
	}
	
}

function validate(){
	
	//grab our keyword variables
	var t_search = $F('SearchKeywords1');
	var b_search = $F('SearchKeywords2');
	var topics   = $F('topicsmenu');
	var sports   = $F('sportsmenu');
	
	//we cant allow everything to be 
	if (t_search  == "" && b_search == "" && topics == "" && sports == ""){
		 if(!$('err_message'))
		 	new Insertion.Top('err_target', '<span id="err_message">You must enter a Keyword or choose a Sport or Topic</span>');
		return false;
	}
	else{
		removeError();
		return true;
	}
}

function isPresent(element){

	if (document.getElementById(element))
		return true;
	else
		return false;
	
}

function showActiveTab(sport, topic){
	
	//grab reference to both menus and the tab images
	sport_menu  = document.getElementById("sportsmenu");
	topic_menu  = document.getElementById("topicsmenu");
	sports_tab  = document.getElementById("sports_tab");
	topics_tab  = document.getElementById("topics_tab");
	
	//swap out the images and display the correct select box
	//as welll as clear any selections made on now hidden element
	if (topic){
		sport_menu.style.display = "none";
		topic_menu.style.display = "inline";
		topics_tab.src = "/img/topictab_selected.gif";
		sports_tab.src = "/img/sporttab_active.gif";
	}
	if (sport){
		sport_menu.style.display = "inline";
		topic_menu.style.display = "none";
		topics_tab.src = "/img/topictab_active.gif";
		sports_tab.src = "/img/sporttab_selected.gif";
	}
	
}