var search_text = '查询货号、产品名称、品牌、品类';
function change_submit(id, form){
    if(document.getElementById(id).value == search_text){
        $("input#" + id).val('');
    }
    form.submit();
}

function change_onfocus(id, value){
    if (value == search_text){
        $("input#" + id).val('');
    }
}

function change_onblur(id, value){
    if (value ==''){
        $("input#" + id).val(search_text);
    }
}

function setAddProductId(name) {
    document.addform.add_product_id.value = name;
    if (document.addform.quantity == null) return;
    if (name == '' || name == 'NULL' || isVirtual(name)) {
        document.addform.quantity.disabled = true;
        var elem = document.getElementById('product_id_display');
        var txt = document.createTextNode('');
        if(elem.hasChildNodes()) {
            elem.replaceChild(txt, elem.firstChild);
        } else {
            elem.appendChild(txt);
        }
    } else {
        document.addform.quantity.disabled = false;
        var elem = document.getElementById('product_id_display');
        var txt = document.createTextNode(name);
        if(elem.hasChildNodes()) {
            elem.replaceChild(txt, elem.firstChild);
        } else {
            elem.appendChild(txt);
        }
    }
}

function toggleAmt(toggle) {
    if (toggle == 'Y') {
        changeObjectVisibility("add_amount", "visible");
    }

    if (toggle == 'N') {
        changeObjectVisibility("add_amount", "hidden");
    }
}

function findIndex(name) {
    for (i = 0; i < OPT.length; i++) {
        if (OPT[i] == name) {
            return i;
        }
    }
    return -1;
}

function getList(name, index, src, max_size) {
    currentFeatureIndex = findIndex(name);

    if (currentFeatureIndex == 0) {
        // set the images for the first selection
        if (IMG[index] != null) {
            if (document.images['mainImage'] != null) {
                document.images['mainImage'].src = IMG[index];
                detailImageUrl = DET[index];
            }
        }

        // set the drop down index for swatch selection
        document.forms["addform"].elements[name].selectedIndex = (index*1)+1;
    }

    if (currentFeatureIndex < (OPT.length-1)) {
        // eval the next list if there are more
        var selectedValue = document.forms["addform"].elements[name].options[(index*1)+1].value;
        eval("list" + OPT[(currentFeatureIndex+1)] + selectedValue + "()");

        // set the product ID to NULL to trigger the alerts
        setAddProductId('NULL');
    } else {
        // this is the final selection -- locate the selected index of the last selection
        var indexSelected = document.forms["addform"].elements[name].selectedIndex;

        // using the selected index locate the sku
        var sku = document.forms["addform"].elements[name].options[indexSelected].value;

        // set the product ID
        setAddProductId(sku);

        // check for amount box
        toggleAmt(checkAmtReq(sku));
    }

    set_class(index, max_size);
}

function set_class(index, max_size){
    if(document.getElementById("suit_" + index).className == "suit"){
        for(var i = 0; i < max_size; i++){
            document.getElementById("suit_" + i).className = "";
            setAddProductId('NULL');
        }
    } else {
        for(var i = 0; i < max_size; i++){
            if(i == index){
                document.getElementById("suit_" + i).className = "suit";
                continue;
            } else {
                document.getElementById("suit_" + i).className = "";
            }
        }
    }
}

function validate(x){
    var msg=new Array();
    msg[0]="Please use correct date format [yyyy-mm-dd]";

    var y=x.split("-");
    if(y.length!=3){ alert(msg[0]);return false; }
    if((y[2].length>2)||(parseInt(y[2])>31)) { alert(msg[0]); return false; }
    if(y[2].length==1){ y[2]="0"+y[2]; }
    if((y[1].length>2)||(parseInt(y[1])>12)){ alert(msg[0]); return false; }
    if(y[1].length==1){ y[1]="0"+y[1]; }
    if(y[0].length>4){ alert(msg[0]); return false; }
    if(y[0].length<4) {
        if(y[0].length==2) {
            y[0]="20"+y[0];
        } else {
            alert(msg[0]);
            return false;
        }
    }
    return (y[0]+"-"+y[1]+"-"+y[2]);
}
function display_radio_error (radio, text) {
    if(radio != null && radio[0] != null){
        display_input_error(radio[0].name + "_error", text);
    }
}
function valid_radio (radio, text) {
    var f = false;
    for (var i = 0; i < radio.length; i++) {
        if (radio[i].checked) {
            f = true;
        }
    }
    if (!f) {
        display_radio_error(radio, text);
        jump_to_error(radio[0].name + "_error_anchor");
    } else {
        hide_input_error(radio[0].name + "_error");
    }
    return f;
}

