
var _sZIndex = 0;
var _sID = 0;

var _inPrototype = false;
function sExtend(fConstr, fSuperConstr, sName) {
	_inPrototype = true;
	var p = fConstr.prototype = new fSuperConstr;
	if (sName) {
		p.oName = sName;
	}
	p.constructor = fConstr;
	_inPrototype = false;
	return p;
}

function clearElement(element){
  if (element)
    element.parentNode.removeChild(element);
}

function sWindows(){

}

sWindows.scrollLeft = function(){
  return (sBrowser.ie) ? document.body.scrollLeft : window.pageXOffset; 
}

sWindows.scrollTop = function(){
  return (sBrowser.ie) ? document.body.scrollTop : window.pageYOffset;
}

sWindows.scrollWidth = function(){
  return document.body.scrollWidth;
}
sWindows.scrollHeight = function(){
  return document.body.scrollWidth;
}
sWindows.innerWidth = function(){
  return document.body.clientWidth;
}
sWindows.innerHeight = function(){
  return document.body.clientHeight;
}

sWindows.width = function(){
  return screen.width;
}
sWindows.height = function(){
  return screen.height;
}

function sBrowser(){

  var ua = navigator.userAgent;
	var _ie = /msie/i.test( ua );
	var _moz = navigator.product == "Gecko";
	var _platform = navigator.platform;
	var _version = "";
	var _hta = "";
	if ( _moz ){
		/rv\:([^\);]+)(\)|;)/.test( ua );
		_version = RegExp.$1;
		_hta = false;
	}
	else{
		/MSIE\s+([^\);]+)(\)|;)/.test( ua );
		_version = RegExp.$1;
		_hta = !window.external;
	}
	
	this.getIE = function(){
    return _ie;
  }
  
  this.getMoz = function(){
    return _moz;
  }
  
  this.getHta = function(){
    return _hta;
  }
  
  this.getVer = function(){
    return _version;
  }
}

var sb = new sBrowser;

sBrowser.ie = sb.getIE();
sBrowser.moz = sb.getMoz();
sBrowser.ver = sb.getVer();
sBrowser.hta = sb.getHta();

function SElement(){
  this._DOC = document;
  this._width = 0;
  this._height = 0;
  this._top = 0;
  this._left = 0;
  this._bottom = 0;
  this._right = 0;
  this._position = "absolute";
  this._childs = new Array();
  this._parent = false;
  this._events = new Array();
  this._displayed = false;
  
  this.create();
  this.setPosition(this._position);
  
  this.setId(this.oName+"-"+_sID++);
  
}

_p = SElement.prototype;

_p.__this = function(){ return this; }

_p.toString = function(){ return "[object "+this.oName+"]"; }
_p.addForDoc = function(){ 
  this._DOC.body.appendChild(this._element); 
  this._displayed = true;
  this.doEvent("displayed");  
}
_p._dispalyed = function(){
  
}
_p.removeForDoc = function(){ this._element.parentNode.removeChild(this._element); this._displayed = false; }
_p.create = function(){ 
  this._element = this._DOC.createElement(this.tagName);
  this._event = new sEvent(this._element); 
}

_p.setAttribute = function(attr, v){this._element.setAttribute(attr, v);}
_p.getAttribute = function(v){ return this._element.getAttribute(v);}

_p.setId = function(id){ this.setAttribute("id", id); }
_p.getId = function(){ return this.getAttribute("id"); }

_p.setTitle = function(v){ this.setAttribute("title", v); }
_p.getTitle = function(){ return this.getAttribute("title"); }

_p.setTop = function(t){
  this._element.style.top = t+"px";
  this._top = t;
}

_p.setBottom = function(t){
  this._element.style.bottom = t+"px";
  this._bottom = t;
}

_p.setLeft = function(l){
  this._element.style.left = l+"px";
  this._left = l;
}

_p.setRight = function(l){
  this._element.style.right = l+"px";
  this._right = l;
}
_p.setWidth = function(w){
  this._element.style.width = w+"px";
  this._width = w;
}

_p.setHeight = function(h){
  this._element.style.height = h+"px";
  this._height = h;
}

