$(document).ready(function() {
    $(".forgottgl").click(function () {
		$('#form_forgot').toggle();
		$('#form_login').toggle();
		return false; 
    });
	$('#form_sub input[type="text"],#form_login input[type="text"],#form_login input[type="text"],#form_forgot input[type="text"]').addClass("idleField").focus(function() {  
		$(this).removeClass("idleField").addClass("focusField");  
	}).blur(function() {  
		$(this).removeClass("focusField").addClass("idleField");   
	});
	$('#form_sub input#email').focus(function() {  
		if (this.value == 'Your EMAIL') this.value = '';
		else this.select();
	}).blur(function() {if (this.value == '') this.value = 'Your EMAIL';});
	$('#form_sub input[type="text"]').focus(function() {  
		$('#form_sub input[type="button"]').addClass("focusField");  
	});
});

function set_contact() {
	$('#form1 input[type="text"]').addClass("idleField");  
	$('#form1 input[type="text"]').focus(function() {  
		$(this).removeClass("idleField").addClass("focusField");  
	}).blur(function() {  
		$(this).removeClass("focusField").addClass("idleField");   
	});  
	$('#form1 input#name').focus(function() {  
		if (this.value == 'Your NAME') this.value = '';
		else this.select();
	}).blur(function() {if (this.value == '') this.value = 'Your NAME';});
	$('#form1 input#phone').focus(function() {  
		if (this.value == 'PHONE') this.value = '';
		else this.select();
	}).blur(function() {if (this.value == '') this.value = 'PHONE';});
	$('#form1 input#email').focus(function() {  
		if (this.value == 'EMAIL') this.value = '';
		else this.select();
	}).blur(function() {if (this.value == '') this.value = 'EMAIL';});
	$('#form1 input#best_time').focus(function() {  
		if (this.value == 'Best Time to Contact You') this.value = '';
		else this.select();
	}).blur(function() {if (this.value == '') this.value = 'Best Time to Contact You';});
	
	$('#form1 textarea').addClass("idleField");  
	$('#form1 textarea').focus(function() {  
		$(this).removeClass("idleField").addClass("focusField");  
	}).blur(function() {  
		$(this).removeClass("focusField").addClass("idleField");   
	});  
	$('#form1 textarea#message').focus(function() {  
		if (this.innerHTML == 'Question or Comments') this.innerHTML = '';
		else this.select();
	}).blur(function() {if (this.innerHTML == '') this.innerHTML = 'Question or Comments';});
	
	$("#contact_button").click(function(e){
		e.preventDefault(); 
		fotvcontact('/go/ajax/contact.php');
	});	
}

function fotvremind() {
	do_ajax(	'/go/ajax/remind.php',
				"fotv=remind"
					+ "&email=" + document.form_forgot.email.value,
				stateChangedL
		);
}

function fotvupdate() {
	do_ajax(	'/go/ajax/update.php',
				"fotv=update"
					+ "&mbr_id=" + document.form_update.mbr_id.value
					+ "&email=" + document.form_update.email.value
					+ "&password=" + document.form_update.password.value
					+ "&first_name=" + document.form_update.first_name.value
					+ "&last_name=" + document.form_update.last_name.value
					+ "&address1=" + document.form_update.address1.value
					+ "&city=" + document.form_update.city.value
					+ "&state=" + document.form_update.state.value
					+ "&zip=" + document.form_update.zip.value
					+ "&blast=" + ((document.form_update.blast.checked) ? 'Y' : 'N'),
				stateChangedU
			);
}
function stateChangedU() { 
	//alert('made it back'); return;
    if (xmlHttp.readyState == 4) { 
		var obj = document.getElementById("update-form");
		if (obj) {
			//alert('object exists');
			if (xmlHttp.status == 200) obj.innerHTML=xmlHttp.responseText;  // error
			else obj.innerHTML=xmlHttp.responseText;
		} //else alert('object problem');
    }
}

function fotvsubscribe() {
	do_ajax(	'/go/ajax/subscribe.php',
				"fotv=subscribe"
					+ "&email=" + document.form_sub.email.value,
				stateChangedS
			);
}
function stateChangedS() { 
	//alert('made it back'); return;
    if (xmlHttp.readyState == 4) { 
		var obj = document.getElementById("cell1");
		if (obj) {
			//alert('object exists');
			if (xmlHttp.status == 200) obj.innerHTML=xmlHttp.responseText;  // error
			else obj.innerHTML=xmlHttp.responseText;
		} //else alert('object problem');
    }
}

function fotvlogin() {
	do_ajax(	'/go/ajax/login.php',
				"fotv=login"
					+ "&email=" + document.form_login.email.value
					+ "&password=" + document.form_login.password.value,
				stateChangedL
			);
}
function stateChangedL() { 
	//alert('made it back'); return;
    if (xmlHttp.readyState == 4) { 
		var obj = document.getElementById("cell2");
		if (obj) {
			//alert('object exists');
			if (xmlHttp.status == 200) obj.innerHTML=xmlHttp.responseText;  // error
			else obj.innerHTML=xmlHttp.responseText;
		} //else alert('object problem');
    }
}

function fotvcontact(program) { 
	//alert('made it here'); return;
	do_ajax(	program,
				"fotv_contact=contact"
					+ "&name=" + escape(document.form1.name.value)
					+ "&email=" + document.form1.email.value
					+ "&phone=" + document.form1.phone.value
					+ "&best_time=" + escape(document.form1.best_time.value)
					+ "&message=" + escape(document.form1.message.value)
					+ "&menu_id=" + document.form1.menu_id.value,
				stateChangedC
			);
}
function stateChangedC() { 
	//alert('made it back'); return;
    if (xmlHttp.readyState == 4) { 
		var obj = document.getElementById("contactform");
		if (obj) {
			//alert('object exists');
			if (xmlHttp.status == 200) document.getElementById("contactform").innerHTML=xmlHttp.responseText;  // error
			else document.getElementById("contactform").innerHTML=xmlHttp.responseText;
			set_contact();
		} //else alert('object problem');
    }
}

function do_ajax(url,params,changefunction) {
	xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
		alert ("Your browser does not support AJAX!");
		return false;
    } 
	xmlHttp.onreadystatechange = changefunction;
	//alert(url + '?' + params); //return;
    xmlHttp.open("POST", url, true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
	//alert(url + params);
}

function GetXmlHttpObject() {
	var xmlHttp;
	try {
		xmlHttp = new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
	} catch (e) {
		try { // Internet Explorer
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				//alert("Your browser does not support AJAX!");
				return null;
			}
		}
	}
	return xmlHttp;
}