/**********************************************************************************/
/** © 1998 - 2009 Ultratec, Inc.                                                 **/
/**                                                                              **/
/** Disclaimer:                                                                  **/
/**   Ultratec, Inc. is providing this code as an example of how to access their **/
/**   Externally Hosted CapTel Application Programming Interface. All example    **/
/**   source code obtained from Ultratec, Inc is delivered "AS IS." Ultratec,    **/
/**   Inc specifically disclaims any and all warranties, expressed or implied,   **/
/**   including but not limited to any implied warranties of merchantability or  **/
/**   fitness for a particular purpose. Ultratec, Inc. does not represent or     **/
/**   warrant that any program is error free or that its use will be             **/
/**   uninterrupted. Ultratec, Inc. shall not be liable for any loss of profit,  **/
/**   loss of business or goodwill, loss of data, interruption in business, nor  **/
/**   for indirect, special, incidental, or consequential damages of any kind    **/
/**   under or arising out of the use or implementation of the example source    **/
/**   code, however caused, whether for breach or warranty, breach or            **/
/**   repudiation of contract, tort, negligence, or otherwise, even if Ultratec, **/
/**   Inc has been advised of the possibility of such a loss. Also Ultratec,     **/
/**   Inc. shall not have any obligation to provide techincal support or         **/
/**   development assistance for the example source code obtained from Ultratec, **/
/**   Inc.                                                                       **/
/**********************************************************************************/
/**********************************************************************************/
/** HELPER FUNCTIONS FOR SIMPLESCREEN                                            **/
/**********************************************************************************/
//This function determines the window height
function getWindowHeight() {
	var windowHeight = 0;
	if(typeof(window.innerHeight) == 'number'){
		windowHeight = window.innerHeight;
	}else{
		if(document.documentElement && document.documentElement.clientHeight){
			windowHeight = document.documentElement.clientHeight;
		}else{
			if(document.body && document.body.clientHeight){
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

//This function places the specified element in the middle of the screen...
function setContent(strElement){
	var iWidth, iHeight;
	if(self.innerHeight){// all except Explorer
		iWidth = self.innerWidth;
		iHeight = self.innerHeight;
	}else if(document.documentElement && document.documentElement.clientHeight){// Explorer 6 Strict Mode
		iWidth = document.documentElement.clientWidth;
		iHeight = document.documentElement.clientHeight;
	}else if (document.body){// other Explorers
		iWidth = document.body.clientWidth;
		iHeight = document.body.clientHeight;
	}
	
	//set the element screen to fill the entire window...
	document.getElementById(strElement+"screen").style.width = iWidth;
	document.getElementById(strElement+"screen").style.height = iHeight;
	if(strElement == "console"){
		var elTemp = document.getElementById(strElement);
		var iConsoleMargin = 10 * 2; //10 is the desired margin around the console window and 2 is the number of sides to take into account
		
		//this sets up the height and width for the outer console window
		elTemp.style.width = iWidth - iConsoleMargin;
		elTemp.style.height = iHeight - iConsoleMargin;
		
		//the below section sets up the height and width for the inner console window
		//  - the only reason why i can use the below code is because the base font is 10px which makes the em value and the pixel value from parseInt the same...
		var iInnerMargin = 0;
		if(elTemp.currentStyle){ //calculated via EM because of the stylesheet.
			var strPadding = elTemp.currentStyle["paddingTop"];
			iInnerMargin = parseInt(strPadding.substr(strPadding.indexOf(".")+1)) * 3;
		}else if(window.getComputedStyle){ //calculated via pixel (transformed from EM via Firefox)
			iInnerMargin = parseInt(document.defaultView.getComputedStyle(elTemp, null).getPropertyValue("padding-top")) * 11;
		}
		document.getElementById(strElement+"inner").style.width = iWidth - iConsoleMargin - iInnerMargin;
		document.getElementById(strElement+"inner").style.height = iHeight - iConsoleMargin - iInnerMargin;
		
		//the below section sets up the height and width for the actual content window of the captions...
		var iCaptionMargin = iConsoleMargin; // The margin for the caption window is the same as the margin for the console window. just setting it up seperate in case I don't want them to be the same.
		var elFooter = document.getElementById(strElement+"footer");
		var iCaptionButtonHeight = elFooter.offsetHeight;// + (iCaptionMargin/2); //This is setup this way because I need to make sure that there is the same amount of space below the footer as there is above the caption window...
		if(elFooter.currentStyle){ //calculated via EM because of the stylesheet.
			iCaptionButtonHeight = iCaptionButtonHeight + 11; //fine tuning for IE...
		}else if(window.getComputedStyle){ //calculated via pixel (transformed from EM via Firefox)
			iCaptionButtonHeight = iCaptionButtonHeight + 40; //fine tuning for Firefox...
		}
		//don't need to setup width because it is already set to 100% in the console...
		document.getElementById(strElement+"captions").style.height = iHeight - iConsoleMargin - iInnerMargin - iCaptionMargin - iCaptionButtonHeight - ((bEmergency)?34:0);
	}
	
	centerObject(strElement);
	if(parseInt(document.getElementById(strElement).style.top) > 100){
		document.getElementById(strElement).style.top = '100px';
	}else if(parseInt(document.getElementById(strElement).style.top) < 10){
		document.getElementById(strElement).style.top = '10px'; //this will force the application to always give the element a 10px top margin.
	}
}

//This centers an object in the browser window.
function centerObject(strName){
	var iWidth, iHeight;
	if(self.innerHeight){// all except Explorer
		iWidth = self.innerWidth;
		iHeight = self.innerHeight;
	}else if(document.documentElement && document.documentElement.clientHeight){// Explorer 6 Strict Mode
		iWidth = document.documentElement.clientWidth;
		iHeight = document.documentElement.clientHeight;
	}else if (document.body){// other Explorers
		iWidth = document.body.clientWidth;
		iHeight = document.body.clientHeight;
	}
	
	if(iHeight > 0){
		//This moves the messagebox to the center of the screen
		var eleContent = document.getElementById(strName);
		var iConHeight = eleContent.offsetHeight;
		var iConWidth = eleContent.offsetWidth;
		
		var iConTop = (iHeight - iConHeight) / 2;
		var iConLeft = (iWidth - iConWidth) / 2;
		if(iConTop<0){
			iConTop = 0;
		}
		if(iConLeft<0){
			iConLeft = 0;
		}
		eleContent.style.top = iConTop + 'px';
		eleContent.style.left = iConLeft + 'px';
	}
}

/**********************************************************************************/
/** CONSOLE MESSAGE FUNCTIONS                                                    **/
/**********************************************************************************/
//This function will display the console (lightbox style)
function showConsole(){
	document.body.style.overflow = "hidden"; //This hides the scroll on the screen...
	
	bKeepSessionAlive = false; //stop keeping the session alive because the console will.
	bCheckForCall = false; //stop checking for a call because user is currently in a call.
	
	document.getElementById('consolescreen').style.display = 'block'; //display the pagescreen
	document.getElementById('console').style.display = 'block'; //display the messagebox
	
	var iWidth, iHeight;
	if(document.layers || (document.getElementById && !document.all)){
		iWidth = window.outerWidth;
		iHeight = window.outerHeight;
	}else if(document.all){
		iWidth = document.body.clientWidth;
		iHeight = document.body.clientHeight;
	}
	
	//currently this is used for the emergency warning only...
	if(bEmergency){
		document.getElementById("consoleheader").style.display = "block";
		document.getElementById("console").style.background = "#550000";
		document.getElementById("consoleinner").style.background = "#ee0000";
		
		sCSSColor = "#550000";
		sCSSBorder = "#550000";
		sCSSBackground = "#ffffff";
		
		if(document.getElementById("btnCloseCaptions")){
			document.getElementById("btnCloseCaptions").style.color = sCSSColor;
			document.getElementById("btnCloseCaptions").style.borderColor = sCSSBorder;
			document.getElementById("btnCloseCaptions").style.background = sCSSBackground;
		}
		if(document.getElementById("btnEndCall")){
			document.getElementById("btnEndCall").style.color = sCSSColor;
			document.getElementById("btnEndCall").style.borderColor = sCSSBorder;
			document.getElementById("btnEndCall").style.background = sCSSBackground;
		}
		
		//this ensures that the "end call" and "close captions" buttons are hidden
		document.getElementById("btnEndCall").style.display = "none";
		document.getElementById("btnCloseCaptions").style.display = "none";
	}else{
		document.getElementById("consoleheader").style.display = "none";
		document.getElementById("console").style.background = "#eeeeee"; //from #messagebox CSS in simplescreen.css
		document.getElementById("consoleinner").style.background = "#dddddd"; //from #messagebox .inner CSS in simplescreen.css
		
		sCSSColor = "#003399";
		sCSSBorder = "#bbbbbb #bbbbbb #999999 #999999";
	//	sCSSBackground = "transparent url(/images/form_button_fade.png) repeat-x";
		
		if(document.getElementById("btnCloseCaptions")){
			document.getElementById("btnCloseCaptions").style.color = sCSSColor;
			document.getElementById("btnCloseCaptions").style.borderColor = sCSSBorder;
		//	document.getElementById("btnCloseCaptions").style.background = sCSSBackground;
		}
		if(document.getElementById("btnEndCall")){
			document.getElementById("btnEndCall").style.color = sCSSColor;
			document.getElementById("btnEndCall").style.borderColor = sCSSBorder;
		//	document.getElementById("btnEndCall").style.background = sCSSBackground;
		}
		
		//this ensures that the "end call" button is displayed and the "close captions" button is hidden
		document.getElementById("btnEndCall").style.display = "inline";
		document.getElementById("btnCloseCaptions").style.display = "none";
	}
	
	window.scrollTo(0,0);
	window.onscroll = function(){window.scrollTo(0,0);};
	window.onresize = function(){setContent("messagebox");setContent("console");setContent("loading");}; //This tells the browser to center the messagebox to the middle of the screen when the browser gets resized
	setContent("console");
	var elCaptions = document.getElementById('consolecaptions');
	elCaptions.innerHTML = ""; //clears the captioning window...
	elCaptions.scrollTop = elCaptions.scrollHeight; //used to scroll the captions window to the bottom of the captions, need to disable when the user moves the scroll and reenable afterwards?
	elCaptions.style.overflowY = "hidden"; //this disables the vertical scroll bar so the user can't review the conversation until after the call ends
	
	//get the messages from the CA
	setConsoleUserSettings();
}

// add messages to console window
function addConsoleMessage(strMessage){
	var elCaptions = document.getElementById('consolecaptions');
	elCaptions.innerHTML = elCaptions.innerHTML + strMessage; //this adds the new message to the caption window
	
	elCaptions.scrollTop = elCaptions.scrollHeight; //used to scroll the captions window to the bottom of the captions
	elCaptions.style.overflowY = "hidden"; //this disables the vertical scroll bar so the user can't review the conversation until the call ends
	
	setContent("console");
}

// Close the loading message
function closeConsole(){
	document.getElementById('console').style.display = 'none';
	document.getElementById('consolescreen').style.display = 'none';
	
	window.onresize = function(){};
	window.onscroll = function(){return 1;};
	
	if(((document.getElementById('messagebox')!=null && document.getElementById('messagebox')!=undefined)?(document.getElementById('messagebox').style.display=='none' || document.getElementById('messagebox').style.display==''):true) && ((document.getElementById('loading')!=null && document.getElementById('loading')!=undefined)?(document.getElementById('loading').style.display=='none' || document.getElementById('loading').style.display==''):true)){ 
		document.body.style.overflow = "auto";
	}
	//if you are on the place call page, clear the Dialed and set the focus back to the dialed
	if(window.location.href.substr(window.location.href.lastIndexOf('/')+1) == "PlaceCall.asp"){
		setFormValue("placecall_form","dialed",""); //clears the dialed
		document.getElementById("dialed").focus(); //this sets the focus on the page to the dialed
	}
}
