
var isIE   = (navigator.userAgent.toLowerCase().indexOf("msie") != -1);

ckdd = {};
ckdd.extend = function(baseClass, subClass) {
  // function inheritance() {}
   //inheritance.prototype = baseClass.prototype;

   //subClass.prototype = new inheritance();
   subClass.prototype = new baseClass();
   subClass.prototype.constructor = subClass;
   subClass.baseConstructor = baseClass;
   subClass.superClass = baseClass.prototype;
   
}

/*------------------------------------------------------------------------------------------------------------
Window
------------------------------------------------------------------------------------------------------------*/
function closeAndRefreshOpener() {
	try {
		window.opener.refreshPage()
		window.close();
	} catch (e) {
		window.close();
	}
}

function openWindow(url,windowName,width,height) {
	var windFeatures = 'width=' + width + ',height='+height;
	if (openWindow.arguments.length == 5) {
		windFeatures += "," + openWindow.arguments[4];
	} else {
		windFeatures += ',scrollbars=yes,locationbar=no,menubar=no,resizable=no'
	}
			
	var win;
	try {
		if (win = window.open(url,windowName,windFeatures)) {
			if ((width !="") && (height != "")) win.resizeTo(width,height);
			win.focus();
		}
	} catch(e) {
		//alert("openWindow()\n   url:" + url + "\n   windowName:" + windowName + "\n   width:" + width + "\n   height:" + height + "\n\n" + e.description);
	}
	return win;
			
}
/*------------------------------------------------------------------------------------------------------------
Refresh (used in intranet template and popup template. 
------------------------------------------------------------------------------------------------------------*/
function refreshPage() {
	/*
	  args[0] : [optional] url-encoded string
	  args[1] : [optional] "+" (+ = append instead of replace)
	*/
			
	var sNewQuery = "";
	var bAppend;
	var sNewParams;
			
	if (arguments.length == 0) {
		//do not use document.location.reload() since that will repost any form that may have been submitted earlier
		bAppend = true; //to preserve any existing params
		sNewParams = "";
	} else {
		bAppend = (arguments.length==2 && arguments[1]=="+"); //by default, replace parameters unless second parameter flag passed
		sNewParams = arguments[0];
	}
	
	var sScriptName = document.location.pathname;
	var sQueryString = document.location.search + "";
	if ((sQueryString != "") && (sQueryString.slice(0,1)=="?")) sQueryString = sQueryString.slice(1,sQueryString.length);
				
	if (bAppend) {
		var aTemp;
		var aNameValues = new Array();
		var aParams = (sQueryString != "" ? sQueryString.split("&") : new Array());
		var aNewParams = sNewParams.split("&");
					
		//do not duplicate parameters, but the passed in params will override the existing query string param
		for (var index=0; index < aParams.length; index++) {
			aTemp = aParams[index].split("=");
			if (aTemp[0] != "") aNameValues[aTemp[0]] = aTemp[1];
		}
		for (var index=0; index < aNewParams.length; index++) {
			aTemp = aNewParams[index].split("=");
			if (aTemp[0] != "") aNameValues[aTemp[0]] = aTemp[1];
		}
					
		//can't use "join" on associative array, so build manually
		for (var key in aNameValues) {
			sNewQuery += (sNewQuery != "" ? "&" : "") + key + "=" + aNameValues[key];
		}

	} else {
		sNewQuery = sNewParams;
	}

	document.location.href= sScriptName + "?" + sNewQuery;
	

}

/*------------------------------------------------------------------------------------------------------------
Layers
------------------------------------------------------------------------------------------------------------*/
function toggleLayer(layerName) {
	var container = document.getElementById(layerName);
	if (container) {
		sDisplay = "block";
		if (container.tagName.toUpperCase()=="TABLE") sDisplay=""; //for Mozilla, table display value should be blank, not block
		container.style.display=(container.style.display=='none' ? sDisplay : 'none')
		return (container.style.display != 'none');
	}
}
function showLayer(layerName) {
	var container = document.getElementById(layerName);
	if (container) container.style.display="block";
}
function hideLayer(layerName) {
	var container = document.getElementById(layerName);
	if (container) container.style.display="none";
}


function toggleExpander(layerID) {
	var ctrlTextHide = "";
	var ctrlTextShow = "";
	var ctrlTextContainer = null;
	if (toggleExpander.arguments.length == 3) {
		ctrlTextShow = toggleExpander.arguments[1];
		ctrlTextHide = toggleExpander.arguments[2];
		ctrlTextContainer = document.getElementById("ctrlText." + layerID);	
	}
	if (toggleLayer(layerID)) {
		document.getElementById("ctrlImg." + layerID).src = "/_ckcommon/images/icons/treeCollapse.gif";
		if (ctrlTextContainer != null) ctrlTextContainer.innerHTML = ctrlTextHide;	
	} else {
		document.getElementById("ctrlImg." + layerID).src = "/_ckcommon/images/icons/treeExpand.gif";
		if (ctrlTextContainer != null) ctrlTextContainer.innerHTML = ctrlTextShow;	
	}
}

