// JavaScript Document

// START: NEW DROP DOWNS CATEGORIES SYSTEM
// START: NEW DROP DOWNS CATEGORIES SYSTEM
// START: NEW DROP DOWNS CATEGORIES SYSTEM

function create_dropdowns(dropdowns_name, number_of_dropdowns, absolute_address_server, dropdowns_label)
{	
	// creating drop_downs_list div		
	
	//var div_drop_downs_list = document.createElement("div");
	////div_drop_downs_list.setAttribute("id", "drop_downs_list");				
	//div_drop_downs_list.setAttribute("class", "category_drop_downs");

	// creating selected_category div			
	//var div_selected_category = document.createElement("div");
	//div_selected_category.setAttribute("id", "selected_category");						
	
	//div_drop_downs_list.appendChild(div_selected_category);
	
	//creating dropdowns
	/*
	var temp_name = "";
	for(var counter = 1; counter <= number_of_dropdowns; counter++)
	{
		//var div_category_drop_down = document.createElement("div");
		//div_category_drop_down.setAttribute("class", "category_drop_down");
		
		//var select_dropdown = document.createElement("select");
		//select_dropdown.setAttribute("id", dropdowns_name+counter);	
		//temp_name = dropdowns_name + (counter + 1);
					
		//select_dropdown.className = temp_name;
		
		//select_dropdown.onchange = function() 
		//{ 			
		//	update_category( this.options[this.selectedIndex].value, this.className, absolute_address_server);
		//};
		
		
		//div_category_drop_down.appendChild(select_dropdown);
		//div_drop_downs_list.appendChild(div_category_drop_down);
	}
	*/
	//$("search_filters").appendChild(div_drop_downs_list);
	
	//alert(number_of_drop_downs);
		
	if(number_of_drop_downs < 1) 
  { 	  
  		$(dropdowns_name + "1").parentNode.style.display = "none";
	}
	
	if(number_of_drop_downs < 2) 
  { 	  
  		$(dropdowns_name + "2").parentNode.style.display = "none";
	}
	
	if(number_of_drop_downs < 3) 
  { 	  		
  		$(dropdowns_name + "3").parentNode.style.display = "none";
	}
	
	if(number_of_drop_downs < 4) 
  { 	  
  		$(dropdowns_name + "4").parentNode.style.display = "none";
	}
	
	update_category(1, dropdowns_name+"1", absolute_address_server, dropdowns_label);		
}


function update_category_box(response, dropdown_id, dropdowns_label)
{
	if(response != "final")
	{				
		var category_list = eval('(' + response + ')');
		
		populate_dropdown(category_list, dropdown_id, dropdowns_label);
	}
	else
	{
		clear_dropdowns(dropdown_id);
	}
}

function update_category(category_id, next_dropdown_id, absolute_address_server, dropdowns_label)
{				
	if($(next_dropdown_id))
	{		
		clear_dropdowns(next_dropdown_id);						
			
		if(category_id != "choose")
		{						
			var choose = document.createElement("option");
			choose.setAttribute("value", "choose");	
			choose.innerHTML = "...Loading...";
			
			$(next_dropdown_id).appendChild(choose);
			
			var ajax = new ajax_file();
			ajax.initialize();			
			ajax.asynchronous = true;
		
			response = ajax.get_content(absolute_address_server + "elements/category/subcategory_details.html", "category=" + category_id, "update_category_box(my_response, '" + next_dropdown_id + "', '" + dropdowns_label + "'); ");
		}		
	}

	var new_cat_choosen = "";
	if(category_id != "choose" && category_id != 1) //category_id = 1 <-- master live category
	{		
		new_cat_choosen = category_id;
	}
	else if(category_id == "choose")
	{
	   var dropdown_name = next_dropdown_id.match("[a-z_-]*");
	   var next_dropdown_number = next_dropdown_id.match("[0-9]+") * 1;	   
	   var this_dropdown_number = next_dropdown_number - 1;
	   var previous_dropdown_number = this_dropdown_number - 1;
	   
	   if(previous_dropdown_number > 0)
	   {
		   var previous_dropdown = dropdown_name + previous_dropdown_number;
		   if( $(previous_dropdown) )
		   {
			   new_cat_choosen = $(previous_dropdown).options[$(previous_dropdown).selectedIndex].value;			   
		   }
	   }
	}
	

	// RESET INPUT "cat[]"; for set it with the new category selected
	//$("drop_downs_list").removeChild( $("selected_category") );

	// creating selected_category div			
	// var div_selected_category = document.createElement("div");
	// div_selected_category.setAttribute("id", "selected_category");				
	// $("drop_downs_list").appendChild(div_selected_category);

	// var selected_category_input = document.createElement("input");
	// selected_category_input.setAttribute("type","hidden");				
	// selected_category_input.setAttribute("name","cat[]");				
	// selected_category_input.setAttribute("value",new_cat_choosen);		
	//$("selected_category").appendChild(selected_category_input); 	
	
	if( $("selected_category") )
	{
		$("selected_category").value = new_cat_choosen;
	}
	
	if( $("selected_category_module") )
	{
		$("selected_category_module").value = new_cat_choosen;
	}
}

