var selected_brand=-1;
var selected_family=-1;
var selected_series=-1;
var selected_link=-1;
var model_list = new Array();
var model_name_list = new Array();
var lock_update=0;

// Entry to create QF, selects the placement of QF
function create_chain_select() 
{
	var url = window.location.href;
	if(url.indexOf("http://www.topmic.com/laptop-ac-adapter-power-supply-charger-and-power-cord") != -1) {
			if(document.getElementById("QF_Center") != null)
				create_QF("QF_Center");
			else
				create_QF("QF_Top");
	}
	else {
		create_QF("QF_Top");
	}
}

// Actual Function to create QF in the desired place
// QFHolder is the div-tag's id that QF is placed in
function create_QF(QFHolder) {
	var innerHTML="";	
	innerHTML += "<table border=0 align=center background=\"http://site.topmic.com/search_engine/search_bg.gif\">";

	// Create the model filter text box
	innerHTML += "<tr><td colspan=3 align=\"left\"><font face=\"arial\" size=2>TopMic Laptop Charger QuickFinder</td></font>";
	innerHTML +="<td align=\"right\" colspan=2><font face=\"arial\" size=2>Model Filter: </font>";
	innerHTML +="<input value=\"\" disabled=\"disabled\" style=\"background-color:#999999;\" id=\"modelFilter\" autocomplete=\"off\" onkeyup=\"reLoadModels()\" size=\"20\" type=\"text\"></td></tr>";
	// Create the gifs
	innerHTML += "<tr><td width=25></td>";
	innerHTML += "<td align=center><img src=\"http://site.topmic.com/search_engine/choose_brand.gif\" alt=\"Choose Brand\"></img></td>";
	innerHTML += "<td align=center><img src=\"http://site.topmic.com/search_engine/choose_family.gif\" alt=\"Choose Family\"></img></td>";
	innerHTML += "<td align=center><img src=\"http://site.topmic.com/search_engine/choose_series.gif\" alt=\"Choose Series\"></img></td>";
	innerHTML += "<td align=center><img src=\"http://site.topmic.com/search_engine/choose_model.gif\" alt=\"Choose Model\"></img></td></tr>";
	
	// Create space right before the first select
	innerHTML += "<tr><td width=25></td>";
	
	// Create the div tag to hold brand Select and update the brand list
	innerHTML += "<td width=150><div id=\"brand_select\"><select size=5 style=\"width:150px;\"></select></div></td>";	
	
	// Create the div tag to hold empty family Select
	innerHTML += "<td width=150><div id=\"family_select\"><select size=5 style=\"background-color:#999999; width:150px;\"></select></div></td>";
	
	// Create the div tag to hold empty series select
	innerHTML += "<td width=150><div id=\"series_select\"><select size=5 style=\"background-color:#999999; width:150px;\"></select></div></td>";
	
	// Create the div tag to hold empty model select
	innerHTML += "<td width=150><div id=\"model_select\"><select size=5 style=\"background-color:#999999; width:150px;\"></select></div></td>";

	// Create the go-button
	innerHTML += "<td width=25><img src=\"http://site.topmic.com/search_engine/go-btn.gif\" onmouseover=\"this.style.cursor='pointer'\" onclick=\"jump_model()\"></img></td>";
	
	innerHTML += "</tr></table>";
	
	document.getElementById(QFHolder).innerHTML=innerHTML;
	update_brand();
}

// ****************************************
//  Functions to deal with model filter
// ****************************************
function reLoadModels()
{
	if(selected_series!=-1)
	{
		var filter_word = document.getElementById('modelFilter').value;
		filter_model(filter_word.toLowerCase());
	}
}

function filter_model (str)
{
	var innerHTML = "<select id=\"model\" size=5 style=\"width:150px\"; OnChange=\"update_link(this.options[this.selectedIndex].value)\">";
	
	//If there is no filter word, then display all options
	//   else only display options satisfying filter word
	if(str == ""){
		innerHTML+=model_list.join("");
	}
	else {
		var i;
		for(i in model_name_list)
		{
			if(model_name_list[i].indexOf(str) != -1)
				innerHTML+=model_list[i];
		}
	}
	
	innerHTML += "</select>";
	
	document.getElementById('model_select').innerHTML=innerHTML;
}

// *****************************************
//  Functions to reset different components
// ***************************************** 
function reset_family_select()
{
	document.getElementById('family_select').innerHTML="<select size=5 style=\"background-color:#999999; width:150px;\"></select>";
	selected_family=-1;
	reset_series_select();
}

