
function clickButton() {
    $('.headerIndex').slideDown('2000');
}



$(function() {

    $("table.orderFormTable tr:odd").addClass("oddrow");


    updateWishlistCount();

    $('.menuUl').easyListSplitter({
        colNumber: 5,
        direction: 'vertical'
    });

    $('.toggle').click(function() {
        $('.headerIndex').slideToggle('2000');
        if ($('.toggle').hasClass('downOpen')) $('.toggle').removeClass('downOpen');
        else $('.toggle').addClass('downOpen');
        return false;
    });



    //Clear default value of focused input
    $('input.searcInputField').clearDefault();
    var minimumHeight = 0;
    $('ul.menuUl li').not('.level2').each(function() {
        var liHeight = $(this).height();
        //  $(this).css('height', $(this).height());
        if (liHeight > minimumHeight) minimumHeight = liHeight;
    });
    $('ul.menuUl, .headerIndex').css('min-height', minimumHeight);
});

//Clear form default value jQuery plugin
(function($) {
    $.fn.clearDefault = function() {
        return this.each(function() {
            var default_value = $(this).val();
            $(this).focus(function() {
                if ($(this).val() == default_value) $(this).val("");
            });
            $(this).blur(function() {
                if ($(this).val() == "") $(this).val(default_value);
            });
        });
    },
	$.fn.invalidCharCheck = function(textFieldSelector) {
	    $(this).click(function() {
	        var val = $(textFieldSelector).val().trim();
	        if (val.length == 0) {
	            alert('Please enter a query');
	            return false;
	        }

	        var invalidChars = ['<', '>', '?', '!', '@'];

	        for (var i = 0; i < invalidChars.length; i++) {
	            if (val.indexOf(invalidChars[i]) != -1) {
	                alert('Invalid characters in your query');
	                return false;
	            }
	        }
	    });
	},
	 $.fn.addOptions = function(data) {
	     return this.each(function() {
	         var list = this;
	         var x = 0;
	         $(list).children().remove();

	         $.each(data, function(index, itemData) {
	             var option = new Option(itemData.name, itemData.value);
	             //list.add(option, null);/*doesn't work in IE 7*/
	             list.options[x++] = option;
	         });
	     });
	 },
	 $.fn.addDefaultOption = function(text, value) {
	     return this.each(function() {
	         var list = this;
	         var data = new Array();
	         for (var i = 0; i < list.options.length; i++) {
	             data[i] = list.options[i];
	         }
	         $(list).children().remove();

	         var option = new Option(text, value);
	         //list.add(option, null);/*doesn't work in IE 7*/
	         list.options[0] = option;

	         for (var i = 1; i <= data.length; i++) {
	             list.options[i] = data[i - 1];
	         }

	     });
	 };
})(jQuery);

/*
* 	easyListSplitter 1.0.2 - jQuery Plugin
*	written by Andrea Cima Serniotti	
*	http://www.madeincima.eu
*
*	Copyright (c) 2010 Andrea Cima Serniotti (http://www.madeincima.eu)
*	Dual licensed under the MIT (MIT-LICENSE.txt)
*	and GPL (GPL-LICENSE.txt) licenses.
*
*	Built for jQuery library
*	http://jquery.com
*
*/

/*
To activate the plugin add the following code to your own js file:
	
$('.your-list-class-name').easyListSplitter({ 
colNumber: 3,
direction: 'horizontal'
});
	
*/

var j = 1;

