var currentPostId=-1;

var selectedTemplate = 'NULL';
var selectedTemplateBkgd = 'yellow';
var lastSelected = '1';

include("/picks/js/picks_configs.js");

function hideListmania(lid){
   new Ajax.Request(
      'showHideListmania.ajax.php',
      {
         method: 'get',
         parameters: 'list_id='+lid+'&showhide='+($('showhide'+lid).className == 'hideIt'?0:1),
         onSuccess: function(rs){
            //alert(rs.responseText);
            e = $('showhide'+rs.responseText);
            if(e.className == 'hideIt') e.className = 'showIt';
            else e.className = 'hideIt';
         }
      }
   );
}

function resetExtraScores(){
   for(i=1;i<=5;i++){
      if(ori_extras[i-1]){
         $('isextra'+i).checked = true;
         $('extra'+i).value = ori_extras[i-1];
         $('extra'+i).disabled = false;
      }else{
         $('isextra'+i).checked = false;
         $('extra'+i).value = '';
         $('extra'+i).disabled = true;
      }
      if(ori_scores[i-1]){
         $('isscore'+i).checked = true;
         $('score'+i).value = ori_scores[i-1];
         $('score'+i).disabled = false;
      }else{
         $('isscore'+i).checked = false;
         $('score'+i).value = '';
         $('score'+i).disabled = true;
      }
   }
}

function updateExtraScores(id){
   beDel = false;
        msg = CHECK_DELETE;
   for(i=1;i<=5;i++){
      if(ori_extras[i-1] && !$('isextra'+i).checked){
         msg += '['+ori_extras[i-1]+'] ';
         beDel = true;
      }
      if(ori_scores[i-1] && !$('isscore'+i).checked){
         msg += '['+ori_scores[i-1]+'] ';
         beDel = true;
      }     
   }
   if(beDel){
      getConfirmBox(msg ,10,'doUpdateExtraScores('+id+')');
      funpDlg.show('confirmBox');
   }else{
      doUpdateExtraScores(id);
   }
}

function doUpdateExtraScores(id){
   funpDlg.close();
   new Ajax.Request(
      PICKS_P+'updateExtraScores.ajax.php',
      {
         method: 'post',
         parameters: Form.serialize($('extrascoreform'))+'&list_id='+id,
         onSuccess: function(rs){
            showAlertBox(rs.responseText);
            //location.reload();
            for(i=1;i<=5;i++){
               if($('isextra'+i).checked) ori_extras[i-1] = $F('extra'+i);
               else{
                  ori_extras[i-1] = null;
                  $('extra'+i).value = '';
               }
               if($('isscore'+i).checked) ori_scores[i-1] = $F('score'+i);
               else{
                  ori_scores[i-1] = null;
                  $('score'+i).value = '';
               }
            }
            funpDlg.close();
            showAlertBox(MODIFY_SUCCSESS);
         }
      }
   ); 
}

function leaveWords(id){
   if($F('words') == '') return;
   params = "words="+$F('words');
   new Ajax.Request(
      PICKS_P+'leaveWords.ajax.php?list_id='+id,
      {
         method: 'post',
         parameters: params,
         onSuccess: function(rs){
            
            if(rs.responseText != 'fail'){
               //alert(rs.responseText);
               rsobj = eval('('+rs.responseText+')');
               rtmsg = '<a class=\"profileLink\" href="/people/profile/?username='+rsobj.name+'\" onclick=\"showProfile('+rsobj.id+',event);return false;\"><img src=\"/base/images/avas.png\" class=\"avatar\"> <span>'+rsobj.name+'</span></a> 說 '+rsobj.msg;
               e = $('logBody').insertRow(0);
               c1 = e.insertCell(0);
               c2 = e.insertCell(1);
               c1.innerHTML = rtmsg;
               c2.innerHTML = rsobj.time
               $('words').value = '';
            }
         }
      }
   );
}


var tagtype;
var be = null;

function setTag(e,lid,tid){
   showLoading();

   if(e.checked) {
      act = 'add';
//      id = lid;   
   }else{
      act = 'rm';
//      id = tid;
   }
   $('tagstag').value = e.value;
   showLoading();

   if(be == null){
      be = e;
      modifyListTag(lid,tid,act,'tags');
   }else{
      setTimeout(500,"modifyListTag("+lid+","+tid+","+act+",'tags')");
   }
}   

