/*
	Various Functions, extentions and objects.
	Extends IE and (mainly) Firefox
	License: http://creativecommons.org/licenses/by-nc-sa/2.5/

	Controlled by Hakan Bilgin © 2005
*/

var $escape = ['"=$34;', '\'=$39;', '<=$60;', '>=$62;'];
var available = ['sv', 'en'];
var IE = /msie/i.test(navigator.userAgent);
var LNG = (new RegExp(navigator.userLanguage,'i').test(available))? navigator.userLanguage : 'en';

String.prototype.trim		= function() {return this.replace(/(^\s*)|(\s*$)/g,'');}
String.prototype.stripHTML	= function() {return this.replace(/<.*?>/g, '');}

//window.onerror = function (e) {return true;}

if (!IE) {
	Document.prototype.selectNodes		= function($_XPath, $_xNode) {if(!$_xNode) $_xNode = this; oNSResolver = this.createNSResolver(this.documentElement); $_aItems = this.evaluate($_XPath, $_xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); $_aResult = []; for(i=0; i<$_aItems.snapshotLength; i++) $_aResult[i] = $_aItems.snapshotItem(i); return $_aResult;}
	Document.prototype.selectSingleNode = function($_XPath, $_xNode) {if(!$_xNode) $_xNode = this; $_xItems = this.selectNodes($_XPath, $_xNode); return ($_xItems.length > 0)? $_xItems[0] : null ;}
	Element.prototype.selectNodes		= function($_XPath) {return this.ownerDocument.selectNodes($_XPath, this);}
	Element.prototype.selectSingleNode	= function($_XPath) {return this.ownerDocument.selectSingleNode($_XPath, this);}
	
	Node.prototype.__defineGetter__('xml',		 function() {return (new XMLSerializer()).serializeToString(this);});
	Node.prototype.__defineGetter__('outerHTML', function() {return this.xml;});
	Node.prototype.__defineSetter__('outerHTML', function(s) {rng = this.ownerDocument.createRange(); rng.setStartBefore(this); cFrag = rng.createContextualFragment(s); this.parentNode.replaceChild(cFrag, this);});
	
	/* Event */
	Event.prototype.__defineGetter__('event',		function() {return this;});
	Event.prototype.__defineGetter__('clientY',		function() {return this.pageY;});
	Event.prototype.__defineGetter__('srcElement',	function() {node = this.target; while (node.nodeType != 1) node = node.parentNode; return node;});
	Event.prototype.__defineSetter__('cancelBubble',function(b) {if (b) this.stopPropagation();});
	Event.prototype.__defineSetter__('returnValue',	function(b) {if (!b) this.preventDefault();});
	
	$e = ['click', 'dblclick', 'mouseover', 'mouseout', 'mousedown', 'mouseup', 'mousemove', 'keydown', 'keypress', 'keyup', 'focus', 'blur'];
	for (i=0; i<$e.length; i++) document.addEventListener($e[i], function(e) {window.event=e;}, true);
}

function $($s) {
	return (typeof($s) == 'string')? document.getElementById($s) : $s ;
}

function getParent(el, a, v) {
	while (v? (eval('el.'+ a) != v && el.getAttribute(a) != v) : (!eval('el.'+ a) && !el.getAttribute(a))) {
		if (el == document.firstChild) return null;
		el = el.parentNode;
	}
	return el;
}

