(function(){var REMEMBER_ME_KEY='apn-remember-me',REMEMBER_ME_OPTIONS={expires:(365*2)};APN.EnquiryCard=(function(){var _this=new Object(),_card,_form,_send,_remember,_location_au,_location_nz,_location_other,_captcha_img,_sending=false,_selectors={card:'#enquiry_card',products:'#enquiry_card #products .item',form:{element:'#enquiry_card_form',remember:'#enquiry_card #remember_me',send:'#enquiry_card .send a'},locations:{all:'.location',in_use:'.location.in_use',select:'#country',au:'#location_au',nz:'#location_nz',other:'#location_other'},captcha:{image:'#captcha_img'}};return $.extend(_this,{init:function(){_card=$(_selectors.card);_form=$(_selectors.form.element);_send=$(_selectors.form.send);_remember=$(_selectors.form.remember);_location_au=$(_selectors.locations.au);_location_nz=$(_selectors.locations.nz);_location_other=$(_selectors.locations.other);_captcha_img=$(_selectors.captcha.image);if(APN.Cookie.hasData(REMEMBER_ME_KEY))_this.populateForm();_this.binds()},binds:function(){$(_selectors.locations.select).live('change',function(){var val=$(this).val();if(val=='Australia')_location_au.addClass('in_use').find('#country').val(val);else _location_au.removeClass('in_use');if(val=='New Zealand')_location_nz.addClass('in_use').find('#country').val(val);else _location_nz.removeClass('in_use');if(val!='Australia'&&val!='New Zealand')_location_other.addClass('in_use').find('#country').val(val);else _location_other.removeClass('in_use')});$(_selectors.locations.in_use).trigger('change');_form.bind('submit',function(e){e.preventDefault();_this.submit()});_send.click(function(e){e.preventDefault();_form.submit()});_form.find('.checkbox_text').click(function(e){e.preventDefault();var checkbox=$(this).parent('.checkbox_option').find('input[type=checkbox]')[0];checkbox.checked=!(checkbox.checked)})},getCheckedProducts:function(){var products=new Array();$(_selectors.products).each(function(){var product=$(this);if(product.find('input:checked').length)products.push({id:product.find('#id').val()})});return products},submit:function(){if(_sending)return;_sending=true;$(_selectors.locations.all).each(function(){var location=$(this);if(!location.hasClass('in_use'))location.data('contents',location.html()).html('')});var _dataObject={},_data,input=[_selectors.form.element+' input[type=checkbox]:checked',_selectors.form.element+' input[type=text]',_selectors.form.element+' textarea',_selectors.form.element+' select'],inputs=input.join(', ');$(inputs).each(function(){var input=$(this);_dataObject[input.attr('id')]=input.val()});_data=$.param(_dataObject);$.each(_this.getCheckedProducts(),function(){_data+='&'+$.param({'product_id[]':this.id})});_send.addClass('sending');$.ajax({url:'/enquiry/send',type:'post',data:_data,dataType:'json',complete:function(){_send.removeClass('sending');_sending=false;$(_selectors.locations.all).each(function(){var location=$(this);if(!location.hasClass('in_use'))location.html(location.data('contents'))})},success:function(response){if(response.sent){_card.find('#products,#form').hide();_card.find('#thankyou').show();if(_remember.is(':checked')){delete _dataObject.captcha;APN.Cookie.setData(REMEMBER_ME_KEY,_dataObject,REMEMBER_ME_OPTIONS)}else APN.Cookie.deleteData(REMEMBER_ME_KEY);try{_pageTracker._trackPageview('/SendEnquiry')}catch(err){};APN.ProductSelect.reset()}else if(response.errors)_this.addErrors(response.messages,_form)}})},error:function(text){return $('<div />').addClass('error').text(text)},addErrors:function(messages,$form){var errorFields=[],currentErrors={};$.each(messages,function(field,validator){errorFields.push(field)});$('.error',$form).each(function(){var $this=$(this),field=$this.siblings('input, select').attr('id');if($.inArray(field,errorFields)==-1){$this.slideUp('fast',function(){$(this).remove()});$this.parents('div.field').children('label.field_error').removeClass('field_error')}else currentErrors[field]={obj:$this,text:$this.text()}});$.each(messages,function(field,validator){var $element=$('#'+field,$form),$error,text;$.each(validator,function(name,message){text=message;return false});if(currentErrors[field]==undefined){$error=_this.error(text).hide();$element.parents('div.control').append($error).parents('div.field').children('label').addClass('field_error');$error.slideDown('slow')}else if(currentErrors[field].text!=text)currentErrors[field].obj.wrapInner('<div />').children('div').fadeOut('fast',function(){$(this).text(text).fadeIn('slow',function(){$(this).parent('.error').text(text).children('div').remove()})});if($element.hasClass('captcha_input'))$element.val('')})},populateForm:function(){var data=APN.Cookie.getData(REMEMBER_ME_KEY);$.each(data,function(key,value){var input=$('#'+key,_form);if(input.is('input[type=checkbox]'))input[0].checked=true;else input.val(value)})}})})();$(function(){APN.EnquiryCard.init()})})(jQuery)