var debug = debug || 0;
CS_Window = function(){
  this.newLocation = false;
  this.children = new Array();
  this.isRPCCapable = (document.createElement) ? true : false;
  if(this.isRPCCapable){
    this.RPCElementId = 'RPCObject';
    this.RPCElement = null;
    this.RPCElementAvail = true; // true = available, false == inuse.
    this.RPCStack = new Array(); //Used to store stack of URLs to be requested.
    this.RPCTimeout = 10000; // Request timeout.
  }
}

CS_Window.prototype.setNewLocation = function(loc){
  var loc = loc || false;
  this.newLocation = loc;
}

CS_Window.prototype.locationReplace = function(win){
  var win = win || self;
  if(this.newLocation) win.location.href = this.newLocation;
  this.setNewLocation();
}

CS_Window.prototype.openDaughter = function(myurl,linkEl){
  var myurl = myurl || null;
  var linkEl = linkEl || null;
  if(typeof(myurl) == 'string'){
    this.children['customer_frameset'] = window.open('',"customer_frameset","width=725,height=450,location=yes,addressbar=no,menubar=yes,toolbar=yes,statusbar=yes,scrollbars=yes,resizable,left=250,top=150");
    if(this.children['customer_frameset'] && linkEl){
      if(linkEl.href.toLowerCase().indexOf('javascript') == -1)
        this.setNewLocation(linkEl.href);
      // Had to move call to replace parent window
      // to onload of child frameset because of IE Mac bug.
      if(linkEl.click){
        // the following resolves IE bug and maintains tracking
        linkEl.onclick = null;
        linkEl.href = myurl;
        linkEl.target="customer_frameset";
        linkEl.click();
      }else this.children['customer_frameset'].location.href = myurl;
      this.children['customer_frameset'].focus();
    }
  }
}

CS_Window.prototype.setStatus = function(str){
  var str = str || null;
  if(!debug && typeof(str) == 'string'){ 
    window.status = str;
    return true;
  } else return false;
}


CS_Window.prototype.openWindow = function(url,name,features){
  var url = url || '';
  var name = name || ('cs_window_' + this.children.length);
  var features = features || '';

  this.children[name] = window.open(url,name,features);
  return this.children[name];
}

CS_Window.prototype.sendRPCRequest = function(url){
  var url = url || null;

  if(!this.isRPCCapable || typeof(url) == null) return false;
  this.RPCStack[this.RPCStack.length] = url;
  this._processRPCStack();

}

CS_Window.prototype._processRPCStack = function(status){
  // This method is psuedoprivate to this object.
  // Do not call this method directly.
  // Please add this call to the stack with sendRPCRequest.
  var status = status || null;
  if(status != null) CS_Window.RPCElementAvail = true;
  var iframe = CS_Window.getRPCElement();

  if(iframe != null && CS_Window.RPCElementAvail && CS_Window.RPCStack.length > 0){
    CS_Window.RPCElementAvail = false;
    iframe.src = CS_Window.RPCStack[0];
    CS_Window.RPCStack = CS_Window.RPCStack.slice(1)
    if(CS_Window.RPCStack.length > 0) setTimeout('CS_Window._processRPCStack(true)',1000);
  }
}


CS_Window.prototype.getRPCElement = function(){
  if(this.RPCElement == null) return this._initRPCElement();
  else return this.RPCElement;
}

CS_Window.prototype._initRPCElement = function(){
  // private method
  // This method depends on the CS_DHTML object being loaded as well.
  if(!this.isRPCCapable || this.RPCElement != null) return this.RPCElement;
  var span = document.createElement('SPAN');
  span.id = this.RPCObjectId + 'Parent';
  document.body.appendChild( span );
  var iframe = document.createElement('IFRAME');
  iframe.name = this.RPCObjectId;
  iframe.id = this.RPCObjectId;
  span.appendChild( iframe );
  span.style.display = 'none';
  iframe.style.display = 'none';
  iframe.style.visibility = 'hidden';
  iframe.height = 0;
  iframe.width = 0;
  CS_DHTML.addEventListener(iframe,'load',CS_Window._processRPCStack);
  this.RPCElement = iframe; 

  return this.RPCElement;
}

CS_Window.prototype.newXMLHTTP = function(){
  var xmlhttp=false;
  /*@cc_on @*/
  /*@if (@_jscript_version >= 5)
  // JScript gives us Conditional compilation, we can cope with old 
  // IE versions and security blocked creation of the objects.
  try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
     xmlhttp = false;
    }
   }
   @end @*/

  if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    xmlhttp = new XMLHttpRequest();
  }

  return xmlhttp;
}

CS_Window.prototype.newXMLHTTPalternate = function() {
    var obj = false;
    var objectIDs = new Array(
                              "Microsoft.XMLHTTP",
                              "Msxml2.XMLHTTP",
                              "MSXML2.XMLHTTP.3.0",
                              "MSXML2.XMLHTTP.4.0"
                              );
    var success = false;

    for (i=0; !success && i < objectIDs.length; i++) {
      try {
        obj = new ActiveXObject(objectIDs[i]);
        success = true;
      } catch (e) { obj = false; }
    }

    if (!obj)
      obj = new XMLHttpRequest();

    return obj;
}


CS_Window.prototype.XMLHTTPping = function( url ){
  xmlhttp = CS_Window.newXMLHTTP();
  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
}

var CS_Window = new CS_Window;

