//this method uses AJAX to get a form, and then writes it into the HTML
function getSearchForm(lfg, gameName, server, faction, classname, gtags, language, playtime, timezone){
	//alert(gameName);
	/*
	gameName = decodeURI(gameName);
	gameName = decodeURI(server);
	gameName = decodeURI(faction);
	gameName = decodeURI(classname);
	gameName = decodeURI(gtags);*/
	//alert(language);
	
	document.getElementById('guildRecruitmentSearchForm').innerHTML = '<center><img src="/javascript/spinner.gif" height="20" width="20">Loading...</center>';
 	
	
 	var url = '/GuildRecruitmentSearchForm.php?lfg=' + lfg + '&gamename=' + gameName + '&server=' + server + '&faction=' + faction + '&classname=' + classname + '&gtags=' + gtags + '&language=' + language + '&playtime=' + playtime + '&timezone=' + timezone;
 	//alert(url);	
  new Ajax.Updater('guildRecruitmentSearchForm', url, {asynchronous:true, evalScripts:true});	
  
}

/* used by the games autocompeter, once a game has been selected */
function search_set_game(){
   var url = '/autocomplete/game_seo_name.php?game_id=' + $('search_game_select_game_id').value;
   
   new Ajax.Request(url, 
       {
        asynchronous:true,
        evalScripts:true,
        onSuccess: function(transport) {
          if (transport.responseText.match(/-1/)){
                        
          }           
          else{
            $('game_name_field').value = transport.responseText;
            updateSearchForm();
            
          }
        },
        onLoading: function(){
          $('search_game_select_autocomplete_loading').show();
        },        
        onComplete: function(){
          $('search_game_select_autocomplete_loading').hide();
        }
    });
}  

//This method reads the values of the form, and re-generates the search form
function updateSearchForm(){
	//alert('called');
	var lfg = 0;
	var gameName = "";
	var serverName = "";
	var factionName = "";
	var className = "";
	var gtags = '';
	var language = "";
	var playtime = "";
	var timezone = "";
	
	
	var lfginput = document.forms["grSearchForm"].elements["lfg"];	
	if (lfginput != null){
		lfg = lfginput.value;
	}
	

	gameName = $('game_name_field').value;

	var selectServer = document.forms["grSearchForm"].elements["servername"];	
	if (selectServer != null){
		if (selectServer.options != null){
			serverName = selectServer.options[selectServer.selectedIndex].value;
		}
	}
	
	var selectFaction = document.forms["grSearchForm"].elements["factionName"];	
	if (selectFaction != null){
		if (selectFaction.options != null){
			factionName = selectFaction.options[selectFaction.selectedIndex].value;
		}
	}
	
	var selectClass = document.forms["grSearchForm"].elements["className"];	
	if (selectClass != null){
		if (selectClass.options != null){
			className = selectClass.options[selectClass.selectedIndex].value;
		}
	}
	
	
	
	//hardcoded that there are 17.. but easier than passing in yet another variable.
	for (var i=0; i < 17; ++i){
		var gtag = document.forms["grSearchForm"].elements["gtag_" + i];
		if (gtag != null && gtag.checked){
			if (gtags == ''){
				gtags = gtag.value;
			}
			else{
				gtags += '_' + gtag.value;
			}
		}		
	}
	
	var selectLanguage = document.forms["grSearchForm"].elements["language"];	
	if (selectLanguage != null){
		if (selectLanguage.options != null){
			language = selectLanguage.options[selectLanguage.selectedIndex].value;
		}
	}
	
	var selectPlaytime = document.forms["grSearchForm"].elements["playtime"];	
	if (selectPlaytime != null){
		if (selectPlaytime.options != null){
			playtime = selectPlaytime.options[selectPlaytime.selectedIndex].value;
		}
	}
	
	var selectTimezone = document.forms["grSearchForm"].elements["timezone"];	
	if (selectTimezone != null){
		if (selectTimezone.options != null){
			timezone = selectTimezone.options[selectTimezone.selectedIndex].value;
		}
	}
	
		 
	getSearchForm(lfg, gameName, serverName, factionName, className, gtags, language, playtime, timezone);		
}

function submitRecruitmentSearchForm(){
	
	var lfg = 0;
	var gameName = "";
	var serverName = "";
	var factionName = "";
	var className = "";
	var language = "";
	var playtime = "";
	var timezone = "";
		
	
	
	var lfginput = document.forms["grSearchForm"].elements["lfg"];
	if (lfginput != null){
		lfg = lfginput.value;
	}
	
	gameName = $('game_name_field').value;
  
  
	var selectServer = document.forms["grSearchForm"].elements["servername"];	
	if (selectServer != null){
		if (selectServer.options != null){
			serverName = selectServer.options[selectServer.selectedIndex].value;
		}
	}
	
	var selectFaction = document.forms["grSearchForm"].elements["factionName"];	
	if (selectFaction != null){
		if (selectFaction.options != null){
			factionName = selectFaction.options[selectFaction.selectedIndex].value;
		}
	}
	
	var selectClass = document.forms["grSearchForm"].elements["className"];	
	if (selectClass != null){
		if (selectClass.options != null){
			className = selectClass.options[selectClass.selectedIndex].value;
		}
	}
	
	
	var gtags = '';
	
	//hardcoded that there are 17.. but easier than passing in yet another variable.
	for (var i=0; i < 17; ++i){
		var gtag = document.forms["grSearchForm"].elements["gtag_" + i];
		if (gtag != null && gtag.checked){
			if (gtags == ''){
				gtags = gtag.value;
			}
			else{
				gtags += '_' + gtag.value;
			}
		}		
	}
	
	
	var selectLanguage = document.forms["grSearchForm"].elements["language"];	
	if (selectLanguage != null){
		if (selectLanguage.options != null){
			language = selectLanguage.options[selectLanguage.selectedIndex].value;
		}
	}
	
	var selectPlaytime = document.forms["grSearchForm"].elements["playtime"];	
	if (selectPlaytime != null){
		if (selectPlaytime.options != null){
			playtime = selectPlaytime.options[selectPlaytime.selectedIndex].value;
		}
	}
	
	var selectTimezone = document.forms["grSearchForm"].elements["timezone"];	
	if (selectTimezone != null){
		if (selectTimezone.options != null){
			timezone = selectTimezone.options[selectTimezone.selectedIndex].value;
		}
	}
	
	
	//now we need to build a url based on these pieces.
	
	
	if (lfg == 1){
		var url ="/lfg";
	}
	else{
		var url ="/recruitment";
	}
	
	if (gameName){
		url += "/" + gameName;
	}
	else{
		url += "/all";
	}
	
	if (serverName){
		url+= "/" + serverName;
	}
	else{
		url += "/all";
	}
	
	if (factionName){
		url+= "/" + factionName;
	}
	else{
		url += "/all";
	}
	
	if (className){
		url+= "/" + className;
	}
	else{
		url += "/all";
	}	
	
	if (gtags != null && gtags != ''){
		url += "/" + gtags;
	}
	else{
		url += "/all";
	}	
	
	
	if (language){
		url += "/" + language;
	}
	else{
		url += "/all";
	}
	
	if (playtime){
		url += "/" + playtime;
	}
	else{
		url += "/0";
	}
	
	if (timezone){
		url += "/" + timezone;
	}
	else{
		url += "/-100";
	}	
	
	//alert(url);
	window.location = url;	
}
