//Cameleon.js Copyright SilverDisc 2011
//To use this library, you have put in the html header tags with the following code 
//<script type="text/javascript" src="camaleon.js"></script>


var cookieName 		= "utm_medium";
var cookieValueToMatch	= "cpc";

var callTracksSpan = "calltracks_tempest4cars";
var callTracksSpan2 = "calltracks_tempest4cars2";
var textFind		= new Array("01903 879 789","01903 251 251"); // Text to Find
var textReplace		= new Array("01903 371 060","01903 371 061"); //Text to Replace

function getUrlParameter(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 compareCookie(){
	
	var urlParameterValue 		= getUrlParameter(cookieName);
	var cookie			= getCookie(cookieName);

	if(urlParameterValue){
		if(urlParameterValue == cookieValueToMatch){
			setCookie( cookieName, urlParameterValue);
			replaceContent();
		}else{
			setCookie( cookieName, 'cpc');

		}
	}else{
		if(cookie == cookieValueToMatch){
			replaceContent();
		}
	}
}

function getCookie(c_name){
	if (document.cookie.length>0){
		c_start = document.cookie.indexOf(c_name + "=");
		if (c_start!=-1){
    		c_start	= c_start + c_name.length+1;
    		c_end	= document.cookie.indexOf(";",c_start);
    		if (c_end==-1) c_end=document.cookie.length;
    		return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}
function setCookie(c_name,value){

	document.cookie	= c_name+ "=" +escape(value)+";path=/;";
}

window.onload = function() {
	compareCookie();
}

function replaceContent(){
	camaleonTextReplace(callTracksSpan,0);
	camaleonTextReplace(callTracksSpan2,1);
}


function camaleonTextReplace(id,arrayValue){
 elements = obtainPartsByName(document, 'span', id);
    for (x in elements) {
      elements[x].innerHTML = textReplace[arrayValue];
    }

}

function replaceTxt(texto,s1,s2){
	return texto.split(s1).join(s2);
}

function obtainPartsByName(Part, lookFor, theID){
      var Parts;
      var arrParts = (lookFor == "*" && document.all)? document.all : Part.getElementsByTagName(lookFor);
      var RegEx = new RegExp("(^|\\s)" + theID + "(\\s|$)");
      var returnArray = new Array();
      theID = theID.replace(/\-/g, "\\-");
      for(var i=0; i<arrParts.length; i++){
          Parts = arrParts[i];
          if(RegEx.test(Parts.className)){
              returnArray.push(Parts);
          }
      }
      return (returnArray)
}






