function addTag(tag,title){
	$('span.'+tag).html('');
	$('span.'+tag).html('<a href="#" title="Remove '+title+' from your search" id="'+tag+'" class="onwhite selected" onclick="removeTag(\''+tag+'\',\''+title+'\');return false"><b><b><b>'+title+'</b></b></b></a>');
	
	$('body#front #search .search .tags p.empty:first').removeClass('empty').html('<a href="#" title="Remove '+title+' from your search" onclick="removeTag(\''+tag+'\',\''+title+'\');return false" id="search-'+tag+'">'+title+'</a>');

	setSearch();

}

function warnIfDelete(){
   if (document.manageForm.action.selectedIndex==1){
      var answer = confirm("Are you sure you want to delete?")
      if (!answer){
         return false;
      } 
   }
      return true; 
}
function addMoreTags(tags){
   var index=tags.selectedIndex;
   var title = tags[index].text;
   var tag=tags[index].value; 
   $('#search .tags a').each(function(){
     if(($(this).attr('id')).indexOf("_")>0){
       var x=($(this).attr('id')).split("_");
       x=x[1];
       var y=tag.split("_");
       y=y[1];
       
       if (x==y) 
         $('#'+($(this).attr('id'))).parents('p').addClass('empty').html('&nbsp;');    
     }
     });
  $('body#front #search .search .tags p.empty:first').removeClass('empty').html('<a href="#" title="Remove '+title+' from your search" onclick="removeTag(\''+tag+'\',\''+title+'\');return false" id="search-'+tag+'">'+title+'</a>');   
   setSearch();
}
function addCategory(element){
   var ni = document.getElementById('catfields');
  var newel = document.createElement('input');
  var elIdName = 'cat'+(element+1);
  newel.setAttribute('id',elIdName);
  newel.setAttribute('type','text');
  newel.setAttribute('name', elIdName);  
  newdiv.innerHTML = 'Element Number '+num+' has been added! <a href=\'#\' onclick=\'removeElement('+divIdName+')\'>Remove the div "'+divIdName+'"</a>';
  ni.appendChild(newel);
   
}

function comments(){
    alert("test");
    //document.getElementById("comments").style.visibility="visible";   
}

function addAdditional(tag, value){
    
    $('#search-'+tag).parents('p').addClass('empty').html('&nbsp;'); 
    $('#search-'+tag).remove();     
    if (value != "Select")
      $('body#front #search .search .tags p.empty:first').removeClass('empty').html('<a href="#" id="search-'+tag+'">'+value+'</a>');   
   
   setSearch();
} 
function toggleTag(tag,title) {
	if ($('#search-'+tag).attr('class') == 'faded') {
		$('#search-'+tag).attr('class','');
	} else {
		$('#search-'+tag).attr('class','faded');
	}
	setSearch();
}
function setfocuselement(){
   if (document.getElementById('upctext') != null) {
      document.getElementById('upctext').focus();
  } 
}


function removeTag(tag,title){

	$('span.'+tag).html('');
	$('span.'+tag).html('<a href="#" title="Add '+title+' to your search" id="'+tag+'" class="onwhite" onclick="addTag(\''+tag+'\',\''+title+'\');return false"><b><b><b>'+title+'</b></b></b></a>');
	
	$('#search-'+tag).parents('p').addClass('empty').html('&nbsp;');

	setSearch();

};

function toggleVarietal(){
    var index=document.getElementById("color").selectedIndex;
    //alert(index);
    if (index==1){
     document.getElementById("red").style.display="block";
   }
   else
     document.getElementById("red").style.display="none";   
   if (index==2)
     document.getElementById("white").style.display="block";
   else
     document.getElementById("white").style.display="none";   
}

function querySearch( page ){

	if(!page)page=1;

	$('#main').load(
		'/retail/action/query',
		{

			page:page
		}
	);

};


 
    
     
                     
function setSearch(page){

    if(!page)page=1;

    var tags='';

    var tagNames='';

    $('#search .tags a').each(function(){
           
        if ($(this).attr('class') != 'faded') {
            var t=$(this).attr('id').replace('search-','');
          
            t=t.split("_");
            t=t[0];
            tags+=t+',';
            tagNames+=$(this).html()+',';
        }
    });

    var front=$('body').attr('id')=='front';

    $((front?'#result_count':'#main')).load(

        (front?'/restaurant/action/set-search':'/restaurant/action/search'),

        {

            page:page,

            tags:tags.substr(0,tags.length-1),

            tagNames:tagNames.substr(0,tagNames.length-1)

        }

    );

};

function switchStock( link ){
	var text=($(link).find('strong').html()=='In-Stock'?'Out-of-Stock':'In-Stock');

	var wine=$(link).parents('.wine-manage').attr('id');

	$(link).find('strong').html(text);

	$.post(
        '/retail/action/set-stock',
		{wine:wine}
		
	);

	return false;

}


function limitChars(textid, limit, infodiv)
{
	var text = $(textid).val();
	var textlength = text.length;
	if(textlength > limit)
	{
		$('#' + infodiv).html('You cannot write more then '+limit+' characters!');
		$('#'+textid).val(text.substr(0,limit));
		return false;
	}
	else
	{
		$('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');
		return true;
	}
}

function process(myform){
    var win = window.open('', myform, 'width=600,height=700,resizeable=yes');
    return true;
} 

$(function(){

	$('#note').hide()
	
		.parents('.panel_br').find('h4 a.expand').click(function(){$('#note').toggle();return false});



})

function display_hidden(){
   if (document.create.home_edition.checked==true){
      document.getElementById("f1").style.display='block';
	  document.getElementById("f2").style.display='block';
	  document.getElementById("f3").style.display='block';
	  document.getElementById("f4").style.display='block';
   }
   else{
      document.getElementById("f1").style.display='none';
	  document.getElementById("f2").style.display='none';
	  document.getElementById("f3").style.display='none';
	  document.getElementById("f4").style.display='none';
   }
}


