
//cookie functions
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function ifEnter(e, btn)
{
	if (window.event && window.event.keyCode == 13)
		$(btn).click();
	else if(e && e.which == 13)
		$(btn).click();
	else
		return true;
}

//ask user before proceeding
function askFirst(text, url)
{
	var answer = confirm (text);
	
	if (answer)
		window.location=url;
}

function toggleCommentArea()
{
	commentareas = $$('div.carea');
	toshow = $F('comment_type');
	var i = 0;
	for(i = 0; i < commentareas.length; i++)
	{
			commentareas[i].hide();
	}
	
	$('carea_'+toshow).show();
}

function toggleDetails(productid)
{
	if($('productsummary_'+productid).style.display == '')
	{	//show details
		Element.hide('productsummary_'+productid);
		Element.show('productdetails_'+productid);
		$('producttoggle_'+productid).innerHTML = 'hide details';
	}
	else
	{	//show summary
		Element.hide('productdetails_'+productid);
		Element.show('productsummary_'+productid);
		$('producttoggle_'+productid).innerHTML = 'show details';
	}
}

function showDirectory(type)
{
	if(type == 'cat')	//cat
	{
		$('dt-alpha').className = '';
		$('dt-cat').className = 'selected';
		
		$('dt-alpha-content').hide();
		$('dt-cat-content').show();
	}
	else				//alpha
	{
		$('dt-alpha').className = 'selected';
		$('dt-cat').className = '';
		
		$('dt-cat-content').hide();
		$('dt-alpha-content').show();
	}
}

function toggleContent(target, opened, closed)
{
	//find target
	var root;
	if(target.substring(0, 1) == '.')
	{
		root = target.substr(1,target.length);		
		var otarget = target;
	}
	else
	{
		root = target;
	}
		
	var eSwitch = $('switch_' + root);
	var target = $('content_' + root);
	
	//todo: add code to handle images for opened and closed
		
	//hide/show
	if (eSwitch.innerHTML == closed)
	{
		eSwitch.innerHTML = opened;

		eSwitch.className = eSwitch.className.replace(' openswitch', '');
		eSwitch.className = eSwitch.className + ' closeswitch';
		
		//if open = close, just hide the switch
		if(opened == closed)
			eSwitch.hide();
		
		//new Effect.BlindDown(target, {});
		if(otarget)
			$$(otarget).each(function(e) {e.show();});
		else
			$(target).show();
			
		createCookie('mallcrawl_toggle_'+root,'hide',30);
	}
	else
	{
		eSwitch.innerHTML = closed;
		
		eSwitch.className = eSwitch.className.replace(' closeswitch', '');
		eSwitch.className = eSwitch.className + ' openswitch';
		
		//new Effect.BlindUp(target, {});
		if(otarget)
			$$(otarget).each(function(e) {e.hide();});
		else
			$(target).hide();
		
		createCookie('mallcrawl_toggle_'+root,'show',30);			
	}		
}