function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i=0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
		return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	if (name == 'tngsource'){
		return 'THWHMPGDO10';	
	}
	return null;
}

function SetCookie (name, value, expires, path, domain, secure) {

	document.cookie = name + "=" + escape (value) +
	((expires == "") ? "" : ("; expires=" + expires.toGMTString())) +
	((path == "") ? "" : ("; path=" + path)) +
	((domain == "") ? "" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");
}

function getCookieVal(offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
	endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function setTnGCookiesValues(){
  var tngSourceValue = GetCookie('tngsource');
  if(tngSourceValue == null){   
    //get the date for 14 days in the future
      var today = new Date();
      today.setTime( today.getTime() );
      var expires_date = new Date( today.getTime() + (14 * 1000 * 60 * 60 * 24) );    
      SetCookie( 'tngsource' , 'THWHMPGDO10', expires_date , '/', '.stjude.org', false );         
  }
  return;
}


function overWriteTnGCookieValue(tngSourceCode, tngPlt){
	var tngSourceValue = GetCookie('tngsource');
	var tngPilotValue = GetCookie('tngplt');
	//if(tngSourceValue == null || tngSourceValue == 'THWHMPGDO09' || tngSourceValue == 'THWSPANDO09' || tngSourceValue == 'THWTRENDO09' || tngSourceValue == 'THWHILTDO09'){

	if(tngSourceValue == null || tngSourceValue.indexOf('THWHMPGDO') > -1 || tngSourceValue.indexOf('THWSPANDO') > -1 || tngSourceValue.indexOf('THWTRENDO') > -1 || tngSourceValue.indexOf('THWHILTDO') > -1 || tngSourceValue.indexOf('THWTRHODO') > -1 || tngSourceValue.indexOf('THWECHODO') > -1 || tngSourceValue.indexOf('THWTRMEDO') > -1 || tngSourceValue.indexOf('THWECMEDO') > -1){

 

       //get the date for 14 days in the future
		var today = new Date();
		today.setTime( today.getTime() );
		var expires_date = new Date( today.getTime() + (14 * 1000 * 60 * 60 * 24) );
		SetCookie( 'tngsource' , tngSourceCode, expires_date , '/', '.stjude.org', false ); 
		if (tngPlt == 'THWHMPGDO10SG' || tngPlt == 'THWHMPGDO10PIH'){
			if (tngPilotValue == null || tngPilotValue == 'THWHMPGDO10SG' || tngPilotValue == 'THWHMPGDO10PIH' || tngPilotValue == 'THWTRENDO10MGC' || tngPilotValue == 'THWTRENDO10EGC' || tngPilotValue == 'THWTRENDO10NC' || tngPilotValue == 'THWTRENDO10HMC' || tngPilotValue == 'THWTRENDO10HEC' || tngPilotValue == 'THWTRENDO10MHC' || tngPilotValue == 'THWTRENDO10EHC' || tngPilotValue == 'THWTRENDO10MMC' || tngPilotValue == 'THWTRENDO10EMC'){
				SetCookie( 'tngplt' , tngPlt, expires_date , '/', '.stjude.org', false );
			}
			return true;
		}
		SetCookie( 'tngplt' , tngPlt, expires_date , '/', '.stjude.org', false );
	}
	return true;
}


function redirectToExpressDonation(hostUrl,siteSrcUrl){
      var URLExtention = '';
      var fullURL = hostUrl+'?program=tng&source_code=';
      var tngSourceValue = GetCookie('tngsource');
      var tngPltValue = GetCookie('tngplt');
      if(tngSourceValue != null)
      {
            fullURL = fullURL + tngSourceValue + '&tngsourcecode=' + tngSourceValue +'&plt=';
            if(tngPltValue != null)            {
                  fullURL = fullURL + tngPltValue;
            } else {
                  fullURL = fullURL + 'THWHMPGDO10NF';
            }
      } else {
            fullURL = fullURL + 'THWHMPGDO10&tngsourcecode=THWHMPGDO10&plt=THWHMPGDO10NF';
      }
	  fullURL = fullURL + '&siteSrc='+siteSrcUrl;
      return fullURL;
}

function clearPixel(value1,value2){
	adTag=new Image();
	adTag.src='https://ace-tag.advertising.com/action/type=152477448/bins=1/rich=0/mnum=1516/site=695501/logs=0/betr=sjdonate_cs=2/xssale=' + value1 + '/xsmemid=' + value2;
}

function getIP(){
	var ip = '<!--#echo var="REMOTE_ADDR"-->';
}

setTnGCookiesValues();
