
App.GetHostData = function() {
	var i;
	for(i=0; i<App.HostNameList.length; i++)
	{
		if(App.Hostname.indexOf(App.HostNameList[i])!=-1)
		{
			App.Host.Name = App.HostNameList[i];
		}
	}
}

App.DisplayHelp = function(response) {
	var outString;
	outString = response.responseText
		.replace("$Title$", App.Help[App.Loc.HelpID].Title)
		.replace("$Topic$", App.Help[App.Loc.HelpID].Topic)
		.replace("$Explain$", App.Help[App.Loc.HelpID].Explain)
		.replace("$ExpoundList$", App.Help[App.Loc.HelpID].ExpoundList)
		.replace("$Fallout$", App.Help[App.Loc.HelpID].Fallout)
		.replace("$Assist$", App.Help[App.Loc.HelpID].Assist)
		.replace("$AssistBody$", App.Help[App.Loc.HelpID].AssistBody);

	document.write(outString);
}

App.DisplayFrame = function(response) {
	var outString;
	outString = response.responseText
		.replace("$Title$", App.Owner.Title)
		.replace("$Href$", App.Owner.Href);

	document.write(outString);
}

// window.onerror
App.Errors = function(helpID) {
	App.Loc.HelpID = "_" + helpID;
	Request.sendGET('/t/' + helpID.substr(0,2) + '.html', App.DisplayHelp, this);
	return true;
}

// window.onload
App.Start = function() {
	App.Owner.Href = eval('_CLIENT_' + App.Host.Leader);
	App.Owner.Title = eval('_TITLE_' + App.Host.Leader);
	App.Owner.FrameBox = eval('_FRAME_' + App.Host.Leader);
	
	if(App.Owner.FrameBox==1) {
		top.location.href = App.Owner.Href + App.Host.After;
	}else{
		Request.sendGET('/t/frame.html', App.DisplayFrame, this);
	}
}

App.Main = function() {

	App.GetHostData();

	if(App.Hostname.indexOf("." + App.Host.Name)!=-1){

		App.Host.Leader = App.Hostname.split(".")[0];
		if(App.Host.Leader.substr(0,4)=='www.')
		{
			App.Host.Leader = App.Host.Leader.substr(4);
		}
		
		App.Host.After = App.Pathname;
		
		if(App.Host.After=='/')
		{
			App.Host.After = '';
		}
		document.title = App.Loc.Title;
	}else{
		App.Errors('405');
	}
}

// Request Object
var Request = new Object();

Request.send = function(url, method, callback, data) {	
	var req;	
	if (window.XMLHttpRequest)
	{
		req = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}

	req.onreadystatechange = function() {
		if (req.readyState == 4)
		{
			// only if req shows "loaded"
			if (req.status < 400)
			{
				// only if "OK"
				(method=="POST") ? callback(req) : callback(req,data);
			}else{
				alert("There was a problem saving your changes :\n" + req.status+ "/" + req.statusText);
			}
		}
	}

	if (method=="POST")
	{
		req.open("POST", url, true);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		//req.setRequestHeader('Connection','close'); //STUPID 400 FF ERROR FIX
		req.send(data);
	}else{
		req.open("GET", url, true);
		req.send(null);
	}
	
	return req;
}

Request.sendPOST = function(url, data, callback) {
	Request.send(url, "POST", callback, data);
}
Request.sendGET = function(url, callback, args) {
	return Request.send(url, "GET", callback, args);
}

App.Main();

window.onerror = function() {
	App.Errors('404');
}

window.onload = function() {
	var spanLoading = document.getElementById("Loading");
	spanLoading.innerHTML = App.Loc.Loading;

	if(App.Host.Leader!='www' && App.Host.Leader!='')
	{
		App.Start();
	}
}