_p.setMaxHeight = function(h){
  if (sBrowser.ie){
    this.setDisplay("block");
    this.setHeight(h);
  }
  else{
    this._element.style.minHeight = h+"px";
    this._height = h;
  }
}

_p.setMinHeight = function(h){
  if (sBrowser.ie){
    this.setDisplay("block");
    this.setHeight(h);
  }
  else{
    this._element.style.minHeight = h+"px";
    this._height = h;
  }
}

_p.getTop = function(){ return this._top; }
_p.getBottom = function(){ return this._bottom; }
_p.getLeft = function(){ return this._left; }
_p.getRight = function(){ return this._right; }
_p.getWidth = function(){ return this._width; }
_p.getHeight = function(){ return this._height; }

_p.setBounds = function(width, height, top, left){
    this.setWidth(width);
    this.setHeight(height);
    this.setTop(top);
    this.setLeft(left);
}
_p.getBounds = function(){
  return {
    width: this.getWidth(), height: this.getHeight(),
    top: this.getTop(), left: this.getLeft()
  };
}
_p.setSize = function(w, h){ this.setWidth(w); this.setHeight(h); }

_p.setZIndex = function(zInd){ this._element.style.zIndex = zInd; }
_p.getZIndex = function(){ return this._element.style.zIndex; }
_p.setPosition = function(pos){
  if ( (pos == "static") || (pos == "relative") || (pos == "absolute") || (pos == "fixed") )
    this._element.style.position = pos;
  else this._element.style.position = "static";
}

_p.setPadding = function(v){ this._element.style.padding = v; }
_p.setVisible = function(v){ 
  this._element.style.visibility = v ? "visible" : "hidden";
  if (v && this._events["visible"])
    new this._events["visible"];
  else if (this._events["hidden"])
    new this._events["hidden"];
}
_p.setDisplay = function( v ){ this._element.style.display = v; }
_p.getDisplay = function( v ){ return this._element.style.display; }
_p.setCursor = function( v ){ this._element.style.cursor = v; }
_p.getCursor = function( v ){ return this._element.style.cursor; }
  
_p.setOverflow = function(v){
  if ( (v == "visible") || (v == "hidden") || (v == "scroll") || (v == "auto") )
    this._element.style.overflow = v;
  else this._element.style.overflow = "auto";
}
  
_p.setCssClassName = function(v){ this._element.className = v; }  
_p.getCssClassName = function(){ return this._element.className; }

_p.setVerticalAlign = function(v){ this._element.style.verticalAlign = v; }
_p.setAlign = function(v){ this._element.style.textAlign = v; }
_p.getAlign = function(v){ return this._element.style.textAlign; }
  
_p.setBackgroundColor = function(v){ this._element.style.backgroundColor = v;}
_p.getBackGroundColor = function(){ return this._element.style.backgroundColor;}
_p.setBackground = function(v){ this._element.style.background = v; }
_p.getBackground = function(){ return this._element.style.background; }
_p.setBorder = function(v){ this._element.style.border = v; }
_p.getBorder = function(v){ return this._element.style.border; }
  
_p.setPaddingTop = function(v){ this._element.style.paddingTop = v; }
_p.setPaddingBottom = function(v){ this._element.style.paddingBottom = v; }
_p.setPaddingLeft = function(v){ this._element.style.paddingLeft = v; }
_p.setPaddingRight = function(v){ this._element.style.paddingRight = v; }
_p.setMargin = function(v){ this._element.style.margin = v; }
_p.setMarginTop = function(v){ this._element.style.marginTop = v; }
_p.setMarginBottom = function(v){ this._element.style.marginBottom = v; }
_p.setMarginLeft = function(v){ this._element.style.marginLeft = v; }
_p.setMarginRight = function(v){ this._element.style.marginRight = v; }

_p.setLeftBorder = function(v){ this._element.style.borderLeft = v; }
_p.setRightBorder = function(v){ this._element.style.borderRight = v; }
_p.setTopBorder = function(v){ this._element.style.borderTop = v; }
_p.setBorderBottom = function(v){ this._element.style.borderBottom = v; }

