
function checkLength(x,y) {
  if (y.length==x.maxLength) {
    var form = x.form;
    var next = getFormItemPosition(x.id);
    if (++next < form.length) {
      form.elements[next].focus();
    }
  }
};

function enableMultiSelectFields(formelement) {
  var i = 0;
  for (i = 0; i < formelement.length; i++) {
    if (formelement.elements[i].type == 'select-multiple') {
      if (formelement.elements[i].disabled == true) {
	formelement.elements[i].disabled = false;
      }
    }
  }
};

function getFormItemPosition(id) {
  var item = document.getElementById(id);
  if (item) {
    var form = item.form;
    var i = 0;
    for (i = 0; i < form.length; i++) {
      if (form.elements[i].id == id)
	return i;
    }
  }
  return false;
};

function isArray(obj) {
  return obj.constructor == Array;
};

function checkboxChecking(elementId, check) {
  var holder = document.getElementById(elementId);
  if (!holder) {
    return;
  }

  var nodes = holder.getElementsByTagName('input');

  for ( i=0; i < nodes.length; i++ ) {
    if (nodes[i].type == 'checkbox') {
      nodes[i].checked = check;
    }
  }
};

// ##### SELECTNWCFIELD FUNCTIONS ##### 

function appendValueToParent(cname, retval) {
  opener.document.getElementById(cname).value = retval;
  window.close();
};

function appendLabelToParent(cname, retval, retlabel) {
  opener.document.getElementById(cname).text = retlabel;
  window.close();
};

function appendLabelValueToParent(cname, retval, retlabel) {
  opener.document.getElementById(cname).value = retval;
  opener.document.getElementById(cname).text = retlabel;
  window.close();
};

function openNWCWindow(url, controlid, width, height, prevar, extraparam, extraparam2nd) {
  var cw = null;

  link = url + '&controlName=' + controlid;

  if (!prevar)
    prevar = document.getElementById(controlid).value;

  link = link + '&value=' + prevar;
    
  if (extraparam)
    link = link + '&extraparam=' + extraparam;

  if (extraparam2nd)
    link = link + '&extraparam2nd=' + extraparam2nd;

  params = 'width=' + width + ', height=' + height + ',scrollbars=yes,toolbar=no,menubar=no,location=no';
  cw = window.open(link, controlid + 'window', params);

  if (window.focus)
    cw.focus();
  
  return true;
};

// ##### MULTISELECTNWCFIELD FUNCTIONS ##### 
function openNWCWindowSimple (url, controlid, width, height, resize, posx, posy) {
  var cw = null;
  link = url + '&controlName=' + controlid;

  params = 'width=' + width + ', height=' + height + ',scrollbars=' + (resize == true ? 'yes' : 'no') + ',toolbar=no,menubar=no,location=no,top=' + posx + ',left=' + posy;
  var windowid = controlid + 'window';
  var iesuxx = new RegExp("\-", "g");

  windowid = windowid.replace(iesuxx, 'x');

  cw = window.open(link, windowid, params);

  if (window.focus)
    cw.focus();
  
  return true;
};

function openNWCWindowMulti (url, controlid, width, height, resize, prevar, extraparam, extraparam2nd) {
  var cw = null;
  link = url + '&controlName=' + controlid;

  link = link + '&value=' + prevar;

  if (extraparam)
    link = link + '&extraparam=' + extraparam;

  if (extraparam2nd)
    link = link + '&extraparam2nd=' + extraparam2nd;
    
  params = 'width=' + width + ', height=' + height + ',scrollbars=' + (resize == true ? 'yes' : 'no') + ',toolbar=no,menubar=no,location=no';
  cw = window.open(link, controlid + 'window', params);

  if (window.focus)
    cw.focus();
  
  return true;
};

/* ##### Extended SELECT Object methods ####### */

