﻿// JScript File

var displayElement;

// Initializes global variables and session state.
function pageLoad()
{
    displayElement = $get("ResultId");
    PageMethods.SetSessionValue("SessionValue", Date(), 
        OnSucceeded, OnFailed);
}

// Gets the session state value.
function GetSessionValue(key) 
{
    PageMethods.GetSessionValue(key, 
        OnSucceeded, OnFailed);
}

//function GetSessionForDisplay(key) 
//{
//    PageMethods.GetSessionForDisplay(key, 
//        OnSucceeded, OnFailed);
//}



//Sets the session state value.
function SetSessionValue(key, add_list) 
{
    PageMethods.SetSessionValue(key, add_list, 
        OnSucceeded, OnFailed);
}

// Callback function invoked on successful 
// completion of the page method.
function OnSucceeded(result, userContext, methodName) 
{
    inputs = document.getElementsByTagName('input'); 
	 var CheckCounter = 0;
	 var CompareList = '';
	 var Separator = '';
	 var ProductCode = '';
	 var SearchResult = -1;
	 var Session_Cat_ID = '';
	 var ProductArray = new Array();
	 var ItemCount = 0;
	 var LastProductCode = '';
	 
	 
     //window.alert(result);

    if (methodName == "GetSessionValue")
    {
        CompareList = result;
       
        
        for(var i=0; i < inputs.length; i++) 
        {
            if(inputs[i].getAttribute('type') == 'checkbox') 
            {
                ProductCode = inputs[i].name.substring(22,34);
               
                 if (CompareList == null || CompareList == '')//
                 {  
                    if(inputs[i].checked)
                    {
                      CompareList = ProductCode;
                    }
                 }
                 else
                 {
              
                   if(inputs[i].checked)
                   {
                        SearchResult = CompareList.search(ProductCode)
                        if (SearchResult == -1) // not found
                        {
                        CompareList =  CompareList + "," + ProductCode;
                        }
                   }
                   else
                        if(inputs[i].checked == false)
                        {
                            
                            SearchResult = CompareList.search(ProductCode)
                           
                            
                            if (SearchResult > -1)  // found
                            {
                                if (SearchResult == 0) // first item 
                                {
                                    if (CompareList == ProductCode)
                                    CompareList =  "";
                                    else
                                    CompareList = CompareList.replace( ProductCode + ",","");
                            
                                }
                                else
                                {
                                    CompareList = CompareList.replace("," +ProductCode,"");
                                    CompareList = CompareList.replace(ProductCode,""); // replace unchecked item with productcode 
                                }
                            }
                          
                   
                        }
                   }
                 
                
            } 
        }//e f
        
         
        if (CompareList != null)
        {
            //window.alert(CompareList);
            
            
            
            
            ProductArray = CompareList.split(',');
            ItemCount = ProductArray.length;
        }
        
        
        if (ItemCount > 5) 
        {
            window.alert('ขออภัย สามารถเปรียบเทียบสินค้าได้ครั้งละ 5 รายการเท่านั้นค่ะ - ' + CompareList.charAt(0) );
            // get last item           
            LastProductCode = ProductArray[ItemCount-1];
           
            // delete last item
            CompareList = CompareList.replace("," +LastProductCode,"");
            ProductArray = CompareList.split(',');
            
            
            for(var j=0; j < inputs.length; j++) 
            {
                if(inputs[j].getAttribute('type') == 'checkbox') 
                {
                    ProductCode = inputs[j].name.substring(22,34);
                    if (ProductCode == LastProductCode)
                    inputs[j].checked = false;
                }
             }
            
         }   
         else
         
         {
        
         
           SetSessionValue('CompareList', CompareList);
           
           
           for(var i=0; i < inputs.length; i++) 
           { 
         
            if(inputs[i].getAttribute('type') == 'hidden') 
            { 
                tagname = inputs[i].name.substring(13,22);
                if (tagname == 'hfCompare')
               
                inputs[i].value = CompareList;
         
            }
           }
           
           }
           
           
           
           
           
           
           
           
           
           
           
           
        } //e check length
//    else
//    if (methodName == "GetSessionForDisplay")
//    {
//         CompareList = result;
//         window.alert("Display");
////        if (result != null)
////        {
////       
////         var w = screen.width; // Get the width of the screen
////	     var h = screen.height; // Get the height of the screen
////         var url =  'ProductComparison.aspx?Param='+CompareList;
//// 
////	    var features = 'width='+w+',height='+h+ ',left=0,top=0,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes'
////	    var win = window.open(url, '', features);
////	    }
//    }


}


// Callback function invoked on failure 
// of the page method.
function OnFailed(error, userContext, methodName) 
{
    if(error !== null) 
    {
        displayElement.innerHTML = "An error occurred: " + 
            error.get_message();
    }
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();