//----------------------------------------------------------------------
//----------------------------------------------------------------------
// DO NOT edit below this line !!!
//----------------------------------------------------------------------
//----------------------------------------------------------------------
/*	_psw__functions.js
* by Jordan Levy Painesville Ohio (www.zajon.com). v1.001
* Copyright (c) 2007 NetTrack Marketing. All Rights Reserved.
*
* This code has been licensed for use on this site, and may not be reused or resold.
*
* 
*/
//----------------------------------------------------------------------
//----------------------------------------------------------------------
// Array of Querystring and value pairs
// access a quary string parameter value _psw__qsParam['q1Name']   :   file.asp?q1Name=theVal
var _psw__qsParam = new Array();
var _psw__foundOne = false;

//----------------------------------------------------------------------
// Executed onLoad
function _psw__swapTheTextGo(swapID_psw, qsParamListAray_psw, urlReferrerListAray_psw)
{
	_psw__foundOne = false; 
	_psw__checkCookie(swapID_psw);
	if (typeof qsParamListAray_psw != 'undefined' &&  _psw__foundOne == false )  { _psw__checkQueryString(swapID_psw, qsParamListAray_psw);}
	if (typeof urlReferrerListAray_psw != 'undefined' &&  _psw__foundOne == false ) { _psw__checkReferer(swapID_psw, urlReferrerListAray_psw); }	
}
//----------------------------------------------------------------------
function _psw__checkCookie(swapID_psw)
{	// Look for cookie with Phone number
	if(_psw__getCookie(swapID_psw) != null)
	{
			swapTextInElemID(_psw__getCookie(swapID_psw) , swapID_psw) ;
			_psw__foundOne = true;
	}
}
//----------------------------------------------------------------------
function _psw__checkQueryString(swapID_psw, qsParamListAray_psw)
{	// now check Query string for parameter names (and values)
	// set values of QString parameters we are intersted (from qsParamListAray_psw) in to NULL
	for (var j = 0 ; j < qsParamListAray_psw.length  ; j++) 
	{
		_psw__qsParam[qsParamListAray_psw[j][0]] = null ;// init QuueryString var
	}
	_psw__getQSParam(); // GET QUERY STRING and place in _psw__qsParam array	
	// Loop through our phoneNumber list and swap out the first match we get to in our querystring
	var i = 0;
	for (var j = 0 ; j < qsParamListAray_psw.length  ; j++) 
	{
		i = 0;
		if (_psw__qsParam [qsParamListAray_psw[j][0]] != null )
		{
			// If qsParamListAray_psw[j].length > 2  we have a list of values to check for the parameter
			if ( qsParamListAray_psw[j].length > 2 )
			{
				for (i = 1 ; i < qsParamListAray_psw[j].length -1  ; i++) 
				{
//					if ( _psw__qsParam [qsParamListAray_psw[j][0]].toLowerCase().indexOf(qsParamListAray_psw[j][i].toLowerCase()) >=0 )  // param value contains element in array
					if ( _psw__qsParam [qsParamListAray_psw[j][0]] == qsParamListAray_psw[j][i] ) // param value == element in the array
					{					
						swapTextInElemID(qsParamListAray_psw[j][qsParamListAray_psw[j].length -1 ], swapID_psw) ; // qsParamListAray_psw[j].length -1  points to last element of array and it should be the phone number
						_psw__setCookie(swapID_psw,qsParamListAray_psw[j][qsParamListAray_psw[j].length -1 ])
						_psw__foundOne = true;
						return;
					}
				}

			}
			else
			{				
				swapTextInElemID(qsParamListAray_psw[j][1], swapID_psw) ;
				_psw__setCookie(swapID_psw,qsParamListAray_psw[j][1])
				_psw__foundOne = true;
				return;
			}
		}
	}
}
//----------------------------------------------------------------------
function _psw__checkReferer(swapID_psw, urlReferrerListAray_psw)
{	// Then look at referring URL
	for (var j = 0 ; j < urlReferrerListAray_psw.length  ; j++) 
	{
		if ( document.referrer.toLowerCase().indexOf(urlReferrerListAray_psw[j][0].toLowerCase()) >=0 )
		{
			swapTextInElemID(urlReferrerListAray_psw[j][1], swapID_psw) ;
			_psw__setCookie(swapID_psw,urlReferrerListAray_psw[j][1])
			_psw__foundOne = true;
			return; 
		}
	}
}

//----------------------------------------------------------------------
function _psw__getQSParam() 
{	// Get Querystring an place in the array _psw__qsParam
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	for (var i=0; i<parms.length; i++) 
	{
		var pos = parms[i].indexOf('=');
		if (pos > 0) {
			var key = parms[i].substring(0,pos);
			var val = parms[i].substring(pos+1);
			_psw__qsParam[key] = val;
		}
	}
} 
//----------------------------------------------------------------------
// BASIC innerHTML Swap used to replace text
	function swapTextInElemID(sText, theID) 
	{
		var swapObj = document.getElementById(theID);
		if (swapObj != null)
			swapObj.innerHTML = sText;
	}
//----------------------------------------------------------------------
//----------------------------------------------------------------------
// COOKIES
/*
   name - name of the cookie
   value - value of the cookie
   [days] - expiration # days from today of the cookie
     (defaults to end of current session)
   * an argument defaults when it is assigned null as a placeholder
   * a null placeholder is not required for trailing omitted arguments
*/
//----------------------------------------------------------------------

function _psw__setCookie(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=/";
  
}

//----------------------------------------------------------------------
/*
  name - name of the desired cookie
  return string containing value of specified cookie or null
  if cookie does not exist
*/

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