// This file includes all the objects(functions) needed for the website functionality



// -------------------------------------------------------------------------------------------------------------------------------------------


// Functions used for the leadpile design


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function showOne(id) {
	for (i = 1; i <= 15; i++) {
		document.getElementById("ans" + i).style.display = (id == i ? "block" : "none");
	}
}

// setPage() will use the variable(s) passed in the () to change the content of the elements in the html page; it returns false to prevent the html attempt to change the location when the function is called from an anchor.

function seller_inquiry() {
	document.contact_form.message.value='Company: '+contact_form.company.value+"\n"+'Website: '+contact_form.website.value+"\n"+'Name: '+contact_form.sender_name.value+"\n"+'Email: '+contact_form.sender_email.value+"\n"+'Address: '+contact_form.address.value+"\n"+'City: '+contact_form.city.value+"\n"+'State: '+contact_form.state.value+"\n"+'ZIP: '+contact_form.zip.value+"\n"+'Phone: '+contact_form.phone1.value+'-'+contact_form.phone2.value+'-'+contact_form.phone3.value+"\n"+"\n"+'What type of leads do you specialize in?'+"\n"+contact_form.nr1.value+"\n"+"\n"+'How many leads per day do you currently produce that can be placed in the Leadpile System?'+"\n"+contact_form.nr2.value+"\n"+"\n"+'Are you currently selling leads?'+"\n"+contact_form.nr3.value+"\n"+"\n"+'Is your business model 100% selling leads?'+"\n"+contact_form.nr4.value+"\n"+"\n"+'What price point are currently selling your leads at?'+"\n"+contact_form.nr5.value+"\n"+"\n"+'Comments:'+"\n"+contact_form.comments.value+"\n"+"\n"+'Site: '+contact_form.site.value+"\n"+"\n";
}


function setPage( element1, content1, element2, content2, element3, content3, element4, content4, element5, content5 ) {



	if (( content1 ) && ( element1 )) document.getElementById(element1).innerHTML = content1;

	if (( content2 ) && ( element2 )) document.getElementById(element2).innerHTML = content2;

	if (( content3 ) && ( element3 )) document.getElementById(element3).innerHTML = content3;

	if (( content4 ) && ( element4 )) document.getElementById(element4).innerHTML = content4;

	if (( content5 ) && ( element5 )) document.getElementById(element5).innerHTML = content5;



	return false;

}



// -------------------------------------------------------------------------------------------------------------------------------------------



// trackOrigin() will save the new visitor's tracking information as cookies and call the server script to record it in a database. The function needs the cookies manipulation functions too



function trackOrigin( site, trackingElement, recordingScript ) {



	// set the cookie lifespan; this will result in having the cookie phisically writen on the client machine

	var expDays = 360;

	var expDate = new Date();

	expDate.setTime(expDate.getTime() +  (24 * 60 * 60 * 1000 * expDays));



	// separate the elements found in the querystring into pairs and look for the "Source" value

	var pairs = location.search.substring(1, location.search.length).split("&");

	var newSource = "";

	for (var i=0; i<pairs.length; i++) {

		if (pairs[i].indexOf("Source=") != -1) {

			newSource = pairs[i].substring(pairs[i].indexOf("=")+1, pairs[i].length);

		}

	}



	// get the page referrer

	var newReferrer = document.referrer;

	var oldReferrer = getCookie('Referrer');



	// check if the visitor is new and record the new visit

	var oldSource = getCookie('Source');



	if((newReferrer.indexOf("leadpile.com/") == -1)&&(newReferrer != "")&&(oldReferrer != newReferrer)) {

		setCookie('Referrer', newReferrer, null, '/');

		//document.getElementById(trackingElement).innerHTML="<img src=http://www.mortgageloanpage.com/cgi-bin/trackVisitor.pl?Site=" + site + "&Source=" + newSource + "&Referrer=" + newReferrer + " height=1 width=1>";

	}



	if (( newSource != oldSource ) && ( newSource != "" )) {

		setCookie('Site', site, null, '/');

		setCookie('Source', newSource, null, '/');

	}

}



// -------------------------------------------------------------------------------------------------------------------------------------------



// The following 3 functions manipulate cookies



function getCookie(name){

  var cname = name + "=";

  var dc = document.cookie;

  if (dc.length > 0) {

    begin = dc.indexOf(cname);

    if (begin != -1) {

      begin += cname.length;

      end = dc.indexOf(";", begin);

      if (end == -1) end = dc.length;

        return unescape(dc.substring(begin, end));

    }

  }

  return null;

}