function makeSelectionAction(itemid, elementid, label) {
  var selectitem = getOpenerElement(itemid)
    if (label) {
      insertSelectOption(selectitem, elementid, label); 
    } else {
      delSelectOptionByValue(selectitem, elementid); 
    }
  fullsizeSelect(selectitem)
    window.close();
};

function getOpenerElement(elid) {
  try {
    var el = opener.document.getElementById(elid);
    if (el)
      return el;
    else
      return false;
  } catch (e) {
    var ei = 0/*
		for (ei = 0; ei < e.length; ei++) {
		alert(e);
		}
	      */
      }
};

function fullsizeSelect(item) {
  if (!item)
    return false;
  changeSelectSize(item, item.length - item.size);
};

/* Changes the  given selectfield's size attribute, if result size is lesser than 1, 1 will be returned. */
function changeSelectSize(item, num) {
  if (!item)
    return false;
  var  actsize = item.size;
  if ((actsize + num) < 1) {
    item.size = 1;
    return true;
  } else {
    item.size = actsize + num;
    return true;
  }
};

/* Return the specified Select's Option by given index */
function getSelectOptionByIndex(item, index) {
  if (!item instanceof Object)
    return false;
  return item.options[index];
};

/* Return the specified Select's Option by given value*/
function getSelectOptionByValue(item, value) {
  if (!item)
    return false;
  var opts = item.options;
  var slen = item.length;
  var i = 0;
  for ( i = 0; i < slen; i++) {
    if (opts[i].value == value)
      return opts[i];
  }
  return false;
};

/* Removes the Option with given index from the specified Select */
function delSelectOptionByIndex(item, index, noresize) {
  if (!item)
    return false;
  item.remove(index);
  if (!noresize)
    fullsizeSelect(item);
  return true;
};

/* Removes the Option with given value from the specified Select */
function delSelectOptionByValue(item, value, noresize) {
  if (!item)
    return false;
  var opts = item.options;
  var slen = item.length;
  var j = 0;
  for ( j = 0; j < slen; j++) {
    if (opts[j].value == value) {
      item.remove(j);
      return false;
    }
  }
  if (!noresize)
    fullsizeSelect(item);
  return true;
};

/* Insert a new Option object to the specified Select object */
function insertSelectOption(item, value, label, beforeindex, noresize) {
  if (!item)
    return false;
  var opt = item.ownerDocument.createElement('option');
  opt.text = label;
  opt.value = value;
  opt.selected = true;

  var before = item.options[beforeindex];  
  
  try {
    if (before instanceof Option)
      item.add(opt, before);
    else 
      item.add(opt, null);
  } catch(ex) {
    if (beforeindex)
      item.add(opt, beforeindex);
    else
      item.add(opt);
  }

  if (!noresize)
    fullsizeSelect(item);
  return true; 
};

/* Insert a new Option object to the specified Select object if undefined else modifies it */
function setSelectOption(item, value, label, noresize) {

  if (!item)
    return false;
  var opt = getSelectOptionByValue(item, value);
  
  if (opt) {
    opt.text = label;
    /*
      var ni = opt.index;
      delSelectOptionByIndex(item, ni);
      insertSelectOption(item, value, label, ++ni);
    */
  } else {
    insertSelectOption(item, value, label);
  }
  if (!noresize)
    fullsizeSelect(item);
  return true; 
};

function getCheckboxValues(formelement) {

  var values = new Array();
  var i = 0;
  for (i = 0; i < formelement.elements.length; i++) {
    if  (formelement.elements[i].type = 'checkbox') {
      if (formelement.elements[i].checked == true) {
	values.push(new Array(formelement.elements[i].value, document.getElementById(formelement.elements[i].value + '-label').innerHTML));
      }
    }
  }
  return values;
}

function convertArray(vari) {
  if (vari instanceof Array)
    return vari.join('||');
  else (vari instanceof String)
	 return vari.split('||');
};

