
// copyright 1999 Idocs, Inc. http://www.idocs.com
// Distribute this script freely but keep this notice in place
function inputAlphaNumeric(e)
{
	var key;
	var keychar;

	if (window.event)
		key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		return true;
	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();

	// control keys
	if ((key==null) || (key==0) || (key==8) || 
		 (key==9) || (key==13) || (key==27) )
		return true;

	// alphas and numbers
	else if ((("abcdefghijklmnopqrstuvwxyz0123456789").indexOf(keychar) > -1))
		return true;
	else
		return false;
}

function inputNumeric(el,e)
{
	var key;
	var keychar;

	if (window.event)
		key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		return true;
	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();

	// control keys
	if ((key==null) || (key==0) || (key==8) || 
		 (key==9) || (key==13) || (key==27) )
		return true;

	else if (keychar=="-") {
		if( el.value.length==0 ) { return true; }
		return false;
	}
	else if (keychar==".")
	{
		if( el.value.indexOf(".") > -1 ) { return false; }
		else { return true; }
	}

	// alphas and numbers
	else if ((("0123456789").indexOf(keychar) > -1))
		return true;
	else
		return false;
}

function inputFeetInches(el,e)
{
	var key;
	var keychar;

	if (window.event)
		key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		return true;
	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();

	// control keys
	if ((key==null) || (key==0) || (key==8) || 
		 (key==9) || (key==13) || (key==27) )
		return true;

	else if (keychar=="-") {
		if( el.value.length==0 ) { return true; }
		return false;
	}
	else if (keychar==".")
	{
		if( el.value.indexOf(".") > -1 ) { return false; }
		else { return true; }
	}
	else if (keychar=="'")
	{
		if( el.value.indexOf("'") > -1 ) { return false; }
		else { return true; }
	}
	else if (keychar=='"')
	{
		if( el.value.indexOf('"') > -1 ) { return false; }
		else { return true; }
	}

	// alphas and numbers
	else if ((("0123456789").indexOf(keychar) > -1))
		return true;
	else
		return false;
}




// disable the submit button once it is clicked.





function singleClickSubmit(btn) {
	// modified by Shawn 29/3/06
	// this is a little piece of JS magic; sadly PHP would miss the disabled submit button's value even if you submitted the
	// form before you disabled the button - so what this now does is renames the button, dynamically creates a hidden form
	// field with the button's name and value, submits the form and disables the button safely!
	var btnName = btn.name;
	var btnVal  = btn.value;
	var input = document.createElement('INPUT');
	btn.name = "___disabled";
	input.type = "hidden";
	input.name = btnName;
	input.value = btnVal;
	btn.form.appendChild(input);
	btn.form.submit();
	btn.disabled = true;
	btn.value = "Please wait ...";
}

function stripToNumber(pstrSource) 
{ 
var m_strOut = new String(pstrSource); 
    m_strOut = m_strOut.replace(/[\.\,\$R]/g, ''); 

    return m_strOut; 
}


/* Made by Mathias Bynens <http://mathiasbynens.be/> */
/* for aus format: number_format('50000','2','.',',') gives 50,000.00 */
function number_format(a, b, c, d) {
 a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
 e = a + '';
 f = e.split('.');
 if (!f[0]) {
  f[0] = '0';
 }
 if (!f[1]) {
  f[1] = '';
 }
 if (f[1].length < b) {
  g = f[1];
  for (i=f[1].length + 1; i <= b; i++) {
   g += '0';
  }
  f[1] = g;
 }
 if(d != '' && f[0].length > 3) {
  h = f[0];
  f[0] = '';
  for(j = 3; j < h.length; j+=3) {
   i = h.slice(h.length - j, h.length - j + 3);
   f[0] = d + i +  f[0] + '';
  }
  j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
  f[0] = j + f[0];
 }
 c = (b <= 0) ? '' : c;
 return f[0] + c + f[1];
}


/*** Convert input type functions by Shawn ***/

function convertSelectToLabel( el )
{

	var isObject = false;
	try
	{
		eval( "var e = "+el+";" );
	}
	catch (err)
	{
		var e = el;
		isObject = true;
	}


	// capture important things like name, class, onchange functions etc
	var elName  = e.name;
	var elID    = e.id;
	var elClass = e.className;
	var elOnCh  = e.onchange;

	var newEl   = document.createElement( "INPUT" );
	newEl.type  = "hidden";
	if( e.type=="select-one" )
	{
		newEl.value = e.options[e.selectedIndex].value;
		var newElLab= document.createElement( "DIV" );
		e.parentNode.insertBefore( newElLab, e );
		newElLab.id        = el+"_label";
		newElLab.style.display   = "none";
		newElLab.innerHTML = newEl.value;
		newElLab.style.display = "inline";
	}
	else
	{
		newEl.value = e.value;
	}
	e.parentNode.insertBefore( newEl, e );
	e.parentNode.removeChild( e );
	newEl.name      = elName;
	newEl.id        = elID;
	newEl.className = elClass;
	newEl.onchange  = elOnCh;
	if( !isObject ) eval( el+" = newEl;" );
}

function convertLabelToSelect( el )
{

	var isObject = false;
	try
	{
		eval( "var e = "+el+";" );
	}
	catch (err)
	{
		var e = el;
		isObject = true;
	}


	var eLab = document.getElementById(el+"_label");

	var elName  = e.name;
	var elID    = e.id;
	var elClass = e.className;
	var elOnCh  = e.onchange;

	var newEl   = document.createElement( "SELECT" );
	newEl.options.length = 0;
	e.parentNode.insertBefore( newEl, e );
	e.parentNode.removeChild( e );
	eLab.parentNode.removeChild( eLab );

	newEl.name      = elName;
	newEl.id        = elID;
	newEl.className = elClass;
	newEl.onchange  = elOnCh;

	if( !isObject ) eval( el+" = newEl;" );
}

