// preloaded button images
var radio_off = new Image;
var radio_on = new Image;
var checkbox_off = new Image;
var checkbox_on = new Image;

radio_off.src = "/images/wizard/radio_off.gif";
radio_on.src = "/images/wizard/radio_on.gif";
checkbox_off.src = "/images/wizard/checkbox_off.gif";
checkbox_on.src = "/images/wizard/checkbox_on.gif";


// set initial selection status
var input_status = new Array;

input_status["system_type"] = 2;
input_status["number"] = 4;
input_status["location"] = 0;
input_status["infrared"] = 0;
input_status["covert"] = 0;
input_status["tvout"] = 0;
input_status["remote_pc"] = 0;
input_status["remote_pda"] = 0;
input_status["remote_cell"] = 0;


// FUNCTION -  set radio button value
function radio_set(group, id) {
	var element;

	if (input_status[group] != id) {  // ignore clicks on selected radio
		// turn off old button
		element = "img_radio_" + group + "_" + input_status[group];
		document.getElementById(element).src = radio_off.src;

		// turn on new button
		input_status[group] = id;
		element = "img_radio_" + group + "_" + id;
		document.getElementById(element).src = radio_on.src;
	}

	if (group == "system_type") {
		if (input_status["system_type"] == 2) {
			document.getElementById("pc_only").style.visibility = "hidden";
		}
		else {
			document.getElementById("pc_only").style.visibility = "visible";
		}
	}

	// if user selects outdoor or mixed, infrared has to be selected
	if (group == "location") {
		if (input_status["location"] == 1  ||  input_status["location"] == 2) {
			if (input_status["infrared"] == 0) {
				checkbox_click("infrared");
			}
		}
	}
}

// FUNCTION - a radio button has been clicked
function radio_click(group, id) {
	radio_set(group, id);
	system_output();
}


// FUNCTION - a check box has been clicked
function checkbox_click(group) {
	var element;

	element = "img_checkbox_" + group;
	if (input_status[group] == 1) {  // was ON, toggle
		input_status[group] = 0;
		document.getElementById(element).src = checkbox_off.src;
	}
	else {  // was OFF, toggle
		input_status[group] = 1;
		document.getElementById(element).src = checkbox_on.src;
	}

	// if user selects remote_cell, select all remote connection options
	if (group == "remote_cell"  &&  input_status["remote_cell"] == 1) {
		if (input_status["remote_pc"] == 0) {
			checkbox_click("remote_pc");
		}
		if (input_status["remote_pda"] == 0) {
			checkbox_click("remote_pda");
		}
	}

	system_output();
}


// FUNCTION - select systems from a list based on user inputs
function system_select() {
	// clear lists
	for (var i = 0; i < select_best.length; i++) {
		select_best[i] = 0;
	}
	for (var i = 0; i < select_entry.length; i++) {
		select_entry[i] = 0;
	}
	for (var i = 0; i < select_standa.length; i++) {
		select_standa[i] =0;
	}

	if (input_status["system_type"] == 1) {
		// generate the select_best list
		for (var i = 0; i < select_best.length; i++) {
			// match a mixed package with or without infrared
			if (input_status["location"] == 2  &&
					list_best[i][5] == input_status["number"]  &&  list_best[i][6] == input_status["location"]  &&
					list_best[i][8] == input_status["covert"] && list_best[i][9] == input_status["system_type"]) {
				select_best[i] = 1;
			}
	
			// basic criteria - match number, location, infrared and covert exactly
			else if (list_best[i][9] == input_status["system_type"] && list_best[i][5] == input_status["number"]  &&  list_best[i][6] == input_status["location"]  &&
					list_best[i][7] == input_status["infrared"]  &&  list_best[i][8] == input_status["covert"]) {
				select_best[i] = 1;
			}
		}
	
		// generate the select_entry list
		for (var i = 0; i < select_entry.length; i++) {
			// can't match tvout or remote cell
			if (input_status["tvout"] == 0  &&  input_status["remote_cell"] == 0) {
				// match a mixed package with or without infrared
				if (input_status["location"] == 2  &&
						list_entry[i][5] == input_status["number"]  &&  list_entry[i][6] == input_status["location"]  &&
						list_entry[i][8] == input_status["covert"] && list_entry[i][9] == input_status["system_type"]) {
					select_entry[i] = 1;
				}
	
				// basic criteria - match number, location, infrared and covert exactly
				else if (list_entry[i][9] == input_status["system_type"] && list_entry[i][5] == input_status["number"]  &&  list_entry[i][6] == input_status["location"]  &&
						list_entry[i][7] == input_status["infrared"]  &&  list_entry[i][8] == input_status["covert"]) {
					select_entry[i] = 1;
				}
			}
		}
	}
	else {
		// generate the select_standa list
		for (var i = 0; i < select_standa.length; i++) {
			// match a mixed package with or without infrared
			if (input_status["location"] == 2  &&
					list_standa[i][5] == input_status["number"]  &&  list_standa[i][6] == input_status["location"]  &&
					list_standa[i][8] == input_status["covert"] && list_standa[i][9] == input_status["system_type"]) {
				select_standa[i] = 1;
			}
	
			// basic criteria - match number, location, infrared and covert exactly
			else if (list_standa[i][9] == input_status["system_type"] && list_standa[i][5] == input_status["number"]  &&  list_standa[i][6] == input_status["location"]  &&
					list_standa[i][7] == input_status["infrared"]  &&  list_standa[i][8] == input_status["covert"]) {
				select_standa[i] = 1;
			}
			
		}
	}
}


