﻿window.addEvent('domready', function(){
    // CSS: noWhiteSpace
    // 不允許輸入空白
    $$('input.noWhiteSpace[type=text]').each(function(textDom){
        textDom.addEvents({            
            keydown: function(event){
                event = new Event(event);
                if(event.key == 'space'){
                    event.stop();
                }
            },
            blur: function(event){
                event = new Event(event);
                if(event.target.value.indexOf(' ') > -1){
                    event.target.value = '';
                }
            }
        });
    });
    // CSS: length80
    // textarea的內容長度限制在80
    $$('textarea.length80').each(function(textareaDom){
        textareaDom.addEvents({
            keydown: function(event){
                event = new Event(event);
                if(event.target.value.length > 80){
                    textareaDom.value = textareaDom.value.substring(0, 80);
                }
            },
            blur: function(event){
                event = new Event(event);
                if(event.target.value.length > 80){
                    event.target.value = event.target.value.substring(0, 80);
                }                
            }
        });
    });
    //設定所有的text欄位不能輸入單引號
    $$('input[type=text]').each(function(textDom){
        textDom.addEvents({            
            keydown: function(event){
                event = new Event(event);                
                if(event.code == 222){
                    event.stop();
                }
            },
            blur: function(event){
                event = new Event(event);
                if(event.target.value.indexOf("'") > -1){
                    event.target.value = '';
                }
            }
        });
    });
    //設定所有的password欄位不能輸入單引號
    $$('input[type=password]').each(function(textDom){
        textDom.addEvents({            
            keydown: function(event){
                event = new Event(event);                
                if(event.code == 222){
                    event.stop();
                }
            },
            blur: function(event){
                event = new Event(event);
                if(event.target.value.indexOf("'") > -1){
                    event.target.value = '';
                }
            }
        });
    });
}); // end with window domready event

// 檢查產品數量
function vFCheckQuantity(quantityDom){
    var vQuantity = parseInt(quantityDom.value);    
    // 檢查是否大於0
    if(vQuantity < 0){
        alert("The quantity must greater than 0!");
        quantityDom.value = quantityDom.defaultValue;
        quantityDom.focus();
        quantityDom.select();
        return false;
    }
    // 檢查是否不是數字NaN(Not a Number)
    if(isNaN(vQuantity)){
        quantityDom.value = quantityDom.defaultValue;
        quantityDom.focus();
        quantityDom.select();
        return false;
    } 
    // 避免小數點
    quantityDom.value = vQuantity;
    return true;
}

// 解析產品規格，vSpecificationValue 為傳入的規格字串
//#線4mm/5mm/6mm/7mm/8mm/9mm/10mm#色BLACK/GREEN/WHITE/YELLO/BLUE/ORANG/BROWN/GRAY/INDIG/PURPL/GOLD/SILVE/NATUR#色BLACK/GREEN/WHITE/YELLO/BLUE/ORANG/BROWN/GRAY/INDIG/PURPL/GOLD/SILVE/NATUR#電220V/125V/250V/24V/12V/9V/7.5V/6V/4.5V/3V/1.5V#安0.1A~30A#歐0.1~10MΩ
function vFGetSpecificationObject(specificationValue){
    // vColorFlag用來檢查是否有兩種顏色規格
    var colorFlag = false;
    // vSpecificationObject代表規格物件，若有對應的規格就新增此規格的屬性
    var specificationObject = new Object();
    // 以'#'當分隔字串，回傳值是個陣列
    var specificationArray = specificationValue.split("#");
    // 注意index從1開始，因為規格字串以#開頭，所以第0個位置為空字串
    for (var index = 1; index < specificationArray.length; index++) {
        // 若有取到線的規格，ex: 線4mm/5mm/6mm/7mm/8mm/9mm/10mm
        if (specificationArray[index].indexOf('線') != -1) {
            // 去掉'線'字首字元，ex: 4mm/5mm/6mm/7mm/8mm/9mm/10mm
            specificationArray[index] = specificationArray[index].substring(1);
            // 以'/'當分隔字串，回傳值是個陣列，在此地方新增'cable'屬性儲存線的規格陣列
            specificationObject.cable = specificationArray[index].split("/");
        }
        if (specificationArray[index].indexOf('色') != -1) {
            if (!colorFlag) {
                colorFlag = true;                
                specificationArray[index] = specificationArray[index].substring(1);
                // [Black,Green,White,Yellow,Blue,Orange,Brown,Gray,Indigo,Purple,Gold,Silver,Natural]
                specificationObject.color = specificationArray[index].split("/");
            }
            else {
                specificationArray[index] = specificationArray[index].substring(1);
                specificationObject.color2 = specificationArray[index].split("/");
            }
        }
        if (specificationArray[index].indexOf('電') != -1) {
            specificationArray[index] = specificationArray[index].substring(1);
            specificationObject.voltage = specificationArray[index].split("/");
        }
        if (specificationArray[index].indexOf('安') != -1) {
            specificationArray[index] = specificationArray[index].substring(1);
            specificationObject.ampere = specificationArray[index].split("/");
        }
        if (specificationArray[index].indexOf('歐') != -1) {
            specificationArray[index] = specificationArray[index].substring(1);
            specificationObject.ohm = specificationArray[index].split("/");
        }
    }
    return specificationObject;
}