﻿/* ******************************* Logic Done By Mishal ************

Js file used for the logic when Compare check box is clicked or remove button is clicked 
It will set the approprite Cookie value as well will work with IE and Firefox also.

*****************************************/

function setCookie(SKU_Headline, SKU_Id, Category_Id)
{  
    var curCookie_Value = "";
    var curCookie = ""
    if(getcookie("Compare_List") != null)
    {
        curCookie_Value = getcookie("Compare_List");
        if(curCookie_Value == "")
        {
            curCookie = "Compare_List=" + SKU_Id + "`" + Category_Id + "`" + SKU_Headline;
        }
        else
        {
            curCookie = "Compare_List=" + curCookie_Value + "^" + SKU_Id + "`" + Category_Id + "`" + SKU_Headline;      
        }
    }
    else
        curCookie = "Compare_List=" + SKU_Id + "`" + Category_Id + "`" + SKU_Headline;      
    
    document.cookie = curCookie;
}

function AddRow(SKU_Headline, SKU_Id, Category_Id)
{   
    var SKU_Info="";
    var source = document.getElementById(document.getElementById("hidControl_Client").value);  
    //var SKU_Info = SKU_Headline.split('`');
    //Swapnila added
     if((SKU_Headline).indexOf('  ') != -1)
        {
             SKU_Info = SKU_Headline.split('  ');
             SKU_Info =SKU_Info[1].split('`');
        }
        else
        {
            SKU_Info =SKU_Headline.split('`');
        }
    cnt = source.rows.length;                          
    row = source.insertRow(cnt-1);
    cell= row.insertCell(0);
    cell.innerHTML = "<h1><input title='Remove from compare List' type='button' Class='closebtn' id='btn_" + SKU_Id + "_" + Category_Id + "' onclick='btnRemove_Click("+SKU_Id + "," + Category_Id + ",this)' />&nbsp;&nbsp;&nbsp;" + SKU_Info[0] + "</h1><h2>" + SKU_Info[1] + "</h2>";    
}

function btnRemove_Click(SKU_Id, Category_Id, ctrl)
{    
    var sPath = window.location.pathname;
    setCookie_After_Delete(SKU_Id, Category_Id,ctrl);    
    var source = document.getElementById(document.getElementById("hidControl_Client").value);
    source.deleteRow(ctrl.parentNode.parentNode.parentNode.rowIndex);    
    var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
    if((sPage.toLowerCase() == "frmProduct_Listing.aspx".toLowerCase()) || (sPage.toLowerCase() == "frmProduct_Detail.aspx".toLowerCase()))    
        chkUncheck(SKU_Id, Category_Id)
    if(source.rows.length <= 3)
        source.rows[source.rows.length-1].style.display = 'none'
    return false;
}
function setCookie_After_Delete(SKU_Id, Category_Id,ctrl)
{
    var curCookie_Value = "";
    var curCookie = ""
    if(getcookie("Compare_List") != null)
    {
        curCookie_Value = getcookie("Compare_List");        
    }
    var final_List = curCookie_Value.split("^");
    final_List.splice((ctrl.parentNode.parentNode.parentNode.rowIndex-1),1);
    curCookie = final_List.join("^")          
    curCookie = "Compare_List=" + curCookie;
    document.cookie = curCookie;
}
function chkUncheck(SKU_Id, Category_Id)
{
    try
    {
        document.getElementById("chk_" + SKU_Id + "_" + Category_Id).checked = false;
    }
    catch(err)
    {
        //alert(err);
    }
}

function btnRemove_Click_By_Uncheck(SKU_Id, Category_Id)
{    
    /* This function will uncheck the chk Box and remove the list from the table and set the cookie as well */
    // First Set the cookie
    try
    {
        var ctrl = document.getElementById("btn_" + SKU_Id + "_" + Category_Id);
        var source = document.getElementById(document.getElementById("hidControl_Client").value); 
        var curCookie_Value = "";
        var curCookie = ""
        if(getcookie("Compare_List") != null)
        {
            curCookie_Value = getcookie("Compare_List");        
        }
        var final_List = curCookie_Value.split("^");   
        
        
        final_List.splice((ctrl.parentNode.parentNode.parentNode.rowIndex-1),1);
        curCookie = final_List.join("^")   
        curCookie = "Compare_List=" + curCookie;
        document.cookie = curCookie;
        
        //Now Remove the row from the Compare list.
        
        source.deleteRow(ctrl.parentNode.parentNode.parentNode.rowIndex);
        
        /* Visible the Compare Button */
        if(source.rows.length <= 3)
            source.rows[source.rows.length-1].style.display = 'none'
    }
    catch(err)
    {
    
    }
}

function chkCompare_From_Compare(idx)
{    
    /* This function will uncheck the chk Box and remove the list from the table and set the cookie as well */
    // First Set the cookie
    try
    {         
        var curCookie_Value = "";
        var curCookie = ""
        if(getcookie("Compare_List") != null)
        {
            curCookie_Value = getcookie("Compare_List");        
        }
        var final_List = curCookie_Value.split("^");   
        
        
        final_List.splice((idx-1),1);
        curCookie = final_List.join("^")   
        curCookie = "Compare_List=" + curCookie;
        document.cookie = curCookie;
    }
    catch(err)
    {
    
    }    
}

