/*

Easy to use.

1.	Place <script language="JavaScript" type="text/javascript" src="dphone.js"></script> in the <head> 
	of the page loading the dynamic phone numbers.
2.	Use any tag like <div>, <span>, even <strong> should work, and use the ID "defaultPhone".
3.	In any URL, just add at the end: ?phn=(xxx)-xxx-xxxx or #phn=(xxx)-xxx-xxxx

	Example: http://www.domain.com/path/file.html#phn=1-(900)-HOT-CALL

4.	Open dphone.js and edit the line:

		var timeExpire = 1;

	This is the number of hours the cookie will remain in the browser memory before it gets erased.
	Thus this is how long the phone number appears on the visitor's browser.
5.	If you wish to have an override of the default hours, you can add in the URL &h=X where X is the 
	number of hours.

	Example: http://www.domain.com/path/file.html?phn=1-(900)-CALL-MEH&h=8
	
Notes:
	- Variable order is not important, it will work both ways.
		http://www.domain.com/path/file.html?phn=1-(900)-CALL-MEH&h=8
		and
		http://www.domain.com/path/file.html?h=8&phn=1-(900)-CALL-MEH
		will both work.

	- Both GET Variable and page anchor will work.
		http://www.domain.com/path/file.html?phn=1-(900)-CALL-MEH
		http://www.domain.com/path/file.html#phn=1-(900)-CALL-MEH
		will both work.
		Probably, use the "#" more to avoid SEO duplicate content issues. Just use "?" if you need the 
		GET variables for other purposes such as other serverside scripting purposes.
		
	- Unique tracking codes for web analytics purposes
		If you still need to run unique tracking codes for analytics purposes, you may separate the
		variable groups with an ampersand (&) just like any other GET variable.
			http://www.domain.com/path/file.html?phn=1-(900)-CALL-MEH&h=5&trackingcodevar=54378439067891780

	- The default phone number in your HTML tag, would be ideal to be the same number found in online
		directories, such as YELP, CitySearch, MerchantsCircle etc. and Google Local Business Center since
		Google checks the consistency of information and helps in the ranking in local search. Google will see
		the default number, but will not see the JavaScript number.

	#####################################################################
	dPhone.js - Dynamic phone number display by JavaScript. Be able to 
	control displayed text phone numbers on webpages for better online 
	call tracking.
    Copyright (C) 2009 Benj Arriola

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

	Benj Arriola
	http://www.benjarriola.com
	http://linkedin.com/in/benjarriola

*/

var timeExpire = 1;

function createCookie(name,value,hours) {
	if (hours) {
		var date = new Date();
		date.setTime(date.getTime()+(hours*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);
}

window.onload = writeMessage;
function writeMessage(){
    var phnNumber = window.location.href.slice(window.location.href.indexOf('phn=') + 4).split('&');
    var timeExpireURL = window.location.href.slice(window.location.href.indexOf('h=') + 2).split('&');
	phnNumber = phnNumber[0].replace('%20', ' ');
	if(window.location.href.indexOf('phn=')!= -1){
		document.getElementById("defaultPhone").innerHTML = phnNumber;

		if(window.location.href.indexOf('h=')!= -1){
			timeExpire = timeExpireURL;
		}
		createCookie('storedPhnNumber',phnNumber,timeExpire);
	} else {
		phnNumber = readCookie('storedPhnNumber');
		if (phnNumber) {
			document.getElementById("defaultPhone").innerHTML = phnNumber;

		} 
	}
}

// These do not work. They will erase the cookie once you visit a new page. Do not even consider.
// window.unload = eraseCookie('storedPhnNumber');
// onbeforeunload = eraseCookie('storedPhnNumber');