// JavaScript Document
var gOffset = null;

$(document).ready(function() {
    loadAndCenterLogo();
    highLightSelectedLink();

    $("#btnSignup").click(function() {
        document.location = "signup.html";
    });

    $("#btnSubmit").click(function() {
        if (IsFormValid()) {
            $("#msgPanel").html("Registering your information. Please wait...");
            $("#msgPanel").fadeIn();
            SubmitForm();
        }
    });

    window.onresize = loadAndCenterLogo;
});

function IsFormValid()
{
	var isvalid = true;
	var firstreq=null;
	$("#form1 :input.req").each(function(i){
		if($(this).val()=="")
		{
			firstreq = firstreq==null?this:firstreq;
			isvalid = false;
			$(this).css("background-color","yellow");
		}
	});

	if (!isvalid)
	{
		$(firstreq).focus();
		alert("Please fill in all required fields.\n");
	}
	else{	    
	    if(!checkEmail()) {
	        isvalid = false;
	        alert("Email address is not valid.\n");
	        $("#email").css("background-color","yellow");
	        $("#email").focus();
	    }
	}
	return isvalid;
}

function checkEmail() {
    var email = document.getElementById("email");
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test(email.value)){
        //email.focus
        return false;
    }
    return true;
}

function SubmitForm() {
    var postData = $(":input").serialize();
//    alert(postData);
//    return;
    $.post("register.aspx", postData, function(data) {
        if (data.search(/pending|exists|failed|error/i) >= 0) {
            $("#msgPanel").html(data);
            $("#msgPanel").fadeIn();
        }
        else {
            var msg = "";
            msg += "<b>Registration completed successfully</b><br>";
            msg += "You should receive an email shortly to confirm your registration.<hr>";
            //msg += "Result:" + data;
            $("#msgPanel").html(msg);
            $("#msgPanel").fadeIn();
        }
    });
}

function loadAndCenterLogo() {
    offset = $("#container").offset();
    if (gOffset == null || offset.top != gOffset.top || offset.left != gOffset.left) {
        gOffset = offset;
        $("#logo").css("position", "absolute");
        $("#logo").css("top", Number(offset.top) + 20);
        $("#logo").css("left", Number(offset.left) + 30);
        $("#logo").fadeIn();
    }
}

function highLightSelectedLink() {
    var href = new String(document.location);
    var index = href.lastIndexOf("/");
    var filename = href.substr(index+1);

    if (filename == "") {
        filename = "default.htm";
    }
    $(".menu a[href=" + filename + "]").css("color", "#426622");
}