var cmnList;
var _sid;
function load() {
    //loadImage(cur);

    centerToWaitWin();

    var myGlobalHandlers = {
        onCreate: function(){
            showWait();
        },
        onComplete: function() {
            if(Ajax.activeRequestCount == 0){
                hideWait();
            }
        }
    };

    Ajax.Responders.register(myGlobalHandlers);

    var loc = location.search.substring(1);
    var params = loc.parseQuery();
    _sid = params["sid"];
    if ($("txtName") != null){
        new Ajax.Request("index.php", {method:"post", parameters:"action_getRsvCmnList=true&sid="+params["sid"], asynchronous:true, onComplete:createCmnList});
        $("txtName").focus();
    }

    return;
}
function createCmnList(resultRequest){
    //事前チェック
    if (resultRequest.responseText.substr(0,1)!="{" && resultRequest.responseText.substr(0,2)!="\n{") {
        alert('データの読込みエラーが発生しました。');
        return;
    }

    var result = eval( '(' + resultRequest.responseText.unescapeHTML() + ')' );

    if (result['Result'] != "success") {
        alert(result['ResultMsg']);
        return;
    } else {
        cmnList = result.Data;
        //createStaffCombo();
        createDateCombo();
        //createMenuList();
        return;
    }
}
/*
function createStaffCombo() {
    if ($("cboNomination").type != 'select') {
        return;
    }
    clearAllComboItem($("cboNomination"));
    addComboItem($("cboNomination"), 0, "特になし ");
    var staffcnt = cmnList["staffcnt"];
    if (staffcnt["mail"] > 0 && staffcnt["femail"] > 0) {
        addComboItem($("cboNomination"), -1, "男性希望 ");
        addComboItem($("cboNomination"), -2, "女性希望 ");
    }
    var staff = cmnList["staff"];
    for (var i=0; i < staff.length; i++) {
        addComboItem($("cboNomination"), staff[i]["ID"], staff[i]["Name"]+" ");
    }
    $("cboNomination").selectedIndex = 0;
}
*/
function createDateCombo() {

    id = $("cboNomination").value;
    if (id == -3){
       id = 0;
    }

    var _day=new Array("日","月","火","水","木","金","土");
    $("cboDate1").length = 1;
    clearAllComboItem($("cboDate1"));
    clearAllComboItem($("cboDate2"));
    clearAllComboItem($("cboDate3"));
    if (cmnList["ResReqCnt"] < 2){
        addComboItem($("cboDate2"), "", "");
    }
    if (cmnList["ResReqCnt"] < 3){
        addComboItem($("cboDate3"), "", "");
    }

    if (typeof cmnList["dates"][id] == 'undefined'){
        alert('ご指定の担当者はネット予約可能期間、休暇中のため、予約ができません。詳しくは直接お問合せください。');
        $("cboDate1").disabled = true;
        $("cboDate2").disabled = true;
        $("cboDate3").disabled = true;
        
    } else {
        var dates = cmnList["dates"][id];
        for (var i=0; i < dates.length; i++) {
            dayarr=dates[i].split("/");
            day=new Date(dayarr[0],dayarr[1]-1,dayarr[2]);
            var strday;
            strday="("+_day[day.getDay()]+")";
            addComboItem($("cboDate1"), dates[i], dates[i]+" "+strday);
            addComboItem($("cboDate2"), dates[i], dates[i]+" "+strday);
            addComboItem($("cboDate3"), dates[i], dates[i]+" "+strday);
        }
        $("cboDate1").selectedIndex = 0;
        $("cboDate2").selectedIndex = 0;
        $("cboDate3").selectedIndex = 0;

        $("cboDate1").disabled = false;
        $("cboDate2").disabled = false;
        $("cboDate3").disabled = false;
    }
}
/*
function createMenuList() {
    $("divMenu").update("");
    var item = cmnList["item"];
    var content = "";
    for (var i=0; i < item.length; i++) {
        content += '<input type="checkbox" id="chkMenu'+i+'" name="chkMenu[]" value="'+item[i]["ID"]+'">'+item[i]["Name"]+'<br>';
    }
    $("divMenu").update(content);
}
*/
function entryData() {
    centerToWaitWin();
    var chkOK = checkData();
    if (!chkOK) {
        return false;
    }
    new Ajax.Request("index.php", {method:"post", parameters:"action_addReserve=true&sid="+_sid+"&"+Form.serialize("frmMain"), asynchronous:true, onComplete:showEntryResult});
}
function showEntryResult(resultRequest){
//alert(resultRequest.responseText);
    //事前チェック
    if (resultRequest.responseText.substr(0,1)!="{" && resultRequest.responseText.substr(0,2)!="\n{") {
        alert('データの読込みエラーが発生しました。');
        return;
    }

    var result = eval( '(' + resultRequest.responseText.unescapeHTML() + ')' );

    if (result['Result'] != "success") {
        alert(result['ResultMsg']);
        return;
    } else {
        alert(result['ResultMsg']);
        $('frmMain').reset();
        if ($('retURL').value != "") {
            window.location.replace($('retURL').value);
            return;
        }
        return;
    }
}
function switchVisitHis(obj){
    if(obj.value==1){
        $('cboMotive').disabled = false;
        $('cboMotive').style.backgroundColor="#ffffff";
        switchMotive($('cboMotive'));
    }else{
        $('cboMotive').disabled = true;
        $('cboMotive').style.backgroundColor="#d4d0c8";
        $('txtIntro').disabled = true;
        $('txtIntro').style.backgroundColor="#d4d0c8";
    }
    
}
function switchMotive(obj){
    if (obj.value.match(/紹介/)) {
        $('txtIntro').disabled = false;
        $('txtIntro').style.backgroundColor="#ffffff";
    }else{
        $('txtIntro').disabled = true;
        $('txtIntro').style.backgroundColor="#d4d0c8";
    }
}
function getCheckboxCount(frm, name) {
    if (frm.elements[name]) {
        if (frm.elements[name].length) {
            return frm.elements[name].length;
        } else {
            return 1; // 配列のlengthがない -> 値が1つで配列ではない
        }
    } else {
        return 0; // 配列自体がない
    }
}
function checkData() {
    var elem;

    elem = $("txtName");
    byteCnt = __getByteCount(elem.value);
    if (byteCnt == 0) {
        alert("お名前が入力されていません。");
        elem.focus();
        return false;
    } else if (byteCnt > elem.maxLength) {
        alert("お名前に入力された文字数("+byteCnt+"バイト)が多すぎます。\n"+elem.maxLength+"バイト以内で入力してください。");
        elem.focus();
        return false;
    }
    elem = $("txtKana");
    byteCnt = __getByteCount(elem.value);
    if (byteCnt == 0) {
        alert("フリガナが入力されていません。");
        elem.focus();
        return false;
    } else if (byteCnt > elem.maxLength) {
        alert("フリガナに入力された文字数("+byteCnt+"バイト)が多すぎます。\n"+elem.maxLength+"バイト以内で入力してください。");
        elem.focus();
        return false;
    }
    elem = $("optGender1");
    if (elem.checked == false && $("optGender2").checked == false) {
        alert("性別を選択してください。");
        elem.focus();
        return false;
    }
    elem = $("txtTel");
    byteCnt = __getByteCount(elem.value);
    if (byteCnt == 0) {
        alert("電話番号が入力されていません。");
        elem.focus();
        return false;
    } else if (byteCnt > elem.maxLength) {
        alert("電話番号に入力された文字数("+byteCnt+"バイト)が多すぎます。\n"+elem.maxLength+"バイト以内で入力してください。");
        elem.focus();
        return false;
    }
    elem = $("txtEmail");
    byteCnt = __getByteCount(elem.value);
    if (byteCnt == 0) {
        alert("電子メールアドレスが入力されていません。");
        elem.focus();
        return false;
    } else if (byteCnt > elem.maxLength) {
        alert("電子メールアドレスに入力された文字数("+byteCnt+"バイト)が多すぎます。\n"+elem.maxLength+"バイト以内で入力してください。");
        elem.focus();
        return false;
    }
    if (!__IsValidEMail(elem.value)) {
        alert("入力された電子メールアドレスが正しくありません。");
        elem.focus();
        return false;
    }
    if (elem.value.match(/[\;\,]/)) {
        alert("入力された電子メールアドレスが正しくありません。");
        elem.focus();
        return false;
    }
    
    var RSVCONT=3;
    for(var i=1;i<=RSVCONT;i++){
        date=$("cboDate"+i);
        time=$("cboTime"+i);
        if(date.value && !time.value){
            alert("第"+i+"希望の時間を選択してください。");
            time.focus();
            return false;
        }else if(!date.value && time.value){
            alert("第"+i+"希望の日付を選択してください。");
            date.focus();
            return false;
        }
    }
    for(var i=RSVCONT;i>1;i--){
        date=$("cboDate"+i);
        time=$("cboTime"+i);
        if(date.value && time.value){
            for(var j=i-1;j>=1;j--){
                if(date.value == $("cboDate"+j).value &&
                   time.value == $("cboTime"+j).value){
                    alert("第"+i+"希望の日時が第"+j+"希望と重複しています。");
                    date.focus();
                    return false;
                }
            }
        }
    }

    menuChecked = false;
    menuCnt = getCheckboxCount(frmMain, "chkMenu[]");
    for (var i=0; i < menuCnt; i++) {
        if ($("chkMenu"+i)) {
            if ($("chkMenu"+i).checked) {
                menuChecked = true;
                break;
            }
        }else{
            break;
        }
    }
    if (!menuChecked) {
        alert("ご希望のメニューを最低1つ選択してください。");
        $("chkMenu0").focus();
        return false;
    }
    elem = $("txtRemarks");
    byteCnt = __getByteCount(elem.value);
    if (byteCnt > 2000) {
        alert("備考欄に入力された文字数("+byteCnt+"バイト)が多すぎます。\n2000バイト以内で入力してください。");
        elem.focus();
        return false;
    }

    if (confirm("入力された内容を送信します。\nよろしければ OKボタンを押して下さい。")) {
        return true;
    } else {
        return false;
    }
}
