function textCounter(field,cntfield,maxlimit) {
    if (field.value.length > maxlimit) // if too long...trim it!
    field.value = field.value.substring(0, maxlimit);
    // otherwise, update 'characters left' counter
    else
    cntfield.value = maxlimit - field.value.length;
}

function lucid_jumpMenu(targ,selObj,restore){
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function showimage(){
if (!document.images)
return
document.images.avatar.src= 'images/avatar/' + document.Register.user_avatar.options[document.Register.user_avatar.selectedIndex].value
}

function lucid_findObj(n, d) {
  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=lucid_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function lucid_validateForm() {
  var i,p,q,nm,test,num,min,max,errors='',args=lucid_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=lucid_findObj(args[i]);
    if (val) { if(args[i+1]!=''){nm=args[i+1] }else{nm=val.name; }
  // value of checkbox is on, need to test checked is true
  	if (test.charAt(0) == 'C' && val.checked != true){ 
		errors += '- '+nm+' is required.\n';
	}
	if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R' && test!='C') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.lucid_returnValue = (errors == '');
}

function lucid_jumpMenuGo(selName,targ,restore){ //v3.0
  var selObj = lucid_findObj(selName); if (selObj)lucid_jumpMenu(targ,selObj,restore);
}

/* start JS help tooltip */

function showHelpTip(e, sHtml, bHideSelects) {

	// find anchor element
	var el = e.target || e.srcElement;
	while (el.tagName != "A")
		el = el.parentNode;
	
	// is there already a tooltip? If so, remove it
	if (el._helpTip) {
		helpTipHandler.hideHelpTip(el);
	}

	helpTipHandler.hideSelects = Boolean(bHideSelects);

	// create element and insert last into the body
	helpTipHandler.createHelpTip(el, sHtml);
	
	// position tooltip
	helpTipHandler.positionToolTip(e);

	// add a listener to the blur event.
	// When blurred remove tooltip and restore anchor
	el.onblur = helpTipHandler.anchorBlur;
	el.onkeydown = helpTipHandler.anchorKeyDown;
}

var helpTipHandler = {
	hideSelects:	false,
	
	helpTip:		null,
	
	showSelects:	function (bVisible) {
		if (!this.hideSelects) return;
		// only IE actually do something in here
		var selects = [];
		if (document.all)
			selects = document.all.tags("SELECT");
		var l = selects.length;
		for	(var i = 0; i < l; i++)
			selects[i].runtimeStyle.visibility = bVisible ? "" : "hidden";	
	},
	
	create:	function () {
		var d = document.createElement("DIV");
		d.className = "help-tooltip";
		d.onmousedown = this.helpTipMouseDown;
		d.onmouseup = this.helpTipMouseUp;
		document.body.appendChild(d);		
		this.helpTip = d;
	},
	
	createHelpTip:	function (el, sHtml) {
		if (this.helpTip == null) {
			this.create();
		}

		var d = this.helpTip;
		d.innerHTML = sHtml;
		d._boundAnchor = el;
		el._helpTip = d;
		return d;
	},
	
	// Allow clicks on A elements inside tooltip
	helpTipMouseDown:	function (e) {
		var d = this;
		var el = d._boundAnchor;
		if (!e) e = event;
		var t = e.target || e.srcElement;
		while (t.tagName != "A" && t != d)
			t = t.parentNode;
		if (t == d) return;
		
		el._onblur = el.onblur;
		el.onblur = null;
	},
	
	helpTipMouseUp:	function () {
		var d = this;
		var el = d._boundAnchor;
		el.onblur = el._onblur;
		el._onblur = null;
		el.focus();
	},	
	
	anchorBlur:	function (e) {
		var el = this;
		helpTipHandler.hideHelpTip(el);
	},
	
	anchorKeyDown:	function (e) {
		if (!e) e = window.event
		if (e.keyCode == 27) {	// ESC
			helpTipHandler.hideHelpTip(this);
		}
	},
	
	removeHelpTip:	function (d) {
		d._boundAnchor = null;
		d.style.filter = "none";
		d.innerHTML = "";
		d.onmousedown = null;
		d.onmouseup = null;
		d.parentNode.removeChild(d);
		//d.style.display = "none";
	},
	
	hideHelpTip:	function (el) {
		var d = el._helpTip;
		/*	Mozilla (1.2+) starts a selection session when moved
			and this destroys the mouse events until reloaded
		d.style.top = -el.offsetHeight - 100 + "px";
		*/		
		
		d.style.visibility = "hidden";
		//d._boundAnchor = null;

		el.onblur = null;
		el._onblur = null;
		el._helpTip = null;
		el.onkeydown = null;
		
		this.showSelects(true);
	},
	
	positionToolTip:	function (e) {
		this.showSelects(false);		
		var scroll = this.getScroll();
		var d = this.helpTip;
		
		// width
		if (d.offsetWidth >= scroll.width)
			d.style.width = scroll.width - 10 + "px";
		else
			d.style.width = "";
		
		// left
		if (e.clientX > scroll.width - d.offsetWidth)
			d.style.left = scroll.width - d.offsetWidth + scroll.left + "px";
		else
			d.style.left = e.clientX - 2 + scroll.left + "px";
		
		// top
		if (e.clientY + d.offsetHeight + 18 < scroll.height)
			d.style.top = e.clientY + 18 + scroll.top + "px";
		else if (e.clientY - d.offsetHeight > 0)
			d.style.top = e.clientY + scroll.top - d.offsetHeight + "px";
		else
			d.style.top = scroll.top + 5 + "px";
			
		d.style.visibility = "visible";
	},
	
	// returns the scroll left and top for the browser viewport.
	getScroll:	function () {
		if (document.all && typeof document.body.scrollTop != "undefined") {	// IE model
			var ieBox = document.compatMode != "CSS1Compat";
			var cont = ieBox ? document.body : document.documentElement;
			return {
				left:	cont.scrollLeft,
				top:	cont.scrollTop,
				width:	cont.clientWidth,
				height:	cont.clientHeight
			};
		}
		else {
			return {
				left:	window.pageXOffset,
				top:	window.pageYOffset,
				width:	window.innerWidth,
				height:	window.innerHeight
			};
		}
		
	}

}
/* end JS help tool tip */


function lucid_swapImgRestore() { //v3.0
  var i,x,a=document.lucid_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function lucid_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.lucid_p) d.lucid_p=new Array();
    var i,j=d.lucid_p.length,a=lucid_preloadImages.arguments; 
    for(i=0; i<a.length; i++)
      if (a[i].indexOf("#")!=0){ 
        d.lucid_p[j]=new Image; 
        d.lucid_p[j++].src=a[i];
      }
    }
}

function lucid_findObj(n, d) { //v3.0
  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=lucid_findObj(n,d.layers[i].document); 
  return x;
}

function lucid_swapImage() { //v3.0
  var i,j=0,x,a=lucid_swapImage.arguments; 
  document.lucid_sr=new Array; 
  for(i=0;i<(a.length-2);i+=3)
    if ((x=lucid_findObj(a[i]))!=null){
      document.lucid_sr[j++]=x; 
      if(!x.oSrc) x.oSrc=x.src; 
      x.src=a[i+2];
    }
}

/*
// Set page as home. (later IE's only)
Function lucid_makeHome(url){
	if (document.all){
  		//document.write('<A HREF="javascript:history.go(0);" onClick="this.style.behavior=\'url(#default#homepage)\';this.setHomePage(\'http://www.southlandnz.com/sections/soilshome/\');">');
		// document.write('Click Here to Make My Web Page Your Homepage</a>');
		this.setHomePage(url);
	}else if (document.getElementById){
		mywindow = window.open('', '', 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no,directories=no,location=no,width=550,height=80');
		mywindow.opener = self;
		mywindow.document.open();
		mywindow.document.write('<head><title>Make this your home page</title></head><body style="background-color: Menu"><a href="' + url + '">Drag this link onto your Home button to make this your Home Page.</a></body>');
		mywindow.document.close();
  		//document.write('<a href="http://www.southlandnz.com/sections/soilshome/">Drag this link onto your Home button to make this your Home Page.</a>');
	}else{
		mywindow = window.open('', '', 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no,directories=no,location=no,width=550,height=80');
		mywindow.opener = self;
		mywindow.document.open();
		mywindow.document.write('<head><title>Make this your home page</title></head><body style="background-color: Menu"><b>Make this site your home page:</b><br>- Go to <b>Preferences</b> in the <B>Edit</B> Menu.<br>- Choose <b>Navigator</b> from the list on the left.<br>- Click on the <b>"Use Current Page"</b> button.</body>');
		mywindow.document.close();
	  	//document.write('<b>Make this site your home page:</b><br>- Go to <b>Preferences</b> in the <B>Edit</B> Menu.<br>- Choose <b>Navigator</b> from the list on the left.<br>- Click on the <b>"Use Current Page"</b> button.');
	}/*else {
	  	document.write('<b>Make this site your home page:</b><br>- Go to <b>Preferences</b> in the <B>Edit</B> Menu.<br>- Choose <b>Navigator</b> from the list on the left.<br>- Click on the <b>"Use Current Page"</b> button.');
	}*
} // end set as homepage*/

function lucid_windowOpen(hyperlink, width, height){

// hyperlink = the url to open
// winstyle = how much browser chrome the user should see, either full,reduced or none
// height = height of the window
// width = width of the window
if(width == 0){width = 400;}
if(height == 0){height = 300;}
popup = window.open(hyperlink,'lucidPopup','height='+height+',width='+width+',scrollbars=yes,resizable=yes');
popup.focus();
return false;
}



  //functions and variables for hiding and showing layers
  //added 8/6/04 by Joshua Vial
  //Copyright Sinuous Design Limited

  //globals
	var currentElement = ''

  //sets id style to display:block
  //if currentElement exists it will be hidden
  //sets currentElement = id
  function setCurrentLayer(id) 
  {
		thisElement = document.getElementById(id);
		if (currentElement != '')
		{
			currentElement.style.display = 'none';
		}
		thisElement.style.display = 'block';
		currentElement = thisElement;
  }

  //if currentElement exists it will be hidden
  //sets currentElement to ''
  function hideCurrentLayer()
  {
		if (currentElement != '')
		{
			currentElement.style.display = 'none';
		}
		currentElement = '';
  }

  //sets id style to display:none
  function hideLayer(id)
  {
		thisElement = document.getElementById(id);
		thisElement.style.display = 'none';
  }

  //sets id style to display:block
  function showLayer(id)
  {
		thisElement = document.getElementById(id);
		thisElement.style.display = 'block';
  }

  //if id is hidden it is displayed, else it is hidden
  function toggleLayer(id)
  {
		thisElement = document.getElementById(id);
		if (thisElement.style.display == 'none')
		{
			thisElement.style.display = 'block';
		}
		else
		{
			thisElement.style.display = 'none';
		}
  }
  
  
/**
* changes to provide a wraper function to toggle an image along with a layer
* added 29/6/04 by Joshua Vial
*/

var imgs = new Array();

function toggleImageNLayer(layerID)
{
	toggleLayer(layerID);
	imgId = layerID + "_image";
	img = document.getElementById(imgId);
	if (img == null) throw "toggleImageNLayer exception: no element with id " + imgId;
	for (i = 0;i < imgs.length;i++)
	{
		if (imgs[i].id == imgId) break;
	}
	if (i >= imgs.length) throw "toggleImageNLayer exception: img " + imgId + " is not initialised";
	if (imgs[i].toggled)
	{
		img.src = imgs[i].src;
		imgs[i].toggled = false;
	}
	else
	{
		img.src = imgs[i].srcOver;
		imgs[i].toggled = true;
	}
}

function addImgSrc(imgId,src,srcOver)
{
	imgs[imgs.length] = new ImgSrc(imgId,src,srcOver);
}

function ImgSrc(imgId,src,srcOver)
{
	this.id = imgId;
	this.src = src;
	this.srcOver = srcOver;
	this.toggled = false;
}
  
  
// toggle.js
// (c) 2002 Jerrett Taylor (www.nullcreations.net / www.liquidpulse.net)
/////////////////////////////////////////////////////////////////////////////////

OriginalHeight = new Array(); 
function SlideOut(object,dest,direc) { 
    // get height and remove the 'px' 
   h = document.getElementById(object).style.height; 
    strip=/[px]/gi; h = h.replace(strip,''); h = parseInt(h); 
    // set sizes and see if we are finished 
    if (direc == 1) { h+=20; keepgoing = (dest >= h) ? true : false; } else { h-=20; keepgoing = (dest < h) ? true : false;    } 
    if (keepgoing) { 
         // not done yet, timeout so we can keep going 
         document.getElementById(object).style.height = h + 'px'; 
         setTimeout("SlideOut('" + object + "'," + dest + "," + direc + ");",60); 
    } else { 
        // either hide it or return to original size 
        if (direc == 0) { document.getElementById(object).style.display = 'none'; } else { document.getElementById(object).style.height = OriginalHeight[object]+2+'px'; } 
        } 
} 

function ToggleBox(object) { 
    // set original height if it's not already set 
    if (!OriginalHeight[object]) { 
         if (document.getElementById(object).clientHeight == 0) { 
            // if our element is hidden we need to prepare it.. show it, grab the height, shrink it to 0, and hide it again! 
             document.getElementById(object).style.display = 'block';     
             OriginalHeight[object] = document.getElementById(object).clientHeight;     
             document.getElementById(object).style.height = 0; document.getElementById(object).style.display = 'none'; 
         } 
         OriginalHeight[object] = document.getElementById(object).clientHeight;     
        } 
    // expand or contract 
    if (document.getElementById(object).style.display == 'block') { 
        document.getElementById(object).style.height = OriginalHeight[object]; 
        lucid_swapImage(object+'-toggle','','images/admin/fold-up.gif',1);
		SlideOut(object,5,0); 
    } else { 
        document.getElementById(object).style.display = 'block'; 
		lucid_swapImage(object+'-toggle','','images/admin/fold-down.gif',1);
        SlideOut(object,OriginalHeight[object],1);     
    } 
}

function emailLink(start,domain)
{
    email = start + "@" + domain;
    document.write("<a href='mailto:" + email +"'>" + email + "</a>");
}


// end toggle.js
