// JavaScript Document

$(document).ready(function(){
	$('#frmAskAQuestions').find('select').each( function() {
		// This checks the Length of each Select Box, if longer then 120px
		// it shrinks it to 120px to fix the display.
		if ($(this).width() > 225) {
			$(this).width(225);
			if (jQuery.browser.msie) {
				// IF using a MSIE, Change how the Dropdown Works if its default
				// length is more then 120px.
				$(this).mousedown( function() {
					$(this).css({
						width: "auto",
						position: "absolute"
					});
					if ($('#PlaceHolder').size() == 0) {
						$(this).after("<select id='PlaceHolder'><option>PlaceHolder</option></select>");
					}
				});
				$(this).change( function() { $(this).blur(); } );
				$(this).blur( function() {
					$(this).css({
						width: "225px",
						position: "static"
					});
					$('#PlaceHolder').remove();
				});
			} // Close If MSIE
		}
	}); // Close Attributes.select
	$('select[name="manufacturerNo"]').change( function() {
		var manufacturerNo = $(this).val(); 
		var EquipSelectBox = $(this).parents('form').find('#modelSelect');
		if (manufacturerNo.length == 0) {
			EquipSelectBox.html('<option value="">View All</option>');
			$('input[name="newManufacturerName"]').remove();
			$('input[name="newEquipmentName"]').remove();
		}
		else if (manufacturerNo == "NEW") {
			$(this).after("<input type='text' name='newManufacturerName' value='' class='txtAddNewEquipInput' />");
		}
		else {
			EquipSelectBox.html('<option value="">Loading...</option>');
			$('input[name="newManufacturerName"]').remove();
			$('input[name="newEquipmentName"]').remove();
			$.ajax({
				type: "post",
				url: "/?home.getModels",
				contentType: "application/x-www-form-urlencoded; charset=UTF-8",
				data: 'ajaxRequest=true&manufacturerNo=' + manufacturerNo,
				dataType: "html",
				async: false,
				complete: function(msg) {
					var thisText = msg.responseText.split("</td></td>");
					thisText = thisText[0];
					var models = thisText.split("~~");
					var splitModels;
					if (EquipSelectBox.attr("name").toLowerCase().indexOf("equipmentno") == '0') {
						var selectBoxHTML = '<option value="0">Choose your equipment model</option><option value="0">---</option><option value="0">Other model not listed</option><option value="0">---</option>';
					} else {
						var selectBoxHTML = '<option value="">View All Models</option>';
					}
					for(var i=0; i < models.length; i++) {
						splitModels = models[i].split("``");
						if(splitModels.length == 2) {
							selectBoxHTML = selectBoxHTML + '<option value="' + splitModels[1].toString() + '">' + splitModels[0].toString() + '</option>';
						}
					}
					EquipSelectBox.html(selectBoxHTML);
				} // Close Complete
			});	// Close $Ajax
			
			var btn = $(this).parents('form').find('#modelSelectBtn');
			if (btn.is(':visible')) {
				EquipSelectBox.after(btn.clone());
				btn.remove();
			}
		} // Close IF THEN ELSE
		if (EquipSelectBox.attr('name') == "equipmentNo") {
			//$('#frmAskAQuestions').find('#modelSelect').append("<option value=''>----------------------</option><option value='NEW'>Not Listed</option>");
		}
	});
	$('select[name="equipmentNo"]').change( function() {
		if ($(this).val() == "NEW") {
			$(this).after("<input type='text' name='newEquipmentName' value='' class='txtAddNewEquipInput' />");
		}
		else {
			$('input[name="newEquipmentName"]').remove();
		}
	});
	
	$('#chooseMMForm').submit( function() {
		var ManufacturerSelectBox = $(this).find('select[name="manufacturerNo"]');
		var EquipmentSelectBox = $(this).find('#modelSelect');
		if (EquipmentSelectBox.val().length > 0) {
			window.location.href = "/?equipment.view/equipmentNo/" + EquipmentSelectBox.val();
			return false;
		}
	});
	bindBenchBehaviour(false);	
	$('#equipmentURL').after("<br /><a href='' id='lnkEquipmentURLSelectLink'>Select Link</a>");
	$('#lnkEquipmentURLSelectLink').click( function() {
		SelectText('equipmentURL');
		return false;
	});
	
	// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
	// -=-= COMAPRE -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
	// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
	$("input[name^='compare_']").attr("checked",false);
	$("input[name^='compare_']").attr("disabled",false);
	if ($("#compareSection").size()) {
		if ($("#compareSection").html().indexOf("equipmentNo") > 0) {
			// get the list of checked equipment
			var selectedEquipment = $("#compareSection input[name='equipmentNo']").val().split(",");				
			// only turn the check on for ones that are left
			for(var i=0; i<selectedEquipment.length; i++){
				$("[name='compare_"+selectedEquipment[i]+"']").attr("checked",true);	
			}
		}
	}
	
	$.ajax({
		type: "post",
		url: "?equipment.equipmentCompareList",
		data: '&ajaxRequest=true&categoryNo=' + $("#categoryNoInput").val(),
		success: function(returnData) {
				// set the returned HTML to the box
				$("#compareSection").html(returnData);
				$("#equipmentCompareBottomForm").html(returnData);
				// turn all equipment to checked off
				$("input[name^='compare_']").attr("checked",false);
				if (returnData.indexOf("equipmentNo") > 0) {
					// get the list of checked equipment
					var selectedEquipment = $("#compareSection input[name='equipmentNo']").val().split(",");				
					// only turn the check on for ones that are left
					for(var i=0; i<selectedEquipment.length; i++){
						$("[name='compare_"+selectedEquipment[i]+"']").attr("checked",true);	
					}
					if ($("#compareSection #numberOfEquipment").val() < 15) {
						$("input[name^='compare_']").each( function() {
							$(this).attr("disabled",false);
							$(".max").html("Compare");
							$(this).parent().find("label").removeClass("max");											
						});
					}
				}
		}				 
	});
	
	
	
	$('a.remove').live('click', function() {
	 // the remove icon that is in the compare Box
		var dataList = $(this).attr("href").split("?");
		var queryList = dataList[1].split("/");
		var equipmentNo = queryList[2];
		var categoryNo = $("#categoryNoInput").val();
		// remove the clicked equipment
		$.ajax({
			 type: "post",
			 url: "?equipment.equipmentCompareList",
			 data: '&ajaxRequest=true&removeEquipment=' + equipmentNo + '&categoryNo=' + categoryNo,
			 success: function(returnData) {
				 // fade it out
					$("#compareSection").fadeOut(250, function() {
							// set the returned HTML to the box
							$("#compareSection").html(returnData);
							$("#equipmentCompareBottomForm").html(returnData);
							// turn all equipment to checked off
							$("input[name^='compare_']").attr("checked",false);
							$("input[name^='compare_']").parent().removeClass("selectedCompare");	
							if (returnData.indexOf("equipmentNo") > 0) {
								// get the list of checked equipment
								var selectedEquipment = $("#compareSection input[name='equipmentNo']").val().split(",");				
								// only turn the check on for ones that are left
								for(var i=0; i<selectedEquipment.length; i++){
										$("[name='compare_"+selectedEquipment[i]+"']").attr("checked",true);	
										$("[name='compare_"+selectedEquipment[i]+"']").parent().addClass("selectedCompare");	
										
								}
								if ($("#compareSection #numberOfEquipment").val() < 15) {
									$("input[name^='compare_']").each( function() {
										$(this).attr("disabled",false);
										$(".max").html("Compare");
										$(this).parent().find("label").removeClass("max");											
									});
								}
								
							}
					});
					// fade back in
					$("#compareSection").fadeIn(250);
			 }
			 
			 });
		return false;
	});
	
	$(".toggleCompare").click( function(e)  {
		toggleCompare(this);
	});

	// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
	// =-=-= TABS -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
	// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
	$('#alphaList a').click( function(e) {
		e.preventDefault();
		$('#alphaList a.active').removeClass('active').addClass('inactive');
		$(this).removeClass('inactive').addClass('active');

		$('#manufacturerList div.active').removeClass('active').addClass('inactive');
		$('#list_' + $(this).text()).removeClass('inactive').addClass('active');
		return false;
	}); // Close alphaList a.click
	$('#EquipmentList_Tabs a').click( function(e) {
		e.preventDefault();
		if ($(this).text() == "Manufacturers") {
			$('#lnkCategoryList').parent('li').removeClass('activeTab').addClass('inactiveTab');
			$('#lnkManufacturerList').parent('li').removeClass('inactiveTab').addClass('activeTab');

			$('#categoryList').removeClass('activeTab').addClass('inactiveTab');
			$('#manufacturerList').removeClass('inactiveTab').addClass('activeTab');
		}
		else {
			$('#lnkCategoryList').parent('li').removeClass('inactiveTab').addClass('activeTab');
			$('#lnkManufacturerList').parent('li').removeClass('activeTab').addClass('inactiveTab');

			$('#categoryList').removeClass('inactiveTab').addClass('activeTab');
			$('#manufacturerList').removeClass('activeTab').addClass('inactiveTab')
		}
		return false;
	});

});

