// Common Scripts Used On All Pages //
// Functions,
//		fctStatusBar				- sets the browser statusbar text
//		fctTextboxFocus				- select the contents of the from field textbox
//		fctDemoMode					- sets the default paramters for the user to log in using the demo mode
//		fctCheckLogin				- validate the login details before submitting the form   - *** change to use form.js functions
//		fctTestMobile				- validate typed UK mobile telephone number   - **** duplicated code with form.js function
//		fctShowHelp					- show pop-up help message					  - **** may no longer be used
//		fctHideHelp					- hide pop-up help message					  - **** may no longer be used
//		fctCreateHelp				- dynamically create a pop-up help message	  - **** may no longer be used
//		fctShowTip					- show tooltip message
//		fctHideTip					- hide tooltip message
//		fctCreateTip				- dynamically create a tooltip message
//		fctPositionTip				- set the positioning for the tooltip message
//		fctShowSending				- display the sending text animation		  - **** move to smstext.js
//		fctHideSending				- hide the sending text animation		      - **** move to smstext.js
//		fctScrolledX				- retrieve the horizontal scrolled position for the screen
//		fctScrolledY				- retrieve the vertical scrolled position for the screen
//
// Requires
//		None.
//
// Version V1.0.0, 03/01/2008 WBM.
// Version V1.0.1, 23/03/2008 WBM. 
// Version V1.0.2, 02/06/2008 WBM. 
// Version V1.0.3, 04/09/2008 WBM. 
// Copyright 2007. Property of Design Atom
// Produced by Design Atom - www.designatom.com

// Test for browser type
var strUserAgent = navigator.userAgent.toLowerCase();
var bolIExplorer = (document.getElementById && document.all); 
var bolNetscape = (document.getElementById && !document.all); 
var bolSafari = (strUserAgent.indexOf('applewebkit') != -1 ? 1 : 0);

// Month Number to Names
arrMonths = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

// Day of Month Postfix
arrDayPostfix = new Array("", "st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th", "th", "st");

// Status Bar Message
function fctStatusBar(strMessage){
	window.status = strMessage;
	return true;
}

// Textbox Focus
function fctTextboxFocus(ctlBox) {
	ctlBox.select();
}

// Demo Mode
function fctDemoMode() {
	// Shortcut to Login Form
	thisForm = document.frmLogin;
	
	// Set username and password
	thisForm.user.value = "demo";
	thisForm.pword.value = "demo";
	
	// Submit the form
	thisForm.submit();
}

// Validate the Login Details entered by the user
function fctCheckLogin(thisForm) {
	// Define valid characters. All others will be removed
	var strValid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKMNOPQRSTUVWXYZ0123456789 ";
	var strSubmit = true;
	
	// Step through each of the Form Fields in turn
	for (var counter=0; counter < thisForm.length; counter++) {
		// Set the Original and Processed (cleaned) strings
		var strOriginal = thisForm[counter].value;
		var strProcessed = "";
		
		// Loop through each character in the submitted number
		for (var intCount = 0; intCount < strOriginal.length; intCount++) {
			// Check that the current character is valid
			strCurrent = strOriginal.charAt(intCount);
			if (strValid.indexOf(strCurrent)!= -1) {
				// This character is valid so keep it
				strProcessed += strCurrent;
			}
		}
	}
	
   // Check that a Username has been entered
   if(!thisForm.user.value || thisForm.user.value=="username") {
      alert("You have not entered a Username.\nPlease Enter Your Username.");
      thisForm.user.focus();
      return;
   }
   
   // Check that a Password has been entered
   if(!thisForm.pword.value) {
      alert("You have not entered a Password.\nPlease Enter Your Password.");
      thisForm.pword.focus();
      return;
   }
   
   // Check if this form needs submitting
   if(strSubmit==true) thisForm.submit();
}

