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 = "Chameleon";
var SeriesName = "Snap-Clad Panel 18";
var currentSurface = 1;
var Color = "";


function changeSurface(newSurfaceNum) {
    currentSurface = newSurfaceNum;
    if (currentSurface == 1) {
        document.getElementById("Roofsurface").style.position = "static";
        document.getElementById("Roofsurface").style.visibility = "visible";
        document.getElementById("Wallsurface").style.position = "absolute";
        document.getElementById("Wallsurface").style.visibility = "hidden";
        document.getElementById("colorspace").style.position = "static";
        document.getElementById("colorspace").style.visibility = "visible";
        document.getElementById("trimspace").style.position = "absolute";
        document.getElementById("trimspace").style.visibility = "hidden";
    }
    if (currentSurface == 2) {
        document.getElementById("Roofsurface").style.position = "absolute";
        document.getElementById("Roofsurface").style.visibility = "hidden";
        document.getElementById("Wallsurface").style.position = "static";
        document.getElementById("Wallsurface").style.visibility = "visible";
        document.getElementById("colorspace").style.position = "static";
        document.getElementById("colorspace").style.visibility = "visible";
        document.getElementById("trimspace").style.position = "absolute";
        document.getElementById("trimspace").style.visibility = "hidden";
    }
    if (currentSurface == 3) {
        document.getElementById("Roofsurface").style.position = "absolute";
        document.getElementById("Roofsurface").style.visibility = "hidden";
        document.getElementById("Wallsurface").style.position = "absolute";
        document.getElementById("Wallsurface").style.visibility = "hidden";
        document.getElementById("colorspace").style.position = "absolute";
        document.getElementById("colorspace").style.visibility = "hidden";
        document.getElementById("trimspace").style.position = "static";
        document.getElementById("trimspace").style.visibility = "visible";
    }
}

//////////////////

function applyColor(color)
{
    Color = color;
    if (parseInt(color) > 0) {
        imageSel = changeSel(color, currentSurface);
    } else {
        imageSel = changeSel("-PLACEHOLDER-", currentSurface);
    }    
    AJAXReplace("renderedimagescreen", "showimage.cfm?specifier=" + imageSpecifier + "&series=" + SeriesName + "&color=" + color + "&surface=" + currentSurface + "&sel=" + imageSel);
}

//////////////////

function changeSeries(Series) 
{
    SeriesName = Series;
    document.getElementById("Snap-Clad Panel 18").className = "";
    document.getElementById("Tite-Loc Panel 12").className = "";
    document.getElementById("Tite-Loc Panel 18").className = "";
    document.getElementById("Redi-Roof Standing Seam 12").className = "";
    document.getElementById("Redi-Roof Standing Seam 18").className = "";
    document.getElementById("Snap-On Standing Seam 12").className = "";
    document.getElementById("Snap-On Standing Seam 18").className = "";
    document.getElementById("Integral Standing Seam 11").className = "";
    document.getElementById("Integral Standing Seam 18").className = "";
    document.getElementById("0.5 in Horizontal Corrugated").className = "";
    document.getElementById("0.5 in Vertical Corrugated").className = "";
    document.getElementById("7.2 in Horizontal Panel").className = "";
    document.getElementById("7.2 in Vertical Panel").className = "";
    document.getElementById("0.875 in Vertical Corrugated").className = "";
    document.getElementById("0.875 in Horizontal Corrugated").className = "";
    document.getElementById(Series).className = "";
    
    
}      

//////////////////

function updateImage(specifier, sel)
{
    
	imageSpecifier = specifier;
	AJAXReplace("renderedimagescreen", "showimage.cfm?specifier=" + imageSpecifier + "&series=" + SeriesName + "&color=-1,-1,-1" + "&surface=1" + "&sel=-1,-1,-1");
}

//////////////////

function flipChamImageLoad(filename)
{
	document.getElementById('load' + filename).style.display = 'none'; document.getElementById('image' + filename).style.display = 'inline';
}

//////////////////

function showEnlarge(specifier) {
    window.open("enlargeprint.cfm?specifier=" + specifier + "&series=" + SeriesName + "&color=" + Color + "&surface=" + currentSurface + "&sel=" + imageSel, "gafenlargeprint", "width=1070,height=600,menu=0,scrollbars=1");
}

/////////////////

function showEnlargeAndPrint(specifier, sel)
{
	var windowHandle = window.open("enlarge.cfm?specifier=" + specifier + "&sel=" + sel, "enlargeandprintwindow", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=790,height=550,top=5,left=5");
	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", "");
				//alert(surfaceNames[i]); ///////////// DEBUG
				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", "getsurfaces.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 showProjectFolder(querystring)
{
	if(currentActiveMenu == "projectFolder")
	{
		deactivateMenu("projectFolder");
	}
	else
	{
		AJAXReplace("projectFolderInner", "userFolder.aspx?gafUserID=" + imUserID + "&email=" + email + "&password=" + password + "&rand=" + Math.random() + "&" + querystring);
		activateMenu("projectFolder");
	}
}

//////////////////

function saveRendering()
{
    //alert('sel: ' + imageSel);
    //alert('email: ' + email);
    AJAXReplace("projectFolderInner", "userFolder.aspx?gafUserID=" + imUserID + "&email=" + email + "&password=" + password + "&action=Save&Sel=" + imageSel + "&specifier=" + imageSpecifier + "&rand=" + Math.random());	
    //alert("userFolder.aspx?gafUserID=" + imUserID + "&email=" + email + "&password=" + password + "&action=Save&Sel=" + imageSel + "&specifier=" + imageSpecifier + "&rand=" + Math.random());
    if(currentActiveMenu != "projectFolder")
	{
		activateMenu("projectFolder");
	}
}

//////////////////

function setUserID(id)
{
    imUserID = id;
    //alert(id);
}

//////////////////

function createNewAccount()
{
  var result = "";

	// Josh put this 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");

	// 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;
        //alert(qs);
        AJAXReplace("projectFolderInner", qs);
    }
}

//////////////////

function removeSaved(srid)
{
    if(confirm("Are you sure you want to remove this Image?"))
    {
        AJAXReplace("projectFolderInner", "userFolder.aspx?gafUserID=" + 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.cfm");
}

//////////////////

function showFeedbackForm()
{
	window.open("feedbackform.cfm","gaffeedback","width=400,height=450,menu=0,scrollbars=1");
}

//////////////////

function showImageMapper()
{
	if(currentActiveMenu == "ImageMapperScreen")
	{
		deactivateMenu("ImageMapperScreen");
	}
	else
	{
		AJAXReplace("ImageMapperScreenInner", "ImageMapper.aspx?zip=" + zipcode);
		activateMenu("ImageMapperScreen");
	}
}

//////////////////

function setupPage()
{
}

//////////////////

window.onload = setupPage;