_p.getLeftBorder = function(){ return this._element.style.borderLeft; }
_p.getRightBorder = function(){ return this._element.style.borderRight; }
_p.getTopBorder = function(){ return this._element.style.borderTop; }
_p.getBorderBottom = function(){ return this._element.style.borderBottom; }

_p.setColor = function(v){ this._element.style.color = v;}
_p.getColor = function(v){ return this._element.style.color;}

_p.setOpacity = function(o){
  if (sBrowser.moz)
    this._element.style.opacity = (o/100);
  else
    if (sBrowser.ie)
      this._element.style.filter = "alpha(opacity="+o+");";

}

_p.inheritLayout = function(){
    this._element.style.top = "0px";
    this._element.style.left = "0px";
    this.setWidth(this._element.parentNode.offsetWidth);
    this.setHeight(this._element.parentNode.offsetHeight);
    if (this._element.parentNode.scrollWidth > this._element.parentNode.offsetWidth){
      this.setWidth(this._element.parentNode.scrollWidth);
    }
    
    if (this._element.parentNode.scrollHeight > this._element.parentNode.offsetHeight){
      this.setHeight(this._element.parentNode.scrollHeight);
    }
}
_p.setSizeForWindow = function(){ this.inheritLayout(); }

_p.setInheritPosition = function(h, v){
    switch (h){
      case "left":
        this.setLeft(0);
        break;
      case "center":
        if (this._width < this._element.parentNode.offsetWidth){
          this.setLeft((this._element.parentNode.offsetWidth/2)-(this._width/2)+this._element.parentNode.scrollLeft);
        }
        else this.setLeft(0);
        break;
      case "right":
        this.setRight(0);
        break;
    }
    switch (v){
      case "top":
        this.setTop(0);
        break;
      case "center":
        //alert(this._element.parentNode.clientHeight);
        if (this._height < this._element.parentNode.clientHeight)
          this.setTop( (this._element.parentNode.clientHeight/2)-(this._height/2)+this._element.parentNode.scrollTop);
        else this.setTop(0);
        break;
      case "bottom":
        this.setBottom(0);
        break;
    }
  }

_p.dispose = function(){
  _p = null;
  //alert(_p);
}

_p.getElement = function(){ return this._element; }

_p.addEvent = function ( action, onEvent ){
  this._event.addEvent(action, onEvent);
}
_p.doEvent = function(action){
  if (this._events[action])
    new this._events[action];
}

_p.removeEvent = function(v){
  this.addEvennt(v, function(){});
  var ev = new Array();
  for (i in this._events)
    if (i != v)
      ev[i] = this._events[i];
  this._events = new Array();
  this._events = ev;
}

function sComponent(){
  SElement.call(this);
}

_c = sExtend(sComponent, SElement, "sComponent");
_c.tagName = "div";

_c.getPane = function(){return this._element} ;
_c.add = function(obj){
  if (obj._element){
    this.getPane().appendChild( obj.getElement() );
    obj._parent = this;
    obj._displayed = true;
    obj.doEvent("displayed");
    //obj.setVisible(true);
    this._childs[this._childs.length] = obj;
  }
}
_c.removeChild = function(obj){
    var restChild = new Array();
    var remChild = false;
    if (this._childs.length)
      for (var i=0; i<this._childs.length; i++){
        if (this._childs[i] == obj){
          this._childs[i].removeForDoc();
          remChild = true;
        }
        else
          restChild[restChild.length] = this._childs[i];
      }
    this._childs = new Array;
    this._childs = restChild;
    return remChild;
}

_c.removeAll = function(){
    var remChild = false;
    if (this._childs.length)
      for (var i=0; i<this._childs.length; i++){
        this._childs[i].removeForDoc();
      }
    this._childs = new Array();
}

_c.addHtmlObject = function(obj){
    this._element.appendChild( obj );
    this._childs[this._childs.length] = obj;
}
_c.getParent = function(){ return this._parent }
_c.getChildren = function(){ return this._childs; }

