
if(getUrlParam("aid") != ""){
    if(getCookie("sourcecode") == ""){ //no cookie so write it
        setCookie("sourcecode",getUrlParam("aid"));
    }
} 

function NavigateToHearAboutPage(url){

     window.location = "http://crown.org/cart/frmselectstation.aspx?refer=" + url;
}

function addSourceCodeAndNavigateToUrlTest( url ){
        
    if(url.indexOf("?") == -1){
        NavigateToHearAboutPage(url + "?aid=" + getCookie("sourcecode"));
    }
    else{
        NavigateToHearAboutPage(url + "&aid=" + getCookie("sourcecode"));
    }
        
}

// if url does not contain ?, put the ?aid=source code cookie into the URL.
//if it is there already, then only put the &aid=source code cookie.
function addSourceCodeAndNavigateToUrl( url ){
        
    if(url.indexOf("?") == -1){
        window.location = url + "?aid=" + getCookie("sourcecode");
    }
    else{
        window.location = url + "&aid=" + getCookie("sourcecode");
    }
        
}


function specifySourceCodeAndNavigateToUrl( url,sourceCode ){
     
    var srcCode;
    
    if(getCookie("sourcecode") == "" ||
	getCookie("sourcecode") == "undefined" ){ //no cookie so write it
        setCookie("sourcecode",sourceCode);
        srcCode = sourceCode;
    }
    else{
        srcCode = getCookie("sourcecode");
    }   
    
    if(url.indexOf("?") == -1){
        window.location = url + "?aid=" + srcCode;
    }
    else{
        window.location = url + "&aid=" + srcCode;
    }
        
}

function specifySourceCodeAndNavigateToUrlAndNewBrowser( url,sourceCode, nb ){
     
    var srcCode;
    
    if(getCookie("sourcecode") == "" ||
	getCookie("sourcecode") == "undefined" ){ //no cookie so write it
        setCookie("sourcecode",sourceCode);
        srcCode = sourceCode;
    }
    else{
        srcCode = getCookie("sourcecode");
    }   
    
    if(url.indexOf("?") == -1){
        window.location = url + "?aid=" + srcCode;
    }
    else{
        window.location = url + "&aid=" + srcCode;
    }
        
}

function getUrlParam( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function setCookie(cookieName,cookieValue){
    
    var tenYearsLater = new Date( new Date().getTime() + 3600000 );//3600000 miliseconds = 1 hr
    
    document.cookie = cookieName+"="+escape(cookieValue)
                 + ";expires="+ tenYearsLater.toGMTString();
}

function getCookie(cookieName) {

    var cookiestring=""+document.cookie;
    var cookiename = cookieName;
    var index1=cookiestring.indexOf(cookiename);

    if (index1==-1 || cookiename=="") return ""; 

    var index2=cookiestring.indexOf(';',index1);

    if (index2==-1) index2=cookiestring.length; 

    return unescape(cookiestring.substring(index1+cookiename.length+1,index2));
}