
// Dropdown Nav Javascript
var timeout	= 500;
var closetimer	= 0;
var ddmenuitem	= 0;

function mopen(id)
{	
	mcancelclosetime();

	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';

	ddmenuitem = document.getElementById(id);
	ddmenuitem.style.visibility = 'visible';

}
function mclose()
{
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}

function mclosetime()
{
	closetimer = window.setTimeout(mclose, timeout);
}

function mcancelclosetime()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

document.onclick = mclose; 

function SlideArea(sButtonID, sAreaID) 
{
    var sButtonJQID = "#" + sButtonID;
    //alert("About to slide");
    if ($("#" + sAreaID).is(":hidden")) {
        ($("#" + sAreaID)).slideDown("slow");
    }
    else {
        ($("#" + sAreaID)).slideUp("slow");
    }
    //alert("Done slide area");
}


// Div Toggle Javascript

var _sCurrentTabID = null;
//Event of the tab being clicked.
function TabClicked(sTabID, sDivID)
{
	//alert("tada");
	ResetTabsAndDivs();
	var oTab = document.getElementById(sTabID);
	var oDiv = document.getElementById(sDivID);
	if((oTab != null) && (oDiv != null))
	{
		//show the div, 
		oDiv.style.display = "block";
		//alert("here");
		oTab.style.backgroundPosition = "0 -47px";
		_sCurrentTabID = sTabID;
	}
}
	
//Sets all the divs associated with mid-page tabs to hidden.
function ResetTabsAndDivs()
{
	for(var nCounter = 1; nCounter < 5; nCounter++)
	{
		var sDivID = "divTab" + nCounter;
		var sTabID = "tab" + nCounter;
		
		//Hide the div
		var oDiv = document.getElementById(sDivID)
		if(oDiv != null)
			oDiv.style.display = "none";
		
		//Reset the tab
		var oTab = document.getElementById(sTabID);
		if(oTab != null)
			oTab.style.backgroundPosition = "0px 0px";
	}
}

/***************************************************
 * Shows or hides the object with the given id, 
 * based on what it's current style.display value
 * is set to.
 * @param sObjID
 **************************************************/
function ShowHideObject(sObjID)
{
	var objElement = document.getElementById(sObjID);
	if(objElement != null)
	{
		//If it's not currently set to hidden, hide it.
		if(objElement.style.display != "none")
			objElement.style.display = "none";
		else
			objElement.style.display = "";
	}
}
	
//Gets the xml http request object for the browser.
function GetXmlHttpRequest()
{
	var xmlHttp = null;
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlHttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return xmlHttp;
}

//Returns an xml document with the given text parsed as a string.
function GetXMLParser(txt)
{
	alert("Text to parse:" + txt); //debug
	var xmlDom = null;
	if (window.DOMParser)
	{
	  parser=new DOMParser();
	  xmlDom=parser.parseFromString(txt,"text/xml");
	}
	else // Internet Explorer
	{
		alert("ie"); //debug
		xmlDom=new ActiveXObject("Microsoft.XMLDOM");
		xmlDom.async=false;
		xmlDom.loadXml(txt); 
	} 
	alert(xmlDom.getElementsByTagName("root").length); //debug
	return xmlDom;
}

//Gets the first parent element that matches the element type.
//If no elements are found, it returns null.
function GetParentByTagName(objChild, sTagName)
{
	var bMatchingParentFound = false;
	var objParent = objChild.parentNode;
	while(!bMatchingParentFound)
	{
		if(objParent == null)
			bMatchingParentFound = true;
		else if(objParent.tagName == sTagName)
			bMatchingParentFound = true;
		else
			objParent = objParent.parentNode;
	}
	return objParent;
}

//Validates whether a given string is a valid email address
function ValidateEmail(sEmail)
{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var bResult = false;
	if(reg.test(sEmail) == false)
	{
		alert('Please enter a valid email address.');
		bResult = false;
	}
	else
		bResult = true;

	return bResult;
}

//Validates whether a given string is a valid email address
function ValidateEmailNoMessage(sEmail)
{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var bResult = false;
	if(reg.test(sEmail) == false)
		bResult = false;
	else
		bResult = true;

	return bResult;
}

