﻿var stuffbuy_match = new Object();

stuffbuy_match.init = function() {
    $("#weblang").val(dfrc.weblang);

    $("#upload_file").val("");

    $('#stmatchdetail_sweight').keypress(function() {
        return Common.number();
    });

    $('#stmatchdetail_eweight').keypress(function() {
        return Common.number();
    });

    $('#stmatchdetail_sprice').keypress(function() {
        return Common.number();
    });

    $('#stmatchdetail_eprice').keypress(function() {
        return Common.number();
    });

    $('#addbutton').click(function() {
        stuffbuy_match.addbutton();
        return false;
    });

    $('#savebutton').click(function() {
        return stuffbuy_match.savebutton();
    });

    $("#stmatchdata_term_0").click(function() {
        $("#term_date").css("display", "none");
        $("#stmatchdata_duedate").val("9998-12-31");
    });

    $("#stmatchdata_term_1").click(function() {
        $("#term_date").css("display", "inline");
        var myDate = new Date();
        $("#stmatchdata_date").val(myDate.format("yyyy-mm-dd"));
        $("#stmatchdata_duedate").val(myDate.format("yyyy-mm-dd"));
    });

    $(".date-pick").datepicker({
        showOn: 'button',
        buttonText: dfrc.datetimepicker_please_choice,
        buttonImage: '/img/common/Calendar.png',
        buttonImageOnly: true,
        changeMonth: true,
        changeYear: true
    });

    $("#uploadify").uploadify({
        'uploader': '/img/Uploadify/uploadify.swf',
        'script': '/uploader.ashx',
        'cancelImg': '/img/common/cancel.png',
        'buttonText': 'upload..',
        'buttonImg': '/img/Uploadify/Load.png',
        'fileDesc': '*.JPG;*.BMP;*.PNG',
        'fileExt': '*.JPG;*.BMP;*.PNG',
        'simUploadLimit': 10,
        'queueID': 'fileQueue',
        'auto': false,
        'multi': true,
        onAllComplete: function() {
            Dafon.ContentPostChange('CENTER', '/ap/b2b/stuffbuy_match.aspx');
        },
        onSelect: function(event, queueID, fileObj) {
            $("#upload_file").val($("#upload_file").val() + fileObj.name + ",");
        },
        onSelectOnce: function(event, data) {
            $("#stmatchphoto_number").val(data.fileCount);
        },
        onClearQueue: function(event, data) {
            $("#stmatchphoto_number").val(data.fileCount);
        },
        onCancel: function(event, queueID, fileObj, data) {
            $("#stmatchphoto_number").val(data.fileCount);
        }
    });

    $("#stmatchdata_nation").change(stuffbuy_match.getSitearea);
    $("#stmatchtype_mcode").change(stuffbuy_match.getStmatchtypeclass02Data);
}

stuffbuy_match.formaction = function() {
    Dafon.ContentPostChange('CENTER', '/ap/b2b/stuffbuy_match.aspx');
}

stuffbuy_match.addbutton = function() {
    Common.scrollTop();
    var myDate = new Date();
    $("#stmatchdata_code").val("");
    $("#stmatchdata_type").val("");
    $("#stmatchdata_title").val("");
    $("#stmatchdata_date").val(myDate.format("yyyy-mm-dd"));
    $("#stmatchdata_term").val("L");
    $("#term_date").css("display", "none");
    $("#stmatchdata_duedate").val(myDate.format("yyyy-mm-dd"));
    $("#stmatchdata_editusername").val("");
    $("#stmatchdata_editdate").val("");
    $("#stmatchdetail_stdname").val("");
    $("#stmatchtype_mcode").val("");
    $("#stmatchtype_dcode").val("");
    $("#stmatchtype_dcode_temp").val("");
    $("#stmatchdetail_style").val("");
    $("#stmatchquot_type").val("");
    $("#stmatchdetail_sweight").val("0");
    $("#stmatchdetail_eweight").val("0");
    $("#stmatchdetail_sprice").val("0");
    $("#stmatchdetail_eprice").val("0");
    $("#stmatchdetail_deliver").val("");
    $("#stmatchdetail_moneycode").val("");
    $("#stmatchdetail_desc").val("");
    $("#stmatchphoto_number").val("0");
    $("#Vaildate_Code").val("");
    $("#upload_image").remove();
    $("#savebutton").get(0).disabled = false;
    $("#addbutton").get(0).disabled = true;
    $("#button_action").val("A");
    $("#stmatchdata_custname").focus();
    return false;
}