function toggleTitleExpander(layerID) {
	if (toggleLayer(layerID)) {
		document.getElementById("ctrlImg." + layerID).src = "/_ckcommon/images/titleBar/titleLeft_hide.jpg";
	} else {
		document.getElementById("ctrlImg." + layerID).src = "/_ckcommon/images/titleBar/titleLeft_show.jpg";
	}
}

/*----------------------------------------------------------------------------------
common popups
----------------------------------------------------------------------------------*/
function userProperties() {
	var userID = (arguments.length >=1 ? arguments[0] : "");
	var url='/ckcommon/intranet/controlPanel/panels/users/user_properties.asp';
	
	if (pageID=='intranet.users.management') {
		oUserSelect.view(userID,url);
	} else {
		url += '?refererPageID=' + pageID;
		if (arguments.length >= 2) url+='&' + arguments[1];
		document.location.href=url;
	}

}

function userView(uid) {
	var refererPageID=(userView.arguments.length==2 ? userView.arguments[1] : "");
	openWindow('/ckcommon/intranet/controlPanel/panels/users/popup/user_edit.asp?userID='+uid+'&mode=view&refererPageID=' + refererPageID,"userEdit",750,600,"scrollbars=yes")
}
function calendarView(eventTypeID,iMonth, iYear) {
	if (!calendarCellWidth) calendarCellWidth=110;
	else if (calendarCellWidth == "") calendarCellWidth = 110;
	
	var winWidth = (calendarCellWidth * 7) + 75;
	openWindow('/ckcommon/calendar/calendarPopup.asp?eventTypeID=' + eventTypeID + '&month='+iMonth+'&year='+iYear,"calendarPreview",winWidth,600,"scrollbars=yes,resizable=yes,status=yes");
}
function insufficientPermissions() { 
	alert("You do not have sufficient permissions to access this area"); 
}
function inactiveModule() { 
	alert("This area is currently inactive"); 
}
/*----------------------------------------------------------------------------------
DOM
----------------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------------
Element Sizing
  - originally created to make fieldset blocks next to each other get set to same height
----------------------------------------------------------------------------------*/
function equalizeElementHeights(aElementIDs) {
	var maxHeight = 0;
	var aElements = new Array();
	var oEl;
	var sVisibility, sDisplay;
	var bShowHide;
	
	for (var index=0; index < aElementIDs.length; index++) {
		oEl = document.getElementById(aElementIDs[index]);
		if (oEl) aElements[aElements.length] = oEl;
		
	}
	
	for (var index=0; index < aElements.length; index++) {
		oEl = aElements[index];
		if (oEl) {
			//clientHeight will be 0 for any display:none element, so...
			bShowHide = (oEl.style.display=="none" || oEl.style.display=="");
			
			if (bShowHide) {
				sDisplay = oEl.style.display;
				sVisibility = oEl.style.visibility;
				oEl.style.visibility="hidden";
				oEl.style.display="block";
			}

			maxHeight = (oEl.clientHeight > maxHeight ? oEl.clientHeight : maxHeight);
			
			if(bShowHide) {
				oEl.style.visibility = sVisibility;
				oEl.style.display=sDisplay;
			}
		}
	}
	
	for (var index=0; index < aElements.length; index++) {
		oEl = aElements[index];
		if (oEl) oEl.style.height = maxHeight;
	}

}

function setMinElementHeight(iMinHeight,aElementIDs) {
	var oEl;
	var bShowHide;
	var sDisplay, sVisibility;
	
	for (var index=0; index < aElementIDs.length; index++) {
		oEl = document.getElementById(aElementIDs[index]);
		if (oEl) {
			bShowHide = (oEl.style.display=="none" || oEl.style.display=="");
			
			if (bShowHide) {
				sDisplay = oEl.style.display;
				sVisibility = oEl.style.visibility;
				oEl.style.visibility="hidden";
				oEl.style.display="block";
			}

			if (oEl.clientHeight < iMinHeight) {
				if (isIE) {
					oEl.style.height = iMinHeight;
				} else {
					oEl.style.minHeight = iMinHeight;
				}
			}
			

			if(bShowHide) {
				oEl.style.visibility = sVisibility;
				oEl.style.display=sDisplay;
			}
		}
		
	}

}
/*----------------------------------------------------------------------------------
print
----------------------------------------------------------------------------------*/
function printWindow() {
	if (window.print) {
    	window.print();
	} else {
      	alert("Sorry, your browser doesn't support this feature.");
    }
}   



/*---------------------------------------------------------------------------------------------
utility
----------------------------------------------------------------------------------------------*/
function trim(str) {
	return(str.replace(/^ */,"").replace(/ *$/,""));
}

