﻿ // <![CDATA[
 
//clientscripts - This service include critial client side javascript functions
var _csDefault_IgnoreZIndexsAbove=1000000;


//
//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,''); }


//this function colapses/expands an object and changes the control object to +/-
function cs_collapseElement(sObject, sCtrlObject) { var oObject=document.getElementById(sObject); if(oObject.style.display=='none'){ oObject.style.display='block'; sCtrlObject.innerHTML=unescape('-'); }else{ oObject.style.display='none'; sCtrlObject.innerHTML=unescape('+'); }}


//
//
//package variables support
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_IsIE() { return Prototype.Browser.IE; } //this function returns true/false

function cs_Focus(sObject) { var oObject=$(sObject); if(oObject) { oObject.select(); oObject.focus(); }} //this function focus's the specified object
function cs_Blur(sObject) { var oObject=$(sObject); if(oObject) { oObject.blur(); }} //this function blurs the specified object

function cs_Form(sObject) { var oObject, sName, sId; oObject=$(sObject); if(oObject) { try { sName=oObject.form.name; } catch(err) { sName=''; } try { sId=oObject.form.id; } catch(err) { sId=''; }} return { name: sName, id: sId };} //this function returns multiple values - you must select the appropriate return code or assign the function to a variable


function cs_MaximumZ(lIgnoreLimit) {
//this functions returns the maximum zindex, uses _csDefault_ZLimit to define a limit if not supplied
var oObjects=document.getElementsByTagName('div'), lCount, lMaxZIndex=0, lZIndex=0;
	if(!cs_IsNumeric(lIgnoreLimit)) { lIgnoreLimit=_csDefault_IgnoreZIndexsAbove; }
	for(lCount=0;lCount<oObjects.length;lCount++) { try { lZIndex=cs_getObjectProperty(oObjects[lCount]).zindex; } catch(err) { lZIndex=0; }
		if((lZIndex<lIgnoreLimit) && (lZIndex>lMaxZIndex)) { lMaxZIndex=lZIndex; }
	}
	return(lMaxZIndex+1);
}



//
//
//string support functions
function cs_StringLength(vValue) { var sString; try { sString=String(vValue); } catch(err) { sString=''; }; return sString.length; } //this function returns the length of the specified string


function cs_splitString(sString, sSeperator) { var rsSplitArray='', lElements=-1; if(cs_StringLength(sString)>0) { rsSplitArray=sString.split(sSeperator); lElements=rsSplitArray.length; if(lElements>0) { lElements--; }} return{ string: rsSplitArray, elements: lElements };}


