/* ******
pageControl.js 
Handling of frames show and hide based on the user action

********* */

var count=0;
var maxWait = 60;	//wait no longer than this amount(seconds)

var menuFrame = "MenuFrame";
var MenuHeight = "25px";
var hiddenFrame = "SubFrame";  
var defaultDomain = "/paceportal";
var defaultHiddenPage = "/gateway/Blank.htm";
var mainFrame = "MainFrame";

var loading = false;

var startURL = 0;
var strDomain ;

// Provide variables to hold references temporarily		
var __ParametersCollectionArray;
var __ParametersCollectionForm;

// Grab the originating domain		
strDomain = (top.location.protocol + '//' + top.location.host) ;
defaultHiddenPage = strDomain + defaultDomain + defaultHiddenPage

// Determine the colors of movement
var defaultColor = "#688D87"
var movingColor = "#134441"

// Set up the starting left and top positions
intLeft = 30
intTop = 40

// Determine the max travel
intMaxTravel = 40
intHeightFactor = 1/2
intWidthFactor = -2

// Determine the appropriate letter spacing
intLetterSpacing = 16

// Determine the incremental timing & speed
var intBounceDelay = 300
var intSpeed = 30
var intSpinRate = 12
var intCircleRepeats = 1

// DHTML variables
var isIE,isNS,isW3C
var visible, hidden
var range = "";
var styleObj = "";
var numberDone=0;

// Declare the window to open
var winMessage;

// Sniff the browser
if (document.layers) 
	{
	isNS = true;
	isIE = false;
	isW3C = false;
	range = "layers['";
	styleObj = "']"
  	visible = 'show';
  	hidden = 'hide';	
	}
else if (document.all) 
	{
	isNS = false;
	isIE = true;
	isW3C = false;
	range = "all('";
	styleObj = "').style";
	visible = 'visible';
	hidden = 'hidden';	
	}
else if (document.getElementsByTagName("*"))
	{
	isNS = false;
	isIE = false;
	isW3C = true;
	range = "getElementById('";
	styleObj = "').style";
	visible = 'visible';
	hidden = 'hidden';
	} 

// This will return to Portal System
function ReturnToPortal(){

	// Determine what index the "main" frame is in	
	for(i=0;i<top.window.frames.length;i++) {
		if (top.window.frames[i] == parent.frames[mainFrame]) {
			break;
		}
	}
		
	// Determine the "main" frame size
	var strRows = new String(top.document.body.rows);
	var arrRows = strRows.split(",");

	// Verify that the frame setting is not as desired (0,0,100)	
	if (parseInt(arrRows[i]) == 100){
		return;
	}

	// Show the main frame containing the portal where this whole mess started
	resize('0','0','100%')

	// Remove the content from the hidden frame, to get a clean start
	parent.frames[hiddenFrame].location.href = defaultHiddenPage;

	// Just in case
	hideMessage();
	
	// 
	parent.frames[mainFrame].focus();
}

function LaunchURL(strURL) {
	parent.frames[hiddenFrame].location.href = strURL ;
	return;
}

function PostURL(strURL,withMenu, MenuTitle) {
	
	
	//alert(strURL);


	// Verify that the user hasn't already tried this
	if (loading == true){
		alert('Please be patient.\nYour last request is processing.');
		return;
	}
	else{
		// Set loading flag to prevent users from "double-clicking"
		loading = true;

		// Show a "PLEASE WAIT" message;
		showMessage();
	}

	// Change the hidden frame's location to URL	
	parent.frames[hiddenFrame].location.href = strURL ;

	// When URL is fully loaded, show the hidden frame
	if (withMenu == true) {
		if(MenuTitle==undefined){
			// Clear out anything that might be there
			parent.frames[menuFrame].setMenuText('');
		}
		else{
			parent.frames[menuFrame].setMenuText(MenuTitle);
		}
		
		// Resize with the menu
		pageResize(MenuHeight,'*','0')
	}
	else {
		pageResize(0,'*','0')
	}
				
}

		
function pageResize(frame1,frame2,frame3) {

	var wait = 500;		// Milliseconds between validations
	
	try{ 	//validate frame loaded with other than blank page
	
		//Determine whether the maxwait in seconds has been reached 
		//if (count < 0) {
		if (count > (maxWait * 1000/wait)) {
			// Reset counter, assume process timeout
			throw("Resource not Found");
		}
		else {		//if not maxwait reached then continue loading the frame

			// Post a status to the user
			window.status = "Loading " + Math.round(maxWait - count * wait/1000);
			
			// Will either determine different title or "access denied"
			if (parent.frames[hiddenFrame].document.title == "Blank") {
				count += 1;
				//Attempt to refresh the page 
				setTimeout("pageResize('" + frame1 + "','" + frame2 + "','" + frame3 + "')", wait);
			}	
			else {
				//Force error to trigger frame reconfiguration
				throw ("[object Error]");
			}			
		}
	}
	catch(er){
		// Reset counter and other flags for next function call
		count = 0;
		window.status = "";
		loading = false;
	
		// Kill the "PLEASE WAIT" message
		hideMessage();
	
		// Set focus to the hidden frame
		parent.frames[hiddenFrame].focus();

		// If error not expected, report to user unexpected error				
		if (er != "[object Error]"){
			alert ('An error occurred while attempting your request.\n' + er);
			return;
		}
		else {  // Resize frameset to expose linked content
			setTimeout("resize('" + frame1 + "','" + frame2 +"','" + frame3 + "')", 100);
								
		}
	}

}