function convertSelectToInput( el )
{

	var isObject = false;
	try
	{
		eval( "var e = "+el+";" );
	}
	catch (err)
	{
		var e = el;
		isObject = true;
	}


	// capture important things like name, class, onchange functions etc
	var elName  = e.name;
	var elID    = e.id;
	var elClass = e.className;
	var elOnCh  = e.onchange;

	var newEl   = document.createElement( "INPUT" );
	newEl.type  = "text";
	if( e.type=="select-one" )
		newEl.value = e.options[e.selectedIndex].value;
	else
		newEl.value = e.value;

	e.parentNode.insertBefore( newEl, e );
	e.parentNode.removeChild( e );
	newEl.name      = elName;
	newEl.id        = elID;
	newEl.className = elClass;
	newEl.onchange  = elOnCh;
	if( !isObject ) eval( el+" = newEl;" );
}

function convertInputToSelect( el )
{

	var isObject = false;
	try
	{
		eval( "var e = "+el+";" );
	}
	catch (err)
	{
		var e = el;
		isObject = true;
	}


	var elName  = e.name;
	var elID    = e.id;
	var elClass = e.className;
	var elOnCh  = e.onchange;

	var newEl   = document.createElement( "SELECT" );
	newEl.options.length = 0;
	e.parentNode.insertBefore( newEl, e );
	e.parentNode.removeChild( e );

	newEl.name      = elName;
	newEl.id        = elID;
	newEl.className = elClass;
	newEl.onchange  = elOnCh;

	if( !isObject ) eval( el+" = newEl;" );
}



/**
* Square feet, hectare and square metre conversion (and now Acres!).
*
* by Bevan
*/	
	/**
	* convert feet into metres or metres to feet
	* 
	* The function ft2m() converts feet into metres and metres into feet
	* by taking the area to convert as input and return the converted value.
	* When inverse is set to true it converts metres into feet when false
	* visa versa.
	*
	* @author Bevan Wishart
	* @param float area the area to be converted
	* @param inverse reverse the conversion (metres to feet)
	*/
	function ft2m(area, inverse) {	
		if (!inverse)  {  
			area = area * 0.09290304; //convert feet to metres
		} else { 
			area = area / 0.09290304; //convert metres to feet
		} 
		return area;
	} //end ft2m

	/**
	* convert acres into feet or feet to acres
	* 
	* The function acres2m() converts acres into meters and meters into acres
	* by taking the area to convert as input and return the converted value.
	* When inverse is set to true it converts meters into acres when false
	* visa versa.
	*
	* @author Bevan Wishart
	* @param float area the area to be converted
	* @param inverse reverse the conversion (meters to acres)
	*/
	function acre2m(area, inverse) {
		//1 acre = 4046.8564224 square meter	
		if (!inverse) {
			area = area * 4046.8564224; //convert acres to meters
		} else {
			area = area / 4046.8564224; //convert meters to acres
		}
		return area;
	} //end acre2m
	
	/**
	* convert metres to hectares and hectares to metres 
	* 
	* The function ha2m() converts hectares to metres and metres to hectares
	* by taking the area to convert as input and return the converted value.
	* When inverse is set to true it converts meters into hectares when false
	* visa versa.
	*
	* @author Bevan Wishart
	* @param float area the area to be converted
	* @param inverse reverse the conversion (metres to hectares)
	*/
	function ha2m (area, inverse) {
		if (! inverse)   area = area * 10000; //convert hectares to metres
		if (inverse)     area = area / 10000; //convert metres to hectares
		return area;
	} //end ha2m

	/**
	* convert between hectares, feet and metres
	* 
	* The function dualConversion() converts between hectares, metres and feet
	* by taking the area, units to convert from and units to convert to and
	* returning the converted value.
	* When dealing with hectares they are always converted to metres
	* (e.g. to convert 1ha to ft, first convert 1ha to 10000m and then 10000m into ft)
	*
	* @author Bevan Wishart
	* @param float area the area to be converted
	* @param string fromUnit units to convert from
	* @param string toUnit units to convert to
	*/	
	function dualConversion (area, fromUnit, toUnit) {
		//If the same unit was size was set for both values, just return the area as is.
		if (fromUnit == toUnit) return area;				


		//Firstly, whatever conversion, we convert it to meters.	
		switch (fromUnit) {

		  //Convert from hectares
		  case 'ha': 
			area = ha2m(area); //Convert from hectares to metres
			break; 
		
		  //Convert from feet
		  case 'ft':
		  	area = ft2m(area); //feet to metres
			break;

		  //convert from acres
		  case 'acre':
		  	area = acre2m(area); //acres to meters
			break;

		  //convert from acres
		  case 'm2':
		  	//do nothing, where happy with meters
			break;

		  default:
			alert('function dualConversion(): Unsupported from unit specification. fromUnit: '+ fromUnit);
			break;
		} //end switch (fromUnit)

		//Now convert our meters to the new unit
		switch (toUnit) {

		  //Convert to hectares
		  case 'ha': 
		  	area = ha2m(area, true); //Convert the metres into hectares
			break; 
		
		  //Convert to feet
		  case 'ft':
		  	area = ft2m(area, true); //convert the metres into feet
			break;

		  //convert to acres
		  case 'acre':
			area = acre2m(area, true); //Convert the metres into acres
			break;

		  //convert to acres
		  case 'm2':
			//do nothing, we should be converted to meters already
			break;

		  default:
			alert('function dualConversion(): Unsupported to unit conversion specification. toUnit: ' + toUnit);
			break;
		} //end switch (toUnit)

		return area;


	} //end dualConversion()

