
function notifyUser(message)
{
	/*
	txt = message;
	txt += "If this error persists, please contact the administrator with the above information.\n\n";
	txt += "enquiry@econfirm.com.au\n\n";
	txt += "Click OK to continue.\n\n";
	alert(txt);
	*/
	setHeaderInfoText(message);
}


/** 
 * These two functions remove the DIV from the page and the browser repaints the page as if they were not there.
 * This is different than hidding the content where the browser leaves the space where the content was still there.
 * 
 * This follows the W3C convention: http://www.w3schools.com/HTMLDOM/prop_style_display.asp
 * Note that older browsers may have trouble using these functions. 
 */
function addDiv(div_name)
{
	document.getElementById(div_name).style.display="inline";
}

function removeDiv(div_name)
{
	document.getElementById(div_name).style.display="none";
}

/**
 * Some code here to refresh a div.
 */ 
/* 
function refreshPageWarning()
{
	var x = document.getElementById("basebEditingPage");
	if ( x.value == "false" )
	{
		setHeaderInfoText("Warning - Page refresh in 30 seconds");
	}
}

function refreshPage()
{
	var x = document.getElementById("basebEditingPage");
	if ( x.value == "false" )
	{
		location.reload(true);
	}
	else
	{
		window.setTimeout("refreshPageWarning()", 270000); 	// 4 minutes and 30 seconds
		window.setTimeout("refreshPage()", 300000); 		// 5 minutes
	}
}
window.setTimeout("refreshPageWarning()", 270000); 	// 4 minutes and 30 seconds
window.setTimeout("refreshPage()", 300000); 		// 5 minutes
*/



/**
 * Be aware that this function will get data synchronously from the server and will wait for the server 
 * to return before continuing execution of code.
 *
 * Basic usage:
 *  - Define an ajaxid in function_library.php
 *
 *  - Write a handler in ajax.php
 *
 *  - Set ""theurl" to something similar to the following:
 *    theurl = "<?php echo getBaseWebSite(); ?>/ajax.php?"
 *	    + "&ajaxid=" + <?php echo AJAXID_GET_LOCAL_FORMATTED_DATE ?> 
 *	    + "&moreparameters=blahblahblah";
 *
 *  - Call with 
 *    var strLocalFormattedDate = ajaxRequestData(theurl);
 *    and then handle the return value with appropriate error checks.
 *
 * Note: Makes no sense to call asyncronously because the function would return instantly without the data.
 *       for async calls where you don't need the data (like setting local time label) look at the function below: ajaxSetTimeAsync()
 */
function ajaxRequestData(theurl)
{
	//alert(theurl);
	var data;
	
	try
	{
	    var request = new AW.HTTP.Request;
	    request.setURL(theurl);
	    request.setAsync(false);
		request.response = function() 
		{
			data = handleAjaxResponse(this);
		}
		request.request();
	}
	catch(err)
	{
		//setHeaderInfoText("Failed to contact server!");
		//notifyUser("Failed to contact server!");
	}
	
	return data;
}

/**
 * This function returns the actual response object.
 */
function ajaxRequestXMLResponse(theurl)
{
	var response;
	
	try
	{
	    var request = new AW.HTTP.Request;
	    request.setURL(theurl);
	    request.setAsync(false);
		request.response = function() 
		{
			response = this;
		}
		request.request();
	}
	catch(err)
	{
		//notifyUser("Failed to contact server!");
	}
	
	return response;
}


/**
 * Sets an activewidgets text field asyncronusly so as not to cause the the resources to wait for response from server.
 */
function ajaxSetTimeAsync(theurl, awLabel)
{
	try
	{
	    var request = new AW.HTTP.Request;
	    request.setURL(theurl);
	    request.setAsync(true);
		request.response = function() 
		{
			data = handleAjaxResponse(this);
			awLabel.setControlText(data);
		}
		request.request();
	}
	catch(err)
	{
		//notifyUser("Failed to contact server!");
	}
}

/**
 * Sets a variable passed in asyncronusly so as not to cause the the resources to wait for response from server.
 */
