﻿/********************************************************************
* Copyright ©  Acsys, Inc.  All rights reserved.
* 
* This material contains the valuable properties and trade secrets of
* Acsys, Inc. embodying substantial creative efforts and confidential 
* information, ideas, and expressions, no part of which may be 
* reproduced or transmitted in any form or by any means, electronic, 
* mechanical, or otherwise, including photocopying and recording or 
* in connection with any information storage or retrieval system 
* without the permission in writing of Acsys, Inc.
*/

/********************************************************************
* This is the Penny Publications web site client library.
*/

/********************************************************************
* 
* Dependencies: 
*	asp.net ajax
*/

function ValidateAddToCart(source, args)
{
    args.IsValid = false;
    var validator = document.getElementById(source.id);
    
    if (typeof(validator.listid) != "string" && typeof(validator.attributes["listid"]) == "undefined")
    {
        return;
    }
    
    var listID = validator.listid;
    
    if (!listID)
    {
        listID = validator.attributes["listid"].nodeValue;
    }
    
    for (var i = 0; true; i++)
    {
        var checkBox = $get(listID + "_" + i);
        
        if (checkBox == null || typeof(checkBox.type) == "undefined" || checkBox.type != "checkbox")
        {
            break;
        }

        if (checkBox.checked == true)
        {
            args.IsValid = true;
            return;
        }
    }
}

function CancelAddToCart(source)
{
    var popup = $find('popup');

    if (!popup)
    {
        return false;
    }

    setTimeout("$find('popup').hidePopup();", 0);
    
    return false;
}

function PageLoadedAddToCart(sender, args) 
{
    if (typeof(_addToCartClientIDs) != "object")
    {
        return;
    }
    
    var itemCountElement = $get(_addToCartClientIDs.itemCount);
    var itemsElement = $get(_addToCartClientIDs.items);
    var divAddToCartContent = $get("divAddToCartContent");
    var divAddToCartProcessing = $get("divAddToCartProcessing");

    if (!itemCountElement || 
        !itemsElement || 
        !divAddToCartContent ||
        !divAddToCartProcessing ||
        isNaN(itemCountElement.value))
    {
        return;
    }
    
    var cvVariantsElement = $get(_addToCartClientIDs.cvVariants);
    var cblVariantsElement = $get(_addToCartClientIDs.cblVariants);
    var btnCloseElement = $get(_addToCartClientIDs.btnClose);
    
    divAddToCartContent.style.display = "";
    divAddToCartProcessing.style.display = "none";

    if (cvVariantsElement && cblVariantsElement)
    {
        cvVariantsElement.listid = cblVariantsElement.id;
        
        // set focus to the first checkbox        
        for (var i = 0; true; i++)
        {
            var checkBox = $get(cvVariantsElement.listid + "_" + i);
            
            if (checkBox == null || typeof(checkBox.type) == "undefined" || checkBox.type != "checkbox")
            {
                if (i >= 100)
                {
                    break;
                }
                continue;
            }
            checkBox.focus();
            break;   
        }
    }
    
    if (btnCloseElement)
    {
        btnCloseElement.focus();
    }
    
    itemsElement.innerHTML = itemCountElement.value;
}

function DoAddToCart(source)
{
    if (typeof(InvokePostBack_DoAddToCart) != "function" ||
        typeof(_addToCartClientIDs) != "object")
    {
        alert("We are unable to process your request at this time.  Please try again later.");
        return false;
    }
    
    var commandNameElement = $get(_addToCartClientIDs.commandName);

    if (!commandNameElement)
    {
        alert("We are unable to process your request at this time.  Please try again later.");
        return false;
    }

    commandNameElement.value = "DoAddToCart";
    InvokePostBack_DoAddToCart();

    if (typeof(Page_IsValid) != "undefined" && Page_IsValid)
    {
        var divAddToCartContent = $get("divAddToCartContent");
        var divAddToCartProcessing = $get("divAddToCartProcessing");
        if (divAddToCartContent &&
            divAddToCartProcessing)
        {
            divAddToCartContent.style.display = "none";
            divAddToCartProcessing.style.display = "";
        }
    }
    
    return false;
}