(function(jQuery) {
    jQuery.fn.easyListSplitter = function(options) {

        var defaults = {
            colNumber: 2, // Insert here the number of columns you want. Consider that the plugin will create the number of cols requested only if there are enough items in the list.
            direction: 'vertical',
            title: ''
        };

        this.each(function() {

            var obj = jQuery(this);
            var settings = jQuery.extend(defaults, options);
            var totalListElements = jQuery(this).children('li').size();
            var baseColItems = Math.ceil(totalListElements / settings.colNumber);
            var listClass = jQuery(this).attr('class');

            // -------- Create List Elements given colNumber ------------------------------------------------------------------------------

            for (i = 1; i <= settings.colNumber; i++) {
                if (i == 1) {
                    jQuery(this).addClass('listCol1').wrap('<div class="listContainer' + j + '"></div>');
                }
                else if (jQuery(this).is('ul')) { // Check whether the list is ordered or unordered
                    if (settings.title == '') {
                        jQuery(this).parents('.listContainer' + j).append('<ul class="listCol' + i + '"></ul>');
                    }
                    else {
                        jQuery(this).parents('.listContainer' + j).append('<ul class="listCol' + i + '"><li>' + settings.title + '</li></ul>');
                    }
                }
                else {
                    if (settings.title == '') {
                        jQuery(this).parents('.listContainer' + j).append('<ol class="listCol' + i + '"></ol>');
                    }
                    else {
                        jQuery(this).parents('.listContainer' + j).append('<ol class="listCol' + i + '"><li>' + settings.title + '</li></ol>');
                    }


                }
                jQuery('.listContainer' + j + ' > ul,.listContainer' + j + ' > ol').addClass(listClass);
            }

            var listItem = 0;
            var k = 1;
            var l = 0;

            if (settings.direction == 'vertical') { // -------- Append List Elements to the respective listCol  - Vertical -------------------------------

                jQuery(this).children('li').each(function() {
                    listItem = listItem + 1;
                    if (listItem > baseColItems * (settings.colNumber - 1)) {
                        jQuery(this).parents('.listContainer' + j).find('.listCol' + settings.colNumber).append(this);
                    }
                    else {
                        if (listItem <= (baseColItems * k)) {
                            jQuery(this).parents('.listContainer' + j).find('.listCol' + k).append(this);
                        }
                        else {
                            jQuery(this).parents('.listContainer' + j).find('.listCol' + (k + 1)).append(this);
                            k = k + 1;
                        }
                    }
                });

                jQuery('.listContainer' + j).find('ol,ul').each(function() {
                    if (jQuery(this).children().size() == 0) {
                        //jQuery(this).remove();
                    }
                });

            } else {  // -------- Append List Elements to the respective listCol  - Horizontal ----------------------------------------------------------

                jQuery(this).children('li').each(function() {
                    l = l + 1;

                    if (l <= settings.colNumber) {
                        jQuery(this).parents('.listContainer' + j).find('.listCol' + l).append(this);

                    } else {
                        l = 1;
                        jQuery(this).parents('.listContainer' + j).find('.listCol' + l).append(this);
                    }
                });
            }

            jQuery('.listContainer' + j).find('ol:last,ul:last').addClass('last'); // Set class last on the last UL or OL	
            j = j + 1;

        });
    };
})(jQuery);


/*UIEL Scripts*/

function initPropertyDetail(selector) {

    initAddToWishlist();

    getSegmentValues("segment_values_1", selector, "Segment1");
    $(selector).change(function() {
        if ($(this).val() != "-1") {
            getSegmentValues("segment_values_2", ".ddlSegmentValue2", "Segment2");
            if ($('.segment2Desc').text().length == 0 && $('.segment1Desc').text().length > 0) {
                getImageBySegment();
                getRefCodeBySegment();
            }
            else {
                var defaultImageID = $('.imageDiv').attr('defaultImageID');
                var imageHtml = "<img src='GetImage.ashx?id=" + defaultImageID + "&w=327&h=327&c=TOP' />";
                $('.imageDiv').html(imageHtml)
            }
        }
        else {
            populateDropDown(null, ".ddlSegmentValue2");
            var defaultImageID = $('.imageDiv').attr('defaultImageID');
            var imageHtml = "<img src='GetImage.ashx?id=" + defaultImageID + "&w=327&h=327&c=TOP' />";
            $('.imageDiv').html(imageHtml);
        }
    });

    $('.ddlSegmentValue2').change(function() {
        if ($('.segment2Desc').text().length > 0) {
            getImageBySegment();
            getRefCodeBySegment();
        }
    });
}

