// JavaScript Document

// global variables
var strOverlayId = 'newoverlay';
var strResultsId = 'newresults';
var objForm0 = '';

function fncShowHide(strId, objLink) {
	new Effect.toggle(strId, 'blind');
	if (objLink.id == 'morelink') {
		objLink.style.display = 'none';
	} else {
		document.getElementById('morelink').style.display = 'inline';
	}
}

// form validation
function checkForm(theForm)
{

	var labels = theForm.getElementsByTagName("label");
	var error = false;
	var first = null;

	for (var i = 0; i < labels.length; i++)
	{
		if (labels[i].className.classExists("required"))
		{
			var input = labels[i].getElementsByTagName("input")[0] || labels[i].getElementsByTagName("textarea")[0] || labels[i].getElementsByTagName("select")[0];
			var spans = labels[i].getElementsByTagName("span");
			var errorText = null;
			var labelText = null;

			for (var j = 0; j < spans.length; j++)
			{
				if (spans[j].className.classExists("labelText"))
				{
					var labelText = spans[j];
				}
			}

			if (input != null)
			{
				if (input.value == "")
				{
					errorText = "Please fill in your " + labelText.childNodes[0].nodeValue.toLowerCase().replace(/:/, "");
				}
				else if (labels[i].className.classExists("requiredEmail") && !input.value.validEmail())
				{
					errorText = "Please supply a valid e-mail address";
				}

				if (errorText != null)
				{
					if (first == null)
					{
						first = input;
					}

					error = true;
					writeCorrection(labels[i], errorText);

					if (input.nodeName.toLowerCase() == "input" && input.getAttribute("type") == "text")
					{
						input.onkeyup = checkValidity;
					}
					else if (input.nodeName.toLowerCase() == "select")
					{
						input.onchange = checkValidity;
					}
				}
				else
				{
					writeCorrection(labels[i]);
				}
			}
		}
	}

	if (error) {
		first.focus();
		return false;
	} else {
		return true;
	}
}

function writeCorrection(label, text, correct)
{
	var spans = label.getElementsByTagName("span");
	var input = label.getElementsByTagName("input")[0] || label.getElementsByTagName("textarea")[0] || label.getElementsByTagName("select")[0];
	var image = label.getElementsByTagName("img")[0];

	if (typeof text == "undefined")
	{
		if (image != null)
		{
			label.removeChild(image);
		}

		for (var j = 0; j < spans.length; j++)
		{
			if (spans[j].className.classExists("correctionText"))
			{
				label.removeChild(spans[j]);

				break;
			}
		}
	}
	else
	{
		if (image == null)
		{
			image = document.createElement("img");
			image.className = "correctionIcon";
			//image = label.insertBefore(image, input);

			var newText = document.createElement("span");
			newText.className = "correctionText";
			label.appendChild(newText);
		}

		var spans = label.getElementsByTagName("span");

		for (var j = 0; j < spans.length; j++)
		{
			if (spans[j].className.classExists("correctionText"))
			{
				var correctionText = spans[j];

				break;
			}
		}

		if (correct == true)
		{
			//image.setAttribute("src", "images/yes.gif");
			//image.setAttribute("alt", "Correct");
			correctionText.className = correctionText.className.removeClass("warning");
		}
		else
		{
			//image.setAttribute("src", "images/x.gif");
			//image.setAttribute("alt", "Incorrect");
			correctionText.className = correctionText.className.addClass("warning");
		}

		writeSpan(correctionText, text);
	}

	return true;
}

function writeSpan(span, text)
{
	var children = span.childNodes;

	for (var i = 0; children.length > 0;)
	{
		span.removeChild(children[i]);
	}

	var textNode = document.createTextNode(text);
	span.appendChild(textNode);

	return true;
}

function checkValidity()
{
	var label = this.parentNode;
	var spans = label.getElementsByTagName("span");
	var labelText = null;

	for (var j = 0; j < spans.length; j++)
	{
		if (spans[j].className.classExists("labelText"))
		{
			var labelText = spans[j];
		}
	}

	if (this.value == "")
	{
		writeCorrection(label, "Please fill in your " + labelText.childNodes[0].nodeValue.toLowerCase().replace(/:/, ""));
	}
	else if (label.className.classExists("requiredEmail") && !this.value.validEmail())
	{
		writeCorrection(label, "Please supply a valid e-mail address");
	}
	else
	{
		writeCorrection(label, "This field is correct", true);
	}

	return true;
}



//
function frmSubmitCallBack(strResponse) {
	var objResponse = eval("(" + strResponse.responseText + ")");
	if (objResponse.response == true) {
		objForm0.style.display = 'none';
		document.getElementById(strResultsId).innerHTML = '<p class="message"><strong>Your message has been sent. Thanks for contacting Videoware!</strong></p><p><a href="/" onclick="Dialog.closeInfo();BackOnHack();return false;">close window</a></p>';
	} else {
//		document.getElementById(strResultsId).innerHTML = '<span class="message">' + objResponse.data + '</span>';
	}
	// fade overlay out
	new Effect.Fade(strOverlayId, {duration: 0.1});
	// fade results in
	new Effect.Appear(strResultsId, {duration: 1.0});
}

