Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};
String.prototype.trim = function(){
	return this.replace(/^\s+|\s+$/g, '');
}
function initiate(current){
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.onclick){
			anchor.href = "";
		}
	}
	try{
		window.xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
	}
	catch(e){
		window.xmlDoc = new window.XMLHttpRequest();
	}
	try{
		window.xmlDoc.async=false;
		window.xmlDoc.load("./index.php?act=03");
		navObj = window.xmlDoc.getElementsByTagName("page");
	}catch(e){
		window.xmlDoc.async=false;
		window.xmlDoc.open("GET", "./index.php?act=03", false);
		window.xmlDoc.send(null);
		navObj = window.xmlDoc.responseXML.documentElement.getElementsByTagName("page");
	}
	pageChange = false;
	window.navObjs = new Array();
	for(i=0;i<navObj.length;i++){
		id = navObj[i].getElementsByTagName("id")[0].childNodes[0].nodeValue;
		pageHash = navObj[i].getElementsByTagName("hash")[0].childNodes[0].nodeValue;
		targetedNode = document.getElementById("l"+id);
		targetedNode.href = pageHash;
		targetedNode.parentNode.className = "unselected";
		window.navObjs[pageHash.substr(1)] = targetedNode;
		if(location.hash == pageHash){
			window.oldLinkObj = "";
			repage(id);
			pageChange = true;
		}
	}
	
	page = location.hash.substr(0,7);
	if(!pageChange){
		window.oldLinkObj = document.getElementById('l'+current).parentNode;
		if(page == '#/blog/'){
			parts = location.hash.split('/');
			blogPost(parts[parts.length-2]);
		}
	}
	window.oldLinkObj.className = "selected";	
	
	var anchors = document.getElementById('cont').getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];
		if (anchor.href && anchor.rel){
			identifier = anchor.rel.substr(0,1);
			if(identifier == 'l'){
				anchor.href = document.getElementById(anchor.rel).href;
			}else if(identifier == 'b'){
				parts = anchor.href.split('/');
				anchor.href = '#/blog/' + parts[parts.length-3] + '/' + parts[parts.length-1];
			}
		}
	}
}
var Ajax = (function(){
	var stack = new Array();
	var poolSize = 10;
	var nullFunction = function() {};
	function createAjax() {
		if (window.XMLHttpRequest){
			return new XMLHttpRequest();
		}else if(window.ActiveXObject){
			return new ActiveXObject('Microsoft.XMLHTTP')
		}
	}
	for (var i = 0; i < poolSize; i++) {
		stack.push(createAjax());
	}
	return({
		release:function(inp){
			inp.onreadystatechange = nullFunction;
			stack.push(inp);
		},
		getInstance:function(){
			if (stack.length < 1) {
				return createXHR();
			} else {
				return stack.pop();
			}
		},
		toString:function(){
			return "stack size = " + stack.length;
		}
	});
})();

function repage(id){
	linkObj = document.getElementById('l'+id);
	if(linkObj != window.oldLinkObj){
		window.oldLinkObj.className = "unselected";
		linkObj.parentNode.className = "selected";
		window.oldLinkObj = linkObj.parentNode;
		document.title = "KaviSiegel.com - " + linkObj.innerHTML;
		document.getElementById('cont_title').innerHTML = linkObj.innerHTML;
		var ajx = Ajax.getInstance();
		var url = "./index.php?act=02&id="+id;
		ajx.open("GET", url, true);
		
		
		currheight = document.getElementById("cont").offsetHeight;
		
		ajx.onreadystatechange = function(){
			if (ajx.readyState<4) {
				var divHTML = '<div style="position: absolute; width: 403px; background: #FFFFFF; padding-top: 4px; height: 40px; border: 1px solid black" align="center"><b>Loading</b><br/><img src="images/loading.gif"></div>';
				document.getElementById('cont').innerHTML = divHTML + document.getElementById('cont').innerHTML;
			}else if(ajx.readyState==4){
				if (ajx.status==200){
					currheight = document.getElementById("cont").offsetHeight;
					document.getElementById('measure').innerHTML = ajx.responseText;
					ofsth = document.getElementById('measure').offsetHeight;
					document.getElementById('measure').innerHTML = "";
					var animation = new Fx.Morph(document.getElementById('cont'), {duration: 400, transition: Fx.Transitions.Sine.easeInOut});
					animation.start({
						height: ofsth
					});
					animation.complete = function(){
						animation.cancel();
						document.getElementById('cont').style.backgroundColor = "#FFFFFF";
						document.getElementById('cont').innerHTML = ajx.responseText;
						var anchors = document.getElementById('cont').getElementsByTagName("a");
						for (var i=0; i<anchors.length; i++){
							var anchor = anchors[i];
							if (anchor.href && anchor.rel){
								identifier = anchor.rel.substr(0,1);
								if(identifier == 'l'){
									anchor.href = document.getElementById(anchor.rel).href;
								}else if(identifier == 'b'){
									parts = anchor.href.split('/');
									anchor.href = '#/blog/' + parts[parts.length-3] + '/' + parts[parts.length-1];
								}
							}
						}
						pageTracker._trackPageview(location.hash);
					}
				}
				Ajax.release(ajx);
			}
		};
		ajx.send("");
	}
}

