﻿$(function(){
	$("#country").change(function(){
		var statezip = $(this).parents("tr").next().next().andSelf();
		if ($(this).val() == "US")
			statezip.removeClass("hidden");
		else
			statezip.addClass("hidden").find("input,select").val("");
	}).change();
	$("#time").change(function(){
		if (("" + this.selectedIndex).match(/^(3|4|6|7|10|14|16|17)$/)) {
			var c = $("#country");
			if (c[0].selectedIndex == 0) {
				c[0].selectedIndex = 1;
				c.change();
			}
		}
	});
	var h = (new Date()).getTimezoneOffset() / 60;
	if (isDST())
		h++;
	if (h > 0) {
		if (h < 10)
			h = "0" + h;
		h = "-" + h;
	} else if (h < 0) {
		h = Math.abs(h);
		if (h < 10)
			h = "0" + h;
		h = "+" + h;
	} else
		h = "(GMT)";
	var opts = $("#time option:contains('" + h + "')");
	if (opts.length > 0) {
		if (opts.length == 1)
			opts[0].selected = true;
		else {
			var fopts = opts.filter(":contains('(US')");
			if (fopts.length > 0)
				fopts[0].selected = true;
			else
				opts[opts.length - 1].selected = true;
		}
		$("#time").change();
	}
	$.validator.addMethod("nospace", function(value){
		return !value.match(/\s+/);
	}, "Spaces are not allowed in the username.");
	$("form").validate({errorElement: "div", errorPlacement: function(error, element){
			element.parent().prev().addClass("t");
			error.appendTo(element.parent());
		},
		rules: {
			first: "required",
			last: "required",
			email: {
				required: true,
				email: true,
				remote: "signup.aspx"
			},
			job: "required",
			username: {
				required: true,
				nospace: true,
				remote: "signup.aspx"
			},
			password: {
				required: true,
				minlength: 5
			},
			password2: {
				required: true,
				equalTo: "#password"
			},
			city: "required",
			country: "required",
			state: {
				required: function(){
					return $("#country").val() == "US";
				}
			},
			zip: {
				digits: true,
				minlength: 5
			},
			time: "required"
		},
		messages: {
			email: {
				remote: "This email address is already in use.<br />Please <a href='login.aspx'>log in</a> if you already have an account."
			},
			username: {
				remote: "This username is already in use. Please try another."
			},
			password2: {
				equalTo: "The passwords entered do not match."
			},
			zip: "This zip code is not valid."
		}
	});
	var submitted = false;
	$("form").submit(function(){
		if (!$("#agree input")[0].checked) {
			alert("You must agree to the Terms of Service before creating an account.");
			return false;
		}
		if ($(this).valid()) {
			if (submitted)
				return false;
			submitted = true;
		}
	});
	$("#first")[0].focus();
});

function isDST() {
	var d = new Date();
	var d1 = new Date(d.getFullYear(), 0, 1, 0, 0, 0, 0);
	var d2 = new Date(d.getFullYear(), 6, 1, 0, 0, 0, 0);
	var t = d1.toGMTString();
	var d3 = new Date(t.substring(0, t.lastIndexOf(" ") - 1));
	t = d2.toGMTString();
	var d4 = new Date(t.substring(0, t.lastIndexOf(" ") - 1));
	var dst = ((d1 - d3) != (d2 - d4));
	if (dst)
		dst = (d.getMonth() >= 2 && d.getMonth() < 10);
	return dst;
}