function supports_canvas() {
  return !!document.createElement('canvas').getContext;
}


if (supports_canvas()){
	Cufon.now();
}


//if ($(".box").length > 0){
	$(".box").hover(function(){
		$(this).addClass("alt");
		if (supports_canvas()){
			Cufon.refresh();
		}
	}, function(){
		$(this).removeClass("alt");
		if (supports_canvas()){
			Cufon.refresh();
		}
	});
//}



//if ($(".inside a img").length > 0){
	$(".inside a img").hover(function(){
		$(this).animate(
		{ 'opacity':'0.7' }
		);
		}, function(){
		$(this).animate(
		{ 'opacity':'1' }
		);
	})
//}


//if ($(".linkSpan").length > 0){
	$(".linkSpan").hover(function(){
		$(this).parent().siblings('div.box').mouseover();
	}, function(){
		$(this).parent().siblings('div.box').mouseout();
	});
	$(".inside a img").hover(function(){
		$(this).animate(
			{ 'opacity':'0.7' }
		);
	}, function(){
		$(this).animate(
			{ 'opacity':'1' }
		);
	});
//}


//if ($('div.slider').length){
	// show the slideshow
	$('div.slider').css('visibility','visible').hide().fadeIn('slow');
//}
var formActive = 0;
//if ($('a.blog').length > 0){
	$('a.blog').toggle(
		function(){
		formActive = 303;
			$("body").scrollTop(0);
			$('#online-enquiry').animate(
				{ 'margin-top':'0px' },	
				500
			)
		},
		function(){
			formActive = 0;
			$('#online-enquiry').animate(
				{ 'margin-top':'-303px' },	
				500
			)
		}
	);
//}

//if ($('input#submit').length > 0){
	$('input#submit')
	.mouseover(function(){
		$(this).fadeTo('fast','0.7');		
	})
	.mouseout(function(){
		$(this).fadeTo('fast','1');	
	});
//}


//if ($('.insideul').length > 0){
	accordian('insideul');
//}

function accordian(listName) {

	$('.' + listName + ' ul > li')
	.hide()

	$('.' + listName + ' li')
	.click(function(e) {
	e.stopPropagation()

	$('.' + listName + ' ul > li').filter(':visible')
	.slideToggle();

	$('.' + listName + ' li img')
	.attr('src', 'images/bullet.png')
	.css({'right':'20px','top':'2px'});

	if($(this).find('ul li:first').is(':hidden')){

			$(this).find('ul li:first').slideToggle();
			
			$(this).find('img:first')
			.attr('src', 'images/BulletRotated.png')
			.css({'right:28px':'25px','top':'5px'});
			
	}
	
			
	})
				
}

setupForm();

/** setup forms  ***********************/

function setupForm(){
	$('form.enquiry-form').submit(function(evt) {

		evt.preventDefault();
		processForm($(this));
	});
};

/** process forms  ***********************/

function processForm(that){

	//alert("made it here");

	var fullname = that.find('input[name=name]').attr('value');
	var email = that.find('input[name=email]').attr('value');
	if (checkNotEmpty(email)){
		
		var atpos=email.indexOf("@");
		var dotpos=email.lastIndexOf(".");
		if (atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length){
		  alert("Not a valid e-mail address!\n Please remove it or enter a new one.");
		  return false;
		}
	}
	var address = that.find('input[name=address]').attr('value');
	var pcode = that.find('input[name=postcode]').attr('value');
	var phone = that.find('input[name=telephone]').attr('value');
	var interest = that.find('input[name=interest]:checked').attr('value');
	var txtmessage = that.find('textarea[name=message]').attr('value');

	if (mandatoryFields(fullname,phone,email)){

		//$('.goform').html('<img src="images/loader.gif" alt="" />');

		that.find('input[name=name]').attr('value','');
		that.find('input[name=email]').attr('value','');
		that.find('input[name=address]').attr('value','');
		that.find('input[name=telephone]').attr('value','');
		that.find('input[name=postcode]').attr('value','');
		that.find('input[name=interest]').attr('checked',false);
		that.find('textarea[name=message]').attr('value','');

		var myRequest = getXMLHTTPRequest();

		url = "mail/mail.php?";
		url += "name="+fullname;
		url += "&email="+email;
		url += "&phone="+phone;
		url += "&pcode="+pcode;
		url += "&address="+address;
		url += "&interest="+interest;
		url += "&txtmessage="+txtmessage;

		url = url.toLowerCase();

		myRequest.open("GET",url,true);

		myRequest.onreadystatechange = function(){

			if (myRequest.readyState == 4){

				if (myRequest.status == 200){

					//$('#goform').html('<input id="submit" type="submit" value="SUBMIT" />');
					//alert(myRequest.responseText);
					$('a.blog').click();

				} else {

					alert("an error has occured: "+myRequest.statusText);

				}
			}

		}

		myRequest.send(null);
	
	} else {

	alert('Please ensure you have entered your name & a method of contact!');

	}

};

function checkNotEmpty(element){

	if (element==null || element==""){
		return false;
	}
	return true;
};

function mandatoryFields(fullName,phone,email){

	if (!checkNotEmpty(fullName) ||
		(!checkNotEmpty(phone) &&
		!checkNotEmpty(email))){
		return false;
	}
	return true;
};


function getXMLHTTPRequest(){
	var req = false;
	try{
		req = new XMLHttpRequest(); /* firefox */
	}
	catch(err1){
		try{
			req = new ActiveXObject("Msxm12.XMLHTTP"); /* some IE */
		}
		catch(err2){
			try{
				req = new ActiveXObject("Microsoft.XMLHTTP"); /* some IE */
			}
			catch(err3){
				req = false;
			}
		}
	}
return req;
};



