<!--
// -----------------------------------------------------------------------------
// Grabs the source from the query string and saves it as a cookie
// unless a source has already been set
//
// Cookie has been set to expire after a 10 days 
// 
// To use this on a page add the following js lines, where DEFAULT_SOURCE_FOR_PAGE
// is the source you want to record if the user does not have one already, or one is
// not specified in the query string
//  var pagesource = 'DEFAULT_SOURCE_FOR_PAGE';
//  setSource();
//
// The following pages are setup for debugging
// whats_my_source.html
// clear_my_source.html
// -----------------------------------------------------------------------------
// -->

function setCookie(c_name,value,expiredays) {
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie=c_name+ "=" +escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString()) + ';domain=hotchalk.com;path=/';
//  alert ("setCookie:" + document.cookie);
}

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 getParameter ( queryString , parameterName ) {
   // Add "=" to the parameter name (i.e. parameterName=value)
   var parameterName = parameterName + "=";
   if ( queryString.length > 0 ) {
      // Find the beginning of the string
      begin = queryString.indexOf ( parameterName );
      // If the parameter name is not found, skip it, otherwise return the value
      if ( begin != -1 ) {
         // Add the length (integer) to the beginning
         begin += parameterName.length;
         // Multiple parameters are separated by the "&" sign
         end = queryString.indexOf ( "&" , begin );
      if ( end == -1 ) {
         end = queryString.length
      }
      // Return the string
//      alert ("Source Code Found!:" + parameterName + queryString);
	  return unescape ( queryString.substring ( begin, end ) );
	  
   }
   // Return "null" if no parameter has been found
//   alert ("NO source Code Found!: [" + parameterName + queryString + "]");
   return "null";
   	  
   }
}

function setSource (){
  if (getCookie('source') == '') {   
    expiredays = 5
    source = getParameter(window.top.location.search.substring(1), 'source')
    if (source == null || source == '') {
      setCookie('source', pagesource, expiredays);
//	   alert ("setSource (pagesource):" + pagesource);
    }
    else {
      setCookie('source', source, expiredays);
//  alert ("setSource (source):" + source);
    }
  }
}
var pagesource = '';
setSource();