function ajaxSetVariableAsync(theurl, awLabel)
{
	try
	{
	    var request = new AW.HTTP.Request;
	    request.setURL(theurl);
	    request.setAsync(true);
		request.response = function() 
		{
			data = handleAjaxResponse(this);
			awLabel.setControlText(data);
		}
		request.request();
	}
	catch(err)
	{
		//notifyUser("Failed to contact server!");
	}
}

/**
 * Only use for a ajax call that you don't want any information back from and runs asyncronusly
 */
function ajaxAsync(theurl)
{
	try
	{
	    var request = new AW.HTTP.Request;
	    request.setURL(theurl);
	    request.setAsync(true);
		request.response = function() 
		{
			data = handleAjaxResponse(this);
		}
		request.request();
	}
	catch(err)
	{
		//notifyUser("Failed to contact server!");
		//alert(err);
	}
}

function handleAjaxResponse(response)
{
	// This call will return null if the XML returned is invalid.
	var dataXML = response.getResponseXML(); // get received xml document returns DOMDocument
	xmlReturnedOK = false;
	returnData = "";
	try
	{
	    // Then this call will throw an exception by using the null reference.
		var returnDataNodes = dataXML.getElementsByTagName("returndata");
		xmlReturnedOK = true;
	}
	catch(err)
	{
		//notifyUser("Cannot contact the eConfirm server!");
	}
	
	if ( xmlReturnedOK )
	{
		var retStatus = "";
	
	    for (var i = 0; i < returnDataNodes.length; i++)
		{ 
			returnData = returnDataNodes[i].getAttribute("data");
		}
	}
	
	return unescape(returnData);
}

function getXMLElementFromAJAXResponse(response, element)
{
	returnData = "";	
	
	try
	{
		// get received xml document returns DOMDocument
		var myDocument = response.getResponseXML(); 
		
	    // Then this call will throw an exception by using the null reference.
		var returnDataNodes = myDocument.getElementsByTagName(element);
		
		// F'n different browsers to return a bloody string.
		var hasInnerText = (returnDataNodes[0].innerText == undefined) ? false : true;
		var hasTextContent = (returnDataNodes[0].textContent == undefined) ? false : true;
		var hasText = (returnDataNodes[0].text == undefined) ? false : true;

		if ( returnDataNodes.length > 0 )
		{
			if( hasText )
			{
				returnData = returnDataNodes[0].text;
			}
			else if( hasInnerText )
			{
			    returnData = returnDataNodes[0].innerText;
			} 
			else if( hasTextContent )
			{
				returnData = returnDataNodes[0].textContent;
			}
			else
			{
				alert("Please notify the administrator that your browser is not supported.");
			}
		}
	}
	catch(err)
	{
		//notifyUser("Cannot contact the eConfirm server!");
	}
	
	return unescape(returnData);
}

/**
 * DO NOT USE ANY MORE - USE ajaxRequest() instead with ajax.php and set an AJAXID in function library.
 *
 * Not possible to do Asyn call because the function returns immediately!!
 */
function getAJAXResponse(theurl, async)
{
	bResponse = false;

	try
	{
	    //alert(theurl);	
	    //alert(async);	

		xmlReturnedOK = false;

		// set the flag to confirm the appointment
	    var request = new AW.HTTP.Request;

	    request.setURL(theurl);
	    request.setAsync(async);
		
		request.response = function()
		{
			
			// This call will return null if the XML returned is invalid.
	        var data = this.getResponseXML(); // show received xml document returns DOMDocument

			try
			{
		        // Then this call will throw an exception by using the null reference.
				var returnDataNodes = data.getElementsByTagName("returndata");
				xmlReturnedOK = true;
			}
			catch(err)
			{
				//notifyUser("Cannot contact the eConfirm server!");
			}

			if ( xmlReturnedOK )
			{
				var retStatus = "";
				//alert("returnDataNodes.length: " + returnDataNodes.length);
		        for (var i = 0; i < returnDataNodes.length; i++)
				{ 
					retStatus = returnDataNodes[i].getAttribute("status");
					//alert("retStatus: " + retStatus);
				}
				
		        if ( retStatus == "success" )
		        {
					//alert("success");
					bResponse = true;
				}
				else
				{
					//notifyUser("getAJAXResponse: Returned failed!");
					bResponse = false;
				}
			}
		}
		request.request();
	}
	catch(err)
	{
		notifyUser("Unable to retrieve data from the server!");
	}
	
	return bResponse;
}


