var loadingImg = '<p class="close"><a href="#" onclick="hideBox(); return false;">close</a></p><div class="sendToFriend"><p class="loader"><img src="/media/build/loader.gif" width="32" height="32" alt="" /></p></div>';

function sendToFriend()
{
	bodyTag = document.getElementsByTagName("body")[0];		
	
	var sizeatt = getPageSize();
	var scrollatt = getPageScroll();
	var fullHeight = "";
	
	/* Create overlay */
	var overlay = document.createElement("div");
	overlay.id = "dataOverlay";
	overlay.style.display = "block";	
	
	if(navigator.appName.indexOf("Opera") != -1)	
		fullHeight = parseInt(document.getElementsByTagName("body")[0].offsetHeight);		
	else
		fullHeight = sizeatt[1];
		
	overlay.style.height = fullHeight + "px";							
	
	if(navigator.userAgent.indexOf("MSIE") != -1)	
		overlay.style.width = (sizeatt[0]) + "px";			
	else
		overlay.style.width = (sizeatt[0] - 17) + "px";		
	
	bodyTag.appendChild(overlay);	
	
	var overlayBox = document.createElement("div");
	overlayBox.id = "dataOverlayBox";
	overlayBox.style.display = "block";
	overlayBox.style.top = scrollatt[1] + "px";
	bodyTag.appendChild(overlayBox);	
	
	var overlayContent = document.createElement("div");
	overlayContent.id = "overlayContent";
	overlayBox.appendChild(overlayContent);
	overlayContent.style.background = "none";	
	
	/* Create popup content */		
	var content = document.createElement("span");							
	content.id = "formResult";	
	content.style.display = "block";			
	overlayContent.appendChild(content);
	popupContent();
}
function hideBox()
{				
	overlayObj = document.getElementById("dataOverlay");	
	document.body.removeChild(overlayObj);
	overlayContentBox = document.getElementById("dataOverlayBox");
	document.body.removeChild(overlayContentBox);
}
function GetXmlHttpObject()
{
	var xmlHttp = null;	
	try
	{
		xmlHttp = new XMLHttpRequest();
	}
	catch (e)
	{
		try
		{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}
function popupContent()
{	
	var isValid = true;
	var call = "new";
	
	if(document.getElementById("friendsname") != null)
	{
		call = "submit";
		var friendsFirstName = document.getElementById("friendsname");
		var friendsEmailAddress = document.getElementById("friendsemail");
		var firstName = document.getElementById("firstname");
		var emailAddress = document.getElementById("email");
		var message = document.getElementById("message");
		
		if(friendsFirstName.value == "")
		{
			isValid = false;
			friendsFirstName.className += " required";
		}
		else
		{
			friendsFirstName.className = "text";
		}
		
		if(friendsEmailAddress.value == "")
		{
			isValid = false;
			friendsEmailAddress.className += " required";
		}
		else
		{
			if(friendsEmailAddress.value != "")
			{
				var supEmail = friendsEmailAddress.value;
				var emLen = supEmail.length;
				var posAt = supEmail.indexOf('@')
				var posDot = supEmail.lastIndexOf('.')
				if((emLen < 7) || (posAt < 1) || (posDot <= (posAt + 1)) || (posDot >= (emLen - 1)))
				{
					isValid = false;		
					friendsEmailAddress.className += " required";			
				}	
				else
				{			
					friendsEmailAddress.className = "text";
				}		
			}
		}
			
		if(firstName.value == "")
		{
			isValid = false;
			firstName.className += " required";
		}
		else
		{
			firstName.className = "text";
		}

		if(emailAddress.value == "")
		{
			isValid = false;
			emailAddress.className += " required";
		}
		else
		{
			if(emailAddress.value != "")
			{
				var supEmail = emailAddress.value;
				var emLen = supEmail.length;
				var posAt = supEmail.indexOf('@')
				var posDot = supEmail.lastIndexOf('.')
				if((emLen < 7) || (posAt < 1) || (posDot <= (posAt + 1)) || (posDot >= (emLen - 1)))
				{
					isValid = false;
					emailAddress.className += " required";			
				}	
				else
				{
					emailAddress.className = "text";
				}
			}	
		}
	}
	
	if(isValid == true)
	{		
		xmlHttp = GetXmlHttpObject();				
		var url = "/send-to-a-friend/popup.aspx?url=" + document.location + "&pageTitle=" + document.title;	
		url = url + "&call=" + call;
		
		if(document.getElementById("friendsname") != null)
		{			
			url = url + "&friendsname=" + friendsFirstName.value;	
			url = url + "&friendsemail=" + friendsEmailAddress.value;
			url = url + "&firstname=" + firstName.value;
			url = url + "&email=" + emailAddress.value;
			url = url + "&message=" + message.value;
		}
		
		xmlHttp.onreadystatechange = function()
		{
			if (xmlHttp.readyState == 4)
				document.getElementById("formResult").innerHTML = xmlHttp.responseText;
			else
				document.getElementById("formResult").innerHTML = loadingImg;	
		}
		xmlHttp.open("POST",url,true);	
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", url.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(url);		
	}	
}
function getPageScroll()
{
	var yScroll;

	if (self.pageYOffset) 
	{
		yScroll = self.pageYOffset;
	} 
	else if (document.documentElement && document.documentElement.scrollTop)
	{
		yScroll = document.documentElement.scrollTop;
	} 
	else if (document.body) 
	{
		yScroll = document.body.scrollTop;
	}
	
	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}
function getPageSize()
{	
	var xScroll, yScroll;	
	if (window.innerHeight && window.scrollMaxY) 
	{	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} 
	else if (document.body.scrollHeight > document.body.offsetHeight)
	{
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} 
	else 
	{ 
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}		
	var windowWidth, windowHeight;
	if (self.innerHeight) 
	{
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} 
	else if (document.documentElement && document.documentElement.clientHeight) 
	{
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} 
	else if (document.body) 
	{
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	if(yScroll < windowHeight)
	{
		pageHeight = windowHeight;
	} 
	else 
	{ 
		pageHeight = yScroll;
	}
	if(xScroll < windowWidth)
	{	
		pageWidth = windowWidth;
	} 
	else 
	{
		pageWidth = xScroll;
	}	
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
