Array.prototype.indexOf = function( value )
{
	for ( var i = 0 ; i < this.length ; i++ )
	{
		if ( this[i] == value )
			return i ;
	}
	return -1 ;
}

String.prototype.startsWith = function( value, ignoreCase )
{
	if ( ignoreCase )
		return ( this.substr( 0, value.length ) == value ) ;
	else
		return ( this.substr( 0, value.length ) == value ) ;
}

String.prototype.endsWith = function( value, ignoreCase )
{
	var L1 = this.length ;
	var L2 = value.length ;
	
	if ( L2 > L1 )
		return false ;

	if ( ignoreCase )
		return ( L2 == 0 || this.toLowerCase().substr( L1 - L2, L2 ) == value.toLowerCase() ) ;
	else
		return ( L2 == 0 || this.substr( L1 - L2, L2 ) == value ) ;
}

String.prototype.remove = function( start, length )
{
	var s = '' ;
	
	if ( start > 0 )
		s = this.substring( 0, start ) ;
	
	if ( start + length < this.length )
		s += this.substring( start + length , this.length ) ;
		
	return s ;
}

String.prototype.trim = function()
{
	return this.replace( /(^\s*)|(\s*$)/g, '' ) ;
}

String.prototype.ltrim = function()
{
	return this.replace( /^\s*/g, '' ) ;
}

String.prototype.rtrim = function()
{
	return this.replace( /\s*$/g, '' ) ;
}

function obj(strObj) {
	return document.getElementById(strObj);
}

function deleteViewState() {
	var oViewState = document.forms[0]["__VIEWSTATE"];
	oViewState.parentNode.removeChild(oViewState);
}

function toggleModlet(intIndex) {
	modlet = obj("Portal_ModletContent" + intIndex);
	if (modlet.style.visibility == "hidden") {
		modlet.style.position = "static";
		modlet.style.visibility = "visible";
		obj("Portal_ImgToggleMod" + intIndex).src = "includes/images/open.gif";
		
	}
	 else 
	{
		modlet.style.position = "absolute";
		modlet.style.visibility = "hidden";
		obj("Portal_ImgToggleMod" + intIndex).src = "includes/images/closed.gif";
	}
}
function toggleModule(clientID, intToggleIndex) {
	module = obj(clientID + "_thisDiv" + intToggleIndex);
	if (module.style.visibility == "hidden") {
		module.style.position = "static";
		module.style.visibility = "";
		obj(clientID+"_ImgToggleMod" + intToggleIndex).src = "images/catalog/arrow-open.gif";
		
	} else {
		module.style.position = "absolute";
		module.style.visibility = "hidden";
		obj(clientID+"_ImgToggleMod" + intToggleIndex).src = "images/catalog/arrow-closed.gif";
	}
}

// Show/hide iframe (updateFrame) for debugging purposes
var debugPress = 0;
document.onkeydown = function(e) {
	if (!e) e = event;
	if (e.keyCode) {
		if (e.keyCode == 192) { // ` key
			if (debugPress < 0) debugPress = 0;
			if (++debugPress == 2) {
				var hf = obj("updateFrame");
				hf.height = (hf.height == 0) ? 200 : 0;
				debugPress = 0;
			} else {
				setTimeout("debugPress--", 350);
			}
		}
	}
}

function hideSelects() {
	var docSelects = document.getElementsByTagName("SELECT");
	
	for(i=0; i<docSelects.length; i++) {
		var select = docSelects[i];
		
		if (select.previousSibling == null || select.previousSibling.nodeType != 1 || 
			select.previousSibling.tagName.toLowerCase() != "input" || select.previousSibling.alt != "isTemp") {
			var text = document.createElement("INPUT");
			text.type				= "text";
			text.alt				= "isTemp";
			text.disabled			= select.disabled;
			text.style.fontFamily	= select.style.fontFamily;
			text.style.fontSize		= select.style.fontSize;
			text.style.position		= select.style.position;
			text.style.visibility	= select.style.visibility;
			text.style.left			= select.style.left;
			text.style.top			= select.style.top;
			text.style.width		= select.offsetWidth + "px";
			text.style.height		= select.offsetHeight + "px";
			text.style.margin		= "0px";
			text.style.padding		= "0px";
			if(select.selectedIndex != null && select.selectedIndex > -1 && select.options[select.selectedIndex].childNodes.length > 0)
				text.value = select.options[select.selectedIndex].childNodes[0].nodeValue;
			
			select.parentNode.insertBefore(text, select);
			
			select.setAttribute("hideCount", 1);
			select.setAttribute("lastPosition", select.style.position);
			select.setAttribute("lastVisibility", select.style.visibility);
			select.style.position = "absolute";
			select.style.visibility = "hidden";
		}
		else {
			select.setAttribute("hideCount", (parseInt(select.getAttribute("hideCount"))+1));
		}
	}
}

