﻿function getCause()
{
    var cause = getCookie('CauseCode');
    
    if (cause != null)
    {
        return cause;
    }
    
    //or get cause from the subdomain
    var hostName = window.location.hostname;
    var parts = hostName.split(".");
    
    var elementsCount = parts.length;
    
    if (elementsCount == 3 || elementsCount == 4)
    {
        if (parts[elementsCount - 2].match(/^causemunity$/i) && 
            !parts[elementsCount - 3].match(/^www$/i) &&
            !parts[elementsCount - 3].match(/^my$/i))
        {
            return parts[elementsCount - 3];
        }
    }
    
    return null;
}

function checkCause(theLink)
{
    //get cause from the cookie
    if (getCookie('CauseCode') != null)
    {
        return true;
    }
    
    //or get cause from the subdomain
    var hostName = window.location.hostname;
    var parts = hostName.split(".");
    
    var elementsCount = parts.length;
    
    if (elementsCount == 3 || elementsCount == 4)
    {
        if (parts[elementsCount - 2].match(/^causemunity$/i) && 
            !parts[elementsCount - 3].match(/^www$/i) &&
            !parts[elementsCount - 3].match(/^my$/i))
        {
            return true;
        }
    }
    
    var redirect = escape(theLink.href);
    
    if (window.top == window.self)
    {
        window.location = '/noselectedcause.aspx?redir=' + redirect;
    }
    else
    {
        window.location = '/NoSelectedCauseFb.aspx?redir=' + redirect;
    }
    
    return false;
    //var toContinue = confirm("You haven't selected your cause yet. Click 'Cancel' to stay on this page, then click 'Causes' on the menu to search for your cause. Click 'OK' to go to the merchant's page. However, if you make a purchase, your purchase will not benefit your cause.");
    //return toContinue;
}

function getCookie ( cookieName )
{
  var results = document.cookie.match ( '(^|;) ?' + cookieName + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}

function toIsoDate(theDate)
{
    var year = theDate.getFullYear();
    var month = theDate.getMonth() + 1;
    var date = theDate.getDate();
    
    if (month < 10)
    {
        month = "0" + month;
    }
    
    if (date < 10)
    {
        date = "0" + date;
    }
    
    return year + "-" + month + "-" + date;
}