/**
 * @author Justin Evans
 * AHA - IS&T
 */
// classes.js
// Extends mootools.js (version 1.11)
// Extends swfobject.js

window.addEvent('domready', function() {
	// Instantiate classes and call DOM-ready functions here
	
	// RSS Source:
	// http://www.politico.com/rss/politics08.xml
	// This CANNOT work in Mozilla due to security - must use local XML file or server side processing
	//showFeed("http://www.politico.com/rss/politics08.xml", "xsl/rss.xsl");
	//showFeed("xsl/rss.xml", "xsl/rss.xsl"); // local test
	set_menu();
});

window.addEvent("onload", function() {
	// Call window.onload functions here
});

function showFeed(xmlUrl, xslUrl)
{
	var feed = $('rss_feed');

	//clear feed div
	while(feed.hasChildNodes())
	{
		feed.removeChild(feed.childNodes[0]);
	}
	//append new htmlfragment
	if(typeof XMLHttpRequest!='undefined')
	{
		// Most browsers should support object [DocumentFragment]
		feed.appendChild(getHtmlFragment(xmlUrl, xslUrl));
	}
	else
	{
		// IE version - problems with object [DocumentFragment]
		feed.innerHTML=getHtmlFragment(xmlUrl, xslUrl);
	}
}

function getHtmlFragment(xmlUrl, xslUrl)
{
	if (typeof XMLHttpRequest!='undefined')
	{
		var xslStylesheet;
		var xsltProcessor;
		var xmlSource;
		xsltProcessor = new XSLTProcessor();
		//load the xml file
		xmlSource = getResponseXml(xmlUrl).responseXML;
		//load the xsl file into the XSLT Processor
		xslStylesheet = getResponseXml(xslUrl).responseXML;
		xsltProcessor.importStylesheet(xslStylesheet);
		return(xsltProcessor.transformToFragment(xmlSource, document)); // object [DocumentFragment]
	}
	else
	{
		// Use IE method
		var XMLDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
		XMLDoc.async = false;
		XMLDoc.load(xmlUrl);
		
		// Load XSLT file
		var XSLTDoc = new ActiveXObject("Msxml2.DOMDocument.3.0");
		XSLTDoc.async = false;
		XSLTDoc.load(xslUrl);
		
		return(XMLDoc.transformNode(XSLTDoc));
	}
}

function getResponseXml(xmlUrl)
{
	var xmlHttp=false;
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	try
	{
	  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	 }
	 catch (e)
	 {
		try
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E)
		{
			xmlHttp = false;
		}
	}
	@end @*/
	if (!xmlHttp && typeof XMLHttpRequest!='undefined')
	{
		try
		{
			xmlHttp = new XMLHttpRequest();
		}
		catch (e)
		{
			xmlHttp=false;
		}
	}
	if (!xmlHttp && window.createRequest)
	{
		try
		{
			xmlHttp = window.createRequest();
		}
		catch (e)
		{
			xmlHttp=false;
		}
	}
	
	xmlHttp.open("GET", xmlUrl, false);
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4)
		{
			
		}
	}
	xmlHttp.send(null);
	
	return xmlHttp;
}

// Loading flash

function load_swf(swfUrl, strWidth, strHeight)
{
	var objSwf = new SWFObject(swfUrl, "featured video", strWidth, strHeight, "8", "#eaf2ff");
	objSwf.write("flashcontent");
}

// Set top menu state based on URL

function set_menu()
{
	if($('about') && $('updates') && $('links') && $('contact'))
	{
		switch( get_file() )
		{
			case "about.html":
				$('about').className = 'active';
				$('contact').className = 'last';
				break;
			case "updates.html":
				$('updates').className = 'active';
				$('contact').className = 'last';
				break;
			case "links.html":
				$('links').className = 'active';
				$('contact').className = 'last';
				break;
			case "contact.html":
				$('contact').className = 'active';
				break;
			default:
				break;
		}
	}
}

function get_file ( )
{
	var url = "" + window.location + ""; // convert window location to a string
	var arrSegments = url.split( "/" );
	var file;
	if(arrSegments[arrSegments.length - 1])
	{
		file = arrSegments[arrSegments.length - 1];
	}
	else
	{
		file = arrSegments[arrSegments.length];
	}
	
	if (file != undefined) 
	{
		return file;
	}
	else
	{
		return "";
	}
}

function valid_form( strForm )
{
	if($E('#' + strForm + ' .name').value == '')
	{
		alert("\"Name\" is a required field");
		$E('#' + strForm + ' .name').focus();
		return false;
	}
	else if($E('#' + strForm + ' .email').value == '')
	{
		alert("\"Email\" is a required field");
		$E('#' + strForm + ' .email').focus();
		return false;
	}
	else 
	{
		return true;
	}
}

// Valid Email
function valid_email ( strForm )
{
	var sEmail=$E('#' + strForm + ' .email').value;
	if (sEmail.indexOf(' ')==-1 && 0<sEmail.indexOf('@') && sEmail.indexOf('@')+1 < sEmail.length)
	{
		return true;
	} 
	else 
	{
		alert ("Invalid Email address");
		$E('#' + strForm + ' .email').focus();
		return false;
	}
}

// On submit, run all the checks
function onSubmitCheck ( strForm )
{
	if ( !valid_form( strForm ) )
	{
		return false;
	}
	else if ( !valid_email( strForm ) )
	{
		return false;
	}
	else
	{
		return true;
	}
}