//********************************************************************************
// GLOBAL SITE FUNCTIONALITY
//********************************************************************************

//--------------------------------------------------------------------------------
// Events
//--------------------------------------------------------------------------------

//Set the events on load
$(document).ready
(
    function()
    {      
        $(".QuickSearch").click
        (
            function(fn)
            {   
                QuickSearch();
                
                return false;
            }            
        );       
        
        //CHECK : Verify if the 'Enter Key' was pressed
        $(".SearchText").keypress
        (
            function(e) 
            {   
                //Gather the key pressed
                var charCode = (e.which) ? e.which : event.keyCode
        
                //CHECK : Verify that the key pressed is 'Enter' key                        
                if (charCode == 13) //Enter Key 
                {  
                    QuickSearch();                            
                    
                    window.event.cancelBubble = true;
                    window.event.returnValue = false;
                    
                    return false;                          
                }                 
            }
        );
        
        //CHECK : Verify if the 'Enter Key' was pressed
        $(".SearchType").keypress
        (
            function(e) 
            {   
                //Gather the key pressed
                var charCode = (e.which) ? e.which : event.keyCode
        
                //CHECK : Verify that the key pressed is 'Enter' key                        
                if (charCode == 13) //Enter Key 
                {  
                    QuickSearch();
                    
                    window.event.cancelBubble = true;
                    window.event.returnValue = false;
                    
                    return false;                          
                }                 
            }
        );
        
        //CHECK : Verify if the 'Enter Key' was pressed
        $(".QuickSearch").keypress
        (
            function(e) 
            {   
                //Gather the key pressed
                var charCode = (e.which) ? e.which : event.keyCode
        
                //CHECK : Verify that the key pressed is 'Enter' key                        
                if (charCode == 13) //Enter Key 
                {  
                    QuickSearch();
                    
                    window.event.cancelBubble = true;
                    window.event.returnValue = false;
                    
                    return false;                          
                }                 
            }
        );
        
       
        $(".Logout").click
        (
            function()
            {   
                LogoutUser();             
                
                return false;
            }            
        );
    }
);

//--------------------------------------------------------------------------------

//--------------------------------------------------------------------------------
// Functions
//--------------------------------------------------------------------------------

function QuickSearch()
{  
    //CHECK : Validate the text entered
    if (ValidateText($(".SearchText").val()) == true)
    {   
        //block UI input and display working sign
         $.blockUI({ css: { 
                border: 'none', 
                padding: '15px', 
                backgroundColor: '#000', 
                '-webkit-border-radius': '10px', 
                '-moz-border-radius': '10px', 
                opacity: '.5', 
                color: '#fff' 
                }});
                
        //Verify the Search data information
        ob_post.AddParam("strSearchText", $(".SearchText").val());
        ob_post.AddParam("strSearchTypeID", $(".SearchType option:selected").attr("value"));
        ob_post.AddParam("strSearchType", $(".SearchType option:selected").text());
        ob_post.post("../Global.aspx", "ValidateQuickSearch", "function(){}");
    }
   
    return false;
}

function TransferToPage(response)
{      
    //Transfer to page
    window.location = response.content;
    
    return false;
}

function ReloadPage()
{   
    //Reset the page
    window.location = $("form").attr("action");
    
    return false;
}

function ResetErrorMessage()
{
    //Hide the error message(s)
    $(".message").html("");
    $(".message").hide(); 
}

function PopupErrorMessage(response)
{   
    //Show the error message
    var t = "Error Message";
	var a = "../ErrorMessage.aspx?height=145&width=360&modal=true&message=" + escape(response.content); 
	var g = "";
	tb_show(t,a,g);
	
    return false;
}

function DisplayErrorMessage(response)
{    
    try
    {        
        setTimeout($.unblockUI, 1);
    }
    catch(err)
    {}
    
    //Initialize variable
    var strMessage = "";

    //Interrogate the 'response' and determine if it is an object 
    if (typeof(response) == 'object')
    {
        strMessage = response.content;
    }
    else
    {
        strMessage = response
    }

    //Show the error message(s)    
    $(".message").html(strMessage);
    $(".message").show();   
}

function ResetModalErrorMessage()
{
    //Hide the error message(s)
    $(".modalmessage").html("");
    $(".modalmessage").hide(); 
}

function DisplayModalErrorMessage(response)
{    
    //Initialize variable
    var strMessage = "";

    //Interrogate the 'response' and determine if it is an object 
    if (typeof(response) == 'object')
    {
        strMessage = response.content;
    }
    else
    {
        strMessage = response
    }

    //Show the error message(s)
    $(".modalmessage").html(strMessage);
    $(".modalmessage").show();   
}

function ToggleSidePanelDisplay()
{   
    //Set 'Side Panel Display' preference
    ob_post.post("../Global.aspx", "SaveNavigationToggle", "function(){}");     
    
    return false;
}

