// -- Infogate AB's standardfunktioner
// -- av Mattias Rundqvist
// --
// -- CVS version: $Revision: 4995 $

var IG_prototypes = true;

Array.prototype.exists = function( value ) {
    for(count = 0; count<this.length; count++) {
        if( this[count] == value ) return true;
    }
    return false;
}

Array.prototype.append = function( ar ) {
	if( typeof ar == "object" && ar.length ) {
		for( count = 0; count < ar.length; count++ ) {
			this[this.length] = ar[count];
		}
	}
}

String.prototype.isMatch = function( valid ) {
	for( count = 0; count < this.length; count++ ) {
		if( valid.indexOf(this.substring(count,count+1)) == -1 )
			return false;
	}
	return true;			
};

String.prototype.isPattern = function( pattern ) {
	
	if( typeof pattern == "string" ) {
		var expression = "/^";
		var sign = "";
		for( count = 0; count<pattern.length; count++ ) {
			sign=pattern.substring(count,count+1);
			if( sign == "?" ) {
				expression+=".{1}";
			} else if( sign=="." ) {
				expression+="\\.{1}";
			} else if( sign=="*" ) {
				expression+=".{0,}";
			} else {
				expression+=(sign+"{1}");
			}
		}
		expression += "$/";
		pattern=eval(expression);
	}
	if(this.search(pattern)==0)
		return true;
	return false;
};

String.prototype.isNum = function() {
	return this.isMatch("1234567890");
};

String.prototype.isDec = function() {
	return this.isMatch("1234567890,.");			
};

String.prototype.isAlpha = function() {
	return this.isMatch("abcdefghijklmnopqrstuvwxyzåäöæøüABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖÆØÜ ");			
};

String.prototype.trim = function() {
	return this.replace(/^\s*/,'').replace(/\s*$/,'');
};
window.onload = function initMenu() {
	
	id="navigation";
	var count = 0;	
	var a = document.getElementById(id).getElementsByTagName("A");
	
	while( count < a.length ) {
		if( a[count].parentNode.childNodes[2] ) {
			a[count].className += " parent";
		}
		a[count].parentNode.className+=" hide";
		a[count].parentNode.id=(id+"li"+count);
		
		a[count].parentNode.onmouseover=function(e){
			clearTimeout(window[id+this.id]);
			window[id+this.id]=setTimeout("toggleNavigation('"+this.id+"','show')",300);
		};
		a[count].parentNode.onmouseout = function() {
			clearTimeout(window[id+this.id]);
			window[id+this.id]=setTimeout("toggleNavigation('"+this.id+"','hide')",300);
		};
		a[count].onfocus = function() {
			var obj = this;
			while( obj.tagName != "DIV" ) {
				if( obj.tagName == "LI" ) {
					toggleNavigation(obj.id,"show");
				}
				obj = obj.parentNode;
			}
		}
		a[count].onblur = function() {
			var obj = this;
			while( obj.tagName != "DIV" ) {
				if( obj.tagName == "LI" ) {
					toggleNavigation(obj.id,"hide");
				}
				obj = obj.parentNode;
			}
		}
		count++;
	}	
}

function toggleNavigation( id, className ) {
	if(typeof id == "undefined")return;
	var obj = document.getElementById( id );
	obj.className = (obj.className.replace("show","").replace("hide","").replace("switch","").trim() + " " + className).trim();
	if( getPos(id)+(2*158)>screenSize()["x"] ) {
		obj.className += " switch";
		//log.write(obj.className);
	}
	//log.write(className + ", id:" + id + ", pos:" + getPos(id) + ", width:" + screenSize()["x"] + ", hasroom:" + (getPos(id)+(2*158)>screenSize()["x"]?"no":"yes"));
}

function screenSize() {
	var x,y;
	
	if (self.innerHeight) {
		x = self.innerWidth;
		y = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	} else if (document.body) {
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	return { "x" : x, "y" : y, "width" : x, "height" : y };
}

function getPos( layer ) {
	var obj = document.getElementById( layer );
	var pos = obj.offsetLeft;
	while( obj = obj.offsetParent ) {
		pos += obj.offsetLeft;
	}
	return pos;
}





function logger( form, field ) {
	this.write = function( text ) {
		document.forms[form][field].value = (text + "\n") + document.forms[form][field].value;
	}
}

//var log = new logger( "log","data");
