// JavaScript Document

function clearFocus(theText) {
	if (theText.value == theText.defaultValue) {
		theText.value = "";
		theText.style.color = "#356480";
	}
}

function resetBlur(theText) {
	if (theText.value == "") {
		theText.value = theText.defaultValue;
		theText.style.color = "#356480";
	}
}

//Browser Support Code
function ajaxFunction(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 1)
		{
		var ajaxDisplay = document.getElementById('ajaxDiv');
		ajaxDisplay.innerHTML = "<div id='ajax'><img src='/images/ajax-loader.gif'><h2 style='padding:7px 0px 0px 40px;'>Loading...</h2></div>";
		}
		else if(ajaxRequest.readyState == 4){
			recordIt('', 'Contact Us - Short Form', 'lead');
			var ajaxDisplay = document.getElementById('ajaxDiv');
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
		}
	}
	var name = document.getElementById('name');
	var email = document.getElementById('email');
	var phone = document.getElementById('phone');
	var captcha = document.getElementById('captcha');
	if (validate(name,phone,email,captcha)) {
	var queryString = "?name=" + name.value + "&email=" + email.value + "&phone=" + phone.value + "&captcha=" + captcha.value;
	ajaxRequest.open("GET", "ajax-form-process.php" + queryString, true);
	ajaxRequest.send(null); 
	}
}

function validate(name, phone, email, captcha) {
	var why = "";
	var elem;
	var emailFilter=/^.+@.+\..{2,3}$/;
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
	
	elem = name;
	if (elem) {
		if (elem.value=='' || elem.value==elem.defaultValue) {
			why += "Please enter your name.\n";
		}
	}
	
	elem = phone;
	if (elem) {
		if (elem.value=='' || elem.value==elem.defaultValue) {
			why += "Please enter your phone number.\n";
		}
	}
	
	elem = email;
	if (elem) {
		if (elem.value=='' || elem.value==elem.defaultValue) {
			why += "Please enter your e-mail address.\n";
		}
		else if (!emailFilter.test(elem.value) || elem.value.match(illegalChars)) { 
			why += "Please enter a valid e-mail address.\n";
		}
	}
	elem = captcha;
	if (elem) {
		if (elem.value=='' || elem.value==elem.defaultValue) {
			why += "Please enter the security code.\n";
		}
	}
	
	if (why != "") {
		alert('There are problems with your request:\n\n'+why);
		return false;
	}
	else {
		return true;
	}	
}

function fudExpand() {
	document.getElementById("fud-1").style.display = "none";
	document.getElementById("fud-2").style.display = "block";
}

function addToSubscribers() {
	if( document.getElementById("receive-newsletters").checked ) 
	{
		var emailFilter=/^.+@.+\..{2,3}$/;
		var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
		var elem;
		var why = "";
		
		elem = document.getElementById("nl-email");
		if (elem) 
		{
			if (elem.value=='' || elem.value==elem.defaultValue) 
			{
				why += "Please enter your e-mail address.\n";
			}
			else if (!emailFilter.test(elem.value) || elem.value.match(illegalChars)) 
			{ 
				why += "Please enter a valid e-mail address.\n";
			}
		}
		
		if (why != "") 
		{
			alert('There are problems with your request:\n\n'+why);
			return false;
		}
		
		window.open('/include/add-to-mailing-list.php?add=1&email='+elem.value,'fuddoc','');
	}
	else 
	{
		window.open('/media/10_Easy_ways_to_look_10_years_younger.pdf','fuddoc','');
	}

	window.location.href = '/';
}