/*************************************************************/
 
//  Accompanies WG Conveyancing site
//  Last Amended: 02-Feb-2005

/*************************************************************/


// Text Changer device
// (Size values are used as percentage font size for BODY element))

LARGE  = 120
MEDIUM = 100
SMALL  = 76          // the default (same as BODY font-size in style sheet)
HILITE = '#BDD8FF'   // currently-selected text sizer background colour
BG     = '#669ACC'   // regular background colour
DAYS   = 365         // days to retain cookie which remembers user choice of text size


CURRENT_MNU = '#FFCC00'    // Current page menu colour

/*************************************************************/

window.onload = setupPage
window.onresize = setMenuHeight

function setupPage()
{
   // sets up positions and/or sizes of various page elements

   setMenuColour();
   setMenuHeight();
   if (document.getElementById('frmTransType'))
	   document.getElementById('frmTransType').onchange = setTransTypeMenu;

   if (document.getElementById('hipDescription'))
   	   document.getElementById('hipDescription').style.display='none';
}

function setMenuColour()
{
	// changes menu background colour for current page

	// Menus have IDs in format "mnuN" where N starts from 0

    var numMenus = 7
	var strIDNum = 0   

    if (location.href.indexOf("index.php") > -1)               strIDNum = 0
    if ( 
		 (location.href.indexOf("logon.php?type=other") > -1) 
		   ||
		 (location.href.indexOf("ref") > -1) 
		)                                                      strIDNum = 1
    if (location.href.indexOf("quote") > -1)                   strIDNum = 2
    if (location.href.indexOf("forms.php") > -1)               strIDNum = 3
    if (location.href.indexOf("instruct.php") > -1)            strIDNum = 4
    if (location.href.indexOf("logon.php?type=client") > -1)   strIDNum = 5
    if (location.href.indexOf("contact.php") > -1)             strIDNum = 6

	for (i = 0; i < numMenus; i++ )
	{
	   if (i == strIDNum)
          document.getElementById("mnu" + strIDNum).style.backgroundColor = CURRENT_MNU   
    }
}

function setMenuHeight()
{
  // ensure menu panel stretches to page height.
   oMenu = document.getElementById("menupanel")
   menuTop = getY(oMenu)
   oMenu.style.height = (getWindowHeight() - menuTop - 5) + "px"

  // match up WG logo to menu bottom (home page only)
   oLogo = document.getElementById("WGSlogo")
   if (oLogo != null)
   {
       logoTop = getY(oLogo)
	   oLogo.style.top = (getWindowHeight() - 155 -5) + "px"
   }
}

function checkTextSize()
{
   // retrieves any cookie-stored text size preference
   var cookie = readCookie("TextSize");
   if (cookie != null) 
	   setTextSize(cookie) 
}
                           
function setTextSize(strSize) 
{
   // sets font size in percent for BODY and highlights user choice

   // get matching numeric font size value for strSize (defined as constant)
   intFontSize = eval(strSize.toUpperCase())
   if (intFontSize.NaN) intFontSize = SMALL 

   document.body.style.fontSize = intFontSize + "%"
   highlight(strSize)
   createCookie("TextSize", strSize, DAYS);

   // Firefox type browsers workaround to get the text 
   // to re-render properly after a font resize
   if (checkBrowser() == "Netscape Navigator 5") 
	  doWindowResizeHack()
}


function highlight(strID)
{
 // toggles highlighting by applying hilite colour to current text size link
 // and reverting the other two elements to the BG colour
    
	if (strID == "large")
    {
       document.getElementById("small").style.backgroundColor = BG
       document.getElementById("medium").style.backgroundColor = BG
	}
	else if (strID == "medium")
    {
       document.getElementById("small").style.backgroundColor = BG
       document.getElementById("large").style.backgroundColor = BG
	}
	else // default
    {
	   strID = "small"
       document.getElementById("medium").style.backgroundColor = BG
       document.getElementById("large").style.backgroundColor = BG
	}
    document.getElementById(strID).style.backgroundColor = HILITE
}


function createCookie(name,value,days) 
{
   if (days) 
   {
      var date = new Date();
      date.setTime(date.getTime()+(days*24*60*60*1000));
      var expires = "; expires="+date.toGMTString();
   }
   else expires = "";
      document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) 
{
   var nameEQ = name + "=";
   var ca = document.cookie.split(';');
   for(var i=0;i < ca.length;i++) 
   {
      var c = ca[i];
      while (c.charAt(0)==' ') c = c.substring(1,c.length);
      if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
   }
   return null;
}


