// JavaScript Document

// Global Variable
var sSectionTitle;
var oImg = new Array(); // PreLoader

if (document.images) {
	// Image URL
	var url = "http://portfolio.deuz.net/pypsimca/images/";

// Loader
	oImg['loader'] = new Image();
	oImg['loader'].src = url+"loader.gif";
}

function requestPage(sTitle,sUrl) { // Requests page, takes 2 arguments, Title and Url to be fetched
	loadingScreen('contentContainer',true);
	sSectionTitle = sTitle;
	var oXmlHttp = zXmlHttp.createRequest();
    oXmlHttp.open("get", sUrl, true);
    oXmlHttp.onreadystatechange = function () {
		if (oXmlHttp.readyState == 4) {
			if (oXmlHttp.status == 200) {
				loadingScreen('contentContainer',false);
				writeContent(oXmlHttp.responseText);
			} else if (oXmlHttp.status == 404) {
				status404();
			} else {
				loadingScreen('contentContainer',false);
				writeContent("Ha ocurrido un Error: " + oXmlHttp.statusText); //statusText is not always accurate	
			}
		}
	};
	oXmlHttp.send(null);
}

function status404() {
	var oXmlHttp = zXmlHttp.createRequest();
    oXmlHttp.open("get", "404.html", true);
    oXmlHttp.onreadystatechange = function () {
		if (oXmlHttp.readyState == 4) {
			if (oXmlHttp.status == 200) {
				loadingScreen('contentContainer',false);
				writeContent(oXmlHttp.responseText);
			} 
		}
	};
	oXmlHttp.send(null);
}

function writeSectionTitle() { // Writes Section Title to SectionTitle Div
	var secTitle = document.getElementById("sectionTitle");
	secTitle.innerHTML = sSectionTitle;
}

function writeContentData(sText) { // Writes Content to ContentContainer Div
	var content = document.getElementById("contentContainer");
	content.innerHTML = sText;
}

function writeContent(sText) { // Wrapper that calls both Functions
	writeSectionTitle();
	writeContentData(sText);
}

function loadingScreen(id,bDisplay) {
	// Make it so it overlaps the entire table, if not, change the innerHTML of bio so it changes to the loader.
	var oLoader = document.getElementById(id);
	if (bDisplay == true) {
		oLoader.innerHTML = '<div id="loader"><img src="'+oImg['loader'].src+'" alt="loader" /><br />Cargando...<\/div>';
	}
}

