var SendMail = {
	setFalse: function (ids){
  		if(typeof(ids)!='object')			{		return;		}
		for(i in ids){
			$JQ("#cd_"+ids[i]).css(			{'border-color': 	'red'});
			$JQ("#star_"+ids[i]).css(		{'color': 			'red'});
	  		$JQ("#star_"+ids[i]).html('*');
  		}	  		  	
	},
	
	setOk: function (ids){
  		if(typeof(ids)!='object')			{		return;		}
		for(i in ids){
			$JQ("#cd_"+ids[i]).css(			{'border-color': 	'green'});
			$JQ("#star_"+ids[i]).css(		{'color': 			'green'});
	  		$JQ("#star_"+ids[i]).html('');
  		}
	},

  	 checkEmail: function(email) {
		if(new RegExp("[^@]{1,}[@]{1}[^@.]{1,}[.]{1}[^@]{1,}","gi").test(email)){
		   return true;
		}
		return false;		
	},
	
	checkPostcode: function(postcode){
		if ((postcode.length!=6)&&(postcode.length!=7)){
			return false;
		}		
		if (!new RegExp("[a-zA-Z]","gi").test(postcode.substr(0,1))){
			return false;
		}
		if (new RegExp("[=;.,?/<>:_!@$%^&{}()~`'\"]","gi").test(postcode)){
			return false;
		}		
		return true;		
	},
	
	checkTelephone: function(phone){		
		if(!(new RegExp("[0-9]{4,}","gi").test(phone))){
			return false;
		}
		if(new RegExp("[a-zA-Z=;.,?/<>:_!@$%^&{}()~`'\"]","gi").test(phone)){
			return false;
		}
		return true;	
	},	
	
	showErrorInfo: function(){
		$JQ('#cd_message').html("Please correct the fields");
		$JQ('#cd_message').css({'color':'red','display':'inline'});
	},
	
	hideErrorInfo: function(){
		$JQ('#cd_message').css({'display':'none'});
	},
	
	sendMail: function(carId){
		if(!this.validateMailForm()){
			this.showErrorInfo();
			return false;			
		}
		alert('Thank you for your enquiry. We will contact you shortly.');
		$JQ.post('index.php?display=send_mail', 
					{ 	name: 		$JQ("#cd_name").val(), 
						surname:	$JQ("#cd_surname").val(),
						email:		$JQ("#cd_email").val(),
						phone:		$JQ("#cd_telephone").val(),
						postcode:	$JQ("#cd_postcode").val(),
						question:	$JQ("#cd_question").val(),
						title:		$JQ("#cd_title").val(),
						car_id:		carId
					}, 
					function(data) {						
						if(data.indexOf('MAIL SENT OK')<0){
							alert('message could not have been sent');
						}else{
							//alert('Thank you for your enquiry. We will contact you shortly.');							
							showHideDiv('contact_dealer_form_'+carId);
						 	$JQ("#cd_name").val('');
							$JQ("#cd_surname").val('');
							$JQ("#cd_email").val('');
							$JQ("#cd_telephone").val('');
							$JQ("#cd_postcode").val('');
							$JQ("#cd_question").val('');								
						}
					});
	},	
	validateMailForm: function(){
		var validated	= true;
		var falseFields	= [];
		this.hideErrorInfo();
		this.setOk(['name', 'surname', 'telephone', 'email', 'postcode', 'question']);
		if($JQ("#cd_name").val()==''){
			falseFields.push('name');			
		}
		if($JQ("#cd_surname").val()==''){
			falseFields.push('surname');
		}		
		if(!this.checkTelephone($JQ("#cd_telephone").val())){
			falseFields.push('telephone');
		}		
		if(!this.checkPostcode($JQ("#cd_postcode").val())){
			falseFields.push('postcode');
		}
		if(!this.checkEmail($JQ("#cd_email").val())){
			falseFields.push('email');
		}
		if(falseFields.length>0){ this.setFalse(falseFields); validated=false;}
		return validated;
	}
}