// JavaScript Document
var ref_list=new Array();
var phone_numbers=new Array();


var reff=document.referrer;//the documents real referrer
var ref;//the referrer we have,or will set in the coockie


//this is the main function
//should be called at the body tag with an onload attribute
//e.g <body onload="changePhonesOnReferrer()">
//OR before the closing body tag
//e.g <script type="text/javascript" defer="defer">changePhonesOnReferrer();</script>
//    </body>
function changePhonesOnReferrer()
{
	if(!reff.match(ref_list[0]) && reff.length>0)//if we got different referrer than the site itself,reff.length>0 to check we got a referrer at all 
	{
		if(get_cookie("refphone").length==0)//if we got a previous valid coockie skips,else (get_cookie("refphone").length==0) we set the coockie
		set_cookie("refphone",reff,60*60*6,"skydivinginnashville.com");//60*60*x means seconds*minutes*hours (e.g 3 hours=60*60*3),thats the time that the coockie will stay valid on browsers other than IE in seconds.On IE it will last for the browser's session(until the user closes the browser)
		ref=get_cookie("refphone");//we get the value of the previous coockie set,or if not set the value of the existing coockie
	
		setTextPhone();//we set the right phone numbers in the text
	
	}
	else//we got the site itself as referrer or direct traffiq
	{
	
		ref=get_cookie("refphone");//we get the coockie if exist,if not we get ""
		if(ref.length>0)//if coockie exist(is valid)
		{
			setTextPhone();
			//we set again the changes so they appear while user navigates in the site(every inner  page has referrer the site itself)
		}
		else//if our coockie is not valid or we have direct traffiq,we set the default images
		{

			setDefaultTextPhone();
		}
	}
}
function setTextPhone()
{
	setDefaultTextPhone();//first we set all text phone numbers with their default value,so we can make the changes later
	var phoneText=document.body.innerHTML;//we get the whole document text
	
	if(ref.match(ref_list[0]))//if we have referrer the same site we dont have to do anything
		return;
				
		
		
	for(var j=0;j<ref_list.length;j++)//we search for the referrer from the referrers list we want to track
	{
		if(ref.match(ref_list[j]))//if we find it
		{
			phoneText=phoneText.replace(new RegExp(phone_numbers[0], "g"), phone_numbers[j]);//we make the change
			document.body.innerHTML=phoneText;//and set for the documents text the default		
			break;//if we did all that,there is no need to continue the loop,we are ready
		}
	}
}
function setDefaultTextPhone()
{

	var phoneText=document.body.innerHTML;
	//we search if text phone number changed,and if changed we set its default value
	for(var i=0;i<ref_list.length;i++)
	{
		phoneText=phoneText.replace(phone_numbers[i],phone_numbers[0]);
		
	}
	document.body.innerHTML=phoneText;
}
function set_cookie(c_name,value,expiresecs,vdomain)//function for seting the coockie,c_name:coockie name,value:the value (referrer),expiresecs: the seconds we want the coockie to stay valid,vdomain:the valid domain e.g domain.com to include subdomains
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiresecs);
	document.cookie=c_name+ "=" +escape(value)+
	((expiresecs==null) ? "" : ";max-age="+expiresecs)+"; path=/"+"; domain="+vdomain;
}

function get_cookie(c_name)//function tha returns the value of the coockie with the given name(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 "";
}



