/* ===========================================================================
// CLASS/COMPONENT:
// DS2.SiteMap.js.viewdetails.js
//
// DESCRIPTION:
// Scripts for the sitemap details page
//
// AUTHOR:
// Joey Krabacher (MEK), jkrabacherATdealerskinsCOMcom
// Jeff Wilkerson , jwilkersonATdealerskinsCOMcom
//
// COPYRIGHT:
// Copyright (c) 2008 Dealerskins, Inc. All Rights Reserved.
//
// REVISION HISTORY:
//
// ******************************************************************************
// User: MEK  Date: [12.18.2008]
// Initial Creation
// ******************************************************************************
=========================================================================== */



$(document).ready(function(){
	$("#quickForm").appendTo("#vehicleInfoContainer");
	
	$(".detailsContainer").each(function(i) {
		var heading = $(this).find("h3").text(); 
		var id = $(this).attr("id"); 
		$(this).appendTo('.tabs');
		$('<li><a href="#' + id + '">' + heading + '</a></li>').appendTo('.tabNavigation');
	})
	
	var tabContainers = $('div.tabs > div');
	tabContainers.hide().filter(':first').show();				
	$('div.tabs ul.tabNavigation a').click(function () {
		tabContainers.hide();
		tabContainers.filter(this.hash).show();
		$('div.tabs ul.tabNavigation a').removeClass('selected');
		$(this).addClass('selected');
		return false;
	}).filter(':first').click();
	
	//setup form submit
	var options = {success:showResponse,error:showError};
	$('#QuickContact').ajaxForm(options);
	
	$("#loading").ajaxStart(function(){
	   $(this).show();
	});
	 
	$("#loading").ajaxStop(function(){
	  $(this).hide();
	}); 
});

function showResponse(){
	//if submit was success disable button and change the value of the button
	$("#submitForm").attr({'disabled':'disabled','value':'Form Submitted'});
}

function showError(){
	$("#submitForm").attr({'disabled':false,'value':'Submit'});
	alert('There was an error while attempting to submit form. Please try again later.'); 
}

function handleCallToAction(){
	
	var r = new XMLHttpRequest();
	
	///handle ajax stuff
	if(r){
		var submitString = '<input id="submitForm" class="submitButton" type="button" value="Submit" name="submitForm"/>';
		var formIDString = '<input type="hidden" name="FormID" id="FormID" value="2"/>';
		$("#submitForm").remove();
		$("#FormsID").remove();
		//add new one
		$("#QuickContact").append(submitString);
		$("#QuickContact").prepend(formIDString);
		var oldAction  = $("#QuickContact").attr('action');
		var newAction = oldAction.replace('dforms/dformsprocess.cfm','sitemap/cfc/formSubmitProxy.cfc?method=submitForm')
		$("#QuickContact").attr('action',newAction);
		$("#QuickContact").attr('method','GET');
	}
	//Form submit handler
	$(".submitButton").click(function() {
		var valid = "";
		//loop over each field with a class of 'required'
	    $(".required").each(function(){
	    	if($(this).val() == ""){
	    		valid = valid + $(this).attr('id') + ' is required. \n';
	    	}else if($(this).attr('id') == 'Email'){
	    		//validate email address
		    	var emailFilter=/^.+@.+\..{2,3}$/;
				if (!(emailFilter.test($(this).val()))) {
					valid = valid + "Please enter a valid email address.\n";
				}
			}else if($(this).attr('id') == 'Phone'){
				//validate phone number
				if($(this).val().search(/\d{3}\-\d{3}\-\d{4}/)==-1){
			      valid = valid + "The phone number you entered is not valid.\r\nPlease enter a phone number with the format xxx-xxx-xxxx.\n";
			      return false;
			   	}
				if (!($(this).val().length > 9)) {
					valid = valid + "The phone number is the wrong length. Make sure you included an area code.\n";
			    }
			}
		});
		
	    if (valid != "") {
	       alert(valid);
	       return false;
	    }
		else{
			//do AJAX call and disable button.
			$("#submitForm").attr({'disabled':'disabled','value':'Please Wait...'});					 
			$("#QuickContact").submit();
	    }
	});
}

