// Created: 01/06/2008 20:29 // common.js: Contains general functions for web content // // classes // ------- // $(strID) // strBuffer() // getParentKey(strKey) // getParent(strKey) // getUIV(strKey) // checkFlagValue(intFlag, intCheck) // XMLify(strIn) // unix2Local(dtIn) // trueBool(strIn) // isBool(strIn) // sec2hms(duration) // unixToLocale(dateIn, strType) // localeToUnix() // getQParam(name) // getPosition(e) // isDisabled(element) // buttonEnable(element) // buttonDisable(element) // myContext() // unixToLocale(dateIn, strType) // getExtn(strIn) // showLoading(tgt) // checkLevel(thisKey, myKey) // showDivUp(strID) // hideDivUp(strID) // checkRN(strIn) // sortObject(data, direction, by, isNumber) // sortFunc(direction, by, isNumber) // toCurrency(intIn) // myContext() function $(strID) { //Returns the targetted element. //Shortcut for getElementById return document.getElementById(strID); } var Ajax = { "Request": function(strURL, objParams) { var http; var options = objParams; //Create a socket on server, trying different browser tech (most modern first) try { http = new XMLHttpRequest(); } catch (e) { try { http = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { return null; } } //Create request to server http.onreadystatechange = function() { if (http.readyState > 1) { alert("Readystate = [" + http.readyState + "]"); } if(http.readyState == 4) { if(http.status) { //If the connection aborts, the status code is 0 //Set up generic success/failure commands var pass = "Failure"; if((http.status >= 200) && (http.status < 300)) { pass = "Success"; } try { //Try to use onXXX callback for specific status codes options["on" + http.status](http); } catch(e) { try { //Use generic success/failure commands options["on" + pass](http); } catch (E) { try { //Call the exception callback options.onException(http, E); } catch (ee) { //Do nothing extra } } } } } } if(objParams.method == "post") { http.open("POST", strURL, true); http.setRequestHeader("Content-Type", options.contentType || "application/x-www-form-urlencoded"); //Iterate through "headers" array to create user headers for(var c = 0; c < options.headers.length; c+=2) { http.setRequestHeader(options.headers[c], options.headers[c+1]); } http.send(options.postBody || null); } else { http.open("GET", strURL, true); http.send(options.postBody || null); } } } //Basic string building function function strBuffer() { this.buffer = []; } strBuffer.prototype.append = function append(strIn) { this.buffer.push(strIn); return this; } strBuffer.prototype.toString = function toString() { return this.buffer.join(""); } var cookie = { "create": function(strName, strVal, intDays) { if(intDays) { var date = new Date(); date.setTime(date.getTime() + (intDays*24*3600*1000)); var expiry = "; expires=" + date.toGMTString(); } else { var expiry = ""; } document.cookie = strName + "=" + strVal + "; path=/"; }, "read": function(strName) { var nameEQ = name + "="; var aryCookie = document.cookie.split(";"); for(var c = 0; c < aryCookie.length; c++) { var pos = aryCookie[c]; while(pos.charAt(0) == " ") { pos = pos.substring(1, pos.length); } if(pos.indexOf(nameEQ) == 0) { return pos.substring(nameEQ.length, pos.length); } } return null; }, "delete": function(strName) { cookie.create(strName, "", -1); } } function getParentKey(strKey) { if(strKey) { var keyList = strKey.split("\\\\"); var strOut = ""; for(var c = 0; c < (keyList.length -1); c++) { strOut += keyList[c] + "\\\\"; } return "\\\\" + strOut; } return ""; } function getParent(strKey) { var strOut = ""; if(strKey) { var keyList = strKey.split("\\"); for(var c = 0; c < (keyList.length -2); c++) { strOut += keyList[c] + "\\"; } } return strOut; } function getUIV(strKey) { var keyList = strKey.split("\\"); return keyList[keyList.length-2]; } function checkFlagValue(intFlag, intCheck) { if((intFlag & intCheck) == intCheck) { return true; } else { return false; } } function XMLify(strIn) { if(strIn) { return String(strIn).replace(/\&/g,"&").replace(/\/g,">").replace(/\"/g,""").replace(/\'/g,"'"); } return strIn; } function unix2Local(dtIn) { this.dt = new Date(dtIn*1000); return this.dt.toLocaleString(); } function trueBool(strIn) { if(strIn.toLowerCase() == "true") { return true; } return false; } function isBool(strIn) { if(String(strIn).search(/^true$|^false$/) > -1) { return true; } return false; } function sec2hms(duration) { if(!isNaN(duration)) { var Hours = Math.floor(duration/3600); if(Hours < 10) { Hours = "0" + Hours; } var Minutes = Math.floor((duration/60) % 60); if(Minutes < 10) { Minutes = "0" + Minutes; } var Seconds = Math.floor(duration % 60); if(Seconds < 10) { Seconds = "0" + Seconds; } return Hours + ":" + Minutes + ":" + Seconds; } return duration; } String.prototype.truncate = function truncate(length, truncation) { truncation = truncation ? truncation : ""; return (this.length > length) ? this.substring(0, length - truncation.length) + truncation : this; } String.prototype.trim = function(){ return this.replace(/^\s+|\s+$/g, ''); } function unixToLocale(dateIn, strType) { var dd = new Date(dateIn * 1000); switch(strType) { case "time": var format = "toLocaleTimeString"; break; case "date": var format = "toLocaleDateString"; break; default: var format = "toLocaleString"; } // + (dd.getTimezoneOffset() * 60000) return new Date(dd.getTime())[format](); } function localeToUnix() {} function getQParam(name) { name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); var regexS = "[\\?&]"+name+"=([^&#]*)"; var regex = new RegExp(regexS); var results = regex.exec(window.location.href); return results ? results[1] : ""; } function getPosition(e) { var left = 0; var top = 0; while (e.offsetParent) { left += e.offsetLeft; top += e.offsetTop; e = e.offsetParent; } left += e.offsetLeft; top += e.offsetTop; return {x:left, y:top}; } function isDisabled(element) { if(element && element.className.search(/disabled/) > -1) { return true; } return false; } function buttonEnable(element) { if(element) { element.className = element.className.replace(/\sdisabled/, ""); } } function buttonDisable(element) { if(element && element.className.search(/disabled/) == -1) { element.className += " disabled"; } } function myContext() { return { key: sessionContext, name: sessionName }; } function unixToLocale(dateIn, strType) { var dd = new Date(dateIn * 1000); switch(strType) { case "time": var format = "toLocaleTimeString"; break; case "date": var format = "toLocaleDateString"; break; default: var format = "toLocaleString"; } // + (dd.getTimezoneOffset() * 60000) return new Date(dd.getTime())[format](); } function getExtn(strIn) { var index = strIn.lastIndexOf("."); return strIn.substring(index); } function showLoading(tgt) { $(tgt).innerHTML = "
Loading...
Loading
"; } function checkLevel(thisKey, myKey) { if(thisKey.indexOf(myKey) != -1) { return myKey; } return ""; } function showDivUp(strID) { $("bg").style.display = "block"; $(strID).style.display = "block"; $("scrollbox").style.overflow = "hidden"; $("scrollbox").style.marginRight = "17px"; } function hideDivUp(strID) { $("bg").style.display = "none"; $(strID).style.display = "none"; $("scrollbox").style.overflow = "scroll"; $("scrollbox").style.overflowX = "hidden"; $("scrollbox").style.marginRight = ""; } String.prototype.cleanHTML = function cleanHTML() { return this.replace(/<.*?>/g, ""); } function checkRN(strIn) { if(strIn.search(/\r\n/) != -1) { return strIn; } if(strIn.search(/\n/) != -1) { var strOut = strIn.split("\n"); var out = ""; for(var c = 0; c < strOut.length; c++) { out += strOut[c] + "\r\n"; } return out; } return strIn; } var sortInProgress = false; function sortObject(data, direction, by, isNumber) { if(!sortInProgress) { sortInProgress = true; var sortArray = new Array(); var objectName = ""; for(var each in data) { if(!objectName) { objectName = String(each).match(/^\D+/); } sortArray.push(data[each]); } try { sortArray.sort(sortFunc(direction, by, isNumber)); } catch(e) { alert(e.description) } var returnData = {}; for(var c = 0; c < sortArray.length; c++) { returnData[objectName + (sortArray[c].uiv || sortArray[c].id)] = sortArray[c]; } sortInProgress = false; return returnData; } } function sortFunc(direction, by, isNumber) { return function(a, b) { var checkA = isNumber ? parseInt(a[by]) : a[by]; var checkB = isNumber ? parseInt(b[by]) : b[by]; if(direction == "asc") { return checkA < checkB ? -1 : (checkA > checkB ? 1 : 0); } else { return checkA > checkB ? -1 : (checkA < checkB ? 1 : 0); } } } function toCurrency(intIn) { var num = 1000.01.toLocaleString(); var decSeperator = num.substr(5, 1); var intOut = Number(Number(intIn.replace(",", "")).toFixed(2)).toLocaleString(); if(intOut.lastIndexOf(decSeperator) == -1) { intOut += decSeperator + "00"; } else { if(intOut.substring(intOut.lastIndexOf(decSeperator) + 1).length != 2) { intOut += "0"; } if(intOut.substring(intOut.lastIndexOf(decSeperator) + 1).length != 2) { intOut += "0"; } } return intOut; } var existingUsers = "

