var popupStatus = 0;

function loadPopup() {
	if (popupStatus==0) {
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn();
		$("#popupContact").fadeIn();
		popupStatus = 1;
	}
}

function disablePopup() {
	if(popupStatus == 1) {
		$("#backgroundPopup").fadeOut();
		$("#popupContact").fadeOut();
		popupStatus = 0;
	}
}


function centerPopup() {
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();

	$("#popupContact").css({
		"position": "absolute",
		"top": 100,
		"left": windowWidth/2-popupWidth/2
	});

	$("#backgroundPopup").css({
		"height": windowHeight
	});
}

function checkStatus() {
	var first_name = $('#first_name').val();
	var last_name = $('#last_name').val();
	var address = $('#address').val();
	var city = $('#city').val();
	var state = $('#state').val();
	var zip = $('#zip').val();
	var email = $('#email').val();
	var hear_about = $('#hear_about').val();

	if (email.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/) &&
		 first_name != "" && last_name != "" && address != "" && 
		 city != "" && state != "" && zip != "" && hear_about != "" && hear_about != "0") {
		document.getElementById("submit").disabled = false;
	} else {
		document.getElementById("submit").disabled = true;
	}
}

$(function() {						
	$("#button").click(function(){
		centerPopup();
		loadPopup();
	});

	$("#popupContactClose").click(function(){
		disablePopup();
	});

	$("#backgroundPopup").click(function(){
		disablePopup();
	});

	$('#first_name').blur(function() {
		var name = $(this).val();
		if (name.length > 0) {
			$('#first_name_status').hide().html('<img src="/images/silk/tick.png" width="16" height="16" alt="">').fadeIn(300);
		} else {
			$('#first_name_status').hide().html('<img src="/images/silk/cross.png" width="16" height="16" alt="">').fadeIn(300);
		}
		checkStatus();
	});

	$('#last_name').blur(function() {
		var last_name = $(this).val();
		if (last_name.length > 0) {
			$('#last_name_status').hide().html('<img src="/images/silk/tick.png" width="16" height="16" alt="">').fadeIn(300);
		} else {
			$('#last_name_status').hide().html('<img src="/images/silk/cross.png" width="16" height="16" alt="">').fadeIn(300);
		}
		checkStatus();
	});

	$('#address').blur(function() {
		var address = $(this).val();
		if (address.length > 0) {
			$('#address_status').hide().html('<img src="/images/silk/tick.png" width="16" height="16" alt="">').fadeIn(300);
		} else {
			$('#address_status').hide().html('<img src="/images/silk/cross.png" width="16" height="16" alt="">').fadeIn(300);
		}
		checkStatus();
	});

	$('#city').blur(function() {
		var city = $(this).val();
		if (city.length > 0) {
			$('#city_status').hide().html('<img src="/images/silk/tick.png" width="16" height="16" alt="">').fadeIn(300);
		} else {
			$('#city_status').hide().html('<img src="/images/silk/cross.png" width="16" height="16" alt="">').fadeIn(300);
		}
		checkStatus();
	});

	$('#state').blur(function() {
		var state = $(this).val();
		if (state.length > 0) {
			$('#state_status').hide().html('<img src="/images/silk/tick.png" width="16" height="16" alt="">').fadeIn(300);
		} else {
			$('#state_status').hide().html('<img src="/images/silk/cross.png" width="16" height="16" alt="">').fadeIn(300);
		}
		checkStatus();
	});

	$('#zip').blur(function() {
		var zip = $(this).val();
		if (zip.length > 0) {
			$('#zip_status').hide().html('<img src="/images/silk/tick.png" width="16" height="16" alt="">').fadeIn(300);
		} else {
			$('#zip_status').hide().html('<img src="/images/silk/cross.png" width="16" height="16" alt="">').fadeIn(300);
		}
		checkStatus();
	});

	$('#email').blur(function() {
		var email = $(this).val();
		var pattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (email.match(pattern)) {
			$('#email_status').hide().html('<img src="/images/silk/tick.png" width="16" height="16" alt="">').fadeIn(300);
		} else {
			$('#email_status').hide().html('<img src="/images/silk/cross.png" width="16" height="16" alt="">').fadeIn(300);
		}
		checkStatus();
	});

	$('#hear_about').blur(function() {
		var hear_about = $(this).val();
		if (hear_about.length > 0) {
			$('#hear_status').hide().html('<img src="/images/silk/tick.png" width="16" height="16" alt="">').fadeIn(300);
		} else {
			$('#hear_status').hide().html('<img src="/images/silk/cross.png" width="16" height="16" alt="">').fadeIn(300);
		}
		checkStatus();
	});

	$('#submit').click(function() {
		var first_name = $('#first_name').val();
		var last_name = $('#last_name').val();
		var address = $('#address').val();
		var city = $('#city').val();
		var state = $('#state').val();
		var zip = $('#zip').val();
		var email = $('#email').val();
		var hear_about = $('#hear_about').val();
		$.ajax ({
				url: '/contest/notify.php',
				type: 'POST',
				data: 'first_name=' + first_name + '&last_name=' + last_name + '&email=' + email  + 
				       '&address=' + address + '&city=' + city +
					   '&state=' + state + '&zip=' + zip + '&hear_about=' + hear_about,
				success: function(results) {
					$('#response').remove();
					$('#contactform').append('<p id="response">' + results + '</p>');
					$('#loading').fadeOut(500, function() {
						$(this).remove();
					});
					return false;
				}
		});
		setTimeout('2500');
		disablePopup();
		return false;
	});
});
