var pagesize = 10; 
var cur_page = 0; 

function showTitle(){
	//var strTitle = '<tr>'+
        //'<td colspan='+(arrayTitle.length+2)+' height=40 valign=top align=right>'+
        //  '<input class=button type=button value=" Prev " onclick=lastPage()>&nbsp;&nbsp;&nbsp;&nbsp;'+
        //  '<input class=button type=button value=" Next " onclick=nextPage()>&nbsp;&nbsp;&nbsp;&nbsp;'+
        //'</td>'+
        //'</tr>';
        var strTitle = '';
	strTitle += '<tr><th>&nbsp;</th><th align=center width=10%>Index</th>';
    
	for(var i = 0; i < arrayTitle.length; i++)
		strTitle += '<th align=center width='+arrayTitle[i][2]+'%>'+arrayTitle[i][0]+'</th>';

	strTitle += '</tr>';
	return strTitle;
}

function addRowInfo(idx){
	var checkit = '';
	if(idx % pagesize == 0) checkit=' checked';
    var strRowInfo = '<tr class=row'+(idx%2)+'>'+
    		'<td width=3% align=center><input type=radio name=rdCheck value='+idx+' onClick="showRow('+idx+')"'+checkit+'>'+
    		'</td>'+
    		'<td width=2% align=center>'+(idx+1)+'</td>';
    var i;
    
    for(i = 0; i < arrayTitle.length; i++)
        strRowInfo += '<td  align=center>'+showColumn(idx, arrayTitle[i][1])+'</td>';
        
	strRowInfo += '</tr>';
	return strRowInfo;
}

function showButtons(){
	strButtons = '<tr>'+
        '<th colspan='+(arrayTitle.length+2)+' align=right>'+
          '<input class=button type=button value="Add" onclick=addRow()>&nbsp;'+
          '<input class=button type=button value="Edit" onclick=editRow()>&nbsp;'+
          '<input class=button type=button value="Delete" onclick=deleteRow()>&nbsp;'+
          '<input class=button type=button value="Prev" onclick=lastPage()>&nbsp;'+
          '<input class=button type=button value="Next" onclick=nextPage()>&nbsp;'
        '</th>'+
      '</tr>';
	return strButtons;
}

function showTable(){
    var innerString = '<table cellpadding="4" cellspacing="0" border="0" width="100%" class="adminlist">'+
    		showTitle();
    var i;
    
    for(i = cur_page*pagesize;i<maxcount && i<(cur_page+1)*pagesize; i++)
		innerString += addRowInfo(i);
	
	innerString += showButtons();
    div.innerHTML = innerString+"</table>";
    showRow(getCheckRadio());
}


function addRow(){

	arrayInfo[maxcount] = new Array();
	if(!modifyRow(maxcount)) return;
	
	if (maxcount < (cur_page+1)*pagesize){
		maxcount++;
		showTable();
	}
	else{
	    cur_page = Math.floor(maxcount/pagesize);
		maxcount++;
	    showTable();
	}
}

function editRow(){
    var idx = getCheckRadio();
    if(idx < 0) return;
    if(!modifyRow(idx)) return;
    showTable();	
}

function deleteRow(){
    var idx = getCheckRadio();
    if(idx < 0) return;

	var i, j;
	for(i = idx++; i < maxcount-1; i++,idx++){
		for(j = 0; j < arrayInfo[0].length; j++)
	    	arrayInfo[i][j] = arrayInfo[idx][j];
	}
	
	maxcount--;
	if(maxcount <= cur_page*pagesize && cur_page>0) cur_page--;
    showTable();
}

function nextPage(){
   if (maxcount > (cur_page+1)*pagesize){
       cur_page++;
	   showTable();
   }
}

function lastPage(){
   if((cur_page-1)>=0){
	   cur_page--;
	   showTable();
   }
}

function getCheckRadio(){
	if(!f.rdCheck) return -1;
	if(!f.rdCheck[0]) return cur_page*pagesize;
	
	var elmt = f.rdCheck[0];
	for (var i=0; i<elmt.form.length; i++ )
    	if ( elmt.name == elmt.form[i].name && (true == elmt.form[i].checked ) )
    return elmt.form[i].value;
}

