// copyright pipeng 2000 - 2008
//
// Utility Functions
// Maths Functions
// Navigation Functions

////////////////////// Utility Functions

function count(array) // mimics php count() function
{
	return array.length;
}

function explode(sep, text) // mimics php explode() function
{
	return text.split(sep);
}

function implode(sep, array) // mimics php implode() function
{
	return array.join(sep);
}

function in_array(string,array) // mimics php in_array() function
{
	var output = false;
	var numcount = count(array);
	for(var i = 0; i < numcount; i++)
	{
		if (string == array[i])
		{
			output = true;
			break
		}
	}
    return output;
}

function is_array(obj) // mimics php is_array() function
{
   if (!obj || obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}

function ShowError(error, errtext) // show error object
{
   try
	{
		console.debug("ShowError() - - error text = %s", errtext);
		for (errid in error)
		{
			console.debug("ShowError() - - error [ " + errid + " ] = %s", error[errid]);
		}
		
	}
	catch(err)
	{
		//
	}
      return true;
}


////////////////////// Maths Functions
var PID = Math.PI / 180;

function acosd(input) // acos() function in degrees
{
   return Math.acos(input) * 180 / Math.PI;
}

function acosh(input) // mimics php acosh() function
{
   return Math.log(input + Math.sqrt(input * input - 1));
}

function asind(input) // asin() function in degrees
{
   return Math.asin(input) * 180 / Math.PI;
}

function asinh(input) // mimics php asinh() function
{
   return Math.log(input + Math.sqrt(input * input + 1));
}

function atan2d(input1, input2) // atan2() function in degrees
{
   return Math.atan2(input1, input2) * 180 / Math.PI;
}

function atand(input) // atan() function in degrees
{
   return Math.atan(input) * 180 / Math.PI;
}

function atanh(input) // mimics php atanh() function
{
   return Math.log((1 + input) / (1 - input)) / 2;
}

function cosd(theta) // cos() function in degrees
{
   return Math.cos(Math.PI / 180 * theta);
}

function cosh(theta) // mimics php cosh() function
{
   return (Math.exp(theta) + Math.exp(-theta)) / 2;
}

function log10(num) // mimics php log10() function
{
	num = Math.log(num) / Math.LN10;
	return num;
}

function sind(theta) // sin() function in degrees
{
   return Math.sin(Math.PI / 180 * theta);
}

function sinh(theta) // mimics php sinh() function
{
   return (Math.exp(theta) - Math.exp(-theta)) / 2;
}

function tand(theta) // tan() function in degrees
{
   return Math.tan(Math.PI / 180 * theta);
}

function tanh(theta) // mimics php tanh() function
{
   return (Math.exp(theta) - Math.exp(-theta)) / (Math.exp(theta) + Math.exp(-theta));
}

////////////////////// Navigation Functions

function DoAction(action, urltext)
{
//	document.write("DoAction() - - action = " + action + " - encoded url = " + urltext + "<br />");

	urltext = unescape(urltext);

//	document.write("DoAction() - - action = " + action + "unencoded url = " + urltext + "<br />");

	
	switch (action)
	{
		case "p":
		{
			document.getElementById("pipform").action = urltext;
			document.getElementById("pipform").submit();
			break;
		}
		case "kl":
		{
			window.location.href = urltext;
			break;
		}
		case "kw":
		{
			newwin = window.open(urltext, "popwin", "height=600,width=800,scrollbars=yes,toolbar=no,menubar=yes,status=yes");
			newwin.focus();
			break;
		}
	}
	
	return true;
}

function DoCalcChange()
{
	document.getElementById("changemode").value = 2;
	return true;
}

function DoChange()
{
	document.getElementById("changemode").value = 1;
	return true;
}

function DoSetChange(urltext, varname, uval)
{
	if(document.getElementById("inputmode").value == "calc")
	{
		DoChange();
		DoAction("p", urltext);
	}
	else
	{
		document.getElementById(varname).value = uval
	}
	return true;
}

function DoDemoChange(urltext)
{
	DoChange();
	if(document.getElementById("inputmode").value == "demo")
	{
		DoAction("p", urltext);
	}
	return true;
}

function DoReset()
{
	document.getElementById("changemode").value = 0;
	return true;
}

function ShowDialog(dialog)
{
	alert('Show Dialog ' + dialog);
	return true;
}

function AddBookmark() 
{
	var url = window.location;
	var title = document.title;

	if (window.sidebar && window.sidebar.addPanel) 
	{
		window.sidebar.addPanel(title, url,"");
	} 
	else if( window.external ) 
	{
		window.external.AddFavorite( url, title); 
	}
	else if(window.opera && window.print) 
	{
		return true; 
	}
	else
	{
		alert("sorry - unsupported browser type");
	}
 }

function SelectText(id)
{
    document.getElementById(id).select();
}