// Validate the Mobile Number entered by the user
function fctTestMobile(thisForm) {
	//alert(thisForm.mobile.value);
	// Retrieve the entered mobile number
	strMobile = thisForm.mobile.value;
	
	// Define valid characters. All others will be removed
	var strValid = "0123456789";
	
	// Placeholder for the cleaned string
	var strProcessed = "";
	
	// Loop through each character in the submitted number
	for (var intCount = 0; intCount < strMobile.length; intCount++) {
		// Check that the current character is valid
		strCurrent = strMobile.charAt(intCount);
		if (strValid.indexOf(strCurrent)!= -1) {
			// This character is valid so keep it
			strProcessed += strCurrent;
		}
	}
	
	// Check the mobile has the correct number of digits and starts correctly
	if ((strProcessed.length == 11 && strProcessed.substr(0,2) == "07") || (strProcessed.length == 12 && strProcessed.substr(0,3) == "447") || (strProcessed.length == 14 && strProcessed.substr(0,5) == "00447")) {
		// Submit the cleaned mobile number
		thisForm.mobile.value = strProcessed;
		thisForm.submit();
	}
	else {
		// Invalid mobile phone number
		// Alert the user and cancel the submission
		thisForm.mobile.select();
		alert("Invalid Mobile Number.\nPlease Re-Enter Your Number");
	}
}


// POP-UP ASSISTANT
// Display the help assistant for the control with focus
function fctShowHelp(strTipid, objThis, offsetX, offsetY) {
	// Hide any existing help assistants before showing the new one
	fctHideHelp();
	
	// Retrieve the help text
	//alert(offsetY);
	var strText = '<table cellpadding="0" cellspacing="0"><tr><td></td><td><p class="center"><img class="normal" src="_images/assistant_top.gif" width="220" height="10"></p></td><td></td></tr>';
	strText += '<tr><td valign="top"><img class="normal" src="_images/transparent.gif" width="22" height="' + (0-offsetY-10) + '"><br><img id="assistleft" class="normal" style="visibility: hidden" src="_images/help_arrow_left.gif" width="22" height="26"></td><td background="../_images/assistant_middle.gif"><p class="helptext">' + document.getElementById(strTipid).innerHTML + '</p></td><td valign="top"><img class="normal" src="_images/transparent.gif" width="22" height="' + (0-offsetY-10) + '"><br><img id="assistright" class="normal" style="visibility: hidden" src="_images/help_arrow_right.gif" width="22" height="26"></td></tr>';
	strText += '<tr><td></td><td><p class="center"><img class="normal" src="_images/assistant_bottom.gif" width="220" height="10"></p></td><td></td></tr><table>';
	
	// Check if there is an existing help assistant and if not create one
	if(!document.getElementById('assistant')) fctCreateHelp('assistant');
	
	// Create a new assistant container
	var objThisHelptip = document.getElementById('assistant');
	
	
	// Create the content for the assistant and display it
	objThisHelptip.innerHTML = strText;
	objThisHelptip.style.display = 'block';
	
	// Work out whether to display the left or right arrow. Use the X offset to determine which side the assistant is
	if (offsetX > 0) {
		// The assistant is sited to the right of the object so point back with the left arrow
		document.getElementById("assistleft").style.visibility = "visible";
	}
	else {
		// The assistant is sited to the left of the object so point back with the right arrow
		document.getElementById("assistright").style.visibility = "visible";
	}
	
	// Declare and initialise object offsets
	var intPosX = 0;
	var intPosY = 0;
	
	// Check that this browser supports object offsets
	if (objThis.offsetParent) {
		// Loop whie the current object has a parent
		do {
			// Add the offset of the current object to the running totals
			intPosX += objThis.offsetLeft;
			intPosY += objThis.offsetTop;
		} while (objThis = objThis.offsetParent);
	}
	
	// Set the position of the help assistant
	objThisHelptip.style.left = (intPosX + offsetX) + 'px';
	objThisHelptip.style.top = (intPosY + offsetY) + 'px';
}

// Hide all tooltips
function fctHideHelp() {
	// Hide all elements with the help assistant class
	if (document.getElementById('assistant')) document.getElementById('assistant').style.display = 'none';
}

// Create a new help assistant (for display) using the assistant class
function fctCreateHelp(strNewAssistant) { 
	// Check if this browser provides support for creating new elements
	if(document.createElement) { 
		// Create a new element (div) to contain this tooltip
		var objNewElement = document.createElement('div'); 
	
		// Set the id for the new (tooltip) container
		objNewElement.id = strNewAssistant;     
		
		// Set style attributes for the tooltip container
		with(objNewElement.style) { 
			display = 'none';
			position = 'absolute';
		}
		
		// Set default content and add the container to the document (body) 
		objNewElement.innerHTML = '&nbsp;'; 
		
		document.body.appendChild(objNewElement); 
	} 
} 