function modifyListTag(lid,tid, act,type){
   if(act == 'add' && $F(type+'tag') == '') return;
   tagtype = type;
   showLoading(); 
   new Ajax.Request(
      PICKS_P+'main/modifytag.ajax.php?tag='+encodeURIComponent($(type+'tag').value)+'&tid='+tid+'&lid='+lid+'&'+act+'&type='+type,
      {
         method: 'get',
         onSuccess: function(rs){
            id = rs.responseText;
//            alert(id);
            target = (tagtype == 'tags')? 'tags':'listtags';
            if(id > 0){
               if($(target+id)){
                  showAlertBox('該標籤已存在');
                  return;
               }
               if(be) be.tid = id;
               new Insertion.Bottom(target,
                  '<a href="javascript:modifyListTag('+lid+','+id+',\'rm\',\''+target+'\')"'+
                  'id="'+target+id+'" title="刪除標籤" class="removableTag">'+$F(target+'tag')+'</a>');
            }else if(id < 0){
               tid=-id;
               if(target != 'listtags')
               {
               cs = document.getElementsByName('types');
               for(i=0;i<cs.length;i++){
                  if(cs[i].id == 'chk'+tid){ 
                     cs[i].checked = false;
                     break;
                  }
               }   
               }
               $(target).removeChild($(target+(tid)));
            }
            $(target+'tag').value = '';
            be = null;
            hideLoading();
         }
      }   
   );     
}


function cancelApply(id){
   new Ajax.Request(
      PICKS_P+'cancelApply.ajax.php?list_id='+id,
      {
         method: 'get',
         onSuccess: function(rs){
            $('member_'+rs.responseText).hide();
         }
      }
   );
}

function applyList(id,name,title){
   location = FUNP_WEB_ROOT + PICKS_P + 'apply.php?list_id='+id+'&list_name='+name+'&list_title='+title;
}

function applyList2(id){
   new Ajax.Request(
      PICKS_P+'applyList.ajax.php?list_id='+id,
      {
         method: 'get',
         onSuccess: function(rs){
            showAlertBox(SUBMIT_FORM,'http://'+tmp_list_name+'.'+LISTMANIA_PATH);
         }
      }
   );
}

function dismissList(name,list_id){
   deleteList('您確定要刪除精華區 '+name+' 嗎？',list_id);
}

function cancelInvite(list_id,user_id){
   new Ajax.Request(
      PICKS_P+'cancelInvite.ajax.php?list_id='+list_id+'&user_id='+user_id,
      {
         method: 'get',
         onSuccess: function(rs){
            //alert(rs.responseText);
            if(rs.responseText == 'norights'){
               showAlertBox(NOT_ALLOW);
            }else{
               $('apptbody').removeChild($('member_'+rs.responseText));
            }
        }
      }
   );
}

function quitFromList(list_id,name){
        msg = '您確定要退出精華區社群 '+name+' 嗎？';
        getConfirmBox(msg,17,'doQuitFromList('+list_id+')');
        funpDlg.show('confirmBox');
}

function doQuitFromList(list_id){
   funpDlg.close();
   new Ajax.Request(
      PICKS_P+'quitFromList.ajax.php?list_id='+list_id,
      {
         method: 'get',
         onSuccess: function(rs){
            //alert(rs.responseText);
            location.reload();
         }
      }
   );
}

function inviteMember(list_id){
   if($F('member_id') == '') return;
   new Ajax.Request(
      PICKS_P+'inviteMember.ajax.php?list_id='+list_id+'&username='+$F('member_id')+'&rights='+getRadioValue('rights'),
      {
         method: 'get',
         onSuccess: function(rs){
            //alert(rs.responseText);
            if(rs.responseText == 'norights'){
               showAlertBox(NOT_ALLOW);
            }else if(rs.responseText == 'nouser'){
               showAlertBox(INVALID_USER);
            }else if(rs.responseText == 'exist'){
               showAlertBox(USER_JOINED);
            }else{
               //alert(rs.responseText);
               showAlertBox(INVITE_SUCCSESS);
               if($('member_'+rs.responseText)){
                  Element.show('member_'+rs.responseText);
               }else{
                  rsobj = eval('('+rs.responseText+')');
                  e = $('apptbody').insertRow(0);
                  e.id = 'member_'+rsobj.lid+'_'+rsobj.uid;
                  c0 = e.insertCell(0);
                  c1 = e.insertCell(1);
                  c2 = e.insertCell(2);
                  c0.innerHTML = '<a href="'+PEOPLE_P+'profile/?username='+rsobj.name+'"><img src="'+BASE_P+'A/S'+rsobj.ava+'?v=1" class="avatar"> '+rsobj.name+'</a>';
                  c1.innerHTML = rsobj.mtype;
                  c2.innerHTML = rsobj.text+' <a href="javascript:cancelInvite('+rsobj.lid+','+rsobj.uid+')">Cancel</a>';
               }
            }
         }
      }
   );
}

function promoteMember(list_id,user_id){
   new Ajax.Request(
      PICKS_P+'promoteMember.ajax.php?list_id='+list_id+'&user_id='+user_id,
      {
         method: 'get',
         onSuccess: function(rs){
            //alert(rs.responseText);
            if(rs.responseText == 'norights'){
               showAlertBox(NOT_ALLOW);
            }else{
               window.location.reload();
            }
         }
      }
   );
}

