;(function($) {

$.fn.ajaxformcb = function(options) {
	
	var $form = $(this);
	var $fields = $(":input",$form);
	
	// - set default variables
	var $submit = $('#submit', $form);
	
	var $errors = false;
	var $errorLog = Array();
	var $minStrLength = 2;
	var $emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	var $errorClass = 'error';
	
	//alert init
	var $overlayOpacity =  .20;           
	var $overlayColor = '#000';
	var $repositionOnResize = true;
	var $verticalOffset= -75;
	var $horizontalOffset= 0;
	
	$('#address_cb', $form).change(function(){
		$('#fullAddress_cb').val($('#address_cb option:selected').text());
	})
	
	if(options){
	if(options.submit) $submit = $(options.submit, $form);

	}
	var $required = $('.required',$form);
	
	// - required fields
	
$submit.bind('click',function(){
$errors= 0;
		$errorLog = Array();
	$fields.each(function(){
		
if($(this).hasClass('required')){
	_validate($(this),'required')
}

if($(this).hasClass('empty')){
		// - validate that current field is not empty
_validate($(this),'empty');

}
if($(this).hasClass('email')){
	// - validate that current field is a valid email
	_validate($(this),'email');
}
if($(this).hasClass('selected')){
	// - validate that current field is a valid email
	_validate($(this),'selected');
}

	});
	if($errors>0){
		jAlert('Please complete these fields:',$errorLog);
	} else {
		_makeRequest();
	}
		

	})
	
	
	
	
	function _makeRequest(){
		var $data = $form.serialize();
		jLoader();
	
		
	
		$.ajax({
			url: "/ajax/ajax.contact.cb.php",
			type: "POST",
			data: $data,
			success : function (data) {
	if(data==1){
		$.loader._hide();
		//$('#contactForm').hide();
		_reset();
	//	$('#contactBtn').removeClass('active');
		//$('#contactBtn').text('Click here to contact us')
		jAlert('Thank you for your enquiry','We have received your message and will contact you shortly.');
	} else {
			jAlert('Error','An error occurred while sending your form. Please submit again.');
	}
			}
			});
	}
	
	function _reset(){
		//$('textarea[name=enquiry]',$).val('');
	}
	
	function _validate($elm,$rule){
	
		var $title = $elm.attr('title');
		var $val = $elm.val();
		var $valid = true;
		switch($rule){
			case 'required':
			if($val=='') $valid = false;
			break;
			case 'empty':
			if($val != '' && $val.length < $minStrLength) $valid = false;
			break;
			case 'email':
			if($val !='' && !$emailReg.test($val)) $valid = false;
			break;
			case 'selected':
		
			if($elm.attr("selectedIndex") < 1 ) $valid = false;
			break;
		}
		if(!$valid){
			$errors++;
			$errorLog.push($title);
			$elm.addClass($errorClass);
		}
}






//
	
}

})(jQuery);






