
var TSoftJavaScriptUtil = new Object();

// Creates a new XML-HTTP object in supported browsers,
// otherwise returns NULL

TSoftJavaScriptUtil.xmlHttpRequest = function () {
	var xmlHttpRequest = null;
	if ( typeof ActiveXObject == 'function' ) {
		try {
			xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");	
		} catch ( e ) {}
	} 
	if ( xmlHttpRequest == null && typeof ActiveXObject == 'function' ) {
		try {
			xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");	
		} catch ( e ) {}
	} 
	if ( xmlHttpRequest == null && (XMLHttpRequest) ) {
		try {
			xmlHttpRequest = new XMLHttpRequest();	
		} catch ( e ) {}
	}
	if ( xmlHttpRequest == null && typeof (window.createRequest) == 'function' ) {
		try {
			xmlHttpRequest = window.createRequest();	
		} catch ( e ) {}
	}
	return xmlHttpRequest;
};

TSoftJavaScriptUtil.xmlHttpResponse = function( url, parameters ) {
	var xmlHttp = TSoftJavaScriptUtil.xmlHttpRequest();
	if ( ! xmlHttp )
		return null;
	try {
		document.body.style.cursor = 'wait';
		xmlHttp.open('POST', url, false);
		xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded; charset=utf-8');
		if ( parameters == null )
			parameters = "";
		xmlHttp.send(parameters);
		if ( xmlHttp.readyState != 4 )
			return null;
		return xmlHttp;
	} catch ( e ) {
		return null;
	} finally {
		document.body.style.cursor = 'default';
	}
}


TSoftJavaScriptUtil.addEventListener = function( target, method, func ) {
	if ( ! (typeof func == "function") || ! (typeof method == "string") )
		return;
	if ( typeof target[method] == "function" ) {
		var oldfunc = target[method];
		target[method] = function () {
			try { oldfunc.apply( target, arguments ); } catch ( e ) {}
			func.apply( target, arguments );
		}
	} else {
		target[method] = func;
	}
}


TSoftJavaScriptUtil.getPageSize = function(){

    var xScroll, yScroll;

    if (window.innerHeight && window.scrollMaxY) {
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }

    var windowWidth, windowHeight;


    if (self.innerHeight) {    // all except Explorer
        if(document.documentElement.clientWidth){
            windowWidth = document.documentElement.clientWidth;
        } else {
            windowWidth = self.innerWidth;
        }
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }

    if(yScroll < windowHeight){
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }


    if(xScroll < windowWidth){
        pageWidth = xScroll;
    } else {
        pageWidth = windowWidth;
    }

    return { pageWidth : pageWidth, pageHeight : pageHeight, windowWidth : windowWidth, windowHeight : windowHeight } ;
}


TSoftJavaScriptUtil.getScrollOffset = function () {
	  var scrOfX = 0, scrOfY = 0;
	  if( typeof( window.pageYOffset ) == 'number' ) {
	    //Netscape compliant
	    scrOfY = window.pageYOffset;
	    scrOfX = window.pageXOffset;
	  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
	    //DOM compliant
	    scrOfY = document.body.scrollTop;
	    scrOfX = document.body.scrollLeft;
	  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
	    //IE6 
	    scrOfY = document.documentElement.scrollTop;
	    scrOfX = document.documentElement.scrollLeft;
	  }
	  return { x: scrOfX, y : scrOfY };
}



