/*
 * YellowPages (NCE Subsea Directory)
 * Copyright(c) 2008-2009, MaritimeColours as.
 * 
 * Developed by Stefan Bruvik.
 */


YellowPages.InfoBox=function(){var messageDivName='msg-div';var mainPanelName='mainPanel';function createBox(title,message,cls,icon){return['<div id="msg-div" class="msg-div ',cls,'">','<img style="float:left; padding-right: 5px;" src="',YellowPages.baseWebUrl,'/img/sprites/',icon,'"/>','<h3>',title,'</h3>','<div id="msg-div-close" style="float: right; margin-top: -14px; text-decoration: underline;">','hide','</div>','<div class="msg-div-text">',message,'</div>','</div>'].join('');}
return{title:'Title',message:'Message',show:function(options){options=options||{};var title,message,cls,icon,animate=null;title=typeof options.title!=='undefined'?options.title:this.title;message=typeof options.message!=='undefined'?options.message:this.message;animate=typeof options.animate!=='undefined'?options.animate:false;cls='msg-div-ok';icon='accept.png';if(typeof options.type!=='undefined'){switch(options.type){case'error':cls='msg-div-error';icon='error.png';break;case'info':cls='msg-div-info';icon='information.png';break;case'notice':cls='msg-div-notice';icon='notice.png';break;}}
this.title=title;this.message=message;var mainPanel=Ext.get(mainPanelName);if(!mainPanel){mainPanel=Ext.getBody().insertFirst({id:mainPanel,tag:'div'});}
if(mainPanel){var message=String.format(message);var messageDiv=Ext.get(messageDivName);if(messageDiv){messageDiv.remove();messageDiv=Ext.DomHelper.insertBefore(mainPanel,{html:createBox(this.title,this.message,cls,icon)},true);messageDiv.frame();}else{messageDiv=Ext.DomHelper.insertBefore(mainPanel,{html:createBox(this.title,this.message,cls,icon)},true);animate?messageDiv.slideIn():messageDiv.show();}
messageDiv.on({'click':function(){if(animate){this.switchOff({remove:true});}else{messageDiv.remove();}},scope:messageDiv});}},hide:function(animate){animate=animate||false;var messageDiv=Ext.get(messageDivName);if(messageDiv){if(animate){messageDiv.switchOff({remove:true});}else{messageDiv.remove();}}}};}();

YellowPages.web=function(){return{requestForm:null,populateSubcategorySelect:function(){var selectedCategoryID=parseInt(this.dom.value);var subcategorySelect=Ext.get('search-subcategory');subcategorySelect.dom.options.length=0;subcategorySelect.dom.options.add(new Option('All',0));Ext.each(YellowPages.subcategories,function(item,index,all){if(item.Subcategory.category_id==selectedCategoryID){if(item.Subcategory.id==YellowPages.selectedSubcategoryID){subcategorySelect.dom.options.add(new Option(item.Subcategory.name,item.Subcategory.id,true,true));}else{subcategorySelect.dom.options.add(new Option(item.Subcategory.name,item.Subcategory.id));}}});subcategorySelect.highlight('ffff9c',{attr:'color',duration:.2});},init:function(){Ext.QuickTips.init();var categorySelect=Ext.get('search-category');categorySelect.on({'change':this.populateSubcategorySelect,scope:categorySelect});this.populateSubcategorySelect.createDelegate(categorySelect)();var resultsCount=Ext.get('search-result-count');if(resultsCount){resultsCount.update(String.format('{0} results found',YellowPages.resultsCount));}
var requestChange=Ext.get('request-change');if(requestChange){requestChange.on({'click':function(event){event.preventDefault();var confirm=Ext.get('request-change-confirm');confirm.setLeft(requestChange.getLeft());confirm.setTop(requestChange.getTop()+20);if(confirm.hasClass('x-hidden')){confirm.removeClass('x-hidden');if(!this.requestForm){this.requestForm=new Ext.form.FormPanel({url:YellowPages.baseWebUrl+'/companies/requestChange/'+YellowPages.currentID,frame:false,border:true,bodyStyle:'padding: 5px',itemCls:'padding: 5px',renderTo:confirm,items:[{xtype:'panel',html:'In order to change information in this registration,<br/>you need approval from your organizations appointed person of contact for this database.<br/><br/>If you would like to change information for this registration, please submit a request.<br/>It will be forwarded to the database administrator,<br/>who will request permission to change information in accordance with your description to your organizations appointed person of contact.<br/><br/>',border:false,bodyStyle:'padding-bottom: 5px'},{id:'form-email',xtype:'textfield',fieldLabel:'Your e-mail',allowBlank:false,vtype:'email'},{id:'submit-button',xtype:'button',text:'Submit request'}]});Ext.getCmp('submit-button').on({'click':this.requestChange,scope:this});}
confirm.fadeIn({stopFx:true});Ext.getCmp('form-email').focus();}else{confirm.fadeOut({stopFx:true,callback:function(){confirm.addClass('x-hidden');}});}},scope:this});}},requestChange:function(){this.requestForm.form.submit({url:YellowPages.baseWebUrl+'/companies/requestChange/'+YellowPages.currentID,success:this.onSuccess,failure:this.onFailure,waitMsg:'Submitting request',scope:this});},onFailure:function(form,action){if(action.result){Ext.Msg.show({msg:action.result.smtpErrors,title:'An error occurred',icon:Ext.MessageBox.ERROR,buttons:Ext.MessageBox.OK});}},onSuccess:function(form,action){var company=action.result.company.Company;var confirm=Ext.get('request-change-confirm')
confirm.fadeOut({callback:function(){confirm.addClass('x-hidden');}});var requestChange=Ext.get('request-change-wrap');requestChange.removeClass('sprite-information');requestChange.addClass('request-change-sent');requestChange.update(String.format('Your request to change information registered for {0} has been successfully forwarded to {1}',company.name,company.contact));}}}();Ext.onReady(YellowPages.web.init,YellowPages.web);Ext.apply(Ext.form.VTypes,{url2:function(v){var url=/(([\-\w]+\.)+\w{2,3}(\/[%\-\w]+(\.\w{2,})?)*(([\w\-\.\?\\\/+@&#;`~=%!]*)(\.\w{2,})?)*\/?)/i;return url.test(v);},IPAddressText:'Must be a numeric IP address'});
