function popUp (sURL, sName, x, y, dx, dy) {
  if(!sURL) return;
  window.open(sURL, sName, 'left='+x+',top='+y+',width='+dx+',height='+dy+',scrollbars=1,resizable=1');
}

function GoURL(s){
  document.location = s;
}

function Add2Fav(){
  if (document.all)
    window.external.AddFavorite(document.location.href,document.title)
}

function OpenPreview(sURL) {
  if(!sURL) return;
  var w_s = 50;
  var h_s = 50;
  var w = window.screen.width;
  var h = window.screen.height;
  popUp(sURL,'PreviewWin', w_s, h_s, w-2*w_s, h-2*h_s-50);
}

function OpenImages(id,f){
  OpenPreview('index.php?page=images&id='+id+'&f='+f);
}

function hwReloadCat(page,cid,hwJsCats){

  var catbox = document.frm.cid;
  var new_cid = catbox.options[catbox.selectedIndex].value;
  var f1,f2,s1,s2;
  s1 = '|' + new_cid + '|';
  s2 = '|' + cid + '|';
  
  for (i = 0; i < hwJsCats.length; i++) {
    f1 = hwJsCats[i].indexOf(s1) != -1;
    f2 = hwJsCats[i].indexOf(s2) != -1;
    if(f1 && f2) return;
    if(f1 || f2) break;
  }
  
  var ch_sep = page.indexOf('?') != -1 ? '&' : '?';  
  document.location = page + ch_sep + 'cid=' + new_cid;
}

function txtCount(f, countf, max) {
  
  if(isNaN(max)) return txtCountW(f, countf, max);

  if (f.value.length > max) f.value = f.value.substring(0, max);
  else countf.value = max - f.value.length;

}

var space_flag = true;

function txtCountW(f, countf, max) {
  
  max = parseInt(max);
  var words = new Array();
  
  re = new RegExp('\\s');
  words = f.value.split(re);

  // delete empty elements
  var words_full = new Array();
  for(i=0;i<words.length;i++) {
    if (words[i]!="") words_full.push(words[i]);
  }
  words = words_full;
  
  if (words.length >= max) {
    for(i=max;i<words.length;i++) words.pop();
    f.value = words.join(" ");
    countf.value = 0;
  }
  else {
    if ((f.value.indexOf("\n")!=-1) && (words.length==1) && space_flag) {
      f.value = words[0]+" \n";
      space_flag = false;
    }
    countf.value = max - words.length;
  }
  if (f.value.indexOf(" ")==-1){ 
    countf.value = f.value ? max-1 : max;
  }
  if (f.value.length==0) space_flag = true;
}

function hwGetMenuValue(ctl){
  ctl = eval('document.' + ctl);
  return ctl.options[ctl.selectedIndex].value;
}

function SwitchItemsAdm (block) {
  if(!block) block = 'chk_id';
  var on = document.frm.chk_all.checked;
  var el = document.frm.elements;
  for (var i=0; i<el.length; i++) {
    if (el[i].name.substr(0,block.length)== block) el[i].checked=on;
  }
}
function toogleValue(varId,a,b){
    if($F(varId)==a){
        $(varId).value = b;
    }else{
        $(varId).value = a;
    }
        
}
function LookupUserAdm(ctl){
  popUp('index.php?page=lookup_uid&tg='+escape(ctl),'LookupUID', 50, 50, 400, window.screen.height-150);
}

function hwGetElementById(id){
  if (document.getElementById) return (document.getElementById(id));
  else if (document.all) return (document.all[id]);
  else if ((navigator.appname.indexOf("Netscape") != -1) && parseInt(navigator.appversion == 4)) return (document.layers[id]);
}

function hwShowPass(ctl){
  alert(eval('document.frm.'+ctl).value);
}

function SwitchBlock(Id, bShow) {
  var el;
  if(document.getElementById) el = document.getElementById(Id);
  else { el = document.all ? document.all[Id] : new Object(); }
  if(!el) return;
  if(el.style) el = el.style;
  if( typeof( el.display ) == 'undefined' && !( window.ScriptEngine && ScriptEngine().indexOf( 'InScript' ) + 1 ) ) { alert('Browser Error!'); return; }
  el.display = bShow ? 'block' : 'none';
}

