// Simplified Popup
// Usage: <a onclick="return popUp(this.href,250,250);" href="popup.aspx">Link</a>
function popUp(theLink, popUpWidth, popUpHeight) {
	var popUpWindow = window.open(theLink, "popUp", "width=" + popUpWidth + ", height=" + popUpHeight + ", left=50, top=50, resizable=yes, scrollbars=yes", false);
	if ( popUpWindow != null ) {
		popUpWindow.focus();
	}
	return false;
}
function closePopUp() {
	window.close();
}

// Bookmark this page
// From http://www.howtocreate.co.uk/tutorials/index.php?tut=0&part=13
/*
usage:

<a href="file.html" title="Quirk's Bookmark"
rel="sidebar" onclick="bookmarkPage(); return false;">Bookmark This Page</a>

Where href, title, and rel values are used by Firefox
*/
function bookmarkPage() {
	if ( window.sidebar && window.sidebar.addPanel ) {
		// Gecko (Netscape 6 etc.) - add to Sidebar
		window.sidebar.addPanel(this.title, this.href, '');
	} else if ( window.external && ( navigator.platform == 'Win32' || ( window.ScriptEngine && ScriptEngine().indexOf('InScript') + 1 ) ) ) {
		/*
		IE Win32 or iCab - checking for AddFavorite produces errors in
		IE for no good reason, so I use a platform and browser detect.
		adds the current page as a favourite; if this is unwanted,
		simply write the desired page in here instead of 'location.href'
		*/
		window.external.AddFavorite(location.href, document.title);
	} else if ( window.opera && window.print ) {
		// Opera 6+ - add as sidebar panel to Hotlist
		return true;
	} else if ( document.layers ) {
		// NS4 & Escape - tell them how to add a bookmark quickly (adds current page, not target page)
		window.alert('Please click OK then press Ctrl+D to create a bookmark');
	} else {
		// other browsers - tell them to add a bookmark (adds current page, not target page)
		window.alert('Please use your browser\'s bookmarking facility to create a bookmark');
	}
}



// FrameChecker -- Remove if frames site.
if (window!=top) { top.location.replace(document.location.href); }

// Popup Windows
function strOpenWindowFeatures(iWindowWidth, iWindowHeight) {
	var iMouseX = 10;
	var iMouseY = 10;
	if ( window.event != null ) {
		iMouseX = window.event.screenX;
		iMouseY = window.event.screenY; }
	var iScreenX = window.screen.availWidth;
	var iScreenY = window.screen.availHeight;
	var iWindowLeft = iMouseX;
	if ( iWindowLeft + iWindowWidth > iScreenX ) { iWindowLeft = iMouseX - iWindowWidth - 10; }
	if ( iWindowLeft < 10 ) { iWindowLeft = 10; }
	var iWindowTop = iMouseY;
	if ( iWindowTop + iWindowHeight > iScreenY - 50 ) { iWindowTop = iMouseY - iWindowHeight - 60; }
	if ( iWindowTop < 10 ) { iWindowTop = 10; }
	return "width=" + iWindowWidth + ", height=" + iWindowHeight + ", left=" + iWindowLeft + ", top=" + iWindowTop + ", resizable=yes";
}
function fnOpenWindow(strWindowURL, strWindowName, iWindowWidth, iWindowHeight) {
	var objNewWindow = window.open(strWindowURL, strWindowName, strOpenWindowFeatures(iWindowWidth, iWindowHeight), false);
	if ( objNewWindow != null ) {
		objNewWindow.focus();
	}
}