xdbc = {
	path		: '/xmlio/xdbc.php',
	cmd			: function($cmdId) {return xdbc.ram.selectSingleNode('//cmd/*[@_cmdId='+ $cmdId +']').cloneNode(true);},
	xslt		: function($xslId) {return xdbc.ram.selectSingleNode('//_doc[@title=\'doctype\']/*[@id='+ $xslId +']').getAttribute('data');},
	http		: function() {return IE? new ActiveXObject(xdbc.otype['XmlHttp']) : new XMLHttpRequest();},
	createNode	: function(name) {return IE? xdbc.que.parentNode.createNode(1, name, '') : xdbc.que.parentNode.createElement(name) ;},
	flushQue	: function() {while (xdbc.que.firstChild) xdbc.que.removeChild(xdbc.que.firstChild);},
	init : function() {
		xdbc.prefix();
		xdbc.xslDoc = new Array();
		xdbc.que = xdbc.load('<data></data>').documentElement;
		//newCmd = xdbc.createNode('doc');
	},
	load : function($xSrc) {
		$dom = IE? new ActiveXObject(xdbc.otype['DomDocument']) : document.implementation.createDocument('', '', null);
		if ($xSrc) {
			if (IE) {
				if ($dom.loadXML($xSrc)) return $dom;
			} else {
				$dom = new DOMParser().parseFromString($xSrc, 'text/xml');
				if ($dom.firstChild.nodeName != 'parsererror') return $dom;
			}
			$dom.async = false;
			$dom.load($xSrc);
			/**/
			if (!IE && !$dom.xml) {
				var $tmp = new XMLHttpRequest();
				$tmp.open("GET", $xSrc, false);
				$tmp.send(null);
				$dom = xdbc.load($tmp.responseText);
			}
			
		}
		return $dom;
	},
	prefix : function() {
		xdbc.otype = new Array();
		type = ['DomDocument', 'XmlHttp'];
		prfx = ['Microsoft', 'MSXML', 'MSXML2', 'MSXML3'];
		for (n=0; n<type.length; n++) {
			for (p=0; p<prfx.length; p++) {
				try {
					new ActiveXObject(prfx[p] +'.'+ type[0]);
					xdbc.otype[type[n]] = prfx[p] +'.'+ type[n];
				}
				catch (e) {}
			}
		}
	},
	transform : function($xml, $xsl) {
		$xml = (typeof($xml) == 'string')? xdbc.load($xml) : $xml ;
		if (typeof($xsl) == 'object') $xsl = $xsl;
		else if (!xdbc.xslDoc[$xsl]) $xsl = xdbc.xslDoc[$xsl] = xdbc.load($xsl);
		else $xsl = xdbc.xslDoc[$xsl];
		
		if (IE) xTrans = $xml.transformNode($xsl.documentElement);
		else {
			xslPrc = new XSLTProcessor();
			xslPrc.importStylesheet($xsl);
			xTrans = xslPrc.transformToFragment($xml, document).xml;
		}
		xTrans = xTrans.replace(/&amp;/ig, '&');
		xTrans = xTrans.replace(/&lt;/ig, '<');
		xTrans = xTrans.replace(/&gt;/ig, '>');
		xTrans = xTrans.replace(/&quot;/ig, '"');
		return xTrans;
	},
	escape : function($iStr) {
		for ($e=0; $e<$escape.length; $e++) 
			$iStr = $iStr.replace(new RegExp($escape[$e].slice(0,1), 'g'), $escape[$e].slice(2));
		return $iStr;
	},
	addQue : function($pNode) {
		$pNode = $pNode.cloneNode(true);
		$nAttr = $pNode.getAttribute('_selby');
		if ($nAttr) {
			for ($a=1; $a<arguments.length; $a++) 
				$nAttr = $nAttr.replace(new RegExp('\\$prm'+ ($a), 'g'), arguments[$a]);
					$pNode.setAttribute('_selby', $nAttr);
		}
		if ($pNode.getAttribute('_intprt').slice(0,1) == 'A') {
			for (a=0; a<$pNode.attributes.length; a++) {
				$pNodeVal = $pNode.attributes.item(a).value;
				$pNode.attributes.item(a).value = ($pNode.attributes.item(a).name.slice(0,1) == '_')? xdbc.escape($pNodeVal) : $pNodeVal = escape($pNodeVal).replace(/%20/ig, ' ');
			}
		}
		xdbc.que.appendChild($pNode);
	},
	exec : function($pNode) {
		if ($pNode) xdbc.addQue($pNode);
		if (!xdbc.que.childNodes.length) return 'Nothing qued!';
		xmlHttp = xdbc.http();
		xmlHttp.open('POST', xdbc.path, false);
		xmlHttp.send(xdbc.que.parentNode);
		xdbc.flushQue();
		if (!xmlHttp.responseXML.documentElement) alert('An error has encountered!');
		else {
			xAll = xmlHttp.responseXML.selectNodes('//*');
			for (x=0; x<xAll.length; x++) {
				if (!xAll[x].attributes) continue;
				for (a=0; a<xAll[x].attributes.length; a++) {
					xVal = unescape(xAll[x].attributes.item(a).value);
					xAll[x].attributes.item(a).value = xVal;
				}
			}
			return xmlHttp.responseXML;
		}
	}
}
xdbc.init();


