// 簡単な入力チェック
function checkEasyInput(elem, msgElem, required) {
    if ($F(elem).length==0) {
        if (required) {
            $(msgElem).innerHTML = NG_icon + "入力して下さい。";
        }
        else {
            $(msgElem).innerHTML = "";
        }
    }
    else {
        $(msgElem).innerHTML = OK_icon;
    }
}

// 簡単な選択チェック
function checkEasySelect(elem, msgElem, required) {
    if ($F(elem)=="") {
        if (required) {
            $(msgElem).innerHTML = NG_icon + "選択して下さい。";
        }
        else {
            $(msgElem).innerHTML = "";
        }
    } else {
        $(msgElem).innerHTML = OK_icon;
    }
}

// 郵便番号チェック
var latestPostal;
function checkPostalInput(elem1, elem2, msgElem, required, convert, target_pref, target_address1, target_address2) {
    // 郵便番号から住所をセット
    function addressSet(request) {
        var result = eval('(' + request.responseText + ')');
        
        if (result.error) {
            $(msgElem).innerHTML = NG_icon + "該当の郵便番号は見つかりませんでした。";
            // フォームリセット
            $(target_address1).value = ""
            $(target_address2).value = ""
            $(target_pref+"_status").innerHTML = "";
            $(target_address1+"_status").innerHTML = "";
            $(target_address2+"_status").innerHTML = "";
        }
        else {
            if (result['pref_no']) {
                $(target_pref).value = result['pref_no'];
                $(target_pref+"_status").innerHTML = OK_icon;
            } else {
                $(target_pref+"_status").innerHTML = "";
            }
            
            if (result['address1']) {
                $(target_address1).value = result['address1'];
                $(target_address1+"_status").innerHTML = OK_icon;
                
                $(target_address2).value = result['address2'];
                
                if (result['address2']) {
                    $(target_address2+"_status").innerHTML = NG_icon + "番地を入力して下さい。";
                } else {
                    $(target_address2+"_status").innerHTML = "";
                }
            } else {
                $(target_address1+"_status").innerHTML = "";
            }
        }
    }
    
    if ($F(elem1).length==0 || $F(elem2).length==0) {
        $(msgElem).innerHTML = NG_icon + "入力して下さい。";
    } else if (!$F(elem1).match(/^[0-9]{3}$/) || !$F(elem2).match(/^[0-9]{4}$/)) {
        $(msgElem).innerHTML = NG_icon + "半角数字で正しく入力して下さい。";
    } else {
        // 上3桁・下4桁がきちんと入力されている場合のみ
        // タブとか→とかを押した場合はチェックしないようにする
        $(msgElem).innerHTML = OK_icon;
        
        if (convert) {
            if ($F(elem1) == '000' && $F(elem2) == '0000') {
                latestPostal = $F(elem1) + $F(elem2);
                // 日本国外
                $(target_pref+"_status").innerHTML = OK_icon;
                $(target_pref).value = '48';
                $(target_address1+"_status").innerHTML = "";
                $(target_address2+"_status").innerHTML = "";
            }
            else if (($F(elem1) + $F(elem2)) != latestPostal) {
                latestPostal = $F(elem1) + $F(elem2);
                // メッセージ表示
                $(target_pref+"_status").innerHTML = SEARCH_icon;
                $(target_address1+"_status").innerHTML = SEARCH_icon;
                $(target_address2+"_status").innerHTML = SEARCH_icon;
                // parsは他にも必要な情報があれば入れる
                var pars = "go=address-search&post_number=" + $F(elem1) + $F(elem2);
                var myAjax = new Ajax.Request (
                    xmlUrl,
                    {
                        method: 'get',
                        parameters: pars,
                        onComplete: addressSet
                    }
                );
            }
        } else {
            latestPostal = $F(elem1) + $F(elem2);
        }
    }
}

// メールアドレスチェック
var latestEmail;
function checkEmailInput(elem, msgElem, required) {
    
    if ($F(elem).length==0) {
        if (required) {
            $(msgElem).innerHTML = NG_icon + "入力して下さい。";
        }
        else {
            $(msgElem).innerHTML = "";
        }
        latestEmail = "";
    } else if (!$F(elem).match(/^[0-9A-Za-z-][+?\/0-9A-Za-z.._-]*@[0-9A-Za-z_-]+[\.]+[0-9A-Za-z_..-]+[A-Za-z]$/)) {
        $(msgElem).innerHTML = NG_icon + "半角英数字で正しく入力して下さい。";
        latestEmail = "";
    } else {
        $(msgElem).innerHTML = OK_icon;
    }
}

