var current_location_type="loc";
var selectedLocationType="station";
var location_type="station";
var select_type="single";


function change_location_type(type_number){
	/*
	0 => Station 
	1 => Region
	2 => Custom Region
	3 => DP Custom Region
	6 => Saved planner
	*/
	//alert("type_number("+type_number+")");
	
	if(type_number == 0){
		location_type="station";
		$('#region_select').hide();
		$('#custom_region_select').hide();
		$('#planner_select').hide();
		$('#dp_custom_region_select').hide();
		$('#station_select').show();
		chgSelectSize('single');
	}
	else if(type_number == 1){
		location_type="region";
		
		$('#custom_region_select').hide();
		$('#station_select').hide();
		$('#planner_select').hide();
		$('#dp_custom_region_select').hide();
		$('#region_select').show();
		chgSelectSize('single');
	}
	else if(type_number == 2){
		location_type="custom_region";
		$('#region_select').hide();
		$('#station_select').hide();
		$('#planner_select').hide();
		$('#dp_custom_region_select').hide();
		$('#custom_region_select').show();
		chgSelectSize('single');
	}
	else if(type_number == 3){
		location_type="dp_custom_region";
		$('#region_select').hide();
		$('#station_select').hide();
		$('#planner_select').hide();
		$('#custom_region_select').hide();
		$('#dp_custom_region_select').show();
		chgSelectSize('single');
	}
	else if(type_number == 6){
		location_type="planner";
		$('#region_select').hide();
		$('#station_select').hide();
		$('#custom_region_select').hide();
		$('#dp_custom_region_select').hide();
		$('#planner_select').show();
		//chgSelectSize('single');
	}else{
		alert("Invalid type_number ("+type_number+") in change_location_type");
		return false;
	}
	
	//change_country();
	return;
	
}


function change_country(type_number){
	/*
	0 => Station 
	1 => Region
	2 => Custom Region	
	3 => DP Custom Region
	*/
	//alert("type_number("+type_number+")");
	
	//Find out which location we need to show and load first
	
	if(type_number == 0){
		location_type="station";
		//REMOVE$('div#debug').append('<br>change_country called for '+location_type+'<br>');
		//REMOVE$('div#debug').append('load_country for '+location_type+'<br>');
		load_country();
		
		country_value=check_country_value("station_country");
		
		location_type="region";
		if($('#region_country').containsOption(country_value)){
			$('#region_country').selectOptions(country_value,true);
			load_country();
		}
		
		location_type="custom_region";
		if($('#custom_region_country').containsOption(country_value)){
			$('#custom_region_country').selectOptions(country_value,true);
			load_country();
		}
		
		location_type="station";
	}
	else if(type_number == 1){
		location_type="region";
		//REMOVE$('div#debug').append('<br>change_country called for '+location_type+'<br>');
		//REMOVE$('div#debug').append('load_country for '+location_type+'<br>');
		load_country();
		
		country_value=check_country_value("region_country");
		if(currentPage && currentPage == 'demand_planner'){
			location_type="dp_custom_region";
			if($('#dp_custom_region_country').containsOption(country_value)){
				$('#dp_custom_region_country').selectOptions(country_value,true);
				load_country();
			}
			location_type="region";
		}
		else{
			location_type="station";

			if($('#station_country').containsOption(country_value)){
				$('#station_country').selectOptions(country_value,true);
				load_country();
			}

			location_type="custom_region";
			if($('#custom_region_country').containsOption(country_value)){
				$('#custom_region_country').selectOptions(country_value,true);
				load_country();
			}

			location_type="region";
		}
		
		
	}
	else if(type_number == 2){
		location_type="custom_region";
		load_country();
		//REMOVE$('div#debug').append('<br>change_country called for '+location_type+'<br>');
		//REMOVE$('div#debug').append('load_country for '+location_type+'<br>');
		
		country_value=check_country_value("custom_region_country");
		
		location_type="station";
		if($('#station_country').containsOption(country_value)){
			$('#station_country').selectOptions(country_value,true);
			load_country();

		}
		
		location_type="region";
		if($('#region_country').containsOption(country_value)){
			$('#region_country').selectOptions(country_value,true);
			load_country();
		}
		
		location_type="custom_region";
	}
	else if(type_number == 3){
		location_type="dp_custom_region";
		//load_country();
		
		country_value=check_country_value("dp_custom_region_country");
		
	}
	
}

