// JavaScript Document

		$( function() {
			$( '#contactFormenquiry' ).ipValidate( {

				required : { //required is a class
					rule : function() {
						return $( this ).val() == '' ? false : true;
					},
					onError : function() {
						if( !$( this ).parent().hasClass( 'element_container' ) ) {
							$( this ).wrap( '<div class="element_container error_div"></div>' ).after( '<span>' + $( this ).attr( 'rel' ) + '</span>' );
						} else if( !$( this ).parent().hasClass( 'error_div' ) ) {
							$( this ).next().text( $( this ).attr( 'rel' ) ).parent().addClass( 'error_div' );
						}
					},
					onValid : function() {
						$( this ).next().text( '' ).parent().removeClass( 'error_div' );
						$(this).focus();
					}
				},
				
				fname: { //required is a class
					rule : function() {
						return $( this ).val() == 'Name' ? false : true;
					},
					onError : function() {
						$(this).parent().append("<span>Please Enter Name</span>");
					},
					onValid : function() {
						$(this).parent().children('span').remove();
						$(this).focus();
					}
				},
				
				subject: { //is a class
					rule : function() {
						return $( this ).val() == 'Subject' ? false : true;
					},
					onError : function() {
						$(this).parent().append("<span>Please Enter Subject</span>");
					},
					onValid : function() {
						$(this).parent().children('span').remove();
						$(this).focus();
					}
				},
				email: { // is a class
					rule : function() {
						//return $( this ).val() == 'Email Address' ? false : true;
						return isValidEmailAddress( $(this).val() );
					},
					onError : function() {
						$(this).parent().append("<span>Please Enter Valid Email</span>");
					},
					onValid : function() {
						$(this).parent().children('span').remove();
						$(this).focus();
					}
				},
				
				desc: { // is a class
					rule : function() {
						return $( this ).val() == 'Enquiry' ? false : true;
					},
					onError : function() {
						$(this).parent().append("<span>Please Enter Enquiry Details</span>");
					},
					onValid : function() {
						$(this).parent().children('span').remove();
						$(this).focus();
					}
				},

				submitHandler : function() {
					$('.salesenqury .submit5 span').remove();
					$('.salesenqury .submit5').append('<span class="loading-mailenq">Loading....</span>');
					$.post('mailEnquiry.php',{name:$('#name2').val(), subject:$('#subject2').val(), email:$('#email2').val(), description:$('#description2').val()},
					function(data){
						$('.salesenqury .submit5 .loading-mailenq').remove();
						if( data == 'success') {
							$('.salesenqury .submit5').append("<span>Thank you for your message</span>");
							$('#name2').val('Name');
							$('#subject2').val('Subject');
							$('#email2').val('Email Address');
							$('#description2').val('Enquiry');
						} else {
							$('.salesenqury .submit5').append("<span>Sorry but your message could not be sent, try again later</span>");
						}
					});		
					
					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);
	}