var days	= ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
var months	= ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

create = {
	shortDate : function (sDate) {
		if (!sDate) {
			sDate = new Date();
			sDate = sDate.getFullYear() +'-'+ create.validInt(sDate.getMonth()+1) +'-'+ create.validInt(sDate.getDate());
		}
		return sDate.split(' ')[0];
	},
	longDate : function (sDate) {
		sDate = sDate.split(' ')[0].split('-');
		sDate = new Date(sDate[0], sDate[1]-1, sDate[2]);
		dInt = new Number(sDate.getDay()) - 1; dInt = dInt < 0? 6 : dInt;
		return days[dInt] +', '+ months[sDate.getMonth()] +' '+ sDate.getDate() +', '+ sDate.getFullYear();
	},
	category : function(i) {
		xbCat = xdbc.ram.selectSingleNode('//category[@id='+ i +']');
		return xbCat? xbCat.getAttribute('title') : '' ;
	},
	comments : function(i) {
		return (i > 1)? i +' comments' : i +' comment';
	},
	dateObj : function (dObj) {
		if (typeof dObj == 'string') aStr = dObj.split(' ')[0].split('-');
		else aStr = [dObj.getFullYear(), dObj.getMonth()+1, dObj.getDate()];
		return new Date(aStr[0], aStr[1]-1, aStr[2]);
	},
	validInt : function (i) {
		return (new Number(i) < 10)? '0'+ i : i ;
	},
	calDate : function($o, $d, $i) {
		calTbl = document.getElementById('calHolder').getElementsByTagName('TABLE')[0];
		$dSt = $o ? $o.split('-') : calTbl.getAttribute('date').split('-');
		$y = $d=='y'? new Number($dSt[0])+$i : $dSt[0];
		$m = $d=='m'? new Number($dSt[1])+$i : $dSt[1];
		$d = !$o && $d=='d'? new Number($dSt[2])+$i : 1;
		$od = new Date($y, $m, $d);
		return $od.getFullYear() +'-'+ create.validInt($od.getMonth()) +'-'+ create.validInt($od.getDate());
	},
	calendar : function (dObj) {
		dObj = create.dateObj(dObj);
		now = new Date();
		oTmp= new Date(dObj.getFullYear(), dObj.getMonth(), 1);
		firstDay = (oTmp.getDay() == 0)? 7 : oTmp.getDay() ;
		oTmp.setMonth(oTmp.getMonth() +(oTmp.getDate() == 31 || oTmp.getDate() == 30? 0 : 1));
		oTmp.setTime(oTmp.valueOf()-10000);
		monthLen = oTmp.getDate();
		
		cStr = '<table class="calendar" cellpadding="0" cellspacing="1" date="'+ dObj.getFullYear() +'-'+ create.validInt(dObj.getMonth()+1) +'-'+ create.validInt(dObj.getDate()) +'">';
		cStr += '<tr><th colspan="7" class="calHeader">'+ months[dObj.getMonth()] +' '+ dObj.getFullYear() +'</th></tr>';
		cStr += '<col/><col/><col/><col/><col/><col/><col class="sunday"/><tr>';
		for (d=0; d<7; d++) {
			isSun = (d==6)? ' class="sunday"' : '' ;
			cStr += '<th'+ isSun +'>'+ days[d].slice(0,3).toLowerCase() +'</th>';
		}
		cStr += '</tr>';
		for (w=0; w<6; w++) {
		  cStr += '<tr>';
			for (d=0; d<7; d++) {
				dt = ((w*7)+d)+1;
				tdTxt = (dt+1>firstDay && dt<monthLen+firstDay)? dt-firstDay+1 : '&#160;';
				cStr += (dObj.getYear() == now.getYear() && dObj.getMonth() == now.getMonth() && dt-firstDay+1 == now.getDate())? '<td class="today">' : '<td>';
				cStr += tdTxt +'</td>';
			}
		  cStr += '</tr>';
		}
		cStr += '</table>';
		return cStr;
	}
}