// JavaScript Document
var GB_ANIMATION = true;
$(document).ready(function(){	
		loadFormStyles();		
      
        //Begin:RememberPassword
		$("a.greybox").click(function()
        {
          GB_show("Forgot password","forgotpassword.aspx",175,600);
          return false;
        });
		//End:RememberPassword
		
		
		if(lastSecureLink!="" && lastglobalMsg!=""){
			$("#errors").html(lastglobalMsg);
		}	
});

function initForm()
{
	$("#registerButton").click(function(){
		lhc("dealerregister.html" + GetCacheBlock(true) );
	});
	
	$("#btnCancel").click(function(){lhp();return false;});	
		
	var errors = "";
	$('form :input').blur(function(){
		if($(this).is('.required') && this.value == '')
		{
			$(this).css("background-color","red");
			errors += "You have not filled all required field.";
		}
	});
					
	$('form#loginForm').submit( function(){
		errors = "";
		$(':input.required').trigger('blur');
			if(errors!="")
			{
				$("#errors").html("<h2 class='error'>Please correctly fill in all highlighted fields.</h2>");
			}
			else
			{
				var postData = $("#loginForm :input").serialize() + GetCacheBlock();
				$("#errors").html("Validating your credentials. Please wait.");
				$.post("code/login.aspx",postData,function(data){
					
					if(data.search(/pending|exists|failed/i)>=0)
					{
						$("#errors").html(data);
					}
					else
					{
						//var loginData = data.split(";")							
						$.cookie(userLoggedInCookieKey,data);
						checkLogin();
						
						if(lastSecureLink!="")
						{
							lhc(lastSecureLink + GetCacheBlock(true));
							lastSecureLink="";
							lastglobalMsg="";
						}
						else
						{
							$("#errors").html("<h2 style='color:green'>You are successfully logged in.</h2>");
							$("#loginForm").hide();
						}							
					}
				});
			}
			return false;
	} );	
}