_c.setContent = function(c){ this._element.innerHTML = c; }
_c.getContent = function(c){ return this._element.innerHTML; }
_c.deleteContent = function(){ this._element.innerHTML = ""; }

_c.setScrollLeft = function(v){ this._element.scrollLeft += v; };
_c.getScrollLeft = function(){ return this._element.scrollLeft; };
_c.setScrollTop = function(v){ this._element.scrollTop += v; };
_c.getScrollTop = function(){ return this._element.scrollTop; };

function sJSLoader(SRC, doc){
  var parentElement
  if (!doc)
    parentElement = document.body;
  else parentElement = document.getElementsByTagName("HEAD")[0];
  
  var scriptObj = document.createElement("SCRIPT");
  scriptObj.setAttribute("type", "text/javascript");
  scriptObj.src = SRC;
  parentElement.appendChild(scriptObj);
}

function pharseJSInHTMLText(element){
	
		var scripts = element.getElementsByTagName("script");

	  for (var i=0; i<scripts.length; i++){
	    if (scripts[i].src != ""){
	      this.loadScript(scripts[i].src);
	    }
	    else
	      eval(scripts[i].innerHTML);
	  }
	  
	};

function simpleLoader(dataSource, postData, method, DIV, async){

  var XMLHttpRequestObject = false;
  
  if (window.XMLHttpRequest) {
    XMLHttpRequestObject = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
      try {
        XMLHttpRequestObject = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
          try {
            XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
          } catch (e) {
              XMLHttpRequestObject = false;
            }
      }
    }
  if(XMLHttpRequestObject) {
    
    XMLHttpRequestObject.open(method, dataSource, async);
    XMLHttpRequestObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    XMLHttpRequestObject.setRequestHeader("Content-length", postData.length);
    
    XMLHttpRequestObject.onreadystatechange = function(){
      if ((XMLHttpRequestObject.readyState == 4) && (XMLHttpRequestObject.status == 200)){
          
          if (DIV){
          
          	document.getElementById(DIV).innerHTML = XMLHttpRequestObject.responseText;
          	pharseJSInHTMLText(document.getElementById(DIV));
          	
          }
      }
    }
    XMLHttpRequestObject.send(postData);
  }

}

function _ajaxLoader( o ){ 

  var XMLHttpRequestObject = false;
  
  if (window.XMLHttpRequest) {
    XMLHttpRequestObject = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
      try {
        XMLHttpRequestObject = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
          try {
            XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
          } catch (e) {
              XMLHttpRequestObject = false;
            }
      }
    }
   if(XMLHttpRequestObject) {
    
    XMLHttpRequestObject.open(o.method || "POST", o.url, o.async || true);
    XMLHttpRequestObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    XMLHttpRequestObject.setRequestHeader("Content-length", o.qStr.length);
    
    XMLHttpRequestObject.onreadystatechange = function(){
      if ((XMLHttpRequestObject.readyState == 4) && (XMLHttpRequestObject.status == 200)){
          
          if (o.DIV)
           document.getElementById(o.DIV).innerHTML = XMLHttpRequestObject.responseText;
          
          if (o.jspharser){
            var jsp = document.createElement("div");
            jsp.innerHTML = XMLHttpRequestObject.responseText;
            pharseJSInHTMLText(jsp);
          }
           
          if (o.success)
            o.success(XMLHttpRequestObject.responseText);
      }
    }
    XMLHttpRequestObject.send(o.qStr);
  }

}

