// appendToFunction
function appendToFunction(functionName, script)
{
  var currentFunction = eval(functionName) ? eval(functionName).toString() : "";
  var currentScript = currentFunction.length ? trim(currentFunction.slice(currentFunction.indexOf("{") + 1, currentFunction.length - 2)) : "";
  var newScript = "";
    
  var endCharacterRegExp = /^[\d\D\s]*[;}]$/m;

  if (currentScript.length && !currentScript.match(endCharacterRegExp))
  {
    currentScript += ";";
  }

  var returnRegExp = /^([\d\D\s]*?)return\s+([^;}]*);?([^$]*)$/m;
  
  if (currentScript.match(returnRegExp))
  {
    var matches = returnRegExp.exec(currentScript);
    
    newScript = matches[1];
    newScript += "\n";
    newScript += "if (" + matches[2] + ")\n";
    newScript += "{\n";
    newScript += "   " + script + "\n";
    newScript += "}\n";
    newScript += "else\n";
    newScript += "{\n";
    newScript += "  return false;\n";
    newScript += "}\n";
    newScript += matches[3];
  }
  else
  {
    newScript = (currentScript.length ? currentScript + "\n" : "") + script;
  }

  eval(functionName + " = new Function(newScript);");
}


// findPosition
function findPosition(obj)
{
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	
	var coordinates = new Object();
	coordinates.top = curtop;
	coordinates.left = curleft;
	
	return coordinates;
}


// trim
function trim(value)
{
  if (value && (typeof value == "string"))
    value = value.replace(/^\s*|\s*$/g, "");
  
  return value;
}

var formSubmitted = false;

function submitFormOnEnter(element, event)
{ 
  var characterCode;

  if (event.which) // if which property of event object is supported (NN4)
  { 
    characterCode = event.which;
  }
  else
  {
    characterCode = event.keyCode; // character code is contained in IE's keyCode property
  }

  if (characterCode == 13) //if generated character code is equal to ascii 13 (if enter key)
  {
    if (formSubmitted == false)
    {
      formSubmitted = true;
      element.form.submit(); // submit the form
    }

    return false;
  }
  else
  {
    return true;
  }
}

function popupWindow(name, href, width, height)
{
  var preferences = "toolbar=no,"
    + "directories=no,"
    + "status=yes,"
    + "scrollbars=yes,"
    + "resizable=yes,"
    + "menubar=no," 
    + "width=" + width + ","
    + "height=" + height;
  
  newWindow = window.open(href, name, preferences);  
  newWindow.focus();
}

