//前台專用 javascript
/**
 *  日誌按讚功能
 */
function DiaryPraise(diary_id,member_id,obj){
    $.ajax({
        url : "ajax.php",
        type : 'post',
        dataType: 'json',
        data :{
            diary_id:diary_id,
            member_id:member_id,
            method:'diaryPraise'
        },
        success:function(response){
            $.each(response,function(Key,Val){
                if(Key=='login'){
                    window.location.href=Val;
                }
                if(Key=='response'){

                    $(obj).html(Val);
                }
            });
        },
        error:function(error){
            alert('Error');
        }
    });
}

/**
 * id : 收藏物品的索引
 * type : single-單項商品 combo-組合商品 diary-日誌 photo-相片
 */
function Collect(id,type) {
    $.ajax({
        url : "ajax.php",
        type : 'post',
        dataType: 'json',
        data:{
            id:id,
            type:type,
            method:'collect'
        },
        success:function(response){
            $.each(response,function(Key,Val){
                if(Key=='login'){
                    window.location.href=Val;
                }
                if(Key=='response'){
                    alert(Val);
                }
            });
        },
        error:function(error){
            alert('Error');
        }
    });
}
/**
 * 移除收藏功能
 * id 收藏的索引
 */
function DelCollect(id) {
    $.ajax({
        url : "ajax.php",
        type : 'post',
        dataType: 'json',
        data:{
            id:id,
            method:'delcollect'
        },
        success:function(response){
            $.each(response,function(Key,Val){
                if(Key=='reload'){
                    window.location.href=window.location.href;
                }
            });
        },
        error:function(error){
            alert('Error');
        }
    });
}

/**
 * 加入購物車
 * relevance_id 商品索引 product_details_id  product_combo_id
 * type single-單項商品 combo-組合商品
 * categories_id 特殊分類 24小時商品 以及 海外詢價  為兩種特殊分類 空值 為一般分類
 */
function AddCart(relevance_id,type,categories_id) {
    $.ajax({
        url : "ajax.php",
        type : 'post',
        dataType: 'json',
        data:{
            relevance_id:relevance_id,
            type:type,
            categories_id:categories_id,
            method:'addcart'
        },
        success:function(response){
            $.each(response,function(Key,Val){
                if(Key=='login'){
                    window.location.href=Val;
                }
                if(Key=='response'){
                    alert(Val);
                }
            });
        },
        error:function(error){
            //            alert(error);
            alert('Error');
        }
    });
}
/**
 * 刪除購物車
 * relevance_id 商品索引 product_details_id  product_combo_id
 * type single-單項商品 combo-組合商品
 * categories 特殊分類
 */