function display_input_error (element_name, text) {
     $("#" + element_name).html(text);
     $("#" + element_name).css("display","inline");
     $("#" + element_name).css("color","red");
     $("#" + element_name).css("marginLeft","10px");
 }

 function hide_input_error (element_name) {
     $("#"+element_name).html("");
     $("#" + element_name).css("display","none");
 }

function jump_to_error(anchor){
    window.location.hash = anchor;
}

function valid_new_address (form) {
    var valid = true;
    if (form.fullName.value == "") {
        display_input_error("fullName_error", "请输入姓名");
        valid = false;
    } else {
        hide_input_error("fullName_error");
    }
    if (form.stateProvinceGeoId.value == "" && form.stateProvinceGeoId.options.length > 1) {
        display_input_error("stateProvinceGeoId_error", "请选择省份");
        $("#geoId_error").css("display", "table-row");
        valid = false;
    } else {
        hide_input_error("stateProvinceGeoId_error");
    }
    if (form.provinceCityGeoId.value == "" && form.provinceCityGeoId.options.length > 1) {
        display_input_error("provinceCityGeoId_error", "请选择城市");
        $("#geoId_error").css("display", "table-row");
        valid = false;
    } else {
        hide_input_error("provinceCityGeoId_error");
    }
    if (form.cityCountyGeoId.value == "" && form.cityCountyGeoId.options.length > 1) {
        display_input_error("cityCountyGeoId_error", "请选择区县");
        $("#geoId_error").css("display", "table-row");
        valid = false;
    } else {
        hide_input_error("cityCountyGeoId_error");
    }
    if (form.address1.value == "") {
        display_input_error("address1_error", "请输入详细地址");
        valid = false;
    } else {
        hide_input_error("address1_error");
    }
    if (form.postalCode.value == "") {
        display_input_error("postalCode_error", "请输入邮政编码");
        valid = false;
    } else {
        hide_input_error("postalCode_error");
    }
    if (form.telephone.value == "" && form.mobile.value == "") {
        display_input_error("mobile_error", "手机和电话必须填写任意一个");
        valid = false;
    } else {
        hide_input_error("mobile_error");
    }
    if(!valid){
        jump_to_error("fullName_error_anchor");
    }
    return valid;
}
function validate_form (form) {
    var valid = valid_radio(form.checkOutPaymentId, "请选择付款方式");
    valid = valid_radio(form.shippingTimePreference, "请选择方便的送货时间") && valid;
   // valid = valid_radio(form.shipmentRegionTypeId, "请选择送货区域") && valid;
    valid = valid_radio(form.shipMethod, "请选择发货方式 ") && valid;
    if ($("input#createnewaddress").val() == "create") {
        valid = valid_new_address(form) && valid;
    } else if ($("input#createnewaddress").val() == "update"){
        valid = valid_new_address(form) && valid;
    } else {
        valid = valid_radio(form.shipping_contact_mech_id, "请选择送货地址") && valid;
    }
    return valid;
}
function validate_submit (form, mode, value) {
    if (validate_form(form)) {
        submitForm(form, mode, value);
    }
}

function deletePostalAddress(index, form){
    ok = confirm("确认删除本收货地址？");
    if(ok){
        $("input#createnewaddress" + index).val("delete");
        form.submit();
    }
}

function retriveAddressInfo(level, p_id, c_id, c_c_id){
    $.getJSON('../js/resources.json', function(json) {
        MSG = json;
        $.post(MSG.url.basePath + MSG.url.geoEmbeded, {
            level: level,
            parent_value: $("select#" + p_id).val()
        }, function(info) {
            if(c_c_id != ''){
                $("#td_" + c_c_id).html("<select id='cityCountyGeoId' name='cityCountyGeoId'><option value=''>请选择</option></select>");
            }
            $("#td_" + c_id).html(info);
        });
    });
}