// 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 String(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;
}


////////////////////// Debug Functions

function SetDebugText(text, tagid)
{
	try
	{
		document.getElementById(tagid).innerHTML = text;
	}
	catch(err)
	{
		//
	}
  return true;
}

function SetErrorText(error, tagid)
{
	try
	{
		var errtext = "There is an error in your entry";
		for (errid in error)
		{
			errtext += errid + " = " + error[errid];
		}
		SetDebugText(errtext, tagid);
	}
	catch(err)
	{
		//
	}
  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)
	{
		//
	}
}


////////////////////// General Functions

PID = Math.PI / 180;

function BugOut(text)
{
	try
	{
		console.debug("BugOut - - ", text);
	}
	catch(err)
	{
		//
	}
	return true;
}

function ArrayFill(start, end)
{
	output = new Array();
	for(var i = start; i <= end; i++)
	{
		output[i] = i;
	}
//	console.debug("start = %s - end = %s", start, end);
//	console.debug(output);
//	BugEcho("ArrayFill() - - ");
	return output;
}

function ArrayFillRandom(start, end)
{
	output = ArrayFill(start, end);
	for(var i = start; i <= end; i++)
	{
		j = i;
		while(j == i){j = GetInteger(start, end, 1);}
		z = output[i];
		output[i] = output[j];
		output[j] = z;
	}
//	console.debug(output);
//	BugEcho("ArrayFill() - - ");
	return output;
}

function GetDouble(start, end, sig, neg)
{
	Wait(1);
	range = end - start;
	var output = Math.random() * range + start;
	switch(neg)
	{
		case 2:
		output = (2 * Math.random() > 1) ? output : - output;
		break;
		case 3:
		output = - output;
		break;
	}
	output = output.toFixed(sig);
//	BugEcho("GetDouble() - - ");
	return 1 * output;
}

function GetExponential(start, end, sig, neg, estart, eend, eneg, esig)
{
	Wait(2);
	var output = GetDouble(start, end, sig, neg) + "e" + GetInteger(estart, eend, eneg);
	output = Number(output).toExponential(esig - 1);
//	BugEcho("GetExponential() - - ");
	return output;
}

function GetInteger(start, end, neg)
{
	Wait(1);
	range = end - start + 1;
	var output = parseInt(Math.random() * range + start);
	var output = parseInt(Math.random() * range + start);
	switch(neg)
	{
		case 2:
		output = (2 * Math.random() > 1 || output == 0) ? output : - output;
		break;
		case 3:
		output = - output;
		break;
	}
//	BugEcho("GetInteger() - - ");
	return output;
}

function GetRandom(input, neg)
{
	Wait(4);
	var len = input.length;
	var ran = GetInteger(0, len - 1);
	var output = input[ran];
	switch(neg)
	{
		case 2:
		output = (2 * Math.random() > 1) ? output : - output;
		break;
		case 3:
		output = - output;
		break;
	}
//	BugEcho("GetRandom() - - ");
	return output;
}

function GetSign(input)
{
	var output = 0;
	if(input > 0) 
		output = 1;
	else if(input < 0) 
		output = -1;
//	BugEcho("GetSign() - - ");
	return output;
}

function GetNegative(input)
{
	var output = (input < 0) ? " + " + input : " - " + input;
//	BugEcho("GetSign() - - ");
	return output;
}

function GetExp(num, digit)
{
	return Number(num).toExponential(digit - 1);
}

function GetFix(num, digit)
{
	return Number(num).toFixed(digit);
}

function GetPre(num, digit)
{
	return Number(num).toPrecision(digit);
}

function SetImage(imageid)
{
	try
	{
		document.getElementById("pageimage").innerHTML = "<img src='" + imageid + "' height='559' width='794' align='center' /> <a onclick='SetImageClear();' >Click To Clear Image </a>";
	}
	catch(err)
	{
		ShowError(err, "GameSetImage()");
	}
//	BugEcho("GameSetImage() - - ");
	return;
}

function SetImageClear()
{
	try
	{
		document.getElementById("pageimage").innerHTML = "Click Link To Show Figure ";
	}
	catch(err)
	{
		ShowError(err, "GameSetImage()");
	}
//	BugEcho("GameSetImageClear() - - ");
	return;
}

function Wait(ms) 
{
	ms += new Date().getTime();
	while (new Date() < ms){}
} 


////////////////////// 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 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 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=800,width=1000,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 DoDemoChange(urltext)
{
	DoChange();
//	if(document.getElementById("inputmode").value == "demo")
	{
		DoAction("p", urltext);
	}
	return true;
}

function DoReset()
{
	document.getElementById("changemode").value = 0;
	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 SelectText(id)
{
    document.getElementById(id).select();
}

function SetTime()
{
	var time = new Date();
	var epoch = Math.round(time.getTime() / 1000);
	document.pipform.localtime.value = epoch;
//	return epoch;
}

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

