var browser = navigator.appName;
var version = navigator.appVersion;
var isIE = 0;
if (browser == "Microsoft Internet Explorer") {isIE=1;}

var request;
var xDiff, yDiff;
var xVal, yVal, xLimit;
var posX, posY;

var divObj;
var divObjToFocus;
var cid;
var intPersonId;
var strFullName = "";
var strToEmail = "";

function createRequest() {
    try { request = new XMLHttpRequest(); } 
    catch (trymicrosoft) {
        try { request = new ActiveXObject("Msxml2.XMLHTTP"); } 
        catch (othermicrosoft) {
            try { request = new ActiveXObject("Microsoft.XMLHTTP"); } 
            catch (failed) { request = false; }
        }
    }
}

function displayTopicForm() {
	var p = document.getElementById("DynamicTopicForm");
	width=310;
			
	// get the center of the window
	var w = 280;
	var h = (isIE)?270:250;
			
	height = 700;
			
	p.style.left = (posX-w) + "px";
	p.style.top = (posY-h) + "px";
    xVal = 0; yVal = 0; xLimit = width;
    yDiff = parseInt(height / 20);
    xDiff = parseInt(width / 20);
    p.style.pixelHeight = height; p.style.pixelWidth = width;
	p.style.visibility = "visible";
	var fader = new Fadomatic(p, 10, 20, 20, 100);
	fader.fadeIn();
}

function displaydynamictext(Id,FullName) {
    cid = Id;
    strFullName = FullName;
    if (strFullName == undefined || strFullName == "undefined") {
		strFullName = "";
	}
    
   	createRequest();
    var url = "Sept11Services.aspx?AjaxCall=1&fn=" + cid + "&rand=" + Math.random();
	if (cid==2 && strFullName=="ContactUs") {
		url = url + "&FromName=Sept11@legacy.com";
	}
    request.open("GET", url, true);
    request.send(null);

	if (cid == 1)
    {
		divObjToFocus = "";
		request.onreadystatechange = displayText;
	}
	else if (cid == 2 || cid == 64)
	{
		if(document.all){
		    intPersonId = document.getElementById("PersonID").innerText;
		} else{
		    intPersonId = document.getElementById("PersonID").textContent;
		}
		divObj = "DynamicMailForm";
		divObjToFocus = "ToEmail";
		request.onreadystatechange = displayDynamicText;
	}
	else if (cid == 16)
	{
		divObj = "DynamicSearchForm";
		divObjToFocus = "LastName";
		request.onreadystatechange = displayDynamicText;
	}
	else if (cid == 32)
	{
		if(document.all){
		    intPersonId = document.getElementById("PersonID").innerText;
		} else{
		    intPersonId = document.getElementById("PersonID").textContent;
		}
    
		divObj = "DynamicDiscussionForm";
		divObjToFocus = "FirstName";
		request.onreadystatechange = displayDynamicText;
	}
	else if (cid == 128 || cid==256)
	{
		divObj = "DynamicTopicForm";
		divObjToFocus = "";
		request.onreadystatechange = displayDynamicText;
	}
	else if (cid == 512)
	{
		divObj = "DynamicDiscussionForm";
		divObjToFocus = "";
		request.onreadystatechange = displayDynamicText;
	}
}

function displayDynamicSearchText(Id, lname, city, state, location)
{
	cid = Id;
	strFullName = "SearchResults";
	createRequest();
    var url = "Sept11Services.aspx?AjaxCall=1&fn=" + cid + "&lname=" + encodeURIComponent(unescape(lname)) + "&city=" + encodeURIComponent(unescape(city));
    url = url + "&stateID=" + state + "&location=" + location + "&rand=" + Math.random();
    request.open("GET", url, true);
    request.send(null);
	divObj = "DynamicSearchForm";
	divObjToFocus = "LastName";
	request.onreadystatechange = displayDynamicText;
}