function ToggleSidePanelDisplaySuccess(response)
{
    //CHECK : Determine if we need to hide the 'Side Panel' display
    if (response.content == "TRUE")
    {
        //Fade Out the side panel display
        $("#ctl00_container_sidecol").fadeOut
        (
            "slow", 
            function()
            {
                //Re-adjust the main content panel
                $(".maincol_str").css("margin-left",0);
                
                //Set the text
                $(".SideColumn").text("Show Navigation");
            }
        );                
    }
    
    //CHECK : Determine if we need to show the 'Side Panel' display
    if (response.content == "FALSE")
    {
        //Re-adjust the main content panel
        $(".maincol_str").css("margin-left",250);
    
        //Fade In the side panel display
        $("#ctl00_container_sidecol").fadeIn("slow");
        
        //Set the text
        $(".SideColumn").text("Hide Navigation");
    }
}

function LogoutUser()
{   
    //Logout the user    
    ob_post.post("../Global.aspx", "LogoutUser", "function(){}");
   
    return false;
}

function AddToCart(intTitleID)
{   
    //Verify the Cart data information
    ob_post.AddParam("intTitleID", intTitleID); 
    ob_post.post("../Global.aspx", "AddToCart", "function(){}");
   
    return false;
}

function AddToCartSuccess(response)
{   
    //Initialize variables
    var strValue = "";      
    var mySplitResult;
    var intTitleID = 0;
    var strTitle = "";        
    
    //Set the value
    strValue = response.content
    
    //Interrogate the string to retrieve the TitleID and the Title value(s)
    strValue = $.trim(strValue);
    mySplitResult = strValue.split("~");
    
    //Set the values
    intTitleID = mySplitResult[0];
    strTitle = mySplitResult[1]

    //Update the 'Cart' link
    $("#cart" + intTitleID).html("<strong>In Cart</strong>");
    $("#cart" + intTitleID).removeAttr("onclick");   
    $("#cart" + intTitleID).css( 'text-decoration' ,'none');
    $("#cart" + intTitleID).css( 'color' ,'#592');
    $("#cart" + intTitleID).css( 'cursor' ,'default');
//     $("#cart" + intTitleID).parent().html("<span class=\"floatright green\"><strong>In Cart</strong></span>");
    //<span class=\"floatright green\"><strong>In Cart</strong></span>
 
    
    //Display the message
    $(".message").html(strTitle + " has been added to your cart.");
    $(".message").show();
       
    return false;
}

function ProductDetail(strSource, intTitleID)
{       
    //Gather the Product Detail information
    ob_post.AddParam("strSource", strSource);
    ob_post.AddParam("intTitleID", intTitleID);    
    ob_post.post("../Global.aspx", "GatherProductDetail", "function(){}");
    
    return false;
}

function LinkToB2C(intTitleID)
{   
    //Verify the B2C Link data information
    ob_post.AddParam("intTitleID", intTitleID); 
    ob_post.post("../Global.aspx", "LinkToB2C", "function(){}");
   
    return false;
}

function LinkToB2CSuccess(response)
{   
    //Set the variables
    var intWidth = 980; 
    var intHeight = 650;
    var URL = "http://lib.myilibrary.com" + response.content;
    var windowName = "B2CPopup";
 	
    //Determine the middle of the screen
    if (document.all)
    {
        var xMax = screen.width, yMax = screen.height;
    }
    else
    {    
        if (document.layers)
        {
            var xMax = window.outerWidth, yMax = window.outerHeight;
        }
        else
        {
            var xMax = 640, yMax=480;
        }
    }
                
    var xOffset = (xMax - intWidth)/2, yOffset = (yMax - intHeight)/2;

    //Set up a var to have all the settings
    var features =
            'screenX='+xOffset +
            ',screenY='+yOffset + 
            ',top='+yOffset+
            ',left='+xOffset+
            ',width='+ intWidth +
            ',height='+ intHeight +
            ',directories=yes'+
            ',location=yes'+
            ',menubar=yes'+
            ',scrollbars=yes' +
            ',status=yes' +
            ',toolbar=yes'+
            ',resizable=yes';
    
    //Open the window            
    window.open(URL, windowName, features);

    return false;
}

function ModalClick(strValue)
{               
    //Select the radio button
    $("#ctl00_cphTemplateContent_Selected" + strValue).find("input[@type=radio]").attr("checked","checked");            
    
    return false;
}

function InternalError(response)
{   
    //Transfer to page
    window.location = '../Error.aspx';
    
    return false;
}

function SessionExpired()
{  
    //Transfer to page
    window.location = '../SessionExpired.aspx';
    
    return false;
}

function SessionExpiredModal()
{  
    //Fade out the Modal window
    tb_remove();

    //Transfer to page
    window.location = '../SessionExpired.aspx';
    
    return false;
}

function CaptureEnter()
{
    //CHECK : Verify that the key pressed is 'Enter' key
    if (window.event.keyCode == 13)
    {        
        window.event.cancelBubble = true;
        window.event.returnValue = false;
    }
}

function DownloadFile(response)
{  
    try
    {
        setTimeout($.unblockUI, 1);
    }
    catch(Err)
    {}
    //Create an IFRAME
    var iframe = document.createElement("iframe");
    
    //Point the IFRAME to DownloadFile
    iframe.src = "../DownloadFile.aspx";    
 
    //This makes the IFRAME invisible to the user
    iframe.style.display = "none";
 
    //Add the IFRAME to the page.  This will trigger a request to DownloadFile now.
    document.body.appendChild(iframe); 
   
    return false;            
}

//--------------------------------------------------------------------------------