
function system_lookup_locations(lookuptype, id_to_lookup, list_size) {

	//det the id of the country or state we're looking for sub components in
    var country_state_city_id = document.getElementById(id_to_lookup).value;	

    //construct the URL we're going to be querying
    var url = "system_lookup_locations.php?lookup_type_id=" + lookuptype + "&lookup_id=" + country_state_city_id + "&list_size=" + list_size;
	
    //amend the name of the dive that'll be updated
    lookuptype = lookuptype + '_div';
    //	query the server
	query_server(url, lookuptype);
	
}

function checkTime(i)
{
if (i<10)
  {
  i="0" + i;
  }
return i;
}

function return_home(){
	//load the home page
	window.location.assign("index.php");
}

function site_visitor_details(filtertype, id_to_lookup) {
	
	//	grab the time period from the submitting page
	var time_period = document.getElementById(id_to_lookup).value;
	
	//add the filter type to the url we'll be querying
	var url = "site_visitor_details_scripts.php?filter_type=" + filtertype;

	//create a new date object
	var today = new Date();
	// start constructing the date that will be shown on the form
	var y = today.getFullYear();
	var mo = today.getMonth();
	var d = today.getDay();
	var h = today.getHours();
	var m = today.getMinutes();
	var s= today.getSeconds();

	mo=checktime(mo);
	d=checktime(D);	
	h=checktime(h);
	m=checktime(m);
	s=checktime(s);	
	
	mydate = y + "/" + mo + "/" + d + " " + h + ":" + m + ":" + s;
	
	//finish constructing  the URL we're sending to the server
	url = url + "&time_period=" + time_period + "&refresh=" + mydate;
	
	//query the server
	query_server(url, "results");	
}



function query_server(url, element_to_update) {
//	alert("query_server.  url = '" + url + "'. element_to_update = '" + element_to_update + "'");

	//initiate the request
	var ajaxRequest;  // The variable that makes Ajax possible!

	//figure out which browser we're using so we know which Ajax object to create
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				return false;
			}
		}
	}

	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
//		alert("ready state: " + ajaxRequest.readyState);
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById(element_to_update);
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
		}
	}	

	ajaxRequest.open("GET", url, true);
	ajaxRequest.send(null);			
	
}


function tournament_quick_filter(calling_control) {

	var url = null;
	// Grab data from the form to use in the filter
	
	//grab the country id
		var country_id = document.getElementById("country_id").value;
	//grab the state id
		var state_id = document.getElementById("state_id").value;
	//grab the city id
		var city_id = document.getElementById("city_id").value;
	//grab the game system id
		var game_system_id = document.getElementById("game_system_id").value;
	//grab month from

		var tournament_type_id = document.getElementById("tournament_type_id").value;		
		var month_from = document.getElementById("month_from").value;
//	alert("Month_from: "  + month_from);
	//grab year from
		var year_from = document.getElementById("year_from").value;
//	alert("Year_From: "  + year_from);
	//grab month to
		var month_to = document.getElementById("month_to").value;
//	alert("Month_to: "  + month_to);
	//grab year to
		var year_to = document.getElementById("year_to").value;
//	alert("Year_to: "  + year_to);
		/**/	
//	alert("Calling control is: "  + calling_control);

		
	//check which control called this function
	switch(calling_control)
	{	
	case "country_id":
		// if it was country_id
        // refilter states list based on selected country
		state_id = "NULL";
		city_id = "NULL";
		url = "system_tournaments_filter_states.php?";
		
		url = url + "country_id=" + country_id;
		query_server(url, "state_id_div");

	     var city_list_contents = '<h4>City:</h4><select class="SearchDDL" id="city_id" name="city_id" onchange="tournament_quick_filter(\'city_id\')">';
	     city_list_contents = city_list_contents + '<option value="NULL">________________</option></select>';        
		 var control_to_update = document.getElementById("city_id_div");
		 control_to_update.innerHTML = city_list_contents;		 
		
		break;
	
	case "state_id":
		// if it was state _id
//		alert("case is state_id");
		city_id = "NULL";
////		url = "system_lookup_locations.php?lookup_type_id=city_id";
		url = "system_tournaments_filter_cities.php?";
//		url = url + "&lookup_id=" + state_id;
//		url = url + "dummy=" + dummy_value;
		url = url + "state_id=" + state_id;
//		alert(url);
		// refilter cities list based on selected state
//		query_server(url, "city_id");
		query_server(url, "city_id_div");
		
		  break;	

	default:
		//else
		//no other actions to perform so we'd do the actions that are after this switch
//		alert("case is otherwise");
	}	
	
		var url2 = "system_tournaments_filter.php?";
//		url = url + "dummy=" + dummy_value;
//		alert(url2);
		url2 = url2 + "country_id=" + country_id + "&state_id=" + state_id + "&city_id=" + city_id;  
//        alert("url2 with location details" + url2);
        url2 = url2 + "&game_system_id=" + game_system_id + "&tournament_type_id=" + tournament_type_id; 
//        alert("url2 game system and tournament type" + url2);
        url2 = url2 + "&month_from=" + month_from + "&month_to=" + month_to + "&year_from=" + year_from + "&year_to=" + year_to; 
//		alert("final url2" + url2);
                
		query_server(url2, "mainContent");
//        alert("tournaments query sent");
        
}



