/* Functions shared between todd and webforms. Should not have dependencies on any todd/webforms libraries (including internals.js/api.js) */


/********************************************************************
   JSON encoding/decoding reference implementation
   See also http://www.JSON.org/js.html
*/
if(!this.JSON){this.JSON={};}(function(){function f(n){return n<10?'0'+n:n;}if(typeof Date.prototype.toJSON!=='function'){Date.prototype.toJSON=function(key){return isFinite(this.valueOf())?this.getUTCFullYear()+'-'+f(this.getUTCMonth()+1)+'-'+f(this.getUTCDate())+'T'+f(this.getUTCHours())+':'+f(this.getUTCMinutes())+':'+f(this.getUTCSeconds())+'Z':null;};String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(key){return this.valueOf();};}var cx=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,escapable=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,gap,indent,meta={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'},rep;function quote(string){escapable.lastIndex=0;return escapable.test(string)?'"'+string.replace(escapable,function(a){var c=meta[a];return typeof c==='string'?c:'\\u'+('0000'+a.charCodeAt(0).toString(16)).slice(-4);})+'"':'"'+string+'"';}function str(key,holder){var i,k,v,length,mind=gap,partial,value=holder[key];if(value&&typeof value==='object'&&typeof value.toJSON==='function'){value=value.toJSON(key);}if(typeof rep==='function'){value=rep.call(holder,key,value);}switch(typeof value){case'string':return quote(value);case'number':return isFinite(value)?String(value):'null';case'boolean':case'null':return String(value);case'object':if(!value){return'null';}gap+=indent;partial=[];if(Object.prototype.toString.apply(value)==='[object Array]'){length=value.length;for(i=0;i<length;i+=1){partial[i]=str(i,value)||'null';}v=partial.length===0?'[]':gap?'[\n'+gap+partial.join(',\n'+gap)+'\n'+mind+']':'['+partial.join(',')+']';gap=mind;return v;}if(rep&&typeof rep==='object'){length=rep.length;for(i=0;i<length;i+=1){k=rep[i];if(typeof k==='string'){v=str(k,value);if(v){partial.push(quote(k)+(gap?': ':':')+v);}}}}else{for(k in value){if(Object.hasOwnProperty.call(value,k)){v=str(k,value);if(v){partial.push(quote(k)+(gap?': ':':')+v);}}}}v=partial.length===0?'{}':gap?'{\n'+gap+partial.join(',\n'+gap)+'\n'+mind+'}':'{'+partial.join(',')+'}';gap=mind;return v;}}if(typeof JSON.stringify!=='function'){JSON.stringify=function(value,replacer,space){var i;gap='';indent='';if(typeof space==='number'){for(i=0;i<space;i+=1){indent+=' ';}}else if(typeof space==='string'){indent=space;}rep=replacer;if(replacer&&typeof replacer!=='function'&&(typeof replacer!=='object'||typeof replacer.length!=='number')){throw new Error('JSON.stringify');}return str('',{'':value});};}if(typeof JSON.parse!=='function'){JSON.parse=function(text,reviver){var j;function walk(holder,key){var k,v,value=holder[key];if(value&&typeof value==='object'){for(k in value){if(Object.hasOwnProperty.call(value,k)){v=walk(value,k);if(v!==undefined){value[k]=v;}else{delete value[k];}}}}return reviver.call(holder,key,value);}text=String(text);cx.lastIndex=0;if(cx.test(text)){text=text.replace(cx,function(a){return'\\u'+('0000'+a.charCodeAt(0).toString(16)).slice(-4);});}if(/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,'@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']').replace(/(?:^|:|,)(?:\s*\[)+/g,''))){j=eval('('+text+')');return typeof reviver==='function'?walk({'':j},''):j;}throw new SyntaxError('JSON.parse');};}}());


/********************************************************************
   XMLHttpRequest
*/
/* Create a request object (aka ajax)
   ADDME: Probably needs to deal with multiple versions of the MSXML components
   */
function toddCreateRequestObject()
{
  var browser = navigator.appName;
  if(browser == "Microsoft Internet Explorer")
  {
    var ro = new ActiveXObject("Microsoft.XMLHTTP");
    //var ro = new ActiveXObject("MSXML2.XMLHTTP");
  }
  else
  {
    var ro = new XMLHttpRequest();
  }
  return ro;
}


