function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}


function updateCartInformation(){
    if(getCartItem(3) == 0)
        $('span[name="spancartLine"]').html('Your basket is empty.');
    else if(getCartItem(3) == 1)
        $('span[name="spancartLine"]').html(getCartItem(3)+' item : '+getCartItem(1));
    else
        $('span[name="spancartLine"]').html(getCartItem(3)+' items : '+getCartItem(1));
    $('span[name="spancartLine"]').css({
        'font-weight':'200',
		'font-size':'11px',
        'line-height':'22px'
    });
    
    //$('div[name="cartNumItems"]').html(getCartItem(3));
    //$('div[name="cartSubtotal"]').html(getCartItem(1));
    //$('div[name="cartItemContainer"]').html('');
    $('div[name="cartItemContainer"]').html(getMiniCart());
    //alert(getMiniCart());
    $('span[name="spancartNumItems"]').html(getCartItem(3));
    $('span[name="spancartSubtotal"]').html(getCartItem(1));
/*
	// Firefox workaround (value property)
	$('div[name="cartNumItems"]').value(getCartItem(3));
	$('div[name="cartSubtotal"]').value(getCartItem(1));
	$('div[name="cartItemContainer"]').value('');
	$('div[name="cartItemContainer"]').value(getMiniCart());
	$('span[name="spancartNumItems"]').value(getCartItem(3));
	$('span[name="spancartSubtotal"]').value(getCartItem(1));*/
}



