$(document).ready(function() {
	    var emailr = "no";
		
		$('#contactform').ajaxForm({ 
			beforeSubmit: validate,
	        // target identifies the element(s) to update with the server response 
	        target: '#contactform',
	        success: function() { 
	            $('#contactform').fadeIn('slow');
	        } 
	      });
		  
	$("input, textarea").focus(
	 function() {
	  // only select if the text has not changed
	  if(this.value == this.defaultValue){
	   this.select();
	  }
	 }
	 )
	 
	    $('input#name').keyup(function() {
			var nameval = $(this).val()
			if (nameval != "") {
				$("label#name_error").hide();
			}
	});
		
		    $('input#lname').keyup(function() {
			var nameval = $(this).val()
			if (nameval != "") {
				$("label#lname_error").hide();
			}
	});
   
      $('input#email').keyup(function() {
		
		var nameval = $(this).val()
			if (nameval != "") {
				$("label#email_error").hide();
				if (isValidEmailAddress(nameval)) {
					$("label#email_error2").hide();
				}
			}
		
	});

//-------------------------------------------
// Shiftbar Code
//-------

/*$("a[href^='http:']").not("[href*='www.endrasbmw.com']").not("[href*='www.twitter.com']").not("[href*='twitter.com']").not("[href*='www.dlsaccelerator.com']").not("[href*='www.facebook.com']").each(function(){ 
	var tempurl = '<?php bloginfo('url'); ?>/shiftbar/shiftbar.html?iframe=';
	var $this = $(this);
	var currenturl = this.getAttribute("href");
    var href = tempurl + currenturl;
	$this.attr('href', href ); 
});
$("a[href^='http:']").filter("[href*='twitter.com']").attr('target','_blank');
$("a[href^='http:']").filter("[href*='www.twitter.com']").attr('target','_blank');
$("a[href^='http:']").filter("[href*='www.facebook.com']").attr('target','_blank'); */

//-------------------------------------------
// Here is the Show/Hide for regular content 
// Showhide text = Less Info / More Info
// BASED ON : Andy Langton's show/hide - Latest version @ http://andylangton.co.uk/jquery-show-hide
//-------------------------------------------

// choose text for the show/hide link - can contain HTML (e.g. an image)
var showText='<div class="showthis">more info</div>';
var hideText='<div class="showthis">less info</div>';
// initialise the visibility check
var is_visible = false;
// append show/hide links to the element directly preceding the element with a class of "toggle"
$('.toggle').prev().append(' <a href="#" class="toggleLink">'+showText+'</a>');
// hide all of the elements with a class of 'toggle'
$('.toggle').hide();
// capture clicks on the toggle links
$('a.toggleLink').click(function() {
// switch visibility
is_visible = !is_visible;
// change the link depending on whether the element is shown or hidden
$(this).html( (!is_visible) ? showText : hideText);
// toggle the display - uncomment the next line for a basic "accordion" style
$(this).parent().next('.toggle').slideToggle(500);
// return false so any link destination is not followed
return false;
});

//-------------------------------------------
// Here is the Show/Hide for Product Models
// Showhide text = Hide Models / Show Models
// BASED ON : Andy Langton's show/hide - Latest version @ http://andylangton.co.uk/jquery-show-hide
//-------------------------------------------
// choose text for the show/hide link - can contain HTML (e.g. an image)
var showModels='<div class="showthis">Show Other Models</div>';
var hideModels='<div class="hidethis">Hide Other Models</div>';
// initialise the visibility check
var models_is_visible = false;
// append show/hide links to the element directly preceding the element with a class of "toggle"
$('.modeltoggle').prev().append(' <a href="#" class="toggleLink1">'+showModels+'</a>');
// hide all of the elements with a class of 'toggle'
$('.modeltoggle').slideToggle(1000);
$(".showthis").css({  opacity: "0"});
$(".showthis").animate({  opacity: "1"}, 1000 );

// capture clicks on the toggle links
$('a.toggleLink1').click(function() {
// switch visibility
models_is_visible = !models_is_visible;
// change the link depending on whether the element is shown or hidden
$(this).html( (!models_is_visible) ? showModels : hideModels);
// toggle the display - uncomment the next line for a basic "accordion" style
$(this).parent().next('.modeltoggle').slideToggle(500);
// return false so any link destination is not followed
return false;
});


});


function isValidEmailAddress(emailAddress) {
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		return pattern.test(emailAddress);
	}

	function validate(formData, jqForm, options) { 
	
	    var namevalue = $('input[name=first-name]').fieldValue();
		var lnamevalue = $('input[name=last-name]').fieldValue();
	    var emailvalue = $('input[name=email]').fieldValue();
		
		var errors = 0;
		
		if (namevalue == "") {
		      $("label#name_error").show();
		      $("input#name").focus();
		      errors++;
		} else {
			$("label#name_error").hide();
		}
		
		if (lnamevalue == "") {
		      $("label#lname_error").show();
		      $("input#name").focus();
		      errors++;
		} else {
			$("label#lname_error").hide();
		}
		
		// email Validation
		
		if (emailvalue == "") {		
			  				
			$("label#email_error2").show();
	    	$("input#email").focus();
	    	errors++;
		}else{
			if (isValidEmailAddress(emailvalue)) {
 				$("label#email_error2").hide();
				$("label#email_error").hide();
			}else{
				$("label#email_error").hide();
		    	$("label#email_error2").show();
		    	$("input#email").focus();
		    	errors++;
		 	}
		 }
		
		if (errors > 0) {
			emailr = "yes";
			return false;
		}
		
	}
