////$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, formonly) {
	/*
	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.
	- formonly (opt.) (true|false) : only search amount form field objects?
	OUTPUT:
	- the object
	*/
	var obj = getele(name, document, formonly);
	if (obj === null) js_error("Object could not be found on this page.", name);
	return obj;
}

function is_obj(name, document, formonly) {
	/*
	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.
	- formonly (opt.) (true|false) : only search amount form field objects?
	OUTPUT:
	- true or false
	*/
	if (getele(name, document, formonly) === null) {
		return false;
	} else {
		return true;
	}
}

function getele(n, d, f){
	/*
	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
	argument 'f' can be set to true to look for form fields only
	*/
	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(!f&&!(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(!f&&!x&&d.getElementById) x=d.getElementById(n);if(typeof x=="undefined")x=null; return x;
}

function count_props(o) {
	/*
	DESCRIPTION:
	- count properties on a object (works on array as well, but if you KNOW it's an array use the length property)
	INPUT:
	- o (req.) : reference to the object
	OUTPUT:
	- number
	*/
	var c=0;
	for (var k in o) {
	    if (o.hasOwnProperty(k)) {
	       ++c;
	    }
	}
	return c;
}

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 || typeof val == "undefined" || 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) {
	/*
	DESCRIPTION:
	- raise Javascript error
	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 = location.protocol +"//"+ location.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);
	}
}

function stop_execution() {
	//stop further processing of Javascript (until iniated again)
	js_error("-SILENT ERROR TO STOP EXECUTION-", "-none-", "SILENT");
}
