/// <reference path="~/js/jquery.js" />
$(document).ready(function() {
    var VideoUrl = $("#VideoPopUpImage_Default").attr("href");
    if (undefined !== VideoUrl && null !== VideoUrl && "" !== VideoUrl) {
        $("#VideoSection_Default").show();
        var VideoPopUp = function() {
            var win = window.open(VideoUrl, "PopupLarge", "scrollbars=yes,width=820,height=530,resizable=yes");
            win.focus();
            return false;
        };
        $("#VideoPopUpImage_Default").click(VideoPopUp);
        $("#VideoPopUpText_Default").click(VideoPopUp);
    } else {
        $("#VideoSection_Default").hide();
    }

    $("#VideoPopUpList li a").each(function() {
        $(this).click(function() {
            var PopUpUrl = $(this).attr("href");
            var win = window.open(PopUpUrl, "PopupLarge", "scrollbars=yes,width=820,height=530,resizable=yes");
            win.focus();
            return false;
        });
    });
});

	// function opens BackorderDelivery.aspx in a popup window
	function win(dis_window) 
	{
	    mywindow = window.open("", "win", "height=400,width= 350,screenX=650,screenY=400,top=400, left=650" +
		"directories=no,location=right,menubar=no," +
		"resizable,status=no,toolbar=no,history=no,scrollbars");
		mywindow.location.href = dis_window;
	}
	
	// function opens 
	function emailWin(dis_window) 
	{
	    mywindow = window.open("", "win", "height=600,width= 550,screenX=650,screenY=400,top=400, left=450" +
		"directories=no,location=right,menubar=no," +
		"resizable,status=no,toolbar=no,history=no,scrollbars");
		mywindow.location.href = dis_window;
	}
	
	// sets sku quantity to 1 if checkbox is checked
	function setQuantity(txt,cb)
	{	
		if(document.Form1.elements[cb].checked == 1)
		{
			document.Form1.elements[txt].value = "1";
		}
		else
		{
			document.Form1.elements[txt].value = "";
		}
		document.Form1.elements[txt].focus();
	}
	
	// examines value put in sku quantity and turns checkbox on or off
	function setCheckBox(cb,txt)
	{
		var cbBox = document.Form1.elements[cb];
		var tBox = document.Form1.elements[txt];
		var txtValue = tBox.value;
		
		if(checkValue(txtValue)>=0)
		{
			if(document.Form1.elements[txt].value == "0")
			{
				cbBox.checked = 0;
				tBox.value = "";
			}
			else
			{
				cbBox.checked = 1;
				tBox.value = parseInt(txtValue, 10);
			}
		}
		else
		{
			alert('Please enter a number from 1-99');
			tBox.value = "";
			cbBox.checked = 0;
		}
		
	}		
	
	// parses to integer
	function checkValue(txtValue)
	{
	    if (!isNaN(parseInt(txtValue, 10)))
		{
		    return parseInt(txtValue, 10);
		}
	}
	
	// show - hides product sku items
	function setSkuView(obj,id)
	{
		obj.blur();
		window.focus();
		
		var ColorCodesList = document.getElementById("txtColorCodeArray").value;
		var ColorCodesSkuCountList = document.getElementById("txtColorCodeSkuCountArray").value;
		if (null == ColorCodesList || null == ColorCodesSkuCountList) {
		    return;
		}
		
		var ColorCodesArray = ColorCodesList.split(",");
		var ColorCodesSkuCountArray = ColorCodesSkuCountList.split(",");
		if (ColorCodesArray.length != ColorCodesSkuCountArray.length) {
		    return;
		}

		var CurrentGeneratedCtlNumber = 0;
		// grouped by colors
		for(var i = 0; i < ColorCodesArray.length; i++)
		{
			var ColorHeader = document.getElementById("color_" + ColorCodesArray[i]);
			if (null != ColorHeader) {
				var IsColorHeaderShown = false;
				// loop through each item in the current color list
				for(var j = 0; j < ColorCodesSkuCountArray[i]; j++) {
					var CurrentSkuPanel = document.getElementById("dlsku__ctl" + CurrentGeneratedCtlNumber + "_pnlItem");
					if(CurrentSkuPanel != null) {
						if (id == "All") {
							CurrentSkuPanel.style.display = "block";
							IsColorHeaderShown = true;
						}
						else {
							var itemSize = document.getElementById("dlsku__ctl" + CurrentGeneratedCtlNumber + "_hdnSize");
							var CurrentItemSize = (itemSize.value + "");
							if ((CurrentItemSize.indexOf(",") < 0 && CurrentItemSize == id) || (CurrentItemSize.indexOf("," + id + ",") >= 0)) {
								CurrentSkuPanel.style.display = "block";
								IsColorHeaderShown = true;
							}
							else {
								CurrentSkuPanel.style.display = "none";
								// find checkbox and textbox and clear if hidden
								var child = CurrentSkuPanel.getElementsByTagName("input");
								child[1].checked = 0;
								child[3].value = "";
							}
						}
						// found a control, increment number to find the next one
						CurrentGeneratedCtlNumber++;
					}
					else {
						break;
					}
				}
				if(IsColorHeaderShown) {
					ColorHeader.style.display = "block";
				}
				else {
					ColorHeader.style.display = "none";
				}
			}
		}
	}
	
	// finds div name "divValidQty" and gets value of each txtQuantity
	function validateAddToCart()
	{
		var frm = document.forms['Form1'];
		var terms = "";
		var txtBox = "";
		
		// gets value of endecca search textbox
		if(frm.elements['Header1:txtSearchDept']!=null)
		{
			txtBox = frm.elements['Header1:txtSearchDept'];
			terms = txtBox.value;
		}

		// this checks if the endecca search should be fired instead
		if (terms == "enter keyword or item #")
		{
			// loop through all elements in form
			for(var i = 0; i < frm.elements.length; i++)
			{
				// find all sku quantity textboxes
				if(frm.elements[i].name.match("txtQuantity"))
				{
					if(frm.elements[i].value>0)
					{
						return true;
					}
				}
			}
			alert("Please select at least one item to add to your cart.");
			return false;
		}
		// initiate search if we've reached this far
		if(!((terms == "") || (terms == "enter keyword or item #")))
		{
			__doPostBack('_ctl1:ibtnSearchDept','');
			return false;
		}
	}
		
	// creates popup for glossary terms in product page
	function GlossaryPopUp(GlossaryID) {
		var win2 = window.open("","GlossaryTermWindow","statusbar=0,height=250,width=425,resizable=0,left=500,top=200,screenX=500,screenY=200,scrollbars=yes,toolbar=0");
		win2.focus();
		win2.document.location.href = "../glossary_popup.asp?id=" + GlossaryID;
	}