function blogPost(id){
	linkObj = document.getElementById('l05');
	if(linkObj != window.oldLinkObj){
		window.oldLinkObj.className = "unselected";
		linkObj.parentNode.className = "selected";
		window.oldLinkObj = linkObj.parentNode;
		document.title = "KaviSiegel.com - " + linkObj.innerHTML;
		document.getElementById('cont_title').innerHTML = linkObj.innerHTML;
		var ajx = Ajax.getInstance();
		var url = "./index.php?act=04&id="+id;
		ajx.open("GET", url, true);
		
		
		ajx.onreadystatechange = function(){
			if (ajx.readyState==1) {
				currheight = document.getElementById("cont").offsetHeight;
				var divHTML = '<div style="height: '+currheight+'px;" align="center"><b>Loading</b><br/><img src="images/loading.gif"></div>';
				document.getElementById('cont').innerHTML = divHTML;
			}else if(ajx.readyState==4){
				if (ajx.status==200){
					currheight = document.getElementById("cont").offsetHeight;
					document.getElementById('measure').innerHTML = ajx.responseText;
					ofsth = document.getElementById('measure').offsetHeight;
					document.getElementById('measure').innerHTML = "";
					var animation = new Fx.Morph(document.getElementById('cont'), {duration: 400, transition: Fx.Transitions.Sine.easeInOut});
					animation.start({
						height: ofsth
					});
					animation.complete = function(){
						animation.cancel();
						document.getElementById('cont').innerHTML = ajx.responseText;
						var anchors = document.getElementById('cont').getElementsByTagName("a");
						for (var i=0; i<anchors.length; i++){
							var anchor = anchors[i];
							if (anchor.href && anchor.rel){
								identifier = anchor.rel.substr(0,1);
								if(identifier == 'l'){
									anchor.href = document.getElementById(anchor.rel).href;
								}else if(identifier == 'b'){
									parts = anchor.href.split('/');
									anchor.href = '#/blog/' + parts[parts.length-2] + '/' + parts[parts.length-1];
								}
							}
						}
						pageTracker._trackPageview(location.hash);
					}
				}
				Ajax.release(ajx);
			}
		};

		ajx.send("");
	}
}

function validateEmail(str){
	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}
function contactForm(){
	error = false;
	document.getElementById('contact_error').innerHTML = '';
	var elements = new Array('contact_name', 'contact_email', 'contact_subject', 'contact_message', 'contact_checkbox');
	for(x = 0; x <= 4; x++){
		document.getElementById(elements[x]+'_label').className = 'clear';
		switch(elements[x]){
			case "contact_email":
				if(!window.validateEmail(document.getElementById('contact_email').value)){
					error = true;
					document.getElementById('contact_email_label').className = 'error';
				}
				break;
			case "contact_checkbox":
				if(!document.getElementById(elements[x]).checked){
					error = true;
					document.getElementById(elements[x]+'_label').className = 'error';
				}
			default:
				if(document.getElementById(elements[x]).value.trim() == ''){
					error = true;
					document.getElementById(elements[x]+'_label').className = 'error';
				}
				break;
		}
	}
	if(error){
		document.getElementById('contact_error').innerHTML = 'Please fix the errors in the bold red feilds above.';
	}else{
		data = 'name='+escape(document.getElementById('contact_name').value)+'&';
		data += 'email='+escape(document.getElementById('contact_email').value)+'&';
		data += 'subject='+escape(document.getElementById('contact_subject').value)+'&check=ajax&';
		data += 'message='+escape(document.getElementById('contact_message').value)+'&';
		data += 'copy='+document.getElementById('contact_emailcopy').checked;
		var ajx = Ajax.getInstance();
		var url = "./index.php?act=01";
		ajx.open("POST", url, true);
		ajx.onreadystatechange = function(){
			if (ajx.readyState==1) {
				var divHTML = '<div style="height: 40px;" align="center"><b>Loading</b><br/><img src="images/loading.gif"></div>';
				document.getElementById('cont').innerHTML = divHTML;
			}else if(ajx.readyState==4){
				if (ajx.status==200){
					document.getElementById('cont').innerHTML = ajx.responseText;
				}
				Ajax.release(ajx);
			}
		};
		ajx.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		ajx.send(data);
	}
}
