var req;

function asyncRequest(url, callback) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = callback;
        req.open("GET", url, true);
        req.send(null);
        
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");        
        if (req) {
            req.onreadystatechange = callback;
            req.open("GET", url, true);
            req.send();
        }
    }
    
    return req;
}

function syncRequest(url) {
    var r;
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        r = new XMLHttpRequest();
        r.open("GET", url, false);
        r.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        r = new ActiveXObject("Microsoft.XMLHTTP");
        if (r) {
            r.open("GET", url, false);
            r.send();
        }
    }
    return r;
}

//callback example
function processReqChange() 
{
    if (req.readyState == 4) {
        if (req.status == 200) {
            alert(req.responseText);
        } else {
            alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }
}

function trim(s)
{               
        var r = s;    
        r = s.replace(/^\s+/,""); 
        r = r.replace(/\s+$/,"");         
        return r; 
}

//returns absolute x and y coord of control
function getAbs(obj)
{
    var abs = new Object();
    abs.x = obj.offsetLeft;
    abs.y = obj.offsetTop;
    
    var parent = obj.offsetParent;
    while (parent.tagName != "HTML" && parent.tagName !="BODY")
    {
        abs.x+=parent.offsetLeft;
        abs.y+=parent.offsetTop;
        parent=parent.offsetParent;
    }
    
    return abs;
}

//returns absolute x coord of control
function getAbsLeft(obj)
{
     var x=obj.offsetLeft;
     var parent=obj.offsetParent;
     while (parent.tagName != "HTML" && parent.tagName !="BODY")
     {
          x+=parent.offsetLeft;
          parent=parent.offsetParent;
     }
     if (parent!= null)
     x+=parent.offsetLeft;
     return x;
}

//return absolute y coord of control
function getAbsTop(obj)
{
     var y=obj.offsetTop;
     var parent=obj.offsetParent;
     while (parent.tagName != "HTML" && parent.tagName !="BODY")
     {
          y+=parent.offsetTop;
          parent=parent.offsetParent;
     }
     if (parent != null)
     y+=parent.offsetTop;
     return y;
}

function showExclamation(obj)
{
return;
  if (typeof(obj) == "string")
    obj = document.getElementById(obj);


  if (obj == null)
  {
    alert("showExclamation: object is null");
    return;
  }

  var top = getAbsTop(obj);
  var left = getAbsLeft(obj);
  var oImg = document.createElement("IMG");
  document.body.appendChild(oImg);
  oImg.src = "images/exclamation.gif";
  oImg.style.position = "absolute";
  oImg.style.left = left;
  oImg.style.top = top;

}

function showConfirmMessage(strMessage, hitOnOk, hitOnCancel)
{
   
  var btnOk = null;
  var btnCancel = null;

  if (hitOnOk != null && hitOnOk != "")
  {
  	btnOk = document.getElementById(hitOnOk);
  	if (btnOk == null)
  	    alert("common.js: showPrompt() - cannot find button with Id: " + hitOnOk);
  }
  
  if (hitOnCancel != null && hitOnCancel != "")
  {
    btnCancel = document.getElementById(hitOnCancel);
    if (btnCancel == null)
        alert("common.js: showPrompt() - cannot find button with Id: " + hitOnCancel);  
  }
  
  if (window.confirm(strMessage))
  {
    if (btnOk != null)
        btnOk.click();
  }
  else
  {
    if (btnOk != null)
        btnCancel.click(); 
  }
}

function getIntAttribute(oElement, attrName, defaultValue)
{
    var s = oElement.getAttribute(attrName);
    if (s != null)
       return parseInt(s);
     
    // not found
    return defaultValue;
}


function deserializeIntArray(str, delimeter)
{
    var intArr = new Array();
    var arr = str.split(delimeter);
//    alert(str);
    for (var i=0; i<arr.length; i++)
    {        
        if (arr[i].length > 0)
        {
            var v = parseInt(arr[i]);
            intArr[intArr.length++] = v == -2147483648 ? null : v;
        }
    }
  
    return intArr;
}

function deserializeArray(str, delimeter)
{
    var intArr = new Array();
    var arr = str.split(delimeter);
    for (var i=0; i<arr.length; i++)
    {        
        if (arr[i].length > 0)
            intArr[intArr.length++] = arr[i] == "-2147483648" ? null : arr[i];
    }
  
    return intArr;
}

function serializeIntArray(arr, delimeter)
{
    var s = "";
    
    for (var i=0; i<arr.length; i++)
    {
        if (i > 0)
            s += delimeter;
            
        s += arr[i] == null ? -2147483648 : arr[i];
    }
    
    return s;
}

function serializeArray(arr, delimeter)
{
    var s = "";
    
    for (var i=0; i<arr.length; i++)
    {
        if (s.length > 0)
            s += delimeter;
            
        s += arr[i] == null ? "-2147483648" : arr[i];
    }
    
    return s;
}

function getParentOfType(thisObj, tagName)
{
    if (thisObj.tagName == tagName)
    	return thisObj;

    while (thisObj.parentNode != this && thisObj.parentNode != null)
    {
    	thisObj = thisObj.parentNode;

    	if (thisObj.tagName == tagName)
            return thisObj;
    }

    // not found
    return null;
}

function ddlRemoveItem(ddl, value)
{
    for (var i=0; i<ddl.options.length; i++)
    {
        if (ddl.options[i].value == value)
        {
            //FireFox Compatibility. i.e. it hasn't function remove()          
            //ddl.options.remove(i); - doesn't work
            ddl.options[i]=null;
            break;
        }
    }
}

// Appends item to drop down list, assuming they are sorted by caption asc
function ddlAddItem(ddl, value, text)
{
    // find place for item
    var textUpper = text.toUpperCase();
    for (var i=0; i<ddl.options.length; i++)
    {
        if (ddl.options[i].text.toUpperCase() > textUpper)
        {
            break;
        }
    }
    
    var opt = document.createElement("OPTION");
    opt.value = value;
    opt.text = text;
    ddl.options.add(opt, i);
}


/*
  compatibility function, adds to FireFox event object some props that are there for IE
*/
function prepareEvent(evObj)
{
  if (window.event != null)
    return window.event; // IE
  else
  {
    evObj.srcElement = evObj.target;
    return evObj;
  }
}

// If user pressed enter, click on button given in second argument
function checkDefaultButton(eventObj, btnId)
{
  eventObj = prepareEvent(eventObj);
  var btn = document.getElementById(btnId);

  if (btn != null && eventObj.keyCode == 13)
  {
    event.cancelBubble = true;
    btn.click();
    return false;
//    window.setTimeout("document.getElementById('" + btnId + "').click()", 50);
  } 

  return true;
}

function getBoolAttribute(element, attrName, defaultValue)
{
    if (defaultValue == null)
        defaultValue = true;
        
    var attr = element.getAttribute(attrName);
    if (attr == null)
        return defaultValue;
        
    return attr.toLowerCase() == "true" || attr == "1";
}

/* DEBUG METHODS */

// dumps object properties to a textarea
function DEBUG_dumpObjectModel(obj)
{
  var textarea = window.document.getElementById("DEBUG");
  if (textarea == null)
  {
    // let's create it :)
    var textarea = document.createElement("TEXTAREA");
    textarea.name = textarea.id = "DEBUG";
    textarea.cols = 60;
    textarea.rows = 10;

    window.document.body.appendChild(textarea);
  }

  var out = "Dumping object properties: \n";

  if (obj != null)
  {
    for (i in obj)
    {
      out += i + " = " + obj[i] + "\n";
    }
  }
  else
    out += "OBJECT IS NULL";

  textarea.value = out;
  return out;
}

function Stack()
{
  this.position = -1;

  this.items = new Array();
}

Stack.prototype.pop = _Stack_pop;
Stack.prototype.push = _Stack_push;
Stack.prototype.getCurrent = _Stack_getCurrent;

function _Stack_pop()
{
  var o = this.items[this.position];
  this.position--;

  return o;
}

function _Stack_push(obj)
{
  this.position++;
  this.items[this.position] = obj;
}

function _Stack_getCurrent()
{
  return this.items[this.position];
}


/* */ 
function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}




/************ Some debug methods ***********/
function dtS()
{
  return (dtS.caller.dateStart__ = (new Date()).getTime());
}

function dtE()
{
  return (dtE.caller.dateEnd__ = (new Date()).getTime());
}

function alertEx()
{
  alert( "PROCESSED TIME: " + (dtE() - alertEx.caller.dateStart__) + "\n"
    + alertEx.caller.valueOf());
}

function dumpArray(arr)
{
    var s = "arraylen: " + arr.length + ")";
    
    for (var i=0; i<arr.length; i++)
    {
        s += arr[i] + "*";
    }
    
    alert(s);
}



