function getObject(id){
    if (document.all){
        return document.all[id];
    }
    else{
        return document.getElementById(id);
    }
}
// Suggest Search
function autoToggle(el,what){
      el.style.display=what;
}
var _Timer;
function autoSuggest(eID,text,_type,_delay){
      if(typeof(_delay)=='undefined'){
            clearTimeout(_Timer);
            _Timer = setTimeout("autoSuggest('"+eID+"','"+text+"','"+_type+"',1)",1000);
            return;
      }
      if($(eID).style.display!='block'){
            $(eID).style.display='block';
      }
      if(typeof(_type) == 'undefined' || _type=='undefined'){
            _type=getQuery('type');
      }
      $(eID).innerHTML="";
       var jSonRequest = new Json.Remote(_USER_INFO['EnvRoot']+'/remote/search.php', {
            onComplete: function(Feedback){
                  Feedback.TotalData=parseInt(Feedback.TotalData);
                  if(Feedback.Status==1){
                        for(i=0;i<Feedback.TotalData;i++){
                               $(eID).innerHTML+="<a href='"+_USER_INFO['url_pm']+"/show/"+Feedback.Data[i]['id']+".html'>"+Feedback.Data[i]['label']+"</a>";
                        }
                  } else {
                        $(eID).innerHTML=Feedback.Message;
                  }
            }
       });
       jSonRequest.send({
            'plugin':_USER_INFO['Layout'],
            'keyword':escape(toEntity(text)),
            'type': _type
       });
}
function toEntity(aa) {
      var bb = '';
      for(i=0; i<aa.length; i++){
            if(aa.charCodeAt(i)>127){
                  bb += '&#' + aa.charCodeAt(i) + ';';
            } else {
                  bb += aa.charAt(i);
            }
      }
      return bb;
}
function _data_filter(el){
      var elAddress = window.document.location.toString();
      
      elAddress = elAddress.replace(/show=\d+/g,"");
      elAddress = elAddress.replace(/order=\w+/g,"");
      elAddress = elAddress.replace(/by=\w+/g,"");
      elAddress = elAddress.replace(/&+/g,"&");
      elAddress = elAddress.replace(/(&|\?)$/g,"");
      
      if(elAddress.indexOf('?') < 0){
            elAddress+="?";      
      }

      elAddress+="&show="+formValue(el.show);
      elAddress+="&order="+formValue(el.order);
      elAddress+="&by="+((el.by.options.selectedIndex==1) ? 'desc' : 'asc');
     
      document.location.href=elAddress;
      return false;
}
function submit_confirm(text){
    if (document.all){
        if (confirm(text)){
            return true;
        }
    }
    else{
        if (window.confirm(text)){
            return true;
        }
    }
    return false;
}
function _confirm_redirect(url){
      if(submit_confirm("Are you sure you want to perform this action")){
            document.location.href=url;
      }
}
function formSet(id,value){
      if(typeof(id) == "string"){
            var e = getObject(id);      
      } else {
            var e = id;     
      }
      if(e.type=='select-one' || e.type=='radio' || e.type=='checkbox'){
            for (i = 0;i < e.length;i++){
                  if(e.type=='select-one' && e.options[i].value==value){
                        e.options[e.selectedIndex].value=value; 
                        e.value=value;
                        e.selectedIndex=i;
                  } else {
                        if(e[i].value==value){
                              e[i].checked=true;      
                        }           
                  }
            }        
      }
}
function formValue(id){
      if(typeof(id) == "string"){
            var e = $(id);      
      } else {
            var e = id;     
      }
      if(e.type=='checkbox' || e.type=='radio'){
            for (var i = 0; i < e.length; i++){
                  if (e[i].selectedIndex){
                        return e[i].value;
                  }
            }
      }
      else if(e.type=='select-one'){
            return e.options[e.selectedIndex].value;
      }
      else if(e.value){
            return e.value;     
      }
      return null;
}
var _CommentCache=new Array();
function quick_edit_cancel(id){
      getObject('CommentText_'+id).innerHTML = _CommentCache[id];
      _CommentCache[id] = null;
}
function quick_edit(id){
      if(_CommentCache[id]!=null){
            return;
      }
   // Trim the comments
      _CommentCache[id]=getObject('CommentText_'+id).innerHTML;
      _CommentCache[id]=_CommentCache[id].replace(/^\s*/,"");
      _CommentCache[id]=_CommentCache[id].replace(/\s*$/,"");

      HTMLBuffers = "<form action='"+_USER_INFO['url_pm']+"/index.php' method='POST'>";
      HTMLBuffers+= "<input type='hidden' name='pm' value='"+_USER_INFO['Layout']+"'/>";
      HTMLBuffers+= "<input type='hidden' name='cmd' value='comments'/>";
      HTMLBuffers+= "<input type='hidden' name='act' value='update'/>";
      HTMLBuffers+= "<input type='hidden' name='id' value='"+id+"'/>";
      HTMLBuffers+= "<textarea name='comment' style='width: 98%' onkeyup='initTyper(this);'>"+toEntity(_CommentCache[id])+"</textarea><br/>";
      HTMLBuffers+= "   <input type='submit' name='btnSubmit' value='Save Changes' />";
      HTMLBuffers+= "   <input type='reset' name='btnReset' value='Reset' />";
      HTMLBuffers+= "   <input type='button' onClick=\"quick_edit_cancel('"+id+"')\" name='btnReset' value='Close' />";
      HTMLBuffers+= "</form>";
      getObject('CommentText_'+id).innerHTML=HTMLBuffers;
}
function getQuery(variable) {
      var query = window.location.search.substring(1);
      var vars = query.split("&");
      for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) return pair[1];
      }
}
function popup(url, name, width, height,  scroll, resize){
      if (name == 'ImgPreview'){
            // url        = url_root + '/index.php?m=common&pm=img_preview&url=' + escape(url);
            height = parseInt(height) + 34;
            width  = parseInt(width) + 29;
      }
      if (scroll == ''){
            scroll = 'no';
      }
      if (resize == ''){
            resize = 'no';
      }
      if (width > screen.width)   width = screen.width;
      if (height > screen.height) height = screen.height;
      var winl = (screen.width) ? (screen.width - width) / 2 : 0;
      var wint = (screen.height) ? (screen.height - height) / 2 : 0;
      window.open(url,
                name,
                'width=' + width                     + ',height=' + height
                         + ',status=no,menubar=no,scrollbars=' + scroll     + ',toolbar=no,location=no,directories=no,resizable='
                         + resize                    + ',top='    + wint
                         + ',left='                  + winl);
}
function ProcessData(url,param,divID){
      var divContents = $(divID).innerHTML;
      var divVariables = divContents.match(/\$(\w+)/g);
      var i,j,buffers,tmp;
      $(divID).innerHTML="";
      var jSonRequest = new Json.Remote(url, {
          onComplete: function(Feedback){
              Feedback.TotalData=parseInt(Feedback.TotalData);
              for(i=0; i<Feedback.TotalData;i++){
                  buffers = divContents;
                  for(j=0;j<divVariables.length;j++){
                        tmp = divVariables[j].replace("$","");
                        if(typeof(Feedback.Data[i][tmp]) !='undefined'){
                            buffers = buffers.replace(divVariables[j],Feedback.Data[i][tmp]);
                        }
                  }
                  $(divID).innerHTML+=buffers;
              }
          }
      }).send(param);
}
function ProcessCategory(path){
       // JSON Root Category Display
       // Programmed by Kenny Ngo
       var _currentDate = new Date();
       var now = new Date();
       var _currentTime = Math.round(_currentDate.getTime()/1000);
       var _flag;

       var jSonRequest = new Json.Remote(_USER_INFO['EnvRoot']+'/remote/category.php', {
            onComplete: function(Feedback){
                  if(Feedback.Status!=1) { alert(Feedback.Message); return; }
                  Feedback.CatInfo['url'] = _USER_INFO['url_pm']+'/browse/'+Feedback.CatInfo['cat_id']+'.html';
                  $('RootShowall').innerHTML = "<a href='"+Feedback.CatInfo['url']+"' class='showAll'>Show all "+Feedback.CatInfo['label']+"</a>";
                  var buffers = '';
                  for(i=0;i<Feedback.TotalData;i++){
                        Feedback.Data[i]['path'] = Feedback.Data[i]['path'].replace(/\+$/g,'');
				Feedback.Data[i]['label'] += " <sup>" + Feedback.Data[i]['datasize'] + "</sup>";
				Feedback.Data[i]['url'] = _USER_INFO['url_pm']+'/browse/'+Feedback.Data[i]['cat_id']+'.html';
                        // Check if there is new
                        _flag ='icn_bullet';
                        if((_currentTime - Feedback.Data[i]['lupdate']) < 2*24*3600){
                              _flag+='_new';
                        }
                        if(Feedback.Data[i]['hasChild']==1){
                              _flag+='_active';
                        }
                        if(Feedback.Data[i]['hasChild']!=1){
                              buffers +="<a style=\"background: url('../global/images/"+_flag+".gif') no-repeat;\" href='"+Feedback.Data[i]['url']+"'>"+Feedback.Data[i]['label']+"</a>";
                        } else {
                              buffers +="<a style=\"background: url('../global/images/"+_flag+".gif') no-repeat;\" href=\"javascript:ProcessCategory('"+Feedback.Data[i]['path']+"');\">"+Feedback.Data[i]['label']+"</a>";
                        }
                  }
                  $("RootCategory").innerHTML = buffers;
            }
      }).send({
      	"path":path,
      	"url_base":_USER_INFO['url_pm']
      });
      path = path.replace(/\+\d+$/g,'');

      if(path==''){
            $("RootNavi").innerHTML="Channels";
      } else {
            $("RootNavi").innerHTML="< <a href=\"javascript:ProcessCategory('"+path+"');\">Go back</a>";
      }
}
