﻿// JScript File

function LoadPage(pageNum) {
	var frm = document.forms[0];
	var obj;

    //Set Page Num
	obj = document.getElementById(document.forms[0].ctrlPrefix.value + 'currentPage');
	obj.value = pageNum;
	
	obj = document.getElementById('cmd');	
	obj.value = 'LoadPage';

//alert(obj.value);
//alert(document.forms[0].Selections.value);
	frm.submit();
}


//Comparison Checkbox Functions
//-------------------------------------------------------------------------------------------------------------
var arrSelections = new Array();	//Array of Selections


//Submit selected items for comparison.
function Compare() {
	var frm = document.forms[0];

	//Add selected items to arrSelections (necessary when user clicks back button)
	for (var x=0;x<frm.elements.length;x++) {		
		if (frm.elements[x].type=='checkbox' && frm.elements[x].id.substr(0,5)=='Cmpr_') {
			CBCompareItem(frm.elements[x], frm.elements[x].id.substr(5));
		}
	}

	if (arrSelections.length <= 0) {
		alert('Please select one or more items to compare.');
	} else {
		//alert('here ' + arrSelections.length);
		
		var strSelections = '';
			
		for(var i=0; i< arrSelections.length; i++) {
			if (arrSelections[i] != '') {
				strSelections += arrSelections[i] + '|';	//Pass IDs
			}
		}
		
		document.location = 'Compare.aspx?SearchType='+searchType+'&Selections='+strSelections;
		
		//frm.action = 'Comparison.aspx';
	//alert(document.forms[0].Selections.value);
		//frm.submit();	
	}

}

function CBCompareItem(ctl, ID) {
	var frm = document.forms[0];

	//Get array of current selections.
	//alert(document.forms[0].ctrlPrefix.value + 'Selections');
	var obj = document.getElementById(document.forms[0].ctrlPrefix.value + 'Selections');	
	if (obj.value != '')
	{
	    arrSelections = obj.value.split('|');			//Get Array of IDs
	}

	if (ctl.checked) {
		AddRemoveSelection(ID, false);	//Add selection to array
//alert('HERE '+ID);		
	} else {
		AddRemoveSelection(ID, true);	//Remove selection from array
	}
	
	//Update "Selections" hidden variable.
	obj.value = '';
    for(var i=0; i< arrSelections.length; i++) {
		if (arrSelections[i] != '') {
			obj.value += arrSelections[i] + '|';	//Append Item or Collection
		}
	}
	//alert(obj.value);
}

function AddRemoveSelection(ID, bolRemove) {
	//Adds or Removes an option from the Selection & SelectionValue arrays.
	var frm = document.forms[0];
	var bolFound = false;
	var i;
	for(i=0; i< arrSelections.length; i++) {
		if (arrSelections[i] == ID) {
			bolFound = true;	//Found matching ID in array.
			break;
		}
	}
	if (bolRemove) {
		if (bolFound) {	//Remove Element
			arrSelections.splice(i, 1);
			//arrSelectionValues.splice(i, 1);
		}
	} else {			//Add Element
		if (!bolFound) {
			arrSelections[arrSelections.length] = ID;
			//arrSelectionValues[arrSelectionValues.length] = escape(eval('document.SelectList.VAL'+ID+'.value'));
		}
	}
}

function ClearCompareSelections() {
	//Clears all Comparison selections
	var frm = document.forms[0];
	
	for (var x=0;x<frm.elements.length;x++) {
		if (frm.elements[x].type=='checkbox' && frm.elements[x].id.substr(0,4)=='Cmpr') {
			frm.elements[x].checked = false;
		}
	}
	//Clear selection array
	document.getElementById(document.forms[0].ctrlPrefix.value + 'Selections').value = '';	
	arrSelections = new Array();
}

//-------------------------------------------------------------------------------------------------------------

//Narrow Your Search Checkbox Functions

function ClearAll(GroupName) {
  //Based on the group name, unchecks the associated "All" (_All) checkbox.
  var obj = document.getElementById(GroupName+'_All');
  obj.checked = false;
  
  //Clear comparision checkboxes
  ClearCompareSelections();
}

