﻿//clientscripts - This service include critial client side javascript functions


//
//create prototype extensions for existing functions
String.prototype.cs_trim=function() { return this.replace(/^\s*|\s*$/g,''); }
String.prototype.cs_ltrim=function() { return this.replace(/^\s*/g,''); }
String.prototype.cs_rtrim=function() { return this.replace(/\s*$/g,''); }


//
//create dyna variable functions
function cs_setDynaVariable(sObject, sVariable, vValue) { var oObject=document.getElementById(sObject); if(oObject) { oObject.setAttribute(sVariable, vValue); }} //this function adds a dynamic attribute to the specified object
function cs_getDynaVariable(sObject, sVariable) { var oObject=document.getElementById(sObject); if(oObject) { return(oObject.getAttribute(sVariable)); }} //this function gets a dynamic attribute from the specified object


function cs_loadDynaValue(sObject, vValue) { var oObject=document.getElementById(sObject); if(oObject) { oObject.value=vValue; }} //this function loads the object with the specified value


function cs_serialiseValues(vSerialValues) {
//this function returns a serial string containing the values - the function filters nulls and undefined values
var vArg, cSOH=String.fromCharCode(1), sSerialised=cSOH, cUnitSeperator=String.fromCharCode(31), cUndefined=String.fromCharCode(4), cObject=String.fromCharCode(24);
	for (var n=0; n<=((arguments.length)-1); n++) { 
		switch(typeof(arguments[n])) { case 'undefined': vArg=cUndefined; break; case 'object': vArg=cObject; break; default: vArg=arguments[n]; break; } //filter values to replace undefined, or null values
		sSerialised+=vArg; if(n<((arguments.length)-1)) { sSerialised+=cUnitSeperator; } //add end of field seperator
	}
	return(sSerialised);
}

function cs_extractValues(sSerialisedValues) {
//this function returns an array element (base 0) containing all the variables serialised - the function filters nulls and undefined values
var rsExtractValues, vValue, cSOH=String.fromCharCode(1), cUnitSeperator=String.fromCharCode(31), cUndefined=String.fromCharCode(4), cObject=String.fromCharCode(24);
	if(sSerialisedValues.substr(0,1)==cSOH) {
		sSerialisedValues=sSerialisedValues.replace(cSOH,''); rsExtractValues=sSerialisedValues.split(cUnitSeperator);
		for(var lElement in rsExtractValues) { switch(rsExtractValues[lElement]) { case cUndefined: rsExtractValues[lElement]=undefined; break; case cObject: rsExtractValues[lElement]=null; break; default: rsExtractValues[lElement]=rsExtractValues[lElement]; break; }}
	}
	return(rsExtractValues);
}


//
//
//standard support functions
function cs_IsNumeric(vValue) { var validChars = "0123456789.", bStatus=true, singleChar, lCount; for (lCount = 0; lCount < vValue.length && jsf_IsNumeric == true; lCount++) { singleChar = vValue.charAt(lCount); if (validChars.indexOf(singleChar) == -1) { bStatus=false; }	} return bStatus; } //this function returns true/false depending on whether the value specified is numeric


function cs_getBackgroundColour() { return document.bgColor; } //this function returns the document background colour


function cs_setObjectColour(sObject, sBackgroundHexColour, sForgroundHexColour) { var oObject; document.getElementById(sObject); if(oObject) { oObject.style.backgroundColor=sBackgroundHexColour; oObject.style.color=sForgroundHexColour; }} //this function sets the object colour
function cs_setObjectBorder(sObject, sBorderHexColour, sBorderWidth) { var oObject; document.getElementById(sObject); if(oObject) { oObject.style.borderColor=sBorderHexColour; oObject.style.borderWidth=sBorderWidth; }} //this function sets the object border


function cs_setObjectFocus(sObject) { var oObject=document.getElementbyId(sObject); if(oObject) { oObject.select(); oObject.focus(); }} //this function sets the specified object


function cs_deleteCookie(sName) { cs_deleteCookie(sName,"-1", -1); } //remove a cookie by setting its expiry date to the passed
function cs_setCookie(sName, vValue, lDays) { var dToday = new Date(), dExpire = new Date(); if (lDays==null || lDays==0) { lDays=1; } dExpire.setTime(dToday.getTime() + 3600000*24 * lDays); document.cookie = sName+"="+escape(vValue) + ";expires="+dExpire.toGMTString(); } //set a cookie that will expire in lDays
function cs_readCookie(sName) { var sCookie=""+document.cookie, ind=sCookie.indexOf(sName), ind1=sCookie.indexOf(';',ind); if (ind==-1 || sName=="") { return ""; } if (ind1==-1) { ind1=sCookie.length; } return unescape(sCookie.substring(ind+sName.length+1,ind1)); } //read a cookie
function cs_cookiesActive() { var lStatus=0; cs_setCookie('ActiveTest', 1); if(cs_readCookie('ActiveTest')==1) { lStatus=1; cs_deleteCookie('ActiveTest');} return lStatus; } //this tests whether cookies are active on the users browser, returns 0 if disabled


function cs_switchCheckbox(oObject) { switch(oObject.checked) { case true: oObject.value="Yes"; break; case false: oObject.value="No"; break; }} //this function switches the specified checkbox value depending on the user clicking
function cs_disableElement_ViaCheckbox(sObjectCheckbox, sObjectElement) { switch(document.getElementById(sObjectCheckbox).value) { case "Yes": document.getElementById(sObjectElement).disabled=false; break; case "No": document.getElementById(sObjectElement).disabled=true; break; } } //this function disables the specified checkbox

function cs_launchBrowserWindow(sURL, sTitle) {
	window.open(sURL, sTitle, "height=300, width=300, left=10, top=10, menubar=no, toolbar=no, scrollbars=yes, status=no, resizeable=no, dependent=yes, location=no")
}