function chgSelectSize(type){
	if(!type){
		//No type specified
	}
	else if(type == "single"){
		if(document.getElementById(location_type+'_location')){
			document.getElementById(location_type+'_location').size=1;
			document.getElementById(location_type+'_location').multiple="";
		}
		
		if(document.getElementById('multi_select_span')){
			document.getElementById('multi_select_span').innerHTML="<a href='javascript:void(0);' onclick=\"chgSelectSize('multiple');\">Select Multiple</a>";
		}
		
	}
	else if(type == "multiple"){
		if(document.getElementById(location_type+'_location')){
			document.getElementById(location_type+'_location').size=6;
			document.getElementById(location_type+'_location').multiple="multiple";
		}
		
		if(document.getElementById('multi_select_span')){
			document.getElementById('multi_select_span').innerHTML="<a href='javascript:void(0);' onclick=\"chgSelectSize('single');\">Select Single</a>";
		}
		
	}
	else{
		document.getElementById('multi_select_span').innerHTML="";
	}
	return 1;
}

function check_country_value(id){
	
	//Make sure ID is set
	if(id.length == 0){
		alert("id is not set");
		return false;
	}
	
	//Must be set, now pull the value of the ID
	selected_id=$("select#"+id+" option:selected");
	return $(selected_id).text();
	//alert($(selected_id).text());
	
}

function check_location_type(type){
	
	//See if "type" is even set
	if(typeof(type) == "undefined"){
		//Not set
		return false;
	}
	//See if it is a valid type
	if(type == "station" || type == "region" || type == "custom_region" || type == "dp_custom_region"){
		return true;
	}
	
	//If we got here the type must be invalid
	return false;
}

function check_state_value(id){
	
	//Make sure ID is set
	if(id.length == 0){
		alert("id is not set");
		return false;
	}
	
	//Must be set, now pull the value of the ID
	selected_id=$("select#"+id+" option:selected");
	return $(selected_id).text();
	
}

function load_country(){
	
	//Make sure a location_type is set

	if(typeof(location_type) == "undefined"){
		//location_type is not set for some reason so we will set it to station
		location_type="station";
		alert("stat");
	}
	
	if(!check_location_type(location_type)){
		//Invalid Type
		alert("type not valid");
		return false;
	}
	//alert("change_country");
	
	location_url="/ajax/locations.php";
	location_data="";
	country_value_id="";
	
	if(location_type == "station"){
		country_value_id="station_country";
		country_value=check_country_value("station_country");
		location_data="type=s_country&val="+country_value;
	}
	else if(location_type == "region"){
		country_value_id="region_country";
		country_value=check_country_value("region_country");
		location_data="type=r_country&val="+country_value;
	}
	else if(location_type == "custom_region"){
		country_value_id="custom_region_country";
		country_value=check_country_value("custom_region_country");
		location_data="type=cr_country&val="+country_value;
	}
	else if(location_type == "dp_custom_region"){
		country_value_id="dp_custom_region_country";
		country_value=check_country_value("dp_custom_region_country");
		location_data="type=dp_cr_country&val="+country_value;
	}
	
	if(!country_value){
		//Invalid country_value
		alert("country not valid");
		return false;
	}else{
		//location_data="type=s_country&val="+country;
	}
	//REMOVE$('div#debug').append('<br>URL=http://beta2.myskeye.com/'+location_url+'?'+location_data+'<br>');
	
	pull_location_data(location_url,location_data,"xml");

}

