function client_application_toggle_form()
{
	$('client_application_person').hide(); 
	$('client_application_company').hide(); 
	$('client_application_' + Form.getInputs('client_application_controller','radio','application_type').find(function(radio) { return radio.checked; }).value).show();
}

function addRows(tableid)
{
	var t = $(tableid);
	var rows = t.getElementsByTagName('TR');
	var newrow = rows[rows.length - 1].cloneNode(true);
	
	var inputs = newrow.getElementsByTagName('INPUT');
	var selects = newrow.getElementsByTagName('SELECT');
	
	fixnames(inputs, rows.length - 2, rows.length - 1);
	fixnames(selects, rows.length - 2, rows.length - 1);
	t.down('tbody').insert(newrow);

}

function fixnames(inputs, oldindex, newindex)
{
	var i = 0;
	while (i < inputs.length)
	{
		inputs[i].name = inputs[i].name.replace('[' + oldindex + ']', '[' + newindex + ']');
		if (inputs[i].id)
			inputs[i].id = inputs[i].id.replace('[' + oldindex + ']', '[' + newindex + ']');
		if (inputs[i].type == "select-one") 
			inputs[i].selectedIndex = 0;
		else if (inputs[i].type == "text") 
			inputs[i].value = '';
			
		i++;
	}
}	


document.observe("dom:loaded", function() {

	$$("#headmenulist li").each( function(x) {
		Event.observe($(x), 'mouseover', function(xx) {
			x.addClassName('hover');
		});
	});	
		 
	$$("#headmenulist li").each( function(x) {
		Event.observe($(x), 'mouseout', function(xx) {
			x.removeClassName('hover');
		});
	});	
});

		
function setCookie(name, value, expires, path, domain, secure)
{
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : ";path=/") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function cancelevent(evt, message) {
   evt = (evt) ? evt : window.event;
   alert(message);
   evt.cancelBubble = true;
}

function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function showElement(id)
{
	setDisplay(id, "block");
}

function clearText(field) 
{
    if (field.defaultValue==field.value) 
        field.value = "";
}

function hideElement(id)
{
	setDisplay(id, "none");
}

function setDisplay(id, display)
{
	elem = document.getElementById(id);
	if (elem && elem.style)
	{
		elem.style.display = display;
	}
}

function startList() {
    if (document.all && document.getElementById) {
        navRoot = document.getElementById("headmenulist");
        for (i = 0; i < navRoot.childNodes.length; i++) {
            node = navRoot.childNodes[i];
            if (node.nodeName == "LI") {
                node.onmouseover = function () {this.className += " over";};
                node.onmouseout = function () {this.className = this.className.replace(" over", "");};
            }
        }
    }
}

function popup(mylink, windowname)
{
if (! window.focus)return true;
var href;
if (typeof(mylink) == 'string')
   href=mylink;
else
   href=mylink.href;
window.open(href, windowname, 'width=650,height=500');
return false;
}

function productview_quantity_change()
{
	try
	{
		$('totalprice_total').update( (parseFloat($('totalprice_base').innerHTML) * parseFloat($('quantity[0]').value)).toFixed(2) );
	}
	catch (e) {	}
}

				function showFullSearchResults(cat)
				{
					document.search_form.search_action.value = 'fullresults';
		
					$('searchfrom').setValue(cat);
					$('search_sb_words').setValue($('search_sb_words_source').value);
					document.search_form.submit();
				}

				function addItemToCart(pid)
				{
					$('product[0]').value = pid;
					$('quantity[0]').value = $('q_' + pid).value;
					document.scart_form.submit();
				}
				function orderBy(col)
				{
					var col_current = window.location.search.toQueryParams().ordercol;
					var dir_current = window.location.search.toQueryParams().orderdir;
					if (col == col_current)
						dir = (dir_current == 'desc' || dir_current == undefined ? 'asc' : 'desc');
					else
						dir = 'asc';
					$('ordercol').value = col;
					$('orderdir').value = dir;
					$('product_list_form').submit();
				}

				function showBigContent(vcat_id, pg_id)
				{
					new Ajax.Request('/products/ajax/content/',
						{
							parameters: {
								vcat_id: vcat_id,
								pg_id: pg_id
							},
							onSuccess: function (t)
							{
								var response = t.responseText.strip();
								if (response.length > 0)
									$('contentdiv').update(response);
							}
						}
					);
				}

				function showCat(vcat_id, pg_id, subgroups, getcontent)
				{
					if (subgroups == 0)
					{
						// show contents
						window.location.href="/products/?pg_id="+pg_id;
						return;
					}

					if (vcat_id == -1)
					{
						vcat_id = 0;
						if (getCookie('last_vcat_id'))
							vcat_id = getCookie('last_vcat_id');
					}
					if (pg_id == -1)
					{
						pg_id = 0;
						if (getCookie('last_pg_id'))
							pg_id = getCookie('last_pg_id');
					}
					setCookie('last_vcat_id', vcat_id);
					setCookie('last_pg_id', pg_id);

					if (getcontent == 1)
					{
						// notify spam-o-matic
						if (self.showBigContent)
							showBigContent(vcat_id, pg_id);
						if (self.showBanners && pg_id == 0)
							showBanners(vcat_id);
					}
					

					$('producttree_data').update( $('loading').innerHTML );
					new Ajax.Updater('producttree_data', '/products/ajax/subcats/',
						{
							parameters: {
								ptree_action: 'getsubcats',
								vcat_id: vcat_id,
								pg_id: pg_id
							}
						}
					);
				}