function ClearAll_IgnoreCompare(GroupName) {
  //Based on the group name, unchecks the associated "All" (_All) checkbox.
  var obj = document.getElementById(GroupName+'_All');
  obj.checked = false;
}

function ClearGroup(GroupName) {
// the name attribute is set on a <span> element containing the checkbox
// loop through all <span> elements on the page, if the name attribute is equal to the
// GroupName passed in then loop through any checkboxes in the <span> and uncheck them.
    var elem = document.getElementsByTagName("span");
    for(i = 0; i < elem.length; i++) 
    {
          att = elem[i].getAttribute("name");
          if(att == GroupName) 
          {
                var checkboxes = elem[i].getElementsByTagName("input");
                for(i2 = 0; i2 < checkboxes.length; i2++) 
                {
                    checkbox = checkboxes[i2];
                    checkbox.checked = false;
                }
          }
     }

	
    //Clear comparision checkboxes
    ClearCompareSelections();
}

function ClearGroup_IgnoreCompare(GroupName) {
    var elem = document.getElementsByTagName("span");
    for(i = 0; i < elem.length; i++) 
    {
          att = elem[i].getAttribute("name");
          if(att == GroupName) 
          {
                var checkboxes = elem[i].getElementsByTagName("input");
                for(i2 = 0; i2 < checkboxes.length; i2++) 
                {
                    checkbox = checkboxes[i2];
                    checkbox.checked = false;
                }
          }
     }
}

function ClearAllCB(Prefix) 
{
	var ctls = document.getElementsByTagName("input")
	var i;
	
	for (i=0; i<ctls.length; i++) 
	{
	    //Check each input to see if "_All" is in the id
		var ctl = ctls[i];
		var pos = ctl.id.indexOf('_All');
		
		if (pos==-1)
		{
		    // "_All" is not in the id
		    // Uncheck the checkbox, including comparision checkboxes
			ctl.checked = false;
		}
		else 
		{
		    // "_All" is in the id
		    // Check the checkbox
		    ctl.checked = true;
		}
	}
	
    //Clear comparision checkboxes
    ClearCompareSelections();
}


function ClearAllCB_IgnoreCompare(Prefix) 
{
	var ctls = document.getElementsByTagName("input")
	var i;
	
	for (i=0; i<ctls.length; i++) 
	{
	    //Check each input to see if "_All" is in the id
		var ctl = ctls[i];
		var pos = ctl.id.indexOf('_All');
		
		if (pos==-1)
		{
		    // "_All" is not in the id
		    // Uncheck the checkbox, including comparision checkboxes
			ctl.checked = false;
		}
		else 
		{
		    // "_All" is in the id
		    // Check the checkbox
		    ctl.checked = true;
		}
	}
}


$(document).ready(function() {
    $(".NarrowYourSearchExplain").children("div").click(
        function() {
            $("." + $(this).attr("id")).toggle("slow");
            $(this).parent().parent().parent().css('z-index', '200');
        }
    );
    
    $(".NarrowYourSearchExplainContent_CloseButton").click(
        function() {
            $(this).parent().parent().toggle("slow");
            $(this).parent().parent().parent().css('z-index', '2');
        }
    );

    // Unselect all the options when the 'all' checkbox is selected
    $(".NarrowYourSearchAllContainer").children('input:checkbox').click(function() {
        if ($(this).attr('checked') == true) {
            $(this).parent().parent().parent().find('input:checkbox').each(function() {
                $(this).attr('checked', false);
            });
            $(this).attr('checked', true);
        }
    });

    // Unselect the 'all' checkbox
    $("#narrow_search_panel > div > ul > li > span:not(.NarrowYourSearchAllContainer) > input:checkbox").click(function() {
        if ($(this).attr('checked') == true) {
            $(this).parent().parent().parent().children("li").children("span.NarrowYourSearchAllContainer").children("input:checkbox").attr('checked', false);
        }
    });
});