function demoteMember(list_id,user_id){
   new Ajax.Request(
      PICKS_P+'demoteMember.ajax.php?list_id='+list_id+'&user_id='+user_id,
      {
         method: 'get',
         onSuccess: function(rs){
            //alert(rs.responseText);
            if(rs.responseText == 'norights'){
               getAlertBox(NOT_ALLOW);
               funpDlg.show('alertBox');
            }else{
               window.location.reload();
            }
         }
      }
   );
}

function kickMember(list_id,user_id,name){
        msg = '您確定要將您的精華區成員 '+name+' 由列表中刪除嗎？';
        getConfirmBox(msg,10,'doKickMember('+list_id+','+user_id+')');
        funpDlg.show('confirmBox');
}

function doKickMember(list_id,user_id){
   new Ajax.Request(
      PICKS_P+'kickMember.ajax.php?list_id='+list_id+'&user_id='+user_id,
      {
         method: 'get',
         onSuccess: function(rs){
//            alert(rs.responseText);
            if(rs.responseText != 'norights'){
               Element.hide('member_'+rs.responseText);
               funpDlg.close();
            }else{
               funpDlg.close();
               showAlertBox(NOT_ALLOW);
            }
         }
      }
   );
}

function updateInviteApply(userid,lmid,yesno){
   if(yesno) yesno = 1;
   else yesno = 0;
   new Ajax.Request(
      PICKS_P+'updateInviteApply.ajax.php?userid='+userid+'&yesno='+yesno+'&lmid='+lmid,
      {
         method: 'get',
         onSuccess: function(rs){
            rsobj = eval('('+rs.responseText+')');
            location.reload();
         }
      }
   );
}

function delPostFromList(post_id, lid, list_id, beOnList){

   if(beOnList)   
      msg = RETURN_ARTICLE;
   else     
      msg = DELETE_ARTICLE;

   getConfirmBox(msg ,10,'doDelPostFromList('+post_id+','+lid+','+list_id+','+beOnList+')');
   funpDlg.show('confirmBox');
}

function doDelPostFromList(post_id, lid, list_id, beOnList) {
   params = 'list_id='+list_id;
   params += '&post_id='+post_id;
   params += '&onlist='+(beOnList?1:0);
   params += '&listing_id='+lid;
   new Ajax.Request( PICKS_P+'delFromList.ajax.php',
      { method: 'post',
        parameters: params,
        onSuccess: delPostFromListCallback
   });
}

function delPostFromListCallback(t) {
   funpDlg.close();
   rsobj = ej(t.responseText);
   if (rsobj.res == 'ok') {
      Element.remove('post_'+rsobj.post_id+'_'+rsobj.list_id);
   } else {
      showAlertBox('刪除失敗');
   }
}

var params;
function deleteList2(){
   new Ajax.Request( PICKS_P+'deleteList.ajax.php',
      { method: 'post',
        parameters: params,
        onSuccess: deleteListCallback
   });
}

function deleteList(msg, list_id) {
   params = 'list_id='+list_id;
   getConfirmBox(msg,10,'deleteList2');
   funpDlg.show('confirmBox');
}

function deleteListCallback(t) {
   resp = t.responseText;
   //alert(resp);
   try {
      rsobj = eval( '(' + resp + ')' );
   } catch (e) {
      alert(resp);
      return;
   }
   //alert(rsobj.res);
   redirect(PICKS_P+"new.php");
}

function showInputDesc(post_id, list_id) {
   if(currentPostId>0 && currentPostId!=post_id){
      updateListDesc(currentPostId, list_id);
   }
   currentPostId=post_id;
   Element.hide('show_desc_'+post_id+'_'+list_id);
   Element.show('div_desc_'+post_id+'_'+list_id);
   Element.show('editingPanel_'+post_id+'_'+list_id);
   Element.show('clear_'+post_id+'_'+list_id);
   Element.hide('edit_'+post_id+'_'+list_id);
   Element.update('desc_left_'+post_id+'_'+list_id, 1024)
   $('input_desc_'+post_id+'_'+list_id).focus();
}

function clearListDesc(post_id, list_id){
   $('input_desc_'+post_id+'_'+list_id).value = '';
   currentPostId=post_id;
   updateListDesc(post_id, list_id);
}

function cancelListDesc(post_id, list_id){
   $('input_desc_'+post_id+'_'+list_id).value = $('show_desc_'+post_id+'_'+list_id).innerHTML.unescapeHTML();
   if( $('input_desc_'+post_id+'_'+list_id).value.length>0){
      Element.show('show_desc_'+post_id+'_'+list_id);
      Element.show('clear_'+post_id+'_'+list_id);
   }else{
      Element.hide('clear_'+post_id+'_'+list_id);
   }
   Element.hide('div_desc_'+post_id+'_'+list_id);
   Element.hide('editingPanel_'+post_id+'_'+list_id);
   Element.show('edit_'+post_id+'_'+list_id);
}

