﻿var WindowOnLoadFuncs = new Array();
window.onload = function ()
{
    for(var i = 0; i < WindowOnLoadFuncs.length; i++)
    {
        eval(WindowOnLoadFuncs[i]);
    }
}
function AddWindowOnLoadFunc(func)
{
    WindowOnLoadFuncs[WindowOnLoadFuncs.length] = func;
}
function ClearElement(elem)
{
    if (elem)
    {
        while(elem.hasChildNodes())
        { 
            elem.removeChild(elem.firstChild);
        }
    }
}

/*****************************/
/**     Ajax callbacks      **/
/*****************************/
function ForwardToAFriendCallback(result)
{
	unblockViewport();
	if (!result.value)
	{
		document.getElementById("ForwardResultLabel").innerHTML = "An error occured on server";
	}
	else
	{
		if (result.value.ResultType == 0)
		{
			ForwardThankYouShow();
		}
		else
		{
			document.getElementById("ForwardResultLabel").innerHTML = "Error: " + result.value.Message;
		}
	}
}
/*****************************/
/**  Ajax calls validation  **/
/*****************************/
function ForwardToAFriendValidate()
{
	var result = "";
	var lineSeparator = "";
	if (
		trim(document.getElementById("YourFirstNameBox").value).length == 0 ||
		trim(document.getElementById("YourLastNameBox").value).length == 0 ||
		trim(document.getElementById("YourMailBox").value).length == 0 ||
		trim(document.getElementById("FriendsNameBox").value).length == 0 ||
		trim(document.getElementById("FriendsMailBox").value).length == 0)
	{
		result = "You forgot to fill out a field. Please fill it out. And don't make us tell you again.";
	}
	else
	{
		if (
			document.getElementById("YourMailBox").value.length != 0 &&
			!document.getElementById("YourMailBox").value.match(/^\s*([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+\s*$/))
		{
			result += lineSeparator + "Your Email is invalid";
			lineSeparator = "<br>";
		}
		if (
			document.getElementById("FriendsMailBox").value.length != 0 &&
			!document.getElementById("FriendsMailBox").value.match(/^\s*([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+\s*$/))
		{
			result += lineSeparator + "Your Friend's Email is invalid";
			lineSeparator = "<br>";
		}
	}
	return result;
}
function ForwardToAFriend()
{
	var validationMessage = ForwardToAFriendValidate();
	if (validationMessage.length != 0)
	{
		document.getElementById("ForwardResultLabel").innerHTML = validationMessage;
	}
	else
	{
		blockViewport(document.getElementById("popup_forward"), ["ForwardSubmitButton"]);
		HotPockets.Webs.AjaxAccessor.ForwardToAFriend(
			document.getElementById("YourFirstNameBox").value,
			document.getElementById("YourLastNameBox").value,
			document.getElementById("YourMailBox").value,
			document.getElementById("FriendsNameBox").value,
			document.getElementById("FriendsMailBox").value,
			location.href,
			document.getElementById("WouldLikeEmailsBox").checked,
			ForwardToAFriendCallback);
	}
	return false;
}
/*****************************/
/**       Busy wheel        **/
/*****************************/
var xval;
var disabledControls;
function blockViewport(element, controlsToDisable)
{
	disabledControls = controlsToDisable;
	if (disabledControls)
	{
		for (var i = 0; i < disabledControls.length; i++)
		{
			document.getElementById(disabledControls[i]).disabled = true;
		}
	}
	xval = getBusyOverlay(
		element,
		{color:'transparent', opacity:0.75, style:'text-decoration:blink;font-weight:bold;font-size:12px;color:#000000'},
		{color:'#66aa66', size:32, type:'o'});
}
function unblockViewport()
{
	if (xval)
	{
		xval.remove();
	}
	if (disabledControls)
	{
		for (var i = 0; i < disabledControls.length; i++)
		{
			document.getElementById(disabledControls[i]).disabled = false;
		}
	}
}

/*****************************/
/**     Popup Routines      **/
/*****************************/

function ForwardFormShow()
{
	ShowPopupForm("popup_forward");
}
function ForwardThankYouShow()
{
	ShowPopupThankYou("popup_forward");
}
function ForwardClose()
{
	ClosePopupForm("popup_forward");
	document.getElementById("ForwardResultLabel").innerHTML = "";
}
function ShowPopupForm(form)
{
	document.getElementById(form + "_thankyou").style.display = "none";
	document.getElementById(form + "_form").style.display = "";
	_popup_show(form, 127, -78);
}
function ShowPopupThankYou(form)
{
	document.getElementById(form + "_form").style.display = "none";
	document.getElementById(form + "_thankyou").style.display = "";
	_popup_show(form, 127, -78);
}
function ClosePopupForm(form)
{
	_popup_close(form);
}

/*****************************/
/**          Other          **/
/*****************************/

function trim(str)
{
	return str.replace(/^\s*/, "").replace(/\s*$/, "");
}

var isAdultUser = null;
function DoAdultAction(action, rollbackAction)
{
	if (!action || isAdultUser)
	{
		eval(action);
	}
	else
	{
		returnRollbackAction = rollbackAction;
		if (isAdultUser != null)
		{
			ShowPopupThankYou('age_screener');
		}
		else
		{
			returnAction = action;
			ShowPopupForm('age_screener');
			document.getElementById('AgeScreenerResultLabel').innerHTML = "";
		}
	}
}

/*****************************/
/** Adult Active Menu Items **/
/*****************************/
function ApplyStylesToHotPocketsMenu()
{
    var documentTDs = document.getElementsByTagName("tr");
    if (documentTDs)
    {
        for (j=0; j<documentTDs.length; j++)
        {
            if (documentTDs[j].id.indexOf("HotPocketsMainMenun0") != -1)
            { // Remove the top margin in very first menu item
                var tableElems = documentTDs[j].getElementsByTagName("table");
                if (tableElems)
                {
                    tableElems[0].style.marginTop = "0px";
                }
            }
            if (documentTDs[j].id.indexOf("HotPocketsMainMenun") != -1
                && documentTDs[j].title == "all products")
            { // Set the top margin in very first sub menu item
                var tableElems = documentTDs[j].getElementsByTagName("table");
                if (tableElems)
                {
                    tableElems[0].style.marginTop = "8px";
                }
            }
            if (documentTDs[j].id.indexOf("HotPocketsMainMenun") != -1)
            {
                var aElems = documentTDs[j].getElementsByTagName("a");
                if (aElems)
                {
                    var span = document.createElement("span");
                    if (aElems[0].innerHTML.indexOf("media") != -1)
                    {
                        span.innerHTML = "downloads<br/>& media";
                        span.style.paddingTop = "5px";
                        span.style.display = "block";
                    }
                    else if (aElems[0].innerHTML.indexOf("store") != -1)
                    {
                        span.innerHTML = "shop<br/>our store";
                        span.style.paddingTop = "5px";
                        span.style.display = "block";
                    }
                    else if (aElems[0].innerHTML.indexOf("buy") != -1)
                    {
                        span.innerHTML = "where<br/>to buy";
                        span.style.paddingTop = "5px";
                        span.style.display = "block";
                    }
                    else
                    {
                        span.innerHTML = aElems[0].innerHTML;
                        if (aElems[0].innerHTML.indexOf("all products") != -1
                            || aElems[0].innerHTML.indexOf("product news") != -1
                            || aElems[0].innerHTML.indexOf("faqs") != -1)
                        {
                            //span.style.lineHeight = "12px";
                        }
                        else
                        {
                            span.style.lineHeight = "33px";
                        }
                    }
                    aElems[0].innerHTML = "";
                    aElems[0].appendChild(span);
                }
            }
        }
    }
}
