/* === font size and stylesheet stuff === */

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

//var cookie = readCookie("style");
//var title = cookie ? cookie : getPreferredStyleSheet();
//setActiveStyleSheet(title);

window.onload = function(e) {
  var cookie = readCookie("button");
  var buttonID = cookie ? cookie : "font80";
  document.getElementById(buttonID).src = "img/" + buttonID + "_on.gif";
  
  cookie = readCookie("size");
  var fontsize = cookie ? cookie : "80%";
  changeSize2(fontsize, buttonID);
    
  cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var fontsize = document.body.style.fontSize;
  createCookie("size", fontsize, 365);
  
  var buttonID = "font" + fontsize.replace("%","");
  createCookie("button", buttonID, 365);
  
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

function changeSize(val,button,x,y,z)
{
  document.body.style.fontSize = val;
  document.getElementById(button).src = "img/" + button + "_on.gif";
  document.getElementById(x).src = "img/" + x + "_off.gif";
  document.getElementById(y).src = "img/" + y + "_off.gif";
  document.getElementById(z).src = "img/" + z + "_off.gif";
 }

function changeSize2(val,button)
{
  document.body.style.fontSize = val;
  document.getElementById(button).src = "img/" + button + "_on.gif";
}


function validate(theForm) {


	if (theForm.FirstName.value == "")
		{
		alert("Please provide First Name");
		theForm.FirstName.focus();
		return (false);
		}
	
	if (theForm.LastName.value == "")
		{
		alert("Please provide Last Name");
		theForm.LastName.focus();
		return (false);
		}
	
	if (theForm.Address1.value == "") {
		alert("Please provide: Address");
		theForm.Address1.focus();
		return (false);
	}
	
	if (theForm.City.value == "") {
		alert("Please provide: City");
		theForm.City.focus();
		return (false);
	}
	
	if (theForm.State.value == "") {
		alert("Please provide: State");
		theForm.State.focus();
		return (false);
	}
	
	if (theForm.PostalCode.value == "") {
		alert("Please provide: Postal Code");
		theForm.PostalCode.focus();
		return (false);
	}
	
	if (theForm.Country.value == "") {
		alert("Please provide: Country");
		theForm.Country.focus();
		return (false);
	}
		
	if (theForm.EmailAddress.value == "") {
		alert("Please enter your email address.");
		theForm.EmailAddress.focus();
		return (false);
	}
	var checkEmailAddress = "@.";
	var checkStr = theForm.EmailAddress.value;
	var EmailAddressValid = false;
	var EmailAddressAt = false;
	var EmailAddressPeriod = false;
	for (i = 0;  i < checkStr.length;  i++)	{
		ch = checkStr.charAt(i);
			for (j = 0;  j < checkEmailAddress.length;  j++) {
				if (ch == checkEmailAddress.charAt(j) && ch == "@")
					EmailAddressAt = true;
				if (ch == checkEmailAddress.charAt(j) && ch == ".")
					EmailAddressPeriod = true;
				if (EmailAddressAt && EmailAddressPeriod)
				break;
				if (j == checkEmailAddress.length)
				break;
			}
		if (EmailAddressAt && EmailAddressPeriod) {
			EmailAddressValid = true
			break;
		}
	}
	
	if (!EmailAddressValid) {
		alert("The email you entered is invalid. It must contain an \"@\" and a \".\"");
		theForm.EmailAddress.focus();
		return (false);
	}
	
		
	if (theForm.Phone.value == "") {
		alert("Please provide: Phone");
		theForm.Phone.focus();
		return (false);
	}
	
	if (theForm.Profession) {
		if (typeof(theForm.Profession) != "undefined") {
			if (theForm.Profession.selectedIndex < 0)
			{
			alert("Please select your profession");
			theForm.Profession.focus();
			return (false);
			}
			if (theForm.Profession.selectedIndex == 0)
			{
			alert("Please select your profession");
			theForm.Profession.focus();
			return (false);
			}
		}
	}
	
	if (theForm.FamiliarWithAdvances) {
		if (typeof(theForm.FamiliarWithAdvances) != "undefined") {
			var radioSelected = false;
			for (i = 0;  i < theForm.FamiliarWithAdvances.length;  i++)
			{
			if (theForm.FamiliarWithAdvances[i].checked)
			radioSelected = true;
			}  
			if (!radioSelected)
			{
			alert("Are you familiar with the advances in the life settlement industry?");
			return (false);
			}
		}	
	}
	
	if (theForm.ContactBy) {
		if (typeof(theForm.ContactBy) != "undefined") {
			var radioSelected = false;
			for (i = 0;  i < theForm.ContactBy.length;  i++)
			{
			if (theForm.ContactBy[i].checked)
			radioSelected = true;
			}  
			if (!radioSelected)
			{
			alert("Please choose how you would like to be contacted.");
			return (false);
			}
		}	
	}
}