/*

ModalBox.js by Christine Panus & Brad Wood


*/


function pageWidth() {
	if (window.innerWidth != null) {
		return window.innerWidth;
	} else if (document.documentElement && document.documentElement.clientWidth) {
		return document.documentElement.clientWidth; 
	} else if (document.body != null) {
		return document.body.clientWidth;
	} else {
		return null;
	}
}
function pageHeight() {
	if (window.innerHeight != null ) {
		return window.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		return document.documentElement.clientHeight;
	} else if (document.body != null) {
		return document.body.clientHeight;
	} else {
		return null;
	}
}
function posLeft() {
	if (typeof(window.pageXOffset) != 'undefined') {
		return window.pageXOffset;
	} else if ( document.documentElement && document.documentElement.scrollLeft ) {
		return document.documentElement.scrollLeft;
	} else if (document.body.scrollLeft) {
		return document.body.scrollLeft;
	} else {
		return 0;
	}
}
function posTop() {
	if (typeof(window.pageYOffset) != 'undefined') {
		return window.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		return document.documentElement.scrollTop;
	} else if (document.body.scrollTop) {
		return document.body.scrollTop;
	} else {
		return 0;
	}
}
//scrollFix keeps the div covering the whole screen even when the user scrolls.
function scrollFix() {
    var greyDiv = document.getElementById('modalInactiveBox');
    var theBox = document.getElementById('modalBox');
	var pagePositionLeft = posLeft();
	var pagePositionTop = posTop();
    var newModalPositionLeft = pagePositionLeft + dragCoordinates[0];
	var newModalPositionTop = pagePositionTop + dragCoordinates[1];
    greyDiv.style.top = (pagePositionTop < 0 ? 0 : pagePositionTop) + 'px';
    greyDiv.style.left = (pagePositionLeft < 0 ? 0 : pagePositionLeft) + 'px';
    theBox.style.top = (newModalPositionTop < 0 ? 0 : newModalPositionTop) + 'px';
    theBox.style.left = (newModalPositionLeft < 0 ? 0 : newModalPositionLeft) + 'px';
}
//sizeFix keeps the div covering the entire screen
function sizeFix() {
    var greyDiv = document.getElementById('modalInactiveBox');
    greyDiv.style.height = pageHeight() + 'px';
    greyDiv.style.width = pageWidth() + 'px';
}
/* function modalKeyPress(e) {
    keys = e ? e.which : event.keyCode;
    if (keys == 88 || keys == 120) {
        hideModalDiv();
    }
    return false;
}
*/

function setModalTitle(newtitle)
	{
		 var titlebox = document.getElementById('modalTableTopRowLeft');
		     titlebox.innerHTML = newtitle;
		 
	}
	// New method to change popup without re-creating
function setModalInnerHTML(newHTML)
	{
	    document.getElementById('embeddedContent').innerHTML = newHTML;		 
	}
	// end 