function sDrag(){

  var dragging = false;
  var top;
  var left;
  var dragStartTop;
  var dragStartLeft;
  var dragblElement = false;
  var innerDiv;
  var outerDiv;
  
  this.DragElement = function(Div, movieDiv) {
    this.dragblElement = Div;
    dragblElement = Div;
    
    if (movieDiv) 
      clickDiv = movieDiv;
    else
      clickDiv = Div;
    
    outerDiv = clickDiv;
    outerDiv.onmousedown = this.startMove;
    document.body.onmouseup = this.stopMove;
    outerDiv.onmousemove = this.processMove;
    outerDiv.onmouseout = this.processMove;

    // necessary to enable dragging on IE
    innerDiv = this.dragblElement;
    outerDiv.ondragstart = function(){ 
      return false; 
    }
  }
  
  this.setInnerDivSize = function (width, height) {
    this.dragblElement.style.width = width;
    this.dragblElement.style.height = height;
  }
  
  this.startMove = function (event) {
    // necessary for IE
    if (!event) 
      event = window.event;
    window.onmousemove = move;
    dragStartLeft = event.clientX;
    dragStartTop = event.clientY;
    innerDiv.style.cursor = "move";
    top = stripPx(innerDiv.style.top);
    left = stripPx(innerDiv.style.left);
    dragging = true;
    return false;
  }
  
  this.processMove = function (event) {
    if (!event) event = window.event; // for IE
    
    if (dragging) {
      innerDiv.style.top = top + (event.clientY - dragStartTop);
      innerDiv.style.left = left + (event.clientX - dragStartLeft);
    }
  }
  
  function move(event){
    if (!event) event = window.event; // for IE

    if (dragging) {
      innerDiv.style.top = top + (event.clientY - dragStartTop);
      innerDiv.style.left = left + (event.clientX - dragStartLeft);
    }
  }
  
  this.stopMove = function () {
    innerDiv.style.cursor = "";
    dragging = false;
    //document.body.onmousemove = function(){};
  }
}

function stripPx(value) {
  if (value == "") return 0;
  return parseFloat(value.substring(0, value.length - 2));
}


function sWindow(c){
  sComponent.call(this);
  
  this.setCssClassName("s_window");
  
  this._header = document.createElement("div");
  
  var _caption = document.createElement("div");
  var _icon = document.createElement("div");
  this._closebutton = document.createElement("div");
  _icon.className = "s_cell";
  this._closebutton.className = "s_cellr s_closebutton";
  this._header.className = "s_windowHeader";
  
  this.setHeaderCssClassName = function(v){ this._header.className = v; }
  this.getHeaderCssClassName = function(){ return this._header.className; }
  
  this.setHeaderBackgroundColor = function(v){ this._header.style.backgroundColor = v; }
  this.getHeaderBackgroundColor = function(){ return this._header.style.backgroundColor; }
  this.setHeaderColor = function(v){ this._header.style.color = v; }
  this.getHeaderColor = function(){ return this._header.style.color; }
  
  if (sBrowser.ie){ 
    this._closebutton.style.backgroundPosition = "2px 1px";
    this._closebutton.style.width = "14px";
  }
  
  var element = this.getElement();
  var _this = this;
  
  this._closebutton.onclick = function(){
    element.parentNode.removeChild(element);
  };
  
  _caption.className = "s_cell s_caption";
  if (c) _caption.innerHTML = c;

  this._header.appendChild(_icon);
  this._header.appendChild(_caption);
  this._header.appendChild(this._closebutton);
  
  this._element.appendChild(this._header);
  this._header.style.height = "19px";
  
  var drag = new sDrag();
  drag.DragElement(this._element, this._header);
  
  this.setCaption = function(c){ _caption.innerHTML = c; }
  this.getCaption = function(c){ return _caption.innerHTML; }
  this.changeCloseButton = function(v){ this._closebutton.style.visibility = v ? "visible" : "hidden"; }
  this.changeCaption = function(v){ _caption.style.visibility = v ? "visible" : "hidden"; }
  this.changeIcon = function(v){ _icon.style.visibility = v ? "visible" : "hidden"; }
  
  this._element.onmousedown = function(){
    _this.changeUp();
  }
  this.changeUp();
  
  this.sThis = function(){ return this}
  //this.headerMinimize(true);
}

_p = sExtend(sWindow, sComponent, "sWindow");

_p.headerMinimize = function(v){
  var height = this.getHeight();
  var element = this._element;
  if (v)
    this._header.ondblclick = function(){
      //alert(element.style.height);
      if (element.style.height != "21px")
        element.style.height = "21px";
      else element.style.height = height+"px";
    }
  else this._header.ondblclick = function(){};
  }

_p.close = function(){
  if (this._events["close"])
    a = new this._events["close"];
  this.removeForDoc();
}

