// This file includes all the objects(functions) needed for the website functionality



// -------------------------------------------------------------------------------------------------------------------------------------------



// 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 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("aolexpert.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 validateContactForm(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';

		}

	}

}



// -------------------------------------------------------------------------------------------------------------------------------------------





