//////////////////////////////////////////////////////////
// shared javascript functions 
//////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////
// javascript error handling 
//////////////////////////////////////////////////////////////////

function betterhandler(description,pagename,lineno)
{
 alert(
  'A Javascript Error has occurred! \n'
  +'\n Description: \t'+description
  +'\n Page Name:      \t'+pagename
  +'\n At Line :       \t'+lineno
  +'\n\n click OK to continue'
 )
 return true
}
window.onerror=betterhandler;


/////////////////////////////////////////////////////////////////////////////////
// browser detection  
/////////////////////////////////////////////////////////////////////////////////
var isIE = (window.navigator.userAgent.indexOf("MSIE") > 0);
var isFirefox = (window.navigator.userAgent.indexOf("Firefox") > 0);

///////////////////////////////////////////////////////////
// pops up window to add to favourites - only works in ie
///////////////////////////////////////////////////////////

function addFavs(){
  
  title=document.title.substr(0,100);
  
  if(isIE)window.external.AddFavorite(location.href, title);
  if(isFirefox)window.sidebar.addPanel(title, location.href, "" );

}

///////////////////////////////////////////////
// functions
///////////////////////////////////////////////

///////////////////////////////////////////////
// javascript link
///////////////////////////////////////////////

function jsLink(link)
{
  window.location=link; 
}

///////////////////////////////////////////////////////////
// validates the passed txt parm 
///////////////////////////////////////////////////////////

function checkText(text,minlen, maxlen){

  /* checks text for min chars of 5 chars and all valid alpha's or or numeric or - space or . etc. */

  if(text.length<minlen){    
    r="is less than " + minlen + " characters";
    return r;
  }

  if(text.length>maxlen){
    r="is greater than " + maxlen + " characters";
    return r;
  }

  var z=0;
  var b;
  a = new Array();

  //for (i = 0; i < text.length; i++){
  //  if(text.charAt(i) == 0x0d) alert(" 0d found");
  //  if(text.charAt(i) == 0x0a) alert(" 0a found");
  //}
  //var text2="empty";  
  //for (i=0;i<text.length;i++){
  //  text2 += text.charCodeAt(i).toString(16); 
  //}
  //alert(text2); 

  for (i = 0; i < text.length; i++)
  {

    //if(i==2) alert("third char is "+ text.charCodeAt(i).toString(16));
    //if(i==3) alert("fourth char is "+ text.charCodeAt(i).toString(16));

    //
    // check for carriage return 0x0d (hex od) followed by  
    // linefeed ox0a (hex oa)
    // if found i is bumped up by to check the next char two chars up 
    // 
 
    if(text.charCodeAt(i).toString(16) == "d" && text.length>=(i+1))
		if(text.charCodeAt(i+1).toString(16) == "a") i=i+2;


    // i doesn't point past the max text length 
    
	// this line changed from if(!(i>=(text.length-1))){ 14/09/2006 cause last char not validated
	//														seems to work ok like this 
	
	if(!(i>=(text.length))){ 

      if( !( ((text.charAt(i) >= "a") && (text.charAt(i) <= "z")) ||
        ((text.charAt(i) >= "A") && (text.charAt(i) <= "Z")) ||
        ((text.charAt(i) >= "0") && (text.charAt(i) <= "9")) ||
        text.charAt(i) == " " || text.charAt(i) == "." || text.charAt(i) == "-" ||
        text.charAt(i) == "_" || text.charAt(i) == "!" ||text.charAt(i) == "?" ||
        text.charAt(i) == ":" || text.charAt(i) == ";" || text.charAt(i) == "'" ||
        text.charAt(i) == "&" || text.charAt(i) == "*" || text.charAt(i) == "(" ||
        text.charAt(i) == ")" || text.charAt(i) == "+" || 
        text.charAt(i) == "£" || text.charAt(i) == "$" || text.charAt(i) == "€" || text.charAt(i) == ","
        ) ){

           r="has a non-alphabetic or non-numeric or invalid character ";
           return r;
      }
    }  
  }  

  b = a.join("");
  r="1" + b;
  return r;
}


