function _byID(id)
{
  return document.getElementById(id);
}

function _sleep(ms)
{
  ms *= 10000;
  while(ms>0){ms--;}
}

function configLayer(id, newTitle, newWidth, newHeight){
  var popup = _byID(id);
  with(popup.style){
    width = newWidth+'px';
    height = newHeight+'px';
  }
  _byID(id+'_content').style.height = (newHeight - 60) + 'px';
  if(newTitle!='') _byID(id+'_title').innerHTML = newTitle;
  else _byID(id+'_title').style.display = 'none';

  _byID('screen_fade').style.display = 'block';
  _byID(id).style.display = 'block';

  centerLayer(id);
}

function centerLayer(id){
  iLeft = parseInt(_byID(id).style.left);
  iTop = parseInt(_byID(id).style.top);

  lWidth = parseInt(_byID(id).style.width);
  lHeight = parseInt(_byID(id).style.height);

  if(String(navigator.userAgent).toLowerCase().search(/gecko/)!=-1){
    cWidth = window.innerWidth;
    cHeight = window.innerHeight;

    xOffset = window.pageXOffset;
    yOffset = window.pageYOffset;
  }else{
    cWidth = document.body.clientWidth;
    cHeight = document.body.clientHeight;

    xOffset = document.body.scrollLeft;
    yOffset = document.body.scrollTop;
  }

  _byID('screen_fade').style.height = document.body.scrollHeight+'px';

  var lLeft = Math.round((cWidth-lWidth)/2)+xOffset;
  var lTop = Math.round((cHeight-lHeight)/2)+yOffset;

  if(lLeft<0) lLeft = 0;
  if(lTop<0) lTop = 0;

  _byID(id).style.left = lLeft+'px';
  _byID(id).style.top = lTop+'px';
  //slideLayerTo(id, lLeft,lTop);

  return;
}

function slideLayerTo(id, cLeft, cTop){
  new Effect.Move(_byID(id),{x: cLeft, y: cTop, mode: 'absolute'});
}

function fadeLayer(id){
  new Effect.Fade(_byID(id),{duration: '0.3'});
  new Effect.Fade(_byID('screen_fade'),{duration: '0.1'});
  _byID('screen_fade').style.display = 'none';
  _byID(id).style.display = 'none';
}

function reloadAccountSettings(id)
{
  new Ajax.Updater(id, '/cle_dashboard/account_settings', {asynchronous:true, evalScripts:false, onLoading:function(request, json){}, onSuccess:function(request, json){}, method:'get', parameters:'or=1'});
}

function reloadAccountInfobox(id)
{
  new Ajax.Updater(id, '/cle_dashboard/index', {asynchronous:true, evalScripts:false, onLoading:function(request, json){}, onSuccess:function(request, json){}, method:'get', parameters:'or=1&_xx='+id});
}

function tinyMCEit(el)
{

//  tinyMCE.addMCEControl(el, el.id);
  tinyMCE.init({
		mode : "exact",
		elements : el.id,
		theme : "advanced",
		plugins : "table,advhr,advimage,advlink,flash,paste,fullscreen,noneditable,contextmenu,spellchecker",
		theme_advanced_buttons1_add_before : "newdocument,separator",
		theme_advanced_buttons1_add : "fontselect,fontsizeselect",
		theme_advanced_buttons2_add : "separator,forecolor,backcolor,liststyle",
		theme_advanced_buttons2_add_before: "cut,copy,paste,pastetext,pasteword,separator,",
		theme_advanced_buttons3_add_before : "tablecontrols,separator",
		theme_advanced_buttons3_add : "flash,advhr,separator,fullscreen,spellchecker",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		extended_valid_elements : "hr[class|width|size|noshade]",
		file_browser_callback : "fileBrowserCallBack",
		paste_use_dialog : false,
		theme_advanced_resizing : true,
		theme_advanced_resize_horizontal : true,
		apply_source_formatting : true,
		force_br_newlines : true,
		force_p_newlines : false,
		remove_script_host : false,
		relative_urls : false,
		gecko_spellcheck : true,
		content_css: "/css/tinymce_classes.css"
    }); 
}

function selectAddOption(id_value,text_value,value){
  if (_byID(id_value)==undefined) alert(id_value+' is "undefined"');
  option=document.createElement('OPTION');
  _byID(id_value).options.add(option);
  option.text=text_value;
  option.value=value;
}

function selectDeleteOption(id_value,option_index){
  if (_byID(id_value)==undefined) alert(id_value+' is "undefined"');
  _byID(id_value).options[option_index]=null;
}

function selectEmpty(id_value){
  if (_byID(id_value)==undefined) alert(id_value+' is "undefined"');
  while (_byID(id_value).length>0) selectDeleteOption(id_value,0);
}

var oacl_paeprem=new Array(120,158.4,180,216,288);
var oacl_pdf=new Array(.60,.70,.80,.90,1.0);
var oacl_covg = new Array("","$100,000/$300,000","$250,000/$750,000","$500,000/$1,500,000","$1,000,000/$3,000,000","$2,000,000/$4,000,000");

function oacl_show_prem() {
 var sindex=$('cl_sp_years').selectedIndex;
 for(i=1; i<=5; i++)
 {
   if(sindex == 0)
   {
     eval("$('cl_coverage_dp_b"+i+"').value = '';");
     eval("$('cl_coverage_option_b"+i+"').checked = false;");
   }
   else
   {
     eval("$('cl_coverage_dp_b"+i+"').value = '$' + Math.round(oacl_paeprem["+(i-1)+"] * oacl_pdf[sindex - 1]);");
   }
 }
}

function oacl_check_selection(rb) {
  var sindex=$('cl_sp_years').selectedIndex;
  covg_txt = rb.value;
  if (sindex == 0 && (covg_txt.charAt(0) == 'b')) 
  {
    alert("Please select the number of year in practice first...!");
    rb.checked = false;
  }

  covg_index = parseInt(covg_txt.charAt(1));
  $('cl_coverage_selected').value=oacl_covg[covg_index];
}