document.write('<link rel="stylesheet" type="text/css" href="../includes/dhtmlxWindows/dhtmlxwindows.css">\n');
document.write('<link rel="stylesheet" type="text/css" href="../includes/dhtmlxWindows/skins/dhtmlxwindows_dhx_blue.css">\n');
document.write('<script src="../includes/dhtmlxWindows/dhtmlxcommon.js"></script>\n');
//document.write('<script src="../includes/dhtmlxWindows/dhtmlxwindows.js"></script>\n');
document.write('<script src="../includes/dhtmlxWindows/dhtmlxwindows_ie.js"></script>\n');   //Made changes for scroll issues
document.write('<script src="../includes/browsercheck.js"></script>\n');


var iedhxWins1;
var iedhxWindow1;


function initdhxWins1(x, y, width, height) {
	
	//Testing code
	//alert(document.body);
	//alert(document.body.offsetHeight + " " + document.body.offsetLeft + "   " + document.body.offsetParent + "   " + document.body.offsetTop + "   " + document.body.offsetWidth);
	//alert(document.body.style.width + " " + document.body.style.height);
	//alert(document.body.scrollHeight);
	
	if (iedhxWins1 == null || !iedhxWins1.window("iedhxWindow1")) {
		iedhxWins1 = new dhtmlXWindows();
		iedhxWins1.setImagePath("includes/dhtmlxWindows/imgs/"); 
	}
	//Enable these two lines once viewport bug for other browsers is not an issue.
	//iedhxWins1.enableAutoViewport(false);
	//iedhxWins1.setViewport(0, 0, x+width+50, y+height+10); 
	
}

function creatWindow(x,y,width,height) {
	
	iedhxWindow1 = iedhxWins1.createWindow("iedhxWindow1", x,y,width,height);
	iedhxWindow1.denyPark(); 
	//iedhxWindow1.denyMove(); 
	//iedhxWindow1.denyResize();
	//iedhxWindow1.keepInViewport(true); 
	iedhxWindow1.button('park').hide();
	iedhxWindow1.button('help').hide();
	iedhxWindow1.button('stick').hide();
	iedhxWindow1.button('sticked').hide();
	iedhxWindow1.button('minmax1').hide();
	iedhxWindow1.button('minmax2').hide();
	
	//iedhxWindow1.hideHeader(); 		//Faster loading
	//iedhxWindow1.setModal(true);
	//iedhxWindow1.button("park").hide(); 
	//iedhxWindow1.clearIcon();
	//iedhxWindow1.setIcon("images/spacer.gif","images/spacer.gif"); 
	
}


function openWindow(title, winURL, x, y, width, height) {
	
	initdhxWins1(x,y,width,height);
	if (!iedhxWins1.window("iedhxWindow1")) {
	 	creatWindow(x,y,width,height);
	 	//alert("New Window:" + x+" "+y+" "+width+" "+height);
	} else {
		//alert("Existing Window:" + x+" "+y+" "+width+" "+height);
	}
	
	iedhxWindow1.attachURL(winURL);
	iedhxWindow1.setPosition(x,y);
	iedhxWindow1.setDimension(width,height);
	iedhxWindow1.setText(title);
	
	//iedhxWindow1.progressOn();
	//iedhxWindow1.denyMove(); 
	//iedhxWindow1.attachObject(id);
	//iedhxWindow1.setPosition(x,y)
	//iedhxWindow1.setDimension(width,height)
	//iedhxWindow1.attachURL("http://www.google.com");
	//iedhxWindow1.attachURL("http://www.google.com",true);   //AJAX	
	//iedhxWindow1.attachHTMLString("Hello Sam");

}


function openWindowWithEvent(event_str, title, winURL, x, y, width, height) {
	
	openWindow(title,winURL,x,y,width,height);
	iedhxWindow1.btns["close"]._doOnClick = function() {eval(event_str); closeWindow();};

}

function openWindowAtObjectPositionWithEvent(event_str, pageObject, title, winURL, pageObjectOffsetLeft, pageObjectOffsetTop, width, height) {
	
	openWindowAtObjectPosition(pageObject, title, winURL, pageObjectOffsetLeft, pageObjectOffsetTop, width, height);
	iedhxWindow1.btns["close"]._doOnClick = function() {eval(event_str); closeWindow();};
	
}

