function redirect (url) {

	location.href = url;

	return false;

}

function confirm_redirect (url, message) {

	if (confirm (message))
		redirect (url);

	return false;

}

function confirm_delete (url, message)
{
	if (confirm (message))
		redirect (url);
}

function show (id)
{
	$('#' + id).show();
}

function hide (id)
{
	$('#' + id).hide();
}

function toggle_visibility(id)
{
	$('#' + id).toggle();
}

function get_XML_element_content(tag, xmlString){

	var startTag = "<"+tag+">";
	var endTag = "</"+tag+">";

	return xmlString.substring( (xmlString.indexOf(startTag)+startTag.length) , (xmlString.indexOf(endTag,xmlString.indexOf(startTag)+startTag.length)) );
}

function nl2br(str){
	return str.replace(/\n/g,'<br/>');
}

function urlencode(str) {
	var result = "";

	for (i = 0; i < str.length; i++) {
		if (str.charAt(i) == " ") result += "+";
		else result += str.charAt(i);
	}

	return escape(result);
}

function urldecode(str) {
	return unescape(str.replace(/\+/g, " "));
}

/*
 * Nicely layout our forms
 */
if( document.addEventListener ) 
	document.addEventListener( 'DOMContentLoaded', ncform, false );
	
function ncform()
{
        $( 'form.ncform' ).hide();

        // Processing
        $( 'form.ncform' ).find( 'li/label' ).not( '.nocmx' ).each( function( i ){
            var labelContent = this.innerHTML;
            var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );
            var labelSpan = document.createElement( 'span' );
            labelSpan.style.display = 'block';
            labelSpan.style.width = labelWidth;
            labelSpan.innerHTML = labelContent;
            this.style.display = '-moz-inline-box';
            this.innerHTML = null;
            this.appendChild( labelSpan );
        } ).end();

       $( 'form.ncform' ).show();
}

$(document).ready(function()
{
	$(".close-widget").each(function(i){
		$(this).bind("click",function(){
			div_parent = this.parentNode.parentNode;
			$(div_parent).hide();
		});
	});

	/*//Auto focus first form element on a page
	focused = false;
	$('.ncform input').each(function(i){
		if(this.type == 'text' && focused == false)
		{
			try
			{
				this.focus();
				focused = true;
			}
			catch(e){}
		}
	});*/
	
	//Clear the search box on focus
	$('#search, #newsletter_full_name, #newsletter_email').bind('focus',function(){
		this.value = '';
	});
	
	$('.clear-on-focus').bind('focus',function(){
		this.value = '';
	});
	
	$('#print-page').bind('click',function(){
		window.print();
	});
	
	$('a.popup-window').bind('click',function(){
		window.open(this.href);
		return false;
	});
	
	//Set numeric fields
	$('.numeric').numeric();
});

//jquery plugin for unsetting an attribute
$.fn.unset = function(a) {
  return this.each(function(){
    if ( a && a.constructor == String ) {
      var fix = {
        'for': 'htmlFor',
        'text': 'cssText',
        'class': 'className',
        'float': 'cssFloat'
      };
      a = (fix[a] && fix[a].replace && fix[a]) || a;
      this[a] = null;
      if ( this.removeAttribute ) this.removeAttribute(a);
    }
  });
} 



/*
Description:

	Allows only valid characters to be entered into input boxes.
	Note: does not validate that the final text is a valid number
	  (that could be done by another script, or server-side)

Usage:

	$(window).load(
		function()
		{
			$(".numeric").numeric(); // or $(".numeric").numeric(",");
		}
	);

Parameters:

	decimal     : Decimal separator (e.g. '.' or ',' - default is '.')
	allowPaste  : let paste operations through (does nothing with IE - paste is always allowed)
*/
$.fn.numeric = function(decimal, allowPaste)
{
	this.keypress(
		function(e)
		{
			decimal = decimal || ".";
			allowPaste = allowPaste || true;
			var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
			var allow = false;
			// allow Ctrl+A
			if((e.ctrlKey && key == 97 /* firefox */) || key == 65 /* opera */) allow = true;
			// allow Ctrl+X (cut)
			if((e.ctrlKey && key == 120 /* firefox */) || key == 88 /* opera */) allow = true;
			// allow Ctrl+C (copy)
			if((e.ctrlKey && key == 99 /* firefox */) || key == 67 /* opera */) allow = true;
			// allow Ctrl+Z (undo)
			if((e.ctrlKey && key == 122 /* firefox */) || key == 90 /* opera */) allow = true;
			// allow or deny Ctrl+V (paste)
			if((e.ctrlKey && key == 118 /* firefox */) || key == 86 /* opera */)
			{
				allow = allowPaste;
			}
			// if a number was not pressed
			if(key < 48 || key > 57)
			{
				/* '-' only allowed at start */
				if(key == 45 && this.value.length == 0) return true;
				/* only one decimal separator allowed */
				if(key == decimal.charCodeAt(0) && this.value.indexOf(decimal) != -1)
				{
					allow = false;
				}
				// check for other keys that have special purposes
				if(
					key != 8 /* backspace */ &&
					key != 9 /* tab */ &&
					key != 13 /* enter */ &&
					key != 35 /* end */ &&
					key != 36 /* home */ &&
					key != 37 /* left */ &&
					key != 39 /* right */ &&
					key != 46 /* del */
				)
				{
					
					allow = false;
				}
				else
				{
					// for detecting special keys (listed above)
					// IE does not support 'charCode' and ignores them in keypress anyway
					if(typeof e.charCode != "undefined")
					{
						// special keys have 'keyCode' and 'which' the same (e.g. backspace)
						if(e.keyCode == e.which && e.which != 0)
						{
							allow = true;
						}
						// or keyCode != 0 and 'charCode'/'which' = 0
						else if(e.keyCode != 0 && e.charCode == 0 && e.which == 0)
						{
							allow = true;
						}
					}
				}
				// if key pressed is the decimal and it is not already in the field
				if(key == decimal.charCodeAt(0) && this.value.indexOf(decimal) == -1)
				{
					allow = true;
				}
			}
			else
			{
				allow = true;
			}
			return allow;
		}
	);
	return this;
}

$(document).ready(function(){
	$('ul#navCol li ul').hide();
	curCat = $('input#rootCategoryId').val();
	$('ul#navCol li#categoryMenu'+curCat+' ul').show();
	
	$('ul#navCol li h2.expandingMenu').bind('click',function(){
		$(this).parent().find('ul').toggle('slow');
	});
});