stdBrowser = (document.getElementById) ? true : false
var saveStyleAttr = "";
var watchListArray;
var actionId;
var pageSerial;

function changeAttribute (idFieldName, styleAttrPair) {
  saveStyleAttr = document.getElementById(idFieldName).style.cssText;
  document.getElementById(idFieldName).style.cssText += ";" + styleAttrPair;
}

function unChangeAttribute (idFieldName) {
  document.getElementById(idFieldName).style.cssText = saveStyleAttr;
}

function getValue(idField) {
  var type = idField.type;
  if (type.indexOf('select') != -1)
    return (idField.options[idField.selectedIndex]).value
  else 
    return idField.value;
}

function putValue(idField, value) {
  var type = idField.type;
  if (type.indexOf('select') != -1) {
    for (var i=0; i<idField.options.length; i++)
      if (idField.options[i].value == value) idField.selectedIndex = i;
  } else 
    idField.value = value;
}

function popUp(evt,currElem) {
  popUpWin = (stdBrowser) ? document.getElementById(currElem).style : eval("document." + currElem)
  if (document.all) {
    popUpWin.pixelTop = parseInt(evt.y) + 15 + getScrollTop(window);
    popUpWin.pixelLeft = Math.max(2,parseInt(evt.x)-125);
  }
  else {
    if (stdBrowser) {
      popUpWin.top = parseInt(evt.pageY)+2 + "px"
      popUpWin.left = Math.max(2,parseInt(evt.pageX)-125) + "px"
    }
    else {
      popUpWin.top = parseInt(evt.pageY)+2
      popUpWin.left = Math.max(2,parseInt(evt.pageX)-125)
    }
  }
  popUpWin.visibility = "visible"
}

function popDown(currElem) {
  popUpWin = (stdBrowser) ? document.getElementById(currElem).style : eval("document." + currElem)
  popUpWin.visibility = "hidden"
}

function getScrollTop(win) {
  var scrollTop = 0;
  if (win.pageYOffset) {
     scrollTop = win.pageYOffset;
  }
  else if (win.document.documentElement && win.document.documentElement.scrollTop) {
     scrollTop = win.document.body.scrollTop;
  }
  else if (win.document.body) {
     scrollTop = win.document.body.scrollTop;
  }
  return scrollTop;
}

var httpWrite,httpUpdate,isWorking=false,idField,idColor,focusId,timer;
var fireFox = navigator.userAgent.indexOf('Firefox') != -1;

function getXMLHttpRequest() {
  var req;
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  } else confirm ("Error creating XMLHttpRequest");

  return req;
}

function asynchAction(widgetIdx, actionId) {
//alert ("asynchAction("+widgetIdx+", "+actionId+")");
  if(isWorking) {
    timer = setTimeout('asynchUpdate()',500);
    return
  }
  httpWrite = getXMLHttpRequest();
//  idColor = idField.style.backgroundColor
  httpWrite.open("GET", "ajaxaction.jsp?_Action=" +actionId + "&_Seq=" + pageSerial + "&" + "Widget=" + widgetIdx
    + "&rnd="+(new Date().valueOf()), true);
  httpWrite.onreadystatechange = handleWrite;
  isWorking = true;
  httpWrite.send(null);
  focusId = null;
//alert ("asynchAction(ajaxaction.jsp?_Action=" +actionId + "&_Seq=" + pageSerial + "&" + "Widget=" + widgetIdx);
  return false;
}

function asynchWrite(idFieldName, actionId) {
  idField = document.getElementsByName(idFieldName)[0];
  if(isWorking) {
    timer = setTimeout('asynchUpdate()',500);
    return
  }
  if (actionId == '') actionId = '0';
  httpWrite = getXMLHttpRequest();
  idColor = idField.style.backgroundColor
  idField.style.backgroundColor="white";
//alert ("ajaxsave.jsp?_Action=" +actionId + "&_Seq=" + pageSerial + "&" + idFieldName + "=" + escape(getValue(idField)));
  httpWrite.open("GET", "ajaxsave.jsp?_Action=" +actionId + "&_Seq=" + pageSerial + "&" + idFieldName + "=" + escape(getValue(idField))
    + "&rnd="+(new Date().valueOf()), true);
  httpWrite.onreadystatechange = handleWrite;
  isWorking = true;
  httpWrite.send(null);
  focusId = null;
  return false;
}

