// Variables
var insertNodeId = 'content'; // ID of the element where new content will be inserted
var removeNodeId = 'quote-ad'; // ID of the element you want to remove
var newContent = '<div class="referrer-welcome map-background">'+
'<div class="keyword_el pin-background searched-keyword">We can help you with<br /><strong>JS_CONTENT</strong><br /><br /></div>'+
'<div class="keyword_el"><form action="http://www.new-gutters.com/step2.php" method="get"><label for="zip">Please enter your zip code:</label><br /> '+
'<input type="text" id="zip" name="zip"> '+
'<input type="submit" value="Continue >">'+
'</form><br />'+
'</div>'+
'</div>'; // variable JS_CONTENT will be replaced with query string

$(function () { welcomeReferred(); });

function welcomeReferred() {
   refDomain =  document.referrer.replace('http://','').split('/');
   if (document.referrer != '' && refDomain != document.domain) {
       q = getQSVar('q',document.referrer);
               if(!q) q = getQSVar('p',document.referrer);
       if(q) {
           q = q.replace( /\+/g ," ");
           newContent = newContent.replace('JS_CONTENT',q);
           $('#'+insertNodeId).prepend(newContent);
           $('#'+removeNodeId).empty();
       }
   }
}
function getQSVar(qsVar,url) {
   url = url.split('?');
   var vars = url[1].split('&');
   for (var i=0;i<vars.length;i++) {
       var pair = vars[i].split("=");
       if (pair[0] == qsVar) {
           return pair[1];
       }
   }
 return false;
}