<!--
var _goodsModelArray={};
var _specValuesArray={};
var _goodsSpecIdArray=[];
var _goodsSpecSelected={};
var _goodsSelectedId=0;
var _selectedText='未选择';
var _specNameStr='_goodsSpecOf_';
 
function initSpecSelected()
{
    var r=/(?!=\?|&)goodsid=([\d]*)/i;   
    var urlParams=location.href.match(r);    
    var goodsId = (urlParams!=null && urlParams.length>1) ? parseInt(urlParams[1]) : 0; 
    selectedGoods(_goodsModelArray[goodsId]);
}
function checkSpecValueInGoods(specId,valueId)
{
    for(var goods in _goodsModelArray){          
        goodsModel=_goodsModelArray[goods];      
        var isSelected=true;
        for(var spec in goodsModel.SpecValues){   
            if(!((spec==specId && valueId==goodsModel.SpecValues[spec].Value) || (spec!=specId && (_goodsSpecSelected[spec]==null || _goodsSpecSelected[spec]==0 || _goodsSpecSelected[spec]==goodsModel.SpecValues[spec].Value)))){
                isSelected=false;
                break;
            }      
        }      
        if(isSelected){ 
            return true;
            break;              
        }
    }
    return false;
}
function selectedGoods(goodsModel)
{
    if(!goodsModel){  
        for(var goods in _goodsModelArray){           
            goodsModel=_goodsModelArray[goods];
            break;
        }
    } 
    if(goodsModel){    
        _goodsSelectedId=goodsModel.Id;       
        for(var spec in goodsModel.SpecValues){          
            selectSpecValue(docEle(_specNameStr+spec+'_'+goodsModel.SpecValues[spec].Value),spec,goodsModel.SpecValues[spec].Value);
        }
        changeGoodsParam();
    }
}
function changeSelectedGoods()
{
    var selGoodsModel=false;    
    
    for(var goods in _goodsModelArray){     
        goodsModel=_goodsModelArray[goods];         
        var isSelected=true;     
        for(var spec in goodsModel.SpecValues){          
            if(_goodsSpecSelected[spec]!=goodsModel.SpecValues[spec].Value){
                isSelected=false;
                break;
            }
        }
        if(isSelected){ 
            selGoodsModel=goodsModel;
            break;              
        }
    }
    if(selGoodsModel){
        _goodsSelectedId=selGoodsModel.Id;       
    }else {
        _goodsSelectedId=-1;
    }
    changeGoodsParam();
}
function changeGoodsParam()
{
    if(_goodsSelectedId>0){
        var goodsModel=_goodsModelArray[_goodsSelectedId];
        if(docEle('productPrice')){
            docEle('productPrice').innerHTML=goodsModel.Price;
        }
        if(docEle('productCode')){
            docEle('productCode').innerHTML=goodsModel.Code;
        }   
        if(docEle('productWeight')){
            docEle('productWeight').innerHTML=goodsModel.Weight;
        }  
        if(docEle('productTotalCount')){
            checkIsUNumber(docEle('productTotalCount'),1);
        }
        if(docEle('productTotalMoney')){
            changeTotalMoney();
        }     
    }else{      
        if(docEle('productPrice')){
            docEle('productPrice').innerHTML='';
        }
        if(docEle('productCode')){
            docEle('productCode').innerHTML='';
        }   
        if(docEle('productWeight')){
            docEle('productWeight').innerHTML='0';
        }  
        if(docEle('productTotalCount')){
            checkIsUNumber(docEle('productTotalCount'),0);
        }
        if(docEle('productTotalMoney')){
            docEle('productTotalMoney').innerHTML=''; 
        }    
    }
}
function changeTotalMoney()
{
    if(_goodsSelectedId>0 && docEle('productTotalMoney')){
        var goodsModel=_goodsModelArray[_goodsSelectedId];
        var totalCount=docEle('productTotalCount').value?docEle('productTotalCount').value:0;
        var sign='';
        var price=0;       
        var r=/^([^\d.]*)([\d.]+)$/i;
        var mons=goodsModel.Price.match(r);
        if(mons!=null && mons.length==3){
            sign=mons[1];
            price=mons[2];
        }
        docEle('productTotalMoney').innerHTML=formatMoney(price*totalCount,sign);       
    }
}
function increaseNumber(id){
    var obj=docEle(id);
    if(obj){            
        var count=parseInt(obj.value);
        obj.value=count+1;
        checkIsUNumber(obj,1);       
    }
}
function decreaseNumber(id){
    var obj=docEle(id);
    if(obj){        
        var count=parseInt(obj.value);
        obj.value=count-1;
        checkIsUNumber(obj,1);        
    }
}
function checkIsUNumber(obj,defValue)
{
    if(obj!=null && obj.type=="text")
    {
        var r=/[^\d]*([\d]*)[\D]*/; 
        var value=obj.value;
        var P=value.match(r);
        if(P[1]==null || P[1]==''){  
            value=(defValue!=null&&typeof(defValue)=='number')?defValue:1;
        }else{
            value=P[1];
        }
        try{
            value=parseInt(value);
            var maxValue=0;
            if(_goodsSelectedId>=0){
                maxValue=_goodsModelArray[_goodsSelectedId].Store;
            }
            if(isNaN(value)){
                value=1;
            }
            if(value>0 && value<=maxValue){
                obj.value=value;
            }else if(value<=0 && maxValue>0){
                obj.value='1';
            }else if(maxValue>0){
                obj.value=maxValue;
            }else{
                obj.value='0';
            }
        }catch(e){
            obj.value='0';
        }
    }
    changeTotalMoney();
}
function initProductGoods(goodsArray,goodsSpecIdArray,specValuesArray)
{
    _goodsModelArray=goodsArray;
    _goodsSpecIdArray=goodsSpecIdArray;
    _specValuesArray=specValuesArray;

    addCssStyle('goods');     
    
    if(goodsSpecIdArray!=null && goodsSpecIdArray.length)
    {   
        var innerHtml='';
        for(var i=0;i<goodsSpecIdArray.length;i++)
        {
            innerHtml+=createSpecHtml(_specNameStr+goodsSpecIdArray[i],specValuesArray[goodsSpecIdArray[i]]);
        }
        var obj=docEle('productGoods');
        if(obj)
        {
            obj.innerHTML=innerHtml;
        }        
    }else if(_goodsModelArray!=null){
        for(var goods in _goodsModelArray){                 
            _goodsSelectedId=goods.Id;
            break;
        }
    }
    addLoadEvent(initSelectedGoodsHtml);
    addLoadEvent(changeTotalMoney);
    initSpecSelected();
}
function initSelectedGoodsHtml()
{
    obj=docEle('productSelectedGoods');
    if(obj && _goodsSpecIdArray!=null && _goodsSpecIdArray.length>0)
    {
        innerHtml='';
        for(var i=0;i<_goodsSpecIdArray.length;i++)
        {
            innerHtml+='<h2>'+_specValuesArray[_goodsSpecIdArray[i]].specName+'：<span name="selected'+_specNameStr+_goodsSpecIdArray[i]+'">';
            if(_goodsSpecSelected[_goodsSpecIdArray[i]]){
                innerHtml+=_specValuesArray[_goodsSpecIdArray[i]].specValues[_goodsSpecSelected[_goodsSpecIdArray[i]]].value;
            }else{
                innerHtml+=_selectedText;
            }            
            innerHtml+='</span></h2>';
        }  
        obj.innerHTML=innerHtml;
        obj.style.display='';        
    }else if(obj){
        obj.style.display='none';
        obj.innerHTML='';
    }    
    obj=docEle('productTotalCount');
    if(obj){        
        var c=/(?!=\?|&)count=([\d]*)/i;
        var urlParams=location.href.match(c);
        var goodsCount = (urlParams!=null && urlParams.length>1) ? parseInt(urlParams[1]) : 1;    
        obj.value=goodsCount;
        checkIsUNumber(obj,1); 
    } 
}
function createSpecHtml(id,specArray)
{ 
    var innerHtml='';   
    if(specArray.isImage){
        innerHtml+='<div class="goods_image">';
    }else{
        innerHtml+='<div class="goods_text">';
    }
    innerHtml+='<p>'+specArray.specName+'：</p><span name="selected'+id+'">'+_selectedText+'</span>';
    innerHtml+='<ul>';
    for(var spec in specArray.specValues)
    {
        innerHtml+="<li id=\""+id+"_"+specArray.specValues[spec].id+"\" name=\""+id+"\" onmouseover=\"checkSelectSpecValue(this,"+specArray.specId+","+spec+");\" onmouseout=\"this.style.borderColor='';\">";
        if(specArray.isImage){
            innerHtml+='<img src="'+specArray.specValues[spec].image+'" title="'+specArray.specValues[spec].value+'" alt="'+specArray.specValues[spec].value+'" width="30px" height="30px" />';
        }else{
            innerHtml+=specArray.specValues[spec].value;
        }
        innerHtml+='</li>';
    } 
    innerHtml+='</ul>';
    innerHtml+='</div>';
    return innerHtml;
}
function checkSelectSpecValue(obj,specId,valueId)
{
    if(obj.className!='on'){      
        if(checkSpecValueInGoods(specId,valueId)){  
            obj.style.borderColor='#FF9900';      
            obj.style.cursor='pointer';
            obj.onclick=function(){
                selectSpecValue(this,specId,valueId);
                changeSelectedGoods();
            };            
        }else{ 
            obj.style.borderColor='';
            obj.style.cursor='not-allowed';
            obj.onclick=function(){
                return false;
            };            
        }
    }else{
        obj.onclick=function(){         
            changeSpecValueStyle(obj,'');
            _goodsSelectedId=-1;
            _goodsSpecSelected[specId]=null;
            setSpecValueText(obj.getAttribute('name'),specId,0);
            changeSelectedGoods();
        };   
    }
}
function selectSpecValue(obj,specId,valueId){    
    if(obj.getAttribute('name')){
        var objs=getTagElements(obj.tagName,'name',obj.getAttribute('name'));   
        if(objs&&objs.length>0){  
            for(var i=0;i<objs.length;i++){ 
                if(objs[i].className=='on'){
                    changeSpecValueStyle(objs[i],'');
                }
            }
        }       
         _goodsSpecSelected[specId]=valueId;
        setSpecValueText(obj.getAttribute('name'),specId,valueId);
        changeSpecValueStyle(obj,'on');
    } 
}
function changeSpecValueStyle(obj,className)
{
    if(className=='on'){
        obj.className='on';   
        obj.mouseover='';
        obj.mouseout='';
    }else if(className==''){
        obj.className='';
        obj.mouseover="this.style.borderColor='#FF9900';";
        obj.mouseout="this.style.borderColor='';";
    }
}
function setSpecValueText(specName,specId,valueId)
{
    var objs=getTagElements('span','name','selected'+specName);
    if(objs&&objs.length>0){  
        for(var i=0;i<objs.length;i++){ 
            if(_specValuesArray[specId]&&_specValuesArray[specId].specValues[valueId]){
                objs[i].innerHTML=_specValuesArray[specId].specValues[valueId].value;
            }else{
                objs[i].innerHTML='';
            }
        }
    } 
}
function getTagElements(tag, name, value)
{   
    var arr = new Array(); 
    var elem = document.getElementsByTagName(tag);
    if(elem!=null)
    {   
        if(name!='')
        {
            for(i = 0,iarr = 0; i < elem.length; i++) {
                var att = elem[i].getAttribute(name);
                if(att!=null && att == value) {
                     arr[iarr] = elem[i];
                     iarr++;
                }
            }
        }
        else
        {
            arr=elem;
        }
    }
    return arr;
} 
function addCssStyle(fileName) { 
    var fileref=document.createElement("link");
    fileref.setAttribute("rel", "stylesheet");
    fileref.setAttribute("type", "text/css");
    fileref.setAttribute("href", '/CustomAction/goods/css/'+fileName+'.css');
    if(document.getElementsByTagName("head")){
        document.getElementsByTagName("head")[0].appendChild(fileref);
    }else if(document.getElementsByTagName("body")){
        document.getElementsByTagName("body")[0].appendChild(fileref);
    }
}
function addSelectedGoodsToCart(e,cookieName,lanid,proId)
{    
    var count=docEle('productTotalCount')?docEle('productTotalCount').value:1;
    if(count>0 &&(_goodsSpecIdArray==null || _goodsSpecIdArray.length==0)){
        doActionsForAddGoodsToCart(e,lanid,proId,0,count,cookieName);       
    }else if(count>0 && (_goodsSpecIdArray!=null && _goodsSpecIdArray.length>0 && _goodsSelectedId>0)){
        doActionsForAddGoodsToCart(e,lanid,proId,_goodsSelectedId,count,cookieName);  
    }else if (count>0){
        alert('请先选择货品规格');
        return false;
    }else{
        alert('购买数量必须大于0');
        return false;
    }
}
-->