// POP-UP TOOLTIP
// Display the tooltip corresponding to the element id that was passed
function fctShowTip(strTipid) {
	// Retrieve the tooltip text
	var tip = document.getElementById(strTipid).innerHTML;
	
	// Check if there is an existing tooltip and if not create one
	if(!document.getElementById('tooltip')) fctCreateTip('tooltip');
	
	// Retrieve the new tooltip container, set content and display it
	var objThisTooltip = document.getElementById('tooltip');
	objThisTooltip.innerHTML = tip;
	objThisTooltip.style.display = 'block';
	
	// Set the position of the tootip
	// Set the mousemove event to move the tooltip with the cursor
	document.onmousemove = fctPositionTip;
}

// Hide all tooltips
function fctHideTip() {
	// Hide all elements with the tootip class
	document.getElementById('tooltip').style.display = 'none';
}

// Create a new element (for display) using the tooltip class
function fctCreateTip(strNewTooltip) { 
	// Check if this browser provides support for creating new elements
	if(document.createElement) { 
		// Create a new element (div) to contain this tooltip
		var objNewElement = document.createElement('div'); 
	
		// Set the id for the new (tooltip) container
		objNewElement.id = strNewTooltip;     
			
		// Set style attributes for the tooltip container
		with(objNewElement.style) { 
			display = 'none';
			position = 'absolute';
		}
		
		// Set default content and add the container to the document (body) 
		objNewElement.innerHTML = '&nbsp;'; 
		document.body.appendChild(objNewElement); 
	} 
} 

// Calculate and set the position of the tooltip on the screen
function fctPositionTip(e) {
	// Tooltip position relative to the mouse 
	var intOffsetX = 15;
	var intOffsetY = 22;

	if(document.getElementById){
		var iebody=(document.compatMode && document.compatMode != 'BackCompat') ? document.documentElement : document.body;
		
		// Retrieve the page offset for this document in relation to the screen
		intPageX = (bolSafari == 1 ? 0 : (bolIExplorer) ? iebody.scrollLeft : window.pageXOffset);
		//intPageX = fctScrolledX();
		intPageY = (bolSafari == 1 ? 0 : (bolIExplorer) ? iebody.scrollTop : window.pageYOffset);
		//intPageY = fctScrolledY();
		
		// Retrieve the current mouse position in relation to the top left corner of this document (page)
		intPosX = ((bolIExplorer) ? event.screenX : (bolNetscape) ? clientX = e.clientX : false);
		intPosY = ((bolIExplorer) ? event.screenY-110 : (bolNetscape) ? clientY = e.clientY : false);
		
		// Set the position of the tooltip relative to the current position of the mouse
		// This function is called on mousemove so the tooltip will track (move with) the mouse
		var objThisTooltip = document.getElementById('tooltip');
		objThisTooltip.style.left = (intPageX + intPosX + intOffsetX) + 'px';
		objThisTooltip.style.top = (intPageY + intPosY + intOffsetY) + 'px';
	}
}

// Show the sending message animated image
function fctShowSending() {
	// Show the image
	document.images.sendingit.style.visibility = "visible";
}

// Hide the sending message animated image
function fctHideSending(thisDoc) {
	// Hide the image
	thisDoc.images.sendingit.style.visibility = "hidden";
	thisDoc.images.explained.style.visibility = "hidden";
	thisDoc.images.closeit.style.visibility = "hidden";
	
	// Reset the animated image
	thisDoc.images.sendingit.src = "_images/sendingit.gif";
}

// Retrieve the offset for how far this page has scrolled horizontally
function fctScrolledX() {
	if (window.pageXOffset != null) {
		// Page X Offset
		return window.pageXOffset;
	}
	else if (document.body.scrollLeft != null) {
		// Document Body Object
		return document.body.scrollLeft;
	}
	else {
		// Default
		return null;
	}
}

// Retrieve the offset for how far this page has scrolled vertically
function fctScrolledY() {
	if (document.body.scrollTop != null) {
		// Document Body Object
		return document.body.scrollTop;
	}
	else if (window.pageYOffset != null) {
		// Page Y Offset
		return window.pageYOffset;
	}
	else {
		// Default
		return null;
	}
}

// Remove whitespace from the leading and trailing edges of the string
function fctTrim(strThis){
    return strThis.replace(/^\s*|\s*$/g,'');
}

// Remove whitespace from the leading edge of the string
function fctLTrim(strThis){
    return strThis.replace(/^\s*/g,'');
}

// Remove whitespace from the trailing edge of the string
function fctRTrim(strThis){
    return strThis.replace(/\s*$/g,'');
}