// JavaScript Document
// -----------------------------------------------
// Begin cookie functions
// -----------------------------------------------

// getCookieData() function is called from other cookie functions
// Takes cookie label as argument, returns cookie value or null
// -----------------
function getCookieData(label) {
    var labelLen = label.length
    var cLen = document.cookie.length
    var i = 0
    var cEnd
    while (i < cLen) {
        var j = i + labelLen
        if (document.cookie.substring(i,j) == label) {
            cEnd = document.cookie.indexOf(";",j)
            if (cEnd == -1) {
                cEnd = document.cookie.length
            }
            return unescape(document.cookie.substring(j,cEnd))
        }
        i++
    }
    return null
}

// loadCookieData() function is called from page with the contact form
// Populates hidden form fields in the first form of the page
// -----------------
function loadCookieValues()
{
if (getCookieData("cookieRefURL"))
{
  var field1 = document.forms[0].cRefURL
  field1.value = getCookieData("cookieRefURL")
  var field2 = document.forms[0].cLandURL
  field2.value = getCookieData("cookieLandURL")
  var field3 = document.forms[0].cDate
  field3.value = getCookieData("cookieDate")
}
}

// cookieTreat() function is called from every page on the site
// Checks if cookies exist, if not - three cookie values are written
// -----------------
function cookieTreat()
{
var exp = new Date()
var oneYearFromNow = exp.getTime() + (365 * 24* 60 * 60 * 1000)
exp.setTime(oneYearFromNow)

var today = new Date()

var checkref = new String(" ")
if(document.referrer.length > 0)
  {
  checkref = checkref + document.referrer
  }
else
  {
  checkref = checkref + "No Referrer"
  }

// set document cookie if they have not been written yet
// if referrer cookie exists, do nothing
// otherwise write all three cookies

if (getCookieData("cookieRefURL") && getCookieData("cookieLandURL") && 
getCookieData("cookieDate")) {
}
else {
  document.cookie = "cookieRefURL=" + checkref + ";domain=.phoenixconstructionresources.com; expires=" + exp.toGMTString() + ";path=/";
  document.cookie = "cookieLandURL=" + location + "; domain=.phoenixconstructionresources.com; expires=" + exp.toGMTString() + "; path=/";
  document.cookie = "cookieDate=" + today + "; domain=.phoenixconstructionresources.com; expires=" + exp.toGMTString() + "; path=/";
}
}
cookieTreat();
// -----------------------------------------------
// End cookie functions
// -----------------------------------------------