function resize(frame1, frame2,frame3) {	//change the frameset dimensions
	var rowsize = frame1 + " ," + frame2 + " ," + frame3 + "  ";
	if (top.document.body.rows != rowsize) {
		top.document.body.rows= rowsize;
	}
}		
		

function hideMessage(){
	
	try{
		winMessage.close();
		winMessage = '';
	}		
	catch(er){
		return;
	}
}

// Function to post the message in a new window	
function showMessage(strMessage){

	// Set up a message for display
	if (strMessage==undefined){strMessage = new String("Processing... Please wait");}

	// Break the message into an array
	var arrMessages = new Array(strMessage.length);
	
	// Open a small window
	var winHeight = 100;
	var winWidth = 475;
	var winLeft = (screen.availWidth - winWidth)/2;
	var winTop = (screen.availHeight - winHeight)/2;

	winMessage=window.open('blank.htm','UserMessage','toolbar=no,scrollbars=no,width=' + winWidth + ',height=' + winHeight + ',left=' + winLeft + ',top=' + winTop + ',screenX=' + winLeft + ',screenY=' + winTop)
	try {
		winMessage.document.open;
	}
	catch(er)
	{
		return;
	}

	var strHTML;
	var strFunction;

	strFunction = '';
	strFunction = strFunction + 'function bounceMessage(degrees)	{';
	strFunction = strFunction + '	var fullCircle=360;';
	strFunction = strFunction + '	var daProcessing;';
	strFunction = strFunction + '	var intCirclePointer;';
	strFunction = strFunction + '	intCirclePointer = degrees;';
	strFunction = strFunction + '	for (i=0; i < ' + strMessage.length + '; i++){';
	strFunction = strFunction + '		daProcessing = eval("document.' + range + 'Msg" + i + "' + styleObj + '");';
	strFunction = strFunction + '		if (intCirclePointer<0){';
	strFunction = strFunction + '			intCirclePointer=0;';
	strFunction = strFunction + '		}';		
	strFunction = strFunction + '		if (intCirclePointer < (' + intCircleRepeats + ' * fullCircle)){';
	strFunction = strFunction + '			radians = (intCirclePointer/fullCircle)*(2*Math.PI);';
	strFunction = strFunction + '			daProcessing.top = ' + parseInt(intTop) + ' +  (Math.sin(radians) * ' + intMaxTravel + ' * ' + intHeightFactor + ');';
	strFunction = strFunction + '			daProcessing.left = parseInt(' + intLeft + ' + (' + intLetterSpacing + ' * i) - (' + intWidthFactor + ') * ' + intMaxTravel + ') +  (Math.cos(radians) * ' + intMaxTravel + ' * (' + intWidthFactor + '));	';
	strFunction = strFunction + '			if (parseInt(daProcessing.top) != ' + parseInt(intTop) + ') {daProcessing.color = "' + movingColor + '"}';
	strFunction = strFunction + '		}';
	strFunction = strFunction + '		else {';
	strFunction = strFunction + '			daProcessing.top = ' + parseInt(intTop) + ';';
	strFunction = strFunction + '			daProcessing.left = parseInt(' + intLeft + ' + (' + intLetterSpacing + ' * i));';
	strFunction = strFunction + '			daProcessing.color = "' + defaultColor + '";'
	strFunction = strFunction + '		}';
	strFunction = strFunction + '		intCirclePointer = intCirclePointer - ' + intSpinRate + ';';
	strFunction = strFunction + '	}';
	strFunction = strFunction + '	if (degrees > ((' + intCircleRepeats + ' * fullCircle) + (' + strMessage.length + ' * ' + intSpinRate + '))){';
	strFunction = strFunction + '		timerID = setTimeout("bounceMessage(0)", ' + intBounceDelay + ');';
	strFunction = strFunction + '	}';
	strFunction = strFunction + '	else {';
	strFunction = strFunction + '		timerID = setTimeout("bounceMessage(" + (degrees + ' + intSpinRate + ') + ")", ' + intSpeed + ');';
	strFunction = strFunction + '	}';
	strFunction = strFunction + '}';

	// Generate the html to post to the new window
	strHTML = "";
	strHTML = strHTML + "<html>";
	strHTML = strHTML + "<head>";
	strHTML = strHTML + "<title>" + strMessage + "</title>";
	strHTML = strHTML + "<script language='javascript'>";
	strHTML = strHTML + strFunction
	strHTML = strHTML + "<" + String.fromCharCode(47) + "script>";
	strHTML = strHTML + "</head>";
	strHTML = strHTML + "<body bgcolor='#fffaf0'>";
	
	// Loop through the message, and post it as content
	for (i=0;i<strMessage.length;i++){
		strHTML = strHTML + "<div id='Msg" + i + "' style='position:absolute; left:" + (intLeft + (intLetterSpacing * i)) + "px; top:" + intTop + "px;width:" + intLetterSpacing + "px;FONT-WEIGHT: 700;FONT-SIZE: 20px;COLOR: " + defaultColor + ";FONT-FAMILY: helvetica, sans-serif;text-align:center;'>" + strMessage.charAt(i) + "</div>";
	}
					
	strHTML = strHTML + "</body>";
	strHTML = strHTML + "</html>";
	
	// Post the html into the window
	winMessage.document.write(strHTML);
	winMessage.document.close;
	
	self.setTimeout("if(winMessage!=''){winMessage.bounceMessage(0);}", intBounceDelay);	
}



