
if(getUrlParam("aid") != ""){
    if(getCookie("sourcecode") == ""){ //no cookie so write it
        setCookie("sourcecode",getUrlParam("aid"));
    }
} 

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") == ""){ //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));
}