$(document).ready(function(){var contactUsForm=new ContactUsForm($('.contact-us-form:first'));contactUsForm.show();});function ContactUsForm(container){this.show=function(){container.show();}
this.hide=function(){container.hide();}
function disable(){containerForm.find('input,select,textarea').attr('disabled','disabled');container.find('.ajax-busy:first').show();}
this.enable=function(){container.find('.ajax-busy:first').hide();containerForm.find('input,select,textarea').attr('disabled','');}
this.reset=function(){validator.resetForm();containerForm.find('input[type=text],select').val('');containerForm.find('textarea').val('');}
function showSuccess(){container.find('.success').fadeIn('slow');}
function hideSuccess(){container.find('.success').fadeOut('slow');}
this.onFocus=function(){hideSuccess();}
this.submit=function(){disable();$.get('/_ajax/contact-us-form.json.php',{type:'submit',data:$.toJSON(this.getValues())},function(data){try{callback.submitResult($.secureEvalJSON(data));}catch(err){alert("Error:\n"+err+"\n\nData:\n"+data);callback.reset();callback.enable();}});}
this.submitResult=function(data){if(typeof(data)!='object')data={success:0};if(data.success==1){this.reset();showSuccess();}else{if(typeof(data.faults)=='object'){validator.showErrors(data.faults);}}
this.enable();}
this.getValues=function(){var data={email:containerForm.find('input[name=email][type=text]:first').val(),name:containerForm.find('input[name=name][type=text]:first').val(),tel:containerForm.find('input[name=tel][type=text]:first').val(),comment:containerForm.find('textarea[name=comment]:first').val(),refered_by:containerForm.find('select[name="refered_by"]:first').val()};return data;}
var callback=this;var containerForm=container.find('form:first');var validator=containerForm.validate({submitHandler:function(form){callback.submit();},rules:{name:{required:true},email:{email:true,required:true}},messages:{email:'Your email address',name:'Your name'},onfocusout:false,onkeyup:false,errorClass:"form-error",errorElement:"label",wrapper:"p",errorLabelContainer:container.find('div.form-error-container:first')});containerForm.find('input[name=reset][type=button]:first').bind('click',function(){callback.reset();});container.find('input[type=text],select').bind('focus',function(){callback.onFocus();});}