What's going on?

"; existingUsers+= "

As part of a major systems improvement, we're in the process of upgrading all our on-line services.

"; existingUsers+= "

Because of the multi-tiered security that this new system employs, it is necessary to close all existing accounts, forcing them to change over to the new style.

"; existingUsers+= "

We're sorry if this causes you any inconvenience but hope this hassle will be offset by the improved facilities available.

"; existingUsers+= "Close"; function divUp(element, strMessage) { if(!$("div_up")) { var pos = getPosition(element); var newDiv = document.createElement("div"); newDiv.id = "div_up"; newDiv.innerHTML = strMessage; newDiv.style.top = (pos.y + element.offsetHeight + 8) + "px"; document.body.appendChild(newDiv); newDiv.style.left = pos.x + element.offsetWidth - newDiv.offsetWidth + "px"; } } function checkForm() { var err = ""; if(!$("customerid").value.trim() || $("customerid").value.trim().search(/\D+/) > -1) { err += " > Customer ID\r\n"; } if($("email").value.trim().search(/^(.+)@(.+)\.(.+)$/) == -1) { err += " > Email address\r\n"; } if(err) { alert("The following details were not filled out correctly:\r\n\r\n" + err + "\r\nPlease check your details and try again."); return false; } return true; } function checkLoginForm() { var err = ""; if(!$("login_company").value.trim()) { err += " > Company name\r\n"; } if($("login_email").value.trim().search(/^(.+)@(.+)\.(.+)$/) == -1) { err += " > Email address\r\n"; } if(err) { alert("The following details were not filled out correctly:\r\n\r\n" + err + "\r\nPlease check your details and try again."); return false; } return true; } function slideDown(strID) { var element = $(strID); if(element) { if(element.offsetTop > -1) { element.style.top = "0"; prepareSnow(); } else { element.style.top = element.offsetTop + 2 + "px"; setTimeout("slideDown('" + strID + "')", 30); } } } function createSystemMessage(msg, backgroundColor, textColor) { if($("system_message")) { $("global_menu").removeChild($("system_message")); } if(msg) { var msgDiv = document.createElement("div"); var msgClose = document.createElement("a"); var msgImg = document.createElement("img"); msgImg.src = "/common/img/button_close_mini.gif"; msgImg.alt = ""; msgImg.title = "Close this panel"; msgClose.className = "system_close"; msgClose.appendChild(msgImg); msgClose.onclick = destroySystemMessage; msgDiv.innerHTML = msg; msgDiv.appendChild(msgClose); msgDiv.style.backgroundColor = backgroundColor; msgDiv.style.color = textColor; msgDiv.id = "system_message"; $("global_menu").appendChild(msgDiv); $(msgDiv.id).style.top = "-" + $(msgDiv.id).offsetHeight + "px"; setTimeout("slideDown('" + msgDiv.id + "')", 1000); } } function destroySystemMessage() { if($("system_message")) { $("system_message").parentNode.removeChild($("system_message")); } }