wFORMS.behaviors['validation'].showAlert = function (nbTotalErrors) { 
var placeHolder = document.getElementById('alertMessagePlaceHolder'); 
placeHolder.innerHTML = wFORMS.behaviors['validation'].errMsg_notification.replace('%%',nbTotalErrors); 
placeHolder.style.display = 'block'; 
}    

var openBlind = 0;
function takeMeTo(){
  if(openBlind==0){
      Effect.BlindUp('d2');
      Effect.BlindDown('d1');
      Effect.BlindDown('d3');
      openBlind = 1;
  }else{       
      Effect.SwitchOff('d3');   
      Effect.BlindUp('d1');
      Effect.BlindDown('d2');
      openBlind = 0;
  }  
}
function returnBlank(contentArea){
  $(contentArea).update();
}
function returnStatus(contentArea){
  $(contentArea).update('<div style="text-align:left;vertical-align:middle;"><img src="/images/status.gif"></div>');
  //Lightbox.showBoxByID('progress_bar');
}
function returnModalContent(url,modTitle,modWidth,modHeight) {
    Modalbox.show(url, {title: modTitle, width: modWidth});
    Modalbox.resizeToContent();
}
function lightBoxFormSubmit(form_id){
    //alert(form_id);
    silentFormSubmit(form_id);
    Lightbox.hideBox();
}
function silentFormSubmit(form_id){
$(form_id).request({   onComplete: function(){ alert('Form data saved!') } })
}
function returnContent(url,contentArea,vars,anchor) {
    var params = vars;
    returnStatus(contentArea);
    if(url==''){
      url = 'index1.php';
    }
    var ajax = new Ajax.Updater(
    {success: contentArea},
    url,
    {method: 'get', parameters: params, onFailure: reportError,evalScripts: true});
    $(anchor).scrollTo();
}
function returnPaymentContent(role_id,addVars,mod){
  var url = 'index1.php';
  var vars = 'mod='+mod+'&page=get_field&HW_WRAP=0&topNavOff=1' + addVars;
  if(role_id==50){
      returnContent(url,'totalContent',vars + '&fieldName=payment_total_owed');
      returnContent(url,'addPayContent',vars + '&fieldName=payment_add_info');
      returnContent(url,'toe_impound',vars + '&fieldName=getTPL&tpl=toe_impound_mort&toe_impound=1');
  }else{
      returnBlank('totalContent');
      returnBlank('addPayContent');
      returnBlank('toe_impound');
  }
  returnContent(url,'descContent',vars + '&fieldName=descSelect&descType='+role_id);   
}
function reportError(request) {
	alert('Sorry. There was an error.');
}
function valCheckBoxes(formName) {
  var form=$(formName);
  var result=false;
  var i=form.getElements('checkbox');
  i.each(function(item) {
    if (item.checked && item.type=='checkbox' && item.name!='checkall'){
      result=true;
    }
  });
  return result;
}
function checkUncheckAll(theElement) {
  var theForm = theElement.form, z = 0;
  for(z=0; z<theForm.length;z++){
    if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall'){
      theForm[z].checked = theElement.checked;
    }
  }
}
/* Code for the drag class not needed at this point with the light box
var domTT_styleClass = 'domTTOverlib';
var domTT_maxWidth = false;
try
{
    var winCloseButton = document.createElement('img');
    winCloseButton.src = '/images/close.gif';
	winCloseButton.style.verticalAlign = 'bottom';
}
// permission denied
catch (e)
{
    var winCloseButton = 'X';
}
*/
//lightbox code
Event.observe(window, 'load', init, false);		
function init() {
	Lightbox.init();
}

function valDocMangerForm(){
  if(!valCheckBoxes('exportDocs')){
    alert("You must select a document to export");
    return false;
  }
  if($F('exportType')==''){
    alert("You must select an export type");
    return false;
  } 
  return true;
}