_p.changeUp = function(){
  this.setZIndex(_sZIndex+1);
  _sZIndex++;
}

_p.addEvent = function(action, onEvent){
  var element = this._element;
  switch (action){
    case "click":
      this._element.onclick = onEvent;
      break;
    case "change":
      this._element.onchange = onEvent;
      break;
    case "dblclick":
      this._element.ondblclick = onEvent;
      break;
    case "mouseover":
      this._element.onmouseover = onEvent;
      break;
    case "mousedown":
      var _this = this.sThis(); 
      this._element.onmousedown = function(){
        new onEvent;
        _this.changeUp();
      };
      break;
    case "mousemove":
      this._element.onmousemove = onEvent;
      break;
    case "mouseup":
      this._element.onmouseup = onEvent;
      break;
    case "keypress":
      this._element.onkeypress = onEvent;
      break;
    case "keydown":
      this._element.onkeydown = onEvent;
      break;
    case "keyup":
      this._element.onkeyup = onEvent;
      break;
    case "close": 
      var closeEvent = onEvent;
      this._closebutton.onclick = function(){
        new closeEvent;
        element.parentNode.removeChild(element);
      }
      break;
  }
  this._events[action] = onEvent;
}

function sEvent(element){
  
  this._element = element;
  this._events = new Array();
  
  this.addEvent = function(action, onEvent){
    if ( !this._events[action] )
      this._events[action] = new Array();
    this._events[action][this._events[action].length] = onEvent;
  
    if (this._element.addEventListener)
      this._element.addEventListener(action, onEvent, false);
    else if (this._element.attachEvent)
      this._element.attachEvent( "on"+action, onEvent );
  
  }
  
  this.addEventListener = this.addEvent;
  
  this.removeEvent = function(action, onEvent){
    if (this._element.addEventListener)
      this._element.removeEventListener(action, onEvent, false);
    else if (this._element.attachEvent)
      this._element.detachEvent ( "on"+action, onEvent );
    
    if (this._element.addEventListener)
      this._element.removeEventListener(action, onEvent, false);
    else if (this._element.attachEvent)
      this._element.detachEvent ( "on"+action, onEvent );
  }
  
  this.removeEventListener = this.removeEvent;
  
  this.doEvent = function(action){
    if (this._events[action])
      for (var i in this._events[action]){
        new this._events[action][i];
      }
  }
  
  this.clearEvent = function(action){
    for (var i in this._events[action]);
      this.removeEvent(action, this._events[action][i]);
  }

}

__sBodyEvent = new sEvent(document.body);

__emailWin = false;

function csSiteLoading(url, x, y, caption){
	var ldisableP = new sComponent();
	
	ldisableP.setZIndex(1);
  ldisableP.setBackgroundColor("#000000");
	ldisableP.setOpacity(70);
	ldisableP.addForDoc();
		
  ldisableP.setVisible(true);
  ldisableP.setSizeForWindow();
    
  var osWin = new sWindow("Story");
  if (caption)
    osWin.setCaption(caption);
  if (x)
    osWin.setWidth(x);
  if (y && y!='auto')
    osWin.setHeight(y);
  else osWin.getElement().style.minHeight="40px";

  osWin.setCssClassName("window");
  osWin.setBackgroundColor("#ffffff");
  osWin.setVisible(true);
	//osWin.setOverflow("auto");

  osWin.addForDoc();
  osWin.setInheritPosition("center", "center");
  if (y=='auto')
  	osWin.setTop(200);
  	
  var cont = new sComponent();
  cont.setPosition("relative");
  if (x)
    cont.setWidth(x-2);
  if (y && y!='auto')
    cont.setHeight(y-22);

  cont.setLeft(1);
 	//cont.setOverflow("auto");
	
	osWin.add(cont);
	
	ldisableP.addEvent("click", function( e ){
    osWin.close();
  });
	
  osWin.addEvent("close", function( e ){
    ldisableP.removeForDoc();
  });
  
  __emailWin = osWin;
  
  if (url)
    simpleLoader(url, '', "POST", cont.getId(), true);
    
  return cont.getElement();
}