var currentActiveMenu = null;
var nextActiveMenu = null;
var imageSpecifier = "";
var imageSel = "";
var selReady = 0;
var surfaceNames = [];
var surfaceNamesXMLObj = null;
var zipcode = 0;
var email = null;
var password = null;
var imUserID = null;
var template = "GAF";
var CurrentProductName = "";
var CurrentProductSeries = "";
var RenderServer = "renderengine";

function updateImage(specifier, sel)
{
	if(imageSpecifier != specifier)
	{
		updateSurfaces(specifier);
	}
	imageSpecifier = specifier;
	imageSel = sel;
	selReady = 0;
	AJAXReplace("renderedimagescreen", "ZoomableImage.aspx?Specifier=" + imageSpecifier + "&Width=626&sel=" + imageSel + "&ProductName=" + CurrentProductName + "&Series=" + CurrentProductSeries + "&RenderServer=" + RenderServer);
	deactivateMenu(currentActiveMenu);
}

function updateImageRoof(specifier, sel, ProductName, ProductSeries)
{
    CurrentProductName = ProductName;
    CurrentProductSeries = ProductSeries;
    
    updateImage(specifier, sel);
}

function showEnlarge(specifier, sel) {
    var windowHandle = window.open("EnlargePrint.aspx?Specifier=" + specifier + "&sel=" + sel + "&ProductName=" + CurrentProductName + "&Series=" + CurrentProductSeries, "gafenlargeprint", "width=750,height=560,menu=0,scrollbars=1");
    windowHandle.focus();
}

