﻿$('#modalWindow').ready(function(){
	if ($(window).height() < document.getElementById('modalWindow').clientHeight) {
		document.getElementById('overlayContentDiv').style.height = ($(window).height() - 50) + "px";
		document.getElementById('overlayContentDiv').style.overflow = "auto";
	}

	newPosition = Math.round(($(window).height() - document.getElementById('modalWindow').clientHeight) / 2);

	if ($.browser.msie && (parseInt($.browser.version) < 7)) {
		// fix for ie 6?
	} else {
		document.getElementById('modalWindow').style.top = newPosition + "px";
	}
});


$('#modalWindowSmall').ready(function(){
	if ($(window).height() < document.getElementById('modalWindowSmall').clientHeight) {
		document.getElementById('overlayContentDiv').style.height = ($(window).height() - 50) + "px";
		document.getElementById('overlayContentDiv').style.overflow = "auto";
	}

	newPosition1 = Math.round(($(window).height() - document.getElementById('modalWindowSmall').clientHeight) / 2);

	if ($.browser.msie && (parseInt($.browser.version) < 7)) {
		// fix for ie 6?
	} else {
		document.getElementById('modalWindowSmall').style.top = newPosition1 + "px";
	}
	
	
	/* ----------------   tech doc search window  ---------------- */
	swapImages('#techDocWindowForm .techDocSearchBtn','btn_search_techdocs','gif');
	
	$('#techDocWindowForm .techDocSearchBtn').click(
		function() {
			var val = $('#techDocWindowForm #techInfoSearchField').val();
			
			if (jQuery.trim(val) != "") {
				$('#techDocWindowForm .techDocSearchValidationMsg').css('display','none');
				location.href = "/resources/technicalfiles/default.aspx?sw=" + val;
			} else {
				$('#techDocWindowForm .techDocSearchValidationMsg').css('display','block');
			}
		}
	);
	
	$('#techDocWindowForm #techInfoSearchField').keypress(
		function(e) {
			if (e.which == 13) {
				var val = $(this).val();
				if (jQuery.trim(val) != "") {
					$('#techDocWindowForm .techDocSearchValidationMsg').css('display','none');
					location.href = "/resources/technicalfiles/default.aspx?sw=" + val;
				} else {
					$('#techDocWindowForm .techDocSearchValidationMsg').css('display','block');
				}
				
				return false;
			}
		}
	);
	
	/* ----------------   cross ref search window  ---------------- */
	swapImages('#crossRefWindowForm .crossRefSearchBtn','btn_search_crossref','gif');
	
	$('#crossRefWindowForm .crossRefSearchBtn').click(
		function() {
			var manufacturerVal = $('#crossRefWindowForm #ddlManufacturer :selected').val();
			var swVal = $('#crossRefWindowForm #txtCrossRefSearch').val();
			
			$('#crossRefWindowForm .crossRefValidationMsg').css('display','none');
			if ((jQuery.trim(manufacturerVal) != "") && (jQuery.trim(swVal) != "")) {
				location.href = "/resources/crossreference/default.aspx?sw=" + swVal + "&m=" + manufacturerVal;
			} else {
				if (manufacturerVal == "") {
					$('#crossRefWindowForm #crossRefValidationManufacturer').css("display","block");
				}
				
				if (swVal == "") {
					$('#crossRefWindowForm #crossRefValidationNumber').css("display","block");
				}
			}
		}
	);
	
	$('#crossRefWindowForm #txtCrossRefSearch').keypress(
		function(e) {
			if (e.which == 13) {
				var manufacturerVal = $('#crossRefWindowForm #ddlManufacturer :selected').val();
				var swVal = $('#crossRefWindowForm #txtCrossRefSearch').val();
				
				if ((jQuery.trim(manufacturerVal) != "") && (jQuery.trim(swVal) != "")) {
					$('#crossRefWindowForm .crossRefValidationMsg').css('display','none');
					location.href = "/resources/crossreference/default.aspx?sw=" + swVal + "&m=" + manufacturerVal;
				} else {
					if (manufacturerVal == "") {
						$('#crossRefWindowForm .crossRefValidationMsg').html('Please select a manufacturer');
					} else {
						if (swVal == "") {
						$('#crossRefWindowForm .crossRefValidationMsg').html('Please enter a Product Number or Product Name');
						}
					}
				}
				
				return false;
			}
		}
	);
});

var arrayOfProductsToCompare = [];

function findIndexInArray(argValue, argArray) {
	var returnValue = -1;
	for(var i=0;i < argArray.length; i++) {
		if (argValue == argArray[i]) {
			returnValue = i;
			break;
		}
	}
	
	return returnValue;
}

function toggleCompareCell(argElementID) {
	prodObj = document.getElementById(argElementID);
	
	if (prodObj.className == "selected") {
		prodObj.className = "unselected";
		
		var elementIndex = findIndexInArray(argElementID, arrayOfProductsToCompare)
		if (elementIndex != -1) {
			arrayOfProductsToCompare.splice(elementIndex, 1);
		}
	} else {
		if (arrayOfProductsToCompare.length < 4) {
			prodObj.className = "selected";
			arrayOfProductsToCompare.push(argElementID);
		} else {
			alert("You can only select 4 items to compare.");
		}
	}
	
	document.getElementById("selectedCount").innerHTML = arrayOfProductsToCompare.length;
	//alert(arrayOfProductsToCompare);
	
	if (arrayOfProductsToCompare.length > 1 && arrayOfProductsToCompare.length <= 4) {
		var htmlStr = "<a href=\"/profile/compare.aspx?ids=";
		htmlStr = htmlStr + arrayOfProductsToCompare;
		if (newQS != "") {
			htmlStr = htmlStr + "&" + newQS;
		}
		
		htmlStr = htmlStr + "\"><img src=\"/assets/images/compareSelectedBtn.gif\" onmouseover=\"this.src='/assets/images/compareSelectedBtn_over.gif'\" onmouseout=\"this.src='/assets/images/compareSelectedBtn.gif'\" alt=\"Compare\" title=\"Compare\" /></a>";
	} else {
		var htmlStr = "<img src=\"/assets/images/compareSelectedBtn_over.gif\" alt=\"You need to select at least 2 items to compare.\" title=\"You need to select at least 2 items to compare.\" />";
	}
	
	document.getElementById("compareAction").innerHTML = htmlStr;
}
