  var max_tag_sponsor = 20;
  var max_tag;
  var option_add;
  var quantity = 0;
  var tmp_click;
  tmp_max_tag = 0;
  
  function countSpnQuantity()
  { 
    quantity += 1;
    $('spn_quantity').value = quantity;
  }
  function decreaseSpnQuantity()
  {   
    sum = ($('tag_tag_st').value.split(",").length )
    if (sum >= max_tag)    
    {
      quantity = sum - max_tag;
    }
    if (sum < max_tag)    
    {
      quantity = 0;
    }
    $('spn_quantity').value = quantity;    
  }
    
  function Atom_addTagFromTextBox()
  {  
    if ($('tag_tag').value == null)
    {
      alert("please input tag");
      return;
    }
    else
    {
      tmp_tag = $('tag_tag').value.split(",");
    }
    if (tmp_tag.length == 0)
    {
      
      alert("please input tag");
      return;
    }
    else if (tmp_tag.length>0)
    {
      i = 0; 
      for(i=0;i<tmp_tag.length;i++)
      {
        if (tmp_tag[i] !="")
        {
          Atom_addTag(tmp_tag[i]);
        }
      }       
    }    
  }    
  function Atom_addTag(tag)
  {     
    if ($('tag_tag_st').value !="")
    {     
      if ($('tag_tag_st').value.split(",").length >=max_tag_sponsor)
      { 
        alert("คุณสามารถใส่ได้ไม่เกิน "+ max_tag_sponsor +" คำ")
        return false;
      }
      $('tag_tag_st').value += ","+tag;
    }
    else
    {      
      $('tag_tag_st').value += tag;
    }
    $('chosen_tag').innerHTML += "<font style=\"padding-left:128px;\">"+tag+"</font><a href='#' onclick=\"Taiko_removeTag('"+tag+"'); decreaseSpnQuantity(); \">ลบ</a><br/>";
    $('tag_tag').value="";
  }
  
  
  function Taiko_addTagFromTextBox(option)
  {
    option_add = option;
    if (option == 'edit')
    {
      //max_tag = $('tag_tag').value.split(",").length;
      max_tag = $('tag_max').value.split(",").length;
      if (max_tag < 5)
      {
        max_tag = 5;
      }
      if (max_tag < tmp_max_tag)
      {        
        max_tag = tmp_max_tag;
      }
    }
    else
    {
      max_tag = 5;
    }
    if (tmp_max_tag == 0)
    {
      tmp_max_tag = max_tag;
    }    
    if ($('tag_tag').value == null)
    {
      alert("please input tag");
      return;
    }
    else
    {
      tmp_tag = $('tag_tag').value.split(",");
    }
    if (tmp_tag.length == 0)
    {
      
      alert("please input tag");
      return;
    }
    else if (tmp_tag.length>0)
    {
      i = 0; 
      for(i=0;i<tmp_tag.length;i++)
      {
        if (tmp_tag[i] !="")
        {
          Taiko_addTag(tmp_tag[i]);
        }
      }       
    }    
  }    
  function Taiko_addTag(tag)
  {     
    if ($('tag_tag_st').value !="")
    {     
      if ($('tag_tag_st').value.split(",").length >=max_tag)
      { 
        if(confirm("คุณสามารถใส่ได้ไม่เกิน "+ tmp_max_tag +" คำ"))
        {
         //$('pic-add').innerHTML = "<img src='/images/buy-tag.gif' alt='เพิ่มคำหลัก' border='0' title='เพิ่มคำหลัก' onclick='Atom_addTagFromTextBox(); countSpnQuantity();' >";
         return true;         
        }
        else
        {
         return false;
        }
      }
      $('tag_tag_st').value += ","+tag;
    }
    else
    {      
      $('tag_tag_st').value += tag;
    }
    $('chosen_tag').innerHTML += "<font style=\"padding-left:128px;\">"+tag+"</font><a href='#' onclick=\"Taiko_removeTag('"+tag+"'); decreaseSpnQuantity(); \">ลบ</a><br/>";
    $('tag_tag').value="";
  }
  function Taiko_removeTag(tag)
  {
    i = 0;
    tag_array = $('tag_tag_st').value.split(",");
    new_tag_array = new Array();
    for(i = 0; i< tag_array.length; i++ )
    {
      if (tag_array[i] != tag)
      {
        new_tag_array.push(tag_array[i]);        
      }
    }
    $('chosen_tag').innerHTML=""
    $('tag_tag_st').value= ""
    for(i = 0; i< new_tag_array.length; i++ )
    {
      $('chosen_tag').innerHTML +="<font style=\"padding-left:128px;\">"+new_tag_array[i]+"</font><a href='#' onclick=\"Taiko_removeTag('"+new_tag_array[i]+"'); decreaseSpnQuantity(); \">ลบ</a><br/>";
      if ($('tag_tag_st').value !="")
      {
        $('tag_tag_st').value += ","+new_tag_array[i];
      }
      else
      {
        $('tag_tag_st').value += new_tag_array[i];
      }
      //alert("new st value = "+$('tag_tag_st').value);
      $('tag_tag').value="";        
    }
    if ($('tag_tag_st').value.split(",").length < max_tag + 1)
    {
      $('pic-add').innerHTML = "<img src='/images/add.gif' alt='เพิ่มคำหลัก' border='0' title='เพิ่มคำหลัก' onclick=\"Taiko_addTagFromTextBox('"+ option_add +"')\">";
    }
  }
  
  function atom_add(tag)
  {     
    $('web_search').value=tag;
    tmp_click = tag;
    document.getElementById('show-tag-search-list').style.visibility='hidden';
  }   
  function atom_showDisplay()
  { 
    if ($('web_search').value != tmp_click)
    {
      document.getElementById('show-tag-search-list').style.visibility='visible';
    }
  }
  
  
