$(document).ready(function(){

	$('div#tb-contact-form').hide();
	$('div#tb-warm-streets').hide();

	$("a.contact-form").each(function(i){
		$(this).attr('href', '#TB_inline?height=360&width=400&inlineId=tb-contact-form');
		$(this).attr('title', 'Contact us');
	});

	$("a.warm-streets").each(function(i){
		$(this).attr('href', '#TB_inline?height=360&width=400&inlineId=tb-warm-streets');
		$(this).attr('title', 'Warm streets');
	});

	$("a.whos-this").each(function(i){
		$(this).attr('href', '#TB_inline?height=300&width=400&inlineId=tb-whos-this');
		$(this).attr('title', 'Who\'s this?');
	});

	$("div#interactive-house a").click(function(){
		$('div#tips div.tip').hide().removeClass('visible');
		$('div#tips div.' + $(this).attr('class')).fadeIn().addClass('visible');
	});

	$("div#tips a.next-tip").click(function(){
		showNextTip();
		return false;
	});

});

var showNextTip = function() {
	// hide
	$('div#tips div.visible').hide().removeClass('visible').next().fadeIn().addClass('visible');
	//$('div#tips div.' + $(this).attr('class')).fadeIn();
}

var ajaxContact = function() {
	if ($('input#ContactCName').val() == '') {
		$("div#status-message-contact").text('Please enter your name');
		$("div#status-message-contact").addClass('error');
	} else if ($('input#ContactCEmail').val() == '' && $('input#ContactCTelephone').val() == '') {
		$("div#status-message-contact").text('Please enter your email address or phone number');
		$("div#status-message-contact").addClass('error');
	} else {
		var str = $("form#ContactEnergyAdviceContactForm").serialize();
		$.post("/contacts/energy_advice_contact/", { name: $('input#ContactCName').val(), email: $('input#ContactCEmail').val(), telephone: $('input#ContactCTelephone').val() } );
		$('input#NewsSignupEmailContact').val($('input#ContactCEmail').val());
		$("div#status-message-contact").text('');
		$('div#contact-step1').hide();
		$('div#contact-step2').fadeIn();
	}
	return false;
}

var ajaxSignUpContact = function() {
	if (echeck($('input#NewsSignupEmailContact').val())) {
		$("div#status-message-contact").text('Adding your email address...');
		$.get("/news/signup/" + $("input#NewsSignupEmailContact").val(), function(data){
			$('div#status-message-contact').text('Thanks, your email address has been added!');
			$("div#status-message-contact").removeClass('error');
			$("div#status-message-contact").addClass('success');
			$("p.no-thanks").hide();
		});
	} else {
		$("div#status-message-contact").text('Please enter a valid email address');
		$("div#status-message-contact").addClass('error');
	}
	return false;
}

var ajaxWarmStreets = function() {
	if ($('input#ContactWsName').val() == '') {
		$("div#status-message-warm-streets").text('Please enter your name');
		$("div#status-message-warm-streets").addClass('error');
	} else if ($('input#ContactWsEmail').val() == '' && $('input#ContactWsTelephone').val() == '') {
		$("div#status-message-warm-streets").text('Please enter your email address or phone number');
		$("div#status-message-warm-streets").addClass('error');
	} else {
		var str = $("form#ContactEnergyAdviceContactForm").serialize();
		$.post("/contacts/energy_advice_warm_streets/", { name: $('input#ContactWsName').val(), email: $('input#ContactWsEmail').val(), telephone: $('input#ContactWsTelephone').val() } );
		$('input#NewsSignupEmailWarmStreets').val($('input#ContactWsEmail').val());
		$("div#status-message-warm-streets").text('');
		$('div#warm-streets-step1').hide();
		$('div#warm-streets-step2').fadeIn();
	}
	return false;
}

var ajaxSignUpWarmStreets = function() {
	if (echeck($('input#NewsSignupEmailWarmStreets').val())) {
		$("div#status-message-warm-streets").text('Adding your email address...');
		$.get("/news/signup/" + $("input#NewsSignupEmailWarmStreets").val(), function(data){
			$('div#status-message-warm-streets').text('Thanks, your email address has been added!');
			$("div#status-message-warm-streets").removeClass('error');
			$("div#status-message-warm-streets").addClass('success');
			$("p.no-thanks").hide();
		});
	} else {
		$("div#status-message-warm-streets").text('Please enter a valid email address');
		$("div#status-message-warm-streets").addClass('error');
	}
	return false;
}


function echeck(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    return false
	 }
	
	 if (str.indexOf(" ")!=-1){
	    return false
	 }

	 return true					
}