// FUNCTION - display the selected systems
function system_output() {
	var dhtml = "";
	var element;
	var best_qty;
	var entry_qty;
	var standa_qty;
	var i;

	// generate the selection lists
	system_select();

	// display at most, the top two priority 'best' systems
	best_qty = 0;
	i = 0;

	while (i < select_best.length  &&  best_qty < 99) {
		if (select_best[i] == 1) {
			dhtml += system_dhtml(list_best[i]);
			best_qty++;
		}
		i++;
	}

	// display at most, the top two priority 'entry' systems
	entry_qty = 0;
	i = 0;

	while (i < select_entry.length  &&  entry_qty < 99) {
		if (select_entry[i] == 1) {
			dhtml += system_dhtml(list_entry[i]);
			entry_qty++;
		}
		i++;
	}
	
	// display standalone systems
	standa_qty = 0;
	i = 0;
	
	while (i < select_standa.length  &&  standa_qty < 99) {
		if (select_standa[i] == 1) {
			dhtml += system_dhtml(list_standa[i]);
			standa_qty++;
		}
		i++;
	}
	
	if (dhtml == "") {
		dhtml = "<div align='center'><img src='../Images/Wizard/Broken_Wand_sml_100x45.jpg' alt='' height='45' width='100' align='absmiddle' border='0'><img src='../Images/Wizard/UhOh_375x89.jpg' alt='' height='89' width='375' align='middle' border='0'><br>You must have narrowed it down too far for the Automated Wizard!<br>	<b>But All Hope is not lost!</b><br><br>You can try a different combination above...<br><b>Or Call us at (866) 537-5438 to assemble a Custom Package!</b></div>";
	}
	else {
		dhtml += "<p><b>" + "The Automated CCTV System Wizard found <font size=4>" + (best_qty + entry_qty + standa_qty) + " </font>systems</p></b>";
		dhtml += "<font size=2 color='navy'><b>Need additional Assistance selecting a system?  Call Us Now Toll Free (866) 537-5438 </font></b>"
	}

	// display dhtml
	element = "output_system";
	document.getElementById(element).innerHTML = dhtml;
}


// FUNCTION - output the given system in html/table format
function system_dhtml(system) {
	var dhtml = "";

	dhtml += '<table width="601" border="4" cellpadding="5" cellspacing="0" frame="box" 	rules="all">';
	dhtml += '<colgroup align="left" valign="middle">';
	dhtml += '<col width="100" />';
	dhtml += '<col width="85" />';
	dhtml += '<col />';
	dhtml += '<col width="85" />';
	dhtml += '</colgroup>';
	dhtml += '<tr>';

	dhtml += '<td><a href="' + system[1] + '">';
	dhtml += '<img src="' + system[2] + '" width="100" height="139" alt="" /></a></td>';
	dhtml += '<td>' + system[0] + '</td>';
	dhtml += '<td><a href="' + system[1] + '">' + system[4] + '</a></td>';
	dhtml += '<td>' + system[3] + '</td>';

	dhtml += '</tr></table>';

	return dhtml;
}