function populate_dropdown(category_list, dropdown_id, dropdowns_label)
{	
	html = '';
	$(dropdown_id).innerHTML = "";
	
	if(!dropdowns_label || dropdowns_label == 'undefined')
	{
		dropdowns_label = "- Choose -";
	}
	
	var choose = document.createElement("option");
	choose.setAttribute("value", "choose");	
	choose.innerHTML = dropdowns_label;
	
	$(dropdown_id).appendChild(choose);
	
	for(var counter=0; counter < category_list.length; counter++)
	{
		var option = document.createElement("option");
		option.setAttribute("value", category_list[counter][0]);	
		option.setAttribute("class", category_list[counter][1]);	
		option.innerHTML = category_list[counter][1];
		
		$(dropdown_id).appendChild(option);
		
	}
		
	$(dropdown_id).disabled = false;			
}
	      
function clear_dropdowns(dropdown_id)
{  
  var drop_down_number = dropdown_id.match("[0-9]+") * 1;
  var drop_down_name = dropdown_id.match("[a-z_-]*");
  
  for(var counter = drop_down_number; counter <= number_of_drop_downs; counter++)
  {
    $(drop_down_name + counter).innerHTML = "";
    $(drop_down_name + counter).disabled = true;    
  }
}
//END: NEW DROP DOWNS CATEGORIES SYSTEM
//END: NEW DROP DOWNS CATEGORIES SYSTEM
//END: NEW DROP DOWNS CATEGORIES SYSTEM