stuffbuy_match.savebutton = function() {
    if ($("#stmatchdata_custname").val() == "") {
        alert(stuffbuy.stmatchdata_custname);
        $("#stmatchdata_custname").focus();
        return false;
    }
    if ($("#stmatchdata_tel").val() == "") {
        alert(stuffbuy.stmatchdata_tel);
        $("#stmatchdata_tel").focus();
        return false;
    }
    if ($("#stmatchdata_email").val() == "") {
        alert(stuffbuy.stmatchdata_email);
        $("#stmatchdata_email").focus();
        return false;
    }
    if (Common.checkmail($("#stmatchdata_email").val()) == false) {
        alert(dfrc.email_format_error);
        $("#stmatchdata_email").focus();
        return false;
    }
    if ($("#stmatchdata_nation").val() == "") {
        alert(stuffbuy.stmatchdata_nation);
        $("#stmatchdata_nation").focus();
        return false;
    }
    if ($("#stmatchdata_area").val() == "") {
        alert(stuffbuy.stmatchdata_area);
        $("#stmatchdata_area").focus();
        return false;
    }
    if ($("#stmatchdata_address").val() == "") {
        alert(stuffbuy.stmatchdata_address);
        $("#stmatchdata_address").focus();
        return false;
    }
    if ($("#stmatchdata_title").val() == "") {
        alert(stuffbuy.stmatchdata_title);
        $("#stmatchdata_title").focus();
        return false;
    }
    if ($("#stmatchdata_date").val() == "") {
        alert(stuffbuy.stmatchdata_date);
        $("#stmatchdata_date").focus();
        return false;
    }
    if ($("#stmatchdetail_stdname").val() == "") {
        alert(stuffbuy.stmatchdetail_stdname);
        $("#stmatchdetail_stdname").focus();
        return false;
    }
    if ($("#stmatchtype_mcode").val() == "") {
        alert(stuffbuy.stmatchtype_mcode);
        $("#stmatchtype_mcode").focus();
        return false;
    }
    if ($("#stmatchtype_dcode").val() == "") {
        alert(stuffbuy.stmatchtype_dcode);
        $("#stmatchtype_dcode").focus();
        return false;
    }
    if ($("#stmatchdetail_sweight").val() == "") {
        alert(stuffbuy.stmatchdetail_sweight);
        $("#stmatchdetail_sweight").focus();
        return false;
    }
    if ($("#stmatchdetail_eweight").val() == "") {
        alert(stuffbuy.stmatchdetail_eweight);
        $("#stmatchdetail_eweight").focus();
        return false;
    }
    if ($("#stmatchdetail_eweight").val() < $("#stmatchdetail_sweight").val()) {
        alert(stuffbuy.stmatchdetail_weightalarm);
        $("#stmatchdetail_eweight").focus();
        return false;
    }
    if ($("#stmatchdetail_sprice").val() == "") {
        alert(stuffbuy.stmatchdetail_sprice);
        $("#stmatchdetail_sprice").focus();
        return false;
    }
    if ($("#stmatchdetail_eprice").val() == "") {
        alert(stuffbuy.stmatchdetail_eprice);
        $("#stmatchdetail_eprice").focus();
        return false;
    }
    if ($("#stmatchdetail_moneycode").val() == "") {
        alert(stuffbuy.stmatchdetail_moneycode);
        $("#stmatchdetail_moneycode").focus();
        return false;
    }
    if ($("#stmatchdetail_desc").val() == "") {
        alert(stuffbuy.stmatchdetail_desc);
        $("#stmatchdetail_desc").focus();
        return false;
    }

    if ($("#Vaildate_Code").val() == "") {
        alert(stuffbuy.Vaildate_Code);
        $("#Vaildate_Code").focus();
        return false;
    }

    $.get("/validate_number_check.ashx", { Vaildate_Code: $("#Vaildate_Code").val() }, function(data) {
        if (data == "true") {
            Common.progressbox(dfrc.process_waiting);
            $("#button_action").val("A");
            stuffbuy_match.getStmatchdatacode();
        } else {
            alert(stuffbuy.Vaildate_Error);
            $("#Vaildate_Code").focus();
            return false;
        }
    });
    return false;
}