function updateListDesc(post_id, list_id) {
   params = 'list_id='+list_id;
   params += '&post_id='+post_id;
   params += '&description='+encodeURIComponent($F('input_desc_'+post_id+'_'+list_id));
   new Ajax.Request( PICKS_P+'updateDesc.ajax.php',
      { method: 'post',
        parameters: params,
        onSuccess: updateListDescCallback
   });
}

function updateListDescCallback(t) {

   rsobj = ej( t.responseText );
   if( rsobj.res == 'ok' ){
      post_id = rsobj.post_id;
      list_id = rsobj.list_id;

      Element.update('show_desc_'+post_id+'_'+list_id, $F('input_desc_'+post_id+'_'+list_id));
      Element.hide('div_desc_'+post_id+'_'+list_id);
      if($F('input_desc_'+post_id+'_'+list_id).length>0){
         Element.show('show_desc_'+post_id+'_'+list_id);
         Element.show('clear_'+post_id+'_'+list_id);  
      }else{
         Element.hide('show_desc_'+post_id+'_'+list_id);
         Element.hide('clear_'+post_id+'_'+list_id);  
      }

      Element.show('edit_'+post_id+'_'+list_id);
      Element.hide('editingPanel_'+post_id+'_'+list_id);
   }
}

function selectTemplate(x,adjust) {
//   if (x == lastSelected) return;
   is_user_defined=false;
   if (lastSelected) Element.removeClassName('templatebox_'+lastSelected, 'selected');
   if(adjust){
      $('template_preview').scrollTop=Position.cumulativeOffset($('templatebox_'+x))[1]-Position.cumulativeOffset($('templatebox_1'))[1];
      if($('template_preview').scrollTop<250)
         $('template_preview').scrollTop=0;
   }
   Element.addClassName('templatebox_'+x, 'selected');
 //  $('template_'+x).checked = true;
   lastSelected = x;
   $('changeTo').innerHTML=DEFAULT_STYLE+' － '+$('name_'+x).innerHTML;
   if(currentSelected==x){
      $('template_change').disabled=true;
   }else{
      $('template_change').disabled=false;
   }
   Element.show('customize');
   Element.hide('cancel');
}

var is_user_defined=false;
function user_defined(url){
   is_user_defined=true;
   Element.hide('customize');
   Element.show('cancel');
   if(!url)
      $('changeTo').innerHTML='自訂樣式 － <input maxlegnth="256" class="fullLEngth" id="userdefinedCSS" onFocus="user_defined_onfocused()" value="請輸入自行設計之 CSS 樣式表網址">'   
   else
      $('changeTo').innerHTML='自訂樣式 － <input maxlegnth="256" class="fullLEngth" id="userdefinedCSS" onFocus="user_defined_onfocused()" value="'+url+'">'
   $('template_change').disabled=false;         
}
function user_defined_onfocused(){
   if($('userdefinedCSS').value==INPUT_SELF_CSS )
      $('userdefinedCSS').value='';
}
function cancel_user_defined(){
   selectTemplate(lastSelected);
}

function submitTemplateChange(){
   showLoading();
   if(!is_user_defined){
      new Ajax.Request(PICKS_P+'submitTemplateChange.ajax.php',
         {
            method: 'post',
            parameters: 'template_id='+ encodeURIComponent(lastSelected)+
                        '&list_id='+ currentListId,
            onSuccess:  submitTemplateChangeCallback
         }
      );
   }else{
      new Ajax.Request(PICKS_P+'submitTemplateChange.ajax.php',
         {
            method: 'post',
            parameters: 'user_defined_template='+ encodeURIComponent($('userdefinedCSS').value)+
                        '&list_id='+ currentListId,
            onSuccess:  submitTemplateChangeCallback
         }
      );
   }
}

function submitTemplateChangeCallback(t){
   resp = t.responseText;
   if(resp != 'ok')
      alert(resp);
   location.reload();
}

var pre_action = null,pre_target = null;

function deleteWidget(msg, widget_id) {
   pre_action = "delete";
   pre_target = widget_id;
   getConfirmBox(msg,10,'customize_widget');
   funpDlg.show('confirmBox');
}