/**
 * DO NOT USE ANY MORE - USE ajaxRequest() instead with ajax.php and set an AJAXID in function library.
 *
 * Not possible to do Asyn call because the function returns immediately!!
 */
function getAJAXData(theurl, async)
{
	returnData = "failed";

	try
	{
	    //alert(theurl);	
	    //alert(async);	
	    
		xmlReturnedOK = false;

		// set the flag to confirm the appointment
	    var request = new AW.HTTP.Request;

	    request.setURL(theurl);
	    request.setAsync(async);
		//alert(theurl);
		
		request.response = function()
		{
			// This call will return null if the XML returned is invalid.
	        var data = this.getResponseXML(); // get received xml document returns DOMDocument
			
			try
			{
		        // Then this call will throw an exception by using the null reference.
				var returnDataNodes = data.getElementsByTagName("returndata");
				xmlReturnedOK = true;
			}
			catch(err)
			{
				//notifyUser("Cannot contact the eConfirm server!");
			}
			
			if ( xmlReturnedOK )
			{
				
				
				var retStatus = "";
	
		        for (var i = 0; i < returnDataNodes.length; i++)
				{ 
					retStatus = returnDataNodes[i].getAttribute("status");
					returnData = returnDataNodes[i].getAttribute("data");
					//alert("retStatus: " + retStatus);
					//alert(returnData);
				}
				
		        if ( retStatus == "success" )
		        {
					//alert("success");
				}
		        else if ( retStatus == "failed" )
		        {
					//alert("success");
					notifyUser("Failed to get data from server!");
				}
				else
				{
					notifyUser("Unable to get status from XML!");
				}
			}
		}
		request.request();
	}
	catch(err)
	{
		//notifyUser("Failed to contact server!");
	}
	
	return returnData;
}

function notifyUser(message)
{
	/*
	txt = message;
	txt += "If this error persists, please contact the administrator with the above information.\n\n";
	txt += "enquiry@econfirm.com.au\n\n";
	txt += "Click OK to continue.\n\n";
	alert(txt);
	*/
	setHeaderInfoText(message);
}

function newWindow(mypage, myname, w, h, scroll) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}


function validate_email(field,alerttxt)
{
	var dotpos;
	var apos = field.value.indexOf("@");
	dotpos = field.value.lastIndexOf(".");
	if (apos < 1 || dotpos - apos < 2) 
	{
		alert(alerttxt);
		return false;
	}
	else 
	{
		return true;
	}
}


function rand ( n )
{
  return ( Math.floor ( Math.random ( ) * n + 1 ) );
}


function validatelogin(e)
{
	var objmyusername = document.getElementById("myusername");
	var objmypassword = document.getElementById("mypassword");
	
  	if (objmyusername.value.length <= 0)
	{
		alert("Please enter a username to login.");
	  	objmyusername.focus();
		e.returnValue = false; 
		return false;
			
	}

  	if (objmypassword.value.length <= 0)
	{
		alert("Please enter a password to login.");
	  	objmypassword.focus();
		e.returnValue = false; 
		return false;
			
	}
	
	e.returnValue = true; 
	return true;
}


function validatereg(e)
{
	
	var objMobile = document.getElementById("mobile");
	var objEmail = document.getElementById("email1");
	var objSecurity = document.getElementById("security_code");
	var objAgreeTerms = document.getElementById("agree_terms");
	
	if ( validate_email(objEmail,"Please enter a valid email address.") == false)
	{
	  	objEmail.focus();
		e.returnValue = false; 
		return false;
	}


	if (!(((objMobile.value.substr(0,2) == "04") && (objMobile.value.length == 10)) ||
		((objMobile.value.substr(0,2) == "61") && (objMobile.value.length == 11)) ||
		((objMobile.value.substr(0,3) == "+61") && (objMobile.value.length == 12)) ))
	{
		alert("Sorry, an Australian mobile number is needed for registration.");
		objMobile.focus();
		e.returnValue = false; 
		return false;	
	}

  	if (objSecurity.value.length != 5)
	{
		alert("The security code has not been entered correctly. It needs to be copied from the image below it.");
	  	objSecurity.focus();
		e.returnValue = false; 
		return false;
			
	}
	
	if (objAgreeTerms.checked == false)
	{
		alert("Sorry, the terms and conditions need to be read and accepted to use this service. Please read and accept to register for eConfirm.");
	  	objAgreeTerms.focus();
		e.returnValue = false; 
		return false;
		
	}
	
	//alert("About to return true")
	
	e.returnValue = true; 
	return true;
  		
}

