// ********* start of script for cookie stuff ************
var CookieCode 
var expDate, fDate
var update = false
var emailcode = ""
var ipAddress = ""
var expDateWork

// The following code is for ppc and affiliate landing pages only
if (LinkCode != HomeCode) 
	{
	expDate = new Date()
	logDate = expDate.getTime()
	fDate = expDate.getTime() + (180 * 24 * 60 * 60 * 1000)  // Set to 180 days in future
	expDate.setTime(fDate)
//	document.cookie = "LinkCode=" + LinkCode.toString() + "; expires = " + expDate.toGMTString()

	var CookieDirectory = "/"
	var CookieDomain = ".ptpinsuranceagents.com"
    document.cookie = "LinkCode=" + LinkCode.toString() + "; expires = " + expDate.toGMTString() + '; path=' + CookieDirectory + '; domain=' + CookieDomain 
	}



// alert ("Cookie will be displayed next.");
// document.write("<b>" + document.cookie + "</B>");





// end of cookie update for ppc landing

CookieCode = get_cookie("LinkCode")
var toStringWork = CookieCode.toString();
if (toStringWork == "") 
  { CookieCode = HomeCode }


// get a time stamp
var Stamp = ""
var dateStamp = ""
var dateMDY = ""

Stamp = new Date()
var yearStamp = Stamp.getYear()
if (yearStamp < 1000) { yearStamp += 1900 }
var monthStamp = (Stamp.getMonth() + 1)
if (monthStamp < 10) { monthStamp = "0" + monthStamp }
var daymStamp = Stamp.getDate()
if (daymStamp < 10) { daymStamp = "0" + daymStamp }
dateStamp = yearStamp + "^" + monthStamp + "^" + daymStamp + "^"
dateStamp += Stamp.getHours() + ":" + Stamp.getMinutes() + ":" + Stamp.getSeconds()
dateMDY = monthStamp + "/" + daymStamp + "/" + yearStamp
expDateWork = dateStamp


function submitForm()
{
if (CookieCode.toString() == "") 
  { update = true }
// Always overwrite cookie if the new value is pay per click or other paid type 
// generated, i.e. the new value is not home code.
else if ( (LinkCode.toString() != HomeCode.toString())  ) 
  { update = true } 
if (update)
  { expDate = new Date()

    fDate = expDate.getTime() + (180 * 24 * 60 * 60 * 1000)  // Set to 180 days in future
    expDate.setTime(fDate)
    document.cookie = "LinkCode=" + LinkCode.toString() + "; expires = " + expDate.toGMTString()
  }
return;
}


// Remove the spaces in a field
function ignoreSpaces(string) {
var temp = "";
string = '' + string;
splitstring = string.split(" ");
for(i = 0; i < splitstring.length; i++)
temp += splitstring[i];
return temp;
}


//Get cookie routine  
function get_cookie(Name) 
{
  var search = Name + "="
  var returnvalue = "";
  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    // if cookie exists
    if (offset != -1) { 
      offset += search.length
      // set index of beginning of value
      end = document.cookie.indexOf(";", offset);
      // set index of end of cookie value
      if (end == -1) end = document.cookie.length;
      returnvalue=unescape(document.cookie.substring(offset, end))
      }
   }
   if (returnvalue == "") {returnvalue = HomeCode }
  return returnvalue;
}
// ********* end of script for cookie stuff ************

// ********* start of script for building the lead email message ************
function InArray(theArray, itemval)
{ /* InArray */
  var index, GotIt
  GotIt = false
  for(index=0; index < theArray.length; index++)
    { if (itemval.toString() == theArray[index].name.toString())
	    { GotIt = true; break }
	}
	
  return GotIt
} /* InArray */ 

