/** General JS functionality for the Highlander site *****/

$(document).ready(
    function(){
        
    // External links load new window
    $('a[rel=external]').click( function( ) { window.open( this.href );return false; } ); 
    
    $('.update_cart').hide( );
    
    
    $(".dtext").focus(function(srcc)
    {
        if ($(this).val() == $(this)[0].title)
        {
            $(this).removeClass("dtextactive");
            $(this).val("");
        }
    });
    
    $(".dtext").blur(function()
    {
        if ($(this).val() == "")
        {
            $(this).addClass("dtextactive");
            $(this).val($(this)[0].title);
        }
    });
    
    $(".dtext").blur();   
    
    
});


function search_click( el, val )
{
    if( el.value == val ) {
        el.value = "";
    }
}

function show_update_button( id )
{
    $( "#update-"+id ).show( );
}

function inc_quantity( id )
{
    var el = document.getElementById( "quantity-"+id );
    var max_quant = parseInt( document.getElementById( "instock-"+id ).value );
    var quant = ( parseInt( el.value ) + 1);
    
    if( quant > max_quant ) {
        return;   
    }
    
    el.value = quant;
    
    show_update_button( id );
}

function dec_quantity( id )
{
    el = document.getElementById( "quantity-"+id );
    if( parseInt( el.value ) < 1 ) {
        return;
    }
    else {
        el.value = ( parseInt( el.value )-1);
    }
    
    show_update_button( id );
}

function buy_case( id )
{
    var el = document.getElementById( "quantity-"+id );
    var case_size = parseInt( document.getElementById( "casesize-"+id ).value );
    
    el.value = case_size;
    
    show_update_button( id );               
}

function update_cart( id )
{
    var quant = parseInt( document.getElementById( "quantity-"+id ).value );
    var max_quant = parseInt( document.getElementById( "instock-"+id ).value );
    
    if( quant > max_quant ) {
        quant = max_quant;
    }   

    $.ajax({
        url:'ajax/updatecart',
        type: 'POST',
        data: {'product_id':id, 'quantity':quant},
        dataType: 'json',
        timeout: 6000,
        error: function( ){
        },
        success: function( data )
        {
            $('#cart_amount').html( data.net_total );
        }
    });
    
    $( "#update-"+id ).hide( );
}

