jQuery(document).ready(function(){  

	jQuery("#curYear").change(function () { 
		jQuery('#curDay').attr('disabled', true);
		jQuery('#curMonth').attr('disabled', false);
		
		updateMonth();
	});
	
	jQuery("#curMonth").change(function () { 
		jQuery('#curDay').attr('disabled', false);
		
		updateDay();
	});	

	jQuery("#curDay").change(function () { 									   
		updateTopTenContentByDate();
	});	
	
	jQuery("span#submitBtn img").click(function () { 
		var formStatus = checkForm();
		if (formStatus)
		{
			//alert(jQuery("form#contactForm").serialize())
			jQuery.ajax({
			type: "POST",
			url: "send_top_ten.php",
			data: jQuery("form#contactForm").serialize(),
				success: function(data){
					
					if (data.match('success'))
					{
						jQuery('div#post_contest').html('Thank you for playing.');
						jQuery('div#post_contest').css('font-weight','bold');
					}
					else
					{
						alert('We are unable to complete your request.');
					}
					
				},								
				error:function (xhr, ajaxOptions, thrownError){
					errorStatus = xhr.status;
					
					if (errorStatus == '404')
					{
						alert('The PHP script on the server has been moved.');
					}
				}   				
			});
		}		
	});
});  


function checkForm() {
	var form = document.forms.contactForm;
	if (form.email.value == 'Email' || form.email.value == '') {
		alert("Please enter your email address.");
		document.contactForm.email.focus();
		return false;
	}
	var validU = document.forms.contactForm.email.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
	if (!validU) {
		alert("Please enter a valid email address.");
		return false;
	}

	if (form.first_name.value == 'First Name' || form.first_name.value == '') {
		alert("Please enter your first name.");
		document.contactForm.firstname.focus();
		return false;
	}
	if (form.last_name.value == 'Last Name' || form.last_name.value == '') {
		alert("Please enter your last name.");
		document.contactForm.last_name.focus();
		return false;
	}
	if (form.city.value == 'Hometown' || form.city.value == '') {
		alert("Please enter your city.");
		document.contactForm.city.focus();
		return false;
	}
	if (form.question.value == '') {
		alert("Please enter your Top Ten.");
		document.contactForm.question.focus();
		return false;
	}
	if (form.disclaimer.checked == false) {
		alert("Please agree to Terms and Conditions.");
		document.contactForm.disclaimer.focus();
		return false;
	}
	
	return true;
}


function updateMonth()
{
	//console.log('updateMonth func');
	
	var y = jQuery('#curYear').val()
	//console.log(y)
	
	jQuery.ajax({
		type: "POST",
		url: "top_ten_update_month.php",
		data: "year=" + y,
		success: function(data){			
			////console.log(data)
			jQuery('#curMonth').html(data);
		}
	});
}

function updateDay()
{
	//console.log('updateDay func');

	var y = jQuery('#curYear').val()
	var m = jQuery('#curMonth').val()

	//console.log(y)
	//console.log(m)
	
	jQuery.ajax({
		type: "POST",
		url: "top_ten_update_day.php",
		data: "year=" + y + "&month=" + m,
		success: function(data){			
			////console.log(data)
			jQuery('#curDay').html(data);
			
		}
	});
}

function updateTopTenContentByDate()
{
	//console.log('updateTopTenByDate func');

	var y = jQuery('#curYear').val()
	var m = jQuery('#curMonth').val()
	var d = jQuery('#curDay').val()

	//console.log(y)
	//console.log(m)
	//console.log(d)
	
	jQuery.ajax({
		type: "POST",
		url: "top_ten_update_by_date.php",
		data: "year=" + y + "&month=" + m + "&day=" + d,
		success: function(data){			
			////console.log(data)
			jQuery('#top_ten').html(data);
			
			jQuery("#curYear").change(function () { 
				jQuery('#curDay').attr('disabled', true);
				
				updateMonth();
			});
			
			jQuery("#curMonth").change(function () { 
				jQuery('#curDay').attr('disabled', false);
				
				updateDay();
			});	
		
			jQuery("#curDay").change(function () { 									   
				updateTopTenContentByDate();
			});	
			
		}
	});
}



function topTen(y,m,i,c)
{
	////console.log('prevTopTen func');
	
	jQuery.ajax({
		type: "POST",
		url: "top_ten_paginate.php",
		data: "year=" + y + "&month=" + m + "&elm=" + i + "&cat=" + c,
		success: function(data){			
			////console.log(data)
			jQuery('div#top_ten').html(data);
		}
	});
	
	return false;
}

/* open window script */
function openWin(url,w,h,winName,scroll,resize) {
	if(winName==''){winName='CbsWindow';}if(scroll==''){scroll='yes';}
	window.open(url,winName,"width="+w+",height="+h+",status=0,scrollbars="+scroll+",resizable="+resize+",location=0,menubar=0").focus();
} 