function tournament_quick_filter_edit(calling_control) {

	var url = null;
	var dummy_value = new Date().getTime;
	
	alert("Calling Control: " + calling_control);
	
	// Grab data from the form to use in the filter
	
	//grab the country id
		var country_id = document.getElementById("country_id").value;
//	alert("Country_id: "  + country_id);
	//grab the state id
		var state_id = document.getElementById("state_id").value;
//	alert("State_id: "  + state_id);
	//grab the city id
		var city_id = document.getElementById("city_id").value;
//	alert("City_id: "  + city_id);
	//grab the game system id
		var game_system_id = document.getElementById("game_system_id").value;
//	alert("Game_System_id: "  + game_system_id);
	//grab month from

		var tournament_type_id = document.getElementById("tournament_type_id").value;		
		var month_from = document.getElementById("month_from").value;
//	alert("Month_from: "  + month_from);
	//grab year from
		var year_from = document.getElementById("year_from").value;
//	alert("Year_From: "  + year_from);
	//grab month to
		var month_to = document.getElementById("month_to").value;
//	alert("Month_to: "  + month_to);
	//grab year to
		var year_to = document.getElementById("year_to").value;
//	alert("Year_to: "  + year_to);
		/**/	
//	alert("Calling control is: "  + calling_control);

		
	//check which control called this function
	switch(calling_control)
	{	
	case "country_id":
		// if it was country_id
        // refilter states list based on selected country
		state_id = "NULL";
		city_id = "NULL";
//		alert("about to find id of referring element: " + document.getElementById(id_to_lookup).value);
//	    var country_state_city_id = document.getElementById(id_to_lookup).value;	
//		alert("country_state_city_id: " + country_id);
	    //var url = "system_lookup_locations.php?lookup_type_id=state_id";
////		url = "system_lookup_locations.php?lookup_type_id=state_id";
		url = "system_tournaments_filter_states.php?";
		url = url + "country_id=" + country_id;
//		alert(url);
		

		query_server(url, "state_id_div");
	

		// null out the city list
/////        var city_list_contents = '<option value="NULL">________________</option>';
	     var city_list_contents = '<h4>City:</h4><select class="SearchDDL" id="city_id" name="city_id" onchange="tournament_quick_filter(\'city_id\')">';
	     city_list_contents = city_list_contents + '<option value="NULL">________________</option></select>';
alert(city_list_contents);	     
	     
	     
		
//		alert(city_list_contents);
        
/////		 var control_to_update = document.getElementById("city_id");
		 var control_to_update = document.getElementById("city_id_div");	    	 
alert(control_to_update);
		 control_to_update.innerHTML = city_list_contents;		 
		
		break;
	
	case "state_id":
		// if it was state _id
//		alert("case is state_id");
		city_id = "NULL";
////		url = "system_lookup_locations.php?lookup_type_id=city_id";
		url = "system_tournaments_filter_cities.php?";
//		url = url + "dummy=" + dummy_value;
//		url = url + "&lookup_id=" + state_id;
		url = url + "state_id=" + state_id;
//		alert(url);
		// refilter cities list based on selected state
//		query_server(url, "city_id");
		query_server(url, "city_id_div");
		
		  break;	

	default:
		//else
		//no other actions to perform so we'd do the actions that are after this switch
//		alert("case is otherwise");
	}	
	
		var url2 = "tournament_edit_filter_tournaments.php?";
//		url = url + "dummy=" + dummy_value;
//		alert(url2);
		url2 = url2 + "country_id=" + country_id + "&state_id=" + state_id + "&city_id=" + city_id;  
//        alert("url2 with location details" + url2);
        url2 = url2 + "&game_system_id=" + game_system_id + "&tournament_type_id=" + tournament_type_id; 
//        alert("url2 game system and tournament type" + url2);
        url2 = url2 + "&month_from=" + month_from + "&month_to=" + month_to + "&year_from=" + year_from + "&year_to=" + year_to; 
//		alert("final url2" + url2);
                
		query_server(url2, "mainContent");
        alert("tournaments query sent");
        
}

function country_address_template_entered_components(country)
{
	//function used to find & return the components selected
	//already for an adddress and update the page without the need for
	//a reload

	//find the country_id
	var country_id = document.getElementById(country).value;
	//construct the URL
	var url = "administration_address_country_template_entered_components.php?country_id=" + country_id;
	//send request to the server	
	query_server(url, "entered_components");	
}

