var Postcodesearch=Class.create();Postcodesearch.prototype={initialize:function(form,byPostcodeUrl,fetchaddressUrl){this.form=form;this.byPostcodeUrl=byPostcodeUrl;this.fetchaddressUrl=fetchaddressUrl;this.onComplete=this.resetLoadWaiting.bindAsEventListener(this);this.containerSuffix='';},_disableEnableAll:function(element,isDisabled){var descendants=element.descendants();for(var k in descendants){descendants[k].disabled=isDisabled;}
element.disabled=isDisabled;},resetLoadWaiting:function(transport){this.setLoadWaiting(false);},setLoadWaiting:function(enable){if(enable){var container=$('postcode-search-container'+this.containerSuffix);container.setStyle({opacity:.5});this._disableEnableAll(container,true);Element.show('postcode-search-please-wait'+this.containerSuffix);}
else{var container=$('postcode-search-container'+this.containerSuffix);container.setStyle({opacity:1});this._disableEnableAll(container,false);Element.hide('postcode-search-please-wait'+this.containerSuffix);}},searchbypostcode:function(){var validator=new Validation(this.form);if(validator.validate()){this.setLoadWaiting(true);var request=new Ajax.Request(this.byPostcodeUrl,{method:'post',onComplete:this.onComplete,onFailure:function(response){alert('An error occurred while processing your request');this.onComplete;},onSuccess:function(response){var addressess=response.responseText;if(addressess==''){$('postcodeaddress_id').options.length=0;$('postcodeaddress_node').hide();$('btn_fetch_address_node').hide();if($('link_shipping_address')){$('link_shipping_address').update('I prefer to enter my address manually');}}
eval("addressess = "+addressess+";");try{if(addressess.length>=1){var present=0;$('postcodeaddress_id').options.length=0;for(var i=0;i<addressess.length;i++){if(addressess[i].description!=''){present=1;option=document.createElement('OPTION');option.value=addressess[i].id;option.text=addressess[i].description;if($('postcodeaddress_id').options.add){$('postcodeaddress_id').options.add(option);}else{$('postcodeaddress_id').appendChild(option);}}}
if(present){$('postcodeaddress_node').show();$('btn_fetch_address_node').show();if($('link_shipping_address')){$('link_shipping_address').update('My address is not included in this list');}}
else{alert('No addresses found for this Postcode.');$('postcodeaddress_node').hide();$('btn_fetch_address_node').hide();}}
else{alert('No addresses found for this Postcode.');$('postcodeaddress_node').hide();$('btn_fetch_address_node').hide();}}
catch(e){alert(e);}},parameters:Form.serialize(this.form)});}},regsearchbypostcode:function(){var validator=new Validation(this.form);if(Validation.validate(document.getElementById('zipcode'))){this.setLoadWaiting(true);var request=new Ajax.Request(this.byPostcodeUrl,{method:'post',onComplete:this.onComplete,onFailure:function(response){alert('An error occurred while processing your request');this.onComplete;},onSuccess:function(response){var addressess=response.responseText;if(addressess==''){$('postcodeaddress_id').options.length=0;$('postcodeaddress_node').hide();$('btn_fetch_address_node').hide();if($('link_shipping_address')){$('link_shipping_address').update('I prefer to enter my address manually');}}
eval("addressess = "+addressess+";");try{if(addressess.length>=1){$('postcodeaddress_id').options.length=0;var present=0;for(var i=0;i<addressess.length;i++){if(addressess[i].description!=''){present=1;option=document.createElement('OPTION');option.value=addressess[i].id;option.text=addressess[i].description;if($('postcodeaddress_id').options.add){$('postcodeaddress_id').options.add(option);}else{$('postcodeaddress_id').appendChild(option);}}}
if(present){$('postcodeaddress_node').show();$('btn_fetch_address_node').show();if($('link_shipping_address')){$('link_shipping_address').update('My address is not included in this list');}}
else{alert('No addresses found for this Postcode.');$('postcodeaddress_node').hide();$('btn_fetch_address_node').hide();}}
else{alert('No addresses found for this Postcode.');$('postcodeaddress_node').hide();$('btn_fetch_address_node').hide();}}
catch(e){alert(e);}},parameters:Form.serialize(this.form)});}},fetchaddress:function(){var addressid=$F('postcodeaddress_id');this.containerSuffix='-address';if(addressid==null){alert('Please select address');return false;}
this.setLoadWaiting(true);var request=new Ajax.Request(this.fetchaddressUrl,{method:'post',onComplete:this.onComplete,onFailure:function(response){alert('An error occurred while processing your request');this.onComplete;},onSuccess:this.updateAddress.bind(this),parameters:{addressid:addressid}});},updateAddress:function(transport){$('shipping_address_autofill').hide();$('shipping_address_manually').show();result=transport.responseText.evalJSON();street1=$('street1')||$('street_1');street2=$('street2')||$('street_2');if(result.line3==''){street1.value=result.LINE1;street2.value=result.LINE2;}else{street1.value=result.LINE1;if(result.LINE2){street1.value+=', '+result.LINE2;}
if(result.LINE3){street2.value=result.LINE3}
if(result.LINE4){street2.value+=', '+result.LINE4;}}
$('city').value=result.POST_TOWN;postcode=$('postcode')||$('zip');postcode.value=result.POSTCODE;country=$('country_id')||$('country');country.value='GB';$('region_id').hide();if(typeof(result.COUNTY)!='undefined'){$('region').value=result.COUNTY;}
else{$('region').value='';}
$('region').show();}}
Validation.addAllThese([['validate-cc-type','Credit card number doesn\'t match credit card type',function(v,elm){elm.value=removeDelimiters(elm.value);v=removeDelimiters(v);var ccTypeContainer=$(elm.id.substr(0,elm.id.indexOf('_cc_number'))+'_cc_type');if(!ccTypeContainer){return true;}
var ccType=ccTypeContainer.value;if(typeof Validation.creditCartTypes.get(ccType)=='undefined'){return false;}
if(Validation.creditCartTypes.get(ccType)[0]==false){return true;}
var ccMatchedType='';Validation.creditCartTypes.each(function(pair){if(pair.value[0]&&v.match(pair.value[0])&&ccType==pair.key){ccMatchedType=pair.key;throw $break;}});if(ccMatchedType!=ccType){return false;}
if(ccTypeContainer.hasClassName('validation-failed')&&Validation.isOnChange){Validation.validate(ccTypeContainer);}
return true;}]]);Validation.creditCartTypes=$H({'UKMC':[new RegExp('^((6759[0-9]{12})|(49[013][1356][0-9]{13})|(633[34][0-9]{12})|(633110[0-9]{10})|(564182[0-9]{10}))([0-9]{2,3})?$'),new RegExp('^([0-9]{3}|[0-9]{4})?$'),true],'VE':[new RegExp('^4[1589][0-9]{14}$'),new RegExp('^[0-9]{3}$'),true],'VD':[new RegExp('^4.{15}$'),new RegExp('^[0-9]{3}$'),true],'VI':[new RegExp('^4[0-9]{12}([0-9]{3})?$'),new RegExp('^[0-9]{3}$'),true],'MC':[new RegExp('^5[1-5][0-9]{14}$'),new RegExp('^[0-9]{3}$'),true],'AE':[new RegExp('^3[47][0-9]{13}$'),new RegExp('^[0-9]{4}$'),true],'DI':[new RegExp('^6011[0-9]{12}$'),new RegExp('^[0-9]{3}$'),true],'SS':[new RegExp('^((6759[0-9]{12})|(49[013][1356][0-9]{13})|(633[34][0-9]{12})|(633110[0-9]{10})|(564182[0-9]{10}))([0-9]{2,3})?$'),new RegExp('^([0-9]{3}|[0-9]{4})?$'),true],'SW':[new RegExp('^[456].{15}$|^[456].{17,18}$'),new RegExp('^([0-9]{3}|[0-9]{4})?$'),true],'OT':[false,new RegExp('^([0-9]{3}|[0-9]{4})?$'),false]});
