// JavaScript Document
function showhidePopUpLayer(layerName,show) {
  if (document.getElementById) {
    obj = document.getElementById(layerName);
  } else if (document.all) {
    obj = document.all.item(layerName);
  } else {
    obj = null;
  }
  if (obj==null) return;
  obj.style.visibility = show ? 'visible' : 'hidden';
  obj.style.display = show ? 'block' : 'none';
}
function showPopupWindow(winURL) {
	self.name = "main"; // names current window as "main"
	OpenWindow = window.open(winURL, "remote", "toolbar=0,location=0,directories=0,status=0,menubar=1,scrollbars=1,resizable=1,width=500,height=500,top=100,left=110");
	OpenWindow.focus();
}

function showHideDivElements(parentDivId, idStartsWith, showId, tabValueHiddenFieldId) {
	var parent;
	if (document.getElementById) {
		parent = document.getElementById(parentDivId);
		tabValueField = document.getElementById(tabValueHiddenFieldId);
	} 
	else if (document.all) {
		parent = document.all.item(parentDivId);
		tabValueField = document.all.item(tabValueHiddenFieldId);
	} 
	else {
		return;
	}

	var divs = parent.getElementsByTagName("div");
	for (i = 0; i < divs.length; i++) {
		obj = divs[i];
		len = idStartsWith.length;
		if (obj.id.substring(len, 0).toLowerCase() == idStartsWith) {
			if (obj.id == showId) {
				obj.style.visibility = 'visible';
				obj.style.display = 'block';
				if (tabValueField != null) {
					index = obj.id.substring(idStartsWith.length, obj.id.length);
					tabValueField.value = index;
				}
			}
			else {
				obj.style.visibility = 'hidden';
				obj.style.display = 'none';
			}
		}
	}
}

function selectThisTab(listName,obj) {
	if (document.getElementById) {
		tabs = document.getElementById(listName);
		obj = document.getElementById(obj);
	} else if (document.all) {
		tabs = document.all.item(listName);
		obj = document.all.item(obj);
	} else {
		tabs = null;
		obj = null;
	}
	if (tabs==null) return;
	var tabList = tabs.getElementsByTagName("a");
	for (i = 0; i < tabList.length; i++) {
    tabList[i].className = "";
  }
  obj.className = "selected";
}

function rotateDivElements(parentDivId, listId, selectedClassName, idStartsWith, listIdStartsWith) {
    //get current selected item
    var oCurSelected = jQuery('#' + listId + ' a.' + selectedClassName);

    //pull out currently selected id from item
    var iCurSelectedId = parseInt(oCurSelected[0].id.substring(listIdStartsWith.length, oCurSelected[0].id.length));

    //get item content from currently selected id and if null return
    var oCurSelectedContent = jQuery('#' + idStartsWith + iCurSelectedId);
    if (oCurSelectedContent == null) {
        return;
    }

    //increment our selected id to move to next item.
    var iNextId = iCurSelectedId + 1;

    //get next selected number
    var oNextSelected = jQuery('#' + listIdStartsWith + iNextId);

    //get item to show using id passed
    var oNextContent = jQuery('#' + idStartsWith + iNextId);
    if (oNextContent[0] == null) {
        //if item is null then we are passed the last item
        iNextId = 0;

        //get item to show using reset id passed
        oNextContent = jQuery('#' + idStartsWith + iNextId);
        oNextSelected = jQuery('#' + listIdStartsWith + iNextId);
        if (oNextContent[0] == null) {
            return;
        }
    }

    //switch to item to show
    oCurSelectedContent[0].style.visibility = 'hidden';
    oCurSelectedContent[0].style.display = 'none';
    oCurSelected.removeClass(selectedClassName);

    oNextContent[0].style.visibility = 'visible';
    oNextContent[0].style.display = 'block';
    oNextSelected.addClass(selectedClassName);

    /*
    //fade to item to show
    oNextContent[0].style.visibility = 'visible';

    oCurSelectedContent.fadeOut('slow', function () {
        jQuery(this)[0].style.display = 'none';
        oCurSelected.removeClass(selectedClassName);
        oNextSelected.addClass(selectedClassName);

        oNextContent.fadeIn('slow', function () {
            jQuery(this)[0].style.display = 'block';
        });
    });
    */
}

/* Counts how many checkboxes are selected */
function GetSelectedCount(className) {
    var count = 0;

    //Finds all state checkboxes and adds their value to the array
    jQuery('.' + className + ' input').each(
		function(box) {
		    if (this.checked) {
		        count += 1;
		    }
		}
	);

    return count;
}

/*
Removes a location from the page
*/       
function removeLocationRow(id) {
    var rowId = 'tr_' + id;
    var row = document.getElementById('tr_' + id);
    if (row != null) {
        var rows = new Array();
        rows.push(row);
        // Need to get all the rows that the location spans
        var rowSpan = jQuery('#' + rowId + " > td:first").attr('rowspan');
        if (rowSpan != null) {
            for (var i = 0; i < rowSpan - 1; i++) {
                var nextRow = jQuery(row).next()[0];                
                rows.push(nextRow);
                row = nextRow;
            }
        }
        jQuery(rows).fadeOut('fast');
    }
    deselectCheckbox(id);
}

function deselectCheckbox(id) {
    var checkboxSelector = "#cb_" + id + " input";
    jQuery(checkboxSelector).each(
		function() {
		    this.checked = false;
		}
	);
}

String.prototype.unescapeHtml = function() {
    var temp = document.createElement("div");
    temp.innerHTML = this;
    var result = temp.childNodes[0].nodeValue;
    temp.removeChild(temp.firstChild)
    return result;
} 

