  var cName = "refCookie";
  var refNum = "";
  
// Function called from web page which ultimately returns the appropriate telephone number
function getRefNumber() {

var refNum = getParamValue('unqid');

if (refNum == "") {

refNum = checkCookie();

	if (refNum == null) {
	refNum = getAlternativeRef();
	}

} 

else {

     setCookie(cName, refNum, 365);
}

return refNum;

}

function getAlternativeRef() {

var altNum = "z112233";
return altNum;
}

// Telephone Tracking: Checks to see if a relevant cookie exists, if there is not then information is passed to the setCookie function. Ultimately returns the tracking number to be displayed on the site.
function checkCookie() {

  var cookieValue = getCookie(cName);
  var cookieNum = cookieValue;
 
  return cookieNum;

}


// Telephone Tracking: Loops through the cookies on the users machine which are related to the website in order to retrieve the cookie related to telephone tracking. Returns this cookie to the checkCookie function.
function getCookie(cookieName) {
	
	//retrieved in the format
	//cookieName4=value; cookieName3=value; cookieName2=value; cookieName1=value
	//only cookies for this domain and path will be retrieved
	var cookieJar = document.cookie.split( "; " );
	for( var x = 0; x < cookieJar.length; x++ ) {
		var oneCookie = cookieJar[x].split( "=" );
		if( oneCookie[0] == escape( cookieName ) ) { return oneCookie[1] ? unescape( oneCookie[1] ) : ''; }
	}
	return null;
}

// Telephone Tracking: Function is called upon if no telephone tracking cookie exists on the user's machine. It creates the telephone tracking cookie and returns the telephone number to be displayed on the users screen to the checkCookie function.
function setCookie(cName, cNumber, cExpire) {

  var myDate = new Date();
  myDate.setDate(myDate.getDate() + cExpire);
  document.cookie = cName + "=" + escape(cNumber) + ";expires=" + myDate + "; path=/";
}

// Telephone Tracking: Function takes in a parameter name from the current URL displayed on the users screen and returns its value to the checkCookie function. 
function getParamValue(paramName) {  

  paramName = paramName.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
  var regexS = "[\\?&#_:+!.]"+paramName+"=([^&#_:+?!.]*)";  
  var regex = new RegExp( regexS );  
  var results = regex.exec( window.location.href ); 

  if(results == null) 
    return "";
  else 
   
  return results[1];

}