function load_states(){
	//Make sure a location_type is set

	if(!check_location_type(location_type)){
		//Invalid Type
		alert("type not valid");
		return false;
	}
	
	location_url="/ajax/locations.php";
	location_data="";
	state_value_id="";
	country_value_id="";
	
	if(location_type == "station"){
		//Pull Country Data
		country_value_id="station_country";
		country_value=check_country_value("station_country");
		location_data="type=s_state&c_val="+country_value;
		
		//Pull State Data
		state_value_id="station_state";
		state_value=check_state_value(state_value_id);		
		location_data+="&val="+state_value;
	}
	
	if(!country_value){
		//Invalid country_value
		alert("country not valid");
		return false;
	}else{
		//location_data="type=s_country&val="+country;
	}
	
	if(!state_value){
		//Invalid country_value
		alert("state not valid");
		return false;
	}else{
		//location_data="type=s_country&val="+country;
	}
	
	pull_location_data(location_url,location_data,"xml");
	
}

function pull_location_data(location_url,location_data,data_type){
	
	$.ajax({
		type: "GET",
		url: location_url,
		data: location_data,
		//dataType : 'xml',
		dataType : data_type,
		success: function(xml) {
			//First Check if an error exists
			error=$("error",xml);
			if($("error",xml).length == 0){
				//$('div#debug').append('<br>NO ERROR:<br>');				
			}else{
				alert("There was an error updating the list ("+$("number",error).text()+")");
				//REMOVE$('div#debug').append('<br>ERROR:<br>');
				//REMOVE$('div#debug').append('<br>location_data:'+location_data+'<br>');
				//REMOVE$('div#debug').append('<br>ERROR:<br>'+$("number",error).text());
			}	
			
			//$('div#debug').append('<br>URL=http://beta2.myskeye.com/'+location_url+'?'+location_data+'<br>');
			
			count=$("count",xml).text();
			locations=$("locations",xml);
			type=$("type",xml).text();
			
			//If the type is not states (only happens for the US) 
			//we should "hide" the states selection box.
			if(country_value == "United States" && type == "station_state"){
				$('#showState').show();
				$('#station_location').html("<option value=''>-Please Choose A State-</option>");
			}else if(country_value != "United States"){
				$('#showState').hide();
			}

			//REMOVE$('div#debug').append('<br>Count=<br>'+count);
			
			html="";
			var optionValue="";
			
			var locationTypeClass="";

			$('#'+type).html("<option value='' selected>-Please Choose One-</option>");
			$("location",locations).each(function(i) {
				if(type == "custom_region_location"){
					locationTypeClass="custom_region";
				}
				else if(type == "dp_custom_region_location"){
					locationTypeClass="dp_custom_region";
				}
				else if(type == "region_location"){
					locationTypeClass="region";
				}
				else if(type == "station_location"){
					locationType=$(this).attr('type');
					if(locationType === "custom_location"){
						locationTypeClass="custom_location";
					}
					else{
						locationTypeClass="station";
					}
				}
				optionValue=locationTypeClass+"|"+$(this).attr('id');
				//console.log('type='+type+' optionValue='+optionValue);
				//REMOVEhtml+="<option class=\""+locationTypeClass+"\" value=\""+$(this).attr('id')+"\">"+$(this).text()+"</option>";
				if(currentPage && (currentPage == "planner" || currentPage == "energy_planner")){
					if(useOptionValue && useOptionValue == 1){
						
					}
					else{
						optionValue=$(this).attr('id');
					}
					
				}
				html+="<option class=\""+locationTypeClass+"\" value=\""+optionValue+"\">"+$(this).text()+"</option>";
			});

			$('#'+type).append(html);
			
		}
	}); //END $.ajax
	
}

function check_location_id(id){
	var valid_location_regex=/^(\d+)$/;
	if(valid_location_regex.test(id)){
		return true;
	}
	//alert ("not valid");
	return false;
}
