var ModalWindows = {};

ModalWindows.darkBackgroundScreen;

/**
* Darken the background behind the Modal Window.
*/
ModalWindows.blurBackground = function()
{
	if (!ModalWindows.darkBackgroundScreen)
	{
		ModalWindows.darkBackgroundScreen = document.body.appendChild(document.createElement("div"));
		ModalWindows.darkBackgroundScreen.id = "darkBackgroundScreen";
	}
	ModalWindows.darkBackgroundScreen.style.width = document.documentElement.scrollWidth + "px";
	ModalWindows.darkBackgroundScreen.style.height = ModalWindows.getDocumentHeight() + "px";
	ModalWindows.darkBackgroundScreen.style.display = "block";
}

/**
* Create a blank Modal Window.
*
* @param int width Size in pixels.
* @param int height Size in pixels.
* @return DOMElement
*/
ModalWindows.createModalWindow = function(width, height)
{
	var modalWindow = document.createElement("div");
	modalWindow.className = "modalwindow";
	modalWindow.style.width = width + "px";
	modalWindow.style.height = height + "px";
	modalWindow.style.top = ModalWindows.getClientHeight() / 2 - height / 2 + "px";
	modalWindow.style.left = ModalWindows.getClientWidth() / 2 - width / 2 + "px";
	
	var closeButton = modalWindow.appendChild(document.createElement("a"));
	closeButton.className = "closebutton";
	closeButton.appendChild(document.createTextNode("X"));
	closeButton.href = "javascript:";
	closeButton.onclick = function() { ModalWindows.focusBackground(); modalWindow.style.display = "none"; return false; };
	
	var content = modalWindow.content = modalWindow.appendChild(document.createElement("div"));
	content.className = "contents";
	
	return modalWindow;
}

/**
* Restore the background behind the Modal Window.
*/
ModalWindows.focusBackground = function()
{
	if (ModalWindows.darkBackgroundScreen)
	{
		ModalWindows.darkBackgroundScreen.style.display = "none";
	}
}

/**
* Return the height of the browser's viewport.
*
* @return int
*/
ModalWindows.getClientHeight = function()
{
	if (window.innerHeight)
	{
		return window.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	{
		return document.documentElement.clientHeight;
	}
	else
	{
		return document.body.clientHeight;
	}
}

/**
* Return the width of the browser's viewport.
*
* @return int
*/
ModalWindows.getClientWidth = function()
{
	if (window.innerWidth)
	{
		return window.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth)
	{
		return document.documentElement.clientWidth;
	}
	else
	{
		return document.body.clientWidth;
	}
}

/**
* Return the full height of the web page.
*
* @return int
*/
ModalWindows.getDocumentHeight = function()
{
	return Math.max(Math.max(document.body.scrollHeight, document.documentElement.scrollHeight),
		Math.max(document.body.offsetHeight, document.documentElement.offsetHeight),
		Math.max(document.body.clientHeight, document.documentElement.clientHeight));
}

/**
* Display the Request Live Demo Window.
*/
ModalWindows.launchRequestLiveDemo = function(form, error)
{
	/**
	* Helper function to get default form values.
	*/
	function value(name)
	{
		return form && form[name] ? form[name] : "";
	}
	
	function notices()
	{
		return error ? '<div class="error">'+error+'</div>' : "";
	}
	
	ModalWindows.blurBackground();
	if (!ModalWindows.requestLiveDemo)
	{
		ModalWindows.requestLiveDemo = document.body.appendChild(ModalWindows.createModalWindow(768, 360));
		ModalWindows.requestLiveDemo.content.innerHTML = '\
			<div class="request_live_demo contents">\
				<h1>Request a Demo</h1>\
				<p>If you would like to request a demo with a member of our sales team, please submit the form below and we will contact you shortly. Thank you.</p>\
				<form action="" method="post">\
					'+notices()+'\
					<p><span class="required">*</span> Indicates a required field.</p>\
					<ul>\
						<li>\
							<label for="employer_name"><span class="required">*</span>Company Name</label>\
							<input type="text" class="text" name="employer_name" id="employer_name" value="'+value('employer_name')+'" />\
						</li>\
						<li>\
							<label for="name"><span class="required">*</span>Contact Person</label>\
							<input type="text" class="text" name="name" id="name" value="'+value('name')+'" />\
						</li>\
						<li>\
							<label for="email"><span class="required">*</span>E-mail</label>\
							<input type="email" class="text" name="email" id="email" value="'+value('email')+'" />\
						</li>\
						<li>\
							<label for="phone"><span class="required">*</span>Telephone</label>\
							<input type="tel" class="text" name="phone" id="phone" value="'+value('phone')+'" />\
						</li>\
						<li>\
							<label for="zipcode"><span class="required">*</span>Zipcode</label>\
							<input type="text" class="text" name="zipcode" id="zipcode" value="'+value('zipcode')+'" />\
						</li>\
					</ul>\
					<div class="submit">\
						<input type="hidden" name="request_demo" value="1" />\
						<button class="green_square_button" type="submit">\
							<span class="green_button_center">Submit</span>\
						</button>\
					</div>\
				</form>\
			</div>';
	}
	ModalWindows.requestLiveDemo.style.display = "block";
	document.getElementById("employer_name").focus();
}

/**
* Display the Login Window.
*/
ModalWindows.launchGotoLogin = function(form, error)
{
	/**
	* Helper function to get default form values.
	*/
	function value(name)
	{
		return form && form[name] ? form[name] : "";
	}
	
	function notices()
	{
		return error ? '<div class="error">'+error+'</div>' : "";
	}
	
	ModalWindows.blurBackground();
	if (!ModalWindows.gotoLogin)
	{
		ModalWindows.gotoLogin = document.body.appendChild(ModalWindows.createModalWindow(360, 220));
		ModalWindows.gotoLogin.content.innerHTML = '\
			<div class="goto_login contents">\
				<h1>Login</h1>\
				<form action="login.php" method="post">\
					'+notices()+'\
					<ul>\
						<li>\
							<label for="email">E-mail Address</label>\
							<input type="email" class="text" name="login" id="email" value="'+value('email')+'" />\
						</li>\
						<li>\
							<label for="password">Password</label>\
							<input type="password" class="text" name="password" id="password" value="'+value('password')+'" />\
						</li>\
					</ul>\
					<p class="forgot_password">\
						<a href="forgot_password.php">Forgot Password?</a>\
					</p>\
					<div class="submit">\
						<button class="green_square_button" type="submit">\
							<span class="green_button_center">Submit</span>\
						</button>\
					</div>\
				</form>\
			</div>';
	}
	ModalWindows.gotoLogin.style.display = "block";
	document.getElementById("email").focus();
}