zen = "０１２３４５６７８９ａｂｃｄｅｆｇｈｉｊｋｌｍｎｏｐｑｒｓｔｕｖｗｘｙｚＡＢＣＤＥＦＧＨＩＪＫＬＭＮＯＰＱＲＳＴＵＶＷＸＹＺ　！＃＄％＆’＊＋−／＝？＾＿｀｛｜｝〜＠．";
han = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ !#$%&'*+\-\/=?^_`{|}~@.";

OK_tel = false;
OK_mail = false;

function zen2han(inStr){
	var outStr = '';
	for(i=0; i<inStr.length; i++){
		var c = inStr.charAt(i);
		var n = zen.indexOf(c, 0);
		if(0 <= n) c = han.charAt(n);
		outStr += c;
	}
	return outStr;
}

function isMailAddress(str, r){
	str = zen2han(str);
	str = str.replace(/^ +/, '');
	str = str.replace(/ +$/, '');
	var s = str.match(/^(?:[^(\040)<>@,;:".\\\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".\\\\\[\]\000-\037\x80-\xff])|"[^\\\\\x80-\xff\n\015"]*(?:\\\\[^\x80-\xff][^\\\\\x80-\xff\n\015"]*)*")(?:\.(?:[^(\040)<>@,;:".\\\\\[\]\000-\037\x80-\xff]+(?![^(\040)<>@,;:".\\\\\[\]\000-\037\x80-\xff])|"[^\\\\\x80-\xff\n\015"]*(?:\\\\[^\x80-\xff][^\\\\\x80-\xff\n\015"]*)*"))*@(?:\w+(?:-*\w+)*\.)+\w{2,}$/i);
	if(s != null){
		str = s[0];
		return str;
	}else if(!r){
		str = str.replace(/^(.+)(\@[^@]+)$/, '"$1"$2');
		return isMailAddress(str, 1);
	}else{
		return '';
	}
}

function ValidateMailAddress(o){
	e = document.getElementById('invalidMailAddress');
	var s = o.value;
	if(s){
		s = isMailAddress(s);
		if(s){
			o.value = s;
			e.style.display = 'none';
			OK_mail = true;
		}else{
			e.style.display = 'inline';
			o.focus();
			o.select();
			OK_tel = false;
		}
	}else{
		e.style.display = 'none';
		OK_tel = false;
	}
}

function ValidateTelNum(o){
	e = document.getElementById('invalidTelNum');
	var s = o.value;
	if(s){
		s = zen2han(s);
		s = s.replace(/[^0-9]/g, '');
		if(s.match(/^0[5789]0\d{8}$/)){
			o.value = s;
			e.style.display = 'none';
			OK_mail = true;
		}else if(s.match(/^0[1-9][1-9]\d{7}$/)){
			o.value = s;
			e.style.display = 'none';
			OK_mail = true;
		}else{
			e.style.display = 'inline';
			o.focus();
			o.select();
			OK_mail = false;
		}
	}else{
		e.style.display = 'none';
		OK_mail = false;
	}
}

function ValidateForm(o){
	ValidateTelNum(o.tel);
	ValidateMailAddress(o.mail);
	if(!(o.name.value)){
		alert("お名前は必須です。");
		o.name.focus();
		return false;
	}else if(!(OK_mail || OK_tel)){
		alert("電話番号とメールアドレスはどちらか必須です。");
		o.tel.focus();
		return false;
	}else if(o.redirect.value == 223){
		if(!(o.month.value && o.day.value && o.days.value)){
			alert("宿泊日は必須です。");
			if(!o.month.value){
				o.month.focus();
			}else if(!o.day.value){
				o.day.focus();
			}else if(!o.days.value){
				o.days.focus();
			}
			return false;
		}else if(!(o.men.value || o.women.value || o.boy.value || o.girl.value)){
			alert("人数は必須です。");
			o.men.focus();
			return false;
		}
	}else if(o.redirect.value == 177){
		if(!(o.body.value)){
			alert("内容は必須です。");
			o.body.focus();
			return false;
		}
	}
}