// 電話番号orFAXチェック
function checkTelInput(elem1, elem2, elem3, msgElem, required) {
    var tmp_elem = $F(elem1) + "-" + $F(elem2) + "-" + $F(elem3);
    if (required) {
        if ($F(elem1).length==0 || $F(elem2).length==0 || $F(elem3).length==0) {
            $(msgElem).innerHTML = NG_icon + "入力して下さい。";
        } else if (!$F(elem1).match(/^[0-9]+$/) || !$F(elem2).match(/^[0-9]+$/) || !$F(elem3).match(/^[0-9]{4}$/)) {
            $(msgElem).innerHTML = NG_icon + "半角数字で正しく入力して下さい。";
        } else {
            $(msgElem).innerHTML = OK_icon;
        }
    } else {
        if ($F(elem1).length==0 && $F(elem2).length==0 && $F(elem3).length==0) {
            $(msgElem).innerHTML = "";
        } else if ($F(elem1).length==0 || $F(elem2).length==0 || $F(elem3).length==0) {
            $(msgElem).innerHTML = NG_icon + "入力して下さい。";
        } else if (!$F(elem1).match(/^[0-9]+$/) || !$F(elem2).match(/^[0-9]+$/) || !$F(elem3).match(/^[0-9]{4}$/)) {
            $(msgElem).innerHTML = NG_icon + "半角数字で正しく入力して下さい。";
        } else {
            $(msgElem).innerHTML = OK_icon;
        }
    }
}

// パスワードチェック
function checkPasswordInput(elem, msgElem, elem2, msgElem2, required) {
    
    if (required) {
        if ($F(elem).length==0) {
            $(msgElem).innerHTML = NG_icon + "入力して下さい。";
        } else {
            $(msgElem).innerHTML = OK_icon;
        }
        
        if ($F(elem2).length==0) {
            $(msgElem2).innerHTML = NG_icon + "入力して下さい。";
        } else {
            $(msgElem2).innerHTML = OK_icon;
        }
    }
    else {
        if ($F(elem).length==0 && $F(elem2).length==0) {
            $(msgElem).innerHTML = "";
            $(msgElem2).innerHTML = "";
        } else {
            if ($F(elem).length==0) {
                $(msgElem).innerHTML = NG_icon + "入力して下さい。";
            } else {
                $(msgElem).innerHTML = OK_icon;
            }
            
            if ($F(elem2).length==0) {
                $(msgElem2).innerHTML = NG_icon + "入力して下さい。";
            } else {
                $(msgElem2).innerHTML = OK_icon;
            }
        }
    }
}

// イベント初期化
function initEvent(division, page, mode, dm) {
    // page 1:member_register, 2:member_change, 4:cart, 5:contact, 6:genpassword, 3:others
    
    if (mode != 'none' && !(navigator.userAgent.indexOf("MSIE")>=0 && navigator.userAgent.indexOf("Mac_PowerPC")>=0)) {
        if (page==5) {
            if (mode=='all') {
                checkName(true);
                checkEmailDual();
                checkTitle();
                checkContent();
            }
        } else if (page==6) {
            // 何も無し
        } else {
            if (division==2) {
                // 法人
                if (mode=='all' || (mode=='input' && $F('company_name').length>0)) {
                    checkCompanyName();
                }
                if (mode=='all' || (mode=='input' && $F('department_name').length>0)) {
                    checkDepartmentName();
                }
            }
            
            if (page==3 && division==2) {
                if (mode=='all' || (mode=='input' && $F('name').length>0)) {
                    checkName(false);
                }
                if (mode=='all' || (mode=='input' && $F('name_ph').length>0)) {
                    checkKana(false);
                }
            } else {
                if (mode=='all' || (mode=='input' && $F('name').length>0)) {
                    checkName(true);
                }
                if (mode=='all' || (mode=='input' && $F('name_ph').length>0)) {
                    checkKana(true);
                }
            }
            
            if (mode=='all' || (mode=='input' && ($F('address_postal1').length>0 || $F('address_postal2').length>0))) {
                checkPostal(false);
            }
            if (mode=='all' || (mode=='input' && $F('address_pref')!="")) {
                checkPrefecture();
            }
            if (mode=='all' || (mode=='input' && $F('address1').length>0)) {
                checkAddress1();
            }
            if (mode=='all' || (mode=='input' && $F('address2').length>0)) {
                checkAddress2();
            }
            if (mode=='all' || (mode=='input' && $F('address3').length>0)) {
                checkAddress3();
            }
            if (page==1 || page==4) {
                if (mode=='all' || (mode=='input' && ($F('email1').length>0 || $F('email2').length>0))) {
                    checkEmailDual();
                }
            } else if (page==2) {
                if (mode=='all' || (mode=='input' && $F('email1').length>0)) {
                    checkEmail();
                }
            }
            if (mode=='all' || (mode=='input' && ($F('tel1').length>0 || $F('tel2').length>0 || $F('tel3').length>0))) {
                checkTel();
            }
            if (page==1 || page==2 || page==4) {
                if (mode=='all' || (mode=='input' && ($F('fax1').length>0 || $F('fax2').length>0 || $F('fax3').length>0))) {
                    checkFax();
                }
                if (dm==true) {
                    if (mode=='all' || (mode=='input' && ($('dm1').checked || $('dm2').checked))) {
                        checkDm();
                    }
                }
            }
            if (page==1) {
                if (mode=='all' || (mode=='input' && ($F('pswd').length>0 || $F('pswd2').length>0))) {
                    checkPassword(true);
                }
            } else if (page==2 || page==4) {
                if (mode=='all' || (mode=='input' && ($F('pswd').length>0 || $F('pswd2').length>0))) {
                    checkPassword(false);
                }
            }
        }
    }
}

