// JavaScript Document
var formHTML = '<table>';
formHTML += '<tr>';
formHTML += '<td class="inputTitle">Mail</td>';
formHTML += '<td><input type="text" name="mailaddress" id="mailaddress" /></td>';
formHTML += '</tr>';
formHTML += '<tr>';
formHTML += '<td class="inputTitle">Subject</td>';
formHTML += '<td><input type="text" name="mailsubject" id="mailsubject" /></td>';
formHTML += '</tr>';
formHTML += '<tr>';
formHTML += '<td class="inputTitle">Body</td>';
formHTML += '<td><textarea name="mailbody" id="mailbody"></textarea></td>';
formHTML += '</tr>';
formHTML += '<tr>';
formHTML += '<td colspan="2" align="center" id="submitTd"><input type="button" id="send" value="Send" onclick="sendmail()" /></td>';
formHTML += '</tr>';
formHTML += '</table>';

YAHOO.namespace('phpmail');

function sendmail(){
	var mailAdress = document.getElementById('mailaddress').value;
	if(!mailAdress.match(/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/)){
		document.getElementById('errorInner').innerHTML = 'メールアドレスが不正です。<br /><a href="javascript:closeError(\'error\');">戻る</a>';
		document.getElementById('error').style.display = 'block';
		var formAnime0 = new YAHOO.util.Anim(
			'error',
			{opacity:{ from:0,to:0.8}},
			1,
			YAHOO.util.Easing.easeBoth
		);
		formAnime0.animate();
	}else{
		YAHOO.phpmail.doLoad();
	}
}

function closeError(id){
	var formAnime1 = new YAHOO.util.Anim(
		'error',
		{opacity:{ from:0.8,to:0}},
		1,
		YAHOO.util.Easing.easeBoth
	);
	formAnime1.animate();
	formAnime1.onComplete.subscribe(function(){
		document.getElementById('errorInner').innerHTML = '';
		document.getElementById('error').style.display = 'none';
	});
}

function GetWindowWidthHeight(){
	var myWidth = 0, myHeight = 0;
	if(typeof(window.innerWidth) == 'number'){
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	}else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)){
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	}else if(document.body && (document.body.clientWidth || document.body.clientHeight)){
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return {'width' : myWidth, 'height' : myHeight};
}

window.onload = function(){
	sWidth = GetWindowWidthHeight().width;
	sHeight = GetWindowWidthHeight().height;
	document.getElementById('error').style.width = sWidth;
	document.getElementById('error').style.height = sHeight;
	document.getElementById('errorInner').style.marginTop = sHeight/2-30+'px';
}

function errorOccur(){
	document.getElementById('errorInner').innerHTML = 'Error!<br /><a href="javascript:closeError(\'error\');">戻る</a>';
	document.getElementById('error').style.display = 'block';
	var formAnime0 = new YAHOO.util.Anim(
		'error',
		{opacity:{ from:0,to:0.8}},
		1,
		YAHOO.util.Easing.easeBoth
	);
	formAnime0.animate();
}

function again(){
	closeError('error');
	var formAnime0 = new YAHOO.util.Anim(
		'form',
		{opacity:{ from:0,to:1}},
		1,
		YAHOO.util.Easing.easeBoth
	);
	formAnime0.animate();
}

//初期化
YAHOO.phpmail.init = function() {   
   var atrr = {
      color: {from:'#ffffff',to:'#969696'},
      backgroundColor: {from:'#FF0000',to:'#FFFFFF'}
   };
   YAHOO.phpmail.anim=new YAHOO.util.ColorAnim('error',atrr,3);
};

YAHOO.util.Event.onAvailable('addLinks',YAHOO.phpmail.init);

var handleSuccess = function(o){
	if(o.responseText !== undefined){
		var elem_target = document.getElementById('form');
		if(o.responseText == '0'){
			errorOccur();
		}else if(o.responseText == ''){
			elem_target.style.filter = 'alpha(opacity=0)';
			elem_target.style.MozOpacity = 0;
			elem_target.style.opacity = 0;
			elem_target.innerHTML = formHTML;
			document.getElementById('errorInner').innerHTML = 'Thanks!!!<br /><a href="javascript:again();">one more</a>';
			document.getElementById('error').style.display = 'block';
			var formAnime0 = new YAHOO.util.Anim(
				'error',
				{opacity:{ from:0,to:0.8}},
				1,
				YAHOO.util.Easing.easeBoth
			);
			formAnime0.animate();
		}else{
			alert(o.responseText);
		}
	}
};

var handleFailure = function(o){
	if(o.responseText !== undefined){
		alert('Error!');
	}
};

var callback =
{
  success:handleSuccess,
  failure:handleFailure
};

var postUrl = './sys/mail.php';


YAHOO.phpmail.doLoad = function(){
	var mailaddress = '';
	var mailsubject = '';
	var mailbody = '';
	mailaddress = document.getElementById('mailaddress').value;
	mailsubject = document.getElementById('mailsubject').value;
	mailbody = document.getElementById('mailbody').value;
	var postData = 'mailaddress='+mailaddress+'&mailsubject='+mailsubject+'&mailbody='+mailbody;
	document.getElementById('form').innerHTML = '<div style="text-align:center; padding:100px 0 100px 0;"><img src="./image/loading.gif" /></div>';
	YAHOO.util.Connect.asyncRequest('POST',postUrl,callback,postData);
}