stuffbuy_match.uploadfile = function() {
    $("#uploadify").uploadifyUpload()
}

stuffbuy_match.onFailed = function(res) {
    alert(res.get_message());
}

//至wcf取得媒合單號
stuffbuy_match.getStmatchdatacode = function() {
    DfrcService.getStmatchdatacode($("#weblang").val(), stuffbuy_match.getStmatchdatacodeOnSuccess, stuffbuy_match.onFailed);
}

stuffbuy_match.getStmatchdatacodeOnSuccess = function(originalRequest) {
    $("#stmatchdata_code").val(originalRequest);
    if ($("#upload_file").val() != "") {
        $("#uploadify").uploadifySettings("folder", "/" + $("#stmatchdata_code").val());
        stuffbuy_match.uploadfile();
    } else {
        Dafon.ContentPostChange('CENTER', '/ap/b2b/stuffbuy_match.aspx');
    }
}

//依國別資料重新建置區域下拉表格
stuffbuy_match.getSitearea = function() {
    if ($("#stmatchdata_nation").val() != "") {
        DfrcService.getSitearea($("#weblang").val(), $("#stmatchdata_nation").val(), stuffbuy_match.getSiteareaOnSuccess, stuffbuy_match.onFailed);
    } else {
        $("#stmatchdata_area").get(0).options.length = 0;
        $("#stmatchdata_area").get(0).options.add(new Option(dfrc.dropdownlist_please_selected, ""));
    }
}

//利用JSON資料重新建置區域下拉表格
stuffbuy_match.getSiteareaOnSuccess = function(originalRequest) {
    $("#stmatchdata_area").get(0).options.length = 0;
    var jsonContent = JSON.parse(originalRequest); //將JSON文字轉成物件    
    for (var i = 0; i < jsonContent.Table.length; i++) {
        var data = jsonContent.Table[i];
        $("#stmatchdata_area").get(0).options.add(new Option(data.sitearea_name, data.sitearea_code));
    }
    $("#stmatchdata_area").val($("#stmatchdata_area_temp").val());
}

//依物料大類別重新建置物類小類別下拉表格
stuffbuy_match.getStmatchtypeclass02Data = function() {
    if ($("#stmatchtype_mcode").val() != "") {
        DfrcService.getStmatchtypeclass02Data($("#weblang").val(), $("#stmatchtype_mcode").val(), stuffbuy_match.getStmatchtypeOnSuccess, stuffbuy_match.onFailed);
    } else {
        $("#stmatchtype_dcode").get(0).options.length = 0;
        $("#stmatchtype_dcode").get(0).options.add(new Option(dfrc.dropdownlist_please_selected, ""));
    }
}

//利用JSON資料重新建置區域下拉表格
stuffbuy_match.getStmatchtypeOnSuccess = function(originalRequest) {
    $("#stmatchtype_dcode").get(0).options.length = 0;
    var jsonContent = JSON.parse(originalRequest); //將JSON文字轉成物件    
    for (var i = 0; i < jsonContent.Table.length; i++) {
        var data = jsonContent.Table[i];
        $("#stmatchtype_dcode").get(0).options.add(new Option(data.stmatchtype_name, data.stmatchtype_code));
    }
    $("#stmatchtype_dcode").val($("#stmatchtype_dcode_temp").val());
}

