﻿
var propertyArr;

function CreateSelectInfo() {
    var ul = document.getElementById("PropertyContainer");
    var li = document.createElement("li");
    li.setAttribute("id", "PropertyInfo");
    var info = "";
    for (i = 0; i < propertyJson.length; i++) {
        info += "“" + propertyJson[i].name + "”，"
    }
    info = info.substr(0, info.length - 1);
    //  li.innerHTML = info; 
    ul.insertBefore(li, ul.lastChild);
}

//多属性商品处理
function CreateProperties() {
    if (propertyJson.length > 0) {
        propertyArr = new Array(propertyJson.length);
        CreateSelectInfo();
        var ul = document.getElementById("PropertyContainer");
        var limsg = document.createElement("li");

        for (i = 0; i < propertyJson.length; i++) {
            var html = "";
            var li = document.createElement("li");
            li.className = "ppar";
            var property = propertyJson[i];
            html += ("<ul class='ProductProperty'><li style='border-bottom-style:none;'>" + property.name + "：</li>");
            if (property.type == "property") {
                for (p = 0; p < property.values.length; p++) {
                    html += "<li title='" + property.values[p] + "' onclick='SelectProperty(" + i + ",\"" + property.values[p] + "\",\"\",\"" + p + "\")' ><a href='javascript:;' id='p" + p + "'>&nbsp;" + property.values[p] + "&nbsp;</a></li>";
                }
            }
            else if (property.type == "style") {
                for (s = 0; s < property.values.length; s++) {
                    var style = property.values[s];
                    html += "<li title='" + style.stylename + "' onclick='SelectProperty(" + i + ",\"" + style.stylename + "\",\"" + style.url + "\",\"" + s + "\")'>";
                    html += "<a href='javascript:;' style='background:" + style.color + "'  id='s" + s + "'>";
                    if (style.url != "") {
                        html += "<img border='0' src='" + style.url + "' width='30px' height='32px' />"
                    }
                    else if (style.color != "") {
                        html += "&nbsp;";
                    }
                    else {
                        html += "&nbsp;" + style.stylename + "&nbsp;";
                    }
                    html += "</a></li>";
                }
            }
            html += "</ul><div class='clearbox'></div>";

            ul.insertBefore(li, ul.lastChild); li.innerHTML = html;
        }
    }
}



function SetPhoto(photoUrl) {
    if ((photoUrl != undefined) && (photoUrl != "")) {
        document.getElementById("productphoto").src = photoUrl;
    }
}

function SelectProperty(index, value, photoUrl, id) {
    var property = propertyJson[index];

    if (property.type == "property") {

        for (i = 0; i < property.values.length; i++) {
            $('p' + i).className = "";
        }
        $('p' + id).className = "focus";
    }
    else {

        for (i = 0; i < property.values.length; i++) {
            $('s' + i).className = "";
        }
        $('s' + id).className = "focus";

    }
    propertyArr[index] = value;
    var info = "";
    for (i = 0; i < propertyArr.length; i++) {
        if (propertyArr[i] != "") {
            info += "“" + propertyArr[i] + "”，";
        }
    }
    if (info != "") {
        //   document.getElementById("PropertyInfo").innerHTML = "您已选择：<span class='p_font_red'>" + info.substr(0, info.length - 1) + "</span>";

    }
    // SetPhoto(photoUrl);
}

function GetProperties() {
    if (propertyArr == undefined) {
        return "";
    }
    var value = "";
    var hasAllSelect = true;
    var info = "请选择：";
    for (var i = 0; i < propertyArr.length; i++) {
        if (propertyArr[i] == undefined || propertyArr[i] == "") {
            info += "“" + propertyJson[i].name + "”，";
            hasAllSelect = false;
        } else {
            value += propertyArr[i] + "|";
        }
    }
    if (!hasAllSelect) {
        alert(info.substr(0, info.length - 1));

        return "";
    } else {
        return value.substr(0, value.length - 1);
    }
}


function isNumber(str) {
    if (str == "") {
        return false;
    }
    for (var i = 0; i < str.length; ++i) {
        if ((str.charAt(i) < '0') || (str.charAt(i) > '9')) {
            return false;
        }
    }
    return true;
}



function GetProductNum() {


    var number = document.getElementById("txtNum").value;
    if (!isNumber(number)) {
        alert("数量必须为数字");
        document.getElementById("txtNum").value = '';
        document.getElementById("txtNum").focus();
        return 0;
    }
    if (number <= 0) {
        alert("定购数量不能小于等于0");
        document.getElementById("txtNum").value = '';
        document.getElementById("txtNum").focus();
        return 0;
    } else if (number > 100000) {
        alert("定购数量不能大于100000");
        document.getElementById("txtNum").value = '';
        document.getElementById("txtNum").focus();
        return 0;
    }

    return number;
}

CreateProperties();


function Buy() {

    var url = "/Shop/ShoppingCart.aspx?Action=AddToCart&ID=" + TemplateID + "&DiyProductID=" + DiyProductID;
    var property = GetProperties();

    if (propertyJson.length > 0 && property == "") {
        return;
    }
    if (property != '') {
        url += "&Property=" + property;
    }
    var proNum = GetProductNum();

    if (proNum == 0)
        return false;

    else

        url += "&Num=" + proNum;

    url = encodeURI(url);
   // window.location.href = url;

    var myAjax = new Ajax.Request(url, { method: 'post', onSuccess: GoToShoppingCart });



}
function GoToShoppingCart() {
   
    window.location.href = "/Shop/ShoppingCart.aspx";

}

