var g_currentNavItem = -1;
var g_numNavItems = 5;
var g_currentPortfolioPage = -1;
var g_numPortfolioPages = 3;
var g_contentRequest = null;
var g_formRequest = null;

function main () {
	var i, a, loc, regex1, regex2, div;

	loc = "" + window.location;
	regex1 = /^http:\/\/pedromargate.com\/.*\.html$/;
	regex2 = /^http:\/\/www\.pedromargate.com/;
	if (loc.match (regex1) || loc.match (regex2)) {
		div = document.getElementById ('content');
		div.innerHTML = '';
		window.location = 'http://pedromargate.com';
		return;
	}

	if (g_currentNavItem < 0) {
		setCurrentNavItem (0);
	}

	for (i = 0; i < g_numNavItems; i++) {
		a = document.getElementById ('navlink' + i);
		a.href = 'javascript: void (0);';
	}
}

function highlightNavItem (itemNumber) {
	var div;

	if (g_currentNavItem == itemNumber) { return; }

	div = document.getElementById ('navitem' + itemNumber);
	if (div == null) { return; }
	div.style.background = '#360036';
}

function unhighlightNavItem (itemNumber) {
	var div;

	if (g_currentNavItem == itemNumber) { return; }

	div = document.getElementById ('navitem' + itemNumber);
	if (div == null) { return; }
	div.style.background = '#240024';
}

function setCurrentNavItem (itemNumber) {
	var div, a, iframe;

	if (g_currentNavItem >= 0) {
		div = document.getElementById ('navitem' + g_currentNavItem);
		if (div != null) { 
			div.style.background = '#240024';
		}

		a = document.getElementById ('navlink' + g_currentNavItem);
		if (a != null) {
			a.style.fontWeight = 'normal';
			a.style.color = '#EEEE00';
		}
	}

	g_currentNavItem = itemNumber;
	div = document.getElementById ('navitem' + g_currentNavItem);
	if (div != null) {
		div.style.background = '#480048';
	}

	a = document.getElementById ('navlink' + g_currentNavItem);
	if (a != null) {
		a.style.fontWeight = 'bold';
		a.style.color = '#DDDDDD';
	}

	g_contentRequest = createXMLHttpRequest ();
	g_contentRequest.open ('GET', 'content' + g_currentNavItem + '.html', true);
	g_contentRequest.onreadystatechange = contentRequestCallback;
	g_contentRequest.send (null);
/*
	iframe = document.getElementById ('contentframe');
	if (iframe != null) {
		if (iframe.src == ('content' + g_currentNavItem + '.html')) { return; }
	}
	div.innerHTML = '<iframe src="content' + itemNumber + '.html" id="contentframe" scrolling="no" border="0" frameborder="0"></iframe>';
*/
}

function highlightPortfolioPage (pageNumber) {
	var div;

	if (g_currentPortfolioPage == pageNumber) { return; }

	div = document.getElementById ('pagecol' + pageNumber);
	if (div == null) { return; }
	div.style.background = '#AAAAAA';
}

function unhighlightPortfolioPage (pageNumber) {
	var div;

	if (g_currentPortfolioPage == pageNumber) { return; }

	div = document.getElementById ('pagecol' + pageNumber);
	if (div == null) { return; }
	div.style.background = '#0A0A1A';
}

function setPortfolioPage (pageNumber) {
	var i, display, span, a;

	switch (pageNumber) {
		default: {
			for (i = 1; i <= 11; i++) {
				div = document.getElementById ('portfoliorow' + i);
				if (div == null) { continue; }
				display = false;

				if ((i >= 1) && (i <= 4)) { display = true; }

				div.style.display = display ? 'block' : 'none';
			}
			break;
		}
		case 2: {
			for (i = 1; i <= 11; i++) {
				div = document.getElementById ('portfoliorow' + i);
				if (div == null) { continue; }
				display = false;

				if ((i >= 5) && (i <= 8)) { display = true; }

				div.style.display = display ? 'block' : 'none';
			}
			break;
		}
		case 3: {
			for (i = 1; i <= 11; i++) {
				div = document.getElementById ('portfoliorow' + i);
				if (div == null) { continue; }
				display = false;

				if ((i >= 9) && (i <= 10)) { display = true; }

				div.style.display = display ? 'block' : 'none';
			}
			break;
		}
	}

	span = document.getElementById ('pagecol' + g_currentPortfolioPage);
	if (span != null) {
		span.style.background = '#0A0A1A';
		span.style.color = '#EEEEEE';
		span.style.fontWeight = 'normal';
	}
	a = document.getElementById ('pagelink' + g_currentPortfolioPage);
	if (a != null) {
		a.style.color = '#EEEE00';
	}

	g_currentPortfolioPage = pageNumber;
	span = document.getElementById ('pagecol' + g_currentPortfolioPage);
	if (span != null) {
		span.style.background = '#999999';
		span.style.color = '#FFFFFF';
		span.style.fontWeight = 'bold';
	}
	a = document.getElementById ('pagelink' + g_currentPortfolioPage);
	if (a != null) {
		a.style.color = '#FFFFFF';
	}
}

