// writed by zhu.junli

var getRadioValue = function(name){
    var objs = document.getElementsByName(name);
    for(var i=0; i<objs.length; i++){
        if(objs[i].checked){
            return objs[i].value;
        }
    }
}
var getCheckboxValue = function(name){
    var objs = document.getElementsByName(name);
    var values=[];
    for(var i=0; i<objs.length; i++){
        if(objs[i].checked){
            values.push(objs[i].value);
        }
    }
    return values;
}

var setCheckboxValue = function(name, values){
    var objs = document.getElementsByName(name);
    for(var i=0; i<objs.length; i++){
        if(indexInArray.call(values, objs[i].value)>=0){
            objs[i].checked = true;
        }else{
            objs[i].checked = false;
        }
    }
}
var strTrim = function(str){
    return str.replace(/^[ กก]*/,'').replace(/[ กก]*$/,'');
}
// get the character length of a string 
var getCharLength = function(str){
    var len = 0;
    for(var i=0; i < str.length; i++){
        if(str.charCodeAt(i)<256){
            len++;
        }else{
            len += 2;
        }
    }
    return len;
}

// page division 
// totalPage:   total pages number
// curPage:     current page
// NPP:         nuber of page code displayed per page
// flag:        division form
// format:  1  2  3  4  5  6  7  8  ... 20      flag=0  (20, 1, 10, 0)
//          1 ... 9  10 11 12 13 14 ... 20      flag=0  (20, 9, 10, 0)
//          1 ... 15 16 17 18 19 20             flag=0  (20, 15, 10, 0)
//          up 1 2 3 4 5  down                  flag=1  (20, 1, 5, 1)
//          up 6 7 8 9 10 down                  flag=1  (20, 6, 5, 1)
function getPageDivisionInfo(totalPage, curPage, NPP, flag){
    // NPP: number of pages displayed
    if(!flag) flag = 0;
    if(flag*1 == 0){ 
        var pages = [];
        if(totalPage*1 <= 0) return [];
        // if totalPage > 0
        var sp = 1;
        if(totalPage <= NPP){
            for(var i=1; i <= totalPage; i++) pages.push(i);
        }else{
            var maxMutiple = parseInt((totalPage-5)/(NPP-4));
            if(maxMutiple<0) maxMutiple=0;
            var mutiple = parseInt((curPage-3)/(NPP-4));
            var postEllipsis = (mutiple+1)*(NPP-4)+3;
            var preEllipsis = (mutiple-1)*(NPP-4)+3;
            if(mutiple<0) mutiple=0;
            if(mutiple>maxMutiple) mutiple=maxMutiple;
            var curEllipsis = mutiple*(NPP-4)+3;
            if(mutiple == 0){
                for(var i=sp; i<=sp+NPP-3; i++) pages.push(i);
                pages.push('down'+postEllipsis);
                pages.push(totalPage);
            }else if(mutiple == maxMutiple){
                pages.push(sp);
                pages.push('up'+preEllipsis);
                for(var i=curEllipsis; i<=totalPage; i++) pages.push(i);
            }else{
                pages.push(sp);
                pages.push('up'+preEllipsis);
                for(var i=curEllipsis; i<=curEllipsis+NPP-5; i++) pages.push(i);
                pages.push('down'+postEllipsis);
                pages.push(totalPage);
            }
        }
        return pages;
    }else if(flag*1 == 1){
        var pages = [];
        if(curPage > totalPage) curPage = totalPage;
        if(totalPage*1 <= 0) return [];
        var mutiple = parseInt((curPage-1)/NPP);
        var curEllipsis = mutiple*NPP+1;
        var postEllipsis = (mutiple+1)*NPP+1;
        var preEllipsis = (mutiple-1)*NPP+1;
        pages.push('up'+preEllipsis);
        for(var i=0; i<NPP; i++){
            if(curEllipsis+i <= totalPage){
                pages.push(curEllipsis+i);
            }else{
                break;
            }
        }
        pages.push('down'+postEllipsis);
        return pages;
    }
}

function indexInArray(value){
    for(var i=0; i<this.length; i++){
        if(this[i] == value)
            return i;
    }
    return -1;
}

//manage pop up
function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_showHideLayers() { //v3.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }
}
//manage pop up end
