var SendMailContact = {
	setFalse: function (ids){
  		if(typeof(ids)!='object')			{		return;		}
		for(i in ids){
			$JQ("#"+ids[i]).css(			{'border-color': 	'red'});
  		}
	  	//$JQ("#star_"+id).css(		{'color': 			'red'});
	  	//$JQ("#star_"+id).html('*');	  	
	},
	
	setOk: function(ids){
  		if(typeof(ids)!='object')			{		return;		}
		for(i in ids){
			$JQ("#"+ids[i]).css(			{'border-color': 	'green'});
  		}
	  	//$JQ("#star_"+id).css(		{'color': 			'green'});
	  	//$JQ("#star_"+id).html('');
	},	

	checkCaptcha: function(  ){		
		jqp = $JQ.post(	AjaxUrl,
						{ 	'useFunc'	:	'checkCaptcha',
							'captcha'	:	$JQ("#Captcha").val()},
						this.stage1);
		return jqp.responseText;
	},

  	checkEmail: function(email) {
		if(!(new RegExp("[^@]{1,}[@]{1}[^@.]{1,}[.]{1}[^@]{1,}","gi").test(email))){
			return false;
		}
		return true;
	},
	
	refreshCaptcha: function (){
		$JQ.get('index.php?display=contact_us',
			{'actionGetCaptcha':''},
			function(data){
				$JQ("#CaptchaImage").attr("src", 'temp/'+data);
			});
	},
	
	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('#InfoAlert').html("Please correct the fields");
		$JQ('#InfoAlert').css({'color':'red','display':'inline','display':'block','font-weight':'bold','font-size':'11px'});
	},
	
	hideErrorInfo: function(){
		$JQ('#InfoAlert').css({'display':'none'});
	},	
	
	stage1: function (data){
		this.captchaResult = data;
		if(this.captchaResult!='1'){
			SendMailContact.setFalse(["Captcha"]);
			SendMailContact.refreshCaptcha();
			return false;
		}
		alert('Thank you for your enquiry. We will contact you shortly.');
		SendMailContact.stage2();
	},
	
	stage2: function (){
		jqsend = $JQ.post('index.php?display=contact_us', 
				{ 	actionSend		:	'',
					FirstName		:	$JQ("#FirstName").val(), 
					LastName		:	$JQ("#LastName").val(),
					Email			:	$JQ("#Email").val(),
					Telephone		:	$JQ("#Telephone").val(),
					Company			:	$JQ("#Company").val(),
					EnquiryGeneral	:	$JQ("#EnquiryGeneral").val(),
					EnquiryAdvert	:	$JQ("#EnquiryAdvert").val(),
					AddInfo			:	$JQ("#AddInfo").val(),
				},
				function(data) {						
					if(data.indexOf('MAIL SENT OK')<0){
						alert('message could not have been sent'+data);
					}else{
					 	$JQ("#FirstName").val('');
						$JQ("#LastName").val('');
						$JQ("#Email").val('');
						$JQ("#Telephone").val('');
						$JQ("#Company").val('');
						$JQ("#EnquiryGeneral").attr("checked",false);
						$JQ("#EnquiryAdvert").attr("checked",false);
						$JQ("#AddInfo").val('');
						$JQ("#Captcha").val('');						
					}
					SendMailContact.refreshCaptcha();
				});
	},
	
	sendMail: function(carId){
		if(!this.validateMailForm()){
			this.showErrorInfo();
			return false;
		}
		this.checkCaptcha();
	},
		
	validateMailForm: function(){
		var validated	= true;
		var falseFields	= [];
		this.hideErrorInfo();
		this.setOk(['FirstName', 'LastName','Telephone','Email','Company','Captcha']);
		if($JQ("#FirstName").val()==''){
			falseFields.push('FirstName');
		}
		if($JQ("#LastName").val()==''){
			falseFields.push('LastName');
		}
		if(!this.checkTelephone($JQ("#Telephone").val())){
			falseFields.push('Telephone');
		}
		if(!this.checkEmail($JQ("#Email").val())){
			falseFields.push('Email');
		}
		if(falseFields.length>0){ this.setFalse(falseFields); validated=false;}
		return validated;
	}

}