function cBubble(e){
    if (!e) var e = window.event;
    e.cancelBubble = true;
    if (e.stopPropagation) e.stopPropagation();
}

/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

/**
 * @example $.cookie('the_cookie', 'the_value');
 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
 * @example $.cookie('the_cookie', null);
 * @example $.cookie('the_cookie');
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

$.fn.validate = function(fn,msg,msghold,tocheck) {
	if(!fn(this,tocheck)){
		if(typeof msghold !== 'undefined' && msghold[0]){ msghold.show().html(msg) }
		else{ alert(msg) }
        return false;
     }
     else{
		msghold.html("").hide();
        return true;
     }
};

$.valid = function(obj,fun){
    var objRegExp;
    if(fun=="blank"){
    	objRegExp=/.+/;
    }
    else if(fun=="numeric"){
    	objRegExp=/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
    }

	var err=0;
	obj.each(function(){
		var el = $(this);
		if(!objRegExp.test($.trim(el.val()))){
			el.addClass("error").focus();
			err = err+1;
		}
		else{
			el.removeClass("error");
		}
	});

	return (err) ? false : true;
}

$.validRange = function(obj){
    if(parseFloat($(obj[0]).val()) > parseFloat($(obj[1]).val())){obj.addClass("error").focus();return false}
	else{obj.removeClass("error");return true}
}

$.doSrchBy = function(frmObj,query){
	query = $.trim(query.makeSearchStr());

    var shopUrl = location.protocol + "//" + location.host;
    query = query.replace(/((\\%3C)|<)((\\%2F)|\/)*[ =''a-z\\%]+((\\%3E)|>)/gi,'');
    if(query == "") query = "shopping";
	location.href = shopUrl + "/jsp/search.jsp?k="+query;
}

String.prototype.gReplace = function(repChar, repWith){return this.replace(new RegExp(repChar, 'ig'), repWith)};
String.prototype.makeSearchStr = function(){var searchTxt = $.escapeSplChars(this);   searchTxt = searchTxt.replace(/\s/g,"+"); searchTxt = searchTxt.replace(/[\+]+/g,"+"); searchTxt = $.replaceQuotes(searchTxt); return escape(searchTxt);};

$.replaceQuotes = function(str){
    str = str.replace(/["]/g,"''");
    return str;
}
$.escapeSplChars = function(str){
    str = str.replace(/[&]/g,'');
    str = str.replace(/[%]/g,'');
    str = str.replace(/[!]/g,'');
    str = str.replace(/[@]/g,'');
    str = str.replace(/[#]/g,'');
    str = str.replace(/[$]/g,'');
    str = str.replace(/[*]/g,'');
    str = str.replace(/[(]/g,'');
    str = str.replace(/[)]/g,'');
    str = str.replace(/[/^]/g,'');
    str = str.replace(/[/?]/g,'');
    str = str.replace(/[/>]/g,'');
    str = str.replace(/[/<]/g,'');
    str = str.replace(/[-]/g,'+');
    return str;
}

$(".search .field").attr("autocomplete","off");

$(document).bind("click",function(e){
	if(!e.button){//ONLY WHEN LEFT CLICKED
		var et=e.target;
		var qet=$(et);
		if(et.tagName=="H6" && qet.parents("#leftcol")[0]){
			var item = qet.siblings("div.box");
			if(et.className=="coll"){
				qet.removeClass("coll");
				item.animate({opacity: 1},100);
			}
			else{
				qet.addClass("coll");
				item.animate({opacity: 0.20},100);
			}
			item.slideToggle(250);
		}
		else if(qet.attr("id")=="zipsubmit"){//WHEN ZIP CODE IS ENTERED
            var zipCode = $("#zipcode").val();
            var errhold=qet.siblings(".errormsg");
            if(! $("#zipcode").validate($.valid,"Please enter Zip Code",errhold,"blank")){
				return false;
			}
			if(! $("#zipcode").validate($.valid,"Please Enter Integer values",errhold,"numeric")){
				return false;
			}
                	$.cookie("zipcode",zipCode, { expires: 1 ,path: '/'});

                	window.location = window.location;
            return false;
        }
		else{
			$("#container .pop").hide();
		}
	}
});

$("#zipcode").bind("focus click keydown",function(){
    if($(this).val()=="zip code"){
        $(this).val("");
    }
}).bind("blur",function(){
    if($.trim($(this).val())==""){
        $(this).val("zip code");
    }
});

$("#sortType,#pagesize").bind("change",function(){//sort type
    var et=this;
	var qet=$(et);
	var fname = qet.attr("id");
    var fval = qet.val();
    if(fval!=""){
        $.cookie(fname ,fval, { expires: 1 ,path: '/'});

        window.location = window.location;
    }
    return false;
});

$("#container .search form").bind("submit", function(){
    var query = $.trim($(this).find("input.field").val().toLowerCase());
    if(query!=''){
        $.doSrchBy($(this),query);
        return false;
    }
    else
        return false;
});