// GLOBAL JAVASCRIPT USED THROUGHOUT THE WEBSITE

	var hide = false;
	
	function toggleTransDisplay(){		
	
		if(document.all){
			var tableTransTemp = document.all.tableTrans
		}
		else {
			var tableTransTemp = document.getElementById('tableTrans')			
		}
		var rows = tableTransTemp.rows;
		
		for (i=1;i<=rows.length-1;i++){
			//alert(i + " - " + rows[i].id)
			if (rows[i].id == 'transHide'){
				if (hide){
					rows[i].style.visibility = 'hidden';
					rows[i].style.display = 'none';
				}
				else{
					rows[i].style.visibility = 'visible';					
					rows[i].style.display = '';
				}
			}
		}
		if (hide){
			hide = false;
			document.getElementById('expandButton').innerHTML = "<img id='expandImage' src='images/essentials/icon_expand.gif' style='margin-right: 5px;' align='absbottom' border='0'>Show More Transactions";

		}
		else if (!hide){
			hide = true;
			document.getElementById('expandButton').innerHTML = "<img id='expandImage' src='images/essentials/icon_collapse.gif' style='margin-right: 5px;' align='absbottom' border='0'>Hide";			
		}
		
	}

	function passwordStrength(password){
		var desc = new Array();
		desc[0] = ""
		desc[1] = "Very Weak";
		desc[2] = "Weak";
		desc[3] = "Better";
		desc[4] = "Medium";
		desc[5] = "Strong";
		desc[6] = "Strongest";
	
		var score = 1;
	
		//if password bigger than 6 give 1 point
		if (password.length > 6) score++;
	
		//if password has both lower and uppercase characters give 1 point	
		if ( ( password.match(/[a-z]/) ) && ( password.match(/[A-Z]/) ) ) score++;
	
		//if password has at least one number give 1 point
		if (password.match(/\d+/)) score++;
	
		//if password has at least one special caracther give 1 point
		if ( password.match(/.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/) )	score++;
	
		//if password bigger than 12 give another 1 point
		if (password.length > 12) score++;
		
		if (password.length > 0){
			document.getElementById("passwordDescription").innerHTML = desc[score];
			document.getElementById("passwordStrength").className = "strength" + score;
		}
	}

	function openMoreInfo(){
		//OPENS THE CAPTCHA MORE INFO WINDOW
		openPopup('../includes/captcha_moreinfo.asp','Captcha','width=300,height=220,status=no,scrollbars=yes,resizable=no,toolbar=no,menubar=no');
	}

	function checkNum(oObj){
		//ENSURE ONLY DECIMALS ARE ALLOWED IN A TEXTBOX
		//Only aLlows one decimal separator (. or ,). Doesn't allow decimal separator as the first char.
		// use via onKeyPress="checkNum(this)" on the textbox

		var bHasDecimal = false;
		if (oObj.value.indexOf('.') > 0 || oObj.value.indexOf(',') > 0 || oObj.value == ''){
			bHasDecimal = true
		}
		var carCode = event.keyCode;
		if ((carCode < 48) || (carCode > 57)){
			if ((carCode != 44) && (carCode != 46)){
				//44 = COMMA; 46 = FULLSTOP
				event.cancelBubble = true;
				event.returnValue = false;
			}
			else if (bHasDecimal){
				event.cancelBubble = true;
				event.returnValue = false;
			}
		}
	}
	
	function checkNumInt(oObj){
		//ENSURE ONLY NUMBERS ARE ALLOWED IN A TEXTBOX
		var carCode = event.keyCode;
		if (((carCode < 48) || (carCode > 57))){
			event.cancelBubble = true;
			event.returnValue = false;
		}
	}
	
	function checkNumException(oObj, intKeyCode){
		//ENSURE ONLY NUMBERS PLUS ONE EXCEPTIONAL KEYCODE ARE ALLOWED IN A TEXTBOX
		var carCode = event.keyCode;
		if (((carCode < 48) || (carCode > 57)) && (carCode != intKeyCode)){
			event.cancelBubble = true;
			event.returnValue = false;
		}
	}
	
	function limitText(limitField, limitNum) {
		//LIMITS A FIELD TO CERTAIN LENGTH. USED FOR TEXTAREAS
    	if (limitField.value.length > limitNum) {
        	limitField.value = limitField.value.substring(0, limitNum);
	    } 
	}


	function validateLogin(){
	//VALIDATES LOGIN FORMS
		if(document.LoginForm.username){
			if(document.LoginForm.username.value == ""){
			
				document.getElementById("lblUsername").style.display = "inline";
				return(false);
			}
			if(document.LoginForm.password.value == ""){
				document.getElementById("lblPassword").style.display = "inline";
				return(false);
			}
			if(document.LoginForm.txtCaptcha.value == ""){
				document.getElementById("lblCaptcha").style.display = "inline";
				return(false);
			}
			
		}
		else {
			return(true);
		}
	}

	function validateSearch() {
	//ENSURE A KEYWORD IS ENTERED
		if(document.searchform.keywords.value == "Enter keyword" || document.searchform.keywords.value == ""){
			alert("Please enter a keyword");
			return (false);
		}
		else{
			return (true);
		}
	}

	function openPopup(popURL,popTitle,popProperties) {
		//OPEN A POP-UP WINDOW
		myPopUp = window.open(popURL,popTitle,popProperties);
		if (!myPopUp.opener) {
			myPopUp.opener = self;
		}
	}


	function openChat(varUrl) {
		//OPENS CHAT WINDOW FOR LIVE HELP MODULE
		var winleft=(screen.width-700)/2;
		var wintop=(screen.height-450)/2;
		openPopup(varUrl,'LiveHelp','toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=0,width=700,height=480,top='+wintop+',left='+winleft)
	}


	function writeStatus(text) {
		//WRITE MESSAGE TO THE STATUS BAR
		window.status = text;
		return(true);
	}

	function clearStatus() {
		//CLEAR THE STATUS BAR MESSAGE
		window.status = "";
	}

	function findPosX(obj) {
		//FIND THE X POSITION OF AN OBJECT
		var curleft = 0;
		if (document.getElementById || document.all) {
			while (obj.offsetParent) {
				curleft += obj.offsetLeft
				obj = obj.offsetParent;
			}
		}
		else if (document.layers) {
			curleft += obj.x;
		}
		return curleft;
	}

	function findPosY(obj) {
		//FIND THE Y POSITION OF AN OBJECT
		var curtop = 0;
		var printstring = '';
		if (document.getElementById || document.all) {
			while (obj.offsetParent) {
				printstring += ' element ' + obj.tagName + ' has ' + obj.offsetTop;
				curtop += obj.offsetTop
				obj = obj.offsetParent;
			}
		}
		else if (document.layers) {
			curtop += obj.y;
		}
		//window.status = printstring;
		return curtop;
	}

	function getObj(name) {
		//GET THE BROWSER-COMPATIBLE REFERENCE FOR AN OBJECT
		if (document.getElementById) {
			this.obj = document.getElementById(name);
			this.style = document.getElementById(name).style;
		}
		else if (document.all) {
			this.obj = document.all[name];
			this.style = document.all[name].style;
		}
		else if (document.layers) {
			if (document.layers[name]) {
				this.obj = document.layers[name];
				this.style = document.layers[name];
			}
		}
	}

	function fadeLayer(lyr,dir,arr,item,start) {
		//FADES A LAYER IN AND OUT
		var objLyr = document.getElementById(lyr);
		if(start == true){
			objLyr.innerHTML = arr[item];
			item += 1;
		}
		else if(item >= arr.length){
			item = 0;
		}
		if(dir > 0) {
			//FADE IN
			opac+=incr;
			if(objLyr.filters.alpha != null){
				if(ie5) objLyr.filters.alpha.opacity = opac;
			}
			if(ns6) objLyr.style.MozOpacity = opac/100;
			if (opac >= 100) {
				if(arr.length == 1){
					//HALT FADING IF THERE IS ONLY ONE ITEM IN THE ARRAY
					return false;
				}
				else {
					setTimeout(function () { fadeLayer(lyr,-1,arr,item,false); }, wait);
				}
			}
			else {
				setTimeout(function () { fadeLayer(lyr,dir,arr,item,false); }, intvl);
			}
		}
		else if (dir < 0) {
			//FADE OUT
			opac-=incr;
			if(objLyr.filters.alpha != null){
				if(ie5) objLyr.filters.alpha.opacity = opac;
			}
			if(ns6) objLyr.style.MozOpacity = opac/100;
			if (opac <= 0) {
				//UPDATE THE LAYER'S TEXT
				objLyr.innerHTML = arr[item];
				item += 1;
				setTimeout(function () { fadeLayer(lyr,1,arr,item,false); }, intvl);
			}
			else {
				setTimeout(function () { fadeLayer(lyr,dir,arr,item,false); }, intvl);
			}
		}
	}
	var ie5 = (document.all && document.getElementById); 
	var ns6 = (!document.all && document.getElementById); 
	var opac = 0;
	var incr = 4;
	var intvl = 50;
	var wait = 4000;


	function showLayer(lyrID){
		//SHOW A HIDDEN LAYER

		//CANCEL ANY PENDING TIMER FUNCTIONS
		clearLayerTimeout();

		//ENSURE LAYER IS ALREADY HIDDEN
		if(hiddenLayer){
			hiddenLayer.style.visibility = 'hidden';
		}
		//SHOW THE LAYER
		hiddenLayer = new getObj(lyrID);
		hiddenLayer.style.visibility = 'visible';
	}

	function hideLayer(){
		//HIDES A LAYER
		if(hiddenLayer){
			hiddenLayer.style.visibility = 'hidden';
		}
	}

	function waitAndHideLayer(){
		//HIDE LAYER AFTER TIMER INTERVAL
		objTimer = window.setTimeout(hideLayer, timeoutValue);
	}

	function clearLayerTimeout(){
		//CLEAR LAYER TIMEOUT
		if(objTimer){
			window.clearTimeout(objTimer);
			objTimer = null;
		}
	}
	var timeoutValue = 200;
	var objTimer = 0;
	var hiddenLayer = 0;

	//HIDE LAYER IF CLICKING OUTSIDE THE DIV
	//document.onclick = hideLayer; 

	function checkAll(state, field)
	{
	for (i = 0; i < field.length; i++)
		field[i].checked = state;
	}


	function assignRandomPwd(strTargetField, strMode, strLength){
		//this function will assign a random password or pin to the field passed by parameter. This field must be unique in the whole page and must have the ID property assigned. Name won't be enough.
		//strMode = 1 Generate Alphanumeric Password
		//strMode = 2 Generate numeric Pin
		//strLength is the lenght of the random password/pin

		var chars1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
		var chars2 = "0123456789";

		if (strMode == 1) finalchars = chars1
		if (strMode == 2) finalchars = chars2

		var string_length = strLength;
		var randomstring = '';
		for (var i=0; i<string_length; i++) {
			var rnum = Math.floor(Math.random() * finalchars.length);
			randomstring += finalchars.substring(rnum,rnum+1);
		}

		document.getElementById(strTargetField).value = randomstring
	}


	function checkSpace(oEle){
		var carCode = event.keyCode;
		if (carCode == 32){
			event.cancelBubble = true;
			event.returnValue = false;
		}
	}


	function isValidDate(dateStr){
		// VALIDATES DATE OF BIRTH: MM/DD/YY MM/DD/YYYY MM-DD-YY MM-DD-YYYY 
		var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/; 
		var matchArray = dateStr.match(datePat);
		if (matchArray == null) { 
			alert("Date is not in a valid format e.g. 01/01/2001") 
			document.LoginForm.DOB.focus();
			return false; 
		} 
		month = matchArray[1];
		day = matchArray[3];
		year = matchArray[4]; 
		if (month < 1 || month > 31) {
			alert("Days must be between 1 and 31."); 
			document.LoginForm.DOB.focus();
			return false; 
		} 
		if (day < 1 || day > 12) {
			alert("Months must be between 1 and 12."); 
			document.LoginForm.DOB.focus();
			return false; 
		} 
		if ((month==4 || month==6 || month==9 || month==11) && day==31) {
			alert("Month "+month+" doesn't have 31 days!") 
			document.LoginForm.DOB.focus();
			return false 
		} 
		if (month == 2) {
			var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); 
			if (day>29 || (day==29 && !isleap)) { 
				alert("February " + year + " doesn't have " + day + " days!"); 
				document.LoginForm.DOB.focus();
				return false; 
			} 
		} 
		return true;
	}

	function toggleDivDisplay(strDivID, sMode){
		//THIS FUNCTION CAN BE USED ANYWHERE TO TOGGLE THE DISPLAY OF A DIV
		oDiv = document.getElementById(strDivID);
		
		if (sMode == ""){
			//TOGGLE MODE
			if (oDiv.style.display == 'none')
				oDiv.style.display = '';
			else
				oDiv.style.display = 'none';
		}
		else{
			//DIRECT MODE
			if (sMode.toLowerCase() == "show")
				oDiv.style.display = ''
			else if(sMode.toLowerCase() == "hide")
				oDiv.style.display = 'none'
		}
	}
	
	function disableButton(oButton){
		oButton.disabled = true;
	}

	function ValidatePINRegistration(){
		//VALIDATES THE PIN REGISTRATION PAGE	
		var returnValue = true;
		var pageMessage = "";
		//MEMBER NUMBER & CHECK DIGIT
		if(document.onlineform.Account){
			var a = new getObj("lblAccount");
			if(document.onlineform.Account.value == ""){
				a.obj.innerText = "* REQUIRED";
				pageMessage += "<li>Member Number is required</li>";
				returnValue=false;
			}
			else if(document.onlineform.AccountCHECK){
				if(document.onlineform.AccountCHECK.value == ""){
					a.obj.innerText = "* CHECK DIGIT IS REQUIRED";
					pageMessage += "<li>Check digit is required</li>";
					returnValue=false;
				}
			}			
			else {
				a.obj.innerText = "*";	
			}
		}
		//NAME
		if(document.onlineform.Name){
			var n = new getObj("lblName");
			if(document.onlineform.Name.value == ""){
				n.obj.innerText = "* REQUIRED";
				pageMessage += "<li>Name is required</li>";
				returnValue=false;
			}
			else {
				n.obj.innerText = "*";	
			}
		}
		//ADDRESS
		if(document.onlineform.Address){
			var ad = new getObj("lblAddress");
			if(document.onlineform.Address.value == ""){
				ad.obj.innerText = "* REQUIRED";
				pageMessage += "<li>Address is required</li>";
				returnValue=false;
			}
			else {
				ad.obj.innerText = "*";	
			}
		}
		//DATE OF BIRTH
		if(document.onlineform.DOBday && document.onlineform.DOBmonth && document.onlineform.DOByear){
			var dob = new getObj("lblDOB");
			if(document.onlineform.DOBday.value == "" || document.onlineform.DOBday.value == "dd" || document.onlineform.DOByear.value == "" || document.onlineform.DOByear.value == "yyyy"){
				dob.obj.innerText = "* REQUIRED";
				pageMessage += "<li>Date of birth is required</li>";
				returnValue=false;
			}
			else if(isNaN(document.onlineform.DOBday.value) || isNaN(document.onlineform.DOBmonth.options[document.onlineform.DOBmonth.selectedIndex].value) || isNaN(document.onlineform.DOByear.value)){
				dob.obj.innerText = "* INVALID DATE";
				pageMessage += "<li>Date of birth is invalid</li>";
				returnValue=false;
			}
			else if(parseInt(Number(document.onlineform.DOBday.value)) > 31 || parseInt(Number(document.onlineform.DOBday.value)) < 1){
				dob.obj.innerText = "* INVALID DAY";
				pageMessage += "<li>Day of birth is invalid</li>";
				returnValue=false;
			}
			else if(parseInt(Number(document.onlineform.DOByear.value)) > 2020 || parseInt(Number(document.onlineform.DOByear.value)) < 1900){
				dob.obj.innerText = "* INVALID YEAR";
				pageMessage += "<li>Year of birth is invalid</li>";
				returnValue=false;
			}			
			else {
				dob.obj.innerText = "*";
			}
			//CONFIRM DATE OF BIRTH
			if(document.onlineform.DOBdayV && document.onlineform.DOBmonthV && document.onlineform.DOByearV){
				var dobv = new getObj("lblDOBV");
				if(document.onlineform.DOBdayV.value != document.onlineform.DOBday.value || document.onlineform.DOBmonthV.options[document.onlineform.DOBmonthV.selectedIndex].value != document.onlineform.DOBmonth.options[document.onlineform.DOBmonth.selectedIndex].value || document.onlineform.DOByearV.value != document.onlineform.DOByear.value){
					dobv.obj.innerText = "* DATES DO NOT MATCH";
					pageMessage += "<li>Confirmation Date of birth does not match</li>";
					returnValue=false;
				}
				else {
					dobv.obj.innerText = "*";
				}
			}
		}
		//TELEPHONE NUMBERS
		if(document.onlineform.HomeTel && document.onlineform.WorkTel && document.onlineform.MobileNo){
			var tel = new getObj("lblTelephone");
			if(document.onlineform.HomeTel.value == "" && document.onlineform.WorkTel.value == "" && document.onlineform.MobileNo.value == ""){
				tel.obj.innerHTML = "<br>* AT LEAST ONE PHONE NUMBER IS REQUIRED";
				pageMessage += "<li>At least one phone number is required</li>";				
				returnValue=false;
			}
			else {
				tel.obj.innerHTML = "";
			}
		}
		//PPS / NATIONAL INSURANCE NO
		if(document.onlineform.PPSNNIN && document.onlineform.IDNumberType && document.onlineform.PPSIsRequired.value == 'True'){
			var pps = new getObj("lblPPS");
			if(document.onlineform.PPSNNIN.value == ""){
				pps.obj.innerText = "* REQUIRED";
				pageMessage += "<li>" + document.onlineform.IDNumberType.value + " is required</li>";
				returnValue=false;
			}
			else {
				pps.obj.innerText = "*";
			}
		}
		//EMAIL ADDRESS / SUBSCRIBE OPTION
		if(document.onlineform.Email){
			var em = new getObj("lblEmail");
			if(document.onlineform.Email.value == ""){
				em.obj.innerText = "* REQUIRED";
				pageMessage += "<li>Email Address is required</li>";
				returnValue=false;
			}
			else {
				em.obj.innerText = "*";
			}
			//CONFIRM EMAIL ADDRESS - ALLOW CASE DIFFERENCES
			if(document.onlineform.EmailV){
				var emv = new getObj("lblEmailV");
				if(document.onlineform.EmailV.value.toLowerCase() != document.onlineform.Email.value.toLowerCase()){
					emv.obj.innerText = "* DOES NOT MATCH";
					pageMessage += "<li>Confirmation Email Address does not match</li>";
					returnValue=false;
				}
				else {
					emv.obj.innerText = "*";
				}				
			}
		}
		//ACCOUNT QUESTIONS
		if(document.onlineform.security1 && document.onlineform.security2 && document.onlineform.security3){
			var sec = new getObj("lblSecurityQuestions");
			if(document.onlineform.security1.value == "" && document.onlineform.security2.value == "" && document.onlineform.security3.value == ""){
				sec.obj.innerHTML = "<br>* AT LEAST ONE QUESTION MUST BE ANSWERED";
				pageMessage += "<li>At least one security question must be answered</li>";
				returnValue=false;
			}
			else {
				sec.obj.innerHTML = "";
			}
		}
		//SECURITY PASSWORD
		if(document.onlineform.Answer){
			var pwd = new getObj("lblPassword");
			if(document.onlineform.Answer.value == ""){
				pwd.obj.innerText = "* REQUIRED";
				pageMessage += "<li>Security Password is required</li>";				
				returnValue=false;
			}
			else {
				pwd.obj.innerText = "*";
			}
			if(document.onlineform.AnswerConf){
				var pwdv = new getObj("lblPasswordV");
				if(document.onlineform.AnswerConf.value != document.onlineform.Answer.value){
					pwdv.obj.innerText = "* DOES NOT MATCH";
					pageMessage += "<li>Confirmation of Security Password does not match</li>";				
					returnValue=false;
				}
				else {
					pwdv.obj.innerText = "*";
				}
			}			
		}
		//PIN
		if(document.onlineform.PIN1){
			var pin = new getObj("lblPIN");
			if(document.onlineform.PIN1.value == ""){
				pin.obj.innerText = "* REQUIRED";
				pageMessage += "<li>PIN is required</li>";				
				returnValue=false;
			}
			else if(isNaN(document.onlineform.PIN1.value)) {
				pin.obj.innerText = "* MUST BE A NUMBER";
				pageMessage += "<li>PIN must be numeric</li>";				
				returnValue=false;
			}	
			else {
				pin.obj.innerText = "*";
			}
			if(document.onlineform.PIN2){
				var pinv = new getObj("lblPINV");
				if(document.onlineform.PIN2.value != document.onlineform.PIN1.value){
					pinv.obj.innerText = "* DOES NOT MATCH";
					pageMessage += "<li>Confirmation PIN does not match</li>";				
					returnValue=false;
				}
				else {
					pinv.obj.innerText = "*";
				}
			}
		}
		//ESTATEMENTS QUESTION
		if(document.onlineform.rdeStatements){
			var eState = new getObj("lbleStatements");
			if((document.onlineform.rdeStatements[0].checked == false) && (document.onlineform.rdeStatements[1].checked == false)){
				eState.obj.innerText = "* REQUIRED";
				pageMessage += "<li>Electronic Statements Answer is required</li>";
				returnValue=false;
			}
			else {
				eState.obj.innerText = "*";	
				if (document.onlineform.rdeStatements[1].checked == true){
					document.onlineform.eStatementsOptInValue.value = "True";
				}
				else {
					document.onlineform.eStatementsOptInValue.value = "False";
				}
			}
		}
		//EAGMS QUESTION
		if(document.onlineform.rdeAGMs){
			var eAGM = new getObj("lbleAGMs");
			if((document.onlineform.rdeAGMs[0].checked == false) && (document.onlineform.rdeAGMs[1].checked == false)){
				eAGM.obj.innerText = "* REQUIRED";
				pageMessage += "<li>Electronic AGM Reports Answer is required</li>";
				returnValue=false;
			}
			else {
				eAGM.obj.innerText = "*";	
				if (document.onlineform.rdeAGMs[1].checked == true){
					document.onlineform.eAGMsOptInValue.value = "True";
				}
				else {
					document.onlineform.eAGMsOptInValue.value = "False";
				}
			}
		}
		if(returnValue == false){
			var pm = new getObj("lblPageMessage");
			pm.obj.innerHTML = "Please note the following errors:<ul>" + pageMessage + "</ul>";
		}
		return	returnValue;
	}
	
	//FUNCTION TO LOOK FOR SOMETHING IN ANY ARRAY. IF NOT FOUND, UNDEFINED WILL BE RETURNED
	function lookInArray(arr, obj){
		for(var i=0; i<arr.length; i++){
			if (arr[i] == obj) return true;
		}
	}

