

// JavaScript Document


// JavaScript Document




//////////////////////////////////////////
///////// Reset Form ////////////////////
////////////////////////////////////////
function resetForm(id) {
	// makes the delete user invisible
	$("#deleteUser").css("display","none"); 
	$("#addUser").css("display","block"); 
	$("#updateUser").css("display","none"); 
	
	
	$('#'+id).each(function(){
	        this.reset();
	});
	 	
	}


 

	
	
	
	
	
// sets the value of the hidden field to reflect the submit button clicked
function submitV(value)
	{
	document.forms['mainForm'].elements['submitValue'].value = value;
	}

function success(msg)
	{
	$("#success").empty();
	$("#success").append(msg);
	$("#failBox").css("display","none"); 
	$("#fail").empty();
	$("#successBox").css("display","block");
	$("#loadingWheel").fadeTo("medium", 0.0);	
	}

function fail(msg)
	{
	$("#fail").empty();
	$("#fail").append(msg);
	$("#successBox").css("display","none"); 
	$("#success").empty();
	$("#failBox").css("display","block"); 
	$("#loadingWheel").fadeTo("medium", 0.0);
	}

////////////////////////////////////////////////
//////////// jQuery Scripts //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////

// enables javascript prior to files loading
$(document).ready(function(){


// hides info on ready, this is for non-javascript users
        $("#preInfo").slideUp("slow");
        $("#sigInfo").slideUp("slow");
        $("#suxInfo").slideUp("slow");
        $("#sunInfo").slideUp("slow");
        $("#ssiInfo").slideUp("slow");
        $("#ssxInfo").slideUp("slow");




/// slider code for open/close details

//Code for example C
 $("#pre").click(function () {
      if ($("div:#preInfo").is(":hidden")) {
        $("#preInfo").slideDown("slow");
      } else {
        $("#preInfo").slideUp("slow");
      }
    });



 $("#sig").click(function () {
      if ($("div:#sigInfo").is(":hidden")) {
        $("#sigInfo").slideDown("slow");
      } else {
        $("#sigInfo").slideUp("slow");
      }
    });


 $("#sux").click(function () {
      if ($("div:#suxInfo").is(":hidden")) {
        $("#suxInfo").slideDown("slow");
      } else {
        $("#suxInfo").slideUp("slow");
      }
    });


 $("#sun").click(function () {
      if ($("div:#sunInfo").is(":hidden")) {
        $("#sunInfo").slideDown("slow");
      } else {
        $("#sunInfo").slideUp("slow");
      }
    });


 $("#ssi").click(function () {
      if ($("div:#ssiInfo").is(":hidden")) {
        $("#ssiInfo").slideDown("slow");
      } else {
        $("#ssiInfo").slideUp("slow");
      }
    });


 $("#ssx").click(function () {
      if ($("div:#ssxInfo").is(":hidden")) {
        $("#ssxInfo").slideDown("slow");
      } else {
        $("#ssxInfo").slideUp("slow");
      }
    });







	
////////////////////////////////////////////////
//////////// Add or update or delete a salesman //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
	//validate form
$(function() {
	// validate Add Salesman form on keyup and submit
$("#contact").validate({
	//set the rules for the field names
rules: {
	firstName: {	
		required: true,
		minlength: 2
		},
	lastName: {	
		required: true,
		minlength: 2
		},
	zip: {	
		required: true,
		minlength: 5
		},
	email: {	
		required: true,
		email: true
		}
},
//set messages to appear inline
messages: {
	firstName: {
      required: "<br /> -Please enter name",
      minlength: "<br /> -Please enter name"
    },
	lastName: {
      required: "<br /> -Please enter name",
      minlength: "<br /> -Please enter name"
    },
	zip: {
      required: "<br /> -Please enter zip",
      minlength: "<br /> -Zip must be valid."
    },
	email: {
		required: "<br /> -Please enter an email",
		email: "<br /> -Email must be valid"
	}
}
});
});					   
						   
						   
	
$("form#contact").submit(function() {
	// disables the submit button
	var $that = $(this);
	var submitButton = $that.find("input[type='submit']");
	$(submitButton).attr("disabled", "disabled"); 

	// starts the loading wheel visual
	$("#contact").fadeTo("medium", 0); 
	$("#loadingWheel").fadeTo("medium", 1); 
	
	
	// we want to store the values from the form input box, then send via ajax below
	var firstName  	= 	$('#firstName').attr('value');
	var lastName   	= 	$('#lastName').attr('value');
	var email     	= 	$('#email').attr('value');
	var zip  		= 	$('#zip').attr('value'); 
	var comments   	= 	$('#comments').attr('value'); 
	var phone   	= 	$('#phone').attr('value'); 
	
	//hidden fields
	var source   	= 	$('#source').attr('value'); 
	var reason = $('#reason').attr('value'); 
	var country   	= 	$('#country').attr('value'); 
	var productlineid = $('#productlineid').attr('value'); 
	
		var response = $.ajax({
			type: "GET",
			url: "../logic/ajax-RedOak.php",
			//dataType:"xml",
			data: "firstName="+ firstName + "&lastName="+ lastName + "& email=" + email + "& zip=" + zip + "& comments=" + comments + "& source=" + source + "& reason=" + reason + "&country=" + country + "& productlineid=" + productlineid,
			error: function(){				//alertResponse(response);

				//alertResponse(response);
			},
			success: function(xml)
				{ // if the response from the query == 1 (success) the success function is fired
				//alertResponse(response);
				

				$(submitButton).removeAttr("disabled"); 
				$("#dealerInfo").css("display", "block"); 
				// parse response xml and assign values
				///alert(xml);
				$(xml).find('dealer').each(function(){
                    var name = $(this).find('name').text();
                    var address1 = $(this).find('address1').text();
                    var city = $(this).find('city').text();
                    var state = $(this).find('state').text();
                    var zip = $(this).find('zip').text();
                    var phone = $(this).find('phone').text();
                    var url = $(this).find('url').text();
					var text = "Visit Dealership Website";
					var text2 = "View Map/Directions";
					//alert(phone);
					
					
					// append proper areas with dealer info
					$("#dealership").empty();
					$("#dealership").append(name);
					
					$("#address").empty();
					$("#address").append(address1);
					
					$("#cityStateZip").empty();
					$("#cityStateZip").append(city + " " + state + ', ' + zip);
					
					$("#url").empty();
					//$("#url").append(url);
					
					if (url != "")
					{
					$('<a href="'+ url +'" target="blank"></a>').html(text).appendTo('#url');
					}
					
					//$('<div class="news_source"></div>').html(source).appendTo('#news_wrap');
					
					
					$("#phoneDealer").empty();
					$("#phoneDealer").append(phone);
					
					$('<a href="http://www.chaparralboats.com/Boat-Dealer-Locator/logic/Chaparral-Dealer.php?prodline=2&radius=500&postalcode='+zip+'&country=US" target="blank"></a>').html(text2).appendTo('#map');
					
					
					
					
					
                });
				
				
				
				
				
				$("#contact").css("display", "none"); 
				$("#dealerInfo").fadeTo("medium", 1); 
			
				//alertResponse(response);

				}
			
			});
	
	return false;
	
	});





function alertResponse(response)
	{
		alert(response.responseText);
	}



});




///////////////////// Tiny MCE implemetation  ////////////////////////////




