var height;
var width;

$(function()
{
	height = document.documentElement.scrollHeight;
	width = document.documentElement.clientWidth;

	appender();
	$('#sendmail').click(function()
	{
		mailSender();
	});
	//setNavi("../");
});

function mailSender()
{
	if (!sendValidate() ) return;
	$.ajax(
	{
		url: 'mailsender.php',
		type: 'POST',
		data: {'name':$('#nameform').val(),
					'mailaddress':$('#addressform').val(),
					'title':$('#titleform').val(),
					'content':$('#textform').val()},
		timeout: 5000,
		success: function()
		{
			showSimpleAlert("送信完了！");
			$('#nameform').val('');
			$('#addressform').val('');
			$('#titleform').val('');
			$('#textform').val('');
		},
		error: function()
		{
			alert('送信エラー');
		}
	});
}

function appender()
{		
	$('body')
		.append('<div id="simplealert"></div>');
	
	$('#simplealert')
		.append('<p id="simplealertmsg"></p>')
		.append('<p id="simplealertok"></p>');
	setSimpleAlert();
}

function showSimpleAlert(msg)
{
	$('#simplealert').height(height).width(width);
	$('#simplealertmsg').text(msg);
	$('#simplealert').fadeIn(200);
	$('#simplealertok')
		.hover(function()
		{
			$(this).css('backgroundImage','url(img/btnform2.png)');
		},
		function()
		{
			$(this).css('backgroundImage','url(img/btnform.png)');
		})
		.click(function()
		{
			$('#simplealert').fadeOut(200);
		});
}

function setSimpleAlert()
{
	$('#simplealert')
		.css('position','absolute')
		.css('top','0')
		.css('left','0')
		.css('zIndex','3')
		.css('backgroundColor','#000')
		.css('opacity','0.9')
		.css('textAlign','center')
		.hide();
	$('#simplealertmsg')
		.css('width','100%')
		.css('color','#fff')
		.css('marginTop', '25%')
	$('#simplealertok')
		.css('width','40px')
		.css('height','25px')
		.css('margin','0 auto')
		.css('backgroundImage','url(img/btnform.png)')
		.css('backgroundRepeat','no-repeat')
		.css('cursor','pointer')
}

function sendValidate()
{
	var isFailed = true;
	if($("#nameform").val() == "")	{
		$("#nameform").css("backgroundColor","#f99");
		isFailed = false;

	}else	{
		$("#nameform").css("backgroundColor","#fff");
	}
	
	if($("#addressform").val().
			search(/^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/)
				== -1)	{
		$("#addressform").css("backgroundColor","#f99");
		isFailed = false;

	}else	{
		$("#addressform").css("backgroundColor","#fff");
	}

	if($("#titleform").val() == "")	{
		$("#titleform").css("backgroundColor","#f99");
		isFailed = false;

	}else	{
		$("#titleform").css("backgroundColor","#fff");
	}

	if($("#textform").val() == "")	{
		$("#textform").css("backgroundColor","#f99");
		isFailed = false;

	}else	{
		$("#textform").css("backgroundColor","#fff");
	}
	
	return isFailed;
}