function setCookie(name, value, expires, path, domain, secure) {

  document.cookie = name + "=" + escape(value) +

  ((expires == null) ? "" : "; expires=" + expires.toGMTString()) +

  ((path == null) ? "" : "; path=" + path) +

  ((domain == null) ? "" : "; domain=" + domain) +

  ((secure == null) ? "" : "; secure");

}



function delCookie(name,path,domain) {

  if (getCookie(name)) {

    document.cookie = name + "=" +

    ((path == null) ? "" : "; path=" + path) +

    ((domain == null) ? "" : "; domain=" + domain) +

    "; expires=Thu, 01-Jan-70 00:00:01 GMT";

  }

}



// -------------------------------------------------------------------------------------------------------------------------------------------



// validateForm() ensures that there is an entry for each field



function validateForm(form)

{

	for(var i=0; i<form.elements.length; i++) {

		if (form.elements[i].value == "") {

			alert("Please complete the missing information.");

			form.elements[i].focus();

			return false;

		}

	}

	return true;

}



// -------------------------------------------------------------------------------------------------------------------------------------------



// countCharacters() limits the space available in a textArea input with the value passed to the function, and, if requested, displays the remaining space available



function countCharacters(field, length, element) {



	var remaining = length - field.value.length;

	if (remaining<1) {

		field.value = field.value.substring(0, length);

		remaining = 0;

	}



	if(element) document.getElementById(element).innerHTML = "Remaining space available: " + remaining + " characters";

}



// -------------------------------------------------------------------------------------------------------------------------------------------



// locatePage() returns the page requested in the query string; the home page is used as default



function locatePage( searchString ) {



	var pairs = searchString.substring(1, searchString.length).split("&");

	for (var i=0; i<pairs.length; i++) {

		var pair = pairs[i].split("=");

		switch (pair[0]) {

			case ('Page'):

				return pair[1];

			default:

				return 'HomePage';

		}

	}

}



// -------------------------------------------------------------------------------------------------------------------------------------------


// Current Date

var myMonths=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var myDays= new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat","Sun");
today=new Date();
thisDay=myDays[today.getDay()]
thisMonth=myMonths[today.getMonth()]
thisYear=today.getFullYear()
thisDate=today.getDate()
switch (thisDate) {
case 1:
dateSuffix="st"
break
case 21:
dateSuffix="st"
break
case 31:
dateSuffix="st"
break
case 2:
dateSuffix="nd"
break
case 22:
dateSuffix="nd"
break;
case 3:
dateSuffix="rd"
break
case 23:
dateSuffix="rd"
break
default:
dateSuffix="th"
}
todaysDate=thisDay+", "+thisMonth+" "+thisDate+", "+thisYear
function myClock(){
today=new Date()
var theHours=today.getHours()
if (theHours>11)
{
theTimeSuffix="PM"
}
if (theHours>12)
var theHours=theHours-12
else
{
theTimeSuffix="AM"
}
var theMinutes=today.getMinutes()
if (theMinutes<10)
var theMinutes="0"+theMinutes
var theSeconds=today.getSeconds()
if (theSeconds<10)
var theSeconds="0"+theSeconds
var theTimeNow=theHours+":"+theMinutes+":"+theSeconds+" "+theTimeSuffix
}
var clockWork=setInterval("myClock()",1000)


// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------


function trackLead() {
	// set the cookie lifespan; this will result in having the cookie phisically writen on the client machine
	expDays = 30;
	expDate = new Date();
	expDate.setTime(expDate.getTime() + (24 * 60 * 60 * 1000 * expDays));

	function getVariable(name) {
		re = new RegExp('[&?]' + name + '=([^&]*)', 'i');
		matches = re.exec(window.location);
		if (matches) {return matches[1]};
		return '';
	}	

	// get the tracking info
	site = getVariable('site');
	source = getVariable('source');
	campaign = getVariable('campaign');
	ad = getVariable('ad');
	referrer = document.referrer;

	if (!site) { site = 'leadpile.com'; }

	// check if the visitor is new and record the new visit
	if (source) {
		setCookie('site', site, expDate, '/');
		setCookie('source', source, expDate, '/');
		setCookie('campaign', campaign, expDate, '/');
		setCookie('ad', ad, expDate, '/');
		setCookie('referrer', referrer, expDate, '/');
	}
}
