﻿IdPrefix = "ctl00_Content_";

function OnLoad()
{
    DisplayStatus();
    ConfigureValidation();
    LoadHoverNavigation();
}

/*** BEGIN WINDOW MANAGEMENT ***/

function OpenDealImage(src, title)
{
	OpenImage(src, title, 850, 480);
}

function OpenVariantImage(src, title)
{
	OpenImage(src, title, 850, 480);
}

function OpenImage(src, title, width, height)
{
	try
	{
		if (Defined(typeof (ImageWindow)))
			ImageWindow.close();
	}
	catch (e) { }

	ImageWindow = window.open("", "DetailWindow", "location=no,scrollbars=yes,left=150,top=100,width=" + width + ",height=" + height);

	ImageWindow.document.write("<html><head>");
	ImageWindow.document.write("<title>" + title + "</title>");
	ImageWindow.document.write("<style>");
	ImageWindow.document.write(".Variants				{font-family:Verdana; font-size:8pt; white-space:nowrap;}");
	ImageWindow.document.write(".Variants .Thumbnail	{width:36px; height:27px; border:1px solid #513f5d; cursor:pointer;}");
	ImageWindow.document.write(".Variants .Variant		{color:#3b243d;}");
	ImageWindow.document.write(".Variants .SoldOut		{color:#998d9a; font-style:italic;}");
	ImageWindow.document.write(".Variants .Detail		{color:#998d9a; font-size:7pt;}");
	ImageWindow.document.write("</style>");
	ImageWindow.document.write("<script>");
	ImageWindow.document.write("function OpenVariantImage(src, title){");
	ImageWindow.document.write("var img = document.getElementById(\"TheImage\");");
	ImageWindow.document.write("img.src=src; img.title=title;");
	ImageWindow.document.write("document.title=title;");
	ImageWindow.document.write("}");
	ImageWindow.document.write("</script>");
	ImageWindow.document.write("</head>");
	ImageWindow.document.write("<body style=\"overflow:auto; background-color:#ffffff; margin:0px; \">");

	ImageWindow.document.write("<table cellpadding=\"0\" cellspacing=\"0\" style=\"border-collapse:collapse;\"><tr><td valign=\"top\" style=\"width:210px; border-right:1px solid #333333; background-color:#eee7ee;\"><div style=\"overflow:auto; height:480px;\">");
	ImageWindow.document.write(Get("ctl00_Content_PopupContent").innerHTML);
	ImageWindow.document.write("</div></td><td valign=\"top\" style=\"width:640px;\">");
	
	ImageWindow.document.write("<img id=\"TheImage\" src=\"" + src + "\" title=\"" + title + "\" onclick=\"window.close();\" style=\"cursor:pointer;\" />");
	
	
	
	ImageWindow.document.write("</td></tr></table></body></html>");
	ImageWindow.document.close();

	ImageWindow.focus();
}

function OpenHelp(href)
{
    try
    {
        if (HelpWindow)
            HelpWindow.close();
    }
    catch (e) { }

    HelpWindow = window.open("/Help/" + href, "HelpWindow", "location=no,scrollbars=yes,width=400,height=500");

    HelpWindow.focus();
}

/*** END WINDOW MANAGEMENT ***/

/*** BEGIN UTILITY ***/

function Defined(Value)
{
	Value = ""+Value
	
	if(Value == "null" || Value == "" || Value == "undefined" || Value == "unknown" || Value == "NaN")
		return false;
	else
		return true;
}
function Get(id)
{
	return document.getElementById(id);
}
function GetByClass(searchClass, node, tag)
{
    var classElements = new Array();
    
    if (node == null)
        node = document;
    if (tag == null)
        tag = '*';
    
    var els = node.getElementsByTagName(tag);
    var elsLen = els.length;
    var pattern = new RegExp("(^|\\\\s)" + searchClass + "(\\\\s|$)");
    
    for (i = 0, j = 0; i < elsLen; i++)
    {
        if (pattern.test(els[i].className))
        {
            classElements[j] = els[i];
            j++;
        }
    }
    return classElements;
}
function GetFormField(id)
{
	return document.getElementById(IdPrefix + id);
}
function FindParentNode(Child, ParentTagName)
{
	var Limit = 8;

	var ParentNode = Child.parentNode;

	while (Limit > 0 && ParentNode.tagName.toLowerCase() != ParentTagName.toLowerCase())
	{
		ParentNode = ParentNode.parentNode;
		Limit--;
	}
	return ParentNode;
}

/*** END UTILITY ***/

/*** BEGIN NAVIGATION ***/

