var firstLine = 4;
function load() {
    //centerToWaitWin();

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

    Ajax.Responders.register(myGlobalHandlers);

//    if ($("ItemName")) {
//        Event.observe("Price1", "keypress", __onlyNum, false);
//        Event.observe("Price2", "keypress", __onlyNum, false);
//    }
    if ($("cartBox")) {
        showCart();
    }
    if ($("Keyword")) {
        Event.observe("Keyword", "focus", Keyword_OnFocus, false);
    }
    //getCatList();
    window.onunload = function(){}

    return;
}

function Keyword_OnFocus() {
    if ($("Keyword").style.color == "#808080" || $("Keyword").style.color == "rgb(128, 128, 128)") {
        $("Keyword").style.color = "#000000"
        $("Keyword").value = ""
    }
    return;
}
function getCatList() {
    new Ajax.Request("/biz/getCatList.php",
                       {method:"post",
                        asynchronous:true,
                        onSuccess:createCatList.bindAsEventListener(this,"CategoryName")});
    return;
}
function createCatList(resultRequest, elem) {
    clearAllComboItem($(elem));
    if (resultRequest.responseText.substr(0,1)!='{') {
        alert("データの読込みエラーが発生しました。");
        return;
    }

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

    if (result["Result"] != "success") {
        alert(result["ResultMsg"]);
        return;
    }

    rows = result.Data;
    for (var i=0; i < rows.length; i++) {
        row = rows[i];
        addComboItem($(elem), row.CategoryName, row.CategoryName);
    }
    return;
}
function searchData(kind) {
    if (kind == undefined) kind = "";
    if (kind == "Mini") {
        frm = "frmMini";
        dest = "divResultMini";
    } else {
        frm = "frmMain";
        dest = "divResult";
    }

    if (!checkBeforeSearch(kind)) return false;
    new Ajax.Request("/biz/searchItem.php",
                       {method:"post",
                        parameters:Form.serialize(frm),
                        asynchronous:true,
                        onSuccess:showItemList.bindAsEventListener(this,dest)});
    return;
}
function showItemList(resultRequest, dest){
//alert(resultRequest.responseText);
    //事前チェック
    if (resultRequest.responseText.substr(0,1)!='{') {
        $(dest).hide();
        alert("データの読込みエラーが発生しました。");
        return;
    }

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

    if (result["Result"] != "success") {
        $(dest).hide();
        alert(result["ResultMsg"]);
        return;
    }

    //一覧の作成

    $(dest).update("");
    rows = result.Data;
    var html = '<div style="height:20px;padding-left:100px;"><a href="#" onclick="$(\''+dest+'\').hide();">検索の終了&nbsp;&gt;&gt;</a></div>';
    //var html = '';
    for (var i=0; i < rows.length; i++) {
        row = rows[i];
        photo = '<img src="/images/item/mini/'+row.ItemID+'.jpg" alt="'+row.ItemName+'" border="0"><br>';
        if (row.DescURL == "") {
            itemName = '<span class="itemname">'+row.ItemName.replace(/ /g, '<br>')+'</span><br>';
        } else {
            itemName = '<a class="itemname" href="'+row.DescURL+'">'+row.ItemName.replace(/ /g, '<br>')+'</a><br>';
            photo = '<a href="'+row.DescURL+'">'+photo+'</a>';
        }
        html += '<div class="search01item" style="float:left;">'+photo+itemName+'</div>';
        if (dest == "divResult" && i==(firstLine-1)) html += '<div style="clear:both;"></div>';
    }
    html += '<div style="clear:both;">&nbsp;</div>';
    $(dest).update(html);
    $(dest).show();
}
function checkBeforeSearch(kind) {
    var obj;
    var byteCnt;
    var charCnt;
    var num;

    if (kind == undefined) kind = "";
    if (kind == "") {
        obj = $("Keyword");
        //byteCnt = __getByteCount(obj.value);
        charCnt = obj.value.length;
        if (byteCnt == 0 || obj.value == "商品名・キーワードを入力してください") {
            alert("キーワードを入力してください。");
            obj.focus();
            return false;
        } else if (charCnt > obj.maxLength) {
            alert("キーワードに入力された文字数("+charCnt+"文字)が多すぎます。\n"+obj.maxLength+"文字以内で入力してください。");
            obj.focus();
            return false;
        }
//        obj = $("GroupName");
//        if (obj.value == "" && $("Keyword").value == "" && $("Price1").value == "" && $("Price2").value == "") {
//            alert("検索条件を指定してください。");
//            obj.focus();
//            return false;
//        }
//        obj = $("ItemName");
//        byteCnt = __getByteCount(obj.value);
//        if (byteCnt == 0) {
//        } else if (byteCnt > obj.maxLength) {
//            alert("商品名に入力された文字数("+byteCnt+"バイト)が多すぎます。\n"+obj.maxLength+"バイト以内で入力してください。");
//            obj.focus();
//            return false;
//        }
//        obj = $("Price1");
//        byteCnt = __getByteCount(obj.value);
//        num = parseFloat(obj.value);
//        if (byteCnt == 0) {
//        } else if (byteCnt > obj.maxLength) {
//            alert("価格に入力された文字数("+byteCnt+"バイト)が多すぎます。\n"+obj.maxLength+"バイト以内で入力してください。");
//            obj.focus();
//            return false;
//        } else if (!__IsNum(obj.value)) {
//            alert("価格に入力された値が正しくありません。");
//            obj.focus();
//            return false;
//        }
//        obj = $("Price2");
//        byteCnt = __getByteCount(obj.value);
//        num = parseFloat(obj.value);
//        if (byteCnt == 0) {
//        } else if (byteCnt > obj.maxLength) {
//            alert("価格に入力された文字数("+byteCnt+"バイト)が多すぎます。\n"+obj.maxLength+"バイト以内で入力してください。");
//            obj.focus();
//            return false;
//        } else if (!__IsNum(obj.value)) {
//            alert("価格に入力された値が正しくありません。");
//            obj.focus();
//            return false;
//        }
    } else {
        //
    }

    return true;
}
