$(document).ready(function() {
	$('.reveal').hide();
	$('#addtolist').click(function(){
		if($(this).attr('checked')==true){
			$('.reveal').show();
		}else{
			$('.reveal').hide();
		}
	});
});

$.fn.clearForm = function() {
  return this.each(function() {
 var type = this.type, tag = this.tagName.toLowerCase();
 if (tag == 'form')
   return $(':input',this).clearForm();
 if (type == 'text' || type == 'password' || tag == 'textarea')
   this.value = '';
 else if (type == 'checkbox' || type == 'radio')
   this.checked = false;
 else if (tag == 'select')
   this.selectedIndex = -1;
  });
};

///////////////// Google Adwords Tracking ///////////////

function ppcconversion() {
var iframe = document.createElement('iframe');
iframe.style.width = '0px';
iframe.style.height = '0px';
document.body.appendChild(iframe);
iframe.src = 'http://www.dmsracingltd.co.uk/contact/contact_confirm.html';
};

//////////// Validate email ///////////////////////
var remail=/^([_&a-zA-Z0-9-]+(\.[_&a-zA-Z0-9-]+)*@[&a-zA-Z0-9-]+\.+[&a-zA-Z0-9-]+)/;
function checkEmail(fieldvalue){
	if(remail.test(fieldvalue))
		return false;
	else
		return true;
}

function validate_contact(){	
	flag=true;
	var message		= '';
	if($('#first_name').val()==''){
	   message += '<li>Please enter first name</li>';
	   flag= false;
	}if($('#last_name').val()==''){
	   message += '<li>Please enter last name</li>';
	   flag= false;
	}if($('#email').val()==''){
	   message += '<li>Please enter email</li>';
	   flag= false;
	}if($('#email').val()!='' && checkEmail($('#email').val())){
	   message += '<li>Please enter valid email</li>';
	   flag= false;
	}if($('#tel').val()==''){
	   message += '<li>Please enter phone number</li>';
	   flag= false;
	}if($('#addtolist').attr('checked')==true && $('#imgverify').val()==''){
	   message += '<li>Please enter text in the image</li>';
	   flag= false;	
	}
   if(flag==true){	 
	   $('#addcontact').find('.error').html('');
	   var ran_unrounded	=	Math.random()*100000;
	   var ran_number	=	Math.floor(ran_unrounded); 
	   var posturl		=   $('#addcontact').serialize()+"&action=add_contact"+"&sid="+ran_number;
	   $.ajax({
			type	:	'POST',
			url		:	'/includes/ajax_functions.php',	
			data	:	posturl,
			error	:	function(){alert('Error loading document!');},
			success	:	function(msg){
			  if(msg !=''){
				var	resp	=	msg.split("^");
				if(resp[1]=='S'){
					$('#addcontact').find('.error').html('<ul><li>'+resp[0]+'</li></ul>');					
					var pageTracker = _gat._getTracker("UA-18972618-1");
					pageTracker._trackPageview("/contact/contact_confirm.html");
					if($('#addtolist').attr('checked')==true){
						document.addcontact.action='http://mailer.dmsracingltd.co.uk/box.php';
						document.addcontact.submit();
					}
					$('#addcontact').clearForm();
				}else{
				   $('#addcontact').find('.error').html('<ul><li>'+resp[0]+'</li></ul>');
				}
			  }
			}
		});
	   return flag;
   }else{		   
		$('#addcontact').find('.error').html('<ul>'+message+'</ul>');		
		return flag;
   }
}
