/*
 * フォトスタジオ若葉
 * Copyright(c) 2008, Daijiro SUENAGA.
 */


function input_check()
{var form=document.getElementById('schedule');var obj_reason_other=document.getElementById('reason_other');obj_reason_other.style.backgroundColor="white";if(document.getElementById('reason_type').value=="28"&&obj_reason_other.value.length==0){alert("その他の場合、理由をお書きください");obj_reason_other.style.backgroundColor="pink";obj_reason_other.removeAttribute("readOnly");obj_reason_other.setAttribute("readonly",false);obj_reason_other.focus();return false;}
var obj_inputs=document.getElementsByTagName("input");var flag=false;var radio_index=0;for(var i=0;i<obj_inputs.length;i++){if(obj_inputs[i].getAttribute("type")!="radio"){continue;}
radio_index=i;if(obj_inputs[i].checked==true){flag=true;}}
if(flag==false){alert("予約確認方法を選択してください");obj_inputs[radio_index].focus();return false;}
var obj_email=document.getElementById('email');obj_email.style.backgroundColor="white";if(obj_email.value.length==0){alert('メールアドレスを入力してください');obj_email.style.backgroundColor="pink";obj_email.focus();return false;}
if(obj_email.value.match(/.+@.+\..+/)==null){alert('メールアドレスが正しくありません');obj_email.style.backgroundColor="pink";obj_email.focus();return false;}
var obj_name=document.getElementById('name');obj_name.style.backgroundColor="white";if(obj_name.value.length==0){alert('お名前を入力してください');obj_name.style.backgroundColor="pink";obj_name.focus();return false;}
var obj_tel=document.getElementById('tel');obj_tel.style.backgroundColor="white";if(obj_tel.value.length==0){alert('電話番号を入力してください');obj_tel.style.backgroundColor="pink";obj_tel.focus();return false;}
if(obj_tel.value.match(/[^0-9\-]+/)!=null){alert('電話番号が正しくありません');obj_tel.style.backgroundColor="pink";obj_tel.focus();return false;}
if(obj_tel.value.length<10){alert('電話番号が正しくありません');obj_tel.style.backgroundColor="pink";obj_tel.focus();return false;}
var obj_sex=document.getElementById('sex');if(obj_sex.value!=99){var obj_age=document.getElementById('age');if(obj_age.value.length==0){alert('年齢を入力してください');obj_age.style.backgroundColor="pink";obj_age.focus();return false;}
if(obj_age.value.match(/[^0-9]+/)!=null){alert('年齢が正しくありません');obj_age.style.backgroundColor="pink";obj_age.focus();return false;}}
form.submit();}
function confirm_reset()
{return confirm("入力した情報が全てクリアされます。\n本当にクリアしますか？");}
function addEvent(elm,listener,fn){try{elm.addEventListener(listener,fn,false);}catch(e){elm.attachEvent("on"+listener,fn);}}
function set_days()
{var hope1_year=parseInt(document.getElementById('hope1_year').value);var hope2_year=parseInt(document.getElementById('hope2_year').value);var hope1_month=parseInt(document.getElementById('hope1_month').value);var hope2_month=parseInt(document.getElementById('hope2_month').value);var obj_hope1_day=document.getElementById('hope1_day');var obj_hope2_day=document.getElementById('hope2_day');var hope1=new Date();hope1.setFullYear(hope1_year);hope1.setMonth(hope1_month);hope1.setDate(-1);var hope1_selected=1;for(var i=0;i<obj_hope1_day.length;i++){if(obj_hope1_day[i].selected==true){hope1_selected=parseInt(obj_hope1_day[i].value);}}
var hope2_selected=1;for(var i=0;i<obj_hope2_day.length;i++){if(obj_hope2_day[i].selected==true){hope2_selected=parseInt(obj_hope2_day[i].value);}}
while(obj_hope1_day.hasChildNodes()){obj_hope1_day.removeChild(obj_hope1_day.firstChild);}
for(var i=0;i<=hope1.getDate();i++){var obj_opt=document.createElement("option");obj_opt.setAttribute("value",i+1);obj_opt.innerHTML=i+1;if(hope1_selected==(i+1)){obj_opt.setAttribute("selected","selected");}
obj_hope1_day.appendChild(obj_opt);}
var hope2=new Date();hope2.setFullYear(hope2_year);hope2.setMonth(hope2_month);hope2.setDate(-1);while(obj_hope2_day.hasChildNodes()){obj_hope2_day.removeChild(obj_hope2_day.firstChild);}
for(var i=0;i<=hope2.getDate();i++){var obj_opt=document.createElement("option");obj_opt.setAttribute("value",i+1);obj_opt.innerHTML=i+1;if(hope2_selected==(i+1)){obj_opt.setAttribute("selected","selected");}
obj_hope2_day.appendChild(obj_opt);}}
function set_year()
{var obj_hope1_year=document.getElementById('hope1_year');var obj_hope2_year=document.getElementById('hope2_year');obj_opts=obj_hope1_year.childNodes;var j=0;for(var i=0;i<obj_opts.length;i++){if(obj_opts[i].tagName=="OPTION"){obj_opts[i].setAttribute("value",(new Date()).getFullYear()+j);j++;}}
obj_opts=obj_hope2_year.childNodes;var j=0;for(var i=0;i<obj_opts.length;i++){if(obj_opts[i].tagName=="OPTION"){obj_opts[i].setAttribute("value",(new Date()).getFullYear()+j);j++;}}}
function set_reason_other()
{var obj_reason_type=document.getElementById('reason_type');var obj_reason_other=document.getElementById('reason_other');if(obj_reason_type.value==28){obj_reason_other.removeAttribute("readOnly");obj_reason_other.style.backgroundColor="white";}else{obj_reason_other.setAttribute("readOnly","readonly");obj_reason_other.style.backgroundColor="silver";}}
function set_age()
{var obj_sex=document.getElementById('sex');var obj_age=document.getElementById('age');if(obj_sex.value!=99){obj_age.removeAttribute("readOnly");obj_age.style.backgroundColor="white";}else{obj_age.setAttribute("readOnly","readonly");obj_age.style.backgroundColor="silver";}}
function restore()
{if(org_data==undefined){return false;}
if(org_data.reason_type!=undefined){var obj_reason_type=document.getElementById('reason_type');obj_reason_type.selectedIndex=org_data.reason_type;set_reason_other();}
if(org_data.hope1!=undefined){var obj_hope1_year=document.getElementById('hope1_year');var obj_hope1_month=document.getElementById('hope1_month');var obj_hope1_day=document.getElementById('hope1_day');obj_hope1_year.value=org_data.hope1.year;obj_hope1_month.value=org_data.hope1.month;obj_hope1_day.value=org_data.hope1.day;}
if(org_data.hope2!=undefined){var obj_hope2_month=document.getElementById('hope2_month');var obj_hope2_year=document.getElementById('hope2_year');var obj_hope2_day=document.getElementById('hope2_day');obj_hope2_year.value=org_data.hope2.year;obj_hope2_month.value=org_data.hope2.month;obj_hope2_day.value=org_data.hope2.day;}
set_days()
if(org_data.contact!=undefined){var radio=parseInt(org_data.contact);var obj_inputs=document.getElementsByTagName("input");for(var i=0;i<obj_inputs.length;i++){if(obj_inputs[i].getAttribute("type")!="radio"){continue;}
if(parseInt(obj_inputs[i].value)==radio){obj_inputs[i].checked=true;}}}
if(org_data.sex!=undefined){var obj_sex=document.getElementById('sex');obj_sex.value=org_data.sex;set_age();}}
function init()
{var obj_submit=document.getElementById('submit_b');addEvent(obj_submit,"click",input_check);addEvent(obj_submit,"keypress",input_check);var obj_schedule=document.getElementById('schedule');addEvent(obj_schedule,"submit",input_check);set_year();var obj_hope1_year=document.getElementById('hope1_year');var obj_hope2_year=document.getElementById('hope2_year');var obj_hope1_month=document.getElementById('hope1_month');var obj_hope2_month=document.getElementById('hope2_month');addEvent(obj_hope1_year,"change",set_days);addEvent(obj_hope2_year,"change",set_days);addEvent(obj_hope1_month,"change",set_days);addEvent(obj_hope2_month,"change",set_days);set_days();var obj_reason_type=document.getElementById('reason_type');addEvent(obj_reason_type,"change",set_reason_other);set_reason_other();var obj_sex=document.getElementById('sex');addEvent(obj_sex,"change",set_age);set_age();restore();}
addEvent(window,"load",init);