
/*

        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.push('<table border=0 align=left width=100% cellpadding=1 cellspacing=1 style=\"border-collapse: collapse;\" class=GridHeader>');
         
             for (lLoop=0; lLoop<lArrayLength; lLoop++)
            { 
               if (lLoop == 0) {   oInp.push(GridWriteHeader(oGridArrays[lLoop])); }
               else {  oInp.push(GridWriteRow(oGridArrays[lLoop])); }
             }

             oInp.push('</table>');
      
	          sH = oInp.join(' ');
             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.push('<tr>');
         
        for (lLoop=0; lLoop<lArrayLength; lLoop++)
       {
			 oInp.push("<td align=left class=GridHeaderLink nowrap><a href=# onclick=\"GridSortSetColumn(" + lLoop + ");\" class=GridHeaderLink>" + sArray[lLoop] + "</a></td>");
	   }

        oInp.push("</tr>");
		sH = oInp.join(' ');
	   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.push("<tr bgColor=\"#ffffff\">");
     
        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.push("<td valign=middle align=left class=GridCell nowrap>" + oGrid.HTML + "</td>");
	   }
	      oInp.push("</tr>");
		  sH = oInp.join(' ');
		 
         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); }
		  }

 }

 