$(document).ready(function() {
    //Configuration of buttons to change the layout type for product pages
	
    $(function() {
        $( "#layout" ).buttonset();
    });
	
    var checkedLayout;
	
    function checkLayoutChange(th) {
        if(th.is(':checked')) {
            checkedLayout = th.attr('id');
            if (checkedLayout == "layout2") {
                $(".productbox").addClass('productboxvert');
                $(".productbox").removeClass('productboxhoriz');
            } else {
                $(".productbox").addClass('productboxhoriz');
                $(".productbox").removeClass('productboxvert');
            }
        }
    }
	
    $('input[name*="layout"]').each(function(){
        checkLayoutChange($(this));
    });
	
    $('input[name*="layout"]').click(function() {
        checkLayoutChange($(this));
    });
	
	
    //Configuration of buttons to change the search type
	
    $(function() {
        $( "#search" ).buttonset();
    });
	
    var checkedSearch;
	
    function checkSearchChange(th) {
        if(th.is(':checked')) {
            checkedSearch = th.attr('id');
            if (checkedSearch == "search1") {
                $(".productbox").addClass('productboxvert');
                $(".productbox").removeClass('productboxhoriz');
            } else {
                $(".productbox").addClass('productboxhoriz');
                $(".productbox").removeClass('productboxvert');
            }
        }
    }
	
    $('input[name*="search"]').each(function(){
        checkSearchChange($(this));
    });
	
    $('input[name*="search"]').click(function() {
        checkSearchChange($(this));
    });

    updateCartInformation();
	
    //Cookie test
	
	
    $('form[name="addToBasketForm"]').ajaxForm({
        success: function() {
            updateCartInformation();
            $.fancybox.close();
        }
    });
	
	$('form[name="addToBasketForm"] input[value="add to basket"]').click(function(){
		$(this).attr('name',$(this).prev().attr('name'));
        $(this).attr('value','added');
		
	});
	
    $('form[name="addToBasketForm"] input[name="checkout"]').click(function(){
        $(this).attr('name',$(this).prev().attr('name'));
        $(this).attr('value','adding...');
        setTimeout(function(){
            window.location ='http://www.printkit.co.uk/cgi-bin/os000001.pl?ACTION=START';
        }, 1000);
    });

    //alert('ready.');
	
    $('div[name*="increaseQuantity"]').click(function() {
        //alert('test');
        var tmpName = $(this).attr('name');
        //alert(tmpName.replace('increaseQuantity','Q'));
        var tmpInputVal = $('input[name="'+tmpName.replace('increaseQuantity','Q')+'"]').val();
        if(tmpInputVal + 1 <= 10000) tmpInputVal++;
        $('input[name="'+tmpName.replace('increaseQuantity','Q')+'"]').val(tmpInputVal);
    //alert(tmpInputVal);
    });
	
    $('div[name*="decreaseQuantity"]').click(function() {
        //alert('test');
        var tmpName = $(this).attr('name');
        //alert(tmpName.replace('increaseQuantity','Q'));
        var tmpInputVal = $('input[name="'+tmpName.replace('decreaseQuantity','Q')+'"]').val();
        if(tmpInputVal - 1 >= 1) tmpInputVal--;
        $('input[name="'+tmpName.replace('decreaseQuantity','Q')+'"]').val(tmpInputVal);
    //alert(tmpInputVal);
    });
	
    /*  $('input[name*="Q_"]').keyup(function(){
        var tmpTxt = $(this).val();
        var regExp = /^\d+$/g;
        //if(tmpTxt == '') $(this).val(1);
        if(!tmpTxt.match(regExp) && tmpTxt != '') $(this).val(1);
    }).keyup();*/

    $('input[name*="Q_"]').keyup(function(e){
        if(e.keyCode != 13){
            var tmpTxt = $(this).val();
            var regExp = /\d+/;
            if(tmpTxt.match(regExp))
                $(this).val(tmpTxt.match(regExp)[0]);
        } else {
            var regExp = /\d+/;
            if($(this).val() == '' || !$(this).val().match(regExp)) $(this).val(1);
            $('form[name="addToBasketForm"]').submit();
        }
       
    }).keyup();

    $('input[name*="Q_"]').blur(function(){
        var regExp = /\d+/;
        if($(this).val() == '' || !$(this).val().match(regExp)) $(this).val(1);
    });
	
    /*$('input[name~="Q_"]').click(function() {
		alert('Clicked!');
	});*/


    //
    // CART INFORMATION
    //

    // Insert default quantity into custom quantity box
    
    $('div[name="cartItemQuantity"] .quantityboxdiv input[name="quantityBox"]').val(function(){
        return $(this).prev().children('input').first().val();
    });

    // Make it possible to change change the value of custom quantity box
    $('div[name="cartItemQuantity"] div div[name*="increaseQuantity"]').click(function(){
        var actualQuantity = parseInt($(this).parent().prev().children('input').first().val());
        if(actualQuantity + 1 < 1000) {
            $(this).parent().prev().children('input').first().val(actualQuantity+1);
            $(this).parent().prev().children('div[name="cartDefaultQuantityInput"]').first().children().first().val(actualQuantity+1);
        }
    });


    $('div[name="cartItemQuantity"] div div[name*="decreaseQuantity"]').click(function(){
        var actualQuantity = parseInt($(this).parent().prev().children('input').first().val());
        if(actualQuantity - 1 > 0) {
            $(this).parent().prev().children('input').first().val(actualQuantity-1);
            $(this).parent().prev().children('div[name="cartDefaultQuantityInput"]').first().children().first().val(actualQuantity-1);
        }
    });

    $('div[name="cartItemQuantity"] .quantityboxdiv input[name="quantityBox"]').keyup(function(e){
        if(e.keyCode != 13){
            var tmpTxt = $(this).val();
            var regExp = /\d+/;
            if(tmpTxt.match(regExp))
                $(this).val(tmpTxt.match(regExp)[0]);
        } else {
            var regExp = /\d+/;
            if($(this).val() == '' || !$(this).val().match(regExp)) $(this).val(1);
        }
        $(this).parent().children('div[name="cartDefaultQuantityInput"]').first().children().first().val($(this).val());
    }).keyup();

    $('div[name="cartItemQuantity"] .quantityboxdiv input[name="quantityBox"]').blur(function(){
        var regExp = /\d+/;
        if($(this).val() == '' || !$(this).val().match(regExp)) $(this).val(1);
        $(this).parent().children('div[name="cartDefaultQuantityInput"]').first().children().first().val($(this).val());
    });

    // Remove item button
    $('div[name="removeBasketItem"]').click(function(){
        $(this).nextAll('div[name="removeButtonDiv"]').first().children('input[type="CHECKBOX"]').first().attr('checked',true);
        $('input[name="ACTION_UPDATE"]').click();
    });
    

    
});