function ShowAddToCart(source, productID)
{
    if (typeof(InvokePostBack_ShowAddToCart) != "function" ||
        typeof(_addToCartClientIDs) != "object")
    {
        alert("We are unable to process your request at this time.  Please try again later.");
        return false;
    }

    var link = $get(source.id);
    var productIDElement = $get(_addToCartClientIDs.productID);
    var commandNameElement = $get(_addToCartClientIDs.commandName);
    var popup = $find('popup');

    if (!link || 
        !productIDElement || 
        !commandNameElement ||
        !popup)
    {
        alert("We are unable to process your request at this time.  Please try again later.");
        return false;
    }

    var divAddToCartContent = $get("divAddToCartContent");
    var divAddToCartProcessing = $get("divAddToCartProcessing");
    if (divAddToCartContent &&
        divAddToCartProcessing)
    {
        divAddToCartContent.style.display = "none";
        divAddToCartProcessing.style.display = "";
    }
    
    // set the product id and update the panel displaying the variants
    productIDElement.value = productID;
    commandNameElement.value = "ShowAddToCart";
    InvokePostBack_ShowAddToCart();

    // kill the click event so the popup actually displays
    var e = new Sys.UI.DomEvent(link);
    if (e) 
    {
        e.stopPropagation();
        e.preventDefault();
    }

    // ok, in order to get the pop to position properly, we need to 
    // keep the element invisible, but enable it for display, so
    // that its bounds can be calculated.
    var popupElement = $get(popup.get_PopupControlID());
    popupElement.style.visibility = "hidden";
    popupElement.style.display = "";
    var popupBounds = Sys.UI.DomElement.getBounds(popupElement);

    // reset the popup parent to the link clicked, yes internals of the PopupControl are perhaps used inappropriately.
    link.style.position = "relative";
    var elementBounds = Sys.UI.DomElement.getBounds(link);
    
    popup._popupBehavior.set_parentElement(link);
    
    var offsetx = (popupBounds.width - elementBounds.width) * -1;
    
    //popup.set_Position(AjaxControlToolkit.PopupControlPopupPosition.Top);
    popup.set_OffsetX(offsetx);
    popup.set_OffsetY(elementBounds.height);

    // in order to let all of the event cancellation to take place 
    // invoke the popup separately
    setTimeout("$find('popup').showPopup();", 0);

    popupBounds = Sys.UI.DomElement.getBounds($get(popup.get_PopupControlID()));
    
    return false;
}

function handleOut(elname) {
// hides all popups, parameter is the div name
// function called on onmouseout of hyperlink parameter

d = document.getElementById(elname) ;
d.style.left = "-650px";
}
function setLyr(obj1,lyr)
{
    // control the blue popups
    /*
    parameters:
    obj1 - the element to reference location from, generally the hyperlink
    lyr - the DIV to position - the divs are positioned off the page to start, the html is at the top of each page for each div
    help - a flag for the help div
    
    requires the browser.js script for browser detection
    as well as findPosX and findPosY, which find the referenced object's position
     
     function is called on the on mouse over of the hyperlink element
    */
	obj = document.getElementById (obj1) ;
	var newX = findPosX(obj);
	var newY = findPosY(obj);
	
	var x = document.getElementById(lyr);
	
	// get the width of the screen
	var winWidth = (typeof(window.innerWidth) != 'undefined') ? window.innerWidth + self.pageXOffset - 20 : document.documentElement.clientWidth + document.documentElement.scrollLeft;
	// get the top
	var winTop =  document.documentElement.scrollTop;
    
    // variables for final resting place
    var finY ;
    var finX ;
    // change final resting places based on whether working with the top popup or bottom (help) popup
    finY = (newY - 185) ;    
	// set the default x
	finX = (newX - 70) ;
	
	// Safari section
	if (BrowserDetect.browser == "Safari")
	{
	    //safari uses the below property for getting the distance to the top of the page
	   winTop = self.pageYOffset ;
	   
	 
		if (winWidth < 475) // if the window is sized this size
	    {
        finX = (newX -125);	    // move the object to the left
	    }				
		   
        if (winTop > 740)   // flip it over if at bottom of page
        {       
        finY = (newY + 30) ; 
        }       
        
	}
	else // ie, firefox
	{
	    
	    if (winWidth < 375) // flip to the other side
	    {
        finX = (newX -125);	    
	    }
	    
	  //alert(winTop);
       if (winTop > 200) // flip over if near top of page
       {       
       finY = (newY + 30) ;        
       }
      	    
	  
	}	
	//set final position
	 x.style.top =  finY + 'px';
	 x.style.left = finX + 'px';
	 
}
function findPosX(obj)
  { //recursively searches through dom to get x postion
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {//recursively searches through dom to get y postion
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }

function switchtabs(t, total)
{
    // 4 is 1 + the number of tabs
    // each tab content area is named tab1, tab2 etc..
    // each tab head is named thead1, thead2 etc...

    for (i=1;i <= total;i++)
    {
        
         el = document.getElementById ("tab" + i);
         elhead = document.getElementById ("thead" + i);
        if (i==t)
        {
            el.style.display = "";
            elhead.className = "current";
        }
        else
        {
            el.style.display = "none";
            elhead.className = "";
        }
    }
}

function switchtabsbyname(baseHeadName, baseElemName, t, total)
{
    // 4 is 1 + the number of tabs
    // each tab content area is named tab1, tab2 etc..
    // each tab head is named thead1, thead2 etc...

    for (i=1;i <= total;i++)
    {
        
         el = document.getElementById (baseElemName + i);
         elhead = document.getElementById (baseHeadName + i);
        if (i==t)
        {
            el.style.display = "";
            elhead.className = "current";
        }
        else
        {
            el.style.display = "none";
            elhead.className = "";
        }
    }
}
