function send(myfield, event, do_this) {
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (event) keycode = event.which;
	else return true;
	
	if (keycode == 13)
	{ 
		return do_this();
	}
	else
		return true;
}


function checkForm()
{
	var fv = new formValidator();
	var formSubmitted = false;
	fv.setForm(document.curves);
	
	fv.addDisplayName('day_phone01', 'Daytime Area Code');
	fv.addDisplayName('day_phone02', 'Daytime Prefix');
	fv.addDisplayName('day_phone03', 'Daytime Number');
	
	fv.addDisplayName('ev_phone01', 'Evening Area Code');
	fv.addDisplayName('ev_phone02', 'Evening Prefix');
	fv.addDisplayName('ev_phone03', 'Evening Number');

	fv.addDisplayName('OptIn', 'Interest in being contacted by email');
	fv.addDisplayName('IsMember', 'Current membership status with Curves');

	fv.addRegExpression('day_phone01', '[0-9]{3}');
	fv.addRegExpression('day_phone02', '[0-9]{3}');
	fv.addRegExpression('day_phone03', '[0-9]{4}');
	fv.addRegExpression('FirstName', '\s*[a-zA-Z0-9]+\s*');
	fv.addRegExpression('LastName', '\s*[a-zA-Z0-9]+\s*');
	fv.addRegExpression('ev_phone01', '[0-9]{3}');
	fv.addRegExpression('ev_phone02', '[0-9]{3}');
	fv.addRegExpression('ev_phone03', '[0-9]{4}');
	
	formSubmitted = fv.validateForm();
	return formSubmitted;
}

function set_name()
{	
	if(checkForm())
	{		
		return true;
	}
	else
	{
		//Don't submit form
		return false;
	}
}

function check_zipcode()
{
	if ($('#postal_code_again').val() != '' && $('#postal_code_again').val() != null)
	{
		var Zipcode= $('#postal_code_again').val();
		get_locations(Zipcode);		
	}		
	else if($('#postal_code').val() != '')
	{
		var Zipcode= $('#postal_code').val();
		get_locations(Zipcode);
	}
	else	
	{
		alert("The following field(s) need to be filled out:\n\nZipcode");
	}

	//Don't submit form
	return false; 	
}

function add_markers_to_map(data)
{
		$.each(data.items, function(i,item)
        {
			$("<img/>").attr("src", item.media.m).appendTo("#images");
         });
}


function get_locations(postal_code) 
{
	$.get("/locations/local_search_lead/get_locations.php?postal_code="+postal_code, function(data) {
		$('#callPhone').hide();
		$('#name').hide();
		$('#zipcode').hide();
		$('#locations').html(""+data);
		
		//remove the existing marker and the mouse over listener
		//GEvent.removeListener(mouseover_info_listener);	
		//map.removeOverlay(marker);
	});
	return false;	
}

function set_location(location_id,chosen_lat,chosen_long)
{	
	$.get("/locations/local_search_lead/set_location.php?location_id="+location_id, function(data) {
		$('#locations').hide();
		$('#callPhone').hide();
		$('#name').hide();		
		$('#zipcode').hide();		
		$('#user_info').html(""+data);
		$('#user_info').show("slow");
	})
	return false;
}


function set_contact_preferences()
{
	if (checkForm())
	{
		if ($('#optin_yes').attr('checked')){
			var optin = '1';
		}else{
			var optin = '0';
		}
		
		if ($('#ismember_current').attr('checked')){
			var status = 'current';
		}else if ($('#ismember_former').attr('checked')){
			var status = 'former';
		}else{
			var status = 'neither';
		}
		
		$.ajax(
				{
					type: "GET",
					url: "/locations/local_search_lead/save_contact_preferences.php",
					data: "optin=" + optin + "&status=" + status,
					async: false,
					success: function(data) 
					{
						if (data.search("ERROR") != -1)
						{
							alert(data);
						}
						else
						{
							document.curves.submit();
						}
					}
				});
	}
	return false;
}

function duplicate_check()
{	
	if(checkForm())
	{	
		var first_name=$('#first_name').val();
		var last_name=$('#last_name').val();
		var state=document.curves.state.value;
		var email=document.curves.email.value;
		var postal_code=document.curves.final_zipcode.value;
		var day_phone01= document.curves.day_phone01.value;
		var day_phone02= document.curves.day_phone02.value;
		var day_phone03= document.curves.day_phone03.value;
		var ev_phone01= document.curves.ev_phone01.value;
		var ev_phone02= document.curves.ev_phone02.value;
		var ev_phone03= document.curves.ev_phone03.value;

		if ($('#contact_day').attr('checked')){
			var contact_preference = 'day';
		}else{
			var contact_preference = 'night';
		}
		
		$('#locations').hide();
		$('#zipcode').hide();		
		
		$.ajax(
		{
			type: "POST",
			url: "/locations/local_search_lead/save_lead.php",
			data: "state=" + state + "&postal_code=" + postal_code + "&day_phone01=" + day_phone01 + "&day_phone02=" + day_phone02 + "&day_phone03=" + day_phone03 + "&ev_phone01=" + ev_phone01 + "&ev_phone02=" + ev_phone02 + "&ev_phone03=" + ev_phone03 + "&email=" + email +"&contact_preference=" + contact_preference,
			async: false,
			success: function(data) 
			{
				if (data.search("error") != -1)
				{
					alert(data);
				}
				else
				{
					document.curves.submit();
				}
			}
		});
		
		return false; //Don't submit form		
	}
	else 
	{
		//Don't submit form
		return false;
	}
}