function SelectText(element) {
    var text = document.getElementById(element);
    if ($.browser.msie) {
        var range = document.body.createTextRange();
        range.moveToElementText(text);
        range.select();
    } else if ($.browser.mozilla || $.browser.opera) {
        var selection = window.getSelection();
        var range = document.createRange();
        range.selectNodeContents(text);
        selection.removeAllRanges();
        selection.addRange(range);
    } else if ($.browser.safari) {
        var selection = window.getSelection();
        selection.setBaseAndExtent(text, 0, text, 1);
    }
}

function toggleCompare(checkboxObj) {
		var boxValue = $(checkboxObj).val().split("_");
		var categoryNo = boxValue[0].split("=");
		categoryNo = categoryNo[1];
		
		var equipmentNo = boxValue[1].split("=");
		equipmentNo = equipmentNo[1];
		
		var includeSeries = boxValue[2].split("=");
		includeSeries = includeSeries[1];
		var addEquipment;
		var removeEquipment;
		var data = "categoryNo=" + categoryNo;
		if (checkboxObj.checked) {
			$(checkboxObj).parent().addClass("selectedCompare");
			data = data + '&addEquipment=' + equipmentNo;
			if (includeSeries.length) {
				data = data + '&includeSeries=' + includeSeries;
			}
			$.ajax({
						 type: "post",
						 url: "?equipment.equipmentCompareList",
						 data: data,
						 success: function(returnData) {
							  $("#compareSection").fadeOut(250, function() {
										$("#compareSection").html(returnData);	
										if ($("#compareSection #numberOfEquipment").val() >= 15) {
											$("input[name^='compare_']").each( function() {
												if ($(this).attr("checked") == false) {
													$(this).attr("disabled",true);
													$(this).parent().find("label").addClass("max");
													$(".max").html("Maximum amount selected");
												}
											});
										}
								});
							 	$("#compareSection").fadeIn(250);
								$("#equipmentCompareBottomForm").html(returnData);
								
						 }
						 
						 });
		} else {
			$(checkboxObj).parent().removeClass("selectedCompare");
			data = data + '&ajaxRequest=true&removeEquipment=' + equipmentNo;
			$.ajax({
						 type: "post",
						 url: "?equipment.equipmentCompareList",
						 data: data,
						 success: function(returnData) {
							  $("#compareSection").fadeOut(250, function() {
								$("#compareSection").html(returnData);								
								if ($("#compareSection #numberOfEquipment").val() < 15) {
									$("input[name^='compare_']").each( function() {
										$(this).attr("disabled",false);
										$(".max").html("Compare");
										$(this).parent().find("label").removeClass("max");											
									});
								}
								$("#equipmentCompareBottomForm").html(returnData);
						  });
							 	$("#compareSection").fadeIn(250);
						 }
						 
						 });
		}
}

function toggleCheckBox(thisBox) {
	if ($(thisBox).attr("checked")) {
		$(thisBox).attr("checked",false); 
	} else {
		$(thisBox).attr("checked",true); 
	}
}