function customize_widget( action, target ) {
   if( action == 'delete' ) {
      deleteWidget(DELETE_COMPONENT, target );
      return;
   } else if( typeof action=="undefined" ) {
      if( pre_action == null ) return;
      action = pre_action;
      target = pre_target;
      funpDlg.close();
   }
   showLoading();
   new Ajax.Request(PICKS_P+'setCustomizeWidget.ajax.php',
      {
         method: 'post',
         parameters: 'action='+ encodeURIComponent(action)+
                     '&target='+ encodeURIComponent(target)+
                     '&list_id='+ currentListId,
         onSuccess:  function(t) {
            resp = t.responseText;
            try {
               rsobj = eval( '(' + resp + ')' );
            } catch (e) {
               alert(resp);
               return;
            }
            if( rsobj.res == 'error' ) {
               alert(resp); return;
            }
            Element.update('customize_widget_form', rsobj.form);
            hideLoading();
            if( action == 'default' )
               Element.hide('customize_widget_form');
            else
               Element.show('customize_widget_form');
         }
      }
   );
   pre_action = null;
   pre_target = null;
}

function CreateWidgetConfigEditor( wid, type )
{
   id = type+'_'+wid;
   e = new Ajax.InPlaceEditor(id,PICKS_P+'setCustomizeWidget.ajax.php?action=edit'+type+'&target='+wid+'&list_id='+currentListId, {
           okText: CONFIRM , cancelText:CANCLE, clickToEditText: MODIFY, rows: type == 'content' ? 10:1,
           onComplete: function(transport,element){
      }
   });
   e.onEnterEditMode = function(){
      if(this.getText() == NO_CONFIG) this.element.innerHTML = '';
   };
   e.onLeaveEditMode = function(){
      if(this.getText() == '') this.element.innerHTML = NO_CONFIG;
   }
}

var name_length = "20";;
var title_length = "40";;
var desc_length = "512";;


function checkForm() {
   if ($F('input_name').length == 0) {
      alert(INPUT_NAME);
      return false;
   } else if (!$F('input_name').match(/^[0-9a-zA-Z]+$/)) {
      alert(NAME_CANT_IN_SPACE);
      return false;
   } else if ($F('input_name').length > name_length) {
      alert(NAME_TOO_LONG);
      return false;
   } else if ($F('input_title').length == 0) {
      alert(INPUT_TITLE);
      return false;
   } else if ($F('input_title').length > title_length) {
      alert(TITLE_TOO_LONG);
      return false;
   } else if ($F('input_desc').length == 0) {
      alert(INPUT_DESCRIPTION);
      return false;
   } else if ($F('input_desc').length > desc_length) {
      alert(INPUT_DESCRIPTION);
      return false;
   }else if($('min_pushes')){
      if(!validPositiveInt($F('min_pushes')) || $F('min_pushes') < 1){
         alert(MIN_IS_ONE);
         return false;
      }
   }
   return true;
}

function checkForm2() {
   if ($F('input_title').length == 0) {
      alert(INPUT_TITLE);
      return false;
   } else if ($F('input_title').length > title_length) {
      alert(TITLE_TOO_LONG);
      return false;
   } else if ($F('input_desc').length == 0) {
      alert(INPUT_DESCRIPTION);
      return false;
   } else if ($F('input_desc').length > desc_length) {
      alert(INPUT_DESCRIPTION);
      return false;
   }else if($('min_pushes')){
      if(!validPositiveInt($F('min_pushes')) || $F('min_pushes') < 1){
         alert(MIN_IS_ONE);
         return false;
      }
   }
   return true;
}

function clearInput(id) {
   if ( id == null ) {
      $('input_name').value = '';
      $('input_title').value = '';
      $('input_desc').value = '';
   } else {
      $(id).value = '';
   }
   Element.update('left_name', ''+(name_length-$F('input_name').length));
   Element.update('left_title', ''+(title_length-$F('input_title').length));
   Element.update('left_desc', ''+(desc_length-$F('input_desc').length));
}

function updateInput(id) {
   if ( id == 'input_name' ) {
      Element.update('left_name', ''+(name_length-$F('input_name').length));
   } else if ( id == 'input_title' ) {
      Element.update('left_title', ''+(title_length-$F('input_title').length));
   } else if ( id == 'input_desc' ) {
      Element.update('left_desc', ''+(desc_length-$F('input_desc').length));
   }
}

function submitPostsForm() {
      params = 'tab=members';
      params += '&list_id='+$F('input_list_id');
      params += '&need_member='+($('need_member').checked? 1:0);
      params += '&min_pushes='+($('min_pushes')? $F('min_pushes'):1);
      params += '&welcome_join='+($('welcome_join_yes')? ($('welcome_join_yes').checked?1:0):1);
//      alert(params);
      new Ajax.Request(PICKS_P+'saveModifyList.ajax.php',
         { method: 'post',
           parameters: params,
           onSuccess: submitPostsFormCallback });
}