function DelCart(categories,type,relevance_id,obj) {
    $.ajax({
        url : "ajax.php",
        type : 'post',
        dataType: 'json',
        data:{
            relevance_id:relevance_id,
            type:type,
            categories:categories,
            method:'delcart'
        },
        success:function(response){
            $.each(response,function(Key,Val){
                if(Key=='response'){
                    alert(Val);
                    if(strDecode(categories)=='time') $(obj).parent().parent().next().remove(); 
                    $(obj).parent().parent().remove();                        
                    RefreshCart(categories,'');
                }
            });
        },
        error:function(error){
            //            alert(error);
            alert('Error');
        }
    });
}
function RefreshCart(categories,url) {      
    categories = strDecode(categories); 
    //var table = $('table#'+categories+'Table');
    var documentType = categories!='overseas'?new Array('order','24hours','time'):new Array('overseas');
    var total = 0;
    var tmp = 0;
    var data = '';
    var CheckStock = true;
    var stockMsg = '';
    var table ='';     
    for(var key in documentType){  
      data='' ;
      table = $('table#'+documentType[key]+'Table');           
      table.find('tr[mark=dataTr]').each(function(){
          tmp = (documentType[key]!='time'?$(this).find('[mark=price]').html():$(this).find('[mark=price]').val()) * $(this).find('[mark=num]').val() ;                
          $(this).find('[mark=sum]').html(tmp)
          total += tmp;              
          data +=  (data==''?'':'-|-')
          + strDecode($(this).find('[mark=type]').val())
          + '||'
          + strDecode($(this).find('[mark=relevance_id]').val())
          + '||'
          + $(this).find('[mark=num]').val()
          if(documentType[key]!='time'){ 
              if(CheckStock==true){                       
                  CheckStock = CheckProductStock(strDecode($(this).find('[mark=type]').val()),strDecode($(this).find('[mark=relevance_id]').val()),$(this).find('[mark=num]').val());
                  if(CheckStock==false){
                      stockMsg = $(this).find('[mark=title]').html();
                      if($(this).find('[mark=color]').size()==1){
                          stockMsg += '  '+$(this).find('[mark=color]').html();
                      }
                      if($(this).find('[mark=specification]').size()==1){
                          stockMsg += '  '+$(this).find('[mark=specification]').html();
                      }
                      alert(stockMsg+'  庫存不足');
                  }
              }
          }else{
                ActiveNumCheck($(this).find('[mark=title]').html(),$(this).find('[mark=limit_order]').val(),$(this).find('[mark=order_num]').val(),$(this).find('[mark=num]').val());
                
          }
        })
        if(CheckStock==true){
          categories = strEncode(documentType[key]);
          data = strEncode(data);                
          RefreshCartData(categories,url,data,total);
        }
    }
    if(strDecode(categories)!='overseas'){ 
        $('table#timeTable').find('[mark=total]').html(total);
    }else{
        table.find('[mark=total]').html(total);
    }
    if(CheckStock==false){
        return;
    }              
}  

function RefreshCartData(categories,url,data,total){  
    $.ajax({
        url : "ajax.php",
        type : 'post',
        dataType: 'json',
        data:{
            categories:categories,
            data:data,
            method:'refresh'
        },
        success:function(response){
            $.each(response,function(Key,Val){  
                if(Key=='response' && url !=null){
                    //if(url=='overseas' || url=='24hours' ){
                    if(url=='overseas'){
                        $.ajax({
                            url: "ajax.php",
                            type : 'post',
                            dataType: 'json',
                            data:{
                                url: url,
                                total: total,
                                method:'inquiry'
                            },
                             success:function(response){
                                 $.each(response,function(key,val){
                                     if(key=='response'){
                                         alert(val);
                                     }
                                     if(key=='reload'){
                                         alert(val);
                                         window.location.href=window.location.href;
                                     }
                                 })
                             },
                            error:function(error){
//                                alert(error);
                                alert('Error');
                            }
                        });
                    }else{
                        if(url!=''){
                            window.location.href = url;
                        }
                    }

                }
            });
        },
        error:function(error){

            alert('Error');
        }
    });
}




function CheckOut(categories,url) {
    RefreshCart(categories,url);
}
//改變網站字型大小
function WebFontSize(size){
    Login('ajax.php?method=WebFontSize&size='+encodeURIComponent(size));
    window.location.href = window.location.href;
}

function ActiveNumCheck(title,limit_order,order_num,num) {
    if(num!=0){ 
        if(parseInt(num,10)>parseInt(limit_order,10) || parseInt(num,10)>parseInt(order_num,10)){
            alert(title+'\n份數不可超過限制份數 和 剩餘份數');     
        } 
    }else{
        alert('份數必須至少一份');    
    }
}

//活動加入購物車
function CheckTime(categories,type) {   
    var num=document.getElementById('num').value;
    $.ajax({
        url : "ajax.php",
        type : 'post',
        dataType: 'json',
        data:{
            id:categories,
            type:type,
            num:num,
            method:'timeaddcart'
        },
        success:function(response){
            $.each(response,function(Key,Val){
               if(Key=='response'){
                    alert(Val);
                }
            });
        },
        error:function(error){
            alert('Error');
        }
    });

}

