function setCookie ( c_name, value, exdays ){

var exdate=new Date();

exdate.setDate(exdate.getDate() + exdays);

var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());

document.cookie=c_name + "=" + c_value;

}

function getCookie(c_name)  {

var i,x,y,ARRcookies=document.cookie.split(";");

  for (i=0;i<ARRcookies.length;i++)	{
    x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
    y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
    x=x.replace(/^\s+|\s+$/g,"");

    if (x==c_name)    {
      return unescape(y);
    }

  }
}

function getParams() {

 var idx = document.URL.indexOf('?');
 var params = new Array();

 if (idx != -1) {
   var pairs = document.URL.substring(idx+1, document.URL.length).split('&');

   for (var i=0; i<pairs.length; i++) {
     nameVal = pairs[i].split('=');
     params[nameVal[0]] = nameVal[1];
   }
 }

 return params;

}
/* Creates the cookie and gets sourceid and campaignid off the address line
** Call this at the top of every page 
*/

function createTrackingCookie () {
	var params = getParams();
	var bSourceIdPresent = false;
	var bCampaignIdPresent = false;

	if ( params['source_id'] != null && params['source_id'] != "" ) {
	    bSourceIdPresent = true;
		setCookie( "source_id", params['source_id'], 31 );
	}
	
	if ( params['campaign_id'] != null && params['campaign_id'] != "" ) {
	    bCampaignIdPresent = true;
		setCookie( "campaign_id", params['campaign_id'], 31 );
	}
	
	/* If they are both missing from the address line then look at referrer info */
	if ( bSourceIdPresent == false && bCampaignIdPresent == false ) {
	    	/* 
		** this bit is not coded yet
		*/
	}
}

/* 
** Pass a superbreak url in this and it will 
** return the same url but with sourceid and campaign in it 
*/
function createUrlWithTrackingInfo ( sUrl ) {
	
	var sourceid = getCookie("source_id");
	var campaignid = getCookie("campaign_id");
	var sNewUrl = sUrl;
	var sParams = "";
	
	if ( sourceid != null && sourceid != "" ) {
	    sParams = "?source_id=" + sourceid;
	}
	else {
	    sParams = "?source_id=none";
	}
	
	if ( campaignid != null && campaignid != "" ) {
	    if ( sParams == "" ) {
	        sParams = "?campaign_id=" + campaignid;
	    }
	    else {
	        sParams = sParams + "&campaign_id=" + campaignid;
	    }
	}
	else {
        if ( sParams == "" ) {
	        sParams = "?campaign_id=none";
	    }
	    else {
	        sParams = sParams + "&campaign_id=none";
	    }
	}
	sNewUrl = sNewUrl + sParams;	

    return ( sNewUrl );   
}
