//window.alert("Loading interface javascript file");

$(document).load(function () {
//	window.alert("The DOM is now loaded. disable type, make, brand, size and submit buttons.");
	$("select[name='type']").attr("disabled",true);
	$("select[name='make']").attr("disabled",true);
	$("select[name='brand']").attr("disabled",true);
	$("select[name='size']").attr("disabled",true);
	$("input[name='submit']").attr("disabled",true);
});

$("input[name='mounttype']").change(function(event){
	$("select[name='type']").html("<option>-- Select a type --</option>");
	$.getJSON("/findamount/dropdowns.asp?action=2a&lang="+Lang+"&mounttype="+$("input[name='mounttype']:checked").val(),
		function(data){
			$.each(data, function(i,item){
				$("select[name='type']").append($("<option></option>").text(item));
			});
			$("select[name='type']").removeAttr("disabled");	
		}
	);
	$("select[name='make']").attr("disabled",true);
	$("select[name='brand']").attr("disabled",true);
	$("select[name='size']").attr("disabled",true);
	$("input[name='submit']").attr("disabled",true);	
});

$("select[name='type']").change(function(event){
	$("select[name='make']").html("<option>-- Select a make --</option>");
	var url="/findamount/dropdowns.asp?action=2b&"
			+"lang="+Lang+"&"
			+"mounttype="+$("input[name='mounttype']:checked").val()+'&'
			+"type="+$("select[name='type']").val();
	$.getJSON(url,
		function(data){
			$.each(data, function(i,item){
				$("select[name='make']").append($("<option></option>").text(item));
			});
			$("select[name='make']").removeAttr("disabled");
		}
	);
	$("select[name='brand']").attr("disabled",true);
	$("select[name='size']").attr("disabled",true);
	$("input[name='submit']").attr("disabled",true);	
});

$("select[name='make']").change(function(event){
	$("select[name='brand']").html("<option>-- Select a brand --</option>");
	var url="/findamount/dropdowns.asp?action=2c&"
			+"lang="+Lang+"&"
			+"mounttype="+$("input[name='mounttype']:checked").val()+'&'
			+"type="+$("select[name='type']").val()+'&'
			+"make="+$("select[name='make']").val()+'&';
	$.getJSON(url,
		function(data){
			// How many?
			if ((data.length) == 1) {
				$.each(data, function(i,item) {
					$("select[name='brand']").html($("<option selected=selected></option>").text(item));
					$("select[name='size']").html("<option>-- Select a size --</option>");
					var url="/findamount/dropdowns.asp?action=3&"
					+"lang="+Lang+"&"
					+"mounttype="+$("input[name='mounttype']:checked").val()+'&'
					+"type="+$("select[name='type']").val()+'&'
					+"make="+$("select[name='make']").val()+'&'
					+"brand="+$("select[name='brand']").val()+'&';
					$.getJSON(url,
						function(data){
							$.each(data, function(i,item){
								$("select[name='size']").append($("<option></option>").text(item));
							});
							$("select[name='size']").removeAttr("disabled");
						}
					);
					$("input[name='submit']").attr("disabled",true);
				});
			} else {
				$.each(data, function(i,item){
					$("select[name='brand']").append($("<option></option>").text(item));
				});
			}

		}
	);
	$("select[name='brand']").removeAttr("disabled");
	$("select[name='size']").attr("disabled",true);
	$("input[name='submit']").attr("disabled",true);	
});

$("select[name='brand']").change(function(event){
	$("select[name='size']").html("<option>-- Select a size --</option>");
	var url="/findamount/dropdowns.asp?action=3&"
			+"lang="+Lang+"&"
			+"mounttype="+$("input[name='mounttype']:checked").val()+'&'
			+"type="+$("select[name='type']").val()+'&'
			+"make="+$("select[name='make']").val()+'&'
			+"brand="+$("select[name='brand']").val()+'&';
	$.getJSON(url,
		function(data){
			$.each(data, function(i,item){
				$("select[name='size']").append($("<option></option>").text(item));
			});
			$("select[name='size']").removeAttr("disabled");
		}
	);
	$("input[name='submit']").attr("disabled",true);	
});

$("select[name='size']").change(function(event){
	// window.alert("tv size selected. enable submit button");
	// Do nothing... nothing is needed here.
});