function GetXmlHttpObject(handler)
{ 
	var objXMLHttp = null;
	if(window.XMLHttpRequest)
	{
		objXMLHttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp;
}	
	
function AJAXReplace(elementid, url)
{
	var xmlHttp = GetXmlHttpObject();
	var baseElement = document.getElementById(elementid);
	if(xmlHttp == null || baseElement == null)
	{
		return;
	}
	baseElement.innerHTML = "Loading, please wait...";
	baseElement.xmlHttpObject = xmlHttp;
	eval("baseElement.xmlHttpFunction = function() { baseElement = document.getElementById(\"" + elementid + "\"); if(baseElement.xmlHttpObject.readyState==4 || baseElement.xmlHttpObject.readyState==\"complete\")	{ baseElement.innerHTML = baseElement.xmlHttpObject.responseText;	} }");
	xmlHttp.onreadystatechange = baseElement.xmlHttpFunction;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function updateSurfaces(passedSpecifier)
{
	surfaceNamesXMLObj = GetXmlHttpObject();
	if(surfaceNamesXMLObj == null)
	{
		return;
	}
	surfaceNamesXMLObj.onreadystatechange = function() 
	{ 
		if(surfaceNamesXMLObj.readyState==4 || surfaceNamesXMLObj.readyState=="complete")
		{
			surfaceNames = surfaceNamesXMLObj.responseText.split(",");
			var roofStyle = "none";
			var stoneStyle = "none";
			var brickStyle = "none";
			var trimStyle = "none";
			for(i=0;i<surfaceNames.length;i++)
			{
				surfaceNames[i] = surfaceNames[i].toLowerCase().replace("\r\n", "");
				if(surfaceNames[i] == "roof" || surfaceNames[i] == "roofing") { roofStyle = "inline"; }
				if(surfaceNames[i] == "stone") { stoneStyle = "inline"; }
				if(surfaceNames[i] == "brick" || surfaceNames[i] == "wall" || surfaceNames[i] == "wall body" || surfaceNames[i] == "siding") { brickStyle = "inline"; }
				if(surfaceNames[i] == "trim") { trimStyle = "inline"; }
			}
			document.getElementById("gafroofingli").style.display = roofStyle;
			document.getElementById("gafstoneli").style.display = stoneStyle;
			document.getElementById("gafbrickli").style.display = brickStyle;
			document.getElementById("gaftrimli").style.display = trimStyle;
			selReady = 1;
		}
	};
	surfaceNamesXMLObj.open("GET", "GetSurfaceList.aspx?Specifier=" + passedSpecifier, true);
	surfaceNamesXMLObj.send(null);
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function prepMenu(elementid)
{
	var menuElement = document.getElementById(elementid);
	if(menuElement)
	{
		menuElement.destinationY = 515;
		menuElement.currentY = 515;
		menuElement.speedY = 30;
	}
}

function changeSel(productid, whichSurface)
{
	var newSelString = "";
	var selArray = imageSel.split(",");
	if(selArray.length < whichSurface)
	{
		for(i=selArray.length;i<whichSurface;i++)
		{
			selArray[i] = "-1";
		}
	}
	for(i=0;i<selArray.length;i++)
	{
		if(i > 0)
		{
			newSelString = newSelString + ",";
		}
		if(i == (whichSurface - 1))
		{
			newSelString = newSelString + productid.toString();
		}	
		else
		{
			newSelString = newSelString + selArray[i];
		}
	}
	return newSelString;
}

function moveMenu(elementid)
{
	var menuElement = document.getElementById(elementid);
//	var bgElement = document.getElementById("transparentmenubg");
	if(menuElement)
	{
		if(menuElement.destinationY < (menuElement.currentY - menuElement.speedY))
		{
			menuElement.currentY -= menuElement.speedY;
			menuElement.style.top = menuElement.currentY + "px";		
//			bgElement.style.top = menuElement.currentY + "px";		
			setTimeout("moveMenu('" + elementid + "');", 50);
		}
		else if(menuElement.destinationY > (menuElement.currentY + menuElement.speedY))
		{
			menuElement.currentY += menuElement.speedY;
			menuElement.style.top = menuElement.currentY + "px";	
//			bgElement.style.top = menuElement.currentY + "px";		
			setTimeout("moveMenu('" + elementid + "');", 50);
		}
		else
		{
			menuElement.currentY = menuElement.destinationY;
			menuElement.style.top = menuElement.currentY + "px";		
//			bgElement.style.top = menuElement.currentY + "px";		
			if(currentActiveMenu == elementid && menuElement.destinationY > 0)
			{
				currentActiveMenu = null;
				unhighlightAllButtons();
			}
			if(nextActiveMenu)
			{
				activateMenu(nextActiveMenu);
				nextActiveMenu = null;
			}
		}
	}
}

function activateMenu(elementid)
{
	if(currentActiveMenu)
	{
		nextActiveMenu = elementid;
		deactivateMenu(currentActiveMenu);
	}
	else
	{
		var menuElement = document.getElementById(elementid);
		if(menuElement)
		{
			if(!(menuElement.currentY))
			{
				prepMenu(elementid);
			}
			menuElement.destinationY = 0;
			currentActiveMenu = elementid;
			setTimeout("moveMenu('" + elementid + "');", 50);
		}
	}
}

function deactivateMenu(elementid)
{
	var menuElement = document.getElementById(elementid);
	if(menuElement)
	{
		if(!menuElement.currentY)
		{
			prepMenu(menuElement);
		}
		menuElement.destinationY = 515;
		setTimeout("moveMenu('" + elementid + "');", 50);
	}
}

function showHomeSelect()
{
	if(currentActiveMenu == "newhouseselectscreen")
	{
		deactivateMenu("newhouseselectscreen")
	}
	else
	{
		AJAXReplace("newhouseselectscreeninner", "HomeSelect.aspx");
		activateMenu("newhouseselectscreen");
	}
}

function showRoofingSelect()
{
	if(currentActiveMenu == "roofingselectscreen")
	{
		deactivateMenu("roofingselectscreen");
	}
	else {
	    AJAXReplace("roofingselectscreeninner", "ProductSelect.aspx?ZipCode=" + zipcode);
        activateMenu("roofingselectscreen");
	}
}

function showProductSelect(whichProduct)
{
	if(currentActiveMenu == whichProduct + "selectscreen")
	{
		deactivateMenu(whichProduct + "selectscreen");
	}
	else
	{
		var whichSurface = 2;
		for(i=0;i<surfaceNames.length;i++)
		{
			if(whichProduct == "stone" && surfaceNames[i] == "stone")	{ whichSurface = i + 1; }
			if(whichProduct == "brick" && (surfaceNames[i] == "brick" || surfaceNames[i] == "wall" || surfaceNames[i] == "wall body" || surfaceNames[i] == "siding"))	{ whichSurface = i + 1; }
			if(whichProduct == "trim" && surfaceNames[i] == "trim")	{ whichSurface = i + 1; }
		}
		AJAXReplace(whichProduct + "selectscreeninner", "getpalette.aspx?Specifier=" + imageSpecifier + "&Surface=" + whichSurface);
		activateMenu(whichProduct + "selectscreen");
	}
}

function showProjectFolder(querystring)
{
	if(currentActiveMenu == "projectFolder")
	{
		deactivateMenu("projectFolder");
	}
	else
	{
		AJAXReplace("projectFolderInner", "userFolder.aspx?UserID=" + imUserID + "&email=" + email + "&password=" + password + "&rand=" + Math.random() + "&" + querystring);
		activateMenu("projectFolder");
	}
}

function saveRendering()
{
    AJAXReplace("projectFolderInner", "userFolder.aspx?UserID=" + imUserID + "&email=" + email + "&password=" + password + "&action=Save&Sel=" + imageSel + "&specifier=" + imageSpecifier + "&rand=" + Math.random());
    if(currentActiveMenu != "projectFolder")
	{
		activateMenu("projectFolder");
	}
}

function setUserID(id)
{
    imUserID = id;
}

function createNewAccount()
{
    var result = "";

	// Josh put these in on 10/28/08 because the way we were referencing the elements wasn't working in Firefox
    var txtNewEmail = document.getElementById("txtNewEmail");
    var txtNewEmailConfirm = document.getElementById("txtNewEmailConfirm");
    var txtNewPassword = document.getElementById("txtNewPassword");
    var txtNewPasswordConfirm = document.getElementById("txtNewPasswordConfirm");
    var txtFirstName = document.getElementById("txtFirstName");
    var txtLastName = document.getElementById("txtLastName");
    var txtAddress = document.getElementById("txtAddress");
    var txtCity = document.getElementById("txtCity");
    var txtState = document.getElementById("txtState");
    var txtCountry = document.getElementById("txtCountry");
    var txtZipCode = document.getElementById("txtZipCode");
    var hdnSpecifier = document.getElementById("hdnSpecifier");
    var hdnSel = document.getElementById("hdnSel");
    var hdnAction = document.getElementById("hdnAction");

	// Verify a few things first
    if(txtNewEmail.value == "" || txtNewEmailConfirm.value == "")
    {
        result += "Email and confirm Email cannot be empty.\n";
    }
    if(txtNewEmail.value != txtNewEmailConfirm.value)
    {
        result += "Email and confirm Email must match.\n";
    }
    if(txtNewPassword.value == "" || txtNewPasswordConfirm.value == "")
    {
        result += "Password and confirm Password cannot be empty.\n";
    }
    if(txtNewPassword .value != txtNewPasswordConfirm.value)
    {
        result += "Password and confirm Password must match.\n";
    }
    if(result != "")
    {
        alert("There were errors: \n" + result);
    }
    else
    {
        var qs = "newAccount.aspx?email=" + txtNewEmail.value;
        qs += "&password=" + txtNewPassword.value;
        qs += "&FirstName=" + txtFirstName.value;
        qs += "&LastName=" + txtLastName.value;
        qs += "&Address=" + txtAddress.value;
        qs += "&City=" + txtCity.value;
        qs += "&State=" + txtState.value;
        qs += "&Country=" + txtCountry.value;
        qs += "&Zip=" + txtZipCode.value;
        qs += "&Specifier=" + hdnSpecifier.value;
        qs += "&Action=" + hdnAction.value;
        qs += "&Sel=" + hdnSel.value;
        AJAXReplace("projectFolderInner", qs);
    }
}

function removeSaved(srid)
{
    if(confirm("Are you sure you want to remove this Image?"))
    {
        AJAXReplace("projectFolderInner", "userFolder.aspx?UserID=" + imUserID + "&email=" + email + "&password=" + password + "&action=Remove&srid=" + srid + "&Sel=" + imageSel + "&specifier=" + imageSpecifier);	
    }
}

function doLogin()
{
    email = document.getElementById("txtEmail").value;
    password = document.getElementById("txtPassword").value;
    AJAXReplace("projectFolderInner", "userFolder.aspx?UserID=" + imUserID + "&email=" + email + "&password=" + password + "&rand=" + Math.random());
}

function doLogout()
{
    email = null;
    password = null;
    imUserID = null;
    AJAXReplace("projectFolderInner", "Login.aspx?Logout=true");
}

function highlightButton(whichButton, Template)
{
    template = Template;
	var buttonElement = document.getElementById("gafbutton" + whichButton);
	if(buttonElement)
	{
		buttonElement.src = "templates/" + Template + "/sidebar_button_" + whichButton + "_on.gif";
	}
}

function unhighlightButton(whichButton, Template)
{
    template = Template;
	var buttonElement = document.getElementById("gafbutton" + whichButton);
	if(buttonElement && currentActiveMenu != whichButton + "selectscreen")
	{
		buttonElement.src = "templates/" + Template + "/sidebar_button_" + whichButton + ".gif";
	}
}

function unhighlightAllButtons()
{
	unhighlightButton("newhouse",template);
	unhighlightButton("roofing",template);
	unhighlightButton("stone",template);
	unhighlightButton("brick",template);
	unhighlightButton("siding",template);
	unhighlightButton("trim",template);
}

function showFeedbackForm()
{
	if(currentActiveMenu == "FeedbackScreen")
	{
		deactivateMenu("FeedbackScreen");
	}
	else
	{
		AJAXReplace("FeedbackScreenInner", "Feedback.aspx?Template=" + template);
		activateMenu("FeedbackScreen");
	}
}

function sendFeedback()
{
    AJAXReplace("FeedbackScreenInner", "SendFeedback.aspx?Email=" + document.getElementById("feedbackemail").value + "&Message=" + document.getElementById("feedback").value + "&Template=" + template + "&zip=" + zipcode);
}

function showImageMapper()
{
	if(currentActiveMenu == "ImageMapperScreen")
	{
		deactivateMenu("ImageMapperScreen");
	}
	else
	{
		AJAXReplace("ImageMapperScreenInner", "ImageMapper.aspx?zip=" + zipcode);
		activateMenu("ImageMapperScreen");
	}
}