	function trim(str, chars) {
		return ltrim(rtrim(str, chars), chars);
	}
	 
	function ltrim(str, chars) {
		chars = chars || "\\s";
		return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
	}
	 
	function rtrim(str, chars) {
		chars = chars || "\\s";
		return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
	}
	$(document).ready(function(){
		$.validator.addMethod("email2", function(value, element) {
			return (value == null)?false:((/^(("[\w-+\s]+")|([\w-+]+(?:\.[\w-+]+)*)|("[\w-+\s]+")([\w-+]+(?:\.[\w-+]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)/i).test(value));
		}, "Invalid mail format.");
		var vaildForm = $("#theForm").validate({
			errorLabelContainer: $("#theForm div.error"),
			rules: {
				request_name: { required: true, minlength: 3},
				request_email: { email2: true, required: true},
				service_needs: { required: true }
			},
			messages: {
				request_name: { required: "Please enter your name.", minlength: "Name should be atleast 3 characters."},
				request_email: { email2: "Please make sure you enter a correct eMail", required: "Your eMail is required."},
				service_needs: { required: "How can we help you?, please specify." }
			}
		});
		$("#request_name").alpha({ichars:'~!@#$%^&*()_+`"-=:;\'[]{}\|\\?/><,'});
		$("#request_email").alphanumeric({ichars:'!#$%^&*()">:;</,{}?=\'\\|[]'});
		$("#service_needs").charCounter(500);
		$("#submit").click(function(){
			if (vaildForm.form()) {
				submitCaptcha();
			}
			return false;
		});
		function submitCaptcha(){
			var response = $("#recaptcha_response_field").val();
			var challenge = $("#recaptcha_challenge_field").val();
			$.ajax({
				type: "POST",
				url: "../code-library/recaptcha-php/index.php",
				data: "recaptcha_challenge_field="+challenge+"&"+"recaptcha_response_field="+response,
				success: function(msg){
					Recaptcha.reload();
					if (trim(msg)=="0"){
						alert("Warning: Captcha (two words entered) is bad, form can not be submitted.");
					} 
					else {
						$('#theForm').ajaxSubmit({
							//iframe: true,
							success: function(msg){
								if (trim(msg)== "true"){
									$("#contact-us-content-left *").hide()
									$("#contact-us-content-left").append("<p></p><p>Thank you for your communication. We will review and respond to your enquiry as soon as possible. Usually within hours, but please allow upto 24 hours.</p>")
								}
							}
						}); 
						//$('#theForm').submit();
						//alert("Testing result: Captcha is good, form is ready to be submitted.");
					}
				}
			});
		}
		reCaptcha();
		
		$("#contact-us-box input:text").each(function(){
		 	if( $(this).data("qtip")){} else{ setQtip("#"+$(this).attr("id"),'');}
		 });
		$("#contact-us-box textarea").each(function(){
		 	if( $(this).data("qtip")){} else{ setQtip("#"+$(this).attr("id"),'');}
		 });
		$("#contact-us-box select").each(function(){
		 	if( $(this).data("qtip")){} else{ setQtip("#"+$(this).attr("id"),'');}
		 });
		 
		function setQtip(id, text){
		    if (text == '') {
		    	text = "Tool tip text to put here when provided.";
		    }
			$(id).qtip({
				  show: { when: { event: 'focus' } },
			      hide: { when: { event: 'blur' } },
				  content: text,
			      position: {
			         corner: {
			            target: 'topRight',
			            tooltip: 'bottomLeft'
			         }
			      },
			      style: {
			         name: 'green',
			         padding: '7px 13px',
			         width: {
			            max: 210,
			            min: 0
			         },
			         tip: true
			      }
		   	});
		}
	});
	function reCaptcha (){
		Recaptcha.create("6LeWBQgAAAAAAEzywAmPDTVQBJ4cFPTsh9rMqj8D", "", {
	        theme: "custom",
	        tabindex: 0,
			custom_theme_widget: 'recaptcha_widget'
		});
	}