function openWindowAtObjectPosition(pageObject, title, winURL, pageObjectOffsetLeft, pageObjectOffsetTop, width, height) {
	
	pageObjectLeft = pageObject.offsetLeft;
	pageObjectTop = pageObject.offsetTop;
	while(pageObject.offsetParent!=null){
		pageObjectParent = pageObject.offsetParent;
		pageObjectLeft += pageObjectParent.offsetLeft;
		pageObjectTop += pageObjectParent.offsetTop;
		pageObject = pageObjectParent;
	}
	pageObjectLeft = pageObjectLeft + pageObjectOffsetLeft;
	pageObjectTop = pageObjectTop + pageObjectOffsetTop;
	
	thisTop = pageObjectTop;
	if ((pageObjectTop-(height/2)) > 0) {
		thisTop = pageObjectTop-(height/2);
	}
	openWindow(title, winURL, pageObjectLeft, thisTop, width, height);
		
}

function openWindowAtCenter (title, winURL, width, height) {
	getCenterWindowCoords(width, height);
	openWindow(title, winURL, leftOffset, topOffset, width, height);
}

function closeWindow() {
	
	if (iedhxWins1 != null && iedhxWins1.window("iedhxWindow1")) {
		iedhxWindow1.close(); 	// close window
	}
	
}


var leftOffset = 0;
var topOffset = 0;	
function getCenterWindowCoords(pop_up_width,pop_up_height) {
	
	leftOffset = 0;
	topOffset = 0;
	var scrolledX, scrolledY;
		
	if( self.pageYOffset ) {
		scrolledX = self.pageXOffset;
		scrolledY = self.pageYOffset;
	} else if( document.documentElement && document.documentElement.scrollTop ) {
		scrolledX = document.documentElement.scrollLeft;
		scrolledY = document.documentElement.scrollTop;
	} else if( document.body ) {
		scrolledX = document.body.scrollLeft;
		scrolledY = document.body.scrollTop;
	}
	
	var centerX, centerY;
	if( self.innerHeight ) {
		centerX = self.innerWidth;
		centerY = self.innerHeight;
	} else if( document.documentElement && document.documentElement.clientHeight ) {
		centerX = document.documentElement.clientWidth;
		centerY = document.documentElement.clientHeight;
	} else if( document.body ) {
		centerX = document.body.clientWidth;
		centerY = document.body.clientHeight;
	}
	leftOffset = scrolledX + (centerX - pop_up_width) / 2;
	if(leftOffset < 20) leftOffset = 20;

	topOffset = scrolledY + (centerY - pop_up_height) / 2;
	if(topOffset < 20) topOffset = 20;	

	//alert(leftOffset);
	//alert(topOffset);
	
}

function showHelp(pageObject, title, compID, helpType, helpUI, x, y, width, height) {

	var helpPageURL = "../showHelp.jsp?compID=" + compID + "&helpType=" + helpType + "&helpUI=" + helpUI;
	openWindowAtObjectPosition(pageObject, title, helpPageURL, x, y, width, height);
}

function showSpellCheck(html_enabled, id) {
	
	if (browser_name == 'Safari' || browser_name == 'Chrome') {
		alert('We have detected that you are using the ' + browser_name + ' browser. Please use the in-built spell-checker.');
	} else {
		var isBlank = false;
		
		if ( html_enabled ) {
			if (tinyMCE.getContent(id) == '')
				isBlank = true;
		} else {
			if (document.getElementById(id).value.length == 0)
				isBlank = true;	
		}
		
		if (isBlank) {
			alert('Please enter some text before running a spell check.');			
		} else {
			
			var pop_up_width = 500;
			var pop_up_height = 300;
			getCenterWindowCoords(pop_up_width,pop_up_height);
			openWindow("Spell Checker","spellchecker.jsp" + "?html_enabled=" + html_enabled + "&id=" + id,leftOffset,topOffset,pop_up_width,pop_up_height);	
		}		
	}	
}


function showSpellCheckCKEditor(id) {
	
	//browser_name == "Explorer" || browser_name == "Firefox" || browser_name == "Chrome" || browser_name == "Safari"
	if (browser_name == 'Opera') {   
		alert('We have detected that you are using the ' + browser_name + ' browser. Please use the in-built spell-checker.');
	} else {
		var isBlank = false;
		
		if (CKEDITOR.instances[id].getData().length == 0) {
			isBlank = true;	
		}
		
		if (isBlank) {
			alert('Please enter some text before running a spell check.');			
		} else {
			
			var pop_up_width = 575;
			var pop_up_height = 250;
			getCenterWindowCoords(pop_up_width,pop_up_height);
			openWindow("Spell Checker", "spellcheckckeditor.jsp?element_id=" + id, leftOffset, topOffset+10, pop_up_width, pop_up_height);	
		}		
	}	
}

