$(function() {
	$("input.inputdate").datepick({dateFormat: 'mm/dd/yy', yearRange: '1900:+0'});
});

$(document).ready(function() {
	//$("input:checkbox").attr("checked",false);
	//$("#vote_pop,#vote_judge,#vote_mix").attr("checked",true);

	$("#zip").typeWatch( { highlight:false, wait:350, callback:checkValidZip, captureLength:4 } );
	$("#postal").typeWatch( { highlight:false, wait:350, callback:checkValidPostal, captureLength:4 } );
	$("#email").typeWatch( { highlight:false, wait:350, callback:checkValidEmail, captureLength:4 } );
	$("#captext").typeWatch( { highlight:false, wait:350, callback:checkValidCap, captureLength:5 } );
	
	$("#zip").keypress(function (e)
	{
	  //if the letter is not digit then display error and don't type anything
	  if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57))
	  {
		if ($("#zip").val().length < 5) {
		//display error message
		$("#zip").addClass('invalid');$("#zip").removeClass('valid');
		var boxlocation = $("#zip").offset();
		var newleft = Math.ceil(boxlocation.left + 230);
		var newtop = Math.ceil(boxlocation.top + 2);
		$("#invalidziptag").css("left",newleft);
		$("#invalidziptag").css("top",newtop);
		$("#invalidziptag").fadeIn("fast");
		return false;
		}
	  } else {
		if ($("#zip").val().length < 6) {
			$("#zip").removeClass('valid');
			$("#locationstring").hide();
		}
		$("#zip").removeClass('invalid');
		$("#invalidziptag").fadeOut("fast");
		}
	});
	
	$('textarea[maxlength]').keyup(function(){  
        //get the limit from maxlength attribute  
        var limit = parseInt($(this).attr('maxlength'));  
        //get the current text inside the textarea  
        var text = $(this).val();  
        //count the number of characters in the text  
        var chars = text.length;  
		// update counter
		$("#textcount").html(limit - text.length);
  
        //check if there are more characters then allowed  
        if(chars > limit){  
            //and if there are use substr to get the text before the limit  
            var new_text = text.substr(0, limit);  
  
            //and change the current text with the new text  
            $(this).val(new_text);  
        }  
    }); 
	
	$("input[name='locations']").click(function() {
		var whatloc = $(this).val();
		$("div.locationfields").hide();
		$("#location"+whatloc).show();
	});
	
	$("span.clickable").click(function() {
		var current = $(this).prev("input").attr("checked");
		if (current == true) {
			$(this).prev("input").attr("checked",false);
			$(this).css("color","#777");
		} else {
			$(this).prev("input").attr("checked",true);
			$(this).css("color","#000");
		}
	});
	
	$("input:checkbox").click(function() {
		var cur = $(this).attr("checked");
		if (cur == true) {
			$(this).next("span").css("color","#000");
		} else {
			$(this).next("span").css("color","#777");
		}
	});
	
	$("input:radio").click(function() {
		var cur = $(this).attr("checked");
		var par = $(this).parent("div").attr("id");
		$("#"+par+" > span").css("color","#777");
		if (cur == true) {
			$(this).next("span").css("color","#000");
		}
	});
	
	$("span.clickableradio").click(function() {
		var par = $(this).parent("div").attr("id");
		var current = $(this).prev("input").attr("checked");
		if (current != true) {
			$("#"+par+" > span").css("color","#777");
			$(this).prev("input").attr("checked",true);
			$(this).css("color","#000");
		}
	});
	
	$("#password,#password2").change(function() {
		var firstpass = $("#password").val();
		var secondpass = $("#password2").val();
		
		if (firstpass != secondpass) {
			$("#password").addClass("invalid");
			$("#password2").addClass("invalid");
		} else {
			$("#password").removeClass("invalid").addClass("valid");
			$("#password2").removeClass("invalid").addClass("valid");
			$("#passerror").hide();
		}
	});
	
	$("#capimg").click(function() {
		$("#capimg").attr("src","inc/securimage_show.php");
	});
	
	$("#title").liveSearch({url: '/searchsubmit.php?st=', id: 'subsearchresults', inputid: '#title', sdiv: 'div.subsearch', posleft: 0});
});

function checkValidZip() {
	var zipcode = $("#zip").val();
	if ($("#zip").val().length == 5) {
		$.post("/ajax_checkzip.php", { zip: zipcode },
		function(data) {
			if (data == '0') {
				//display error message
				$("#zip").addClass('invalid');$("#zip").removeClass('valid');
				var boxlocation = $("#zip").offset();
				var newleft = Math.ceil(boxlocation.left + 230);
				$("#invalidziptag").css("left",newleft);
				$("#invalidziptag").fadeIn("fast");
				return false;
			} else {
				$("#zip").addClass('valid');
				$("#locationstring").html(data);
				$("#locationstring").show();
				$("#ziperror").hide();
			}
		});
	}
}