function showModalDiv(type, title, content, modalWidth, modalHeight, modalClass, forceModal, contentHTML, followScroll, initPosRelativeTo, initPosX, initPosY) { //4047NTS07  //10244NTS07
	var greyDiv = document.getElementById('modalInactiveBox');
    var modalClass = (modalClass == null) ? "" : modalClass; //4047NTS07 
	// default values
    var forceModal = (forceModal == null) ? true : forceModal; 
    var content = (content == null) ? "" : content; 
    var contentHTML = (contentHTML == null) ? '' : contentHTML; 
    var followScroll = (followScroll == null) ? true : followScroll; 
    var initPosRelativeTo = (initPosRelativeTo == null) ? 'center' : initPosRelativeTo;  // center, page, event
    var initPosX = (initPosX == null) ? 0 : initPosX; 
    var initPosY = (initPosY == null) ? 0 : initPosY; 
    
    //Check for greyDiv's existance, if not here, intialize
    if (greyDiv == null) {
    	initModalDiv(forceModal, followScroll); // pass these along
    	greyDiv = document.getElementById('modalInactiveBox');
    }
    var modalContentDiv = document.getElementById('embeddedContent');
    var titlebox = document.getElementById('modalTableTopRowLeft');
    var modalHead = document.getElementById('modalContentHead');
    var modalBody = document.getElementById('modalContentBody');
    var modalFoot = document.getElementById('modalContentFoot');
    var printCell = document.getElementById('modalTableTopRowPrint');
		var theBox = document.getElementById('modalBox');
    if(isIE() && forceModal) var greyboxIframe = document.getElementById('greyboxiFrame'); 
	var headBarHeight = 45;
	var footBarHeight = 5;
	var totalHeight = modalHeight + headBarHeight + footBarHeight;
		modalContentDiv.style.height = modalHeight;
	 		if ( modalClass.length > 0 ) {
	 		document.getElementById('modalContentBody').style.backgroundColor='';
	 		modalContentDiv.className = modalClass;
	 		document.getElementById('modalContentBody').className = modalClass; 
 		}
		printCell.innerHTML = '';
    switch (type) {
    	case 'alertbox':
    		document.getElementById('modalCloseImg').style.display = 'block';
    		modalContentDiv.style.overflow = 'auto';
    		modalContentDiv.style.textAlign = 'center';
    		modalContentDiv.style.verticalAlign = 'middle';
    		modalContentDiv.style.margin = '10px';
    		modalContentDiv.innerHTML = content;
			var linebreak = document.createElement('br');
	    	modalContentDiv.appendChild(linebreak);
			var linebreak = document.createElement('br');
	    	modalContentDiv.appendChild(linebreak);
	    	var OKButton = document.createElement('button');
	    		OKButton.className = 'addedit';
	    		OKButton.style.width = '70px';
				OKButton.onmouseover = function() { this.className='addeditover' };
				OKButton.onmouseout = function() { this.className='addedit' };
				OKButton.onclick = function() { hideModalDiv() };
	    		OKButton.appendChild(document.createTextNode('OK'));
	    	modalContentDiv.appendChild(OKButton);
    	break;
    	case 'popup':
		// Use url or HTML if provided, favoring url first
    		document.getElementById('modalCloseImg').style.display = 'block';
	    		modalContentDiv.style.margin = '10px';
			if(content.length)
				{
					modalContentDiv.innerHTML = '';
					var Iframe = document.createElement('iframe'); 
		                Iframe.id = 'modalPopupIframe'; 
		                Iframe.name = 'modalPopupIframe'; 
		                Iframe.scrolling='yes';
		                Iframe.src='javascript:void(0)';
		                Iframe.style.height = modalHeight + 'px'; 
		                Iframe.style.width = '100%';
								    Iframe.style.zIndex = 999;
		                Iframe.frameBorder = 0;
		                Iframe.marginHeight = 0;
		                Iframe.marginWidth=0;
		        	modalContentDiv.appendChild(Iframe); 
			// TODO: FInd a way to print if using innerHTML instead of iframe. 
        	//Print Image
		    //	var topBarPrintImg = document.createElement('img');
			//   	topBarPrintImg.setAttribute('id', 'modalCloseImg');
			//			topBarPrintImg.setAttribute('src', '/skins/tempest_standard/images/shared/icons/icon_print.gif');
			//			topBarPrintImg.onclick = function() { modalPopupIframe.focus();modalPopupIframe.print(); };
			//			topBarPrintImg.className = 'icon';
	    	//	printCell.appendChild(topBarPrintImg);
					document.getElementById('modalPopupIframe').src = content + '&modalPopup=true&use_class=' + modalClass;  //4047NTS07
				}
			else if(contentHTML.length)
				{
					modalContentDiv.innerHTML = contentHTML;
					launchJavascript(contentHTML); // 10996NTS07
				}
			// end 10244NTS07
    	break;
    	case 'waitscreen':
	    	var waitscreenHTML = document.getElementById('layoutContentPleaseWait').innerHTML;
	    		document.getElementById('modalCloseImg').style.display = 'none';
				modalContentDiv.innerHTML =  '<br />' + waitscreenHTML + '<br />' + content;
    	break;
    }
    titlebox.innerHTML = title;
    greyDiv.style.height = pageHeight() + 'px';
    greyDiv.style.width = pageWidth() + 'px';
    greyDiv.style.top = posTop() + 'px';
    greyDiv.style.left = posLeft() + 'px';
    greyDiv.style.display = 'block';
	
	// Align accordingly
	var theBox = document.getElementById('modalBox');
	if(initPosRelativeTo == 'center')
		{
			var positionTop = posTop() + ((pageHeight() - totalHeight) / 2) - initPosY;
			var positionLeft = posLeft() + ((pageWidth() - modalWidth) / 2) + initPosX;
		}
	else if(initPosRelativeTo == 'page')
		{
			var positionTop = posTop() + initPosY;
			var positionLeft = posLeft() + initPosX;
		}
	else if(initPosRelativeTo == 'event')
		{
			var positionLeft = posLeft() + event.clientX + initPosX;
			var positionTop = posTop() + event.clientY + initPosY;
		}
	theBox.style.top = (positionTop < 0 ? 0 : positionTop) + 'px';
	theBox.style.left =  (positionLeft < 0 ? 0 : positionLeft)  + 'px';		
	dragCoordinates = [(positionLeft < 0 ? 0 : positionLeft),(positionTop < 0 ? 0 : positionTop)];
	//centerModalDiv(totalHeight, modalWidth);
    theBox.style.width = modalWidth + 'px';
    theBox.style.display = 'block';
    document.getElementById('recenterModalDivButton').onclick = function() { centerModalDiv(totalHeight, modalWidth) };
    document.getElementById('recenterModalDivButton').style.display = 'block';
    if(isIE() && forceModal) { //10244NTS07
	    greyboxIframe.style.height = pageHeight() + 'px';
	    greyboxIframe.style.width =  pageWidth() + 'px';
	    greyboxIframe.height = pageHeight() + 'px';
	    greyboxIframe.width =  pageWidth() + 'px';
	    greyboxIframe.style.display =  'inline';
    }
    // 5390NTS07 Removed return false;
}
function centerModalDiv(totalHeight, modalWidth) {
	var theBox = document.getElementById('modalBox');
  var positionTop = posTop() + ((pageHeight() - totalHeight) / 2);
  var positionLeft = posLeft() + ((pageWidth() - modalWidth) / 2);
   // dragCoordinates = [positionLeft,positionTop]; // 10244NTS07 move down 
	theBox.style.top = (positionTop < 0 ? 0 : positionTop) + 'px';
	theBox.style.left =  (positionLeft < 0 ? 0 : positionLeft) + 'px';
	dragCoordinates = [(positionLeft < 0 ? 0 : positionLeft),(positionTop < 0 ? 0 : positionTop)]; // 10244NTS07
}
function hideModalDiv() {
    if (document.getElementById('modalInactiveBox')) {
    document.getElementById('modalInactiveBox').style.display = 'none';
    document.getElementById('modalBox').style.display = 'none';
    document.getElementById('recenterModalDivButton').style.display = 'none';
    //inf(v);
    //document.onkeypress = '';
    }
}

