

var lastUpdate;
var theRequest;
var requestTimer;
var requestBusy = false;
var firstInput = true;

include('/library/enlarge/scripts.js');

function initLightbox () { }
function showLightbox () { return true; }

function include (theUrl) {
	try {
		var theScript = document.createElement('script');
		var theHead = document.getElementsByTagName('head').item(0);
		theScript.src = theUrl;
		theHead.appendChild(theScript);
	} catch(e) { }
}

function addMarkup (theUrl) {

	theWidth = 340;
	theHeight = 200;
	leftCoord = (screen.width/2) - (theWidth/2);
	topCoord  = (screen.height/2) - (theHeight/2) - 20;

	markupWindow = window.open(theUrl,"markupwindow","width="+theWidth+
	",height="+theHeight+",top="+topCoord+",left="+leftCoord+
	",menubar=no,toolbar=no,location=no,status=no,directories=no,resizable,scrollbars=no");
	markupWindow.focus();

	return false;

}

function contactUser (theUrl) {

	theWidth = 340;
	theHeight = 440;
	leftCoord = (screen.width/2) - (theWidth/2);
	topCoord  = (screen.height/2) - (theHeight/2) - 20;

	contactWindow = window.open(theUrl,"contactwindow","width="+theWidth+
	",height="+theHeight+",top="+topCoord+",left="+leftCoord+
	",menubar=no,toolbar=no,location=no,status=no,directories=no,resizable,scrollbars=no");
	contactWindow.focus();

	return false;

}

function checkPreview () {
	theField = document.getElementById('formtext');
	if (lastUpdate != theField.value) {
		lastUpdate = theField.value;
		livePreview();
	}
}

function livePreview () {
	if (requestBusy == false) {
		window.clearTimeout(requestTimer);
		getPreview();
	} else {
		window.clearTimeout(requestTimer);
		requestTimer = window.setTimeout('livePreview()',1000);
	}
}

function fullEscape (theString) {
	theString = escape(theString);
	theString = theString.replace('+','%2B');
	return theString;
}

function getPreview () {
	requestBusy = true;

	// plus signs get lost here, possibly others

	theUrl = document.getElementById('comment').action;
	theData  = 'cmd=commentpreview';
	theData += '&formname=' + fullEscape(document.getElementById('formname').value);
	if (document.getElementById('formemail'))
		theData += '&formemail=' + fullEscape(document.getElementById('formemail').value);
	if (document.getElementById('formsite'))
		theData += '&formsite=' + fullEscape(document.getElementById('formsite').value);
	theData += '&formtext=' + fullEscape(document.getElementById('formtext').value);
	if (document.getElementById('alternate')) 
		theData += '&alternate=' + fullEscape(document.getElementById('alternate').value);

	if (theRequest) {
		theRequest.abort();
	} else {
		theRequest = false;
		if (window.XMLHttpRequest) { // branch for native XMLHttpRequest object
			try {
				theRequest = new XMLHttpRequest();
			} catch(e) {
				theRequest = false;
			}
		} else if (window.ActiveXObject) { // IE/Windows ActiveX version
			try {
				theRequest = new ActiveXObject('Msxml2.XMLHTTP');
			} catch(e) {
				try {
					theRequest = new ActiveXObject('Microsoft.XMLHTTP');
				} catch(e) {
					theRequest = false;
				}
			}
		}
	}
	if (theRequest) {
		theRequest.onreadystatechange = updatePreview;
		theRequest.open('POST', theUrl, true);
		theRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		theRequest.send(theData);
	} // else silently ignore unsupported browsers
}

function updatePreview () {
	if (theRequest.readyState == 4 && theRequest.status == 200) {
		if (theRequest.responseText && theRequest.responseText != "\n") {
			requestBusy = false;
			if (document.getElementById('commentpreviewarea')) {
				document.getElementById('commentpreviewarea').innerHTML = theRequest.responseText;
				if (firstInput == true) {
					firstInput = false;
					document.getElementById('commentpreviewarea').style.display = 'block';
					// scroll the form and preview area into full view
					if (!document.getElementById('addcomment').scrollIntoView(true)) {
						var scrollX = 0;
						var scrollY = 0;
						var theObject = document.getElementById('addcomment');
						do {
							scrollX += theObject.offsetLeft;
							scrollY += theObject.offsetTop;
						}
						while ((theObject = theObject.offsetParent));
						window.scrollTo(scrollX,scrollY);
					}
				}
			}
		}
	} // else silently ignore errors
}

function rememberMe (cookieDomain) {

	if (document.getElementById('formremember').value == 'true') {
		document.getElementById('formremember').value = '';
		if (document.getElementById('formname')) document.getElementById('formname').value = '';
		if (document.getElementById('formemail')) document.getElementById('formemail').value = '';
		if (document.getElementById('formsite')) document.getElementById('formsite').value = 'http://';
		setCookie('logname','',-1,'/',cookieDomain,0);
		setCookie('logemail','',-1,'/',cookieDomain,0);
		setCookie('logsite','',-1,'/',cookieDomain,0);
		document.getElementById('remembermessage').innerHTML = 'Information forgotten. ';
		document.getElementById('rememberlink').innerHTML = 'Save personal information?';
	} else {
		document.getElementById('formremember').value = 'true'
		document.getElementById('remembermessage').innerHTML = 'Personal information will be saved. ';
		document.getElementById('rememberlink').innerHTML = 'Forget personal information?';
	}
	return false;

}

function setCookie (name,value,days,path,domain,secure) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = '; expires='+date.toGMTString();
	} else expires = '';
	document.cookie = name+'='+value+expires+'; path='+path+'; domain='+domain;
}

function inputKeypress (e) {
	var keycode = false;
	if (e == null) {
		keycode = event.keyCode;
	} else keycode = e.which;
	if (keycode == 13) this.form.submit();
}

function initPage () {

	initLightbox();

	var theInputs = document.getElementsByTagName('input');
	var inputCount = theInputs.length;
	for (x = 1; x < inputCount; x++) {
		if (theInputs[x].type == 'text' || theInputs[x].type == 'password') 
			theInputs[x].onkeypress = inputKeypress;
	}

	if (document.getElementById('formtext')) {

		lastUpdate = document.getElementById('formtext').value;

		document.getElementById('formtext').onkeyup = livePreview;
		document.getElementById('formtext').onchange = livePreview;
		document.getElementById('formtext').onfocus = checkPreview;
		document.body.onfocus = checkPreview;

		if (document.getElementById('formname')) {
			document.getElementById('formname').onkeyup = livePreview;
			document.getElementById('formname').onchange = livePreview;
		}
		if (document.getElementById('formemail')) {
			document.getElementById('formemail').onkeyup = livePreview;
			document.getElementById('formemail').onchange = livePreview;
		}
		if (document.getElementById('formsite')) {
			document.getElementById('formsite').onkeyup = livePreview;
			document.getElementById('formsite').onchange = livePreview;
		}

		if (document.getElementById('formtext').value) livePreview();
	}

}