// イベント定義
function setEvent(division, page, dm) {
    // page 1:member_register, 2:member_change, 4:cart, 5:contact, 6:genpassword, 3:others
    
    if (!(navigator.userAgent.indexOf("MSIE")>=0 && navigator.userAgent.indexOf("Mac_PowerPC")>=0)) {
        if (page==5) {
            // お問い合わせ
            $('name').onkeyup = function () {checkName(true);};
            $('name').onfocus = function () {checkName(true);};
            $('email1').onkeyup = function () {checkEmailDual();};
            $('email1').onfocus = function () {checkEmailDual();};
            $('email2').onkeyup = function () {checkEmailDual();};
            $('email2').onfocus = function () {checkEmailDual();};
            $('title').onkeyup = function () {checkTitle();};
            $('title').onfocus = function () {checkTitle();};
            $('content').onkeyup = function () {checkContent();};
            $('content').onfocus = function () {checkContent();};
        } else if (page==6) {
            // パスワード忘れ
            $('name').onkeyup = function () {checkName(true);};
            $('name').onfocus = function () {checkName(true);};
            $('email1').onkeyup = function () {checkEmail();};
            $('email1').onfocus = function () {checkEmail();};
        } else {
            if (division==2) {
                // 法人
                $('company_name').onkeyup = function () {checkCompanyName();};
                $('company_name').onfocus = function () {checkCompanyName();};
                $('department_name').onkeyup = function () {checkDepartmentName();};
                $('department_name').onfocus = function () {checkDepartmentName();};
            }
            
            if (page==3 && division==2) {
                // 送り主・お届け先の法人は担当者名とフリガナ不要
                $('name').onkeyup = function () {checkName(false);};
                $('name').onfocus = function () {checkName(false);};
                $('name_ph').onkeyup = function () {checkKana(false);};
                $('name_ph').onfocus = function () {checkKana(false);};
            } else {
                $('name').onkeyup = function () {checkName(true);};
                $('name').onfocus = function () {checkName(true);};
                $('name_ph').onkeyup = function () {checkKana(true);};
                $('name_ph').onfocus = function () {checkKana(true);};
            }
            
            $('address_postal1').onkeyup = function () {checkPostal(true);};
            $('address_postal1').onfocus = function () {checkPostal(true);};
            $('address_postal2').onkeyup = function () {checkPostal(true);};
            $('address_postal2').onfocus = function () {checkPostal(true);};
            $('address_pref').onchange = function () {checkPrefecture();};
            $('address_pref').onfocus = function () {checkPrefecture();};
            $('address1').onkeyup = function () {checkAddress1();};
            $('address1').onfocus = function () {checkAddress1();};
            $('address2').onkeyup = function () {checkAddress2();};
            $('address2').onfocus = function () {checkAddress2();};
            $('address3').onkeyup = function () {checkAddress3();};
            $('address3').onfocus = function () {checkAddress3();};
            if (page==1 || page==4) {
                // お客様情報新規登録の場合はメールアドレス確認が必要
                $('email1').onkeyup = function () {checkEmailDual();};
                $('email1').onfocus = function () {checkEmailDual();};
                $('email2').onkeyup = function () {checkEmailDual();};
                $('email2').onfocus = function () {checkEmailDual();};
            } else if (page==2) {
                $('email1').onkeyup = function () {checkEmail();};
                $('email1').onfocus = function () {checkEmail();};
            }
            
            $('tel1').onkeyup = function () {checkTel();};
            $('tel1').onfocus = function () {checkTel();};
            $('tel2').onkeyup = function () {checkTel();};
            $('tel2').onfocus = function () {checkTel();};
            $('tel3').onkeyup = function () {checkTel();};
            $('tel3').onfocus = function () {checkTel();};
            if (page==1 || page==2 || page==4) {
                $('fax1').onkeyup = function () {checkFax();};
                $('fax1').onfocus = function () {checkFax();};
                $('fax2').onkeyup = function () {checkFax();};
                $('fax2').onfocus = function () {checkFax();};
                $('fax3').onkeyup = function () {checkFax();};
                $('fax3').onfocus = function () {checkFax();};
                if (dm==true) {
                    $('dm1').onclick = function () {checkDm();};
                    $('dm1').onfocus = function () {checkDm();};
                    $('dm2').onclick = function () {checkDm();};
                    $('dm2').onfocus = function () {checkDm();};
                }
            }
            if (page==1) {
                $('pswd').onkeyup = function () {checkPassword(true);};
                $('pswd').onfocus = function () {checkPassword(true);};
                $('pswd2').onkeyup = function () {checkPassword(true);};
                $('pswd2').onfocus = function () {checkPassword(true);};
            } else if (page==2 || page==4) {
                $('pswd').onkeyup = function () {checkPassword(false);};
                $('pswd').onfocus = function () {checkPassword(false);};
                $('pswd2').onkeyup = function () {checkPassword(false);};
                $('pswd2').onfocus = function () {checkPassword(false);};
            }
        }
    }
}