/********************************************************************
   Events
*/
// Prevent event from bubbling up
function toddDontPropagateEvent(theEvent)
{
  if(theEvent.stopPropagation)
  {
    if (theEvent.cancelable)
      theEvent.preventDefault();
    theEvent.stopPropagation();
  }
  else
  {
    theEvent.returnValue = false;
    theEvent.cancelBubble = true;
    if(theEvent.keyCode)
      theEvent.keyCode = 0;
  }
  return false;
}


/********************************************************************
   Dimensions and positions
*/
/** @short returns the size of the visual area of window
    @return object with width and height
*/
function toddGetViewPortDimensions()
{
  // all except Explorer
  if (window.innerHeight)
    return { width:  window.innerWidth
           , height: window.innerHeight
           };

  // Explorer 6 Strict Mode
  if (document.documentElement && document.documentElement.clientHeight)
    return { width:  document.documentElement.clientWidth
           , height: document.documentElement.clientHeight
           };

  return { width:  document.body.clientWidth
         , height: document.body.clientHeight
         };
}


/** @short returns the full size of the document
    @return size of the document
*/
function toddGetPageDimensions()
{
  var htmlwidth = document.documentElement.scrollWidth;
  var htmlheight = document.documentElement.scrollHeight;

  // IE6-QuirksMode and SF (all versions? tested with 3.2.1) give page height in <BODY>
  // All other browsers give the size in documentElement (<HTML>)
  var bodywidth = document.body.scrollWidth;
  var bodyheight = document.body.scrollHeight;

  // return largest values
  return { width:  htmlwidth  > bodywidth  ? htmlwidth  : bodywidth
         , height: htmlheight > bodyheight ? htmlheight : bodyheight
         };
}


/** @short returns the absolute position an element in <body> would need to have the same position as the given element
    @param obj reference to element
    @return {left:X,top:Y}
*/
function toddGetBodyPos(obj)
{
  // getBoundingClientRect is supported by IE5+, FF3+, OP9.50+, SF4+
  if (obj.getBoundingClientRect)
  {
    var bounds = obj.getBoundingClientRect();

    var xscroll = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft;
    var yscroll = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;

    return { left: bounds.left + xscroll - document.documentElement.clientLeft - document.body.clientLeft
           , top:  bounds.top + yscroll - document.documentElement.clientTop - document.body.clientTop
           };
  }


  // getBoxObjectFor works in FF2, deprecated in FF3/3.5, removed in FF3.6
  // Only use it for Firefox, since other Gecko based browsers may not correctly implement getBoxObjectFor
  if (toddUserAgentIsFirefox && document.getBoxObjectFor)
  {
    var box   = document.getBoxObjectFor(obj);
    var vpBox = document.getBoxObjectFor(document.documentElement);

    return { left: box.screenX - vpBox.screenX
           , top:  box.screenY - vpBox.screenY
           };
  }


  // the oldskool method of getting positions
  // Safari <4 & Opera 9.25 will use this method
  var elemx = obj.offsetLeft;
  var elemy = obj.offsetTop;
  var parent = obj.offsetParent;

  while (parent)
  {
    // add offset to content part of our offsetParent
    elemx += parent.offsetLeft;
    elemy += parent.offsetTop;

    // Quirks:
    // - Opera already adds clientTop/Left to offsetTop/Left
    // - Firefox <3 (Gecko <1.9) doesn't support clientTop/Left
    if (!toddClientTopIsAddedToOffsetTop && typeof parent.clientLeft != 'undefined')
    {
      // also add the edge size of the offsetParent
      elemx += parent.clientLeft;
      elemy += parent.clientTop;
    }

    // subtract the amount the offsetParent is scrolled, except if it's the scrollposition of the page
    if (parent != document.body && parent != document.documentElement)
    {
      elemx -= parent.scrollLeft;
      elemy -= parent.scrollTop;
    }

    parent = parent.offsetParent;
  }

  return { left: elemx
         , top:  elemy
         };
}

// deprecated
function toddGetObjectPosX(obj)
{
  return toddGetBodyPos(obj).left;
}

// deprecated
function toddGetObjectPosY(obj)
{
  return toddGetBodyPos(obj).top;
}

// deprecated
function toddGetObjectScreenX(obj)
{
  return toddGetBodyPos(obj).left;
}

// deprecated
function toddGetObjectScreenY(obj)
{
  return toddGetBodyPos(obj).top;
}

if(typeof toddMarkScriptComplete != 'undefined')
  toddMarkScriptComplete("base.js");