function submitPostsFormCallback(t) {
   resp = t.responseText;
   try {
      rsobj = eval( '(' + resp + ')' );
   } catch (e) {
      alert(resp);
      return;
   }
   if (rsobj.res == 'ok') {
//      alert('修改成功');
//      redirect('?tab='+rsobj.tab+'&list_name='+$F('input_name'));
   } else {
      if (rsobj.msg == 'name_exists') {
         alert(NAME_REAPEAT);
      } else {
         alert('輸入有誤!');
         //alert(resp);
      }
   }
}

function submitSettingsForm() {

   if (checkForm2()) {
      bepub = 1;
      gpush = 0;
      if($('listpub_no') && $('listpub_no').checked) bepub = 0;
      if($('guestpush_yes') && $('guestpush_yes').checked) gpush=1;
      params = 'tab=settings';
      params += '&title='+$F('input_title');
      params += '&desc='+$F('input_desc');
      params += '&list_id='+$F('input_list_id');
      params += '&pub='+bepub;
      params += '&guestpush='+gpush;
//      alert(params);
      new Ajax.Request(PICKS_P+'saveModifyList.ajax.php',
         { method: 'post',
           parameters: params,
           onSuccess: submitFormCallback });
   }

}

function submitFormCallback(t) {
   resp = t.responseText;
   try {
      rsobj = eval( '(' + resp + ')' );
   } catch (e) {
      alert(resp);
      return;
   }
   if (rsobj.res == 'ok') {
     showAlertBox('修改成功');
//      redirect('?tab='+rsobj.tab+'&list_name='+$F('input_name'));
   } else {
      if (rsobj.msg == 'name_exists') {
         alert(NAME_REAPEAT);
      } else {
         showAlertBox('輸入有誤!');
         //alert(resp);
      }
   }
}

function updateCreateListForm(e)
{
  if ( e == 'input_title' )
    p = 'list_title';
  else if ( e == 'input_name' )
    p = 'list_name';
  else if ( e == 'input_desc' )
    p = 'list_description';
  else
    return ;

  params = p + '=' + $F(e);
  new Ajax.Request
  ( PICKS_P+'updateCreateListForm.ajax.php',
    {
      method: 'post',
      parameters: params,
      onSuccess: function(rs) {
         if(!rs.responseText.length) return;
         rsobj = eval( '(' + rs.responseText + ')' );
         if($(e + '_err_msg')) $(e + '_err_msg').innerHTML = rsobj.errmsg;
      }
    }
  );
}

function setCreateListFormTag(e,id)
{
  if(e.checked) 
  {
    act = 'addtag';
  }
  else
  {
    act = 'deltag';
  }
//   $('tagstag').value = e.value;
  params = act + '=' + e.value + '&id=' + id;
  new Ajax.Request
  ( PICKS_P+'updateCreateListForm.ajax.php',
    {
      method: 'post',
      parameters: params,
      onSuccess: function(rs)
      {
        if(rs.responseText)
        {
          if(act == 'addtag')
          {
            id=rs.responseText;
            tg='tags'+rs.responseText;
            new Insertion.Bottom('tags',
              '<a href="javascript:delCreateListFormTag('+id+')"'+
              'name="alltags" id='+tg+' title="刪除標籤" class="removableTag">'+e.value+'</a>');              
          }
          else if(act == 'deltag')
          {
            id=rs.responseText;
            target='tags';
            $(target).removeChild($(target+(id)));
          }
        }
      }
    }
  );
}

function setCustomCreateListFormTag(t)
{
  params = 'addmytag=' + $F(t) + '&id=17';
  new Ajax.Request
  ( PICKS_P+'updateCreateListForm.ajax.php',
    {
      method: 'post',
      parameters: params,
      onSuccess: function(rs)
      {
        if(rs.responseText)
        {
          id=rs.responseText;
          tg='tags'+rs.responseText;
          new Insertion.Bottom('tags',
              '<a href="javascript:delCreateListFormTag('+id+')"'+
              'name="alltags" id='+tg+' title="刪除標籤" class="removableTag">'+$F(t)+'</a>');              
          $('tagstag').value='';
        }
      }
    }
  );
}

function delCreateListFormTag(id)
{
  params = 'deltag=true&id=' + id;
  
  new Ajax.Request
  ( PICKS_P+'updateCreateListForm.ajax.php',
    {
      method: 'post',
      parameters: params,
      onSuccess: function(rs)
      {
        target = 'tags';
        id=rs.responseText;
        if(rs.responseText)
        {
          cs = document.getElementsByName('types');
          for(i=0;i<cs.length;i++)
          {
            if(cs[i].id == 'chk'+id)
            {
              cs[i].checked = false;
              break;
            }
          }
          $(target).removeChild($(target+(id)));
        }
      }
    }
  );
}