function Login(strUsername, strPassword) {
	
		
	if (strUsername.length == 0){
		alert ('Login Name can not be blank') ;
		return false;
	}
	
	if (strPassword.length == 0){
		alert ('Password can not be blank') ;
		return false;
	}
		
	// push a test cookie to user machine
	document.cookie = "confirmation=true";
	
	// Confirm that cookies are working
	if (document.cookie.indexOf("confirmation") == -1) {
		alert("www.Paceinformation.com requires cookies enabled.\nYour browser has cookies disabled.");  
		return false;
	}	
	
	// Post the login information to a new form, and submit
	newParametersCollection();
	putParameter (__ParametersCollectionArray, 'username', strUsername);
	putParameter (__ParametersCollectionArray, 'password', strPassword);
	submitParametersCollection(strDomain + defaultDomain + '/Default.aspx?tabid=135');
	return true;
	
}



//////////////////////////////////////////////////////////////////////	
	function submitParametersCollection(SubmitToURL){

		var strHTML='';
		var WindowHandle;

		// Verify that there are parameters to submit
		if (__ParametersCollectionArray.length==0) {
			alert("There are no parameters to submit.");
			return(0);
		}
		
		// Concatenate the domain to the URL if it doesn't exist
		if (SubmitToURL.indexOf('http') == -1) {SubmitToURL = strDomain + defaultDomain + SubmitToURL}

		// Verify that the user hasn't already tried this
		if (loading == true){
			alert('Please be patient.\nYour last request is processing.');
			return false;
		}
		else{
			// Set loading flag to prevent users from "double-clicking"
			loading = true;
		}

		// Post a message to let the user know something hapenin'
		showMessage('Contacting server...');

		// Build an HTML form to submit
		strHTML=strHTML+"<html>";
		strHTML=strHTML+"<head><title>Blank</title></head>";
		strHTML=strHTML+"<body>";
		strHTML=strHTML+"<form name='ParametersCollectionForm' method='post' action='" + SubmitToURL + "'>";
		
		// Loop through the parameters and add a hidden control for each
		for(i=0;i<__ParametersCollectionArray.length;i++){
			strHTML=strHTML+"<input type='hidden' name='" + __ParametersCollectionArray[i][0] + "' value='" + __ParametersCollectionArray[i][1] + "'>";
		}
		
		strHTML=strHTML+"</form>";
		strHTML=strHTML+"</body>";
		strHTML=strHTML+"</html>";
		
		
		// Post html content into the hidden frame and let the sparks fly
		parent.frames[hiddenFrame].document.write(strHTML);

		// Resize the frames when the page changes
		pageResize('30','70%','0');

		// clear the menu text
		parent.frames[menuFrame].setMenuText('');
		
		// Submit the form
		parent.frames[hiddenFrame].document.ParametersCollectionForm.submit();

		// Reset the loading flag
		loading = false;

		// Clear the parameters collection and form reference to avoid re-submit
		__ParametersCollectionArray = '';
		__ParametersCollectionForm = '';
		
		return true;
	}
	
			//////////////////////////////////////////////////////////////////////	
			function newParametersCollection(){
				// Initialize a new collection to gather parameters
				
				
				__ParametersCollectionArray = new Array();
				
				
			}
			//////////////////////////////////////////////////////////////////////	
			function bindFormToParametersCollection(FormObject){
				// Confirm the submitted is of object type, then store reference
				if(typeof(FormObject)=='object'){
					__ParametersCollectionForm = FormObject;
				}
				else{
					alert("Sorry, not an object");
				}
			}
			//////////////////////////////////////////////////////////////////////	
			function bindParameterToControl(ParameterName, ControlName){
				// Confirm the form referense exists, and gather control intformation
				
				if(typeof(__ParametersCollectionForm)!='object'){
					alert("A form needs to be bound before parameters.");
				}
				else{				
					// Add the parameter to the parameter stacks
						putParameter(__ParametersCollectionArray, ParameterName, getControlValue (__ParametersCollectionForm,ControlName))
					}
			}
			
		//////////////////////////////////////////////////////////////////////
			function getControlValue (ControlForm, ControlName){
				// Loop through a forms control collection to find the specified control by name
				if(typeof(__ParametersCollectionForm)!='object'){
					alert("A form needs to be bound before parameters.");
				}
				else{				

				
					for(i=0;i<ControlForm.elements.length;i++){
						if(ControlForm.elements[i].name.indexOf(ControlName) != -1){
						 switch(ControlForm.elements[i].type){
							
							case "label":
								// ignore labels
								break;
							case "radio":

								if (ControlForm.elements[i].checked) return(ControlForm.elements[i].value);
								break;

							default :
								return(ControlForm.elements[i].value);			
								break;
							}
						}
					}

				}
								
				// Always provide a default value, in case the control is not found
				return('');
			}
			//////////////////////////////////////////////////////////////////////
			function putParameter (ParameterArray, ParameterKey, ParameterValue){
				
				// Create  a new 2-dimensional array to store
				//  a key-value pair
				var arrInner = new Array(2)
				
				// Insert the key-value pair
				arrInner[0] = ParameterKey;
				arrInner[1] = ParameterValue;
				
				// Insert the new array into the parameter array
				ParameterArray[ParameterArray.length] = arrInner
				
				
			}

			
		function openWindow(urlPath) {
			

				var winparams = "toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=500,height=450,title=Form";
				window.open(urlPath,'Win', winparams);
			}	

		function launchwin(tutorial){
				
				newwin = window.open("","robodemowin","fullscreen=0,scrollbars=1")
				newwin.resizeBy(15,0)
				
				// Add the path to the flash directory
				tutorial = "./training/tutorials/" + tutorial 

				
				var htmlString=
				"<HTML> "+
				"<BODY onLoad='window.document.robodemo.focus();'><center>"+
				"<OBJECT CLASSID='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' id=robodemo WIDTH='100%' HEIGHT='100%' CODEBASE='http://active.macromedia.com/flash5/cabs/swflash.cab#version=5,0,0,0'> "+
				"<PARAM NAME=movie VALUE='" + tutorial + "'> "+
				"<PARAM NAME=play VALUE=true> "+
				"<PARAM NAME=loop VALUE=0>"+
				"<PARAM NAME=quality VALUE=high>"+
				"<EMBED name=robodemo SRC='" + tutorial + "' WIDTH='105%' HEIGHT='105%' "+
				"loop=0 quality=high "+
				"TYPE='application/x-shockwave-flash' "+
				"swLiveConnect=true "+
				"PLUGINSPAGE='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash'>"+
				"</EMBED>"+
				"</OBJECT>"+
				"</center>"+
				"</BODY></HTML>"

				newwin.document.open();
				newwin.document.write(htmlString);
				newwin.document.close();
				
				}

