$(document).ready(function(){

	$("#contact-form").RSV({
	  onCompleteHandler: myOnComplete,
	  displayType: "display-html",	  
	  errorFieldClass: "errorField",	  
                rules: [	
						"required,company,Please enter your Company Name.",
						"required,contactname,Please enter your Contact Name.",
						"required,telephonenumber,Please enter your Telephone Number.",	
                        "required,Email,Please enter your Email.",
                        "valid_email,Email,Please enter a valid Email.",
                        "required,handsets,Please enter Number Of Handsets.",
						"required,businessmobiles,Please enter Number of Business Mobiles.",
						"required,captcha,Please enter the captcha word.",

                ]
	});	


  function myOnComplete()
  {
	  var str = $("#contact-form").serialize(); 
	  $("#rsvErrors").fadeIn(1500, function() {
          $(this).html("<p><img  src='public/images/ajax-loader.gif' /></p>");
        });

	
	
	$.ajax({
      type: "POST",
      url: "/process-quote.php",
      data: str,
      success: function(msg) {
		if(msg == "invalidcaptcha"){
			result = "<h4>Please enter the captcha word correctly</h4>";
			$("#captcha").attr('src', 'captcha.php?'+Math.random());
			$("#rsvErrors").html(result)
			.hide()
			.fadeIn(1500);				
		}
		else{
			result = "<p class='success'> Contact Form Submitted!<br/>We will be in touch soon.</p>";	
			$("#rsvErrors").html(result)
			.hide()
			.fadeIn(1500, function() {
				clearForm($("#contact-form"));
			});				
		}

      }
     });
	
	
    return false;
  }
  
});



function clearForm(form) {
  // iterate over all of the inputs for the form
  // element that was passed in
  $(':input', form).each(function() {
 var type = this.type;
 var tag = this.tagName.toLowerCase(); // normalize case
 // it's ok to reset the value attr of text inputs,
 // password inputs, and textareas
 if (type == 'text' || type == 'password' || tag == 'textarea')
   this.value = "";
 // checkboxes and radios need to have their checked state cleared
 // but should *not* have their 'value' changed
 else if (type == 'checkbox' || type == 'radio')
   this.checked = false;
 // select elements need to have their 'selectedIndex' property set to -1
 // (this works for both single and multiple select elements)
 else if (tag == 'select')
   this.selectedIndex = -1;
  });
};