function reset_series_select()
{
	document.getElementById('series_select').innerHTML="<select size=5 style=\"background-color:#999999; width:150px;\"></select>";
	selected_series=-1;
	reset_model_select();
}

function reset_model_select()
{
	document.getElementById('model_select').innerHTML="<select size=5 style=\"background-color:#999999; width:150px;\"></select>";
	selected_link=-1;
	model_list = new Array();
	model_name_list = new Array();
	reset_filter();
}

function reset_filter()
{
	var model_filter=document.getElementById('modelFilter');
	
	if(selected_series!=-1)
	{
		model_filter.disabled=false;
		model_filter.style.backgroundColor='#FFFFFF';
	}
	else
	{
		model_filter.disabled=true;
		model_filter.style.backgroundColor='#999999';
	}
	
	model_filter.value="";
}

// ********************************************
//  Functions to update different components
// ********************************************
function skip_family()
{
	var family = document.getElementById('family');
	var family_size = family.options.length;
	
	//if only 1 family is available, skip to series
	if(family_size == 1) {
		family.selectedIndex=0;
		update_series(family.options[0].value);
	}
}

function skip_series()
{
	var series = document.getElementById('series');
	var series_size = series.options.length;
	
	//if only 1 series is available, skip to model
	if(series_size == 1) {
		series.selectedIndex=0;
		update_model(series.options[0].value);
	}
}

function skip_model()
{
	var model = document.getElementById('model');
	var model_size = model.options.length;
	
	//if only 1 model is available, select the model
	if(model_size == 1)
	{
		model.selectedIndex=0;
		selected_link=model.options[0].value;
	}
}

function update_brand()
{
	//Removes Extra script tags from previous dynamic script load
	var headElement = document.getElementsByTagName("head")[0];
	
	var fileref=document.createElement('script');
	fileref.setAttribute("type","text/javascript");
	fileref.setAttribute("src", "http://site.topmic.com/search_engine/print_brand.php");
	
  	headElement.appendChild(fileref);
}

function update_family(brand_name)
{
	if(!lock_update)
	{
		lock_update=1;
		
		selected_brand=brand_name;
		reset_family_select();

		var fileref=document.createElement('script');
		fileref.setAttribute("type","text/javascript");
		fileref.setAttribute("src", 'http://site.topmic.com/search_engine/print_family.php?Brand='+selected_brand);
	
		//Removes Extra script tags from previous dynamic script load
		var headElement = document.getElementsByTagName("head")[0];
		headElement.removeChild(headElement.lastChild);
  		headElement.appendChild(fileref);
		
		lock_update=0;
	}
	else
	{
		thisObj=this;
		setTimeout(function() {thisObj.update_family(brand_name);},100);
	}
}

function update_series(family_name)
{
	if(!lock_update)
	{
		lock_update=1;
		
		selected_family=family_name;
		reset_series_select();

		var fileref=document.createElement('script');
		fileref.setAttribute("type","text/javascript");
		fileref.setAttribute("src", 'http://site.topmic.com/search_engine/print_series.php?Brand='+selected_brand+'&Family='+selected_family);
  	
		//Removes Extra script tags from previous dynamic script load
		var headElement = document.getElementsByTagName("head")[0];	
		headElement.removeChild(headElement.lastChild);
		headElement.appendChild(fileref);
		
		lock_update=0;
	}
	else
	{
		thisObj=this;
		setTimeout(function() {thisObj.update_series(family_name);},100);
	}
}

function update_model(series_name)
{
	if(!lock_update)
	{
		lock_update=1;
		
		selected_series=series_name;
		reset_model_select();
	
		var fileref=document.createElement('script');
		fileref.setAttribute("type","text/javascript");
		fileref.setAttribute("src", 'http://site.topmic.com/search_engine/print_model.php?Brand='+selected_brand+'&Family='+selected_family+'&Series='+selected_series);
  	
		//Removes Extra script tags from previous dynamic script load
		var headElement = document.getElementsByTagName("head")[0];
		headElement.removeChild(headElement.lastChild);
		headElement.appendChild(fileref);
		
		lock_update=0;
	}
	else
	{
		thisObj=this;
		setTimeout(function() {thisObj.update_model(series_name);},100);
	}
}

function update_link (url)
{
	selected_link=url;
}

function jump_model()
{
	if(selected_link==-1)
		alert("Please select a Brand, Family, Series, and Model before you go to the next page.");
	else
	{
		var add_count = new Image();
		add_count.onLoad=redirect();
		add_count.src='http://site.topmic.com/search_engine/addCount.php';
	}
}

function redirect()
{
	location=selected_link.toLowerCase();
}