function sendMail() {
    createRequest();
    strToEmail = document.getElementById("ToEmail");
    var strFromEmail = document.getElementById("FromEmail");
    var strComments = document.getElementById("Comments");
    
	var param = "ToEmail=" + encodeURIComponent(strToEmail.value) + "&FromEmail=" + encodeURIComponent(strFromEmail.value) + "&Comments=" + encodeURIComponent(strComments.value);
    
	if (cid==64)
	{
		param = param + "&PersonID=" + intPersonId + "&FullName=" + encodeURIComponent(strFullName);
		var url = "Sept11Services.aspx?AjaxCall=1&fn=8";
	}
	else
	{    
		var url = "Sept11Services.aspx?AjaxCall=1&fn=4";
	}
	param = param + "&LocationInfo="+ encodeURIComponent(document.location.href) +"&rand=" + Math.random();
	
	request.open("POST", url, true);
	request.onreadystatechange = displayResult;
	request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	request.setRequestHeader("Content-Length", param.length);
	request.send(param);
}

function displayResult() {
	if (request.readyState == 4) {
        if (request.status == 200) 
        {
			if (request.responseText == "True") 
			{ 
				hideDynamicText(divObj); 
				alert("Your e-mail has been sent successfully to " + strToEmail.value + ".");
			}
			else
			{
				alert("We are sorry, your e-mail was not successful. Please ensure that all addresses entered are \ncorrect and that there are no spaces in the e-mail addresses.\n\nPlease try to send your e-mail again.");
			}
        }
        else
        {
			//alert(request.responseText);
			alert("We are sorry, your e-mail was not successful. Please ensure that all addresses entered are \ncorrect and that there are no spaces in the e-mail addresses.\n\nPlease try to send your e-mail again.");
		}
    } 
}

function ProcessMessage(argMsg)
{
	var a = document.getElementById("dForm");
	a.innerHTML = argMsg + "<br><br><br><br><br><br><br><br><br><br><br>";
			
	var img = document.getElementById("image1");
	if (img != null) {img.style.visibility="hidden";}
}

function SubmitDiscussion()
{
	createRequest();
	var strFirstName = document.getElementById("FirstName").value;
    var strLastName = document.getElementById("LastName").value;
    var strCity = document.getElementById("City").value;
    var strState = document.getElementById("State").value;
    if (strState=="NoState") {
		strState = "";
	}
    var strMessage = document.getElementById("Message").value;
    
    if (strFirstName == "" && strLastName == "" && strMessage == "")
    {
		alert("Cannot save entry to the message board for the following reason:\n\nThere is nothing to submit.");
		document.getElementById("FirstName").focus();
    }
    else
    {
		var param = "FirstName=" + encodeURIComponent(strFirstName) + "&LastName=" + encodeURIComponent(strLastName);
		param = param + "&City=" + encodeURIComponent(strCity) + "&State=" + encodeURIComponent(strState);
		param = param + "&Message=" + encodeURIComponent(strMessage) + "&PersonID=" + intPersonId + "&rand=" + Math.random();
		var url = "Sept11Services.aspx?AjaxCall=1&fn=256&PersonID=" + intPersonId;
		request.open("POST", url, true);
		var ReturnedMsg = "<table width=85%><tr><td width=100%>Thank You.<br><br>Your message will be reviewed for appropriate content. ";
		ReturnedMsg = ReturnedMsg + "<br><br>Messages typically appear online within hours.</td></tr></table>";
		ProcessMessage(ReturnedMsg);
		request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		request.setRequestHeader("Content-Length", param.length);
		request.send(param);
	}
}

function displayText() {
    if (request.readyState == 4) {
        if (request.status == 200) { 
            var p = document.getElementById("DynamicFeaturedProfile");
            p.innerHTML = request.responseText;
            var fader = new Fadomatic(p, 10, 20, 20, 100);
			fader.fadeIn();
        }
    }
}

