/******************************************************/
/* File: iandevlin.js                                 */
/* Version: 0.1                                       */
/* Created: 03/01/2010                                */
/* Author: Ian Devlin (http://www.iandevlin.com)      */
/******************************************************/

/******************************************************/
/* setTitleRollOver()                                 */
/* Adds the current year to the copyright message on  */
/* the titleImage title                               */
/******************************************************/
function setTitleRollOver() {
	document.getElementById('titleImage').title += " " +  new Date().getFullYear();
}

/******************************************************/
/* goto()                                             */
/* Goes to the requested URL                          */
/******************************************************/
 function goto(url) {
 	document.location.href=url;
 }
 /******************************************************/
/* toggleDisplay()                                     */
/* Uses the JQuery timers library to toggle the        */
/* visiblity of the different infocontainers           */
/*******************************************************/
 function toggleDisplay(id) {
	var item = '#' + id;
	if ($(item).is(':hidden')) {
		if($('.formmarker').is(':visible')) $('.formmarker').slideUp('slow');
		   $(item).oneTime(1, function() { $(item).slideDown('slow'); });
	}
	else $(item).slideUp('slow');
}
/******************************************************/
/* slideUpInterval()                                  */
/* Uses the JQuery timers library to slide up the     */
/* specified div in the specified interval            */
/******************************************************/
function slideUpInterval(id,interval) {
	var item = '#' + id;
	$(item).oneTime(interval, function() { $(item).slideUp('slow'); });
}

/******************************************************/
/* AJAX  */
/******************************************************/
var xmlhttp;

/******************************************************/
/* sendEmail()                                        */
/* Parses the arguments and uses AJAX to call the PHP */
/* file that actually sends the email                 */
/******************************************************/
function sendEmail(obj) {
	var params = "";
	if (obj.elements.name != "") {
		params += (params=='')?'?':'&';
		params += "name=" + escape(obj.elements.name.value);
	}
	if (obj.elements.emailaddr != "") {
		params += (params=='')?'?':'&';
		params += "emailaddr=" + escape(obj.elements.emailaddr.value);
	}
	if (obj.elements.subject != "") {
		params += (params=='')?'?':'&';
		params += "subject=" + escape(obj.elements.subject.value);
	}
	if (obj.elements.message != "") {
		params += (params=='')?'?':'&';
		params += "message=" + escape(obj.elements.message.value);
	}
	
	if (params != "") {
		xmlhttp = GetXmlHttpObj();
		if (xmlhttp == null) {
			alert ("Your browser does not support XMLHTTP!");
			return;
		}
		var url = "sendemail.php" + params;
		xmlhttp.onreadystatechange = sendEmailStateChanged;
		xmlhttp.open("POST", url, true);
		xmlhttp.send(null);
	}
}

/******************************************************/
/* sendEmailStateChanged()                            */
/* Displays the return message from the email sending */
/******************************************************/
function sendEmailStateChanged() {
	if (xmlhttp.readyState == 4) {	
		if (xmlhttp.responseText == "ok") {
			document.getElementById("emailcontainer").innerHTML = "Thank you";
			slideUpInterval('emailbox',100);
		}
		else {
			document.getElementById("formerror").style.display = "block";
			document.getElementById("formerror").innerHTML = xmlhttp.responseText;
		}
	}
}

/******************************************************/
/* GetXMLHttpObj()                                    */
/* Returns the correct type of XML Http object for the*/
/* browser being used                                 */
/******************************************************/
function GetXmlHttpObj() {
	if (window.XMLHttpRequest) return new XMLHttpRequest();
	if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
	else return null;
}