function req_param( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

$(document).ready(function() {
	// validate signup form on keyup and submit
	var validator = $("#signupForm").validate({
		rules: {
			fullname: {
				required: true,
				minlength: 3
			}, 
			password: {
				required: true,
				minlength: 6
			},
			password_confirm: {
				required: true,
				minlength: 6,
				equalTo: "#password"
			},
			email: {
				required: true,
				email: true,
				remote: "lib/ajax/checkemail.php"
			},
			email_confirm: {
				required: true,
				email: true,
				equalTo: "#email"
			},
			captcha: {
				required: true,
				remote: "lib/ajax/checkcaptcha.php"
			},
                        terms : "required"
		},
		messages: {
			fullname: {
				required: "Enter your full name",
				minlength: "Name must be at least 3 characters long"
			},
			password: {
				required: "Provide a password",
				minlength: jQuery.format("Enter at least {0} characters")
			},
			password_confirm: {
				required: "Repeat your password",
				minlength: jQuery.format("Enter at least {0} characters"),
				equalTo: "Enter the same password"
			},
			email: {
				required: "Enter a valid email address",
                                email: "Enter a valid email address",
				remote: jQuery.format("{0} is already in use")
			},
			email_confirm: {
				required: "Enter a valid email address",
                                email: "Enter a valid email address",
				equalTo: "Enter the same email address"
			},
			captcha: {
				required: "Captcha Cannot be empty",
				remote: "Wrong Captcha"
			},
                        terms : "You must agree to the Terms of Use"
		},
		// the errorPlacement has to take the table layout into account
		errorPlacement: function(error, element) {
			if ( element.is(":radio") )
				error.appendTo( element.parent().next().next() );
			else
				error.appendTo( element.parent().next() );
		},
		// specifying a submitHandler prevents the default submit, good for the demo 
		submitHandler: function() {
                    form.submit();
//		    alert("submitted!");
		}, 
		// set this class to error-labels to indicate valid fields 
		success: function(label) { 
		    // set   as text for IE 
		    label.html(" ").addClass("checked"); 
		} 
	});

	// check if confirm password is still valid after password changed
	$("#password").blur(function() {
		$("#confirm_password").valid();
	});


	var url = req_param("redirect");
	if (url == ""){
		$("#redirect").val( top.location )
	} else {
		$("#redirect").val(  unescape( url) ) ;
	}

        $("#isframe").val( req_param("isframe")) ;

});

