/*
	Central script which must be included in the portal main page
*/

/*
	Creates a new Jgis instance
*/
function Jgis(){
	this.initFunctions = new Array();
}

/* 
	Called when the map is loaded and the relevant 
	parameters are available. Calls initComponents()
*/
Jgis.prototype.mapLoaded = function(){

}

/*
	Calls the registered function on each component
*/
Jgis.prototype.initComponents = function(){
	for(var i = 0; i < this.initFunctions.length; i++){
		this.initFunctions[i]();
	}
}
Jgis.prototype.initPortalPage = function(){
    this.initWindowName();
    this.initComponents();
}

Jgis.prototype.initWindowName = function(){
		if ( !window.name || 
			window.name.length == 0 ) {
			window.name="portal";
		}
	}

Jgis.prototype.registerInitFunction = function(func){
	this.initFunctions.push(func);
}

Jgis.prototype.setError = function(ele){
	ele.className = ele.className+ " validation-error";	
}

Jgis.prototype.clearError = function(ele){
	ele.className = ele.className.replace(/validation-error/g,"");
}

Jgis.prototype.hasError = function(ele){
	return (ele.className.search(/validation-error/) >-1);
}

jgis = new Jgis();

jgis.workingId = "working-banner";

/*
	Calls all registered scripts. 
	
	Scripts are registered using jgis.registerInitFunction();
*/
function jgis_initComponents(){
	jgis.initComponents();
}

/*
	Backstop to provide onsubmit() behaviour
*/
Jgis.prototype.preSubmit = function(obj){
	var o = xbGetElementById(jgis.workingId);
	if ( o ) {
		var s = new xbStyle(o);
		s.setVisibility('visible');
	} 
	return true;
}

/*
	Wrapper to submit a form
*/ 
Jgis.prototype.submit = function(form){
	this.preSubmit(form);
	form.submit();
}

/*
    Resets all text field of given form to blank values regardless of the initial value
*/
Jgis.prototype.resetForm = function(frm){
	for (var idx = 0; idx < frm.elements.length; idx++){
		var e = frm.elements[idx];
		if ( e.value && e.type=="text") e.value ="";
	}
}
