/*
 * cookieName is the same name as the varibale name in the get string
 * Vont.html?campaign=BigPush its campaign in that example
 * 
 * campaignName is the value of the variable you are passing
 * Vont.html?campaign=BigPush its BigPush in this example
 * 
 * add this to the web page
 * <body onload="javascript:writeNumber();">
 * 
 * where the phone numebr is wrap it in a span with id=phone
 *  <div>Company <span id='phone'>222.1212</span></div>
 *  
 *  the vars are CASE SENSITIVE!!!!!!!!!!!!!!!!!!
 * 
 */
//change these to suit
var cookieName = new Array('campaign');
var campaignName = new Array('Maine');
var campaignPhone = new Array('877.904.7486');

function GGgetQueryVariable(variable){
  var returnVal = false;
  var query = window.location.search.substring(1);
  var vars = query.split('&');
  for (var i=0;i<vars.length;i++){
    var pair = vars[i].split('=');
    if (pair[0] == variable){returnVal = pair[1];}
  }
  return returnVal;
}

function GGgetCookie() {
    var flag = false;
    var cookieArray = document.cookie.split(';');
    for(var i=0;i<cookieArray.length;i++){
      var newStr = cookieArray[i].replace(/^\s*/, '').replace(/\s*$/, '');   
      //loop through the cookie arrays if array index not set
      if(inAddress){
        if((cookieName[arrayIndex] + '='+ campaignName[arrayIndex]) ==  newStr){flag = true;}
      }
      else{
        for(var j=0;j<cookieName.length;j++){
          if((cookieName[j] + '='+ campaignName[j]) ==  newStr){flag = true;arrayIndex = j;}
        }
      }
    }  
    return flag;
}

//loop through the address and the names and the get vars
var arrayIndex = false;
var inAddress = false;
for(var i=0;i<cookieName.length;i++ ){
  if(GGgetQueryVariable(cookieName[i]) != false){arrayIndex = i;inAddress = true;}
}

//set the cookie if we need to
if(inAddress){
  //write the cookie
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+30);
  document.cookie=cookieName[arrayIndex]+'='+campaignName[arrayIndex]+';expires='+exdate.toUTCString();
}

//this just writes the number on load
function writeNumber(){
  if (GGgetCookie() == true) {
    document.getElementById('gphone').innerHTML = campaignPhone[arrayIndex];} 
}

