////$Id$////

/*
This file contain basic/system Javascript functions that should be included every time Javascript is used
Copyright © 2006 WinterNet Studio, Allan Jensen (www.winternet.no). All rights reserved.
*/

function get_obj(name, document) {
	/*
	DESCRIPTION:
	- get any object on a page
	- raises error if object is not found (contrary to getele() )
	INPUT:
	- name (req.) : name of object to get
	- document (opt.) : document object that contains the object. Defaults to current document/page.
	OUTPUT:
	- the object
	*/
	var obj = getele(name, document);
	if (obj === null) js_error("Object could not be found on this page.", name);
	return obj;
}

function is_obj(name, document) {
	/*
	DESCRIPTION:
	- determines if an object exists on a page
	INPUT:
	- name (req.) : name of object to get
	- document (opt.) : document object that contains the object. Defaults to current document/page.
	OUTPUT:
	- true or false
	*/
	if (getele(name, document) === null) {
		return false;
	} else {
		return true;
	}
}

function getele(n, d){
  //source: MM_findObj(n, d) { //v4.01 (not under copyright of WS)
  //argument 'n' is the name of the object you want to get
  //argument 'd' is the document object, it is not required
  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=getele(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function set_text(object, text, append) {
	// Set the text/HTML for an object on the page
	if (!append) {
		getele(object).innerHTML = text;
	} else {
		theobject = getele(object);
		theobject.innerHTML = theobject.innerHTML + text;
	}
}

function get_text(object) {
	// Get the text/HTML for an object on the page
	return getele(object).innerHTML;
}

function isinteger(val) {
	if (val === null || val.length == 0) return false;
	if (isNaN(val)) return false;  //is Not A Number
	return (setinteger(val) === parseFloat(val) ? true : false);  //true: is integer number, false: is float/decimal number
}

function setinteger(val) {
	return parseInt(val,10);
}

function js_error(msg, errcode, flags) {
	/*
	INPUT:
	- flags : string with optional keywords:
		- "CONTINUE" : don't halt all Javascript processing, but continue
		- "SILENT" : don't show anything, just stop processing Javascript
	*/
	if (!flags) flags = new String("");
	flag_silent   = (flags.indexOf("SILENT") == -1 ? false : true);
	flag_continue = (flags.indexOf("CONTINUE") == -1 ? false : true);
	if (!flag_silent) {
		msg = "Sorry, there was a problem...\n\n- "+ msg;
		if (errcode) msg += "  (CODE: "+ errcode +")";
		remove_url = "http://"+ jsglobals.host + jsglobals.pathprefix;
		url = document.location.href.substr(remove_url.length+1);
		if (url.length > 150) url = "(copy it from the browser's address line)";
		msg += "\n\nPlease contact the website developer if this is a persistent problem." +
		"\nWhen reporting errors please always provide:\n" +
		"\n- the address: "+ url +
		"\n- date and time: "+ jsglobals.servertime +
		"\n- as detailed an error description as possible" +
		"\n- the steps you went through to get the error";
		alert(msg);
	}
	if (!flag_continue) {  //TODO: change this function to use onerror when supported (so we only get one alert()) and use current setup when not supported
		window.onerror = function(msg, url, line) {  //More information on onerror: http://research.nihonsoft.org/javascript/jsref/evnt8.htm
			if (url || line) {  //Firefox does not provide this info
				alert("Extended error information:\n\nAddress: "+ url +"\nLine: "+ line);  //QUEST: this fires in IE but not in Firefox and Opera - even though at least Firefow seems to run it (because the throw afterwards is not seen)
			}
			return true;  //"To suppress the standard JavaScript error dialog, the function must return true"
		}
		throw "Further execution has been cancelled on purpose.";  //throw an exception/error that causes the browser to stop further execution of Javascript code, but do it silently (because of onerror above)
	}
}

function check_support(obj, obj_ref, continue_on_fail) {
	//OLD METHOD: has_support = eval("if("+ obj_ref +"){true}else{false}");
	if (obj) return true;
	if (continue_on_fail) {
		return false;
	} else {
		js_error("Sorry, your browser does not seem to comply with the technical standards used by this website. Please upgrade to newer version.", obj_ref);
	}
}
