/*	

	Dusted Design Partners Limited

*/
function checkEmail(email)
	{	
		var pattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var emailVal = $("#" + email).val();
		return pattern.test(emailVal);
	}
	$(document).ready(function(){
$("#signupForm #sign-up").click(function() {	

			// First, disable the form from submitting
			$('#signupForm').submit(function() { return false; });
			
			// Grab form action
			formAction = $(this).parents('form').attr("action");
			
			// Hack together id for email field
			emailId = formAction.replace("http://ebull.dusteddesign.com/t/r/s/", "");
			emailId = emailId.replace("/", "");
			emailId = emailId + "-" + emailId;

			emailChk = true;
			
			// Validate email address with regex
			if (!checkEmail(emailId)) 
			{
				
				$("#confirmation").slideDown("slow");  // Shows "Thanks for subscribing" div
				$("#confirmation").addClass("panel");
				$("#confirmation").html("Please enter a valid email address");
				return false;
			}
			
			// Serialize form values to be submitted with POST
			var str = $(this).parents('form').serialize();
	
			// Add form action to end of serialized data
			final = str + "&action=" + formAction;
			
			// Submit the form via ajax
			$.ajax({
				url: "http://www.the-mermaid.co.uk/proxy.php",
				type: "POST",
				data: final,
				success: function(html){
					$("#confirmation").slideDown("slow");  // Shows "Thanks for subscribing" div
					$("#confirmation").addClass("panel");
					$("#confirmation").html("Thanks for subscribing");
				}
			});
			return false;
	});

});
/* BROWSER SNIFFER */
var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;

if (checkIt('konqueror'))
{
        browser = "Konqueror";
        OS = "Linux";
}
else if (checkIt('safari')) browser = "Safari"
else if (checkIt('omniweb')) browser = "OmniWeb"
else if (checkIt('opera')) browser = "Opera"
else if (checkIt('webtv')) browser = "WebTV";
else if (checkIt('icab')) browser = "iCab"
else if (checkIt('msie')) browser = "Internet Explorer"
else if (!checkIt('compatible'))
{
        browser = "Mozilla"
        version = detect.charAt(8);
}
else browser = "An unknown browser";

if (!version) version = detect.charAt(place + thestring.length);

if (!OS)
{
        if (checkIt('linux')) OS = "Linux";
        else if (checkIt('x11')) OS = "Unix";
        else if (checkIt('mac')) OS = "Mac"
        else if (checkIt('win')) OS = "Windows"
        else OS = "an unknown operating system";
}

function checkIt(string)
{
        place = detect.indexOf(string) + 1;
        thestring = string;
        return place;
}

printLink = function() {
	if (document.getElementById("footer-print")) {
		var printBut = document.getElementById("footer-print");
		printBut.onclick=function() {
			if (window.print) {
				window.print();
			} else {
				alert("Unfortunately your browser doesn't support printing via Javascript.\nTo print this page please select \"File > Print...\" in your browser's menu.");
			}
			return false;
		}
	}
}

  // this function is needed to work around 
  // a bug in IE related to element attributes
function hasClass(obj) {
	var result = false;
	if (obj.getAttributeNode("class") != null) {
		result = obj.getAttributeNode("class").value;
	}
	return result;
}   

function stripe(id) {
	var even = false;
	var evenColor = arguments[1] ? arguments[1] : "#fff";
	var oddColor = arguments[2] ? arguments[2] : "#eee";
	var statList = document.getElementById(id);
	
	if (! statList) { return; }
	
	var lis = statList.getElementsByTagName("li");
	
	for (var h = 0; h < lis.length; h++) {
		var myli = lis[h]
		if (! hasClass(myli) && ! myli.style.backgroundColor) {
			myli.style.backgroundColor = even ? evenColor : oddColor;
		}
		even =  ! even;
	}
}

/*
mooFx = function(){
	if (document.getElementById("organise-list")) {
		myHeight = new fx.Height('organise-list', {duration: 400});
		myHeight.hide();
		if (document.getElementById("toggle")) {
			var toggleBut = document.getElementById("toggle");
			toggleBut.onclick=function() {
				myHeight.toggle();
				return false;
			}
		}
	}
}
*/
backgroundFull = function(){
	var pageY,portY;

	if (self.innerHeight) // all except Explorer
	{
		portY = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		portY = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		portY = document.body.clientHeight;
	}

	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight
	if (test1 > test2) // all but Explorer Mac
	{
		pageY = document.body.scrollHeight;
	}
	else // Explorer Mac;
	     //would also work in Explorer 6 Strict, Mozilla and Safari
	{
		pageY = document.body.offsetHeight;
	}
	if (pageY<=portY) {
		document.body.style.height = "100%";
	}
}
function showFields(whichLayer) {
	if (document.getElementById)
	{
	// this is the way the standards work
		document.getElementById(whichLayer).className = "formrow clearfix";
	}
	else if (document.all)
	{
	// this is the way old msie versions work
		document.all[whichLayer].className = "formrow clearfix";
	}
	else if (document.layers)
	{
	// this is the way nn4 works
		document.layers[whichLayer].className = "formrow clearfix";
	}
}
function hideFields(whichLayer) {
	if (document.getElementById)
	{
	// this is the way the standards work
		document.getElementById(whichLayer).className = "remove";
	}
	else if (document.all)
	{
	// this is the way old msie versions work
		document.all[whichLayer].className = "remove";
	}
	else if (document.layers)
	{
	// this is the way nn4 works
		document.layers[whichLayer].className = "remove";
	}
}
toggleField = function() {
	if (document.getElementById("category")) {
		hideFields("others");

		var natureSel = document.getElementById("category");
		natureSel.onchange=function() {
			if (this.value == 29) {
				showFields("others");
			} else {
				hideFields("others");
			}
		}
	}
}
/* FOOTER MANAGER */
function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	}
	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		}
		else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}
function setFooter() {
	if (browser != "Internet Explorer" || OS !="Mac" && OS !="an unknown operating system") {
		if (document.getElementById) {
			var windowHeight = getWindowHeight();
			if (windowHeight > 0) {
				var contentHeight = document.getElementById('container').offsetHeight;
				var footerElement = document.getElementById('footer');
				var footerHeight  = footerElement.offsetHeight+1;
				if (windowHeight - (contentHeight + footerHeight) >= 0) {
					footerElement.style.top = (windowHeight - (contentHeight + footerHeight)) + 'px';
				}
				else {
					footerElement.style.top = '0px';
				}
			}
		}
	}
}
/* backgroundFull = function(){
	var x,y;
	if (self.pageYOffset) // all except Explorer
	{
		x = self.pageXOffset;
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	alert(self.pageYOffset);
	if (y==0) {
		document.body.style.height = "100%";
	}
}
*/


window.onload = function() {
	//mooFx();
	setFooter();toggleField();printLink();stripe('organise-list');backgroundFull();
}
window.onresize = function() {
	setFooter();
}