var loginActive = 0;
var searchActive = 1;
var COOKIE_NAME = 'cookie';
var options = { path: '/', expires: 3 }; 

$(function() {
	
	//Contact Form
	$('.error').hide();
	$("input#name").select().focus();
	$("#submit_btn").click(function() {
		sendEmail();
	});
	
	randHeader();

});

function randHeader(){
	//var max = 6;
	//$("#header").css({backgroundImage : "url(/assets/images/header-bg-"+Math.floor(Math.random()*max+1)+".jpg)"});
}

function showMore(id) {
	$(".newscontent-summary-" + id).toggle();
	$(".newscontent-full-" + id).toggle();
}

function hideStage() {
	$("#member-login").fadeOut('fast');
	$("#site-search").fadeOut('fast');
}

function isValidEmailAddress(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}

function sendEmail() {
	
$('.error').hide();
	
var name = $("input#name").val();

	if (name == "") {
		$("label#name_error").show();
		$("input#name").focus();
		return false;
	}

var email = $("input#email").val();

	if(isValidEmailAddress(email)) {

	} else {
		$("label#email_error").show();
		$("input#email").focus();
		return false;
	}
	
var message = $("input#message").val();

	if (message == "") {
		$("label#message_error").show();
		$("input#message").focus();
		return false;
	}
	
var dataString = 'name='+ name + '&email=' + email + '&message=' + message;
//alert (dataString);return false;

$.ajax({
	type: "POST",
	url: "includes/process.php",
	data: dataString,
	success: function() {
	$('#contact_form').html("<div id='message'></div>");
	$('#message').html("<h2 class='grey'>Thank you!</h2>")
	.hide()
	.fadeIn(500);
	}
});

return false;
    
}

function disablePage(id) {
	$.get("disable.php", {id: id},
		 function(data){
		 	if(data == 'Page disabled') {
		 		$('#enable-btn').html("Disabled");
		 	} else {
		 		$('#enable-btn').html("Enabled");
		 	}
		 	
	});
}

function submitPoll() {
	if($.cookie(COOKIE_NAME)!='poll_cookie') {
		$('#poll_submit_btn').attr("disabled", true);
	
		var pollOption = $("input[name='poll-option']:checked").val();
		var pollId = $("input#poll-id").val();
		
		$.post("/mod/poll-insert.php", {pollId: pollId, pollOption: pollOption},
		 function(data){
			$('#home-poll').html("<div id='message'></div>");
			$('#message').html("<h2 class='grey'>Thank you!</h2>")
			.hide()
			.fadeIn(500);
		});
		
		//set cookie
		$.cookie(COOKIE_NAME, 'poll_cookie', options);
	} else {
			$('#home-poll').html("<div id='message'></div>");
			$('#message').html("<h2 class='grey'>You have voted already!</h2>")
			.hide()
			.fadeIn(500);
	}
}