$("#findamount-go").click(function(){
	var url="/findamount/dropdowns.asp?action=go&"
		+"lang="+Lang+"&"
		+"mounttype="+$("input[name='mounttype']:checked").val()+'&'
		+"type="+$("select[name='type']").val()+'&'
		+"make="+$("select[name='make']").val()+'&'
		+"brand="+$("select[name='brand']").val()+'&'
		+"size="+$("select[name='size']").val();
	$.getJSON(url,
		function(data){
			// setup spinny spinner #animagic
			// clear result div
			$("#productlist").html('<a href="#null" onclick="printContent(\'productlist\')">Print</a><br /><br /><div id="mountFinderMain"></div><br/><table width="750" cellspacing="1" cellpadding="3" border="0"><tbody><tr>'

			+	'<td width=25% valign=top class="mountFinderSlimline">'
			+		'<div id="productlist-slimline" style="width:200px;"></div>'
			+	'</td>'

			+	'<td width=25% valign=top class="mountFinderPerfectmount">'
			+		'<div id="productlist-perfectmount" style="width:200px;"></div>'
			+	'</td>'

			+	'<td width=25% valign=top class="mountFinderParamount">'
			+		'<div id="productlist-paramount" style="width:200px;"></div>'
			+	'</td>'

			+	'<td width=25% valign=top class="mountFinderTruvue">'
			+		'<div id="productlist-truvue" style="width:200px;"></div>'
			+	'</td>'

			+	'</tr></tbody></table>');

			$(".mountFinderSlimline").prepend($("#SL-header").html());
			$(".mountFinderPerfectmount").prepend($("#PM-header").html());
			$(".mountFinderParamount").prepend($("#PR-header").html());
			$(".mountFinderTruvue").prepend($("#TV-header").html());
			$("#mountFinderMain").prepend($("#MAIN-header").html());

			$('html, body').animate({
				scrollTop: $("#findamount-go").offset().top
			}, 2000);


			$.each(data, function(i, item){
				// Feth the item URL
				searchurl="/"
					+"TermsofUse/SingleProduct/tabid/108/language/"+Lang+"/"
					+"Default.aspx?Search="+item;
				
				// Decide what type of mount this is:
				switch(item.substring(0,2)) {
					case "SL":
						// Now fetch the actual results: SLIMLINE
						$.ajax({
							url:	searchurl,
							type:	"GET",
							success:	function(msg){
										$("#productlist-slimline").append("<div style='float:left;'>"+$('#dnn_ctr569_ProductList_pnlProductList',msg).html()+"</div>");
									}
						});
						// lastProd=$('#dnn_ctr569_ProductList_pnlProductList').text();
						// $("#productlist").append(lastProd);
					break;
					case "PM":
						// Now fetch the actual results: PERFECT MOUNT
						$.ajax({
							url:	searchurl,
							type:	"GET",
							success:	function(msg){
										$("#productlist-perfectmount").append("<div style='float:left;'>"+$('#dnn_ctr569_ProductList_pnlProductList',msg).html()+"</div>");
									}
						});
						// lastProd=$('#dnn_ctr569_ProductList_pnlProductList').text();
						// $("#productlist").append(lastProd);
					break;
					case "PR":
						// Now fetch the actual results: PARAMOUNT
						$.ajax({
							url:	searchurl,
							type:	"GET",
							success:	function(msg){
										$("#productlist-paramount").append("<div style='float:left;'>"+$('#dnn_ctr569_ProductList_pnlProductList',msg).html()+"</div>");
									}
						});
						// lastProd=$('#dnn_ctr569_ProductList_pnlProductList').text();
						// $("#productlist").append(lastProd);
					break;
					case "TV":
						// Now fetch the actual results: TRUVUE
						$.ajax({
							url:	searchurl,
							type:	"GET",
							success:	function(msg){
										$("#productlist-truvue").append("<div style='float:left;'>"+$('#dnn_ctr569_ProductList_pnlProductList',msg).html()+"</div>");
									}
						});
						// lastProd=$('#dnn_ctr569_ProductList_pnlProductList').text();
						// $("#productlist").append(lastProd);
					break;
				}				

			});
			// disable spinny spinner
		}
	);

});

function printContent(id){
	str=document.getElementById(id).innerHTML
	newwin=window.open('','printwin','left=100,top=100,width=400,height=400')
	newwin.document.write('<HTML>\n<HEAD>\n')
	newwin.document.write('<TITLE>Print Page</TITLE>\n')
	newwin.document.write('<script>\n')
	newwin.document.write('function chkstate(){\n')
	newwin.document.write('if(document.readyState=="complete"){\n')
	newwin.document.write('window.close()\n')
	newwin.document.write('}\n')
	newwin.document.write('else{\n')
	newwin.document.write('setTimeout("chkstate()",2000)\n')
	newwin.document.write('}\n')
	newwin.document.write('}\n')
	newwin.document.write('function print_win(){\n')
	newwin.document.write('window.print();\n')
	newwin.document.write('chkstate();\n')
	newwin.document.write('}\n')
	newwin.document.write('<\/script>\n')
	newwin.document.write('</HEAD>\n')
	newwin.document.write('<BODY onload="print_win()">\n')
	newwin.document.write(str)
	newwin.document.write('</BODY>\n')
	newwin.document.write('</HTML>\n')
	newwin.document.close()
}

