// Class: Cookie
// name=<value>;expires=<date>;path=<path>;domain=<domain>;secure
function Cookie(name) {
  // Public Properties
  this.name=name;
  this.value;
  this.expires;
  this.path;
  this.domain;
  this.secure=false;
  this.keys;
  // Private Properties
  // Public functions
  this.Read = fnRead;
  this.Write = fnWrite;
  this.ExpiresIn = fnExpiresIn;
  this.Delete = fnDelete;
  // Constructor
  this.Read();


  // private functions
  function fnRead() {
    this.value = null;
    this.keys = new Array;

    var start = document.cookie.indexOf(this.name + '=');
    var len = start + this.name.length + 1;
    if ((!start) && (this.name != document.cookie.substring(0, this.name.length)))
      return;
    if (start == -1) 
      return;
    var end = document.cookie.indexOf(';', len);
    if (end == -1) 
      end = document.cookie.length;
    this.value = unescape(document.cookie.substring(len, end));
    //this.value = document.cookie.substring(len, end);

    // fill keys if necessary (ASP cookies)
    SplitParameters(this.keys, this.value);
  }

  function fnWrite() {
    var sCookie = this.name + '=';

    // ASP cookies. name=key1=value1&key2=value2 ...[keyN=valueN];
    var sValue = JoinParameters(this.keys);

    // if bFirst plain name=value
    sCookie += escape( (sValue) ? sValue : this.value );
    //sCookie += (sValue) ? sValue : this.value;

    if (this.expires) sCookie += ';expires=' + this.expires.toUTCString();
    if (this.path) sCookie    += ';path=' + this.path;
    if (this.domain) sCookie  += ';domain=' + this.domain;
    if (this.secure == true) sCookie += ';secure';
    //alert(sCookie);        
    document.cookie = sCookie;
  }

  function fnExpiresIn(days) {
    var dt = new Date();
    dt.setDate(dt.getDate() + days);
    this.expires = dt;
  }

  function fnDelete() {
    this.ExpiresIn(-1);
    this.Write();
  }
}


// Class: Response
function Response() {
  // Public Properties
  this.QueryString = new Array;

  // Constructor
  var query = unescape(window.location.search.substring(1));
  SplitParameters(this.QueryString, query);
}


// helper functions
function SplitParameters(ary, sLine) {
  var split = sLine.split('&');
  for (var i=0; i<split.length; i++) {
    var nvp = split[i];
    var nvps = nvp.split('=')
    if (nvps)
      ary[nvps[0]] = nvps[1];
    else
      ary[nvp]=null;
  }
}

function JoinParameters(ary) {
  var sValue;
  for(var key in ary) {
    if (sValue)
      sValue += '&' + key;
    else
      sValue = key;

    if(ary[key].length > 0)
      sValue += '=' + ary[key];
  }
  return sValue;
}


// Read a file from the server
var _xmlhttp;
var _Url;
var _fnCallBack;

function getFile(pURL, pCallBack) {
	_Url = pURL;
	if (window.XMLHttpRequest) { // code for Mozilla, Safari, etc 
		_xmlhttp=new XMLHttpRequest();
	} else if (window.ActiveXObject) { //IE 
		_xmlhttp=new ActiveXObject('Microsoft.XMLHTTP'); 
	}
	if (_xmlhttp != null) {
		_fnCallBack = pCallBack;

		_xmlhttp.onreadystatechange=postFileReady;
		_xmlhttp.open("GET", pURL, true);
		_xmlhttp.send(null);
	}
	else {
		alert("Your browser does not support XMLHTTP");
	}
}

// function to handle asynchronous call
function postFileReady() {
	if (_xmlhttp.readyState==4) {
		if (_xmlhttp.status==200)
			_fnCallBack(_xmlhttp.responseText);
		else
			alert("Problem retrieving file: "+ _Url);
	}
}

// find left, top of an element
function findPos(ele) {
	var curleft = curtop = 0;
	if (ele.offsetParent) {
		curleft = ele.offsetLeft;
		curtop = ele.offsetTop;
		while (ele = ele.offsetParent) {
			curleft += ele.offsetLeft;
			curtop += ele.offsetTop;
		}
	}
	return [curleft,curtop];
}