function smallTalkVar(str) {
      str = str.replace(/^ */,"").replace(/ *$/,"");  //trim
      var debug1=str;
      str = unescape(str); //unescape special characters so they can be stripped
      str = str.replace(/[\.\,\/\\\-\'\&]/g,""); //remove special characters
      
      var word;
      var aWords = str.split(" ");
      for (var index=0; index < aWords.length; index++) {
            word = aWords[index];
            if (index==0) aWords[index] = word.slice(0,1).toLowerCase() + word.slice(1,word.length);
            else aWords[index] = word.slice(0,1).toUpperCase() + word.slice(1,word.length);
      }
      newVar = aWords.join("");
      return newVar;
 
}
function uniqueIDFilename(sUID) {
	//since uniqueIDs converted to varchar in DB are returned without braces, we're standardizing filenames using uniqueids (user pictures...) to have them removed
	if (sUID) return sUID.replace(/{/,"").replace(/}/,"");
	else return "";
}
 

function objectDebugStr(oObj) {
      var sData = "";
      var maxIndentLevels = (objectDebugStr.arguments.length >=2 ? objectDebugStr.arguments[1] : 0) ;//sometimes when iterating an HTML object or similar, there are too many nested object levels to handle, so this gives users the option of not recursing.  By default, do not recurse
      var indentLevel = (objectDebugStr.arguments.length >=3 ? objectDebugStr.arguments[2] : 0); //this is never passed in by the user...only this function when it recurses
      if (oObj) { 
            for (var key in oObj) {
                  if (typeof(oObj[key]) != "function") {
                        if (indentLevel > 0) {
                              for (var index=0; index < indentLevel * 5; index++) sData+= " ";
                              sData += ".";
                        }
                  
                        sData += key + " (" + typeof(oObj[key]) + ")=" + oObj[key] + "\n";
                        if ((typeof(oObj[key])=="object") && (indentLevel < maxIndentLevels)){ //&& (oObj[key] != null)) {
                              sData += objectDebugStr(oObj[key],maxIndentLevels,++indentLevel); 
                              indentLevel--;
                              
                        }
                        
                  }
            }
           
      } else {    
            sData = "[null] object"
      }
      return sData;
}

/*---------------------------------------------------------------------------------------------
output to browser error console (FF js console or IE alert)
----------------------------------------------------------------------------------------------*/
function throwConsoleError(src,err) {
	var sErr = "";
	sErr += "\n" + Date()
	sErr += "\nSource:" + src;
	
	switch ((typeof(err)).toLowerCase()) {
		case "object" :
			sErr += "\nMessage:" + err.message;
			sErr += "\nLine:" + err.lineNumber;
			sErr += "\n\nName:" + err.name;
			if (err.fileName) sErr += "\nFilename:" + err.fileName;
			if (err.stack) sErr += "\nStack:" + err.stack;
			break;
		case "string" :
			sErr += "\n" + err;
			break;
	}
	if (isIE) alert(sErr) 
	else alert(sErr); //throw(sErr);
}

/*---------------------------------------------------------------------------------------------
JSON
----------------------------------------------------------------------------------------------*/
function getJSON(oObj) { 
	var sName = (arguments.length==2 ? arguments[1] : "");
	var keyCount;
	var sOutput = "";
	
	if (sName != "") sOutput = "'" + sName + "':"

	if (typeof(oObj)=="array") {
		//sName can not be blank when processing an array
		if (sName=="") return
		
		sOutput += "[";
		for (var index=0; index < oObj.length; index++) {
			if (index != 0) sOutput += ",";
			sOutput += getJSON(oObj[intIndex]);
		}
		sOutput += "]";
	} else if (typeof(oObj)=="object") {
		sOutput += "{";
		keyCount = 0;
		for (var key in oObj) {
			if (key != "") {
				if (keyCount != 0) sOutput += ",";
				sOutput += getJSON(oObj[key],key);
				keyCount++;;
			}
		}			
		sOutput += "}";
	} else if (!oObj) {
		sOutput += "''";
	} else {
		switch (typeof(oObj)) {
			case "number" :
				sOutput += oObj;
				break;
			case "boolean" :
				sOutput += (oObj ? "true" : "false");
				break;
			default :
				//carriage return and line feeds mess up the JSON structure during the eval process. Eliminate them.
				oObj = oObj.replace(/\n/,"");
				oObj = oObj.replace(/'/g,"\'");
				sOutput += "'" + oObj + "'";
		}
	}

	return sOutput;
	
}
function arrayToString(aParams) {
	var aValues = Array();
	for (var index=0; index < aParams.length; index++) { //important this be kept as an indexed iteration to support iterating the "arguments" property of a function
		switch (typeof(aParams[index])) {
			case "string" : aValues[aValues.length] = "'" + aParams[index] + "'";break;
			case "number" :
			case "boolean":	
				aValues[aValues.length] = aParams[index];break;
			case "object" :	aValues[aValues.length] = getJSON(aParams[index]);	break;
			
		}
	}
	
	return aValues.join(",");
}