function HighlightNavigation()
{
	if (this.HoverImage.PreLoaded)
	{
		this.RestoreImage = this.src;
		this.src = this.HoverImage.src;
	}
}
function RestoreNavigation()
{
	if (Defined(this.RestoreImage))
	{
		this.src = this.RestoreImage;
	}
}
function InitiateHoverImage(img)
{
	var PreloadImage = new Image();
	PreloadImage.onload = function() { this.PreLoaded = true; }
	PreloadImage.src = img.src.replace(/\.gif/gi, "-H.gif");

	img.onmouseover = HighlightNavigation;
	img.onmouseout = RestoreNavigation;
	img.HoverImage = PreloadImage;
}
function LoadHoverNavigation()
{
	var Images = document.getElementsByTagName("img");

	for (var i = 0; i < Images.length; ++i)
	{
		if (Images[i].className == "HoverImage")
		{
			InitiateHoverImage(Images[i]);
		}
	}
}
/*** END NAVIGATION ***/

/*** BEGIN MESSAGES ***/

function DisplayStatus()
{
    var Status = ReadCookie("KWStatus");
    if (Status != null)
    {
    	//var Html = "<a href='javascript:ClearStatus();'>&laquo; " + Status + " &raquo;</a>";
    	Get("Status").innerHTML = Status;
    	Get("Status").style.display = "block";
    }

    DeleteCookie("KWStatus");
}

function ClearStatus()
{
	Get("Status").style.display = "none";
    Get("Status").innerHTML = "";
}

function AddMessage(message)
{
    var MessageDiv = Get("Message");

    var Lists = MessageDiv.getElementsByTagName("ul");
    var List;

    if (Lists.length > 0)
    {
        List = Lists[0];
    }
    else
    {
        List = document.createElement("ul");
        MessageDiv.appendChild(List);
    }
    
	var ListItem = document.createElement("li");
	ListItem.innerHTML = message;

	List.appendChild(ListItem);
	window.scrollTo(0, 0);
}

function ClearMessages()
{
	Get("Message").innerHTML = "";
}

function ConfigureValidation()
{
    if (typeof (Page_ValidationSummaries) == "undefined")
    {
        Page_ValidationSummaries = new Array(Get("Message"));
    }
    else
    {
        Page_ValidationSummaries.push(Get("Message"));
    }
}

/*** END MESSAGES ***/

/*** BEGIN COOKIES ***/

function ReadCookie(name)
{
    var results = document.cookie.match('(^|;|&|=) ?' + name + '=([^;&]*)(;|&|$)');

    if (results)
        return (unescape(results[2]));
    else
        return null;
}

function DeleteCookie(name)
{
    document.cookie = name += "=; expires=Thu, 01-Jan-1970 00:00:01 GMT"
}

/*** END COOKIES ***/

/*** BEGIN AUTHENTICATION ***/

function IsAuthenticated()
{
    return ReadCookie("Ver") == Version && ReadCookie("Token") != null;
}

function GetNickname()
{
    return ReadCookie("Nickname");
}

function IsAdministrator()
{
    return ReadCookie("Admin") == "Y";
}

function RenderAuthentication()
{
    if (IsAuthenticated())
    {
        Get("Auth_Authenticated").style.display = "inline";
        Get("Auth_Nickname").innerHTML = GetNickname();

        if (IsAdministrator())
            Get("Auth_Administration").style.display = "inline";
    }
    else
    {
        Get("Auth_Anonymous").style.display = "inline";
    }
}

/*** END AUTHENTICATION ***/

/*** BEGIN SIMPLE AJAX ***/

function Execute(page, preRequest, postRequest, state)
{
    var Request;
    try { Request = new ActiveXObject('Msxml2.XMLHTTP'); }
    catch (e)
    {
        try { Request = new ActiveXObject('Microsoft.XMLHTTP'); }
        catch (e2)
        {
            try { Request = new XMLHttpRequest(); }
            catch (e3) { Request = false; }
        }
    }

    if (Request)
    {
        if (preRequest != null)
            preRequest(Request, state);

        if (postRequest == null)
        {
            postRequest = function(r, s)
            {
                try { eval(r.responseText); }
                catch (e) { }
            };
        }

        Request.onreadystatechange = function()
        {
            if (Request.readyState == 4)
                postRequest(Request, state);
        };

        Request.open("GET", page, true);    //true for asynchronous
        Request.send(null);                 //send nothing to the server
    }
}

/*** END SIMPLE AJAX ***/

function CSS(obj, value)
{
    if (!Defined(obj) || !Defined(value))
        return;

    if (value.indexOf(':') < 0)  //css class name
    {
        if (obj.className.indexOf(value) < 0)    //not applied yet
        {
            obj.className += ' ' + value;
        }
    }
    else    //inline style
    {
        obj.setAttribute("style", value);
        try { obj.style.cssText = value; }
        catch (e) { }
    }
}

function Disable(obj)
{
    if (!Defined(obj))
        return;

    obj.setAttribute("disabled", "disabled");

    var Type = obj.tagName;

    if (Type == 'A')
        obj.setAttribute("href", "javascript:void(0);");   
}

