
//****
//Warning message
//****
function setupWarningMessage(){
	//Javascript
	document.getElementById("spnWarningMessage").innerHTML = "";
}


//****
//Warning message
//****
function setupSpecialDesigns(){
	//Go red
	if(true){
		//Title stock
		document.getElementById("headerContainer").style.backgroundImage = "url(" + getUrl("images/common/titleStockGoRed.png") + ")";
		
		//Click
		document.getElementById("tabAboveClick").style.cursor = "pointer";
		document.getElementById("tabAboveClick").onclick = function(){openPage("http://www.heart.org/lancasterpagoredluncheon")};
		return
	}
	
	//Green week
	//if(thirdMondayToFriday()){
	if(false){
		//Title stock
		document.getElementById("headerContainer").style.backgroundImage = "url(" + getUrl("images/common/titleStockGreenWeek.png") + ")";
		
		//Click
		document.getElementById("tabAboveClick").style.cursor = "pointer";
		document.getElementById("tabAboveClick").onclick = function(){goToPage(getFullUrl("greenweek"))};
		return;
	}
}
function thirdMondayToFriday(){
	//Helpers
	var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
	
    //Get first day of month
	var theFirst = new Date();
	theFirst.setDate(1);

	//Get first monday
	var firstMonday = new Date();
	if(days[theFirst.getDay()] == "Sunday"){
		//Add 1
		firstMonday.setDate(theFirst.getDate() + 1);
	}else if(days[theFirst.getDay()] == "Monday"){
		//Do nothing
	}else if(days[theFirst.getDay()] == "Tuesday"){
		//Add 6
		firstMonday.setDate(theFirst.getDate() + 6);
	}else if(days[theFirst.getDay()] == "Wednesday"){
		//Add 5
		firstMonday.setDate(theFirst.getDate() + 5);
	}else if(days[theFirst.getDay()] == "Thursday"){
		//Add 4
		firstMonday.setDate(theFirst.getDate() + 4);
	}else if(days[theFirst.getDay()] == "Friday"){
		//Add 3
		firstMonday.setDate(theFirst.getDate() + 3);
	}else if(days[theFirst.getDay()] == "Saturday"){
		//Add 2
		firstMonday.setDate(theFirst.getDate() + 2);
	}
	
	//Get third monday
	var thirdMonday = new Date();
	thirdMonday.setDate(firstMonday.getDate() + 14);
	thirdMonday.setHours(00,00,00,00)
	
	//Get third friday
	var thirdFriday = new Date();
	thirdFriday.setDate(thirdMonday.getDate() + 4);
	thirdFriday.setHours(23,59,59,99)

	//Check
	var now = new Date();
	if(now >= thirdMonday){
		if(now <= thirdFriday){
			return true;
		}
	}
	
	return false;
}
function goToPage(url){
	location.href = url;
}
function openPage(url){
	window.open(url ,"_blank");
}


//****
//Copyright year
//****
function setupCopyrightYear(){
	var curDate = new Date();
	document.getElementById("spnCopyrighyYear").innerHTML = curDate.getFullYear();
}