// This function is also in the admin.js file,
//  and fully documented there...
function fnSimulateRepeaterCommand(strLinkButtonUniqueID, strHiddenFieldUniqueID, strHiddenFieldValue) {
	//alert("fnSimulateRepeaterCommand('" + strLinkButtonUniqueID + "', '" + strHiddenFieldUniqueID + "', '" + strHiddenFieldValue + "');");

	// Split and Join the UniqueID property of the LinkButton to get
	//  the DHTML ID and .NET __doPostBack ID for the server control.
	var aryLinkButtonID = strLinkButtonUniqueID.toString().split(":");
	var strLinkButtonClientID = aryLinkButtonID.join("_");
	var strLinkButtonPostBackID = aryLinkButtonID.join("$");

	// Split and Join the UniqueID property of the HtmlInputHidden
	//  to get the DHTML ID for the server control.
	var aryHiddenFieldID = strHiddenFieldUniqueID.toString().split(":");
	var strHiddenFieldClientID = aryHiddenFieldID.join("_");

	// Look for DHTML objects for the LinkButton and HtmlInputHidden.
	var objLinkButton = document.getElementById(strLinkButtonClientID);
	var objHiddenField = document.getElementById(strHiddenFieldClientID);

	// A variable to verify that everything will work...
	var blnOK = false;
	//alert("typeof __doPostback = '" + typeof __doPostBack + "'");
	if ( typeof __doPostBack == 'function' ) {
		if ( objLinkButton != null ) {
			if ( typeof objLinkButton == 'object' ) {
				if ( objHiddenField != null ) {
					if ( typeof objHiddenField == 'object' ) {
						if ( objHiddenField.value != null ) {
							if ( strHiddenFieldValue.toString().length > 0 ) {
								blnOK = true;
							} else { alert("ERROR! strHiddenFieldValue parameter is an empty string!"); }
						} else { alert("ERROR! DHTML element with id='" + strHiddenFieldClientID + "' has no 'value' attribute!"); }
					} else { alert("ERROR! DHTML element with id='" + strHiddenFieldClientID + "' is not an object!"); }
				} else { alert("ERROR! Could not find a DHTML element with id='" + strHiddenFieldClientID + "'"); }
			} else { alert("ERROR! DHTML element with id='" + strLinkButtonClientID + "' is not an object!"); }
		} else { alert("ERROR! Could not find a DHTML element with id='" + strLinkButtonClientID + "'"); }
	} else { alert("ERROR! Could not find a '__doPostBack' function!"); }

	if ( blnOK == true ) {
		objHiddenField.value = strHiddenFieldValue.toString();
		try {
			__doPostBack(strLinkButtonPostBackID,'');
		} catch (err) {
			alert("JavaScript error:\n" + err.description);
		}
	}
}

// Cross-Browser Block Element Display Toggler
function toggleElement(elId) {
	var visibleStyle = "block";
	if (!document.all) {
		var el = document.getElementById(elId);
		if((el != null) && (typeof(el) == 'object')) {
			var elName = el.tagName.toLowerCase();
			if (elName == "tr") {
				visibleStyle = "table-row";
			}
			else if (elName == "tbody") {
				visibleStyle = "table-row-group";
			}
		} else {
			alert("ElementId '" + elId + "' is null or not an object.");
		}
	}
	var v = ((document.getElementById(elId).style.display == visibleStyle) || (document.getElementById(elId).style.display == ""));
	document.getElementById(elId).style.display = v ? "none" : visibleStyle;
}



// Pre-load plus sign and minus sign images, used by
//  fnToggleElementVisibilityAndPersistWithIndicator.
// When testing with a different application name (i.e.
//  locally), the path may be different
indicatorBlank = new Image();
indicatorBlank.src = "/imgs/icons/indicatorBlank.gif";
indicatorMinus = new Image();
indicatorMinus.src = "/imgs/icons/indicatorMinus.gif";
indicatorPlus = new Image();
indicatorPlus.src = "/imgs/icons/indicatorPlus.gif";