function submitCreateListForm() {
   if (checkForm()) {
     params = "submit=true";
  
     new Ajax.Request
     ( PICKS_P+'updateCreateListForm.ajax.php',
       {
         method: 'post',
         parameters: params,
         onSuccess: function(rs)
         {
           resp=rs.responseText;
           try {
             rsobj = eval( '(' + resp + ')' );
           } catch (e) {
            alert(resp);
             return;
           }
           
           if(rsobj.res=='error')
           {
             $('title_err_msg').innerHTML = '';
             $('name_err_msg').innerHTML = '';
             
             $(rsobj.type).innerHTML = rsobj.errmsg;
             alert(rsobj.errmsg);
             return;
           }
           
           if(rsobj.res == 'ok')
           {
             redirect(PICKS_P + 'new.php');
           }
           else
           {
             alert(UNDIFINE_ERROR);
           }

                      
         }
       }
     );
   }
}

function clearCreateListForm()
{
  params = "list_name=&list_title=&list_description=";
  
  new Ajax.Request
  ( PICKS_P+'updateCreateListForm.ajax.php',
    {
      method: 'post',
      parameters: params,
      onSuccess: function(rs)
      {
        $('input_name').value = '';
        $('input_title').value = '';
        $('input_desc').value = '';
      }
    }
  );
}

function applyJoin(list_id)
{
   new Ajax.Request
   (
     PICKS_P+'applyJoinMember.ajax.php?list_id='+list_id,
     {
       method: 'get',
       onSuccess:  function(rs)
       {
         alert(rs.responseText);
//         redirect(PICKS_P + 'favo.php?tab=list');
         location.reload();
       }
     }
   );
}


function cancelJoin(member_id)
{
   new Ajax.Request
   (
     PICKS_P+'cancelJoinMember.ajax.php?member_id='+member_id,
     {
       method: 'get',
       onSuccess:  function(rs)
       {
         alert(rs.responseText);
//         redirect(PICKS_P + 'favo.php?tab=list');
         location.reload();
       }
     }
   );
}

// forums
function showBoardDesc(board_id) {
   Element.hide('show_desc_'+board_id);
   Element.show('div_desc_'+board_id);
   Element.show('editingPanel_'+board_id);
   Element.hide('edit_'+board_id);
   Element.update('desc_left_'+board_id, 1024)
   $('input_desc_'+board_id).focus();
}


function cancelBoardDesc(board_id){
   $('input_desc_'+board_id).value = $('show_desc_'+board_id).innerHTML.unescapeHTML();
   if( $('input_desc_'+board_id).value.length>0){
      Element.show('show_desc_'+board_id);
   }
   Element.hide('div_desc_'+board_id);
   Element.hide('editingPanel_'+board_id);
   Element.show('edit_'+board_id);
}

function updateBoardDesc(board_id) {
   params = 'board_id='+board_id;
   params += '&board_desc='+encodeURIComponent($F('input_desc_'+board_id));
   new Ajax.Request( BOARD_P+'updateBoardDesc.ajax.php',
   { method: 'post',
        parameters: params,
        onSuccess: updateBoardDescCallback
   });
}


function updateBoardDescCallback(t) {

   rsobj = ej( t.responseText );
   if( rsobj.success == 1 ){
      board_id = rsobj.board_id;

      Element.update('show_desc_'+board_id, $F('input_desc_'+board_id));
      Element.hide('div_desc_'+board_id);
      Element.show('show_desc_'+board_id);

      Element.show('edit_'+board_id);
      Element.hide('editingPanel_'+board_id);
   }
}

function showBoardName(board_id) {
   Element.hide('show_name_'+board_id);
   Element.show('div_name_'+board_id);
   Element.show('editingPanel_'+board_id);
   Element.hide('edit_'+board_id);
   Element.update('name_left_'+board_id, 1024)
   $('input_name_'+board_id).focus();
}


function cancelBoardName(board_id){
   $('input_name_'+board_id).value = $('show_name_'+board_id).innerHTML.unescapeHTML();
   if( $('input_name_'+board_id).value.length>0){
      Element.show('show_name_'+board_id);
   }
   Element.hide('div_name_'+board_id);
   Element.hide('editingPanel_'+board_id);
   Element.show('edit_'+board_id);
}

function updateBoardName(board_id) {
   params = 'board_id='+board_id;
   params += '&board_name='+encodeURIComponent($F('input_name_'+board_id));
   new Ajax.Request( BOARD_P+'updateBoardName.ajax.php',
   { method: 'post',
        parameters: params,
        onSuccess: updateBoardNameCallback
   });
}


function updateBoardNameCallback(t) {

   rsobj = ej( t.responseText );
   if( rsobj.success == 1 ){
      board_id = rsobj.board_id;

      Element.update('show_name_'+board_id, $F('input_name_'+board_id));
      Element.hide('div_name_'+board_id);
      Element.show('show_name_'+board_id);

      Element.show('edit_'+board_id);
      Element.hide('editingPanel_'+board_id);
   }
}

