function getQueryVariable(variable)
{
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++)
{
var pair = vars[i].split("=");
if (pair[0] == variable)
{
return pair[1];
}
}
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
var cookiename = c_name + "=" +escape(value);
if (expiredays==null) {
cookiename += ";expires=" + exdate.toUTCString();
}
cookiename += ";path=/;domain=www.whowantsatreat.com";
document.cookie = cookiename;
}

function checkCookie()
{
affid=getCookie('affid');
if (affid!=null && affid!="")
  {}
        else
        {
  affid=getQueryVariable("affid");
  if (affid!=null && affid!="")
    {
    setCookie('affid',affid,365);
    }
    else
    {
    setCookie('affid','999',365);
    }
  }
}


    
function capStr(obj) {
        val = obj.value;
        newVal = '';
        val = val.split(' ');
        for(var c=0; c < val.length; c++) {
                newVal += val[c].substring(0,1).toUpperCase() +
val[c].substring(1,val[c].length) + ' ';
        }
        obj.value = newVal;
}

function strstr(strstring, strsearch, bool) {
    var pos = 0;
    strstring += '';
    pos = strstring.indexOf( strsearch );    
    if (pos == -1) {
        return false;
    }
    else
    {
        if (bool){
            return strstring.substr( 0, pos );        
            } else
            {
            return strstring.slice( pos );
        }
    }
}

function init() {
    cqs = getQueryVariable("utm_campaign");
    if (cqs!=null && cqs!="")
    {
        cid = strstr(cqs, "-", true);
        if (cid != false) {
            setCookie('mccampaignid', cid, 30);
        }
    }
    checkCookie();
    //setCouponCode();
}

window.onload = init;
