// Javascript utilities

// This function will submit a form if the event
// represents an Enter key press. To ensure that pressing
// Enter in a text field submits a form (even if the form
// has multiple text fields), associate this funciton
// with the text field as follows:
// onkeypress="return submitFormOnEnter (this,event,linkname)"
function submitFormOnEnter (myfield, e, linkName)
{
 	var keycode;
	if (window.event){
	    keycode = window.event.keyCode;
	} else if (e){
	    keycode = e.which;
	} else
        return true;

    if (keycode == 13) {
       btn = document.getElementById (linkName);
       if (btn != null) {
           btn.onclick(null);
           btn.click ();
       }
       return false;
   } else
        return true;
}

function disableButton(button) {
	button.disabled = true;
}

function disableButtonOnSubmit(formName, buttonName)
{
	// We could try to disable the form directly, but this is easier for now
	// var form = document.forms[formName];
	var button = document.getElementsByName(formName + ':' + buttonName)[0];
	if (button) {
	  window.setTimeout(function(){disableButton(button);}, 1);
	}
}

// Add extra JavaScript for long operations so they will not be submitted multiple
// times.  I.E. Registration, and others.
function disableLinkOnSubmit(linkName)
{
    // Get us a wait cursor
    document.body.style.cursor = "wait";
    
    // Do what we can to disable the link
	var link = document.links[linkName];
	if (link == null) {
		// Workaround for I.E.
		var link = document.getElementsByName(linkName)[0];
	}

    if (link != null) {
      //link.className='sswsbutton-down';
      link.onclick='return false';
	  link.onmouseover='return false';
	  link.onmousedown='return false';
	  link.onmouseup='return false';
	  link.onmouseout='return false';
      //link.disabled=true; // interferes with submit
    } 
}

var debugZones;

// used by the ajaxTrigger tag
function aa_submit (control, form, zones)
{
    var formObj = document.forms[form];
    if (formObj == null) {
        for (var i = 0; i < document.forms.length; i++) {
            if (document.forms[i].id == form) {
                formObj = document.forms[i];
                break; 
            }
        }
    }
    if (control != null) {
        formObj.elements[form + ':_link_hidden_'].value = control;
    } else {
        formObj.elements[form + ':_link_hidden_'].value = '';
}    
    
    if (zones.length > 0 && debugZones != undefined) {
        zones += "," + debugZones;
    }
    ajaxAnywhere.getZonesToReload = function () {
        return zones;
    };
    ajaxAnywhere.formName = form;
    ajaxAnywhere.submitAJAX();
}

function getAbsPosition (control) {
//            if (el.getBoundingClientRect) { // IE
//               box = el.getBoundingClientRect();
   var el = control;
   var pos = [];
   pos = [el.offsetLeft, el.offsetTop];
   parentNode = el.offsetParent;
   if (parentNode != el) {
      while (parentNode) {
      pos[0] += parentNode.offsetLeft;
      pos[1] += parentNode.offsetTop;
      parentNode = parentNode.offsetParent;
     }
   }
   return pos;
}

function getSize (control) {
//            if (el.getBoundingClientRect) { // IE
//               box = el.getBoundingClientRect();
   var el = control;
   var pos = [];
   pos = [el.offsetWidth, el.offsetHeight];
   if (pos[0] == undefined)
        pos[0] = 0;
   if (pos[1] == undefined)
        pos[1] = 0;
        
   if (el.offsetWidth == 0 || el.offsetHeight == 0) {
       for (var i = 0; i < el.childNodes.length; i++) {
          childPos = getSize (el.childNodes[i]);
          var w = el.childNodes[i].offsetLeft;
          if (w == undefined)
              w = 0;
          pos[0] = Math.max (pos[0], childPos[0] + w);
          var h = el.childNodes[i].offsetTop;
          if (h == undefined)
              h = 0;

          pos[1] = Math.max (pos[1], childPos[1] + h);
       }
   }   
   return pos;
}
function performCompletion (textCtrl, popupCtrl, value) {
    document.getElementById(textCtrl).value = value;
}