function appendSelectOptions(item, arr, noresize) {
  var i = 0;
  for ( i = 0; i < arr.length; i++) {
    var act = arr[i];
    var f = setSelectOption(item, act[0], act[1], true);
  }
  if (!noresize)
    fullsizeSelect(item);
};

function removeSelectOptions(item, arr, noresize) {
  var i = 0;
  for ( i = 0; i < arr.length; i++) {
    var act = arr[i];
    var f = delSelectOptionByValue(item, act[0], true);
  }
  if (!noresize)
    fullsizeSelect(item);
};

function removeSelectAllOptions(item, noresize) {
  var opts = item.options;
  opts.length = 0;
  if (!noresize)
    fullsizeSelect(item);
};

function getSelectAllOptionsData(item) {
  var opts = item.options;
  var slen = item.length;
  var j = 0;
  var arr = new Array();
  for ( j = 0; j < slen; j++) {
    arr[j] = 'v::' + opts[j].value + '|l::' + opts[j].text;
  }
  return convertArray(arr);
};

// ##### DATE FUNCTIONS ##### 

function dpYearAdjust(formid, inc) {
  dpform = document.getElementById(formid);
  if (inc)
    dpform.year.value = parseInt(dpform.year.value) + 1;
  else 
    dpform.year.value = parseInt(dpform.year.value) - 1;

  dpform.submit()
    };

function dpMonthAdjust(formid, num) {
  dpform = document.getElementById(formid);
  dpform.month.value = num;
  dpform.submit();
};

function dpMonthAdjust2(formid, inc) {
  dpform = document.getElementById(formid);
  if (inc)
    dpform.month.value = parseInt(dpform.month.value) + 1;
  else 
    dpform.month.value = parseInt(dpform.month.value) - 1;

  dpform.submit()
    };

function dpSelectDate(cname, retval) {
  opener.document.getElementById(cname).value = retval;
  window.close();
};

function setToNow(syear, smonth, sday, shour, sminutes, sseconds) {
  var date = new Date();

  syear.value = date.getFullYear();
  smonth.value = date.getMonth() + 1;
  sday.value = date.getDate();
  shour.value = date.getHours();
  sminutes.value = date.getMinutes();
  sseconds.value = date.getSeconds();
};

function radioOrderize(changedEl) {
  var radios = changedEl.form.getElementsByTagName('input');
  var selectedVal = changedEl.value;
  for (i = 0; i < radios.length; i++) {
    if (radios[i].type == 'radio' && radios[i].id != changedEl.id && radios[i].value == selectedVal) {
      radios[i].checked = '';
    }
  }
};

function formatInputNumber(numberInputs) {
  var convertToCurrencyDisplayFormat = function(str) {
    var regex = /(-?[0-9]+)([0-9]{3})/;
    str += '';
    while (regex.test(str)) {
      str = str.replace(regex, '$1 $2');
    }
    return str;
  };
  var stripNonNumeric = function(str) {
    str += '';
    str = str.replace(/[^0-9]/g, '');
    return str;
  };
  numberInputs.each(function() {
      this.value = convertToCurrencyDisplayFormat(this.value);
    });
  numberInputs.blur(function() {
      this.value = convertToCurrencyDisplayFormat(this.value);
    });
  numberInputs.focus(function() {
      this.value = stripNonNumeric(this.value);
    });
  $("form").submit(function() {
      numberInputs.each(function() {
	  this.value = stripNonNumeric(this.value);
        });
    });
};

function jumpToNextGrouped($groupNode) {
  var $inputs = $groupNode.find("input[type='text']");
  $inputs.keyup(function(){
      var $field = $(this);
      var max = $field.attr('maxlength');
      if ($field.val().length >= max) {
	var $next = $field.nextAll("input[type='text']").eq(0);
	if ($next.length) {
	  $next.focus().select();
	} else {
	  $field.blur();
	}
      }
    });

  // remove onfocus attr
  $inputs.attr('onfocus', '');
};

