var focusDiv = '';
var XMLdata = '';
var moduleFiles; // array set via getFiles() ... used with loadFiles()
var currentPath = '';
var currentFile = '';
var currentModule = '';
var moduleOptions = '';
var frameTitle = 'mainTitle';




//***** SETTERS *****
function setCurrentModule(inString, inOptions) {
	currentModule = inString;
	moduleOptions = inOptions;
}

function setCurrentFile(inString) {
	currentFile = inString;
}

function setCurrentPath(inString) {
	currentPath = inString;
}

function setModuleFiles(inArray) {
	moduleJS = inArray;
}

function setXMLdata(inObject) {
	XMLdata = inObject;
}

function setFocus(inFocus){
	focusDiv = inFocus;
}




//***** GETTERS *****
function getModuleOptions() {
	options = '';
	if (moduleOptions != '') {
		options = '?' + moduleOptions;
	}
	return options;
}

function getCurrentModule() {
	return currentModule;
}

function getCurrentFile() {
	return currentFile;
}

function getCurrentPath() {
	return currentPath;
}

function getModuleFiles() {
	return moduleJS;
}

function getXMLdata() {
	return XMLdata;
}

function getFocus() {
	return focusDiv;
}





//***** MISC *****
function setSectionTitle (inTitle, inDiv) {
	document.getElementById(inDiv).innerHTML = inTitle;
}

function onLoadFunction(){
	startList();
	setFocus('contentText');
	getPage('init');
}

function mainSelect(inObj){
	setFocus('contentText');
	getPage(inObj);
}

function googleSearch(inText){
	if (inText != ''){
		window.open("http://www.google.com/search?hl=en&q="+escape(inText));
	}
}

function loadFiles() {
	files = getModuleFiles();
	if (files.length > 0) {
		last = files.length - 1;
		filelist = files[last]['files'];
		if (filelist.length > 0) {
			setCurrentFile(filelist.pop());
			setModuleFiles(files);
			files[last]['action']();
		} else {
			files.pop();
			setModuleFiles(files);
			loadFiles();
		}
	} else {
		getModule();
	}
}

function openFile(inFile, inFrame) {
	loc = getCurrentPath() + inFile;
	if (document.getElementById(inFrame).contentDocument) {
		document.getElementById(inFrame).src = loc;
	} else {
		document.frames[inFrame].location = loc;
	}
//	windowOne = window.open(loc, 'openFileWindow');
//	windowOne.close();
}

function login() {
	doLogin();
}

function logout() {
	doLogout();
}

function getCSSFile() {
	file = getCurrentFile();
	path = getCurrentPath();

	var head = document.getElementsByTagName('head').item(0);
	
	csslink = document.createElement('link');
	csslink.href = path + 'css/' + file;
	csslink.rel = 'stylesheet';
	csslink.type = 'text/css';

	head.appendChild(csslink);

	loadFiles();
}

function updateProg(inCurrent/*, inTot*/) {
//	alert(inTot);
//	if (inTot != '\0') {
//		progress = (inCurrent/inTot) * 100;
//	} else {
		progress = inCurrent;
//	}
	bar = '';//"<table border=0 cellpadding=0 cellspacing=0 id=progBar><tr><td width=100><table border=0 cellpadding=0 cellspacing=0><tr><td width=" + progress + "></td></tr></table></td></tr></table>";
	document.getElementById(focusDiv).innerHTML = "<div id=loadBar>" + "Loading!" + "<br>" + bar + inCurrent/* + "/" + inTot*/ + "</div>";
}









//***** AJAX GETS *****
function doLogin() {
	var url="manage/login.php";
	xmlHttp=GetXmlHttpObject(stateChanged);
	xmlHttp.open("POST", url , true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	xmlHttp.send("username="+document.getElementById('emailInput').value+"&password="+document.getElementById('passwordInput').value);
}

function doLogout() {
	var url="manage/logout.php";
	xmlHttp=GetXmlHttpObject(stateChanged);
	xmlHttp.open("GET", url , true);
	xmlHttp.send(null);
}

function getFile(inFile, inPath, inDiv, options) {
	path = inPath;
//	alert(document.getElementById(inDiv).innerHTML);
	setFocus(inDiv);
	if (inFile.length > 0){
		fileLoc = path + inFile + options;
//		alert(fileLoc);
		url = fileLoc;
		xmlHttp=GetXmlHttpObject(stateChanged);
		xmlHttp.open("GET", url , true);
		xmlHttp.send(null);
	} else {
		document.getElementById(focusDiv).innerHTML="";
	}
}

function getModule(){
	path = getCurrentPath();
	if (path.length > 0){
		var moduleLoc = path + getCurrentModule() + ".php" + getModuleOptions();
		var url = moduleLoc;
		xmlHttp=GetXmlHttpObject(stateChangedLoad);
		xmlHttp.open("GET", url , true);
		xmlHttp.send(null);
	} else {
		document.getElementById(focusDiv).innerHTML="";
	}
}

function getFiles(inModule, offsetPath, inDiv, inOptions){
	if (inModule.length > 0){
		path = offsetPath + "modules/" + inModule + "/";
		setCurrentModule(inModule, inOptions);
		setCurrentPath(path);
		setFocus(inDiv);
		fileLoc = path + "module_files.xml";
		var url = fileLoc;
		xmlHttp=GetXmlHttpObject(xmlstateChanged);
		xmlHttp.open("GET", url , true);
		xmlHttp.send(null);
	}
}

function getJSFile() {
	file = getCurrentFile();
	path = getCurrentPath();
	if (file.length > 0){
		fileLoc = path + "js/" + file;
		var url = fileLoc;
		xmlHttp=GetXmlHttpObject(jsstateChanged);
		xmlHttp.open("GET", url , true);
		xmlHttp.send(null);
	}
}







//***** AJAX STATE CHANGES *****
function stateChanged() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		document.getElementById(focusDiv).innerHTML=xmlHttp.responseText;
//		alert(xmlHttp.getAllResponseHeaders());
	} else {
//		updateProg(xmlHttp.responseText.length/*, xmlHttp.getResponseHeader("Content-Length")*/)
//		alert(xmlHttp.readyState);
//		alert(xmlHttp.getResponseHeader("Date"));
	}
}

