var cookieValue = $.cookie("CAPassport");
function signupBoxLoad(){
	$("#toggleLogin").click(function(){
		$(".LoginBox").load("../Modules/Passport.aspx .loginAjax", loginBoxLoad);
	});
}
function loginBoxLoad(){
	var el = $("input").not($(".login_window input").not('[disabled=true]'));
	el.disabled = true;
	$('.input_username').attr('value','');//ensure username resets to blank upon refresh of page
	$('.login_window input').focus(function(){
		$(this).css('color', '#333');//change the color of the text as the user is typing their info
		if ($(this).hasClass('input_username'))
			$(this).css('background-position','100% 0')//clear the ID field when user clicks
		if ($(this).hasClass('input_password'))
			$(this).css('background-position','100% 0')//show the password field for user input
	});  
	$('.remember_me img').toggle(function(){
		$(this).attr('src','http://nxcache.nexon.net/combatarms/common/remember_checkbox_checked.gif')},//change the checkbox image once clicked,
	function(){
		$(this).attr('src','http://nxcache.nexon.net/combatarms/common/remember_checkbox_unchecked.gif')//change back to other image if clicked again
	});
	if (cookieValue != null) {
		$("input[id*=tbID]").val(cookieValue).css({color: '#333',backgroundPosition: '100% 0'});
		$('.remember_me img').click();
	}
	$(".button_login").click(function(){
		var alertStr = ($("input[id*=tbPass]").val() == "" ? "Please enter your Password" : 
						($("input[id*=tbID]").val() == "ID" || $("input[id*=tbID]").val() == "" ? "Please input your ID" : ""));		
		if (alertStr != "") {
			alert(alertStr);
			return false;
		}
		else {
			var postParams = $.grep($("form").serializeArray(), function(value) {return (value.name == "tbID" || value.name == "tbPass");});
			$.ajax({
				url: "../Modules/Passport.aspx",
				type: "POST",
				data: postParams,
				datatype: "html",
				success: function(data){
					var alertData = data.split("<script language=\"javascript\">");
					if (alertData[1] != null) {
						alertData = alertData[1].split("</script>");
						eval(alertData[0]);
					}
					else {
						if ($(".remember_me img[src*=unchecked]").size() < 1)
							$.cookie("CAPassport",postParams[0].value.toString(),{expires: 365,path: "/",domain:"nexon.net"});
						else
							$.cookie("CAPassport", null, {path: "/",domain:"nexon.net"});
						$(".LoginBox").load("../Modules/Passport.aspx .logoutAjax", function(){el.removeAttr("disabled");startUpdateSession(isSSO);});
//						$(".LoginBox").load("../Modules/Passport.aspx .logoutAjax", function(){el.removeAttr("disabled");});
					}
				},
				error: function(xhr){
					alert("I'm sorry. There was an error. Please try again later. "+ xhr.status + " " + xhr.statusText);
				}
			});
			return false;
		}
	});
}