TSoftJavaScriptUtil.virtualDialogHolder = function() {
	
	var w = window;
	while ( w.parent && w.parent.window != w && w.parent.window.TSoftJavaScriptUtil ) {
		w = w.parent.window;
	}

	if ( w !== window ) {
		var pvd =  w.TSoftJavaScriptUtil.virtualDialogHolder;
		return {
			openDialog:  function(url, width, height) { return pvd.openDialog(url, width, height) },
			closeDialog: function() { return pvd.closeDialog() }
		};
	}
	
	var virtualDialog = null;
	var resizeListener = null;
	var align = function() {
		if ( virtualDialog != null ) {
			var vsize = TSoftJavaScriptUtil.getPageSize();
			var scroll = TSoftJavaScriptUtil.getScrollOffset();
			
			var y = (vsize.windowHeight - virtualDialog.height - 40) / 2;
			virtualDialog.border.style.marginTop = Math.max((scroll.y+y),0) + "px";
		}	}
	var resize = function() {
		if ( virtualDialog != null ) {
			align();
			virtualDialog.container.style.width="100%";
			virtualDialog.container.style.height = TSoftJavaScriptUtil.getPageSize().pageHeight+"px";
		}
	}
	var scroll = function() {
		align();
	}
	var close = function() {
		virtualDialog.container.parentNode.removeChild(virtualDialog.container);
		//window.removeEventListener("resize", resize, false );
		virtualDialog = null;
		//document.getElementById('mainTable').style.visibility = 'visible';
	};
	
	TSoftJavaScriptUtil.addEventListener( window, "onresize", resize );
	TSoftJavaScriptUtil.addEventListener( window, "onscroll", scroll );

	return {
		openDialog: function( url, width, height ) {
			if ( virtualDialog == null ) {
				//document.getElementById('mainTable').style.visibility = 'hidden';


				//var top = window.pageYOffset ? window.pageYOffset : ((document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop);
				var div = document.createElement("div");


				div.style.position="absolute";
				div.style.top= "0px";
				div.style.left="0px";

				var iframe = document.createElement("iframe");
				iframe.id = "windowiframe";
				iframe.style.width = width + "px";
				iframe.style.height = height + "px";
				iframe.style.border = "none";

				var headerTable = document.createElement("table");
				headerTable.border = "0";
				headerTable.cellSpacing = 0;
				headerTable.cellPadding = 0;
				headerTable.className = "dialogHeaderTable";
				var headerRow = headerTable.insertRow(0);
				headerRow.insertCell(0);
				headerRow.insertCell(0);

				headerRow.cells[0].className = "dialogWindowTitle";
				headerRow.cells[0].appendChild( document.createTextNode("\u00A0"));
				headerRow.cells[1].className = "dialogWindowButton";
				var closeButton = document.createElement("div");
				closeButton.onclick = close;
				closeButton.className = "dialogWindowClose";
				headerRow.cells[1].appendChild(closeButton);

				// Borders:
				var cellClasses = new Array(
					new Array( "dialogWindowHeader1",  "dialogWindowHeader2",  "dialogWindowHeader3" ),
					new Array( "dialogWindowContent1", "dialogWindowContent2", "dialogWindowContent3" ),
					new Array( "dialogWindowFooter1",  "dialogWindowFooter2",  "dialogWindowFooter3" )
				);

				var borderTable = document.createElement("table");
				div.appendChild(borderTable);
				borderTable.style.marginTop = (TSoftJavaScriptUtil.getScrollOffset().y+50) + "px";
				borderTable.border = "0";
				borderTable.align = "center";
				borderTable.cellSpacing = 0;
				borderTable.cellPadding = 0;
				for ( var iRow = 0; iRow < cellClasses.length; iRow++ ) {
					var row = borderTable.insertRow(iRow);
					for ( var iCell = 0; iCell < cellClasses[iRow].length; iCell++ ) {
						var cell = row.insertCell(iCell);
						cell.className = cellClasses[iRow][iCell];
						if ( iCell != 1 || iRow != 1 ) {
							cell.appendChild(document.createElement("div"));
						} else {
							cell.appendChild(headerTable);
							cell.appendChild(iframe);
						}
					}
				}
				div.className = 'dialogFilter';
				virtualDialog = { 
					container: div, 
					border: borderTable,
					url: url,
					width: width,
					height: height
				};

				iframe.src = url;
				iframe.border = "0";
				iframe.frameBorder = "0";
				document.body.appendChild(virtualDialog.container);

				resize();
				return true;
			} else {
				return false;
			}
		},
		closeDialog: close
	};
} ();


