 $(document).ready( function() {
 
	if (/\?status=success/.test(document.location.search)) {
		window.parent.goto_MBAIntranet();
	} else if (/\?status=oldstudent/.test(document.location.search)) {
		$('#msg_login').html('Please click <a href="http://tpgstudent.fbe.hku.hk" target="_blank">here</a> to login.').effect("highlight",{},1500);
	} else if (/\?status=fail/.test(document.location.search)) {
		$('#msg_login').text('Wrong Login or Account is disabled!').effect("highlight",{},1500);
	}
	
	
	
	
	$('#btn_login').click(function (event) {
		event.preventDefault();

		$('#msg_login').text('');

		$('input#username').val($('input#username').val().replace(/^\s+|\s+\$/g, "")) + '';
		$('input#password').val($('input#password').val().replace(/^\s+|\s+\$/g, "")) + '' ;
		var strusername = $('input#username').val() + '';
		var strpwd = $('input#password').val() + '' ;
		if (!/^.+$/i.test(strusername)) {
			$('#msg_login').text('Please input username').effect("highlight",{},1500);
			return false;
		} else if (!/^.+$/i.test(strpwd)) {
			$('#msg_login').text('Please input password').effect("highlight",{},1500);
			return false;
		} else {
			$("#loginframe").contents().find("#username").val($("input#username").val());
			$("#loginframe").contents().find("#password").val($("input#password").val());
			var strText = $("#loginframe").contents().find("#username").val();
			var strPassword = $("#loginframe").contents().find("#password").val();
			$("#loginframe").contents().find("form").submit();
			//document.forms['loginform'].submit();
		}
	})

	$('#btn_email').click(function (event) {
		event.preventDefault();

		$('input#email').val($('input#email').val().replace(/^\s+|\s+\$/g, "")) + '';

		var stremail = $('input#email').val() + '';
		$('#msg_email').text('Processing...');

		if (!/^.+$/i.test(stremail)) {
			$('#msg_email').text('Please input email').effect("highlight",{},1500);
			return false;
		} else if (!/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(stremail)) {
			$('#msg_email').text('Email is not in correct format.').effect("highlight",{},1500);
			return false;
		} else {
			//Let's go Ajax
			var strurl = "http://intranet.mba.hku.hk/forgetpassword_ajax.aspx";
			var parameters = {email:stremail};
			$.get(strurl, parameters,
				function(data) {
					
					if (data.status != "success") {
						$('#msg_email').text(data.reason).effect("highlight",{},1500);
						return;
					} else {
						$('#msg_email').text(data.reason).effect("highlight",{},1500);
					}
					
				}, "jsonp");
		}
	})

 });

 	function goto_Intranet() {
		document.location.href = "http://intranet.mba.hku.hk";
	}
	
	function show_msg(strMsg) {
		$('#msg_login').html(strMsg).effect("highlight",{},1500);
	}