function frmSubmitHandler(objForm) {
	if (!checkForm(objForm)) {
		return false;
	}
	if (Ajax) {
		//
		objForm0 = objForm;
		// hide existing results or create results div
		if (document.getElementById(strResultsId)) {
			new Effect.Fade(strResultsId, {duration: 0.1});
		} else {
			var objResults = Builder.node('div', {id:strResultsId, style:"display:none;"});
			objForm.parentNode.appendChild(objResults);
		}
		new Ajax.Request(
			'contact/index.php', {
				method: 'post',
				parameters: $('contactform').serialize(true),
				onSuccess: frmSubmitCallBack
		});
		// test for existing overlay; if no overlay, then add one
		if (!document.getElementById(strOverlayId)) {
			var objOverlay = Builder.node('div', {id:strOverlayId, className:"overlay", style:"display:none;"});
			objForm.appendChild(objOverlay);
		}
		// fade overlay in
		new Effect.Appear(strOverlayId, {duration: 0.4, from:0.0, to:0.6});
		// return false to avoid form submission
		return false;
	} else {
		return true;
	}
}
function HideHack() {
	document.getElementById('flashcontent').style.visibility = 'hidden';
	var arrH2 = document.getElementsByTagName('h2');
	if (arrH2[0].hasClassName) {
		for (var i = 0; i < arrH2.length; i++) {
			if (arrH2[i].hasClassName('sIFR-flash') || arrH2[i].hasClassName['sIFR-replaced']) {
				arrH2[i].style.visiblity = 'hidden';
			}
		}
	}
}
function BackOnHack() {
	document.getElementById('flashcontent').style.visibility = 'visible';
	var arrH2 = document.getElementsByTagName('h2');
	if (arrH2[0].hasClassName) {
		for (var i = 0; i < arrH2.length; i++) {
			if (arrH2[i].hasClassName('sIFR-flash') || arrH2[i].hasClassName['sIFR-replaced']) {
				arrH2[i].style.visiblity = 'visible';
			}
		}
	}
}
function showVideo() {
	if (Dialog) {
		HideHack();
		Dialog.info(
			'<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="320" height="240" id="foundationvideo" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="media/video/foundationvideo.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src="media/video/foundationvideo.swf" quality="high" bgcolor="#ffffff" width="320" height="240" name="foundationvideo" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object><form id="frmVideo" onsubmit="return false;"><fieldset style="margin-top:20px;text-align:center;width:320px;"><input type="button" id="cmdClose" value="Close" onclick="javascript:Dialog.closeInfo();BackOnHack();" /></fieldset></form>',
			{
				width: 520,
				className: 'alphacube',
				showProgress: false
			}
		);
		return false;
	} else {
		return true;
	}
}
function showModal(strContact) {
	if (Dialog) {
		HideHack();
		Dialog.info(
			{
				url: 'includes/contact.php',
				options: {
					method: 'get',
					parameters: { contact:strContact }
				}
			},
			{
				width: 600, 
				className: 'alphacube',
				showProgress: false
			} 
		);
		return false;
	} else {
		return true;
	}
}

function lnkExternalLaunch(strHref) {
	objNewWin = window.open(strHref, "newwin");
	objNewWin.focus();
}

function pgInit() {
	// add actions to links
	if (!document.getElementsByTagName) {
		return false;
	}
	var arrLinks = document.getElementsByTagName('a');
	var arrClasses = "";
	for (var i = 0; i < arrLinks.length; i++) {
		if (arrLinks[i].getAttribute("href") && arrLinks[i].getAttribute("rel") == "external") {
			arrLinks[i].href = "javascript:lnkExternalLaunch('" + arrLinks[i].getAttribute("href") + "');"
		}
	}
}

// extend string to add class names
String.prototype.addClass = function(theClass) {
	if (this != "") {
		if (!this.classExists(theClass)) {
			return this + " " + theClass;
		}
	} else {
		return theClass;
	}
	return this;
}
// extend string to check for existence of class name
String.prototype.classExists = function(theClass) {
	var regString = "(^| )" + theClass + "\W*";
	var regExpression = new RegExp(regString);
	
	if (regExpression.test(this))
	{
		return true;
	}
	return false;
}
// remove class name from string
String.prototype.removeClass = function(theClass) {
	var regString = "(^| )" + theClass + "\W*";
	var regExpression = new RegExp(regString);
	return this.replace(regExpression, "");
}

// extend string to include email validation
String.prototype.validEmail = function() {
	if (this.match(/^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/)) {
		return true;
	}
	return false;
}

// add event handlers
Event.observe(window, 'load', pgInit);