////////////////////////////////////////////////////////
// checks if the passed parm is alpha/numeric 
////////////////////////////////////////////////////////

function alphaNumCheck(text){
  
  for (i = 0; i < text.length; i++)
  {

      if( !( ((text.charAt(i) >= "a") && (text.charAt(i) <= "z")) ||
        ((text.charAt(i) >= "A") && (text.charAt(i) <= "Z")) ||
        ((text.charAt(i) >= "0") && (text.charAt(i) <= "9"))
		))
		{
           r="text has a non-alphabetic or non-numeric character ";
           return r;
        }
  }  
  
  return "1";

}  

////////////////////////////////////////////////////////
// checks if the passed parm is numeric 
////////////////////////////////////////////////////////

function numeric(text){
  
  for (i = 0; i < text.length; i++)
  {

      if( !( (text.charAt(i) >= "0") && (text.charAt(i) <= "9") ))
		{
           r="text has a non-numeric character ";
           return r;
        }
  }  
  
  return "1";

}  

////////////////////////////////////////////////////////
// checks if the passed parm is alpha 
////////////////////////////////////////////////////////

function alpha(text){
  
  // convert string to upper case 
  text=text.toUpperCase();
  
  for (i = 0; i < text.length; i++)
  {

      if( !( (text.charAt(i) >= "A") && (text.charAt(i) <= "Z") ))
		{
           r="text has a non-alphabetic character ";
           return r;
        }
  }  
  
  return "1";

}  

///////////////////////////////////////////////////////////
// validates the passed URL parm 
///////////////////////////////////////////////////////////

function checkTextURL(text,minlen, maxlen){

  /* checks text for min chars of 5 chars and all valid alpha's or or numeric or - space or . etc. */

  if(text.length<minlen){    
    r="is less than " + minlen + " characters";
    return r;
  }

  if(text.length>maxlen){
    r="is greater than " + maxlen + " characters";
    return r;
  }

  var z=0;
  var b;
  a = new Array();

  //for (i = 0; i < text.length; i++){
  //  if(text.charAt(i) == 0x0d) alert(" 0d found");
  //  if(text.charAt(i) == 0x0a) alert(" 0a found");
  //}
  //var text2="empty";  
  //for (i=0;i<text.length;i++){
  //  text2 += text.charCodeAt(i).toString(16); 
  //}
  //alert(text2); 

  for (i = 0; i < text.length; i++)
  {

    //if(i==2) alert("third char is "+ text.charCodeAt(i).toString(16));
    //if(i==3) alert("fourth char is "+ text.charCodeAt(i).toString(16));

    //
    // check for carriage return 0x0d (hex od) followed by  
    // linefeed ox0a (hex oa)
    // if found i is bumped up by to check the next char two chars up 
    // 
 
    if(text.charCodeAt(i).toString(16) == "d" && text.length>=(i+1))
		if(text.charCodeAt(i+1).toString(16) == "a") i=i+2;


    // i doesn't point past the max text length 
    
	// this line changed from if(!(i>=(text.length-1))){ 14/09/2006 cause last char not validated
	//														seems to work ok like this 
	
	if(!(i>=(text.length))){ 

      if( !( ((text.charAt(i) >= "a") && (text.charAt(i) <= "z")) ||
        ((text.charAt(i) >= "A") && (text.charAt(i) <= "Z")) ||
        ((text.charAt(i) >= "0") && (text.charAt(i) <= "9")) ||
        text.charAt(i) == " " || text.charAt(i) == "." || text.charAt(i) == "-" ||
        text.charAt(i) == "_" || text.charAt(i) == "!" ||text.charAt(i) == "?" ||
        text.charAt(i) == ":" || text.charAt(i) == ";" || text.charAt(i) == "'" ||
        text.charAt(i) == "&" || text.charAt(i) == "*" || text.charAt(i) == "(" ||
        text.charAt(i) == ")" || text.charAt(i) == "+" || text.charAt(i) == "/" ||
        text.charAt(i) == "?" || text.charAt(i) == "&" || text.charAt(i) == "=" ||        
        text.charAt(i) == "£" || text.charAt(i) == "$" || text.charAt(i) == "€" || text.charAt(i) == ","
        ) ){

           r="has a non-alphabetic or non-numeric or invalid character ";
           return r;
      }
    }  
  }  

  b = a.join("");
  r="1" + b;
  return r;
}