function initModalDiv(forceModal, followScroll) { // 10244NTS07
    var obody = document.getElementsByTagName('body')[0];
    var frag = document.createDocumentFragment();
    //Create the Grey Box
    var greyDiv = document.createElement('div');
    greyDiv.setAttribute('id', 'modalInactiveBox');
	if(forceModal)
		{
		    greyDiv.className = 'modalInactiveBox';
		}
    greyDiv.style.display = 'none';
    greyDiv.style.position = 'absolute';
    greyDiv.style.top = 0;
    greyDiv.style.left = 0;
    greyDiv.style.zIndex = (forceModal ? 985 : -985); // 10244NTS07
    greyDiv.style.width = '100%';
    if(isIE() && forceModal){ // 10244NTS07
			var protectorGBIframe = document.createElement('iFrame');
	  		protectorGBIframe.setAttribute('id','greyboxiFrame');
	  		protectorGBIframe.setAttribute('src','javascript:void(0);');
	  		protectorGBIframe.setAttribute('scrolling','no');
			protectorGBIframe.style.filter='progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
	  		//protectorGBIframe.style.border = 'solid medium blue';
	  		protectorGBIframe.style.zIndex = 988;
	  		protectorGBIframe.style.position = 'absolute';
	  		protectorGBIframe.style.width = '100%';
	  		protectorGBIframe.style.left = 0;
	  		protectorGBIframe.style.top = 0;
	  		greyDiv.appendChild(protectorGBIframe);
	  	}
    frag.appendChild(greyDiv);
    var recenterModalDiv = document.createElement('div');
    		recenterModalDiv.setAttribute('id','recenterModalDiv');
		    recenterModalDiv.style.position = 'absolute';
		    recenterModalDiv.style.top = 0;
		    recenterModalDiv.style.left = 0;
		    recenterModalDiv.style.textAlign = 'right';
		    recenterModalDiv.style.width = '100%';
		    recenterModalDiv.style.zIndex = (forceModal ? 986 : -986); //10244NTS07
    			var recenterModalDivButton = document.createElement('button');
    			recenterModalDivButton.setAttribute('id','recenterModalDivButton');
	    		recenterModalDivButton.className = 'addedit';
					recenterModalDivButton.onmouseover = function() { this.className='addeditover' };
					recenterModalDivButton.onmouseout = function() { this.className='addedit' };
	    		recenterModalDivButton.appendChild(document.createTextNode('Center Window'));
    		recenterModalDiv.appendChild(recenterModalDivButton);
    frag.appendChild(recenterModalDiv);
    //Create the space for the Modal box to exist above the grey box
    var modalDiv = document.createElement('div');
	    modalDiv.setAttribute('id', 'modalBox');
    	//modalDiv.style.border = 'medium solid #ff0000';
	    modalDiv.style.position = 'absolute';
	    modalDiv.style.display = 'none';
	    //Border Order: Width, Style, color, inherit
	    modalDiv.style.borderBottom = '1px solid #000000';
	    modalDiv.style.borderRight = '1px solid #000000';
	    modalDiv.style.borderLeft = '1px solid #FFFFFF';
	    modalDiv.style.borderTop = '1px solid #FFFFFF';
    	modalDiv.style.zIndex = 992;
    	//modalDiv.ondblclick = function() { prompt('modaldiv HTML', document.getElementById('modalBox').innerHTML) };
		var modalTable = document.createElement('table');
			modalTable.setAttribute('id', 'modalTable');
			modalTable.cellPadding = 0;
			modalTable.cellSpacing = 0;
			modalTable.border = 0;
			modalTable.style.zIndex = 998;
  		modalTable.style.position = 'relative';
  		modalTable.style.width = '100%';
  		modalTable.style.left = 0;
  		modalTable.style.top = 0;
			modalTable.style.height='100%';
				//Header
				var modalTableHead = document.createElement('thead');
					modalTableHead.setAttribute('id', 'modalContentHead');	
				//Title Bar Row
				var modalTableRow = document.createElement('tr');
					modalTableRow.setAttribute('id', 'modalTableTopRow');
					modalTableRow.className = 'titlebox';
					var modalTableCell = document.createElement('td');
						modalTableCell.setAttribute('id', 'modalTableTopCell');
						//Split the top row into two sides
						var modalTableTop = document.createElement('table');
							modalTableTop.setAttribute('id', 'toprowtable');
							modalTableTop.classname = 'titlebox';
							modalTableTop.style.cursor = 'move';
							modalTableTop.width='100%';
							modalTableTop.cellPadding = 2;
							modalTableTop.cellSpacing = 0;
							modalTableTop.border = 0;
						    modalTableTop.style.borderBottom = '1px solid #c0c0c0';
						    modalTableTop.style.borderRight = '1px solid #c0c0c0';
						    modalTableTop.style.borderLeft = '1px solid #FFFFFF';
						    modalTableTop.style.borderTop = '1px solid #FFFFFF';
							var modalTableTopBody = document.createElement('tbody');
								var modalTableTopRow = document.createElement('tr');
									modalTableTopRow.setAttribute('id', 'toprowtr');
								var modalTableTopLeftCell = document.createElement('td');
									modalTableTopLeftCell.setAttribute('id', 'modalTableTopRowLeft');
									modalTableTopLeftCell.setAttribute('width', '98%');
									modalTableTopLeftCell.className = 'leftside';
									modalTableTopRow.appendChild(modalTableTopLeftCell);
								var modalTableTopRightCell = document.createElement('td');
									modalTableTopRightCell.setAttribute('id', 'modalTableTopRowPrint');
									modalTableTopRightCell.setAttribute('width', '1%');
									modalTableTopRightCell.setAttribute('nowrap','true');
									modalTableTopRow.appendChild(modalTableTopRightCell);
								var modalTableTopRightCell2 = document.createElement('td');
									modalTableTopRightCell2.setAttribute('id', 'modalTableTopRowClose');
									modalTableTopRightCell2.setAttribute('width', '1%');
									modalTableTopRightCell2.setAttribute('nowrap','true');
										//Close Image
								    var topBarCloseImg = document.createElement('img');
								    	topBarCloseImg.setAttribute('id', 'modalCloseImg');
										topBarCloseImg.setAttribute('src', '/pwcommon/scripts/modal/error.png');
										topBarCloseImg.onclick = function() { hideModalDiv() };
										topBarCloseImg.className = 'icon';
							    		modalTableTopRightCell2.appendChild(topBarCloseImg);
									modalTableTopRow.appendChild(modalTableTopRightCell2);
								modalTableTopBody.appendChild(modalTableTopRow);
							modalTableTop.appendChild(modalTableTopBody);
						modalTableCell.appendChild(modalTableTop);
					modalTableRow.appendChild(modalTableCell);
				modalTableHead.appendChild(modalTableRow);
			modalTable.appendChild(modalTableHead);
				//Body
				var modalTableBody = document.createElement('tbody');
					modalTableBody.setAttribute('id', 'modalContentBody');
					modalTableBody.style.backgroundColor = '#FFFFFF';
				//Modal Content Row
				var modalTableRow = document.createElement('tr');
					modalTableRow.setAttribute('id', 'modalTableContentRow');
					var modalTableCell = document.createElement('td');
						modalTableCell.setAttribute('id', 'modalTableContentCell');
						modalTableCell.setAttribute('width', '99%');
						//modalTableCell.setAttribute('colSpan', '2');
						var modalContentDiv = document.createElement('div');
					    	modalContentDiv.setAttribute('id', 'embeddedContent');
				    	modalTableCell.appendChild(modalContentDiv);
					modalTableRow.appendChild(modalTableCell);
				modalTableBody.appendChild(modalTableRow);
			modalTable.appendChild(modalTableBody);
				//Footer
				var modalTableFooter = document.createElement('tfoot');
					modalTableFooter.setAttribute('id', 'modalContentFoot');
					modalTableFooter.style.height = '0px';
				//Bottom Row
				var modalTableRow = document.createElement('tr');
					modalTableRow.setAttribute('id', 'modalTableBottomRow');
					modalTableRow.className = 'bottomrow';
					var modalTableCell = document.createElement('td');
						modalTableCell.setAttribute('id', 'modalTableBottomRowCell');
					    modalTableCell.style.borderTop = '1px solid #c0c0c0';
						//modalTableCell.setAttribute('colSpan', '2');
						modalTableCell.appendChild(document.createTextNode(' '));
					modalTableRow.appendChild(modalTableCell);
				modalTableFooter.appendChild(modalTableRow);
				modalTable.appendChild(modalTableFooter);
    	modalDiv.appendChild(modalTable);
    frag.insertBefore(modalDiv, greyDiv.nextSibling);
    obody.insertBefore(frag, obody.firstChild);
	// Made this optional
	if(followScroll)
		{
		    window.onscroll = function() { scrollFix() };
		}
   window.onresize = function() { sizeFix() };
    dragArea = document.getElementById('modalBox');
    Drag.init(document.getElementById('toprowtable'), dragArea);
    dragArea.onDragEnd = function(x,y) {
    	dragCoordinates = [x,y];
    }
}

function isIE(){
  // only for Win IE 6+
  var strBrwsr= navigator.userAgent.toLowerCase();
  if(strBrwsr.indexOf("msie") > -1 && strBrwsr.indexOf("mac") < 0){
    if(parseInt(strBrwsr.charAt(strBrwsr.indexOf("msie")+5)) < 6){
      return false;
    }
	  // But not in Windows 98, Me, NT 4.0, 2000
    /* if(strBrwsr.indexOf("win98") > -1 ||
       strBrwsr.indexOf("win 9x 4.90") > -1 ||
       strBrwsr.indexOf("winnt4.0") > -1 ||
       strBrwsr.indexOf("windows nt 5.0") > -1)
    {
      return false;
    } */
    return true;
  }else{
    return false;
  }
}