function setBoardStatus(board_status, list_id, board_id)
{
   params='tab=board&list_id='+list_id+'&board_id='+board_id+'&board_status='+board_status;
   new Ajax.Request
   (
     PICKS_P+'saveModifyList.ajax.php',
     {
       method: 'post',
       parameters: params,
       onSuccess:  function(rs)
       {
          resp =rs.responseText;
          try {
            rsobj = eval( '(' + resp + ')' );
          } catch (e) {
            alert(resp);
            return;
          }

          if (rsobj.res == 'ok') {
            showAlertBox('修改成功');
          } 
       }
     }
   );
}

function searchForums(list_name)
{
  location = '?tab=board&list_name='+ list_name +'&opt=waiting&user=' + encodeURIComponent($('user').value) + '&keyword=' + encodeURIComponent($('keyword').value);
}

function editTagName(tag_id) {
   Element.hide('show_name_'+tag_id);
   Element.show('div_name_'+tag_id);
   Element.show('editingPanel_'+tag_id);
   Element.hide('edit_'+tag_id);
   Element.update('name_left_'+tag_id, 10)
   $('input_name_'+tag_id).focus();
}

function cancelEditTag(tag_id){
   $('input_name_'+tag_id).value = $('show_name_'+tag_id).innerHTML.unescapeHTML();
   if( $('input_name_'+tag_id).value.length>0){
      Element.show('show_name_'+tag_id);
   }
   Element.hide('div_name_'+tag_id);
   Element.hide('editingPanel_'+tag_id);
   Element.show('edit_'+tag_id);
}

function saveTagName(tag_id) {
   params = 'tag_id='+tag_id;
   params += '&tag_name='+encodeURIComponent($F('input_name_'+tag_id));
   new Ajax.Request( PICKS_P+'saveListTag.ajax.php',
   { method: 'post',
        parameters: params,
        onSuccess: saveTagNameCallback
   });
}


function saveTagNameCallback(t) {

   rsobj = ej( t.responseText );
   if( rsobj.success == 1 ){
      tag_id = rsobj.tag_id;

      Element.update('show_name_'+tag_id, $F('input_name_'+tag_id));
      Element.hide('div_name_'+tag_id);
      Element.show('show_name_'+tag_id);

      Element.show('edit_'+tag_id);
      Element.hide('editingPanel_'+tag_id);
   }
}

function delTagName(tag_id) {
   params = 'tag_id='+tag_id;
   new Ajax.Request( PICKS_P+'delListTag.ajax.php',
   { method: 'post',
        parameters: params,
        onSuccess: delTagNameCallback
   });
}


function delTagNameCallback(t) {

   rsobj = ej( t.responseText );
   if( rsobj.success == 1 ){
      tag_id = rsobj.tag_id;
      $('tag_list').removeChild($('tag_item_'+tag_id));
   }
}

function addListTagName(list_id) {
   params = 'list_id='+list_id;
   params += '&tag_name='+encodeURIComponent($F('listtagstag'));
   new Ajax.Request( PICKS_P+'addListTag.ajax.php',
   { method: 'post',
        parameters: params,
        onSuccess: addListTagNameCallback
   });
}


function addListTagNameCallback(t) {

   rsobj = ej( t.responseText );
   if( rsobj.success == 1 ){
      tag_id = rsobj.tag_id;
      tag_name = rsobj.tag_name;
      text='<div class="fieldValue" style="height: 60px;" id="tag_item_'+tag_id+'">'+
      '<div onclick="editTagName('+tag_id+')" id="show_name_'+tag_id+'" class="listComments">'+tag_name+'</div>'+
      '<div id="div_name_'+tag_id+'" style="display: none;padding: 5px;">'+
      '<input type="text" class="halfLength" onkeyup="updateTextCounter(\'input_name_'+tag_id+'\',\'name_left_'+tag_id+'\',10, event, \'cancelEditTag('+tag_id+')\');" maxlength="20" value="'+tag_name+'" name="input_name_'+tag_id+'" id="input_name_'+tag_id+'"/>'+
      '</div>'+
      '<span id="editingPanel_'+tag_id+'" style="display: none;float: right;">'+
      '(<span id="name_left_'+tag_id+'">10</span>)\n'+
      '<a href="javascript:saveTagName('+tag_id+')">確定</a>\n'+
      '<a href="javascript:cancelEditTag('+tag_id+')">取消</a>\n'+
      '</span>'+
      '<span style="float:right;" id="edit_'+tag_id+'">'+
      '<a href="javascript:editTagName('+tag_id+')">編輯</a>\n'+
      '<a href="javascript:delTagName('+tag_id+')">刪除</a>\n'+
      '</span>'+
      '</div>';
      new Insertion.Top('tag_list',text);
      $('listtagstag').value="";
      $('input_name_'+tag_id).focus();
   }
}

