var g_administration={selectUser:function(user_id)
{g_common.updateDiv("user_form_container","/administration/select_user","user_id="+user_id);},onChannelSelect:function(val)
{g_common.updateDiv("call_center_agent_container","/administration/get_call_center_select","channel_id="+val);}}
var g_customer_management={clearEnrollmentErrors:function(enrollmentIDs)
{g_common.updateServer("/customer_management/clear_enrollment_errors","enrollments="+enrollmentIDs);},removeContractWarning:function(contract,urlAction)
{var result=confirm("This action will delete '"+contract+".pdf' from storage.\n\nClick OK if you wish to proceed with the deletion and upload\na different scanned contract, otherwise, click Cancel.");if(result)
{g_common.updateDiv('contract_image_detail',urlAction);}},resendContractConfirmation:function(customer_id,contract_id)
{g_common.updateServer("/customer_management/resend_contract_confirmation","customer_id="+customer_id+"&contract_id="+contract_id,"g_common.onSelectDisable('1', '0', 'resend_confirmation_button');");}}
var g_enrollment={numpops:0,addNote:function()
{this.hidePopup("notes_popup");},addPremise:function()
{var x=$('premise_premise_number');if(x){x.value="";if(x.hasAttribute&&x.hasAttribute("disabled"))
{x.removeAttribute("disabled");}}
tarr=$A(['premise_id','premise_premise_number','premise_name']);tarr.each(function(tid){x=$(tid);if(x){x.value="";}});x=$('premise_contract_id');var y=$('contract_id');if(x&&y)
{x.value=y.value;}
var apb=$("add_premise_button");if(apb)
{apb.value="Add";}
this.displayPopup('premise_popup');},displayPopup:function(popup)
{var elem=$(popup);if(elem)
{this.numpops++;elem.style.zIndex=""+(10*this.numpops);elem.show();}},hidePopup:function(popup)
{var elem=$(popup);if(elem)
{this.numpops--;elem.hide();elem.style.zIndex="1";}},enableControlIfChecked:function(parentCheckbox,ctrlName)
{var enable=parentCheckbox&&parentCheckbox.checked;var ctrl=$(ctrlName);if(ctrl)
{ctrl.disabled=!enable;}},enableControlIfMultipleChecked:function(checkbox1,checkbox2,checkbox3,ctrlName)
{var enable=$(checkbox1)&&$(checkbox2)&&$(checkbox3)&&$(checkbox1).checked&&$(checkbox2).checked&&$(checkbox3).checked;var ctrl=$(ctrlName);if(ctrl)
{ctrl.disabled=!enable;}},enableControlIfBothChecked:function(checkbox1,checkbox2,ctrlName)
{var enable=$(checkbox1)&&$(checkbox2)&&$(checkbox1).checked&&$(checkbox2).checked;var ctrl=$(ctrlName);if(ctrl)
{ctrl.disabled=!enable;}},highlightAdditionalFields:function(fields)
{fieldIDs=fields.split("|");for(var idx=0;idx<fieldIDs.length;idx++)
{field=$(fieldIDs[idx]);if(field)
{Element.extend(field);field.addClassName("highlight");}}},onPermitTypeChange:function(premise_type)
{g_common.onSelectDisable(premise_type,"commercial","company_industry_classification_code,company_operating_name,company_legal_name,premise_name");g_common.onSelectShow(premise_type,"commercial","company_details_block");g_enrollment.update_contract_defaults();},selectAgent:function(channel_id)
{g_common.updateDiv("customer_agent_id_container","/enrollment/select_agent","channel_id="+channel_id);},selectProvinceState:function(country_id)
{g_common.updateDiv("customer_address_province_id_container","/enrollment/select_province_state","country_id="+country_id);},process_premise:function(myform)
{x=$('premise_type_commercial');if(x)
{is_commercial=x.checked;}
else
{is_commercial=false;}
new Ajax.Request('/enrollment/premise',{asynchronous:true,evalScripts:true,onSuccess:function(request)
{x=$('premise_list');if(x)
{x.innerHTML=request.responseText;}
g_enrollment.hidePopup('premise_popup');},onFailure:function(request)
{x=$('premise_popup_form');if(x)
{x.innerHTML=request.responseText;}},parameters:Form.serialize(myform)+"&is_commercial="+is_commercial});},copyMailingAddress:function()
{fields=$A(['flat','house','street','postal_code','town']);fields.each(function(field){from=$("customer_address_"+field);to=$("premise_address_"+field);if(from&&to)
{to.value=from.value;}});},display_premise:function(premiseid)
{if(premiseid)
{var contract_id="";var x=$('contract_id');if(x)
{contract_id=x.value;}
x=$('premise_type_commercial');if(x)
{is_commercial=x.checked;}
else
{is_commercial=false;}
new Ajax.Request('/enrollment/get_premise',{asynchronous:true,evalScripts:true,onSuccess:function(request)
{var x=$('premise_popup_form');if(x)
{x.innerHTML=request.responseText;}
g_enrollment.displayPopup('premise_popup');},onFailure:function(request)
{alert("Unable to load premise:\n"+request.responseText);},parameters:"id="+premiseid+"&contract[id]="+contract_id+"&is_commercial="+is_commercial});}},delete_premise:function(premiseid)
{if(premiseid)
{if(!confirm("Really delete this premise?  Remember, every contract requires at least one premise."))
{return false;}
var x=$('contract_id');contract_id="";if(x)
{contract_id=x.value;}
new Ajax.Request('/enrollment/delete_premise',{asynchronous:true,evalScripts:true,onSuccess:function(request)
{x=$('premise_list');if(x)
{x.innerHTML=request.responseText;}},onFailure:function(request)
{alert("Unable to delete premise:\n"+request.responseText);},parameters:"id="+premiseid+"&contract[id]="+contract_id});}},premise_notes:function(premise_id)
{if(premise_id)
{new Ajax.Request('/enrollment/new_premise_note',{asynchronous:true,evalScripts:true,onSuccess:function(request)
{x=$('notes_popup_form');if(x)
{x.innerHTML=request.responseText;}
g_enrollment.displayPopup("notes_popup");},onFailure:function(request)
{alert("Unable to add note for premise:\n"+request.responseText);},parameters:"id="+premise_id});}},customer_notes:function(customer_id)
{if(customer_id)
{new Ajax.Request('/enrollment/new_customer_note',{asynchronous:true,evalScripts:true,onSuccess:function(request)
{x=$('notes_popup_form');if(x)
{x.innerHTML=request.responseText;}
g_enrollment.displayPopup("notes_popup");},onFailure:function(request)
{alert("Unable to add note for customer:\n"+request.responseText);},parameters:"id="+customer_id});}},go_to_consumption:function(premise_id)
{g_common.updateDiv("consumption_records","/customer_management/consumption_details","premise_id="+premise_id,"g_enrollment.displayPopup('consumption_popup');");},go_to_enrollment_details:function(contract_id)
{g_common.updateDiv("enrollment_records","/customer_management/enrollment_details","contract_id="+contract_id,"g_enrollment.displayPopup('enrollment_popup');");},premise_num_change:function(nprem)
{if(nprem)
{x=$('premise_type_commercial');if(x)
{is_commercial=x.checked;}
else
{is_commercial=false;}
x=$('premise_contract_id');if(x)
{contract_id=x.value;}
else
{contract_id=null;}
new Ajax.Request('/enrollment/get_premise_by_num',{asynchronous:true,evalScripts:true,onSuccess:function(request)
{x=$('premise_popup_form');if(x)
{x.innerHTML=request.responseText;$('premise_contract_id').value=$('contract_id').value;}},parameters:"id="+nprem+"&is_commercial="+is_commercial+"&contract[id]="+contract_id});}},cancel_premise:function(enrollment_id)
{g_common.updateDiv("premise_popup_form",'/enrollment/cancel_premise',"id="+enrollment_id)},drop_premise:function(enrollment_id,reason_id,status_code)
{g_common.updateDiv("premise_popup_form",'/enrollment/drop_premise',"id="+enrollment_id+"&termination_reason_id="+reason_id+"&transition_code="+status_code)},enroll_premise:function(premise_id,contract_id)
{g_common.updateDiv("premise_popup_form",'/enrollment/enroll_premise',"id="+premise_id+"&contract_id="+contract_id)},com_cop_premise:function(premise_id,contract_id)
{g_common.updateDiv("premise_popup_form",'/enrollment/com_cop_premise',"id="+premise_id+"&contract_id="+contract_id)},clear_premise:function()
{$A(["premise_id","premise_name","premise_premise_number",'premise_address_flat','premise_address_house','premise_address_street','premise_address_postal_code','premise_address_town']).each(function(field){var x=$(field);if(x)
{x.value="";}});var x=$('premise_premise_number');if(x)
{x.disabled=null;}
x=$("cleardiv");if(x)
{x.hide();}},update_contract_defaults:function()
{if($('new_contract').value=="true"){premise_type='';if($('premise[type]').value=='residential'){if($('premise[type]').checked){premise_type='residential';}else{premise_type='commercial';}}
else if($('premise[type]').value=='commercial'){if($('premise[type]').checked){premise_type='commercial';}else{premise_type='residential';}}
contract_start=$('contract[start_date]');contract_end=$('contract[end_date]');contract_rate='';if(premise_type=='residential'){contract_start.value=default_start_date;contract_end.value=default_end_date;contract_rate=default_residential_rate;}
else if(premise_type=='commercial'){contract_start.value='';contract_end.value='';contract_rate=default_commercial_rate;}
contract_rate_field=$('contract_rate_id');for(i=0;i<contract_rate_field.length;i=i+1){if(contract_rate_field[i].value==contract_rate){contract_rate_field.selectedIndex=i;}}}},setPrimeRateCheckboxDefault:function(promo_code)
{checked=false;if(promo_code=='80000'){checked=true;}
$('contract_auto_renewal').checked=checked;}}
var g_menu={updateMenuState:function(div)
{var visible=g_common.toggleDisplay(div);var params=div+"="+visible;g_common.updateServer("set_expanded_menu_state",params);}}
var g_notes={show_note:function(note_id)
{if(note_id)
{new Ajax.Request('/enrollment/show_note',{asynchronous:true,evalScripts:true,onSuccess:function(request)
{x=$('notes_popup_form');if(x)
{x.innerHTML=request.responseText;}
g_enrollment.displayPopup("notes_popup");},onFailure:function(request)
{alert("Unable to find note:\n"+request.responseText);},parameters:"id="+note_id});}}}
var g_search={previous_selected_row:"",highlightRow:function(id,bHighlight)
{var divs=$(id).childNodes;for(idx in divs)
{var div=divs[idx];if("DIV"==div.tagName)
{Element.extend(div);if(bHighlight)
{div.addClassName("highlight");}
else
{div.removeClassName("highlight");}}}},selectResult:function(customer_id)
{if(customer_id)
{if(""!=this.previous_selected_row)
{this.highlightRow(this.previous_selected_row,false);}
var current_selected_row="search_"+customer_id;if(current_selected_row!=this.previous_selected_row)
{this.highlightRow(current_selected_row,true);this.previous_selected_row=current_selected_row;g_common.updateDiv("customer_management_enrollment","/customer_management/populate_enrollment","customer_id="+customer_id);}
else
{this.previous_selected_row="";}}},setSearchLabels:function()
{var search_by=$("search_by")
if(search_by)
{switch(search_by.value)
{case"1":case"2":$("search_label1").innerHTML="FortisBC Acct #";$("search_label2").innerHTML="Customer Name";$("search_label3").innerHTML="Customer Address";$("search_label4").innerHTML="&nbsp;";break;case"3":$("search_label1").innerHTML="FortisBC Acct #";$("search_label2").innerHTML="Customer Name";$("search_label3").innerHTML="Premise Number";$("search_label4").innerHTML="Premise Address";break;case"4":$("search_label1").innerHTML="FortisBC Acct #";$("search_label2").innerHTML="Customer Name";$("search_label3").innerHTML="Company Operating Name";$("search_label4").innerHTML="Premise Address";break;case"5":$("search_label1").innerHTML="FortisBC Acct #";$("search_label2").innerHTML="Customer Name";$("search_label3").innerHTML="Customer Address";$("search_label4").innerHTML="Contract Number";break;}}}}
var g_common={displayDiv:function(div,bDisplay)
{var elem=$(div);if(elem)
{if(bDisplay)
{elem.show();}
else
{elem.hide();}}},toggleDisplay:function(div)
{var elem=$(div);if(elem)
{elem.toggle();return elem.visible();}
return false;},onSelectDisable:function(val,condition,element_list)
{var element_array=element_list.split(',');var disabled=(val==condition?null:true);element_array.each(function(element_name)
{var elem=$(element_name);if(elem)
{elem.disabled=disabled;}});},onSelectShow:function(val,condition,element_list)
{var element_array=element_list.split(',');var enabled=(val==condition?true:null);element_array.each(function(element_name)
{var elem=$(element_name);if(elem)
{if(enabled)
{elem.show();}
else
{elem.hide();}}});},updateDiv:function(sectionRefresh,controllerAction,params,success,failure,complete)
{if(typeof(params)=='undefined')var params="";if(typeof(success)=='undefined')var success="";if(typeof(failure)=='undefined')var failure="";if(typeof(complete)=='undefined')var complete="";var func=function(request)
{if(request.status>=200||request.status<300)
{if(typeof(success)=="function")
{success(request);}
else
{eval(success);}}
else
{if(typeof(failure)=="function")
{failure(request);}
else
{eval(failure);}}
if(typeof(complete)=="function")
{complete(request);}
else
{eval(complete);}}
new Ajax.Updater(sectionRefresh,controllerAction,{method:'post',asynchronous:true,evalScripts:true,postBody:params,onComplete:func});},updateServer:function(controllerAction,params,success,failure,complete)
{if(typeof(params)=='undefined')var params="";if(typeof(success)=='undefined')var success="";if(typeof(failure)=='undefined')var failure="";if(typeof(complete)=='undefined')var complete="";var func=function(request)
{if(request.status>=200||request.status<300)
{if(typeof(success)=="function")
{success(request);}
else
{eval(success);}}
else
{if(typeof(failure)=="function")
{failure(request);}
else
{eval(failure);}}
if(typeof(complete)=="function")
{complete(request);}
else
{eval(complete);}}
new Ajax.Request(controllerAction,{method:'get',asynchronous:true,evalScripts:true,parameters:params,onComplete:func});},insertAtCursor:function(fieldName,insertStr)
{field=$(fieldName);if(document.selection){field.focus();sel=document.selection.createRange();sel.text=insertStr;}
else if(field.selectionStart||field.selectionStart=='0'){var startPos=field.selectionStart;var endPos=field.selectionEnd;field.value=field.value.substring(0,startPos)+insertStr+field.value.substring(endPos,field.value.length);}
else{field.value+=insertStr;}}}
var g_grid={selected_row:null,select_row:function(row)
{if(this.selected_row==row)
{this.deselect_row();$('grid_delete').hide();$('grid_add').show();$('grid_save').hide();if("rate"==row.id.split("_")[0])
{$('rate_code').disabled=null;$('rate_rate').disabled=null;}}
else
{hf=$('selected_');if(hf)
{hf.value=row.id;}
if(this.selected_row)
{this.deselect_row();}else{$('grid_delete').show();$('grid_add').hide();$('grid_save').show();}
this.selected_row=row;$(row).addClassName("highlight");var item_type=row.id.split("_").slice(0,-1).join("_");g_common.updateDiv(item_type+"_form",item_type,"id="+row.id,"","","g_grid.selected_row = $('"+row.id+"');");}},deselect_row:function()
{if(this.selected_row)
{$(this.selected_row).removeClassName("highlight");this.selected_row=null;}},call_center_select_row:function(row)
{if(this.selected_row==row)
{this.deselect_row();$('grid_delete').hide();$('grid_add').show();$('grid_save').hide();}
else
{hf=$('selected_');if(hf)
{hf.value=row.id;}
if(this.selected_row)
{this.deselect_row();}else{$('grid_delete').show();$('grid_add').hide();$('grid_save').show();}
this.selected_row=row;$(row).addClassName("highlight");var item_type=row.id.split("_");item_type=item_type[0]+"_"+item_type[1];var div=item_type+"_form";var raction=item_type+"_user";var params="id="+row.id;var success="$('user_agent_id').disabled = null;";var completed="g_grid.selected_row = $('"+row.id+"');";g_common.updateDiv(div,raction,params,"","",completed);}}}
