//Switcher method to set the visibility of an object to true/false
//Modified to change id's of elements to either "show" or "hide"
//This is to enable compliance in print style sheets
function showHide(obj)
{
	//Get the element to show/hide
	var el = document.getElementById(obj);
	
	//Get the class of the element
	var elClass = el.className;
	
	//Check if the class of the element is "Hide"
	if(elClass == "Hide")
	{
		//Show the element
		el.className = "Show";
	}
	else
	{
		//Hide the element
		el.className = "Hide";
	}
}

//Swaps images given a passing Element, destinationID image and a srcFile to swap to
function swapImage(passingElement, destinationID, srcFile)
{
	//Get the image to swap
	var destination = document.getElementById(destinationID);
	//Set the src
	destination.src = srcFile;
	//De-Highlight all images
	var thumbTable = document.getElementById("thumbTable");
	var imgs = thumbTable.getElementsByTagName('img');
	//Loop thru the images, de-highlight them so they don't stick
	for(i = 0; i < imgs.length; i++)
	{
		imgs[i].id = "";
	}
	//Highlight the current image
	passingElement.id = "currentImg";
}

//Helper function by Jonathan Snook
//http://www.snook.ca
function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/-/g, "\-");
	var oRegExp = new RegExp("(^|\s)" + strClassName + "(\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

//Uses MooTools Ajax Framework
function ajaxAdRefresh()
{
	var myAjax = new Ajax("/Xhtml/Equipment/ByJustListed.html?Top=" + Math.round(Math.random()*19),{ method: 'get', update:$("justListedItem")}).request();
	window.setTimeout("ajaxAdRefresh()", 8000);
}

//Used to get the number of search results for the fastSearchForm
function fastSearchResults(TypeID, MfID, Model)
{
	if(TypeID == 0 && MfID == 0 & Model == '')
	{
		$("currentSearchResults").innerHTML = '';	
	}
	else
	{
		if(TypeID == 0 && MfID == 0 & Model.length > 0)
		{
			$("currentSearchResults").innerHTML = 'Specify a type or manufacturer as well.';
		}
		else
		{
			if(Model == '')
			{
			var myAjax = new Ajax("/Controller/Equipment/AjaxFastSearch.jsp?TypeID=" + TypeID + "&ManufacturerID=" + MfID + "&Model=", {method: 'get', update:$("currentSearchResults")}).request();
			}
			else
			{
			var myAjax = new Ajax("/Controller/Equipment/AjaxFastSearch.jsp?TypeID=" + TypeID + "&ManufacturerID=" + MfID + "&Model=" + Model, {method: 'get', update:$("currentSearchResults")}).request();
			}
		}
	}
}

function checkKeywordValue(formObj)
{
    keywordString = formObj.Keyword.value;
    if (keywordString.length <= 1)
    {
        alert ("You must enter two or more characters");
        return (false);
    }
    else
    {
        return (true);

    }
}

function checkFastSearchForm(formObj)
{

      Model = formObj.Model.value;
    if (formObj.TypeID[formObj.TypeID.selectedIndex].text == "[Not Specified]" && Model <= 1 && formObj.ManufacturerID[formObj.ManufacturerID.selectedIndex].text == "[Not Specified]" )
    {
        alert ("Please specify a type of equipment, a manufacturer, or a model to proceed.");
          return (false);
    }
    else
    {
		if(formObj.TypeID.value == 0 && formObj.ManufacturerID.value == 0 && formObj.Model.value.length > 0)
		{
			alert("You must specify a type of equipment or manufacturer in addition to the model to proceed.");
			return(false);
		}
        return (true);
    }
}

function checkEmailValues(formObj)
{
    Name = formObj.Name.value;
    Email = formObj.Email.value;
    EmailConfirmation = formObj.EmailConfirmation.value;

    Comment = formObj.Comment.value;

    if (Name.length < 1)
    {
        alert ("Name, E-mail, E-mail Confirmation and Comment are required fields");
        return (false);
    }
    else if (Email.length < 1)
    {
        alert ("Name, E-mail, E-mail Confirmation and Comment are required fields");
        return (false);
    }
    else if (Email!= EmailConfirmation)
    {
        alert ("E-mail and E-mail confirmation must be the same");
        return (false);
    }
    else if (Comment.length < 1)
    {
        alert ("Name, E-mail, E-mail Confirmation and Comment are required fields");
        return (false);
    }
    else
    {
        return (true);
    }
}

// sets the options for a select box, from an array
// @param theSelect the select box to fill
// @param theValues the array of Option objects to use

function setSelectOptions(theSelect, theValues)
{
    // remove old options from theSelect
    for(i=theSelect.options.length; i>=theValues.length; i--) {
        theSelect.options[i] = null;
    }

    // populate theSelect with theValues
    for(i=0; i<theValues.length; i++) {
        theSelect.options[i] = new Option(theValues[i][0], theValues[i][1], false, false);
    }
}

function cloneObject(what) {
    for (i in what) {
        this[i] = what[i];
    }
}

// Selects all of the checkboxes for a given form
// @param theForm the form to select all checkboxes for
function selectAllCheckboxes(theForm)
{
    for (i = 0; i < theForm.elements.length; i++)
    {
        if (theForm.elements[i].type == 'checkbox')
        {
            theForm.elements[i].checked = true;
        }
    }
}

// Deselects all of the checkboxes for a given form
// @param theForm the form to deselect all checkboxes for
function deselectAllCheckboxes(theForm)
{
    for (i = 0; i < theForm.elements.length; i++)
    {
        if (theForm.elements[i].type == 'checkbox')
        {
            theForm.elements[i].checked = false;
        }
    }
}

//check at least one checkbox is checked.
function checkAtLeastOneSelected(theForm){

    for (i = 0; i < theForm.elements.length; i++)
    {
        if (theForm.elements[i].type == 'checkbox')
        {
	   	 	if (theForm.elements[i].checked) 
	   		{
			return true;
		    }
        }
    }
    //no checkbox is checked.
    alert("You must select at least one check box to proceed.");
    return false;
};

// Converts the values from a set of checkboxes into a comma delimted string.
// If no checkboxes have been checked, returns "''".
//
// @param theForm a form that has checkboxes
function compressToString(theForm)
{
    var string = "''";
    
    if (theForm == null)  return "''";
    
    for (i = 0; i < theForm.elements.length; i++)
    {
        if (theForm.elements[i].type == 'checkbox' && theForm.elements[i].checked == true && theForm.elements[i].value)
        {
            string += "," + theForm.elements[i].value;
        }
    }
    
    // Remove extra comma.
    if (string != "''") string = string.substr(1);
    
    return string;
}

// Converts the values from a set of checkboxes into a Tilda delimted string.
// If no checkboxes have been checked, returns "''".
//
// @param theForm a form that has checkboxes
function compressToStringTildSeperator(theForm)
{
    var string = "''";
    
    if (theForm == null)  return "''";
    
    for (i = 0; i < theForm.elements.length; i++)
    {
        if (theForm.elements[i].type == 'checkbox' && theForm.elements[i].checked == true && theForm.elements[i].value)
        {
            string += "~" + theForm.elements[i].value;
        }
    }
    
    // Remove extra comma.
    if (string != "''") string = string.substr(1);
    
    return string;
}

function sortColumn(column,direction) 
{ 
	document.Form1.Page.value=1;
	if (direction=="Descending")
	{
		document.Form1.SB1.value=column;
	}
	else 
	{
		document.Form1.SB1.value=column+ ' DESC';
	}						
	document.Form1.submit();
}		
function showPage(page)
{            				            				
	document.Form1.Page.value=page.substring(5,page.length);
	document.Form1.submit();
}

/************ MENU SYSTEM ***************/

function subNav(subNavID, itemIndex)
{
	var div = document.getElementById(subNavID);
	var element = div.getElementsByTagName("a")[itemIndex];
	element.className = "subHere";
}

//Tabbed Menu System

var tabMenu={
	disabletablinks: false,
	////Disable hyperlinks in 1st level tabs with sub contents (true or false)?
	currentpageurl: window.location.href.replace("http://"+window.location.hostname, "").replace(/^\//, ""),
	//get current page url (minus hostname, ie: http://www.iedaused.com/)
	timer: -1,
	currentPageIndex: -1,

definemenu:function(tabid, dselected)
{
	this[tabid+"-menuitems"]=null;
	tabMenu.currentPageIndex = dselected;
	this.addEvent(window, function(){tabMenu.init(tabid, dselected)}, "load");
},

showsubmenu:function(tabid, targetitem)
{
	var menuitems=this[tabid+"-menuitems"]
	for (i=0; i<menuitems.length; i++)
	{
		menuitems[i].className=""
		if (typeof menuitems[i].hasSubContent!="undefined")
			document.getElementById(menuitems[i].getAttribute("rel")).style.display="none"
	}
	targetitem.className="current"
	if (typeof targetitem.hasSubContent!="undefined")
		var subNavDiv = document.getElementById(targetitem.getAttribute("rel"))
		subNavDiv.style.display="block"
		var snapBack=function(){tabMenu.init(tabid, tabMenu.currentPageIndex)}
		subNavDiv.onmouseout=function(){tabMenu.timer = setTimeout(snapBack, 1000)}
		subNavDiv.onmouseover=function(){tabMenu.cancelSnapback()}
},

isSelected:function(menuurl)
{
	var menuurl=menuurl.replace("http://"+menuurl.hostname, "").replace(/^\//, "")
	return (tabMenu.currentpageurl==menuurl)
},

addEvent:function(target, functionref, tasktype)
{ //assign a function to execute to an event handler (ie: onunload)
	var tasktype=(window.addEventListener)? tasktype : "on"+tasktype
	if (target.addEventListener)
		target.addEventListener(tasktype, functionref, false)
	else if (target.attachEvent)
		target.attachEvent(tasktype, functionref)
},

cancelSnapback:function()
{
	clearTimeout(tabMenu.timer);
},

init:function(tabid, dselected)
{
	var menuitems=document.getElementById(tabid).getElementsByTagName("a")
	var snapBack=function(){tabMenu.init(tabid, tabMenu.currentPageIndex)}
	this[tabid+"-menuitems"]=menuitems
	for (var x=0; x<menuitems.length; x++)
	{
		if (menuitems[x].getAttribute("rel"))
		{
			this[tabid+"-menuitems"][x].hasSubContent=true
			if (tabMenu.disabletablinks)
				menuitems[x].onclick=function(){return false}
		}
		else //for items without a submenu, add onMouseout effect
			menuitems[x].onmouseout=function(){this.className=""
	}
	menuitems[x].onmouseout=function(){tabMenu.timer = setTimeout(snapBack, 1500)}
	menuitems[x].onmouseover=function(){tabMenu.showsubmenu(tabid, this); tabMenu.cancelSnapback();
}
if (dselected=="auto" && typeof setalready=="undefined" && this.isSelected(menuitems[x].href))
{
	tabMenu.showsubmenu(tabid, menuitems[x])
	var setalready=true
}
else if (parseInt(dselected)==x)
	tabMenu.showsubmenu(tabid, menuitems[x])
}
}
}