function openJQUIPopup(page, action, params, dialog_title, w_width, w_height){
	if (typeof(w_width)=='undefined'){
		var w_width = 700;
	}

	if (typeof(w_height)=='undefined'){
		var w_height = 500;
	}

	if (typeof(params)=='undefined'){
		var params = "";
	}

	$('<div class="popupContainer" id="jq_popup">loading...</div>').
        dialog({
            stack: false,
            modal: true,
            title: dialog_title,
            width: w_width,
            height: w_height,
            close: function () {
                $(this).dialog("destroy").remove();
            },
            open: function() {
                //start_loading_dialog();
            }
        }).
        load(
            'ajax_handler.php',
            {
				'page': page,
				'action': action,
				'params': params
            }
        );
}

function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=500,left = 0,top = 0');");
}


    function checkLength(field, prefix) {
        var max = 35;
        var currentFieldValue = document.getElementById(field).value;
        var currentFieldLength = currentFieldValue.length;

        if (currentFieldLength > max) {
            currentFieldValue = currentFieldValue.substr(0,35);
            document.getElementById(field).value = currentFieldValue;
            var pos = prefix.length;
            field = field.substr(pos,field.length);
            document.getElementById(field+'1').innerHTML = (max - (currentFieldLength - 1));
        }

        max = '';
        currentFieldValue = '';
        currentFieldLength = '';
    }

function update_qty_in_shopping_cart(id, rowId)
{
    var prod_qty_obj = document.getElementById('product_quantities[' + id + ']');

    var formObj = document.cart_quantity;
    var total_qty = 0;
    var reg = new RegExp("size_"+ rowId+"_.+", "i");
    for (var i=0;i < formObj.length;i++)
    {
        fldObj = formObj.elements[i];
        if(reg.exec(fldObj.id))
        {
            if(fldObj.value != '')
            {
                total_qty = parseInt(total_qty) + parseInt(fldObj.value);
            }
        }
    }
    prod_qty_obj.value = total_qty;
}

function isNumber(e)
{
    var key;
	var keychar;
	var reg;

    if(window.event && window.event.keyCode > 0) {
        key = window.event.keyCode;
	}else if(e && e.which) {
        key = e.which;
	}else {
        return true;
	}
	//allow for backspace and delete
    /* not really sure what keycode 127 is but left it for now
     * keycode 46 appears to be the delete key
     */
	if (key == 8 || key == 127 || key == 46) {
        return true;
	}

	//allow keys 1-0
    // keycodes being used did not appear to be correct
//	if ((key > 43 && key < 58) && key != 47) {
    if ((key > 47 && key < 58) || (key > 95 && key < 106)) {
        return true;
	}

	//strange, why use regular expression for 10 keys?
	//i'm leaving this, in case the above doesn't work on other keyboards.
    keychar = String.fromCharCode(key);
	reg = /(^-?[1-9](\d{1,2}(\,\d{3})*|\d*)|^0{1})$/;
    return reg.test(keychar);
}

function rowOverEffect(object)
{
    if (object.className == 'moduleRow') object.className = 'moduleRowOver';
}

function rowOutEffect(object)
{
    if (object.className == 'moduleRowOver') object.className = 'moduleRow';
}

$().ready(function() {
    bindSizeChartButton();
});

function bindSizeChartButton()
{
    $('#size_chart_link').live('click', function() {
        $($('#size_chart').html()).dialog({
            modal: false,
            title: "Size Chart",
            draggable:  false,
            width: 580,
            zIndex: 1004,
            dialogClass: 'size_chart_dialog'
        });
    });

    $('#chart_link').live('click', function() {
        $($('#size_chart').html()).dialog({
            modal: false,
            title: "Size Chart",
            draggable:  false,
            width: 580,
            zIndex: 1004,
            dialogClass: 'size_chart_dialog'
        });
    });
}

function size_chart_us_sizes() {
    $('.us_size').css('display', 'block');
    $('.unit').css('display', 'inline');
    $('.metric_size').css('display', 'none');
}
function size_chart_metric_sizes() {
    $('.metric_size').css('display', 'block');
    $('.unit').css('display', 'inline');
    $('.us_size').css('display', 'none');
}

function bindAutoComplete() {
    loadAutoComplete();
    var searchtext = '[Enter Keyword(s)]';

    if ($('input[name=q]').val() == ""){
        $('input[name=q]').val(searchtext).css('color', '#555');
    }

    $('input[name=q]').click(function(){
        if($(this).val() == searchtext) $(this).css('color', '').val('');
        repositionSubmit();
    }).blur(function(){
        if($(this).val() == '') $(this).val(searchtext).css('color', '#555');
    }).keydown(function(){
        repositionSubmit();
    }).keyup(function(){
        repositionSubmit();
    }).mouseout(function(){
        repositionSubmit();
    }).search(function(){
        repositionSubmit();
    });

    $('.ac_even').live('mousedown',function(){
        setTimeout('repositionSubmit()',200);
    });
    $('.ac_odd').live('mousedown',function(){
        setTimeout('repositionSubmit()',200);
    });


    $('form[name=xml_search]').submit(function(){
        var v = $(this).find('input[name=q]').val();
        if(v == '' || v == searchtext){//If the string is blank or the same as the default entry, refust to continue and alert the user.
            alert('Please type in something to search for.');
            return false;
        }
        //Strip out trailing spaces before submit
        $(this).find('input[name=q]').val(v.replace(/\s+$/, ''));
    });
}

function loadAutoComplete() {
    var url = 'ajax_handler.php';
    $("#search_autocomplete").autocomplete(url, {
        autoFill:false,
	selectFirst:false,
        delay:10,
        matchContains: true,
        max:10,
        multiple: true,
        multipleSeparator: " ",
        width: 167,
        cacheLength:0,
        extraParams:{
            page:'search_autocomplete'
        },
        formatItem: function(row,i,num,term){
            var search = $('#search_autocomplete').val();
            var spaces = search.lastIndexOf(' ');
            if (spaces != -1){
                var prefix = search.substring(0,search.lastIndexOf(' '));
                return prefix+" "+row[0];
            } else {
                return row[0];
            }
        }
    }).result(function(event, item) {
        document.xml_search.submit();
    });


}
function repositionSubmit(){//Function to move down/up the submit button while the autocomplete box is changing size.
    if($('.ac_results').css('display') == 'block' ) {
        $('#search_button').css('padding-top', $('.ac_results').height()+'px');
    }
    if($('.ac_results').css('display') == 'none' ) {
        $('#search_button').css('padding-top', '');
    }
}

function trackJSEvent(event_name) {
    if(_gat) {
        var pageTracker = _gat._getTrackers()[0];
        if(pageTracker) {
            pageTracker._trackPageview('/js-events/'+event_name);
        }
    }
}