function stateChangedLoad() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		document.getElementById(focusDiv).innerHTML=xmlHttp.responseText;
//		alert(xmlHttp.getAllResponseHeaders());
	} else {
		try {
			updateProg(xmlHttp.responseText.length/*, xmlHttp.getResponseHeader("Content-Length")*/)
		} catch (e) {}
//		alert(xmlHttp.readyState);
//		alert(xmlHttp.getResponseHeader("Date"));
	}
}

function jsstateChanged() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		eval(xmlHttp.responseText);
		loadFiles();
	}
}

function xmlstateChanged() {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		setXMLdata(xmlHttp.responseXML);
		xml = getXMLdata();
		if (xml == null) {
			alert("FILE NOT FOUND!!");
		}
		xmlfiles = xml.getElementsByTagName('files').item(0);
		files = new Array();
		for (i = 0; i < xmlfiles.childNodes.length; i++) {
			t = xmlfiles.childNodes.item(i);
			if(t.attributes){
//				alert(t + " " + t.nodeName + " " + t.attributes.getNamedItem('src').value + " " + t.nodeValue);
				temp = new Array();
				temp['files'] = new Array();
				switch (t.nodeName) {
					case 'title' :
						for (j = 0; j < t.childNodes.length; j++) {
							tt = t.childNodes.item(j);
							if(tt.attributes){
								switch (tt.nodeName) {
									case 'useTitle' :
										setSectionTitle(tt.attributes.getNamedItem('title').value, frameTitle);
										break;
								}
							}
						}
						break;
					case 'css' :
						for (j = 0; j < t.childNodes.length; j++) {
							tt = t.childNodes.item(j);
							if(tt.attributes){
								switch (tt.nodeName) {
									case 'file' :
										temp['files'].push(tt.attributes.getNamedItem('src').value);
										break;
									case 'styleID' :
										temp['styleID'] = tt.attributes.getNamedItem('src').value
										break;
								}
							}
						}
						temp['action'] = getCSSFile;
						files.push(temp);
						break;
					case 'js' :
						for (j = 0; j < t.childNodes.length; j++) {
							tt = t.childNodes.item(j);
							if(tt.attributes){
								switch (tt.nodeName) {
									case 'file' :
										temp['files'].push(tt.attributes.getNamedItem('src').value);
										break;
								}
							}
						}
						temp['action'] = getJSFile;
						files.push(temp);
						break;
				}
			}
		}
		setModuleFiles(files);
		loadFiles();
	}
}








//***** AJAX HANDLER *****
function GetXmlHttpObject(handler){ 
	var objXmlHttp=null

	if (navigator.userAgent.indexOf("Opera")>=0){
		alert("This example doesn't work in Opera");
		return;
	}
	if (navigator.userAgent.indexOf("MSIE")>=0){
		var strName="Msxml2.XMLHTTP";
		if (navigator.appVersion.indexOf("MSIE 5.5")>=0){
			strName="Microsoft.XMLHTTP";
		}
		try{
			objXmlHttp=new ActiveXObject(strName);
			objXmlHttp.onreadystatechange=handler;
			return objXmlHttp;
		}
		catch(e){
			alert("Error. Scripting for ActiveX might be disabled");
			return;
		}
	}
	if (navigator.userAgent.indexOf("Mozilla")>=0){
		objXmlHttp=new XMLHttpRequest();
		objXmlHttp.onreadystatechange=handler;
//		objXmlHttp.onload=handler;
//		objXmlHttp.onerror=handler;
		return objXmlHttp;
	}
}

function isDigitsOnly(inString) {
	var re = /^[0-9]{1,}$/;
	if (!inString.match(re))
		return false;
	return true;
}

function isEmptyString(inString) {
	stCheck = true;
	for (i=0; i<inString.length; i++){
		if (inString.charAt(i) != ' '){
			stCheck = false;
			break;
		}}
	return stCheck;
}

function clean(inString) {
	re = /[^a-z,0-9,A-Z. ]/g;
	return inString.replace(re, '');
}

function isNumberKey(evt) {
	try {
		var charCode = (evt.which) ? evt.which : event.keyCode
		if (charCode > 31 && (charCode < 48 || charCode > 57)) {
			return false;
		}
		return true;
	} catch(e) {
		return true;
	}
}