function getSegmentValues(action, selector, category) {
    var groupCode = $(selector).attr('grpCode');
    var values = "";
    if (category == "Segment2") {
        values = "Segment1:" + $('.ddlSegmentValue1').val() + ";";
    }
    else {
        populateDropDown(null, ".ddlSegmentValue2");
    }
    $.ajax({
        type: "POST",
        url: "Handlers/UielHandler.ashx",
        data: "action=" + action + "&groupCode=" + groupCode + "&category=" + category + "&categoryValues=" + values,
        success: function(msg) {
            var values = eval("(" + msg + ")");
            populateDropDown(values, selector);
        },
        error: function() {
            alert('An error occured');
        }
    });
}

function populateDropDown(values, selector) {
    $(selector).children().remove();
    if (values == null || values.length == 0) {
        $(selector).addDefaultOption("No options available", "-1");
        $(selector).attr('disabled', 'disabled');
    }
    else {
        $(selector).removeAttr('disabled');
        $(selector).addOptions(values);
        $(selector).addDefaultOption("Select an option", "-1");
        $(selector).val("-1");
    }
}

function getImageBySegment() {
    var groupCode = $('.ddlSegmentValue1').attr('grpCode');
    var segmentValue1 = $('.ddlSegmentValue1').val();
    var segmentValue2 = $('.ddlSegmentValue2').val();

    $.ajax({
        type: "POST",
        url: "Handlers/UielHandler.ashx",
        data: "action=set_image_by_segment&groupCode=" + groupCode + "&segmentValue1=" + segmentValue1 + "&segmentValue2=" + segmentValue2,
        success: function(msg) {
            var value = eval("(" + msg + ")");
            if (!value) {
                $('.imageDiv').empty();
            }
            else {
                //change image
                var imageHtml = "<img src='GetImage.ashx?id=" + value + "&w=327&h=327&c=TOP' />";
                $('.imageDiv').html(imageHtml);
            }
        },
        error: function() {
        }
    });
}

function getRefCodeBySegment() {
    var groupCode = $('.ddlSegmentValue1').attr('grpCode');
    var segmentValue1 = $('.ddlSegmentValue1').val();
    var segmentValue2 = $('.ddlSegmentValue2').val();

    $.ajax({
        type: "POST",
        url: "Handlers/UielHandler.ashx",
        data: "action=get_product_ref_code&groupCode=" + groupCode + "&segmentValue1=" + segmentValue1 + "&segmentValue2=" + segmentValue2,
        success: function(msg) {
            var value = eval("(" + msg + ")");
            if (!value) {
            }
            else {
                //change ref
                $('.lblRefNo').text(value);
            }
        },
        error: function() {
        }
    });
}

function addToWishlist(isDataSheetRequest) {
    var groupCode = $('.ddlSegmentValue1').attr('grpCode');
    var segmentValue1 = $('.ddlSegmentValue1').val();
    var segmentValue2 = $('.ddlSegmentValue2').val();
    var productID = $('.imageDiv').attr('productID');

    if ($('.ddlSegmentValue1 option').size() > 1 && segmentValue1 == "-1") {
        alert("Please select an option");
        return false;
    }

    if ($('.ddlSegmentValue2 option').size() > 1 && segmentValue2 == "-1") {
        alert("Please select an option");
        return false;
    }

    $.ajax({
        type: "POST",
        url: "Handlers/UielHandler.ashx",
        data: "action=add_to_wishlist&groupCode=" + groupCode + "&segmentValue1=" + segmentValue1 + "&segmentValue2=" + segmentValue2 + "&datasheet=" + isDataSheetRequest + "&productID=" + productID,
        success: function(msg) {
            var value = eval("(" + msg + ")");
            if (!value) {
                alert('An error occured');
            }
            else {
                updateWishlistCount();
                alert('Added to order form!');
            }
        },
        error: function() {
        }
    });
}

function initAddToWishlist() {
    $('.addToOrderForm').click(function() {
        addToWishlist(false);
        return false;
    });

    $('.dataSheet').click(function() {
        addToWishlist(true);
        return false;
    });

}

function updateWishlistCount() {
    $.ajax({
        type: "POST",
        url: "Handlers/UielHandler.ashx",
        data: "action=get_wishlist_count",
        success: function(msg) {
            var value = eval("(" + msg + ")");
            if (!value) {
                alert('An error occured');
            }
            else {
                $('.wishlistCount').text(value);
            }
        },
        error: function() {
        }
    });
}


