﻿function Form() {
    this.Form = null;
    this.Id = "";
    this.newDiv = null;
    this.BgColor = "#eee";
    this.Width = "";
    this.Height = "";
    this.Left = "";
    this.Top = "";
    this.zIndex = "999";
    this.Display = "";

    this.CreateForm = function(obj) {

        this.Form = obj.createElement("DIV");

        this.Form.id = this.Id;
        this.Form.style.setAttribute("position", "absolute");
        this.Form.style.setAttribute("backgroundColor", this.BgColor);
        this.Form.style.setAttribute("display", this._Display);
        this.Form.style.setAttribute("left", this.Left);
        this.Form.style.setAttribute("top", this.Top);
        this.Form.style.setAttribute("width", this.Width);
        this.Form.style.setAttribute("height", this.Height);
        this.Form.style.setAttribute("zIndex", this.zIndex);
        this.Form.Display = "block";
    }
}

function WebForm() {

    this.Show = function(obj, Id, zIndex, url) {

    var form = new Form();
        
        form.Id = Id;
        form.zIndex = zIndex;
        form.Top = 0;
        form.Left = 0;
        form.Width = window.screen.availWidth + 'px';
        
        form.Height = obj.body.clientHeight + 'px';
        
        form.CreateForm(obj);

        form.Form.style.setAttribute("filter", "Alpha(Opacity=90)");
        form.Form.style.setAttribute("opacity", "90 / 100");
        form.Form.style.setAttribute("MozOpacity", "90 / 100");
        form.Form.innerHTML = this.GetIFrame(url, 200, window.screen.availWidth / 2 - 180, obj);

        var newForm = form.Form;
        
        obj.body.appendChild(newForm);
    }

    this.GetIFrame = function(url, top, left, obj) {

        var XY = getScrollXY(obj);

        top = top + XY[1];
        left = left + XY[0];

        var strIFrame = "<iframe src='" + url + "' id='openform' marginwidth='0px' marginheight='0' style='top:" + top + ";left:" + left + ";position:absolute;width:360px;height:207px' onload='ReinitIframe(this.id)' frameborder='0' scrolling='no' ></iframe>";

        return strIFrame;
    }

    this.Remove = function(id, bodyId) {
        var WebElement = new WebElementControl();
        
        var parentBody = WebElement.GetParentElementByIdAndNodeName(bodyId, 'body', parent.document);
        var elems = parentBody.getElementsByTagName("div");
        var index = 0;
        for (index = 0; index < elems.length; index++) {
            if (elems[index].id == id)
                parentBody.removeChild(elems[index])
        }
    }
}

function getScrollXY(obj) {

  var scrOfX = 0, scrOfY = 0;
  
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } 
  else if( obj.body && ( obj.body.scrollLeft || obj.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = obj.body.scrollTop;
    scrOfX = obj.body.scrollLeft;
  } 
  else if( obj.documentElement && ( obj.documentElement.scrollLeft || obj.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = obj.documentElement.scrollTop;
    scrOfX = obj.documentElement.scrollLeft;
  }
  
  return [ scrOfX, scrOfY ];
}

function ReinitIframe(id) {

    try {
        var openWebForm = document.getElementById(id);

        if (openWebForm == null) {
            return;
        }

        var bmainHeight = openWebForm.contentWindow.document.body.scrollHeight;
        var dmainHeight = openWebForm.contentWindow.document.documentElement.scrollHeight;
        var mainHeight = Math.max(bmainHeight, dmainHeight);
        var height = mainHeight;

        var bmainWidth = openWebForm.contentWindow.document.body.scrollWidth;
        var dmainWidth = openWebForm.contentWindow.document.documentElement.scrollWidth;
        var mainWidth = Math.max(bmainWidth, dmainWidth);
        var width = mainWidth;
//        alert(height + "|" + width);
//        openWebForm.height = 207;//height;
//        openWebForm.width = 361;//width;

    //    window.setTimeout("ReinitIframe(" + id + ")", 500);
    }
    catch (e) {
        //    alert(e);
    }

    window.setTimeout("ReinitIframe(" + id + ")", 500);
}

function WebElementControl() {

    this.GetParentElementByIdAndNodeName = function(Id, NodeName, obj) {
        
        var IsFind = 0;
        var elems = obj.getElementsByTagName(NodeName);

        var index; 
        if(elems.length > 0)
            return elems[0];
        //for (index = 0; index < elems.length; index++) {
        //    if (elems[index].id == Id) {
        //        return elems[index];
        //    }
        //}
    }
}