function validateActivation(e)
{
	var objAgreeTerms = document.getElementById("agree_terms");
	
	if (objAgreeTerms.checked == false)
	{
		alert("Sorry, the terms and conditions need to be read and accepted to use this service. Please read and accept to register for eConfirm.");
	  	objAgreeTerms.focus();
		e.returnValue = false; 
		return false;
		
	}
	
	//alert("About to return true")
	
	e.returnValue = true; 
	return true;
  		
}


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

//Function runs when this page has been loaded and does the following:
//1. Determine the browser name and version  (since the script will only work on Netscape 3+ and Internet Explorer 4+).
//2. Start the timer object that will periodically change the image displayed by the Banner Ad.
//3. Preload the images used by the Banner Ad rotator script
function InitialiseBannerAdRotator() {

//Determine the browser name and version
//The script will only work on Netscape 3+ and Internet Explorer 4+
var BrowserType = navigator.appName;
var BrowserVersion = parseInt(navigator.appVersion);

if (BrowserType == "Netscape" && (BrowserVersion >= 3)) {
IsValidBrowser = true;
}

if (BrowserType == "Microsoft Internet Explorer" && (BrowserVersion >= 4)) {
IsValidBrowser = true;
}

if (IsValidBrowser) {
TimerObject = setTimeout("ChangeImage()", DisplayInterval);
BannerAdCode = 0;

for (i = 0; i < NumberOfImages; i++) {
BannerAdImages[i] = new Image();
BannerAdImages[i].src = ' ' + ImageFolder + ImageFileNames[i];
}

}

}

//Function to change the src of the Banner Ad image
function ChangeImage() {

if (IsValidBrowser) {
BannerAdCode = BannerAdCode + 1;

if (BannerAdCode == NumberOfImages) {
BannerAdCode = 0;
}

window.document.bannerad.src = BannerAdImages[BannerAdCode].src;
TimerObject = setTimeout("ChangeImage()", DisplayInterval);
}
}

//Function to redirect the browser window/frame to a new location,
//depending on which image is currently being displayed by the Banner Ad.
//If Banner Ad is being displayed on an old browser then the DefaultURL is displayed
function ChangePage() {

if (IsValidBrowser) {

if (TargetFrame != '' && (FramesObject)) {
FramesObject.location.href = ImageURLs[BannerAdCode];
} else {
document.location = ImageURLs[BannerAdCode];
}

} else if (!IsValidBrowser) {
document.location = DefaultURL;
}

}

function enableLayer( whichLayer )
{
	var elem, vis;
	if( document.getElementById ) // this is the way the standards work
	elem = document.getElementById( whichLayer );
	else if( document.all ) // this is the way old msie versions work
	  elem = document.all[whichLayer];
	else if( document.layers ) // this is the way nn4 works
	elem = document.layers[whichLayer];
	vis = elem.style;
	
	// if the style.display value is blank we try to figure it out here
	vis.display = 'block';
  
}

function disableLayer( whichLayer )
{
	var elem, vis;
	if( document.getElementById ) // this is the way the standards work
	elem = document.getElementById( whichLayer );
	else if( document.all ) // this is the way old msie versions work
	  elem = document.all[whichLayer];
	else if( document.layers ) // this is the way nn4 works
	elem = document.layers[whichLayer];
	vis = elem.style;
	
	// if the style.display value is blank we try to figure it out here
	vis.display = 'none';
  
}

function trim(s)
{
	var l=0; var r=s.length -1;
	while(l < s.length && s[l] == ' ')
	{	l++; }
	while(r > l && s[r] == ' ')
	{	r-=1;	}
	return s.substring(l, r+1);
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789.+ ";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}


// allows navigation of the parent from the iframe
function change_parent_url(url)
{
	document.location=url;
}
			