//Validate wether a given string is a valid phone number
//Source: http://www.dreamincode.net/code/snippet69.htm
function ValidatePhone(sPhone)
{
	// Check for correct phone number
	var rePhoneNumber = new RegExp(/^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/);
	if (!rePhoneNumber.test(sPhone)) 
	{
	     alert("Phone Number Must Be Entered As: (555) 555-1234");
	     return false;
	}

	return true;
}

//Validate wether a given string is a valid phone number. 
//Does not display a message box of the error.
//Source: http://www.dreamincode.net/code/snippet69.htm
function ValidatePhoneNoMessage(sPhone)
{
	// Check for correct phone number
	var rePhoneNumber = new RegExp(/^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/);
	if (!rePhoneNumber.test(sPhone)) 
	     return false;
	return true;
}

//Validates the length of the value in the passed in object, 
//as long as it has a value attribute.
function ValidateTextLength(objInput, nMaxLength, bShowError)
{
	if(objInput.value)
	{
		var sValue = objInput.value; 
		if(sValue.length > nMaxLength)
		{
			objInput.style.backgroundColor = "#ecb9af";
			if(bShowError)
				alert("Value cannot be more than " + nMaxLength + " characters.");
		}
	}
	return;
}

//Source http://www.somacon.com/p355.php
//trims the given string on both ends
function Trim(sText) 
{
	return sText.replace(/^\s+|\s+$/g,"");
}
//Trims the left side of the given string.
function LTrim(sText) 
{
	return sText.replace(/^\s+/,"");
}
//Trims the right side of the given string.
function RTrim(sText) 
{
	return sText.replace(/\s+$/,"");
}

//Validates whether the passed value is an integer
function IsInteger(sValue)
{
	if(isNaN(sValue))
	{
		return false;
	}
	if(!parseInt(sValue, 10))
	{
		return false;
	} 
	return true;
}

///Sets the target div area's inner object to be the object
//found by the source target.
function SetMainFeatureContentForTarget(sDivSourceID, sDivTargetID, e)
{
	var objSource = document.getElementById(sDivSourceID);
	var objTarget = document.getElementById(sDivTargetID);

	if(objSource != null && objTarget != null)
	{
		//remove the first node
		if(objTarget.childNodes.length > 0)
		{	
			var oRemoveNode= objTarget.firstChild;
			oRemoveNode.style.display = "none";
			objTarget.removeChild(oRemoveNode);
			objRemoved = oRemoveNode;
			document.body.appendChild(oRemoveNode);
		}
	
		//add the new child
		objTarget.appendChild(objSource);
		objSource.style.display = "block";
	}
	SetActiveMenuItem(e)
}

//Sets the active menu item for the list of features.
function SetActiveMenuItem(e)
{
	//Get the event source
	var sourceElement;
	try
	{
		if (!e) 
			e = window.event;
		
		//Get the source element
		if (e.srcElement) 
			sourceElement = e.srcElement;
		else if (e.target) 
			sourceElement = e.target;
		
		if (sourceElement.nodeType == 3) // defeat Safari bug
			sourceElement = targ.parentNode;
		
		if(e != null)
		{
			//Get all the anchors in the parent
			var parentDiv = GetParentByTagName(sourceElement, "DIV");
			var arrAnchors = parentDiv.getElementsByTagName("A");
			//For each anchor, clear the class name
			for(var nCounter = 0; nCounter < arrAnchors.length; nCounter++)
				arrAnchors[nCounter].className = "";
			
			//Set the classname to be active for the clicked item
			if(sourceElement.tagName == "A")
				sourceElement.className = "active"
		}
	}
	catch(exception)
	{
		//Catch the error that we know IE is going to throw.
	}
}

function MaskedText_OnFocus(objText)
{
	objText.value = "";
	objText.style.color = "black"
}

//Checks the given masked text box for 
function MaskedText_onBlur(objText, sDefaultText, sDefaultColor)
{
	if(Trim(objText.value) == "")
	{
		objText.value = sDefaultText;
		if(sDefaultColor != null && sDefaultColor != "")
			objText.style.color = sDefaultColor;
	}
}

