/* CALLRAIL SCRIPT Customized by AP on 2011-12-02 */

/*window.onload = function () {
  referrer_key = getReferrerKey();
  new_number = findNumberForSwap(referrer_key);
}*/

  /******************************************
* Swapping the phone number
*******************************************/
function findNumberForSwap(referrer_key) {

      /* Swap numeric phone numbers */
      keys_583 = ['google_organic', 'yahoo_organic', 'bing_organic'];
      if(contains(keys_583, referrer_key)){
        old_number = '8889326861';
        new_number = '8665723998';
        swapPhone(old_number, new_number);
      }
      keys_582 = ['google_paid', 'yahoo_paid', 'bing_paid'];
      if(contains(keys_582, referrer_key)){
        old_number = '8889326861';
        new_number = '8668790386';
        swapPhone(old_number, new_number);
      }
      
      /* Swap the alphanumeric phone number */
      keys_583 = ['google_organic', 'yahoo_organic', 'bing_organic'];
      if(contains(keys_583, referrer_key)){
        old_number = '888WEBNUM1';
        new_number = '8665723998';
        swapPhone(old_number, new_number);
      }
      keys_582 = ['google_paid', 'yahoo_paid', 'bing_paid'];
      if(contains(keys_582, referrer_key)){
        old_number = '888WEBNUM1';
        new_number = '8668790386';
        swapPhone(old_number, new_number);
      }
}

function swapPhone(old_number, new_number) {
  old_123 = old_number.substring(0,3);
  old_456 = old_number.substring(3,6);
  old_6789 = old_number.substring(6,10);
  new_123 = new_number.substring(0,3);
  new_456 = new_number.substring(3,6);
  new_6789 = new_number.substring(6,10);

  var re = new RegExp("(\\(?)"+old_123+"(\\))?([-. ]?)"+old_456+"([-. ]?)"+old_6789,"g");
  
  document.body.innerHTML = 
      document.body.innerHTML.replace(
          re, 
          "$1" + new_123 + "$2$3" + new_456 + "$4" + new_6789
      );
}

/******************************************
* Dealing with referrer
*******************************************/

// Get the referrer from a cookie, otherwise get it from the url and set a cookie
function getReferrerKey(){
  if(readCookie('callrail')){
    referrer_key = readCookie('callrail');
  }else{
    referrer_key = getReferrerKeyFromURL();
    createCookie('callrail', referrer_key, 365);
  }
  return referrer_key;
}

// Get the referrer key from document.referrer and document.url
function getReferrerKeyFromURL(){
  referrer = document.referrer;
  
  if(referrer.match(/^(.*)google(.*)$/)) {
    if(referrer.match(/^(.*)maps\.google\.com(.*)$/)) {
      referrer_key = 'google_local';
    } else if(referrer.match(/^(.*)google\.com\/aclk(.*)$/)) {
      referrer_key = 'google_paid';
    } else {
      referrer_key = 'google_organic';
    }
  } else if(referrer.match(/^(.*)yahoo(.*)$/)) {
    if(referrer.match(/^(.*)local\.yahoo\.com(.*)$/)) {
      referrer_key = 'yahoo_local';
    } else if(referrer.match(/^(.*)msn\.com(.*)$/)) {
      referrer_key = 'yahoo_paid';
    } else {
      referrer_key = 'yahoo_organic';
    }
  } else if(referrer.match(/^(.*)bing(.*)$/)) {
    if(referrer.match(/^(.*)bing\.com\/local(.*)$/)) {
      referrer_key = 'bing_local';
    } else {
      referrer_key = 'bing_organic';
    }
  } else if(referrer.match(/^(.*)msn\.com(.*)$/)) {
    referrer_key = 'bing_paid'
  } else if (!document.referrer || document.referrer == '') {         
    referrer_key = 'direct';
  } else {
    referrer_key = getReferrerDomain();
  }
  return referrer_key;
}

// Get the domain name of the referrer
function getReferrerDomain(){
  var host = document.referrer.split('/')[2];
  var host_split = host.split('.');
  if(host_split.length > 2){
    var domain = host_split[host_split.length - 2] + '.' + host_split[host_split.length - 1];
  }else{
    var domain = host;
  }
	return domain;
}


/******************************************
* Dealing with cookies
* http://www.quirksmode.org/js/cookies.html
*******************************************/
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

/******************************************
* Search array
* http://stackoverflow.com/questions/237104/javascript-array-containsobj/1342312#1342312
*******************************************/
function contains(a, obj) {
  var i = a.length;
  while (i--) {
    if (a[i] === obj) {
      return true;
    }
  }
  return false;
}