function fnToggleElementVisibilityAndPersistWithIndicator(strElementId, strIndicatorElementId, strHiddenFieldId, intRecordId) {
		
	var objHiddenField = document.getElementById(strHiddenFieldId);
	if ( objHiddenField == null )
		{ alert("Could not find '" + strHiddenFieldId + "' element."); return; }
	if ( objHiddenField.value == null )
		{ alert("Could not find 'value' attribute of '" + strHiddenFieldId + "' element."); return; }

	var strRecordId = " " + intRecordId;
	var regExRecordId = new RegExp(strRecordId);

	var objElement = document.getElementById(strElementId);
	var objIndicatorElement = document.getElementById(strIndicatorElementId);

	var blnVisible;

	// Displaying the correct image ("+" or "-"), setting the hidden values
	if ( objElement != null ) {
		if (objElement.style.display == "") { objElement.style.display = "none"; }
		blnVisible = objElement.style.display != "none";
		if ( blnVisible == true ) {
			//	alert("Hiding DocumentList element '" + strElementId + "'.");
			objHiddenField.value = objHiddenField.value.replace(regExRecordId, "");
			
			if ( objIndicatorElement != null ) {
				if ( objIndicatorElement.src != null ) {
					objIndicatorElement.src = indicatorPlus.src;
				} else { alert("Could not find 'src' attribute of '" + strIndicatorElementId + "' element."); }
			} else { alert("Could not find '" + strIndicatorElementId + "' element."); }
		} else {
			//	alert("Showing DocumentList element '" + strElementId + "'.");
			objHiddenField.value = objHiddenField.value + strRecordId;
			
			if ( objIndicatorElement != null ) {
				if ( objIndicatorElement.src != null ) {
					objIndicatorElement.src = indicatorMinus.src;
				} else { alert("Could not find 'src' attribute of '" + strIndicatorElementId + "' element."); }
			} else { alert("Could not find '" + strIndicatorElementId + "' element."); }
		}
	} else { alert("Could not find '" + strElementId + "' element."); }

	// Now do the toggling (show/hide)
	toggleElement(strElementId);
}

// ** fnConfirmCheckboxes **
//
// This function returns true or false, and is commonly used to
//  verify that at least one checkbox is checked in a list of
//  DHTML checkboxes with the same "name" attribute.
// <input type="submit" onclick="return fnConfirmCheckboxes('[value of "name" attribute]');" />

function fnConfirmCheckboxes(strCheckboxesName, strErrorMessage, strConfirmMessage) {
	var blnOK = false;

	var strAlertMessage = "Please check at least one item.";
	if ( strErrorMessage != null ) {
		if ( strErrorMessage.toString().length > 0 ) {
			strAlertMessage = strErrorMessage.toString();
		}
	}

	var strConfirmQuestion = "Are you sure?";
	if ( strConfirmMessage != null ) {
		if ( strConfirmMessage.toString().length > 0 ) {
			strConfirmQuestion = strConfirmMessage.toString();
		}
	}

	var i = 0;
	var objCheckbox;
	var objCheckboxes = document.getElementsByName(strCheckboxesName.toString());
	if ( objCheckboxes != null ) {
		if ( typeof objCheckboxes == 'object' ) {

			if ( objCheckboxes.length > 0 ) {
				for (i = 0; i < objCheckboxes.length; i++) {
					objCheckbox = objCheckboxes.item(i);
					if ( objCheckbox.checked != null ) {
						if ( objCheckbox.checked == true ) { blnOK = true; }
					} else { alert("ERROR! Element at index " + i.toString() + " of '" + strCheckboxesName.toString() + "' collection has no 'checked' attribute!"); }
				}
				if ( blnOK ) {
					blnOK = confirm(strConfirmQuestion);
				} else { alert(strAlertMessage); }
			}

			else {
				objCheckbox = objCheckboxes.item(0);
				if ( objCheckbox.checked != null ) {
					if ( objCheckbox.checked == true ) {
						blnOK = confirm(strConfirmQuestion);
					} else { alert(strAlertMessage); }
				} else { alert("ERROR! The one '" + strCheckboxesName.toString() + "' element has no 'checked' attribute!"); }
			}

		} else { alert("ERROR! DHTML element named '" + strCheckboxesName.toString() + "' is not an object!"); }
	} else { alert("ERROR! Could not find a DHTML element named '" + strCheckboxesName.toString() + "'"); }

	return blnOK;
}

ieHover = function() {
    // specify hovered element and div in which it sits 
    var ieEls = document.getElementById("BodyWrapper").getElementsByTagName("input");
	for (var i=0; i<ieEls.length; i++) {
        ieEls[i].onmouseover=function() {
            this.className+=" hover";
        }
        ieEls[i].onmouseout=function() {
            this.className=this.className.replace(new RegExp(" hover\\b"), "");
        }
    }
}
if (window.attachEvent) window.attachEvent("onload", ieHover);