/*
function decode_des(text)
{
	//alert(text);
	text = text.replace("&#169;", "&copy;");	
	text = text.replace("&#62;", "&gt;");	
	text = text.replace("&#60;", "&lt;");
	text = text.replace("&#170;", "&not;");
	text = text.replace("&#38;", "&amp;");
	text = text.replace("&#32;", "&nbsp;");
	text = text.replace("&#34;", "&quot;");
	text = text.replace("&#163;", "&pound;");	
	//alert(text);
	return text;
}

function addOnChange(obj, drop_down_number)
{
	obj.onchange = function()
	{
		update_drop_down(drop_down_number, this.value);
	};
}


function update_drop_down(drop_down_to_update, ref_value)
{	
	// REMOVE OPTIONS; in those drop downs that follows the drop down to be updated	
	var drop_down_number = drop_down_to_update;			
	for(drop_down_number; drop_down_number <= number_of_drop_downs; drop_down_number++)
	{
		document.getElementById("drop_down_"+drop_down_number).options.length = 1;
	}				
	
	// FINDING SUBTREE; going trough category_tree_array finding the subtree position
	var temp_array = category_tree_array;
	var cat_path;	
	ref_value_arr = ref_value.split("-");
	//alert(ref_value);
	//alert(ref_value_arr);

	for(var key = 0; key < ref_value_arr.length; key++)
	{
		cat_path = ref_value_arr[key];
		temp_array = temp_array[cat_path];
	}
	
	// RESET INPUT "cat[]"; for set it with the new category selected
	document.getElementById("drop_downs_list").removeChild( document.getElementById("selected_category") );

	// creating selected_category div			
	div_selected_category = document.createElement("div");
	div_selected_category.setAttribute("id", "selected_category");				
	document.getElementById("drop_downs_list").appendChild(div_selected_category);

	var selected_category_input = document.createElement("input");
	selected_category_input.setAttribute("type","hidden");				
	selected_category_input.setAttribute("name","cat[]");				
	selected_category_input.setAttribute("value",temp_array[0][0]);		
	document.getElementById("selected_category").appendChild(selected_category_input); 
	
	temp_array = temp_array[1];

	//FILLING DROP DOWN
	var my_drop_down = document.getElementById("drop_down_"+drop_down_to_update);

	for(var key = 1; key < temp_array.length; key++)
	{ 
		if(typeof(temp_array[key][0]) != 'undefined')
		{
			drop_down_option = document.createElement('option');
			drop_down_option.text = temp_array[key][0][1];
			drop_down_option.value = ref_value+"-1-"+key;						
	
			try 
			{
				my_drop_down.add(drop_down_option, null); // standards compliant; doesn't work in IE
			}
			catch(ex) 
			{
				my_drop_down.add(drop_down_option); // IE only
			}			
		}
	}

}

// creating drop downs, from number_of_drop_downs global variable
function create_drop_downs(text_string, drop_downs_div_id, drop_down_box_style, text_box_style, input_box_style, select_box_style)
{
	//alert(category_tree_array[10][1][0][1]);
	//creating the first drop down
	var number = 1;
	var drop_down_to_update;
	
	// creating drop_down div
	var div_drop_down = document.createElement("div");
	div_drop_down.className = drop_down_box_style;

	// creating text div			
	var div_text = document.createElement("div");
	div_text.className = text_box_style;
	div_text.innerHTML = text_string;
	div_drop_down.appendChild(div_text);

	// creating input_box div			
	var div_input_box = document.createElement("div");
	div_input_box.className = input_box_style;				
	div_drop_down.appendChild(div_input_box);

	first_drop_down = document.createElement('select');
	first_drop_down.id = "drop_down_" + number;
	first_drop_down.className = select_box_style;
	drop_down_to_update = number + 1;

	addOnChange(first_drop_down, drop_down_to_update);

	drop_down_option = document.createElement('option');
	drop_down_option.text = "- Select -";
		
	try 
	{
		first_drop_down.add(drop_down_option, null); // standards compliant; doesn't work in IE
	}
	catch(ex) 
	{
		first_drop_down.add(drop_down_option); // IE only
	}		
	
	// filling the first drop down with the main categories; taken from category_tree_array
	for(var key = 1; key < category_tree_array.length; key++)
	//for(key in category_tree_array)
	{ 
		drop_down_option = document.createElement('option');
		
		if(typeof(category_tree_array[key][0]) != 'undefined')
		{			
			drop_down_option.text = category_tree_array[key][0][1];
			drop_down_option.value = key;
			
			try 
			{
				first_drop_down.add(drop_down_option, null); // standards compliant; doesn't work in IE
			}
			catch(ex) 
			{
				first_drop_down.add(drop_down_option); // IE only
			}			
		}		
	}

	div_input_box.appendChild(first_drop_down);
	document.getElementById(drop_downs_div_id).appendChild(div_drop_down);
					
	number++;

	// creating the drop downs left (empty drop downs)
	for(drop_down_number = number; drop_down_number <= number_of_drop_downs; drop_down_number++)
	{		
		// creating drop_down div
		div_drop_down = document.createElement("div");
		div_drop_down.className = drop_down_box_style;

		// creating text div			
		div_text = document.createElement("div");
		div_text.className = text_box_style;
		div_text.innerHTML = text_string;
		div_drop_down.appendChild(div_text);

		// creating input_box div			
		div_input_box = document.createElement("div");
		div_input_box.className = input_box_style;				
		div_drop_down.appendChild(div_input_box);
				
		new_drop_down = document.createElement('select');
		new_drop_down.id = "drop_down_" + drop_down_number;
		new_drop_down.className = select_box_style;
		drop_down_to_update = drop_down_number + 1;					
		//new_drop_down.setAttribute("onchange","update_drop_down("+drop_down_to_update+", this.value)");

		addOnChange(new_drop_down, drop_down_to_update);

		drop_down_option = document.createElement('option');
		drop_down_option.text = "- Select -";
			
		try 
		{
			new_drop_down.add(drop_down_option, null); // standards compliant; doesn't work in IE
		}
		catch(ex) 
		{
			new_drop_down.add(drop_down_option); // IE only
		}		
		
		div_input_box.appendChild(new_drop_down);
		document.getElementById(drop_downs_div_id).appendChild(div_drop_down);
	}
	
}
*/
