var isIE = document.all ? true:false;

function openPopUp(url, width, height, options)
{
	if (!options || options == '')
	{
		options = "location=no,status=no,menubar=no,scrollbars=no,resizable=no,toolbar=no";
	}
	var left = (screen.width - width) / 2;
	var top = (screen.height - height) / 2;
	window.open(url, "_blank", "left=" + left + ",top=" + top + ",height=" + height + ",width=" + width + "," + options);
}

function openWindow (url)
{
	window.open(url, "_blank");
}

function CheckEmailFormat(stringToCheck)
{
	var arr = stringToCheck.match(/[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)*@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+/);
	return (arr != null && arr[0] == stringToCheck);
}

function CheckEmailFormatFromValidator(source, arguments)
{
	arguments.IsValid = CheckEmailFormat(arguments.Value);
}


function closePopUpAndOpenUrl(url)
{
	opener.parent.location.href=url;
	window.close();
}

function openPrintWindow(objectType, printWrapperId, width, height)
{

	var srcContainer = document.getElementById(printWrapperId);

	if(srcContainer)
	{
		if(!width) width = srcContainer.offsetWidth + 30;
		if(width > screen.width) w = screen.width;
		if(!height) height = srcContainer.offsetHeight + 75;
		if(height > screen.height) h = screen.height;
		if(height < 500) h = 500;
		openPopUp("/PrintContent.aspx?objtype=" + objectType + "&container=" + printWrapperId, width, height, "scrollbars=1");
	}
}

function loadPrintContent(container)
{
	var srcContainer = window.opener.document.getElementById(container);
	
	if(srcContainer)
	{
		var destContainer = document.getElementById("printContent");
		if(destContainer)
		{
			destContainer.innerHTML = srcContainer.innerHTML;
			var w = srcContainer.offsetWidth + 30;
			if(w > screen.width) w = screen.width;
			var h = srcContainer.offsetHeight + 75;
			if(h < 500) h = 500;
			if(h > screen.height) h = screen.height;
			var l = (screen.width - w) / 2;
			var t = (screen.height - h) / 2;
			window.moveTo(l, t);
			window.resizeTo(w, h);
		}
	}
}

/*
	Special offers that belong to multiple objects render selection div 
	that needs to be shown/closed when clicked on Details button.
*/
function ToggleDetailsDiv(divContinueId)
{
	var divContinue = document.getElementById(divContinueId);
	if(divContinue)
	{
		if(divContinue.style.visibility == 'visible')
		{
			divContinue.style.visibility = 'hidden';
		}
		else if(divContinue.style.visibility == 'hidden')
		{
			divContinue.style.visibility = 'visible';
		}
	}
}

/*
	Function for maintaining scrollbar position, when, for example, 
	clickiing special offer type menu or buttons in subtype list.
*/
function ScrollWindowToAnchor(anchorId)
{
	var anchor = document.getElementById(anchorId);
	
	if(anchor.scrollIntoView)
	{
		anchor.scrollIntoView(isIE);
	}
}

/*
	Functions for special offer photo gallery.
*/

function displayCurrentSpecialOfferImage()
{
	setSpecialOfferImageUrl(0);
}

function displayNextSpecialOfferImage()
{
	if (galleryCurrentIndex == galleryMaxIndex)
	{
		setSpecialOfferImageUrl(0);
	}
	else
	{
		setSpecialOfferImageUrl(galleryCurrentIndex + 1);
	}
}

function displayPreviousSpecialOfferImage()
{
	if (galleryCurrentIndex == 0)
	{
		setSpecialOfferImageUrl(galleryMaxIndex);
	}
	else
	{
		setSpecialOfferImageUrl(galleryCurrentIndex - 1);
	}
}

function setSpecialOfferImageUrl(index)
{
	var spImageTitle = document.getElementById('spImageTitle');
	if (spImageTitle)
	{
		spImageTitle.innerHTML = eval('galleryImage' + index + '.alt');
	}
	
	var imgCurrentImage = document.getElementById('imgCurrentImage');
	if (imgCurrentImage)
	{
		imgCurrentImage.src = eval('galleryImage' + index + '.src');
		imgCurrentImage.alt = eval('galleryImage' + index + '.alt');
		imgCurrentImage.title = eval('galleryImage' + index + '.title');
	}
	
	var spCurrentPageNumber = document.getElementById('spCurrentPageNumber');
	var spLastPageNumber = document.getElementById('spLastPageNumber');

	if (spCurrentPageNumber && spLastPageNumber)
	{
		spCurrentPageNumber.innerHTML = index + 1;
		spLastPageNumber.innerHTML = galleryMaxIndex + 1;
	}

	galleryCurrentIndex = index;
	
	var spPrevious = document.getElementById('spPrevious');
	var spNext = document.getElementById('spNext');
	
	if (spPrevious && spNext)
	{
		spPrevious.style.visibility = (index == 0) ? 'hidden' : 'visible';
		spNext.style.visibility = (index == galleryMaxIndex) ? 'hidden' : 'visible'
	}
}
