// JavaScript Document
$(document).ready(function(){

		$(":input[type=submit]").addClass("FormButton");
		$(":input[type='text']").addClass("FormTextBox");	
		
		$("#shippingAddress").hide();			
		$("#chkShipToAbove").click(function(){
			if($(this).attr("checked")== null || $(this).attr("checked")== "")
			{
				$("#shippingAddress").show();
			}
			else
			{
				$("#shippingAddress").hide();
			}
		});
		
		//$("#registerForm #theTerms").hide();
		//$("#registerForm #theForm").show();
		SetAjaxWaitingStatus("Loading content. Please wait...","#theTerms");
		$("#theTerms").load("fragments/ndaterms.html" + GetCacheBlock(true),"",function(){
			$("#btnShowForm").click(function(){
				$("#registerForm #theTerms").hide();
				$("#registerForm #theForm").show();
			});
			$("#btnCancelNDATerms").click( function(){
				//lhc("login.html");return false;
				document.location = "default.htm";
			} );
		});
		$("#registerForm #theForm").hide();
		
		$("#btnCancel").click( function(){lhc("dealerregister.html" + GetCacheBlock(true));return false;} );
		
		//+ GetCacheBlock()
		$.get("code/register.aspx?getcountries=1" + GetCacheBlock() ,function(data){
			$("#cboCountry").html(data);
			$("#cboShipCountry").html(data);
			
			//Get US States by default
			var q = "code/register.aspx?getstates=1" + GetCacheBlock();			
			$.get(q ,function(data){
				$("#cboStateProv").html(data);
				$("#cboShipStateProv").html(data);
			});			
		});
		
		$("#cboCountry").change(function(){
			//+ GetCacheBlock()
			q = "code/register.aspx?getstates=" + $(this).val() + GetCacheBlock();			
			$.get(q ,function(data){
				$("#cboStateProv").html(data);
			});
		});
		
		$("#cboShipCountry").change(function(){
			//+ GetCacheBlock()
			q = "code/register.aspx?getstates=" + $(this).val() + GetCacheBlock();			
			$.get(q ,function(data){
				$("#cboShipStateProv").html(data);
			});
		});
		
		//$("#btnRegister").click(beginRegister);
		$("form#registerForm").submit(function(){
			if(IsFormValid())
			{
				var postData = $("#registerForm :input").serialize() + GetCacheBlock();
				SetAjaxWaitingStatus("Begining registration process. Please wait...", "#messagePanel");
				$.post("code/register.aspx",postData,function(data){
					if(data!="" && data!=null)
					{
						PrintMessage(data);
						if(data.match(/registered /i))
						{
							$("#theForm").load("fragments/dealerregisterthanks.html");
						}
						else
						{
							PrintMessage(data);
						}
					}
				});
			}
			else
			{
				PrintMessage("Form is incomplete. Please fill in the highlighted fields");
				//$(":input.required").addClass("selectreq");
			}
			return false;						   
		});
});

function IsFormValid()
{
	var IsValid = true;
	$("form :input.required").each(function(i){
		if(this.value == '')
		{
			$(this).css("background-color","#FFCCCC");
			IsValid = false;		
		}
		else
		{
			$(this).css("background-color","");
		}
	});
	
	return IsValid;
}

function PrintMessage(msg)
{
	$("#registerForm #messagePanel").show();
	$("#registerForm #messagePanel").html(msg);
}