// urchin tracking module(UTM), $Revision: 1.3 $, copyright 2005 urchin software corporation
var __utm1,__utm2,__utm3,__utmi,__utmn,__utmd,__utmu,__utmp,__utmr;
__utm1 = document.cookie.indexOf("__utm1=");
__utm2 = document.cookie.indexOf("__utm2=");
__utm3 = document.cookie.indexOf("__utm3=");
__utmn = Math.round(Math.random() * 4294967295);
__utmd = new Date();
__utmu = Math.round(__utmd.getTime()/1000);
if ((__utm1 < 0) || (__utm2 < 0)) {
 document.cookie="__utm1="+__utmn+"."+__utmu+"; path=/; expires=Sun, 18 Jan 2038 00:00:00 GMT";
 document.cookie="__utm2="+__utmu+"; path=/; expires=Sun, 18 Jan 2038 00:00:00 GMT";
 document.cookie="__utm3="+__utmu+"; path=/;";
} else if (__utm3 < 0) {
 document.cookie="__utm2="+__utmu+"; path=/; expires=Sun, 18 Jan 2038 00:00:00 GMT";
 document.cookie="__utm3="+__utmu+"; path=/;";
}
__utmr = document.referrer;
if (!__utmr) {__utmr = "-";}
else {
 __utmp = __utmr.indexOf(document.domain);
 if ((__utmp >= 0) && (__utmp <= 8)) __utmr = 0;
}
var __utmi = new Image(1,1);
__utmi.src ="/images/__utm.gif?utmn="+__utmn+"&utmr="+__utmr;

// quick links drop-down menu
function quickLinks(targ,selObj,restore) {
        eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
        if (restore) selObj.selectedIndex=0;
}

function showhide(id)  
{ 
 var style = document.getElementById(id).style  
 if (style.display == "block")  
  style.display = "none";  
 else  
  style.display = "block";  
}

// gallery links
function galleryLinks() {
	var URL = document.galleryList.links.options[document.galleryList.links.selectedIndex].value;
	window.location.href = URL;
}

// open external links in a new window
function externalLinkHandler() {
        if(document.getElementById) {
                linkTarget="other";

                for (var i=0; i<=(document.links.length-1); i++) {
                	isExternal=((document.links[i].href.indexOf("http://")!=-1)&&(document.links[i].href.indexOf("retro.xhtmled.com")==-1))

                        if(isExternal) {
                                document.links[i].target = linkTarget;
                        }
                }
        }
}

// create cookie in comment/contact forms
function setCookie (name, value, expires, path, domain, secure) {
    var curCookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
    document.cookie = curCookie;
}

