﻿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 SubmitLawCallback(result)
{
	unblockViewport();
	if (!result.value)
	{
		document.getElementById("LawResultLabel").innerHTML = "An error occured on server";
	}
	else
	{
		if (result.value.ResultType == 0)
		{
			LawThankYouShow();
		}
		else
		{
			document.getElementById("LawResultLabel").innerHTML = "Error: " + result.value.Message;
		}
	}
}
/*****************************/
/**  Ajax calls validation  **/
/*****************************/
function SubmitLawValidate()
{
	var result = "";
	if (
		trim(document.getElementById("LawContentBox").value).length == 0 ||
		trim(document.getElementById("LawFirstNameBox").value).length == 0 ||
		trim(document.getElementById("LawLastInitialBox").value).length == 0 ||
		trim(document.getElementById("LawStateBox").value).length == 0)
	{
		result = "Hey. You missed something. Please go back and fill it out. Thanks.";
	}
	return result;
}
/*****************************/
/**        Ajax calls       **/
/*****************************/
function SubmitLaw()
{
	var validationMessage = SubmitLawValidate();
	if (validationMessage.length != 0)
	{
		document.getElementById("LawResultLabel").innerHTML = validationMessage;
	}
	else
	{
		blockViewport(document.getElementById("popup_law"), ["LawSubmitButton"]);
		EatFreely.Webs.AjaxAccessor.SubmitLaw(
			document.getElementById("LawContentBox").value,
			document.getElementById("LawWhereHeardBox").value,
			document.getElementById("LawFirstNameBox").value,
			document.getElementById("LawLastInitialBox").value,
			document.getElementById("LawStateBox").value,
			SubmitLawCallback);
	}
	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 FreediaFormShow()
{
	ShowPopupForm("popup_freedia");
	document.getElementById("freediaUploadIframe").src = "FileUpload.aspx";
	return false;
}
function LawFormShow()
{
	ShowPopupForm("popup_law");
	return false;
}
function FreediaThankYouShow()
{
	ShowPopupThankYou("popup_freedia");
}
function LawThankYouShow()
{
	ShowPopupThankYou("popup_law");
}
function LawClose()
{
	ClosePopupForm("popup_law");
	document.getElementById("LawResultLabel").innerHTML = "";
	document.getElementById("LawContentBox").value = "";
	document.getElementById("LawWhereHeardBox").value = "";
	document.getElementById("LawFirstNameBox").value = "";
	document.getElementById("LawLastInitialBox").value = "";
	document.getElementById("LawStateBox").value = "";
	$("#popup").hide();
	$("#layout").hide();
}
function ShowPopupFreedia(){
	document.getElementById("FreediaUploadIframe").src="FileUpload.aspx";
	$("#PopupFreedia").show().css({
		'left': ($(window).width()/2)-($("#PopupFreedia").width()/2),
		'top': ($(window).height()/2+$(window).scrollTop())-($("#PopupFreedia").height()/2)
	})
	var height_container = document.getElementById("container") ? document.getElementById("container").offsetHeight : 0;
	var height_win =
		typeof(window["scrollMaxY"]) != "undefined"	?
		innerHeight + scrollMaxY :
			(document.body.parentNode.scrollHeight > document.body.scrollHeight ?
			document.body.parentNode.scrollHeight :
			document.body.scrollHeight);
	if (document.body.parentNode.clientHeight > height_win)
	{
		height_win = document.body.parentNode.clientHeight;
	}
    var height = (height_container > height_win)? height_container: height_win;
	var width  = document.documentElement.clientWidth;
	$("#popup_overlay").show().height(height).width(width);
}
function ClosePopupFreedia() {
	$("#PopupFreedia").hide(); 
	$("#popup_overlay").hide();
	document.getElementById("FreediaUploadIframe").src="";
}

function ShowPopupForm(form)
{
	document.getElementById(form + "_thankyou").style.display = "none";
	document.getElementById(form + "_form").style.display = "";
	$("#popup_law").show().css(
		{
			'left': ($(window).width()/2)-($("#popup_law").width()/2),
			'top': ($(window).height()/2+$(window).scrollTop())-($("#popup_law").height()/2)
		});
	var height_container = document.getElementById("container") ? document.getElementById("container").offsetHeight : 0;
	var height_win =
		typeof(window["scrollMaxY"]) != "undefined"	?
		innerHeight + scrollMaxY :
			(document.body.parentNode.scrollHeight > document.body.scrollHeight ?
			document.body.parentNode.scrollHeight :
			document.body.scrollHeight);
	if (document.body.parentNode.clientHeight > height_win)
	{
		height_win = document.body.parentNode.clientHeight;
	}
    var height = (height_container > height_win)? height_container: height_win;
	var width  = document.documentElement.clientWidth;
	$("#layout").show().height(height).width(width);
}
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("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);
                }
            }
        }
    }
}