function contentRequestCallback () {
	switch (g_contentRequest.readyState) {
		case 1: {
			break;
		}
		case 3: {
			break;
		}
		case 4: {
			var i, a, found, div, input;

			if (g_contentRequest.status != 200) { break; }

			div = document.getElementById ('content');
			div.innerHTML = g_contentRequest.responseText;
			h = div.offsetHeight;

			found = false;
			for (i = 1; i <= g_numPortfolioPages; i++) {
				a = document.getElementById ('pagelink' + i);
				if (a != null) {
					a.href = 'javascript: void (0);';
					found = true;
				}
			}

			if (found) {
				setPortfolioPage (1); 
			}

			input = document.getElementById ('contactbutton');
			if (input != null) {
				input.style.display = 'none';
			}

			g_contentRequest.abort ();
			g_contentRequest = null;
			break;
		}
	}

//	console.log ("EVENT WITH READY of " + g_contentRequest.readyState);
}

function submitForm () {
	var params, input, err, span, name, email, message, errcount;

	if (g_formRequest != null) { return; }

	err = null;
	errcount = 0;
	input = document.getElementById ('nameinput');
	name = input.value;
	if (name.length <= 0) { errcount++; }

	input = document.getElementById ('emailinput');
	email = input.value;
	if (email.length <= 0) { errcount++; }

	input = document.getElementById ('messageinput');
	message = input.value;
	if (message.length <= 0) { errcount++; }

	if (errcount > 0) {
		if (errcount > 1) {
			err = "* Please enter values in all fields and submit the form again.";
		}
		else {
			if (name.length <= 0) {
				err = "* The name field can't be empty. Please enter a value there and submit the form again.";
			}
			else if (email.length <= 0) {
				err = "* The email field can't be empty. Please enter a value there and submit the form again.";
			}
			else if (message.length <= 0) {
				err = "* The message field can't be empty. Please enter some text there and submit the form again.";
			}
		}
	}

	if (err != null) {
		span = document.getElementById ('contacttext');
		span.innerHTML = err;
		return;
	}

	params = 'name=' + escape (name) + '&email=' + escape (email) + '&message=' + escape (message);
	g_formRequest = createXMLHttpRequest ();
	g_formRequest.open ('POST', '/cgi-bin/contact', true);
	g_formRequest.setRequestHeader ('Content-type', 'application/x-www-form-urlencoded');
	g_formRequest.setRequestHeader ('Content-length', params.length);
	g_formRequest.setRequestHeader ('Connection', 'Close');
	g_formRequest.onreadystatechange = formRequestCallback;
	g_formRequest.send (params);
}

function formRequestCallback () {
	switch (g_formRequest.readyState) {
		case 1: {
			break;
		}
		case 3: {
			break;
		}
		case 4: {
			var div;

			div = document.getElementById ('contactform');
			if (div != null) {
				div.style.display = 'none';
			}

			div = document.getElementById ('contactintro');
			if (div != null) {
				div.style.display = 'none';
			}

			div = document.getElementById ('contactformresult');
			if (div != null) {
				if ((g_formRequest.status != 200) || (g_formRequest.responseText != '<html><body>OK</body></html>')) {
					div.innerHTML = 'Sorry, an error occurred while processing your request. Please try again later.';
				}
				else {
					div.innerHTML = 'Your message has been sent. Thank you for the submission!';
				}
				div.style.display = 'block';
			}

			g_formRequest.abort ();
			g_formRequest = null;
			break;
		}
	}
//	console.log ("EVENT WITH READY of " + g_contentRequest.readyState);
}

function createXMLHttpRequest () {
	var req;

	req = null;
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest ();
		if (req.overrideMimeType) { req.overrideMimeType ('text/xml'); }
	}
	
	if (! req) {
		if (window.ActiveXObject) {
			try {
				req = new ActiveXObject ("Msxml2.XMLHTTP");
			}
			catch (e) {
				req = null;
			}
	
			if (! req) {
				try {
					req = new ActiveXObject ("Microsoft.XMLHTTP");
				}
				catch (e) {
					req = null;
				}
			}
		}
	}
	
	return (req);
}

