/**
*Utility object to help in display of meesages on 
*/

function JMessageUtil(){};

/*-- Presentation of errors in response to feedback from the validation object --*/
//Hide and clear error messages
JMessageUtil.hideError  = function(_msg_elem){
  _msg_elem.className = 'hide';
  _msg_elem.innerHTML = '';  
}

//Toggle error message that appear next to fields
JMessageUtil.toggleInlineError  = function(_msg_elem,_state,oValidation){
  _msg_elem.className = (_state)?'msgbox error':'hide';
  _msg_elem.innerHTML = (_state)?oValidation.getErrorDescription(_state):'';  
}

//Toggle error message that appears at top of page
JMessageUtil.toggleMainError = function(oValidation){
  oValidation.main_msg_box.className = (oValidation.hasErrors())?'msgbox error':'hide';
  oValidation.main_msg_box.innerHTML = (oValidation.hasErrors())?oValidation.getErrorDescription('main'):'';
}

//Hide error message that appears at top of page
JMessageUtil.hideMainError = function(oValidation){
  if(!oValidation.hasErrors()){
    oValidation.main_msg_box.className = 'hide';
    oValidation.main_msg_box.innerHTML = ''; 
  }
}

//Focus UI on first form element with an associated error stored in the validation object
//strategy - sort elements on their tabIndex property
JMessageUtil.focusFirstError = function(error_hash){
  var _firstElem=false;
  for(var i in error_hash){
    _curElem = document.getElementById(i);
    if(!_firstElem){
      _firstElem = _curElem;
      continue;
    }
    _firstElem = (_curElem.tabIndex < _firstElem.tabIndex)?_curElem:_firstElem; 
    
  }
  
  if(_firstElem){
    _firstElem.focus(); 
  }
}


//Focus UI on first form element of an error
JMessageUtil.focusFirstErrorByOrder = function(error_hash, error_order) {
  //go through order list and see if there is error on that field
  for (var i in error_order) {
    var id = error_order[i];

    if (error_hash[id]) { //found error
      var obj = document.getElementById(id);
      if (obj) {
        obj.focus();
      }
      break;
    }
  }
}