function showSelects() {
	var docSelects = document.body.getElementsByTagName("SELECT");
	
	if(arguments.length == 0 || arguments[0] != true) {
		for(i=0; i<docSelects.length; i++) {
			var select = docSelects[i];
			
			if(select.getAttribute("hideCount") != null) {
				select.setAttribute("hideCount", (parseInt(select.getAttribute("hideCount"))-1));
				if(select.getAttribute("hideCount") == "0") {
					if (select.previousSibling != null && select.previousSibling.tagName != null && 
						select.previousSibling.tagName.toLowerCase() == "input" && select.previousSibling.alt == "isTemp")
						select.parentNode.removeChild(select.previousSibling);
					select.style.position = select.getAttribute("lastPosition");
					select.style.visibility = select.getAttribute("lastVisibility");
				}
			}
		}
	}
	else {
		for(i=0; i<docSelects.length; i++) {
			var select = docSelects[i];
			
			if (select.previousSibling != null && select.previousSibling.tagName != null && 
				select.previousSibling.tagName.toLowerCase() == "input" && select.previousSibling.alt == "isTemp")
				select.parentNode.removeChild(select.previousSibling);
			select.setAttribute("hideCount", "0");
			if(select.getAttribute("lastPosition") != null)
				select.style.position = select.getAttribute("lastPosition");
			else
				select.style.position = "";
			if(select.getAttribute("lastVisibility") != null)
				select.style.visibility = select.getAttribute("lastVisibility");
			else
				select.style.visibility = "";
		}
	}
}

//#########################################
//# Div Code
//#########################################

var moveDivObj = null;
var moveDivXOff = 0;
var moveDivYOff = 0;

document.addevent("onmousemove", 
    function(e) {
	    if(!e) e = event;
    	
	    if(moveDivObj != null) {
		    var divX = e.clientX + document.body.scrollLeft;
		    var divY = e.clientY + document.body.scrollTop;
    		
		    if(moveDivXOff + divX < 0)
			    divX = 0 - moveDivXOff;
		    if(moveDivYOff + divY < 0)
			    divY = 0 - moveDivYOff;
    	
		    moveDivObj.style.left = (moveDivXOff + divX) + "px";
		    moveDivObj.style.top = (moveDivYOff + divY) + "px";
		    return false;
	    }
    }
);

document.addevent("onmouseup",
    function(e) {
	    moveDivObj = null;
    }
);

function moveDiv(e, divObj) {
	if (typeof(divObj) == "string")
		divObj = obj(divObj);
	while (divObj != null && divObj.tagName != "DIV")
		divObj = divObj.parentNode;
	if (divObj == null) return;
	
	moveDivObj = divObj;
	moveDivXOff = parseInt(moveDivObj.style.left) - e.clientX - document.body.scrollLeft;
	moveDivYOff = parseInt(moveDivObj.style.top) - e.clientY - document.body.scrollTop;
}

