$(document).ready(function(){
	$("#save_property").click(function(){
		go = true;
		error = '';
		
		if ($("select[name=contactId]").val() == ''){
			error += "- Contact\n";
		}
		
		if ($("input[name=streetAddress]").val() == ''){
			error += "- Address\n";
		}
		
		if ($("input[name=city]").val() == ''){
			error += "- City\n";
		}
		
		if ($("input[name=zip]").val() == ''){
			error += "- Zipcode\n";
		}
		
		if ($("select[name=stateAbvr]").val() == ''){
			error += "- State\n";
		}
		
		if ($("input[name=title]").val() == ''){
			error += "- Property title\n";
		}
		
		if ($("input[name=price]").val() == ''){
			error += "- Price\n";
		}
		
		if ($("select[name=lakeId]").val() == ''){
			error += "- Lake\n";
		}
		
		if (error.length > 0){
			alert("Mandatory information is missing\n\n" + error);
			return false;
		}
		
		if ($("input[name=price]").val() == '0'){
			go = confirm('Do you really want to set the price to zero?');
		}
		
		if (go){
			$("#form_submit_property").append('<input type="hidden" name="'+$(this).attr("name")+'" />').submit();
			return true;
		}
		
		return false;
	});
	
	
	$("#description").keyup(function(){
		cnt_chars("description", 1000);
	});
	
	
	$("#save_user").click(function(){
		error = '';

		if ($("input[name=firstname]").val() == ''){
			error += "- First Name\n";
		}

		if ($("input[name=lastname]").val() == ''){
			error += "- Last Name\n";
		}

		if ($("input[name=email]").val() == ''){
			error += "- E-Mail\n";
		}

		if ($("select[name=userright]").val() == ''){
			error += "- Account Type\n";
		}

		if ($("input[name=adress]").val() == ''){
			error += "- Address Line\n";
		}

		if ($("input[name=city]").val() == ''){
			error += "- City\n";
		}

		if ($("select[name=state]").val() == ''){
			error += "- State\n";
		}

		if ($("input[name=zip]").val() == ''){
			error += "- Zip Code\n";
		}

		if (error.length > 0){
			alert("Mandatory information is missing\n\n" + error);
			return false;
		}
	});



	$("#save_contact").click(function(){
		error = '';

		if ($("input[name=firstName]").val() == ''){
			error += "- Firstname\n";
		}

		if ($("input[name=lastName]").val() == ''){
			error += "- Lastname\n";
		}

		if ($("input[name=email]").val() == ''){
			error += "- E-Mail\n";
		}


		if (error.length > 0){
			alert("Mandatory information is missing\n\n" + error);
			return false;
		}
	});


	$("select[name=stateAbvr]").change(function(){
		if ($(this).val() == ""){
			$(".lake_select .lake_selection").hide();
			$(".lake_select .error_message").show();
		}else{
			$(".lake_select .lake_selection").show();
			$(".lake_select .error_message").hide();
		}

		$.get('/_ajax/ajax.state_lakes.php', { stateAbvr : $(this).val() }, function(ret){
			$("select[name=lakeId]").html(ret);
		});
	});


	$("#add_lake_trigger").click(function(){
		$("#add_lake").slideDown();
		return false;
	});

	$("#add_lake .add_lake_abort").click(function(){
		$("#add_lake").slideUp();
		return false;
	});

	// save new lake
	$("#add_lake .add_lake_save").click(function(){
		var stateAbvr = $("select[name='stateAbvr']").val();
		var lakeName = $("#add_lake input").val();

		if (stateAbvr.length !== 2){
			alert('There has been an error, please choose state again and retry.');
			return false;
		}

		if (lakeName.length < 3){
			alert('Please enter a valid lake name');
			return false;
		}

		$.post('/_ajax/ajax.property.addlake.php', { stateAbvr : stateAbvr, lakeName : lakeName }, function(ret){
			if (ret !== 'false'){
				if (ret == 'double'){
					alert('There is a lake with the same name for ' + stateAbvr + ' already.');
					return false;
				}
				
				$("select[name=lakeId]").html(ret);
				$("#add_lake input").val("");
				$("#add_lake").slideUp();
			}else{
				alert('There has been an error storing your lake. Please try again later');
			}
		});
		
		return false;
	});
	
	
	$(".icon.as-seen-on-lakefrog").click(function(){
		$(".as-seen-on-lakefrog-content.p-"+ $(this).attr("href").substring(1)).show();
		return false;
	});
	
	$(".as-seen-on-lakefrog-content .close").click(function(){
		$(".as-seen-on-lakefrog-content.p-"+ $(this).attr("href").substring(1)).hide();
		return false;
	});
	
	$(".as-seen-on-lakefrog-content").hide();

});
