//************************************************** // commonFunc.js // Author: Tong Lim // Version: 1.2 // Date: 9/27/2006 // Update: 8/28/2008 //*************************************************** /* Example how to call it: commonFunc.mainInit("") commonFunc.addOnload("test();") commonFunc.getElementObj("test") commonFunc.getStyleObj("test") commonFunc.displayElement("test", true) commonFunc.toggleDisplayElement("test") commonFunc.visibleElement("test", true) commonFunc.toggleVisibleElement("test") commonFunc.readonlyTextInput("test", true) commonFunc.disableTextInput("test", true) printPage() onkeypress="return(commonFunc.isNumeric(event))" onkeypress="return(commonFunc.isChar(event))" onkeypress="commonFunc.numericOnly(event)" onkeypress="commonFunc.noZero(event)" onkeypress="return(commonFunc.charOnly(event))" commonFunc.Trim(str) commonFunc.centerDiv("test") commonFunc.isCheckboxCheck("test") onkeyup="commonFunc.autoTab(this,'contactPhoneNumber_b',event)" commonFunc.overLayDiv(true) or commonFunc.overLayDiv(false) commonFunc.divPopUP("testDiv",true) or commonFunc.divPopUP("testDiv",false) commonFunc.toggleTextBold("test") commonFunc.textBold("test", true) commonFunc.getCookie("test") commonFunc.setCookie("test","this is test") commonFunc.deleteCookie("test") */ var commonFunc = new function () { var W3C = (!document.all && document.getElementById); var IE = (document.all); var ns4 = (document.layers); var v_debug = false; var vMainInit = ''; // Name: getFunctionName // Desc: get the name of the current function // param: pass in the string of the function // return: name of the function function getFunctionName(str) { var ownName = str; ownName = ownName.substr('function '.length); // trim off "function " ownName = ownName.substr(0, ownName.indexOf('(')); // trim off everything after the function name return ownName; } this.debug = function(p) { v_debug = p; } function displayError(str) { if (v_debug) alert("error occur in " + getFunctionName(str)); } //-------------------------------------------- // Name: getElementObj // Desc: Function to get an obj of element. pass in an id and it return the obj // param: string of id // return: an object // Example how to call it: commonFunc.getElementObj("test", true) this.getElementObj = function getElementObj(param) { try { if (typeof param == 'object') { return param; } else { if (document.getElementById) { // W3C - Explorer 5+ and Netscape 6+ return document.getElementById(param); } else if (document.all) { // Explorer 4 return document.all[param]; } else if (document.layers) { // NS4 return document.layers[param]; } } } catch(e) { displayError(arguments.callee.toString()); } } // end of getElementObj //-------------------------------------------- // Name: disableTextInput // Desc: Function disable or enable the input text field. // param: p1 = pass in an object or string id; status = true or false // return: nothing // Example how to call it: commonFunc.disableTextInput("test", true) this.disableTextInput = function disableTextInput(p1, status) { try { var obj = this.getElementObj(p1); obj.disabled = status; } catch(e) { displayError(arguments.callee.toString()); } } // end of disableTextInput //------------------------------------------ // Name: visibleElement // Desc: Function to hide or show an element with visibility property. Pass in an id or object and it will hide the element. // param: p1 = id string or an object; status = true or false // return: nothing // Example how to call it: commonFunc.visibleElement("test", true) this.visibleElement = function visibleElement(p1, status) { try { var obj = this.getElementObj(p1); if (obj != undefined) { if (status == true) { obj.style.visibility = ""; } else if (status == false) { obj.style.visibility = "hidden"; } } } catch(e) { displayError(arguments.callee.toString()); } } // end of visibleElement //-------------------------------------------- // Name: centerDiv // Desc: Move the div to the center of the browser. // param: id of div // return: nothing // Example how to call it: commonFunc.centerDiv("test") this.centerDiv = function centerDiv(param) { try { var dsocLeft=0, dsocTop=0; obj = this.getElementObj(param); if (IE) { var iebody = (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body; winH = parseInt(iebody.clientHeight / 2); winW = parseInt(iebody.clientWidth / 2); dsocLeft = iebody.scrollLeft; dsocTop = iebody.scrollTop; } else { winH = parseInt(window.innerHeight / 2); winW = parseInt(window.innerWidth / 2); dsocLeft = window.pageXOffset; dsocTop = window.pageYOffset; } var top = dsocTop + winH - parseInt(obj.offsetHeight / 2); var left = dsocLeft + winW - parseInt(obj.offsetWidth / 2); obj.style.top = top + "px"; obj.style.left = left + "px"; } catch(e) { displayError(arguments.callee.toString()); } } // end of centerDiv //-------------------------------------------- // Name: overLayDiv // Desc: Expand the div to cover the whole document and you can still can see it // param: param = true for expand or false for shrink // return: nothing // Example how to call it: commonFunc.overLayDiv(true); or commonFunc.overLayDiv(false); this.overLayDiv = function overLayDiv(param) { try { var winW = 1, winH = 1; var v_filter = 0; v_opacity = 0; var v_name = "overlayDiv"; var obj; obj = this.getElementObj(v_name); if (obj == null) { divElement = document.createElement("div"); divElement.setAttribute('id', v_name); divElement.setAttribute('style', "z-index:50;"); document.getElementsByTagName('body')[0].appendChild(divElement); obj = this.getElementObj(v_name); } if (param == true) { if (IE) { winW = document.body.scrollWidth; winH = document.body.scrollHeight ; v_filter ="alpha(opacity=50)"; v_opacity = "50"; } else { winW = document.width; winH = document.height; v_filter ="alpha(opacity=.5)"; v_opacity = ".5"; } obj.style.filter = v_filter; obj.style.opacity = v_opacity; } obj.style.position = "absolute"; obj.style.top = "0px"; obj.style.left = "0px"; obj.style.height = winH + "px"; obj.style.width = winW + "px"; obj.style.backgroundColor = "black"; return(v_name); } catch(e) { displayError(arguments.callee.toString()); } } // end of overLayDiv //-------------------------------------------- // Name: divPopUp // Desc: perform a div popup message // param: id = pass id of the div; visible = true for show div or false for hide div // return: nothing // Example how to call it: commonFunc.divPopUP("testDiv",true); or commonFunc.divPopUP("testDiv",false); this.divPopUp = function divPopUp(id, visible) { try { this.visibleElement(id, visible); if (visible) this.centerDiv(id); } catch(e) { displayError(arguments.callee.toString()); } } // end of divPopUp } // end of commonFunc function showPopup(divId, blockBG, closeButton) { try { var overLayDiv_id, messDivObj, popupDiv, id, buttonDivObj; if (blockBG) { overLayDiv_id = commonFunc.overLayDiv(true); } if (closeButton == true) { commonFunc.visibleElement("popCloseButton", false); } if (divId != undefined) { messDivObj = commonFunc.getElementObj(divId); commonFunc.getElementObj("mainPopUpTitle").innerHTML = messDivObj.title; commonFunc.getElementObj("mainPopupBody").innerHTML = messDivObj.innerHTML; buttonDivObj = commonFunc.getElementObj(divId + "Button"); if (buttonDivObj != null) commonFunc.getElementObj("mainPopupButton").innerHTML = buttonDivObj.innerHTML; popupDiv = commonFunc.getElementObj("mainPopupDiv"); popupDiv.style.width = messDivObj.style.width; //popupDiv.style.height = messDivObj.style.height; } commonFunc.divPopUp('mainPopupDiv',true); commonFunc.getElementObj("mainPopUpTitleAnchor").focus(); } catch(e) { // do nothing } } // end of showPopup function showAvPopup(divId, blockBG, closeButton) { try { var overLayDiv_id, messDivObj, popupDiv, id, buttonDivObj; if (blockBG) { overLayDiv_id = commonFunc.overLayDiv(true); } if (closeButton == true) { commonFunc.visibleElement("popCloseButton", false); } if (divId != undefined) { messDivObj = commonFunc.getElementObj(divId); commonFunc.getElementObj("mainPopUpTitle").innerHTML = messDivObj.title; // commonFunc.getElementObj("mainPopupBody").innerHTML = messDivObj.innerHTML; buttonDivObj = commonFunc.getElementObj(divId + "Button"); if (buttonDivObj != null) commonFunc.getElementObj("mainPopupButton").innerHTML = buttonDivObj.innerHTML; popupDiv = commonFunc.getElementObj("mainPopupDiv"); popupDiv.style.width = messDivObj.style.width; messDivObj.style.width = "100%"; //popupDiv.style.height = messDivObj.style.height; } commonFunc.divPopUp('mainPopupDiv',true); commonFunc.getElementObj("mainPopUpTitleAnchor").focus(); } catch(e) { // do nothing } } // end of showPopup function closePopup(divId) { try { overLayDiv_id = commonFunc.overLayDiv(false); if (divId == undefined) { commonFunc.divPopUp('mainPopupDiv',false); commonFunc.getElementObj("mainPopupBody").innerHTML = ""; commonFunc.visibleElement("popCloseButton", true); commonFunc.getElementObj("mainPopupButton").innerHTML = ""; } else commonFunc.divPopUp(divId,false); } catch(e) { // do nothing } } function HideSelectOption(p) { try { // for IE6 var version=navigator.appVersion; if (version.indexOf("MSIE 6.0") != -1){ var select_array = document.getElementsByTagName("select"); for (i=0; i