function updateFrameDiv(divObj) {
	if (typeof(divObj) == "string")
		divObj = obj(divObj);
	while (divObj != null && divObj.tagName != "DIV")
		divObj = divObj.parentNode;
	if (divObj == null) return;
	
	var inputs = divObj.getElementsByTagName("INPUT");
	for(i=0; i<inputs.length; i++) {
		if (inputs[i].id != "") {
			var frmObj = updateFrame.obj(inputs[i].id);
			if (!frmObj) {
				alert("'" + inputs[i].id + "' Doesn't exist in the hidden frame");
				continue;
			}
			frmObj.value = inputs[i].value;
			frmObj.disabled = inputs[i].disabled;
			if (inputs[i].type.toLowerCase() == "checkbox" || inputs[i].type.toLowerCase() == "radio")
				frmObj.checked = inputs[i].checked;
		}
	}
	var textarea = divObj.getElementsByTagName("TEXTAREA");
	for(i=0; i<textarea.length; i++) {
			var frmObj = updateFrame.obj(textarea[i].id);
			if (!frmObj) {
				alert("'" + textarea[i].id + "' Doesn't exist in the hidden frame");
				continue;
			}
			frmObj.value = textarea[i].value;
			frmObj.disabled = textarea[i].disabled;
	}
	var selects = divObj.getElementsByTagName("SELECT");
	for(i=0; i<selects.length; i++) {
		if (selects[i].id != "") {
			var frmObj = updateFrame.obj(selects[i].id);
			if (!frmObj) {
				alert("'" + selects[i].id + "' Doesn't exist in the hidden frame");
				continue;
			}
			frmObj.selectedIndex = selects[i].selectedIndex;
			frmObj.disabled = selects[i].disabled;
		}
	}
}

function centerDiv(divObj) {
	if (typeof(divObj) == "string")
		divObj = obj(divObj);
	while (divObj != null && divObj.tagName != "DIV")
		divObj = divObj.parentNode;
	if (divObj == null) return;
	
	var centerX = document.body.clientWidth / 2 + document.body.scrollLeft;
	var centerY = document.body.clientHeight / 2 + document.body.scrollTop;
	for(var i=0; i<divObj.childNodes.length; i++)
		if (divObj.childNodes[i].tagName == "TABLE")
		{
			centerX -= divObj.childNodes[i].offsetWidth / 2;
			centerY -= divObj.childNodes[i].offsetHeight / 2;
		}
	if (centerY < document.body.scrollTop) centerY = document.body.scrollTop;
	divObj.style.left = centerX + "px";
	divObj.style.top = centerY + "px";
}

function closeDiv(divObj) {
	if (typeof(divObj) == "string")
		divObj = obj(divObj);
	while (divObj != null && divObj.tagName != "DIV")
		divObj = divObj.parentNode;
	
	if (divObj != null) {
		var selects = divObj.getElementsByTagName("SELECT");
		for(i=selects.length-1; i>=0; i--)
			selects[i].parentNode.removeChild(selects[i]);
		while (divObj.childNodes.length > 0)
			divObj.removeChild(divObj.firstChild);
		divObj.style.visibility = "hidden";
		divObj.style.top = "-10000px";
		divObj.style.left = "-10000px";
	}
	
	showSelects();
}

// Use:  dropDownDiv( { divObject | 'div id' } [ ,'focus id' ] )
function dropDownDiv(divObj) {

	if (typeof(divObj) == "string")
		divObj = obj(divObj);

	while (divObj != null && divObj.tagName != "DIV")
		divObj = divObj.parentNode;
	if (divObj == null) return;

	hideSelects();

	// make sure the div is absolute positioned and hidden
	// way off the screen so you don't get a flicker
	divObj.style.position = "absolute";
	divObj.style.top = "-10000px";
	divObj.style.left = "-10000px";
	divObj.style.visibility = "hidden";

	
	divObj.innerHTML = updateFrame.obj(divObj.id).innerHTML;

	divObj.style.width = divObj.offsetWidth + "px";
	divObj.style.height = divObj.offsetHeight + "px";
	divObj.style.left = (parseInt(divObj.style.left.replace("px", ""))+1) + "px";
	divObj.style.zIndex = 20;
	divObj.style.visibility = "visible";
	centerDiv(divObj);
	
	// give focus to the object passed
	if (arguments.length > 1 && typeof(arguments[1]) == "string" && obj(arguments[1]) != null)
		obj(arguments[1]).focus();
}