//****
//Html retrieval
//****
function loadHtmlBody(htmlUrl, targetId, finishFunctionCall){
	var xhr = false;
	try{
        xhr = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(err2){
    	try{
        	xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(err3){
			try{
				xhr = new XMLHttpRequest();
			}catch (err1){
				xhr = false;
			}
        }
    }
	
	xhr.onreadystatechange = 
		function(){ 
			if(xhr.readyState == 4){
				document.getElementById(targetId).innerHTML = getBody(xhr.responseText);
				
				if(finishFunctionCall != ""){
					if(finishFunctionCall != null){
						eval(finishFunctionCall);
					}
				}
			}
		};
	
	xhr.open("GET", htmlUrl, true);
	xhr.send("");
}
function getBody(content){
   test = content.toLowerCase();
   
   var x = test.indexOf("<body");
   if(x == -1) return "";

   x = test.indexOf(">", x);
   if(x == -1) return "";

   var y = test.lastIndexOf("</body>");
   if(y == -1) y = test.lastIndexOf("</html>");
   if(y == -1) y = content.length;

   return content.slice(x + 1, y);
}
function fixDropDownListLinks(){
	var aElements = document.getElementById("dropDownsContainer").getElementsByTagName("a");
	for(var i = 0; i < aElements.length; i++){
		if(aElements[i].title.indexOf("http://") == -1){
			aElements[i].href = getUrl(aElements[i].title);
			aElements[i].title = "";
		}else{
			aElements[i].href = aElements[i].title;
			aElements[i].title = "";
		}
	}
}


//****
//Url information
//****
function getFullUrl(url){
	return "http://" + window.location.hostname + "/" + url;
}
function getUrl(url){
	//Get directories back to root
	var toRoot = document.getElementById("linkDropDownsHtmlHelper").getAttribute("href");
	toRoot = toRoot.slice(0, toRoot.lastIndexOf("/") + 1);
	
	return toRoot + url;
}
function getUrlParameter(myParameterName){
	var sURL = document.URL.toString();
	
	if(sURL.indexOf("?") > 0){
		var arrParams = sURL.split("?");
		var arrURLParams = arrParams[1].split("&");
		var arrParamNames = new Array(arrURLParams.length);
		var arrParamValues = new Array(arrURLParams.length);
	
		var i = 0;
		for (i=0; i < arrURLParams.length; i++) {
			var sParam =  arrURLParams[i].split("=");
			arrParamNames[i] = sParam[0];
			if (sParam[1] != "")
				arrParamValues[i] = unescape(sParam[1]);
			else
				arrParamValues[i] = "";
		}
	
		for(i=0; i < arrURLParams.length; i++){
			if(arrParamNames[i] == myParameterName){
				return arrParamValues[i];
			}
		}
	}else{
		return "";
	}
}


//****
//Drop down actions
//****
function showDropDown(myId){
	//Show it
	if(navigator.appName != "Microsoft Internet Explorer"){
		$("#" + myId).fadeIn(250);
	}else{
		$("#" + myId).show();
	}
	
	//Change the tab link color
	var linkId = myId.replace("dropDown", "");
	linkId = "link" + linkId;
	document.getElementById(linkId).style.color = "#000";
}
function hideDropDown(myId, ms){
	//Hide it
	if(navigator.appName != "Microsoft Internet Explorer"){
		$("#" + myId).fadeOut(ms);
	}else{
		$("#" + myId).hide();
	}
	
	//Change the tab link color
	var linkId = myId.replace("dropDown", "");
	linkId = "link" + linkId;
	document.getElementById(linkId).style.color = "#FFF";
}
function dropDownOver(myId){
	//Hide all except
	hideAllDropDownsExcept(myId);
	
	//Save open id
	document.getElementById("openDropDown").innerHTML = myId;
	
	//Show
	showDropDown(myId);
}
function hideAllDropDownsExcept(myId){
	//Hide them
	if(myId != "dropDownAbout"){
		hideDropDown("dropDownAbout", 250);
	}
	if(myId != "dropDownServices"){
		hideDropDown("dropDownServices", 250);
	}
	if(myId != "dropDownProducts"){
		hideDropDown("dropDownProducts", 250);
	}
	if(myId != "dropDownNews"){
		hideDropDown("dropDownNews", 250);
	}
	if(myId != "dropDownDownloads"){
		hideDropDown("dropDownDownloads", 250);
	}
	if(myId != "dropDownContact"){
		hideDropDown("dropDownContact", 250);
	}
	
	//Clear open id
	document.getElementById("openDropDown").innerHTML ="";
}
function dropDownOut(myId){
	//Clear open id
	document.getElementById("openDropDown").innerHTML ="";
	
	//Wait and check open id
	setTimeout("completeDropDownOut('" + myId + "')", 1000);
}
function completeDropDownOut(myId){
	//Check open id
	if(document.getElementById("openDropDown").innerHTML != myId){
		//Hide
		hideDropDown(myId, 500);
	}
}


//****
//Setup survey
//****
function setupSurvey(){
	return;
	
	//Hide survey
	hideSurvey(false);
	
	//Remove hide class
	document.getElementById("survey").className = "";
	
	//Setup scroll follow
	var offset = $("#survey").offset();
	var topPadding = 0;
	$(window).scroll(function() {
		if ($(window).scrollTop() > offset.top) {
			$("#survey").stop().animate({
				marginTop: $(window).scrollTop() - offset.top + topPadding
			});
		} else {
			$("#survey").stop().animate({
				marginTop: 0
			});
		};
	});
	
	//Check cookie
	if(getCookie("showSurvey") != "no"){
		//Wait and show survey
		setTimeout("showSurvey()", 5000);
	}
}
function hideSurvey(hideClicked){
	var distance = parseInt($("#survey").css("left"), 10) - $("#survey").outerWidth();
	
	if(hideClicked){
		//Slide closed
		$("#survey").animate({left: distance}, 250);
		
		//Set cookie
		setCookie("showSurvey", "no");
	}else{
		$("#survey").animate({left: distance}, 0);
	}
}
function showSurvey(){
	$("#survey").animate({left: parseInt($("#survey").css("left"), 10) + $("#survey").outerWidth()}, 500);
}
function setCookie(cookieName, value){
	var expiration_date = new Date();
	expiration_date.setFullYear(expiration_date.getFullYear() + 1);
	
	document.cookie = cookieName + "=" + escape(value) + "; expires=" + expiration_date.toGMTString() + "; path=" + escape("/");
}
function getCookie(cookieName){
	var results = document.cookie.match("(^|;) ?" + cookieName + "=([^;]*)(;|$)");
	
	if(results)
		return(unescape(results[2]));
	else
		return null;
}


//****
//Site path
//****
function setupSitePath(){
	//Get directory count
	var toRoot = document.getElementById("linkDropDownsHtmlHelper").getAttribute("href");
	var countToRoot = countInstances(toRoot, "../");
	
	//Get paths
	var paths = new Array();
	for(var i = 0; i < countToRoot; i++){
		//var x = i + 1;
		paths[i] = getDirectories(i + 1, countToRoot);
	}
	
//	<img src="../images/common/other/sitePathArrow.png" />
//	<div><a href="" class="item">Testing</a></div>
//	<img src="../images/common/other/sitePathArrow.png" />
//	<div><a href="" class="lastItem">Testing</a></div>
	
	//Build html
	var htmlString = "";
	if(paths.length > 0){
		htmlString = htmlString + "<img src='" + getUrl("images/common/other/sitePathArrow.png") + "' />";
		htmlString = htmlString + "<div><a href='" + getUrl("") + "' class='item'>Home</a></div>";
		
		for(var i = 0; i < paths.length; i++){
			htmlString = htmlString + "<img src='" + getUrl("images/common/other/sitePathArrow.png") + "' />";
			if(i == paths.length - 1){
				htmlString = htmlString + "<div><a href='" + getUrl(paths[i]) + "' class='lastItem'>" + getName(paths[i]) + "</a></div>";
			}else{
				htmlString = htmlString + "<div><a href='" + getUrl(paths[i]) + "' class='item'>" + getName(paths[i]) + "</a></div>";
			}
		}
	}
	
	document.getElementById("sitePath").innerHTML = htmlString;
}
function getDirectories(count, toRoot){
	//Get url and remove parameters
	var curUrl = document.location.href;
	if(curUrl.indexOf("?") != -1){
		curUrl = curUrl.slice(0, curUrl.indexOf("?"));
	}
	
	//Get levels of url
	var levels = curUrl.split("/");
	levels.reverse();
	
	//Get good levels
	var j = 0;
	var goodLevels = new Array();
	for(var i = 0; i < levels.length; i++){
		if(levels[i] != "" && levels[i].indexOf("index.") == -1){
			if(j < toRoot){
				goodLevels[j] = levels[i];
				j++;
			}
		}
	}
	goodLevels.reverse();
	
	//Build directories
	var directories
	for(var i = 0; i < count; i++){
		if(i == 0){
			directories = goodLevels[i];
		}else{
			directories = directories + "/" + goodLevels[i];
		}
	}
	
	return directories;
}
function countInstances(myString, myWord) {
  var subStrings = myString.split(myWord);
  return subStrings.length - 1;
}
function getName(directory){
	//Remove all but last directory
	if(directory.indexOf("/") != -1){
		directory = directory.slice(directory.lastIndexOf("/"));
	}
	
	//Remove any extra slashes
	directory = directory.replace("/", "");
	
	//Capitalize, spaces, and exceptions
	if(directory == "it" || directory == "dpm"){
		//Capitalize all
		directory = directory.toUpperCase();
	}else if(directory == "care"){
		//Add space
		directory = "12:34 CARE";
	}else if(directory == "greenweek"){
		//Add space
		directory = "Green Week";
	}else{
		//Capitalize first letter
		directory = directory.charAt(0).toUpperCase() + directory.substr(1).toLowerCase();
	}
	
	return directory;
}
function removeSitePath(){
	document.getElementById("sitePath").innerHTML = "";
}


//****
//Scroll to
//****
function scrollToId(){
	//Get id
	var myId = getUrlParameter("scroll");
	
	//Scroll
	if(myId != ""){
		if(document.getElementById(myId) != null){
			window.scrollTo(0, document.getElementById(myId).offsetTop - 20);
		}
	}
}


//****
//Common roll-over images
//****
function setupDynamicImages(){
	var learnMoreImages = document.getElementsByTagName("img");
	
	for(var i = 0; i < learnMoreImages.length; i++){
		if(learnMoreImages[i].className == "learnMoreLarge"){
			learnMoreImages[i].onmouseover = function(){this.src = getUrl("images/common/other/learnMoreLargeHover.png");}
			learnMoreImages[i].onmouseout = function(){this.src = getUrl("images/common/other/learnMoreLarge.png");}
		}else if(learnMoreImages[i].className == "learnMore"){
			learnMoreImages[i].onmouseover = function(){this.src = getUrl("images/common/other/learnMoreHover.png");}
			learnMoreImages[i].onmouseout = function(){this.src = getUrl("images/common/other/learnMore.png");}
		}else if(learnMoreImages[i].className == "backLarge"){
			learnMoreImages[i].onmouseover = function(){this.src = getUrl("images/common/other/backLargeHover.png");}
			learnMoreImages[i].onmouseout = function(){this.src = getUrl("images/common/other/backLarge.png");}
		}
	}
}


//****
//Form actions
//****
function enterCheck(e, executeButtonId){
	//Check for enter
	if(e.keyCode == 13){
		//Click the button
		document.getElementById(executeButtonId).click();
	}
}
function getMessage(){
	var myId = getUrlParameter("id");
	var message = getUrlParameter("message");
	
	if(myId != "" && message != ""){
		if(document.getElementById(myId) != null){
			changeMessage(myId, "goodFields", message);
		}
	}
}
function changeMessage(myId, myClass, myString){
	$("#" + myId).slideUp("normal", function(){
										   		document.getElementById(myId).className = myClass;
												document.getElementById(myId).innerHTML = myString;
											 });
	$("#" + myId).slideDown();
}