// cookie detector
function getCookie (name) {
    var prefix = name + '=';
    var c = document.cookie;
    var nullstring = '';
    var cookieStartIndex = c.indexOf(prefix);
    if (cookieStartIndex == -1)
        return nullstring;
    var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
    if (cookieEndIndex == -1)
        cookieEndIndex = c.length;
    return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

// delete cookie
function deleteCookie (name, path, domain) {
    if (getCookie(name))
        document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

// fix date
function fixDate (date) {
    var base = new Date(0);
    var skew = base.getTime();
    if (skew > 0)
        date.setTime(date.getTime() - skew);
}

// cookie functions in contact/comment forms
var HOST = 'xhtmled.com';

// create cookie
function rememberMe (f) {
    var now = new Date();
    fixDate(now);
    now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
    setCookie('mtcmtauth', f.author.value, now, '/', HOST, '');
    setCookie('mtcmtmail', f.email.value, now, '/', HOST, '');
    setCookie('mtcmthome', f.url.value, now, '/', HOST, '');
}

// delete cookie
function forgetMe (f) {
    deleteCookie('mtcmtmail', '/', HOST);
    deleteCookie('mtcmthome', '/', HOST);
    deleteCookie('mtcmtauth', '/', HOST);
    f.email.value = '';
    f.author.value = '';
    f.url.value = '';
    f.bakecookie.checked = false;
}

// comment form error checking routine
function setFocus(aField) {
	document.forms[0][aField].focus();
}

function isAnEmailAddress(aTextField) {
	if (document.forms[0][aTextField].value.length<5) {
		return false;
	} else if (document.forms[0][aTextField].value.indexOf("@") < 1) {
		return false;
	} else if (document.forms[0][aTextField].value.length - document.forms[0][aTextField].value.indexOf("@") < 4) {
		return false;
	} else {
		return true;
	}
}

function isEmpty(aTextField) {
	if ((document.forms[0][aTextField].value.length==0) || (document.forms[0][aTextField].value==null)) {
		return true;
	} else {
		return false;
	}
}

function validate() {
	// check that the name field is valued
	if (isEmpty("author")) {
		alert("Please enter your \"Name\".");
		setFocus("author");
		return false;
	}
	// check that the email field is valued
	if (isEmpty("email")) {
		alert("Please enter your \"Email Address\".");
		setFocus("email");
		return false;
	}
	if (!isAnEmailAddress("email")) {
		alert("The \"Email Address\" that you entered is invalid.");
		setFocus("email");
		return false;
	}
	// check that comment field is valued
	if (isEmpty("text")) {
		alert("Please enter a \"Comment\".");
		setFocus("text");
		return false;
	}
	return true;
}

var isMinNS4 = (navigator.appName.indexOf("Netscape") >= 0 && parseFloat(navigator.appVersion) >= 4) ? 1 : 0;
var isMinNS6 = (isMinNS4 && navigator.userAgent.indexOf("Gecko")>=0) ? 1 : 0;
var isMinIE4 = (document.all) ? 1 : 0;
var isMinIE5 = (isMinIE4 && navigator.appVersion.indexOf("5.")) >= 0 ? 1 : 0;
var isDOM = (document.getElementById) ? 1 : 0;

// popups
function isWin(){
	if (navigator.appVersion.indexOf("Win") != -1)	return true;
	else return false;
}

function isIE(){
	if (navigator.appName == "Microsoft Internet Explorer") return true;
	else return false;
}

function isIEPC(){
	if (isWin() && isIE()) return true;
	else return false;
}

function popUp(image,title,width,height) {
	win = window.open('/popup/view.php?image=' + image + '&width=' + width + '&height=' + height + '&title=' + title + '','title','width=' + width + ',height=' + height + ',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no');
	if (isIEPC() != 1) {
		winname.focus();
	}
}

// general popups
function popup(imagesrc,imgwidth,imgheight) {
	winwidth = imgwidth;
	winheight = imgheight;
	var sX = 0;
	var sY = 0;
	var wstring = "width=" + winwidth;
	var hstring = "height=" + winheight;
	var winParams = wstring + "," + hstring + ",left=" + sX  + ",top=" + sY + ",toolbar=0,resizable=0,scrollbars=0,location=0,status=0"

	popwin=window.open("","",winParams);
	popwin.document.open();
	popwin.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"\r"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\r<html xmlns="http://www.w3.org/1999/xhtml">\r<head>\r<title>Enlarged Photo<\/title>\r<\/head>\r\r<body leftmargin="0" rightmargin="0" topmargin="0" marginwidth="0" marginheight="0">\r\t<div style="text-align:center;"><a href="javascript:self.close();" title="Click to close window"><img src="'+imagesrc+'" width="'+imgwidth+'" height="'+imgheight+'" border="0" alt=""><\/a><\/div>\r<\/body>\r<\/html>');
	popwin.document.close();
}

/*
// +----------------------------------------------------------------------+
// | Copyright (c) 2004 Bitflux GmbH                                      |
// +----------------------------------------------------------------------+
// | Licensed under the Apache License, Version 2.0 (the "License");      |
// | you may not use this file except in compliance with the License.     |
// | You may obtain a copy of the License at                              |
// | http://www.apache.org/licenses/LICENSE-2.0                           |
// | Unless required by applicable law or agreed to in writing, software  |
// | distributed under the License is distributed on an "AS IS" BASIS,    |
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      |
// | implied. See the License for the specific language governing         |
// | permissions and limitations under the License.                       |
// +----------------------------------------------------------------------+
// | Author: Bitflux GmbH <devel@bitflux.ch>                              |
// +----------------------------------------------------------------------+
*/
var liveSearchReq = false;
var t = null;
var liveSearchLast = "";
var isIE = false;

// on !IE we only have to initialize it once
if (window.XMLHttpRequest) {
	liveSearchReq = new XMLHttpRequest();
	newMastheadReq = new XMLHttpRequest();
}

function liveSearchInit() {
	if (navigator.userAgent.indexOf("Safari") > 0) {
		document.getElementById('livesearch').addEventListener("keydown",liveSearchKeyPress,false);
	} else if (navigator.product == "Gecko") {
		document.getElementById('livesearch').addEventListener("keypress",liveSearchKeyPress,false);	
	} else {
		document.getElementById('livesearch').attachEvent('onkeydown',liveSearchKeyPress);
		isIE = true;
	}
}

function liveSearchKeyPress(event) {
	if (event.keyCode == 40 )
	//KEY DOWN
	{
		highlight = document.getElementById("searchhighlight");
		if (!highlight) {
			highlight = document.getElementById("searchresult").firstChild.firstChild.nextSibling.nextSibling.firstChild;
		} else {
			highlight.removeAttribute("id");
			highlight = highlight.nextSibling;
		}
		if (highlight) {
			highlight.setAttribute("id","searchhighlight");
		} 
		if (!isIE) { event.preventDefault(); }
	} 
	//KEY UP
	else if (event.keyCode == 38 ) {
		highlight = document.getElementById("searchhighlight");
		if (!highlight) {
			highlight = document.getElementById("searchresult").firstChild.firstChild.nextSibling.nextSibling.lastChild;
		} 
		else {
			highlight.removeAttribute("id");
			highlight = highlight.previousSibling;
		}
		if (highlight) {
				highlight.setAttribute("id","searchhighlight");
		}
		if (!isIE) { event.preventDefault(); }
	} 
	//ESC
	else if (event.keyCode == 27) {
		highlight = document.getElementById("searchhighlight");
		if (highlight) {
			highlight.removeAttribute("id");
		}
		document.getElementById("searchresult").style.display = "none";
		document.forms.searchform.s.value = '';
	} 
}

function closeLiveSearch() {
	highlight = document.getElementById("searchhighlight");
	if (highlight) {
		highlight.removeAttribute("id");
	}
	document.getElementById("searchresult").style.display = "none";
	document.forms.searchform.s.value = '';
}

function liveSearchStart() {
	if (t) { window.clearTimeout(t); }
	t = window.setTimeout("liveSearchDoSearch()",200);
}

function liveSearchDoSearch() {
	if (liveSearchLast != document.forms.searchform.s.value) {
	if (liveSearchReq && liveSearchReq.readyState < 4) {
		liveSearchReq.abort();
	}
	if ( document.forms.searchform.s.value == "") {
		document.getElementById("searchresult").style.display = "none";
		highlight = document.getElementById("searchhighlight");
		if (highlight) {
			highlight.removeAttribute("id");
		}
		return false;
	}
	if (window.XMLHttpRequest) {
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		liveSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	liveSearchReq.onreadystatechange= liveSearchProcessReqChange;
	liveSearchReq.open("GET", "/livesearch.php?s=" + document.forms.searchform.s.value);
	liveSearchLast = document.forms.searchform.s.value;
	liveSearchReq.send(null);
	}
}

function liveSearchHover(el) {
		highlight = document.getElementById("searchhighlight");
		if (highlight) {
			highlight.removeAttribute("id");
		}
		el.setAttribute("id","searchhighlight");
}

function liveSearchClicked(el) {
		highlight = document.getElementById("searchhighlight");
		if (highlight) {
			highlight.removeAttribute("id");
		}
		el.setAttribute("id","searchhighlight");
		return liveSearchSubmit();
}

function liveSearchProcessReqChange() {
	if (liveSearchReq.readyState == 4) {
		var res = document.getElementById("searchresult");
		res.style.display = "block";
		res.firstChild.innerHTML = '<div id="searchcontrols"><div class="left">tip: use arrow keys + enter!</div><div class="right"><a href="javascript://" title="Close results" onclick="closeLiveSearch()">close (esc)</a></div><br /></div><div id="searchheader">top results:</div>'+liveSearchReq.responseText;//+'<div id="searchformore"><a href="/search/'+document.forms.searchform.s.value+'" title="More results">more results &raquo;</a></div>';
	}
}

function liveSearchSubmit() {
	var highlight = document.getElementById("searchhighlight");
	if (highlight && highlight.firstChild) {
		window.location = highlight.firstChild.getAttribute("href");
		return false;
	} else {
		return false;
	}
}

function closeResults() {
    document.getElementById("searchresult").style.display = "none";
}

wf.functionName_formValidation = "myCustomValidation";

function myCustomValidation (evt) {
	if (wf.formValidation(evt)) new Ajax.Updater('read', '/mt/mt-comments.cgi',{onLoading:function(request){item_loading()},onComplete:function(request){item_added()},parameters:Form.serialize(document.forms[0]), insertion:Insertion.Bottom, asynchronous:true});
	return wf.utilities.XBrowserPreventEventDefault(evt);
}

function item_added() {
	for (i=0;i<document.forms[0].elements.length;i++) {
		document.forms[0].elements[i].disabled = false;
	}
	document.forms[0].text.value = '';
	new Effect.Fade('progress');
	new Effect.Appear('newComment');
	document.getElementById('TextDisplay').innerHTML = '';
}

function item_loading() {
	for (i=0;i<document.forms[0].elements.length;i++) {
		document.forms[0].elements[i].disabled = true;
	}
	new Effect.Appear('progress');
	if (document.forms[0].bakecookie[0].checked) rememberMe(document.forms[0]);
}

function ReloadTextDiv() {
	document.getElementById('TextDisplay').innerHTML = '<p>'+document.getElementById('text').value.replace(/(\r\n|\n)/g,'<br />').replace(/(<br \/>){2,}/gi,'<'+'/p><p>')+'<'+'/p>';
}

function ReloadNameDiv() {
	document.getElementById('NameDisplay').innerHTML = document.comments_form.author.value;
}