function checkValidPostal() {
	var postalcode = $("#postal").val();
	if ($("#postal").val().length == 6) {
		$.post("/ajax_checkpostal.php", { postal: postalcode },
		function(data) {
			if (data == '0') {
				//display error message
				$("#postal").addClass('invalid');$("#postal").removeClass('valid');
				return false;
			} else {
				$("#postal").removeClass('invalid');$("#postal").addClass('valid');
				$("#locationstringcanada").html(data);
				$("#locationstringcanada").show();
				$("#ziperror").hide();
			}
		});
	}
}

function checkValidEmail() {
	var emailaddr = $("#email").val();
	if (!check_email(emailaddr)) {
		if ($("#email").val().length < 3) {
			$("#email").removeClass("invalid");
			$("#email").removeClass("valid");
			$("#emailerror").hide();
		} else {
			$("#email").addClass("invalid");
			$("#emailerror").html("This is not a valid email address.");
			$("#emailerror").show();
		}
	} else {
		$.post("/ajax_checkemail.php", { email: emailaddr },
		function(data) {
			if (data == '1') {
				// good to go!
				$("#emailerror").hide();
				$("#email").removeClass("invalid");
				$("#email").addClass("valid");
			} else {
				$("#email").removeClass("valid");
				$("#email").addClass("invalid");
				$("#emailerror").html("This email address is already in use!");
				$("#emailerror").show();
			}
		});
	}
}

function checkValidCap() {
	var cap = $("#captext").val();
	$.post("/ajax_checkcap.php", { captext: cap },
	function(data) {
		if (data == '1') {
			$("#captext").removeClass("invalid").addClass("valid");
			$("#caperror").hide();
		} else {
			$("#captext").removeClass("valid").addClass("invalid");
			$("#caperror").html("This is incorrect! Try again or click the code for another.");
			$("#caperror").show();
		}
	});
}
			

function checkSignup() {
	var error = 0;
	if ($("#email").hasClass('invalid')) {
		$("#emailerror").html("Please fix your email address.");
		$("#emailerror").show();
		error++;
	}
	
	if ($("#email").val() == "") {
		$("#emailerror").html("You need an email address to sign up!");
		$("#emailerror").show();
		error++;
	}
	
	if ($("#password").val() == "") {
		$("#passerror").html("Please make sure your passwords match.");
		$("#passerror").show();
		error++;
	}
	
	if ($("#birthday").val() == "") {
		$("#bderror").html("Please make sure you put in a birthdate!");
		$("#bderror").show();
		error++;
	}
	
	/*if ($("input[name=locations]").attr("checked") == false) {
		$("#locerror").html("You need to choose a location before submitting.");
		$("#locerror").show();
		error++;
	} else {
		$("#locerror").hide();
	}*/
	
	if ($('input:radio[name=locations]:checked').val() == "1") {
		if ($("#zip").val() == "") {
			$("#ziperror").html("Please enter a valid zip code.");
			$("#ziperror").show();
			error++;
		}
	} else if ($('input:radio[name=locations]:checked').val() == "2") {
		if ($("#postal").val() == "") {
			$("#ziperror").html("Please enter a valid postal code.");
			$("#ziperror").show();
			error++;
		}
	}
	
	/*if ($("#tos").attr("checked",false)) {
		$("#toserror").html("Please agree to our Terms and Conditions.");
		$("#toserror").show();
		error++;
	}
	
	if ($("#privpol").attr("checked",false)) {
		$("#priverror").html("Please mark that you have read our Privacy Policy.");
		$("#priverror").show();
		error++;
	}*/
	
	if (error > 0) return false;
}

function showLocBox(what) {
	$("div.locationfields,#location0").hide();
	$("#location"+what).show();
}

function expand(wh) {
	var cur = $("#cat"+wh+"prizes").css("display");
	if (cur == "block") {
		$("#cat"+wh+"prizes > input").attr("checked",false);
		$("#cat"+wh+"prizes > span").css("color","#777");
		$("#cat"+wh+"prizes").hide();
	} else {
		$("#cat"+wh+"prizes").show();
		$("#cat"+wh+"prizes > input").attr("checked",true);
		$("#cat"+wh+"prizes > span").css("color","#000");
	}
}

function smexpand() {
	var cur = $("div.lev2").css("display");
	if (cur == "block") {
		$("div.lev2 > input,div.lev3 > input").attr("checked",false);
		$("div.lev3 > span,div.lev2 > span").css("color","#777");
		$("div.lev3,div.lev2").hide();
	} else {
		$("div.lev2").show();
	}
}
