//Because IE doesn't know how to name it's elements...
document.createNamedElement = function( type, name ){
	var element = null;
	try{
		element = document.createElement('<'+type+' name="'+name+'">');
	}catch(e){}

	if (!element || element.nodeName != type.toUpperCase()) {
		element = document.createElement(type);
		element.name = name;
	}

	return element;
}
document.createElementAdvanced = function( tagName, props ){
	if( !tagName ) alert("you must give a tag name");

	var elmnt = null;
	if( props["name"] ){
	 	elmnt = document.createNamedElement( tagName, props["name"] );
	} else {
		elmnt = document.createElement( tagName );
	}

	if( !elmnt ) alert("invalid tagname: " + tagName);

	return applyPropertiesToElement( elmnt, props );
}

function applyPropertiesToElement( elmnt, props ){
	if( props["id"] ) elmnt.id = props["id"];
	if( props["name"] ) elmnt.name = props["name"];
	if( props["class"] ) elmnt.className = props["class"];
	if( props["type"] ) elmnt.type = props["type"];
	if( props["value"] ) elmnt.value = props["value"];
	if( props["checked"] ) elmnt.checked = props["checked"];
	if( props["selected"] ) elmnt.selected = props["selected"];
	if( props["innerHTML"] ) elmnt.innerHTML = props["innerHTML"];
	if( props["src"] ) elmnt.src = props["src"];
	if( props["href"] ) elmnt.href = props["href"];
	if( props["onClick"] ) elmnt.onclick = props["onClick"];
	if( props["style"] ){
		var style = props["style"];
		if( style["width"] ) elmnt.style.width = style["width"];
		if( style["height"] ) elmnt.style.height = style["height"];
		if( style["clear"] ) elmnt.style.clear = style["clear"];
		if( style["position"] ) elmnt.style.position = style["position"];
		if( style["margin"] ) elmnt.style.margin = style["margin"];
		if( style["margin-left"] ) elmnt.style.marginLeft = style["margin-left"];
		if( style["margin-right"] ) elmnt.style.marginRight = style["margin-right"];
		if( style["margin-top"] ) elmnt.style.marginTop = style["margin-top"];
		if( style["margin-bottom"] ) elmnt.style.marginBottom = style["margin-bottom"];
		if( style["border"] ) elmnt.style.border = style["border"];
		if( style["border-left"] ) elmnt.style.borderLeft = style["border-left"];
		if( style["border-right"] ) elmnt.style.borderRight = style["border-right"];
		if( style["border-top"] ) elmnt.style.borderTop = style["border-top"];
		if( style["border-bottom"] ) elmnt.style.borderBottom = style["border-bottom"];
		if( style["padding"] ) elmnt.style.padding = style["padding"];
		if( style["padding-left"] ) elmnt.style.paddingLeft = style["padding-left"];
		if( style["padding-right"] ) elmnt.style.paddingRight = style["padding-right"];
		if( style["padding-top"] ) elmnt.style.paddingTop = style["padding-top"];
		if( style["padding-bottom"] ) elmnt.style.paddingBottom = style["padding-bottom"];
		if( style["float"] ) elmnt.style.float = style["float"];
		if( style["display"] ) elmnt.style.display = style["display"];
		if( style["font-size"] ) elmnt.style.fontSize = style["font-size"];
		if( style["text-align"] ) elmnt.style.textAlign = style["text-align"];
	}

	return elmnt;
}

/* Simple debug system */
function DebugObject(){
	this.active = false; //the on-off switch for debugging
	this.debugWindow = null;

	this.spawnWindow = function(){
		this.debugWindow = window.open("", "", 'width=500,height=300,resizable=yes,scrollbars=yes');
	}
	this.print = function(text){
		if( !this.active ) return;
		if( !this.debugWindow ) this.spawnWindow();
		this.debugWindow.document.write(text);
	}
	this.println = function(text){
		if( !this.active ) return;
		if( !this.debugWindow ) this.spawnWindow();
		this.debugWindow.document.write(text + "<br>");
	}
}
var debug = new DebugObject();

/*
 * simple way to parse xml. psudo-extends the Node object.
 */
function XMLMapObject( xmlNode ){
	this.getNode = function( tagName ){
		for(var i=0; i<xmlNode.childNodes.length; i++){
			if( xmlNode.childNodes[i].tagName == tagName ){
				return xmlNode.childNodes[i];
			}
		}
		return null;
	};

	this.getXMLMapNode = function( tagName ){
		for(var i=0; i<xmlNode.childNodes.length; i++){
			if( xmlNode.childNodes[i].tagName == tagName ){
				return new XMLMapObject(xmlNode.childNodes[i]);
			}
		}
		return null;
	};

	this.getNodeArray = function( tagName ){
		var nodeArray = new Array();
		for(var i=0; i<xmlNode.childNodes.length; i++){
			if( xmlNode.childNodes[i].tagName == tagName ){
				nodeArray.push( xmlNode.childNodes[i] );
			}
		}
		return nodeArray;
	};

	this.get = function( tagName ){
		var node = this.getNode( tagName );
		if( node == null || node.childNodes[0] == null ) return null;
		return node.childNodes[0].nodeValue;
	};
}

//thanks to quirksmode.org for cookie functions
function createCookie(name,value,days){
	if(days){
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name){
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++){
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

//Jump to id
function jump( jump_name ) {
	document.location = '#'+jump_name;
}

//Select value in select block
function selectOptionValue( elemSelect, myValue ){
	try {
		var setFlag = false;
		for( i=0; i<elemSelect.options.length; i++ ){
			if( elemSelect.options[i].value == myValue ){
				elemSelect.selectedIndex = i;
				setFlag = true;
				continue;
			}
		}
		if( !setFlag ) elemSelect.selectedIndex = 0;
	}
	catch(e){}
}