function validate(formname) 
{

	// Prompts for confirmation before deletion, where applicable
	if (typeof(formname.delconf) == 'object')
	{ 
		if (formname.delconf.value == "Yes")
		{
			if (confirm("Are you sure you want to delete this record?")) 
				return true;
			else
				return false;
		}
	}

    // this only used on Instruct Us form
	if (formname.name == 'instruct')
	{
		if ( (formname.Terms.checked == false) ||
		     (formname.RefName.value == "") ||
		     (formname.RefBranch.value == "") ||
		     (formname.RefNotes.value == "") ||
		     (formname.RefReference.value == "")
		   )
		   {
		       alert("Please fill in all the fields marked with an asterisk. Thank you.")
			   return false;
		   }
	}

    // this only used on the Logon form
	// and only if there is a "Terms Read" check box

	if ( (formname.name == 'logon') && 
		 (typeof(formname.logon_terms) == 'object')
		)
	{
		if (formname.logon_terms.checked == false)
		{
		   alert("Please tick the box to show you have read the Terms and Conditions. Thank you.")
		   return false;
		}
	}

    // if PostCode field exists, strip out any whitespace
	if (typeof(formname.FORM_rofPostCode) == 'object')
	{
		oldPC = formname.FORM_rofPostCode.value
		cleanPC = oldPC.replace(" ", "")
		formname.FORM_rofPostCode.value = cleanPC
    }

    // if password editing field exists, disallow a blank one
	if (typeof(formname.FORM_logPassword) == 'object')
	{
		if (formname.FORM_logPassword.value == '')
		{
		   alert("Please enter a password. This field cannot be blank. Thank you.")
		   return false;
		}
    }

    // permit submission by default
	return true; 
}
 
function doWindowResizeHack()
{
   // force a 1px window width resize followed by 
   // an immediate return to the original window width
   // to work around some text-resizing bugs, notably in FireFox 1.0
 
   var winWidth = window.innerWidth 
   window.innerWidth = winWidth - 1
   window.innerWidth = winWidth
}


// vars used by checkBrowser() & checkIt() from www.quirksmode.org
var detect = navigator.userAgent.toLowerCase();
var OS, browser, version, total, thestring;

function checkBrowser()
{
	
	if (checkIt('konqueror'))
	{
		browser = "Konqueror";
		OS = "Linux";
	}
	else if (checkIt('safari')) browser = "Safari"
	else if (checkIt('omniweb')) browser = "OmniWeb"
	else if (checkIt('opera')) browser = "Opera"
	else if (checkIt('webtv')) browser = "WebTV";
	else if (checkIt('icab')) browser = "iCab"
	else if (checkIt('msie')) browser = "Internet Explorer"
	else if (!checkIt('compatible'))
	{
		browser = "Netscape Navigator"
		version = detect.charAt(8);
	}
	else browser = "An unknown browser";

	if (!version) version = detect.charAt(place + thestring.length);

	if (!OS)
	{
		if (checkIt('linux')) OS = "Linux";
		else if (checkIt('x11')) OS = "Unix";
		else if (checkIt('mac')) OS = "Mac"
		else if (checkIt('win')) OS = "Windows"
		else OS = "an unknown operating system";
	}

	return browser + " " + version  // + " " OS
}

function checkIt(string)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

function show(strID, x, y)
{  
   // Shows a hidden description div just below and 
   // to the right the activating link

   // offsets from page origin
   origLeft = 250
   origTop = 60

   myObj = document.getElementById(strID)
   myObj.style.display = "block"
   myObj.style.left = (x - origLeft) + "px"
   myObj.style.top = (y - origTop) + "px"
}

function hide(strID)
{  
   myObj = document.getElementById(strID)
   myObj.style.display = "none";
}

function getY(obj)
{
	// from www.quirksmode.org
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function getX(obj)
{
	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 getWindowHeight()
{
    // return height of windows or frame

	var height

	if (self.innerHeight) // all except Explorer
		height = self.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight)
		height = document.documentElement.clientHeight; // Explorer 6 Strict Mode
	else if (document.body) // other Explorers
		height = document.body.clientHeight;

	return height;
}

function checkUserDate(strDate)
{
	// Returns a date string converted to MySQL yyyy-mm-dd format
	// or pops an error message
	var mySQLDate = agent.call('secure/formFunction.php','checkUserDate', '', strDate);
	if (mySQLDate.length == 10)
		return mySQLDate;
	else
	{
		alert(mySQLDate);
		return false;
	}
}

function setTransTypeMenu()
{
	// change Transaction menu

	var selected = this.value
     alert(selected)
	//agent.call('secure/formFunction.php','doTIFmenu','TIFpanel', this.value)
	
}