function handleWrite () {
  var innerHTML = "", currentBlock;
  if (httpWrite.readyState == 4) {
    if (httpWrite.status == 200) {
      isWorking = false;
      if (idField) idField.style.backgroundColor=idColor;
      var widgetIds = httpWrite.responseXML.getElementsByTagName("widgetid");
      var values = httpWrite.responseXML.getElementsByTagName("value");
      for (var i=0; i<values.length; i++) {
        currentBlock = values[i].firstChild;
        while (currentBlock) {
          innerHTML = innerHTML + currentBlock.data;
          currentBlock = currentBlock.nextSibling;
        }
        widgetId = widgetIds[i].firstChild.data;
        widget = document.getElementById("W"+widgetId);
        widget.innerHTML = innerHTML;
//alert (widgetId);
//        widget.innerHTML = unescape(innerHTML);
      }
    }
  }
}

function asynchUpdate() {
  var watchList = document.getElementById("watchList");
  if (watchList.value == "") return;
  timer = setTimeout('asynchUpdate()',3000);
  update (watchList.value);
}
/*
//var isNow = false;
function updateNow (fieldName) {
//  var watchList = document.getElementById("watchList");
//  var watchListValues = "";
//  var watchListArray = watchList.value.split(",");
  isNow = true;
  update(watchListArray[0]+","+watchListArray[1]+","+fieldName);
}
*/
function update(watchListValue) {
  var watchListValues = "";
//  var watchListArray = watchListValue.split(",");
//  var actionId = watchListArray[0];
//  var pageSerial = watchListArray[1];
  if (!isWorking) {
    httpUpdate = getXMLHttpRequest();
    for (var i=2; i<watchListArray.length; i++) {
//if (!document.getElementById(watchListArray[i])) alert (watchListArray[i]);
      watchListValues += '&' + watchListArray[i] + '=' + escape(getValue((document.getElementsByName(watchListArray[i])[0])));
//document.getElementById(watchListArray[i])));
    }
    httpUpdate.open("POST","ajaxupdate.jsp"
    + "&rnd="+(new Date().valueOf()), true);
    httpUpdate.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    if (fireFox)
      httpUpdate.setRequestHeader('Connection','close'); //STUPID 400 FF ERROR FIX
    httpUpdate.onreadystatechange = handleUpdate;
    httpUpdate.send("_Action=" +actionId + "&_Seq=" + pageSerial + watchListValues);
  }
}

function handleUpdate() {
  var innerHTML = "", currentBlock;
  if (httpUpdate.readyState == 4) {
    if (httpUpdate.status == 200) {
      var widgetIds = httpUpdate.responseXML.getElementsByTagName("widgetid");
      var values = httpUpdate.responseXML.getElementsByTagName("value");
      for (var i=0; i<values.length; i++) {
        currentBlock = values[i].firstChild;
        while (currentBlock) {
          innerHTML = innerHTML + currentBlock.data;
          currentBlock = currentBlock.nextSibling;
        }
        widgetId = widgetIds[i].firstChild.data;
        widget = document.getElementById("W"+widgetId);
        if (focusId != "F"+widgetId) {
          widget.innerHTML = unescape(innerHTML);
        }
      }
      isWorking = false;
    }
  }
}

function doOnLoad (_pageYOffset) {
  watchListArray = document.getElementById("watchList").value.split(",");
  actionId = watchListArray[0];
  pageSerial = watchListArray[1];
//alert ("actionId = " + actionId + "\npageSerial =" + pageSerial); 
  window.scrollBy(0, _pageYOffset);
  if (watchListArray.length > 2) asynchUpdate();
  fadeInit();
}

function fadeInit() {
  for (var i=0; i<fadeId.length; i++) {
     widget = document.getElementById(fadeId[i]);
     if (widget != null) {
       widget.style.filter="alpha(opacity=0)";
       setTimeout("fadeWidget("+i+",0)",startFade[i]);
       if (eraseFade[i] != 0) setTimeout ("fadeEnd("+i+")",eraseFade[i]);
    }
  }
}

function fadeEnd(id) {
  document.getElementById(fadeId[id]).innerText="";
}

function fadeWidget (id,opacity) { 
  opacity += 5; // increase color darkness
  document.getElementById(fadeId[id]).style.filter="alpha(opacity="+opacity+")";
  if (opacity != 100)  setTimeout("fadeWidget("+id+","+opacity+")",50);
}

var fadeId=[];
var startFade=[];
var eraseFade=[];

function newWindow(url,args) {
  window.open (url,args);
  return;
}

function newWindow(url) {
  window.open (url);
  return;
}