//
//
//validation support functions
function cs_IsAlpha(vValue) { var regEx=/^[a-zA-Z\W]{0,}$/, bStatus=false; if(regEx.test(vValue)) { bStatus=true; } return bStatus; } //this function returns true/false depending on whether the vValue is alpha
function cs_IsAlphaNumeric(vValue) { var regEx=/^[a-zA-Z_0-9\s\W]{0,}$/, bStatus=false; if(regEx.test(vValue)) { bStatus=true; } return bStatus; } //this function returns true/false depending on whether the vValue is alphanumeric
function cs_IsNumeric(vValue) { var regEx=/^-{0,1}\d*\.{0,1}\d+$/, bStatus=false; if(regEx.test(vValue)) { bStatus=true; } return bStatus; } //this function returns true/false depending on whether the vValue is numeric
function cs_IsEmail(vValue) { var regEx=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/, bStatus=false; if(regEx.test(vValue)) { bStatus=true; } return bStatus; } //this function returns true/false depending on whether the vValue is an email format
function cs_IsPhone(vValue) { var regEx= /^(\+\d{0,})*\s*(\(\d{0,}\)\s*)*\d{0,}(-{0,1}|\s{0,1})\d{0,}(-{0,1}|\s{0,1})\d{0,}$/, bStatus=false; if(regEx.test(vValue)) { bStatus=true; } return bStatus; } //this function returns true/false depending on whether the vValue is an phone format +XX (XX) XXX-XXXX etc.
function cs_IsUKDate(vValue) { var regEx=/^((((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$/, bStatus=false; if(regEx.test(vValue)) { bStatus=true; } return bStatus; } //this function returns true/false depending on whether the vValue is ukdate dd/mm/yyyy
function cs_IsUSDate(vValue) { var regEx=/^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/, bStatus=false; if(regEx.test(vValue)) { bStatus=true; } return bStatus; } //this function returns true/false depending on whether the vValue is usdate mm/dd/yyyy
function cs_IsBlank(vValue) { var bStatus=false; if(cs_StringLength(vValue)==0) { bStatus=true;} return bStatus;} //this function returns true/false depending on whether the vValue is blank

function cs_ValidatePassword(vValue, lMinChars, lMaxChars, lLower, lUpper, lNumeric) { 
//this password returns the status of the specified password
var bStatus=false, lowerChars=0, upperChars=0, numericChars=0, sMessage='', lMode=0, lLength=cs_StringLength(vValue);
	if(cs_IsAlphaNumeric(vValue)) {
		if(lLength>=lMinChars && lLength<=lMaxChars) {
			sMessage='password: okay'; bStatus=true; lowerChars=(vValue.match(/[a-z]/g)); upperChars=(vValue.match(/[A-Z]/g)); numericChars=(vValue.match(/[0-9]/g)); //calculate character counts
			try { lowerChars=parseInt(lowerChars.length, 10); } catch(err) { lowerChars=0; } try { upperChars=parseInt(upperChars.length, 10); } catch(err) { upperChars=0; } try { numericChars=parseInt(numericChars.length, 10); } catch(err) { numericChars=0; }		
			if(lowerChars<lLower) { bStatus=false; sMessage='password: too few lowercase characters'; lMode=-3; } 
			if(upperChars<lUpper) { bStatus=false; sMessage='password: too few uppercase characters'; lMode=-4; } 
			if(numericChars<lNumeric) { bStatus=false; sMessage='password: too few numeric characters'; lMode=-5; }
		} else { sMessage='password: unexpected length'; lMode=-2; }
	} else { sMessage='password: invalid'; lMode=-1; }
	return{status: bStatus, message: sMessage, mode: lMode};
 }



//
//
//
//pre-built quick call object functions
function cs_setObjectVariable(sObject, sVariable, vValue) { var oObject=$(sObject); if(oObject) { oObject.setAttribute(private_supportFilterAttributeName(sVariable), vValue); }} //this function sets the object variable/attribute
function cs_getObjectVariable(sObject, sVariable) { var oObject=$(sObject); if(oObject) { return(oObject.getAttribute(private_supportFilterAttributeName(sVariable))); }} //this function gets the object variable/attribute
function cs_setObjectValue(sObject, vValue) { var oObject=$(sObject); if(oObject) { oObject.value=vValue; }} //save the objects value
function cs_getObjectValue(sObject) { var oObject=$(sObject); if(oObject) { return(oObject.value); }} //recover the objects value
function cs_setObjectZIndex(sObject, lZIndex) { var oObject=$(sObject); if(oObject) { oObject.style.zIndex=lZIndex;}} //this function sets the zIndex
function cs_setObjectSourceAttribute(sObject, sSource, sTitle) { var oObject=document.getElementById(sObject); if(oObject) { try { oObject.src=sSource; oObject.title=sTitle; } catch(err) {}}} //this function sets the specified objects source


function cs_setObjectColour(sObject, sBackgroundHexColour, sForgroundHexColour) { var oObject=$(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=$(sObject); if(oObject) { oObject.style.borderColor=sBorderHexColour; oObject.style.borderWidth=sBorderWidth; }} //this function sets the object border
function cs_setObjectDisabled(sObject, bDisabled) { var oObject=$(sObject); if(oObject) { if(bDisabled) { oObject.disabled=true; } else { oObject.disabled=false; }}} //this function disables/enables the specified element



function cs_getObjectProperty(oObject) {
//this function returns the specified properties base on the supplied object
var oStyle, lZIndex=0, lHeight=0, lWidth=0, sClassName='', sPosition='', sVisibility='', bReadOnly=false, vValue='', bStatus=false;
	if(oObject) { //confirm object
		bStatus=true;
		sClassName=oObject.className; 
		bReadOnly=oObject.getAttribute('readonly');
		vValue=oObject.value; 

		if(cs_IsIE()) { //create object shortcut - microsoft ie
			oStyle=oObject.currentStyle; 
			lZIndex=oStyle['zIndex']; //an alternative version is: lZIndex=oObject.currentStyle.zIndex;
			lHeight=oStyle.height; 
			lWidth=oStyle.width; 
			sPosition=oStyle.position; 
			sVisibility=oStyle.visibility;
		} else { //create object shortcut - non ie browsers
			oStyle=document.defaultView.getComputedStyle(oObject, null); 
			lZIndex=oStyle.getPropertyValue('z-index'); 
			lHeight=oStyle.getPropertyValue('height'); 
			lWidth=oStyle.getPropertyValue('width'); 
			sPosition=oStyle.getPropertyValue('position'); 
			sVisibility=oStyle.getPropertyValue('visibility');
		}
	}
	try { lZIndex=parseInt(lZIndex, 10); } catch(err) { lZIndex=0; } //apply numeric filtering
	return { zindex: lZIndex, height: lHeight, width: lWidth, classname: sClassName, position: sPosition, visibility: sVisibility, readonly: bReadOnly, value: vValue, status: bStatus };
}



//
//
//beta functions
function cs_resetValues(sForm) {
//this function resets all the selected form element values
var oForm=document.getElementById(sForm), oElement;

	for(var i=0; i<=(oForm.elements.length-1); i++) { oElement=oForm.elements[i];
		switch(oElement.type.toLowerCase()) {
			case 'button':			break;
			case 'checkbox':	if(oElement.checked) { oElement.checked=false; }; break; 
			case 'file':				break;
			case 'hidden':			break;
			case 'image':			break;
			case 'password':		oElement.value=''; break;
			case 'radio':			if(oElement.checked) { oElement.checked=false; }; break;
			case 'select-one':	oElement.selectedIndex=-1; break;
			case 'select-multiple':	oElement.selectedIndex=-1; break;
			case 'submit':			break;
			case 'text':				oElement.value=''; break;
			case 'textarea':		oElement.value=''; break;
		}
	}
}

function cs_ListAll(oObject) {
	for (var name in oObject) {  
		alert(name); 
	}
}



//
//
//cookie services
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



//
//
//conversion functions
function cs_convertBooleanBit(bBoolean) { switch(bBoolean) { case true: return(1); break; default: return(0); }} //this function converts a javascript boolean into a 1 or 0
function cs_convertBitBoolean(lBit) { try{ lBit=parseInt(lBit, 10); } catch(err) { lBit=0; }; switch(lBit) { case 1: return(true); break; case -1: return(true); break; default: return(false); }} //this function converts a 1 or -1 into a javascript boolean true
function cs_appendString(sOriginal, sAppend, sPrefix) {var sString=''; if(cs_IsAlphaNumeric(sOriginal)) { sString=sOriginal;} if(!cs_IsAlphaNumeric(sPrefix)) { sPrefix='';}; if(cs_IsAlphaNumeric(sAppend)) { if(sAppend.length>0) { sString=sString + sPrefix + sAppend; }};	return(sString);} //this function returns the appended string to the specified original



//
//
//html management functions
function cs_fireEvent(oObject, sEvent) { var csEvent; if(cs_IsIE()) { csEvent=document.createEventObject(); return oObject.fireEvent('on'+sEvent, csEvent); } else { csEvent=document.createEvent('HTMLEvents'); csEvent.initEvent(sEvent, true, true); 	return !oObject.dispatchEvent(csEvent); }} //this function creates a dynamic event call

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") }



//
//
//private support functions
function private_supportFilterAttributeName(sAttribute) { return(sAttribute); }

// ]]>