//////////////////////////////////////////////////////////////////////////////////////////////
// functions to toggle menus in and out of the viewable screen 
//////////////////////////////////////////////////////////////////////////////////////////////

function menuToggle(menu,target1,x){  //  function for drop down menu

  // alert("menu is " + menu + " target1 is " + target1 + " x is " + x );

  // this works pre xml with the strict definition   
  //
  // if IE gets object by getElementById if netscape gets object by eval
  // targetMenu = (document.getElementById) ? document.getElementById(target).style : eval ("document." +target);
  // menuObject = (document.getElementById) ? document.getElementById(menu).style   : eval ("document" + menu);
  // menuObject.top = 22;
  //objectMenu    = (document.getElementById) ? document.getElementById(menu).style : eval ("document." + menu);
  //objectTarget1 = (document.getElementById) ? document.getElementById(target1) : eval ("document." + target1);
  //
  // note also addding the "px" is not required pre xml strict 
  
  // the following works with xml strict for IE and Firefox 
  
  objectMenu=document.getElementById(menu).style;
  objectTarget1=document.getElementById(target1);

  posX = findPosX(objectTarget1);
  posY = findPosY(objectTarget1);
  
  posX=posX+parseFloat(x);
  document.getElementById(menu).style.left = posX + "px";
 
  posY+=-5;
  objectMenu.top = (parseInt(objectMenu.top) == posY) ? "-2000px" : posY + "px";
}


//  function for drop down menu

function findPosX(obj)   //  function for drop down menu
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)  //  function for drop down menu
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

//////////////////////////////////////////////////////////////////////////////////////////////
// functions to display menu in viewable screen 
//////////////////////////////////////////////////////////////////////////////////////////////

function menuDisplay(menu,target1,x){  
 
  objectMenu=document.getElementById(menu).style;
  objectTarget1=document.getElementById(target1);

  posX = findPosX(objectTarget1);
  posY = findPosY(objectTarget1);
  
  posX=posX+parseFloat(x);
  document.getElementById(menu).style.left = posX + "px";
 
  posY+=-5;
  objectMenu.top = posY + "px";
}

//////////////////////////////////////////////////////////////////////////////////////////////
// functions to hide menu from viewable screen 
//////////////////////////////////////////////////////////////////////////////////////////////

function menuHide(menu,target1,x){  
 
  objectMenu=document.getElementById(menu).style;
  objectTarget1=document.getElementById(target1);

  posX = findPosX(objectTarget1);
  posY = findPosY(objectTarget1);
  
  posX=posX+parseFloat(x);
  document.getElementById(menu).style.left = posX + "px";
 
  posY+=-5;
  objectMenu.top = "-2000px";
}


////////////////////////////////////////////////
// ajax functions
////////////////////////////////////////////////

function getAjaxObj()
{

  //creat a boolean variable to check for a valid Internet Explorer Instance 
  
  var xmlhttp = false

  //////////////// check if ie and verion of javascript 
  try{
  
    // if the javascript version is greater than 5 (which has been around for 9 years)
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    //alert ("you are using IE with javascript greater than 5 ");
   
  } 
  catch (e){
  
  try{
  
   
    // if the javascript version is greater than 5 (which has been around for 9 years)
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    //alert ("you are using IE with javascript 5 or less "); 
  
    } 
    catch (e) {
  
      //else using a non-IE browser
      xmlhttl = false;
      //alert("you are a non IE javascript ");
  
    }
   
  }

  // if we are using non-IE create a javascipt instance of the object 
  if(!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    xmlhttp = new XMLHttpRequest();
    // Some versions of some Mozilla browsers won't work properly if the response from the server
    // doesn't have an XML mime-type header. To satisfy this, you can use an extra method call to
    // override the header sent by the server, just in case it's not text/xml. 
  
    if (xmlhttp.overrideMimeType)
            xmlhttp.overrideMimeType('text/html');

                // See note below about this line

    // alert("you are a non IE javascript ");    
  }
  
  return xmlhttp;  
    
} // end of function getAjaxObj    