function displayDynamicText() {
    var height, width;
    if (request.readyState == 4) {
        if (request.status == 200) { 
			var p = document.getElementById(divObj);
			width=310;
			height = (cid==32)?400:380;
			
			// get the center of the window
			var w = 280;
			var h = (isIE)?270:250;
			
			if (cid==2||cid==64) {
				h = h+100;
				if (strFullName=="ContactUs") {
					w = w-300;
					h = h-200;
				}
			}
			if (cid==16) {
				if (strFullName == "SearchResults")
				{
					w = w-65;
					h = h-((isIE)?280:260);
				}
				else
				{
					w = w-250;
					h = h+40;
				}
			}
			else if (cid==32) {
				if (isIE) {
					posX = (posX>450)?450:posX;
				}

				w = w-200;
				h = h-100;
			}
			else if (cid==128) {
				h = h-80;
				height = 690;
			}
			
			p.style.left = (posX-w) + "px";
			p.style.top = (posY-h) + "px";
			
			p.innerHTML = request.responseText;
            xVal = 0; yVal = 0; xLimit = width;
            yDiff = parseInt(height / 20);
            xDiff = parseInt(width / 20);
            p.style.pixelHeight = height; p.style.pixelWidth = width;
	        p.style.visibility = "visible";
	        
			var fader = new Fadomatic(p, 10, 20, 20, 100);
			fader.fadeIn();
			
			if (cid == 2 || cid == 16 || cid == 32 || cid == 64) {
	        	document.getElementById(divObjToFocus).focus();
	        }
        }
    }
}

function hideDynamicText(e)
{
	//togglecover(false);
	var p = document.getElementById(e);
	p.style.visibility = "hidden";
}

function rollopenclose(name, bOpen) { 
    var obj = document.getElementById(name).style;
    if (bOpen) {
        if (xVal < xLimit + xDiff) {	
    		xVal += xDiff;
	    	yVal += yDiff;
		    obj.clip="rect(0px "+xVal+"px "+yVal+"px 0px)";
		    setTimeout("rollopenclose('"+name+"', true)", 10);
	    }
    } else {
        if (xVal < xLimit) {	
    		xVal += xDiff;
	    	yVal -= yDiff;
		    obj.clip="rect(0px auto "+yVal+"px "+xVal+"px)";
		    setTimeout("rollopenclose('"+name+"', false)", 10);
	    } else { obj.visibility = "hidden"; }
    }
}

function setOpacity(obj, opacity) {
	opacity = (opacity == 100)?99.999:opacity;
	// IE/Win
	obj.style.filter = "alpha(opacity:"+opacity+")";
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;
	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}

function togglecover(bShow) {
    var obj = document.getElementById("sitecover");
    if (obj != null) {
        if (!bShow) { obj.style.visibility = "hidden"; } 
        else {
            obj.style.pixelHeight = (isIE) ? document.body.scrollHeight : document.height;
            obj.style.pixelWidth = (isIE) ? document.body.scrollWidth : document.width;
            setOpacity(obj, 50) 
            obj.style.visibility = "visible"; 
        }
    }
}

function captureMousePosition(e){
	// captures the mouse position
	posX = 0; posY = 0;
	if (!e){var e = window.event;}
	if (e.pageX || e.pageY){
		posX = e.pageX;
		posY = e.pageY;
	}
	else {
		var de = document.documentElement;
        var b = document.body;
        if (de && b) {
			posX = e.clientX + 
				(de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
			posY = e.clientY + 
				(de.scrollTop || b.scrollTop) - (de.clientTop || 0);
        }
	}
}

if (!isIE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = captureMousePosition

function CheckSept11SearchForm()
{
	var strLastName = document.SearchForm.LastName.value;
	var strHomeCity = document.SearchForm.City.value;
	var SelectedLocation = document.SearchForm.Location.options[document.SearchForm.Location.selectedIndex].value;
	var SelectedState = document.SearchForm.StateID.options[document.SearchForm.StateID.selectedIndex].value;
	var strErrorMsg = "Please complete one or more of the search fields.";
		
	if (strLastName == "" && strHomeCity == "" && SelectedState == "" && SelectedLocation == "")
	{
		alert(strErrorMsg);
		//return false;
	}
	else
	{
		var strParam = "SearchResult.aspx?LastName=" + strLastName + "&City=" + strHomeCity;
		strParam = strParam + "&StateID=" + SelectedState + "&Location=" + SelectedLocation;
		document.location.href = strParam;
		//return true;
	}			
}

function OpenTribute(intPersonId)
{
	window.open("/MT/Tribute.aspx?TributePersonId=" + intPersonId + "&Cobrand=LEGACY",'tribute', 
				'menubar=no,scrollbars=yes,height=520,width=758,dependent=yes,left=50,top=50');
}
