// CALENDAR
//

function calendarFocus(divID)
{
    // Mark as focused
    eval('window.focus_' + divID + ' = true;');

    // Show popup
    showDiv(divID);
}

function calendarBlur(divID)
{
    // Mark as blurred
    eval('window.focus_' + divID + ' = false;');

    // If not moused over, hide popup
    eval('var over = window.over_' + divID + ';');
    if (!over)
        hideDiv(divID);
}

function calendarOver(divID)
{
    // Mark as moused over
    eval('window.over_' + divID + ' = true;');
}

function calendarOut(divID, e)
{
    if (window.event)
        e = window.event;
        
    var div = document.getElementById(divID);
    if (!div)
        return;

    var eventX = e.clientX + document.body.scrollLeft;
    var eventY = e.clientY + document.body.scrollTop;
    getAbsolutePosition(div);
    var divW = getDivW(div);
    var divH = getDivH(div);

    // Ignore this event if the cursor is inside the div
    if (eventX > _absX && eventX < _absX + divW && eventY > _absY && eventY < _absY + divH)
        return;
    
    // Mark as moused out
    eval('window.over_' + divID + ' = false;');

    // If not focused, hide popup
    eval('var focus = window.focus_' + divID + ';');
    if (!focus)
        hideDiv(divID);
}

// UPLOAD
//

function preUpload(id)
{
    var form = document.forms['aspnetForm'];
    var action = form.elements[id + '_Action'];
    var target = form.target;

    // Set values
    action.value = 'upload';    
    form.target = id + '_Target';
    
    // Submit form
    form.submit();

    // Restore values
    action.value = '';
    form.target = target;
}

function postUpload(id, filename)
{
    var hidden = document.getElementById(id + '_SlickBack');
    var slickback = hidden.value.replace("__FILE__", filename);

    eval(slickback);
}

function resizeIframe()
{
  var flyoutCart = document.getElementById('flyoutCart');
  var iFrame = document.getElementById('iFrame');
  alert(flyoutCart);
  alert(iFrame);
  
  var height = getDivH(flyoutCart);
  
  iFrame.style.height = height;
}

function goToLink(pSelect)
{
  if(pSelect[pSelect.selectedIndex].value.length == 0)
  {
    return;
  }
  else if (pSelect[pSelect.selectedIndex].value.substring(0, 4) == "http")
  {
    window.open(pSelect[pSelect.selectedIndex].value,'_blank');
  }
  else if (pSelect[pSelect.selectedIndex].value.substring(0, 3) == "www")
  {
    window.open("http://" + pSelect[pSelect.selectedIndex].value, '_blank');
  }
  else
  {
    window.open(pSelect[pSelect.selectedIndex].value, '_blank');
  }
}

function ChangePayPalDropDown(v)
{
  var el = document.getElementById('PayPalSelect');
  el.value = v;
}

function OpenPopup(popupID, blockerDivID)
{
  var popupDiv = document.getElementById(popupID + '_Cal');
  var blockerDiv = document.getElementById(blockerDivID);

  var containerDiv = document.getElementById(popupID + '_Popup');
  var contentDiv = document.getElementById(popupID + '_Content');

  containerDiv.innerHTML = contentDiv.innerHTML;
  popupDiv.style.display = "block";
  popupDiv.style.visibility = "visible";
  
  var viewPortHeight = getViewportHeight();
  var viewPortWidth = getViewportWidth();

  var divH = getDivH(popupDiv);
  var divW = getDivW(popupDiv);
  
  var top = (viewPortHeight / 2) - (divH / 2) + (getScrollPosition().scrollTop);
  var left = (viewPortWidth / 2) - (divW / 2);

  popupDiv.style.top = top + "px";
  popupDiv.style.left = left + "px";
 
  blockerDiv.style.width = getViewportWidth();
  blockerDiv.style.height = document.body.scrollHeight;
  blockerDiv.style.display = "block";
  blockerDiv.style.visibility = "visible";
}

function ClosePopup(popupID, blockerDivID)
{
  var popupDiv = document.getElementById(popupID + '_Cal');
  var blockerDiv = document.getElementById(blockerDivID);

  var containerDiv = document.getElementById(popupID + '_Popup');

  containerDiv.innerHTML = '';
  
  popupDiv.style.display = "none";
  popupDiv.style.visibility = "hidden";

  blockerDiv.style.display = "none";
  blockerDiv.style.visibility = "hidden";
  
}

function getDivW(el)
{
  return forceInt(
el ? (el.style.width || el.offsetWidth || el.style.pixelWidth || el.clientWidth || 0)
: 0
);
}

function getDivH(el)
{
  return forceInt(
el ? (el.style.height || el.offsetHeight || el.style.pixelHeight || el.clientHeight || 0)
: 0
);
}

function getDivWR(el)
{   
    var maxW = 0;     
    var id = el.id;
    
    for(var i = 0; i < el.childNodes.length; i++)
    {
        var child = el.childNodes[i];
        if (child && child.style)        
            maxW = Math.max(maxW, getDivW(child));        
    }    
    
    return maxW;
}

function getDivHR(el)
{   
    var maxH = 0;     
    var id = el.id;
    
    for(var i = 0; i < el.childNodes.length; i++)
    {
        var child = el.childNodes[i];
        if (child && child.style)        
            maxH = Math.max(maxH, getDivH(child));        
    }    
    
    return maxH;
}

function getViewportWidth()
{
  return self.innerWidth || (document.documentElement.clientWidth || document.body.clientWidth);
}

function getViewportHeight()
{
  return self.innerHeight || (document.documentElement.clientHeight || document.body.clientHeight);
}

function getScrollPosition()
{
  var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
  var scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0;
  return { "scrollTop": scrollTop, "scrollLeft": scrollLeft };
}

function forceInt(x, y)
{
  return isNaN(y = parseInt(x)) ? 0 : y;
}

