/*

    Author:		Robbe D. Morris
	Date:		October 20, 2002
	URL:			http://www.robbemorris.com

*/
 
   
  
 function GridDrawTable(oGridArrays)
  {

    var sH='';     
    var lArrayLength=0;
    var lLoop=0;
	var oGrid = document.getElementById('GridTable');
	var oInp = new Array();
 
   try
	  {   
             lArrayLength = oGridArrays.length;

    	     if (lArrayLength < 1) { 
    	     	return false; 
    	     }
			 
             oGridArrays = GridSort(oGridArrays);
             lArrayLength = oGridArrays.length;

    	     if (lArrayLength < 1) { 
    	     	return false; 
    	     }

             oInp[oInp.length] = ('<table border=0 align=center width="100%" cellpadding=6 cellspacing=0 class=GridHeader>');
         
             for (lLoop=0; lLoop<lArrayLength; lLoop++)
            { 
               if (lLoop == 0) {   oInp[oInp.length] = (GridWriteHeader(oGridArrays[lLoop])); }
               else {  oInp[oInp.length] = (GridWriteRow(oGridArrays[lLoop])); }
             }

             oInp[oInp.length] = ('</table>');
      
                         for(var i=0;i<oInp.length;i++){ 
                            sH += oInp[i]+" "; 
                         }
             oGrid.innerHTML=sH;
             GridArrays = oGridArrays;

	      }
	       catch (exception) 
		  { 
		     if (exception.description == null) { alert("Grid Draw Error: " + exception.message); }  
		     else {  alert("Grid Draw Error: " + exception.description); }
		  }

  }



  function GridWriteHeader(sArray)
  {
        var sH='';
		var lArrayLength=0;
		var lLoop=0;
        var oInp = new Array();

        lArrayLength = sArray.length;

    	if (lArrayLength < 1) { return false; }
 
        oInp[oInp.length] = ('<tr>');
         
        for (lLoop=0; lLoop<lArrayLength; lLoop++)
       {
			 oInp[oInp.length] = ("<td align=center class=GridHeaderLink nowrap><a href=# onclick=\"GridSortSetColumn(" + lLoop + ");\" class=GridHeaderLink>" + sArray[lLoop] + "</a></td>");
	   }

        oInp[oInp.length] = ("</tr>");
                         for(var i=0;i<oInp.length;i++){ 
                           sH += oInp[i]+" "; 
                         }
         return sH;
  }


  function GridWriteRow(sArray)
  {
       
		var lArrayLength=0;
		var lLoop=0;
        var sH='';
        var oInp = new Array();

		lArrayLength = sArray.length;

    	if (lArrayLength < 1) { return false; }

        oInp[oInp.length] = ("<tr>");

        for (lLoop=0; lLoop<lArrayLength; lLoop++)
       {
           oGrid = sArray[lLoop];

		   switch (oGrid.Type)
		   {
                 case idxGrid_CheckBox:
 
				         if (oGrid.SortVal==1) {   oGrid.HTML = GridReplace(oGrid.HTML,"value=1"," value=1 checked "); }
						 else {  oGrid.HTML = GridReplace(oGrid.HTML,"checked",""); }

					     break;

                 case idxGrid_DropDown:

                         oGrid.HTML = GridReplace(oGrid.HTML,"selected","");
					     oGrid.HTML = GridReplace(oGrid.HTML,"value='" + oGrid.SortVal + "'","value='" + oGrid.SortVal + "' selected ");
                               
						 break;

                 case idxGrid_Input:

                         oGrid.HTML = GridWriteInputBox(oGrid.Column,oGrid.ID,oGrid.Name,oGrid.SortVal);
                       
						 break;
			  

		   }
            oInp[oInp.length] = ("<td valign=middle align=" + oGrid.alignment + "  class=GridCell nowrap>" + oGrid.HTML + "</td>");
	   }
	      oInp[oInp.length] = ("</tr>");
                         for(var i=0;i<oInp.length;i++){ 
                            sH += oInp[i]+" "; 
                         }
		 
         return sH;
  }



 function GridSetVal(nColumn,sID,oFormElement)
 {
        
	    var oRow;
		var oCell;
		var lIdx;
	 
        try
	    {
             lArrayLength = GridArrays.length;
     
    	     if (lArrayLength < 1) { return false;}
 
             for (lLoop=0; lLoop<lArrayLength; lLoop++)
            { 
               
			   if (lLoop != 0)
			    {
				    
					oRow = GridArrays[lLoop];
				    oCell = oRow[nColumn];
                
                    if (oCell.ID == sID)
					  {
  
  								  
		 
						switch (oCell.Type)
		                   {
 
							  case idxGrid_CheckBox:
                                     
							          oCell.SortVal='0';
				                       if (document.getElementById(oFormElement.name).checked == true) {  oCell.SortVal='1'; }

					                    break;

						     case idxGrid_DropDown:
                                     
									  	 lIdx = document.getElementById(oFormElement.name).selectedIndex;
					                     oCell.SortVal = document.getElementById(oFormElement.name).options[lIdx].value;
									    
					                    break;

							 case idxGrid_Input:
                                   
					                     oCell.SortVal = document.getElementById(oFormElement.name).value;
					 
					                    break;

		                     }

                       
					     
                         oRow[nColumn] = oCell;
                         GridArrays[lLoop] = oRow;
						 return true;
					  }
			
				 }      
             }

		}
	 	   catch (exception) 
		  { 
		     if (exception.description == null) { alert("GridSetVal: " + exception.message); }  
		     else {  alert("GridSetVal: " + exception.description); }
		  }

 }

 
