

/*
 *  Globals
  ------------------------------------------------------------------------------------------------- */
  
var sections = new Array('Internal', 'Public');
var MIN_WIDTH = 400;
var MAX_WIDTH = 500;
var MIN_HEIGHT = 70;
var PORTAL_HEIGHT = 95;
var request = null;
var authRequest = null;


/*
 *  Common Functions
  ------------------------------------------------------------------------------------------------- */

window.onload = initActions;

function initActions() {
	var object = document.getElementById('adminmap');
	object.onmouseout = imageRollout;
	object.onmouseover = imageRollover;
}

function loadProgressImage(sectionID) {
	document.getElementById(sectionID).getElementsByTagName('img').src = './include/main/progress.gif';
}

function focusUsername() {
	document.getElementById('login').style.display = 'block';
	document.getElementById('login').style.visibility = 'visible';
	document.getElementById('username').focus();
}	

function imageRollover() {
	document.images['adminimg'].src = './include/main/cornerhover.gif';
}

function imageRollout() {
	document.images['adminimg'].src = './include/main/corner.gif';
}

function redirect(hrefLoc) {
	window.location.href = './' + hrefLoc + '';
}


/*
 *  Section / Portal Functions
  ------------------------------------------------------------------------------------------------- */

function activateContents(sectionID) {
	document.getElementById(sectionID).style.backgroundColor = '#CCCCCC';
	var	section = document.getElementById(sectionID).childNodes;
	for (var i=0; i<section.length; i++) {
		if (section[i].id == 'internal') {
			section[i].className = 'subsectionactive';
		}
	}
}

function setSectionSize(height, width, sectionID, inactiveID) {
	document.getElementById(sectionID).style.height = height + "px";
	document.getElementById('spacer').style.height = (height + 1) + "px";
	document.getElementById(sectionID).style.width = width + "px";
	document.getElementById(inactiveID).style.width = (MIN_WIDTH - (width - MIN_WIDTH)) + "px";
}

function setSectionSizeAnimation(height, width, sectionID, inactiveID, timer) {
	setTimeout("setSectionSize(" + height + ", " + width + ", '" + sectionID + "', '" + inactiveID + "')", timer);
}

function openSection(sectionID) {
	var height = MIN_HEIGHT;		
	var width = MIN_WIDTH;
	var timer = 0;	
	var inactiveID = sectionID == sections[0] ? document.getElementById(sections[1]).id : document.getElementById(sections[0]).id;
	var portal_num = document.getElementById(sectionID).getElementsByTagName('a').length-1;
	var max_height = ((((portal_num%2)+portal_num)/2)*PORTAL_HEIGHT)+MIN_HEIGHT;	
	var cur_section = document.getElementById(sectionID).getElementsByTagName('div');
	cur_section[0].style.display = 'none';
	cur_section[0].style.visibility = 'hidden';
	
	while (width <= MAX_WIDTH) {			
		width += 10;
		timer += 5;
		setSectionSizeAnimation(height, width, sectionID, inactiveID, timer);
	}
	timer += 30;
	while (height <= max_height) {
		height += (max_height-MIN_HEIGHT) / 10;
		timer += 5;
		setSectionSizeAnimation(height, width, sectionID, inactiveID, timer);
	}
}

function closeSections() {
	var section_arr = new Array(document.getElementById(sections[0]), document.getElementById(sections[1]));
	if (section_arr[0].style.width != section_arr[1].style.width) {
		for (var i=0; i<section_arr.length; i++) {
			var section = section_arr[i].childNodes;
			for (var j=0; j<section.length; j++) {
				if (section[j].tagName == 'div' || section[j].tagName == 'DIV') {
					if (section[j].id == 'internal') {
						section[j].className = 'subsection';
					} else if (section[j].id == 'header') {
						section[j].style.display = 'block';
						section[j].style.visibility = 'visible';
					} else {
						section[j].style.display = 'none';
						section[j].style.visibility = 'hidden';
					}
				}
			}			
		}		
		var section;
		var inactive;
		if (section_arr[0].style.width > section_arr[1].style.width) {
			section = section_arr[0];
			inactive = section_arr[1];
		} else {
			section = section_arr[1];
			inactive = section_arr[0];
		}
		section.style.backgroundColor = '#EEEEEE';
		inactive.style.backgroundColor = '#EEEEEE';
		var max_height = MIN_HEIGHT + PORTAL_HEIGHT;
		var height = max_height;
		var width = MAX_WIDTH;
		while (width >= MIN_WIDTH) {
			setSectionSizeAnimation(height, width, section.id, inactive.id, (max_height-height));
			height -= ((max_height-MIN_HEIGHT)/10);
			width -= 10;
		}				
	}
}

function openLogin() {
	closeSections();
	openSection('Internal');
	setTimeout("focusUsername()", 150);
}

function openContent(sectionID) {
	closeSections();
	openSection(sectionID);
	setTimeout("activateContents('" + sectionID + "')", 150);	
}

function openLoading(linkLoc) {
	document.getElementById(linkLoc).innerHTML = document.getElementById('loading').innerHTML;
	setTimeout("redirect('" + linkLoc + "')", 1500);	
}


/*
 *  Login Functions
  ------------------------------------------------------------------------------------------------- */
  
function createRequest() {
	request = null;
	var tempRequest = null;
	try {
		tempRequest = new XMLHttpRequest();
	} catch (trymicrosoft) {
		try {
			tempRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft) {
			try {
				tempRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				tempRequest = null;
			}
		}
	}
	request = tempRequest;
}

function login() {
	document.getElementById('progress').style.display = 'block';
	setTimeout("loadProgressImage('Internal')", 200);
	var username = document.getElementById('username').value; 
	var password = document.getElementById('password').value;
	createRequest();
	request.open("post", "./include/main/authenticate.php", true, username, password); 
	request.onreadystatechange = updatePage;
	request.send(""); 	
}
	
function updatePage() {
	if (request.readyState == 4) {
		var text = request.responseText;	
		var section = document.getElementById('progress');
		if (text == 'Success!') { 			
			section.innerHTML = text;
			setTimeout("closeSections()", 500);
			setTimeout("openContent('Internal')", 1000);
			document.getElementById('Internal').getElementsByTagName('div')[0].innerHTML = '<a href="javascript:openContent(\'Internal\')" class="portal">INTERNAL<br /><span class="portaldescription">Restricted Company Resources</span></a>';
		} else { 
			section.innerHTML = text;
			setTimeout("loginFailure()", 500);
		}		
	}	
}

function loginFailure() {
	var section = document.getElementById('progress');
	section.style.display = 'none';
	section.innerHTML = '<img src="./include/main/progress.gif" />';
	document.getElementById('password').focus();
}


