$(document).ready(function onload(){
	operatedInput('age','``');
	operatedInput('zipcode','`````');	
});

function hideErrorBlock(id, message, display){

	if (message != '')
		$('#ErrorMessages').append('<li>' + message + '</li>'); 

	var html = "";
	if (display == 'inline')
		html = "&nbsp;*";
		
	document.getElementById(id + 'ErrorBox').style.display = display;
	document.getElementById(id + 'ErrorBox').innerHTML = html;
}

function checkRequire(id){
	if (document.getElementById(id).value == ''){
		hideErrorBlock(id, 'This field is required', 'inline');
	} else {
		hideErrorBlock(id, '', 'none');
	}
	validForm();
	Validation();
}

function checkRequire2(id1, id2){
	if (document.getElementById(id1).value == ''){
		hideErrorBlock(id1, 'THIS FIELD IS REQUIRED', 'inline')
	} else {
		hideErrorBlock(id1, '', 'none');
		hideErrorBlock(id2, '', 'none')		
	}
	validForm();
	Validation();
}

function emailValid(id){
	$.post("/ajax/ajaxcheckemailnewsletter", { email: document.getElementById(id).value },
			  function(data){
				Validation();
				if (data.result == false){
					document.getElementById(id).value = '';				
					hideErrorBlock(id, data.messages, 'inline');
				} else {					
					hideErrorBlock(id, '', 'none');
				}
				validForm();
			  }, "json");	
}

function zipValid(id){
	var url = '/ajax/ajaxzipvalid';
	$.post(url, { zipcode: document.getElementById(id).value },
			  function(data){
					Validation();
					if (data.result == false){
						document.getElementById(id).value = '';
						hideErrorBlock(id, data.messages, 'inline');
					} else {
						hideErrorBlock(id, '', 'none');
					}
					validForm();
			  }, "json");
}

function getNeighList(id){	
	$.post("/signup/ajaxgetneighlist", { zipcode: document.getElementById(id).value },
			  function(data){
				document.getElementById('neighborhoodID').options.length = 0;
				document.getElementById('neighborhoodID').options[0] = new Option('Select a Neighborhood', '');
					if (data.length != 0){
						$("#neighborhoodID").css("display", "");
						for (var i=0; i<data.length; i++){
							document.getElementById('neighborhoodID').options[i+1] = new Option(data[i].label, data[i].value);
						}
						$("#Outside-Portland").css("display", "none");
					} else {
						$("#neighborhoodID").css("display", "none");
						$("#Outside-Portland").css("display", "inline");
						
					}
			  }, "json");    
}

function validForm(){
	if (document.getElementById('firstnameErrorBox').innerHTML != '' ||	
			document.getElementById('lastnameErrorBox').innerHTML != '' ||
			document.getElementById('emailErrorBox').innerHTML != '' ||
			document.getElementById('cityErrorBox').innerHTML != '' ||
			document.getElementById('ageErrorBox').innerHTML != '' || 
			document.getElementById('zipcodeErrorBox').innerHTML != ''){
			return false;
		} else {
			$("#errorNotice").css("display", "none");
			return true;
		}
}

function Validation() {
	$('#ErrorMessages').html("");

	/*if (document.getElementById('firstname').value == '') hideErrorBlock('firstname', 'First name is required', 'inline');
	else hideErrorBlock('firstname', '', 'none');
	
	if (document.getElementById('lastname').value == '') hideErrorBlock('lastname', 'Last name is required', 'inline');
	else hideErrorBlock('lastname', '', 'none');*/
	
	if (document.getElementById('email').value == '') hideErrorBlock('email', 'Email is required', 'inline');		
	else hideErrorBlock('email', '', 'none');
	
	/*if (document.getElementById('age').value == '') hideErrorBlock('age', 'Age is required', 'inline');
	else hideErrorBlock('age', '', 'none');
	
	if (document.getElementById('city').value == '') hideErrorBlock('city', 'City is required', 'inline');
	else hideErrorBlock('city', '', 'none');
	
	if (document.getElementById('zipcode').value == '') hideErrorBlock('zipcode', 'ZIP Code is required', 'inline');
	else hideErrorBlock('zipcode', '', 'none');*/
		
	return validForm();
}

function lastValidation() {
	if (Validation()){
		$("#errorNotice").css("display", "none");
		return true;
	} else {
		$("#errorNotice").css("display", "block");
		return false;
	}
}