function makeRequest(serverPage, objID, xmlhttp)
{
  
  // note the xmlhttp object has to be allocated outside this function 
  // with the getAjaxObj or it seems to re-use the previous one 
  
  // alert("serverPage is " + serverPage + " objID is " + objID);  
  // alert("making request");
  
  var obj = document.getElementById(objID);
  
  xmlhttp.open("GET", serverPage, true);
  
  // alert("made request");
  
  xmlhttp.onreadystatechange = function() {
    
    if (xmlhttp.readyState != 4)
            return;
 
    // Firefox may throw exception while getting XmlHttpRequest status
    try { real_status = xmlhttp.status; }
    catch (e) { return; }
    
    // status != 200 means that page was not downloaded
    // error 404 happened or something like this
    if (real_status != 200)
    {
	  // alert("second page not returned");
      return;
    }
      
    // alert("got page successfully");
      
	obj.innerHTML = xmlhttp.responseText;	  
	
	// alert("finished makeRequest");
 
  }
  
  xmlhttp.send(null);  

}

/////////////////////////////////////////////////////////////////////////////////////////
//
// this function not used as synchronous but it works - return result of web services call
// to javascript - not used cause the follow-on would use pop-up windows 
//
// note synchronous function to return output to a javascript function
// only call for quick piece of data as this will lock the whole 
// browser till the call is complete to the server 
/////////////////////////////////////////////////////////////////////////////////////////
function makeRequestToJS(serverPage, xmlhttp)
{
  
  // note the xmlhttp object has to be allocated outside this function 
  // with the getAjaxObj or it seems to re-use the previous one 
  
  // alert("serverPage is " + serverPage + "objID is " + objID);  
  // alert("making request");
  
  var retText="";
  
  // third parm is false which means it's a synchronous request which means it locks the web b
  // browser until the request is complete - should be ok as it's a short request 
  // need to do this to get the response back to javascript to process 
  xmlhttp.open("GET", serverPage, false);
  
  // alert("made request");
  
  xmlhttp.onreadystatechange = function() {
    
    if (xmlhttp.readyState != 4)
            return;
 
    // Firefox may throw exception while getting XmlHttpRequest status
    try { real_status = xmlhttp.status; }
    catch (e) { return; }
    
    // status != 200 means that page was not downloaded
    // error 404 happened or something like this
    if (real_status != 200)
    {
	  // alert("second page not returned");
      return;
    }
      
    // alert("got page successfully");
      
	retText = xmlhttp.responseText;	  
	
	// alert("retText is " + retText);
 
  }
  
  xmlhttp.send(null);  
  
  retText = xmlhttp.responseText;	 
  
  return retText;

}


//////////////////////////////////////////////////////////////////////////
// check email address 
//////////////////////////////////////////////////////////////////////////

  function checkEmailAddress(emailAddress){

/* validate email address */

    if (emailAddress.length<5){
      r="email address is less than five characters";
      return r;
    }
    
    if (emailAddress.length>50){
      r="email address is longer than 50 characters";
      return r;
    }

    atloc=(emailAddress.indexOf("@"));
    dotloc=(emailAddress.indexOf("."));
    len=emailAddress.length;

    if (!(atloc>1 && dotloc>4 && len>dotloc+2)){
        r="email address is not valid";
        return r;
    }

	for (i = 0; i < emailAddress.length; i++) {
        if ( emailAddress.charAt(i) == "<" || emailAddress.charAt(i) == ">" ||
             emailAddress.charAt(i) == "=" || emailAddress.charAt(i) == "/" ){
            return "email address contains an invalid character";
        
		}

    }

    return "1";
  
  }
 