function checkTitle() {
    checkEasyInput('title', 'title_status', true);
}

function checkContent() {
    checkEasyInput('content', 'content_status', true);
}

function checkCompanyName() {
    checkEasyInput('company_name', 'company_name_status', true);
}

function checkDepartmentName() {
    checkEasyInput('department_name', 'department_name_status', false);
}

function checkName(required) {
    checkEasyInput('name', 'name_status', required);
}

function checkKana(required) {
    checkEasyInput('name_ph', 'name_ph_status', required);
}

function checkPostal(convert) {
    checkPostalInput('address_postal1', 'address_postal2', 'address_postal_status', true, convert, 'address_pref', 'address1', 'address2');
}

function checkPrefecture() {
    checkEasySelect('address_pref', 'address_pref_status', true);
}

function checkAddress1() {
    checkEasyInput('address1', 'address1_status', true);
}

function checkAddress2() {
    checkEasyInput('address2', 'address2_status', false);
}

function checkAddress3() {
    checkEasyInput('address3', 'address3_status', false);
}

function checkEmail() {
    checkEmailInput('email1', 'email1_status', true);
}

function checkEmailDual() {
    if ($F('email1').length==0) {
        $('email1_status').innerHTML = NG_icon + "入力して下さい。";
    } else if (!$F('email1').match(/^[0-9A-Za-z-][+?\/0-9A-Za-z.._-]*@[0-9A-Za-z_-]+[\.]+[0-9A-Za-z_..-]+[A-Za-z]$/)) {
        $('email1_status').innerHTML = NG_icon + "半角英数字で正しく入力して下さい。";
    } else {
        $('email1_status').innerHTML = OK_icon;
    }
    
    if ($F('email2').length==0) {
        $('email2_status').innerHTML = NG_icon + "入力して下さい。";
    } else if (!$F('email2').match(/^[0-9A-Za-z-][+?\/0-9A-Za-z.._-]*@[0-9A-Za-z_-]+[\.]+[0-9A-Za-z_..-]+[A-Za-z]$/)) {
        $('email2_status').innerHTML = NG_icon + "半角英数字で正しく入力して下さい。";
    } else if ($F('email1') != $F('email2')) {
        $('email2_status').innerHTML = NG_icon + "一致しません。";
    } else {
        $('email2_status').innerHTML = OK_icon;
    }
    
}

function checkTel() {
    checkTelInput('tel1', 'tel2', 'tel3', 'tel_status', true);
}

function checkFax() {
    checkTelInput('fax1', 'fax2', 'fax3', 'fax_status', false);
}

function checkDm() {
    if (!$('dm1').checked && !$('dm2').checked) {
        $('dm_status').innerHTML = NG_icon + "選択して下さい。";
    } else {
        $('dm_status').innerHTML = OK_icon;
    }
}

function checkPassword(required) {
    checkPasswordInput('pswd', 'pswd_status', 'pswd2', 'pswd2_status', required);
}

function checkQuestion(page, name, required) {//アンケート欄チェック nakayama
    // page 1:member_register, 4:cart, 5:contact
    if (page==4) {
        q_msgElem="customer_question_status_"+name;
        q_length = document.register.elements['customer_question['+name+']'].value.length;
    } else {
        q_msgElem="question_status_"+name;
        q_length = document.checkForm.elements['question['+name+']'].value.length;
    }
    if (q_length==0) {
        if (required) {
            $(q_msgElem).innerHTML = NG_icon + "入力して下さい。";
        }
        else {
            $(q_msgElem).innerHTML = "";
        }
    }
    else {
        $(q_msgElem).innerHTML = OK_icon;
    }
}