function submitData(theform)
{ /* submitData */
  var errormsg = ""
  var theData = ""
  var NoErrors = true

var sellmethod = "no"
theData += "Get a quote for:  "
if (document.dataForm.optPauto.checked)   
     { sellmethod = "ok"   ;
     theData += "Personal Auto - "
     }
if (document.dataForm.optPhealth.checked)   
     { sellmethod = "ok"   
     theData += "Personal Health - "
     }
if (document.dataForm.optPhome.checked)   
     { sellmethod = "ok"   
     theData += "Personal Homeowners - "
     }
if (document.dataForm.optPlife.checked)   
     { sellmethod = "ok"   
     theData += "Personal Life - "
     }
if (document.dataForm.optPmortgage.checked)   
     { sellmethod = "ok"   
     theData += "Personal Mortage Protection - "
     }
if (document.dataForm.optPrenters.checked)   
     { sellmethod = "ok"   ;
     theData += "Personal Renters Insurance - "
     }
if (document.dataForm.optPumbrella.checked)   
     { sellmethod = "ok"   
     theData += "Personal Umbrella - "
     }
if (document.dataForm.optCauto.checked)   
     { sellmethod = "ok"   
     theData += "Commercial Auto - "
     }
if (document.dataForm.optCgeneral_liability.checked)   
     { sellmethod = "ok"   
     theData += "Commercial General Liability - "
     }
if (document.dataForm.optCcontractors_liability.checked)   
     { sellmethod = "ok"   
     theData += "Commercial Contractors Liability - "
     }
if (document.dataForm.optCbuilders_risk.checked)   
     { sellmethod = "ok"   ;
     theData += "Commercial Builders Risk - "
     }
if (document.dataForm.optCbusiness_owners.checked)   
     { sellmethod = "ok"   
     theData += "Commercial Business Owners Policies - "
     }
if (document.dataForm.optCgroup_life.checked)   
     { sellmethod = "ok"   
     theData += "Commercial Group Life - "
     }
if (document.dataForm.optCgroup_health.checked)   
     { sellmethod = "ok"   
     theData += "Commercial Group Health - "
     }
if (document.dataForm.optCworker_comp.checked)   
     { sellmethod = "ok"   
     theData += "Commercial Workers Comp - "
     }
if (document.dataForm.optCbonds.checked)   
     { sellmethod = "ok"   ;
     theData += "Commercial Bonds - "
     }

if (sellmethod == "no")  
     { errormsg += "Please check at least one type of policy for a quote." + "\n"}
theData += "\n \n"

  if (InArray(theform.elements,"firstname"))
    { if (theform.firstname.value == "") 
        { errormsg += "You must give us your first name." + "\n";
	      NoErrors = false
	    }
	  else if (NoErrors)
	    { theData += "First Name:        " + theform.firstname.value + "\n" }
    }
     
  if (InArray(theform.elements,"lastname"))
    { if (theform.lastname.value == "") 
        { errormsg += "You must give us your last name." + "\n";
	      NoErrors = false
	    }
	  else if (NoErrors)
	    { theData += "Last Name:         " + theform.lastname.value + "\n" }
    }     
	 
  if (InArray(theform.elements,"address"))
    { if (theform.address.value == "") 
        { errormsg += "You must give us your address." + "\n";
	      NoErrors = false
	    }
	  else if (NoErrors)
	    { theData += "Address:           " + theform.address.value + "\n" }
    } 	 
	 
  if (InArray(theform.elements,"city"))
    { if (theform.city.value == "") 
        { errormsg += "You must give us your city." + "\n";
	      NoErrors = false
	    }
    }	
	
  if (InArray(theform.elements,"state"))
    { if (theform.state.selectedIndex == 0) 
        {  errormsg += "You must give us your state." + "\n";
	      NoErrors = false
	    }
    }	 
	 
  if (InArray(theform.elements,"zip"))
    { if (theform.state.selectedIndex == 0) 
        {  errormsg += "You must give us your zip." + "\n";
	      NoErrors = false
	    }
    }
	
  if (NoErrors)
    { if ((InArray(theform.elements,"city")) && (InArray(theform.elements,"state")) && (InArray(theform.elements,"zip")))
	    { theData += "                   " + theform.city.value + ", " + theform.state.options[theform.state.selectedIndex].value + " " + theform.zip.value + "\n" }

	theform.Subject.value = "PTP Insurance Inq / " + theform.lastname.value + " / " + theform.city.value + " / " + theform.state.options[theform.state.selectedIndex].value;
	}
	
  if (InArray(theform.elements,"phone"))
    { if (theform.phone.value == "") 
        {  errormsg += "You must give us your phone number." + "\n";
	      NoErrors = false
	    }
    }	
		 
  if (NoErrors)
    { if (InArray(theform.elements,"phone"))
	    { theData += "Phone:             " + theform.phone.value + "\n" }
	}	
	
  if (InArray(theform.elements,"besttime") && NoErrors)
    { theData += "Best time to call: " + theform.besttime.value + "\n" + "\n"}		
	
  if (InArray(theform.elements,"email"))
    { if (theform.email.value == "") 
        { errormsg += "You must give us your email address." + "\n";
	      NoErrors = false
	    }
	  else if (NoErrors)
	    { theData += "Email address:     " + theform.email.value + "\n" + "\n"  
           emailcode = theform.email.value;
         }
    }	
	
  if (NoErrors)
    { if ((InArray(theform.elements,"fullName")) && (InArray(theform.elements,"lastname")) && (InArray(theform.elements,"firstname")))
	    { theform.fullName.value = theform.lastname.value + " " + theform.firstname.value; }
	}		
	
  // NOTE: CookieCode is defined outside and prior to this function.
  //       Also, Source Code (CookieCode) is appended to theData 
  //       whether or not it is a form element.	
  if (NoErrors)
    { submitForm();
            if (InArray(theform.elements,"srcCode"))
               { theform.srcCode.value = CookieCode }
	       theData += "Source Code:       " + CookieCode + "\n"
	}
  	
	
  if (errormsg != "")  
    { alert ("Data is missing. Please fill in all required fields." + "\n" + errormsg);
	  return false
    }
  else
    {
    theform.theData.value = theData 
    var logWork = expDateWork + "^" + dateMDY + "^" + CookieCode + "^" + ipAddress + "^" + emailcode
    logWork += "^" + theform.lastname.value
    logWork += "^" + theform.firstname.value
    logWork += "^" + theform.city.value
    logWork += "^" + theform.state.options[theform.state.selectedIndex].value
    theform.logFile.value = logWork
	
    return true
    }
}  
// **************end of script for building the lead email message **********************
