/**
 * For many controls
 */ 
function enterToTab(e, el){
  if (getKeyCode(e) == 13){
    doTab(el);    
  }
}
 
function doTab(el){
  el.focus();
  if (el.id && el.form){  
    foundme = false;
    for (i = 0; i < el.form.elements.length; i++){
      if (el.form.elements[i].id){
        if (foundme){
          if (
            el.form.elements[i].type != "hidden" &&
            !el.form.elements[i].disabled
          ){
            el.form.elements[i].focus();
            if (el.form.elements[i].type == "text"){
              el.form.elements[i].select();          
            }
            break;
          }
        }
        if (!foundme){
          foundme = (el.form.elements[i].id == el.id);
        }
      }
    }
  }
}

function submitThisForm(aFormElement) {
  aFormElement.form.submit(); 
}

function eolsubmit(aForm, ajax) {
  // SG: Temp hack for YUI Datatable
  if (typeof requeryYUIDataTable == 'function') {
    return requeryYUIDataTable();
  }
  // End hack
  if (typeof aForm != 'undefined'){
    var bla = true;
    if (aForm.onsubmit){
      bla = aForm.onsubmit();
    }
    // bla can be undefined if onsubmit does not return a value
    if (bla == false) {
      return;
    }
    if (ajax) {
      doUpdateRequest(getFormQueryString());
    } else {
      aForm.submit();
    }
  }
}

function eolconfirm(key, vars) {
  var translated = eoltrans[key];
  if(vars instanceof Array) {
    for (i=0;i<vars.length;i++) {
      translated = translated.replace('$'+i,vars[i]);
    }
  }
  return confirm(translated);
}

/**
 * For saving the form after adding a related item
 */
function saveForm(aFormName){
  if (eval(document.forms[aFormName])) {
    document.forms[aFormName].submit();
  }
}

var formname = null;

function checkChanges() {
  if (formname == null || !document.forms[formname]) {
    //alert('formname not set, wont work');
    return true;
  }
  if (typeof handleAllCheckBoxControls != 'undefined') {
    handleAllCheckBoxControls();
  }

  var changed = document.forms[formname].eol_changed;
  var applied = document.forms[formname].eol_applied;

  if (changed != null && applied != null) {
    if (changed.value != '0' && applied.value == '0') {
      return confirm(eoltrans['unsaved changes']);
    }
  }
  el = document.getElementById('md_detail');
  if (el) {
    if (frames[0].window.formname == null) {
      return true;
    }
    var aForm = frames[0].document.forms[frames[0].window.formname];
    if (!aForm) {
      return true;
    }   
    changed = aForm.eol_changed;
    applied = aForm.eol_applied;
    if (changed != null && applied != null) {
      if (changed.value != '0' && applied.value == '0') {
        return confirm(eoltrans['unsaved changes']);
      }
    }
  }
  return true;
}

function checkUnLoad() {
  if (formname == null || !document.forms[formname]) {
    return;
  }
  var changed = document.forms[formname].eol_changed;
  var applied = document.forms[formname].eol_applied;
  if (changed == null || applied == null) {
    return;
  }
  if (changed.value != '0' && applied.value == '0') {
    return eoltrans['unsaved changes'];
  }
  return;  
}

function setEOLChanged(fname) {
  if (document.forms[fname]) {
    document.forms[fname].eol_changed.value=1
  }
  if (document.forms[formname]) {
    document.forms[formname].eol_changed.value=1
  }
}

function limitText(limitField, limitNum) {
  if (limitField.value.length > limitNum) {
    limitField.value = limitField.value.substring(0, limitNum);
    alert(eoltrans['maximum is']+' '+limitNum+' '+eoltrans['characters']);
  }
}

function limitTextWords(field, maxlimit) {
	var wordcounter=0;
	var i;
	
	for (i=0;i<field.value.length;i++) {
		//alert(i + ' - ' + maxlimit);
	    if (field.value.charAt(i) == " " && field.value.charAt(i-1) != " ")  {
	      wordcounter++;
	    }  // Counts the spaces while ignoring double spaces, usually one in between each word.
	    if (wordcounter >= maxlimit) {
	      field.value = field.value.substring(0, i);
	      alert(eoltrans['maximum is']+' '+maxlimit+' '+eoltrans['words']);
	      return;
	    }
	}
}


