<script language="javascript" src="../jscript/calendar.js"></script>


 
<?php


$table_name = $_SESSION['db_prefix']."_customer";
    $position_query = "SELECT cust_id, comp_name FROM $table_name ORDER BY comp_name ASC";
	$arrId3 = array ();
	$arrName3 = array ();
	$position_result = execsql($position_query);
	if ($position_result!="") {
	$position_num = mysql_num_rows($position_result);
	$k = 0;
	while ($k < $position_num) {
		$arrId3[$k] = mysql_result($position_result, $k, "cust_id");
		$arrName3[$k] = mysql_result($position_result, $k, "comp_name");
		$k++;
	}
	}
  
?>

<script language="javascript">
        function addRow(tableID) {
 
            var table = document.getElementById(tableID);
 
            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);
			var line_no = rowCount-1;
 
            var cell1 = row.insertCell(0);
            var element1 = document.createElement("input");
            element1.type = "checkbox";
            element1.name="line_checkbox"+line_no;
			element1.id="line_checkbox"+line_no;
			/*element1.checked="checked";*/
            cell1.appendChild(element1);
			
							
 
            var cell3 = row.insertCell(1);
            var element2 = document.createElement("select");
            element2.name = "comp_name_select"+line_no;
			element2.className = "select200";
			element2.value = "";
			element2.onchange = function(){document.getElementById("line_checkbox"+line_no).checked = "checked";}
			cell3.appendChild(element2);
			
			
			var option1 = document.createElement("option");
   				 	option1.text = "";
    				option1.value = "";
					option1.selected = "selected";
					element2.add(option1);
					
			var option2 = document.createElement("option");		
					option2.text = ".";
    				option2.value = ".";
					element2.add(option2);
			<?php
    			for ($j = 0; $j < count($arrId3); $j++) { ?>
				
				var option<?php echo $j?> = document.createElement("option");
   				 	option<?php echo $j?>.text = "<?php echo $arrName3[$j]?>";
    				option<?php echo $j?>.value = "<?php echo  $arrId3[$j]?>";
				element2.add(option<?php echo $j?>);
			<?php	
			}
     		?>	
	  		
			
			var cell4 = row.insertCell(2);
            var element3 = document.createElement("input");
            element3.type = "text";
            element3.name = "ref_no_text"+line_no;
			element3.className = "text150";
			element3.onkeyup = function() {ToUpper(this)};
            cell4.appendChild(element3);
			
			var cell5 = row.insertCell(3);
			cell5.align='right';
			var element6 = document.createElement("input");
            element6.type = "text";
            element6.name = "amount_text"+line_no;
			element6.className = "text100";
			element6.onkeypress = function isNumberKey(evt)
       								{
          							var charCode = (evt.which) ? evt.which : event.keyCode
          							if (charCode != 46 && charCode > 31 
            						&& (charCode < 48 || charCode > 57))
             						return false;
 
          							return true;
       								}
									;
			element6.onchange = function() {this.value = this.value.replace (/(\...).*/, '$1')};
			element6.style = "text-align:right";		

            var element4 = document.createElement("input");
            element4.type = "hidden";
            element4.name = "line_num";
			element4.value = rowCount;
            						
			cell5.appendChild(element6);
            cell5.appendChild(element4);
			
			var element8 = document.createElement("script");
			element8.setAttribute('type', 'text/javascript');
			element8.innerHTML="var frmvalidator  = new Validator('form1');frmvalidator.EnableMsgsTogether();for (i = 0; i < 10; i++) {frmvalidator.addValidation('comp_name_select'+i,'req','- client on line '+(i+1)+'.');frmvalidator.addValidation('ref_no_text'+i,'req','- reference number on line '+(i+1)+'.');frmvalidator.addValidation('amount_text'+i,'req','- amount on line '+(i+1)+'.');}"; 
			document.getElementsByTagName('head').item(0).appendChild(element8);

			element1.onmouseup = function() {
				 if (document.getElementById('line_checkbox'+line_no).checked) {
                         element2.style.background = '#F0F0F0';
						 element3.style.background = '#F0F0F0';
						 element6.style.background = '#F0F0F0';
						 element2.selectedIndex = 1;
						 element3.value = '-----';
						 element6.value = '0';
						 element2.disabled = true;
						 element3.disabled = true;
						 element6.disabled = true;
						 
                } else {
                        element2.style.background = '#FFF';
						element3.style.background = '#FFF';
						element6.style.background = '#FFF';
						element2.selectedIndex = 0;
						element3.value = '';
						element6.value = '';
						element2.disabled = false;
						element3.disabled = false;
						element6.disabled = false;
						 
                }
				};
			
}

</script>



<?php include('../include/wait.php') ?>
</head>


<body onLoad="MM_showHideLayers('wait','','hide')" onFocus="MM_showHideLayers('wait','','hide');">

   
<form id="form1" method="post" action="qbillWizbase.php?p=3&s=2">
<div class="wizard_page_middle_frame">
<div class="wizard_frame">
  <table id="dataTable" width="611" border="1" style="border-collapse:collapse;">
    <?php
	  $table_name = $_SESSION['db_prefix']."_qbill_lines_temp";
				$bill_query = "SELECT * FROM $table_name WHERE user_name = '".$_SESSION['username']."' ";
				$bill_result = mySQL_query($bill_query);
				$bill_num = mysql_num_rows($bill_result);
				
				if ($bill_num!=0) {
				$cust_id = mysql_result($bill_result, 0, "cust_id");
				$reference = mysql_result($bill_result, 0, "reference");
				$amount = mysql_result($bill_result, 0, "amount");
				
				$table_name = $_SESSION['db_prefix']."_customer";
				$name_query = "SELECT comp_name FROM $table_name WHERE cust_id = '".$cust_id."' ";
				$name_result = mySQL_query($name_query);
				$comp_name = mysql_result($name_result, 0, "comp_name");
				
				} else {
				$cust_id = '';
				$comp_name = '';
				$reference = '';
				$amount = '';
				}
  ?>
    <thead>
      <tr>
        <td>&nbsp;</td>
        <td>Client</td>
        <td>Reference no</td>
        <td>Amount</td>
      </tr>
    </thead>
    <script>
			 function check_it() {
				 if (document.getElementById('line_checkbox0').checked) {
                         document.getElementById('reference_text0').style.background = '#F0F0F0';
						 document.getElementById('ref_no_text0').style.background = '#F0F0F0';
						 document.getElementById('amount_text0').style.background = '#F0F0F0';
						 document.getElementById('reference_text0').value = '-----';
						 document.getElementById('ref_no_text0').value = '-----';
						 document.getElementById('amount_text0').value = '0';
						 document.getElementById('reference_text0').disabled = true;
						 document.getElementById('ref_no_text0').disabled = true;
						 document.getElementById('amount_text0').disabled = true;
						 
                } else {
                        document.getElementById('reference_text0').style.background = '#FFF';
						 document.getElementById('ref_no_text0').style.background = '#FFF';
						 document.getElementById('amount_text0').style.background = '#FFF';
						 document.getElementById('reference_text0').value = '';
						 document.getElementById('ref_no_text0').value = '';
						 document.getElementById('amount_text0').value = '';
						 document.getElementById('reference_text0').disabled = false;
						 document.getElementById('ref_no_text0').disabled = false;
						 document.getElementById('amount_text0').disabled = false;
                }
				}
		     </script>
    <tr>
      <td width="20"><input type="checkbox" name="line_checkbox0" id="line_checkbox0" onMouseUp="check_it()" checked="checked"  /></td>
      <td width="214"><select name="comp_name_select0" class="select200"  >
        <option value="<?php echo $cust_id ?>" selected="selected"><?php echo $comp_name ?></option>
        <?php
        	for ($k = 0; $k < count($arrId3); $k++) {
    		echo "<option value=" . "\"" . $arrId3[$k] . "\"" . ">" . $arrName3[$k] . "</option>";
	}
?>
      </select></td>
      <td width="205"><input  name="ref_no_text0" type="text" class="text150" id="ref_no_text0" value="<?php echo $reference ?>" onKeyUp="ToUpper(this)"/></td>
      <td width="144" align="right"><input  name="amount_text0" type="text" class="text100" id="amount_text0" value="<?php if ($amount!=0) {echo number_format($amount, 2, '.', '');} ?>" onKeyPress="return isNumberKey(event)" onChange=" this.value = this.value.replace (/(\...).*/, '$1')" style="text-align:right"/>
        <input type="hidden" name="line_num" id="line_num" value="1" ></td>
      <!--<input name="textfield0" type="text" id="textfield0" size="5" value="1"></td>-->
    </tr>
    
    <?php
			  $i=1;
			  while ($i<$bill_num) { 
			    $cust_id = mysql_result($bill_result, $i, "cust_id");
				$reference = mysql_result($bill_result, $i, "reference");
				$amount = mysql_result($bill_result, $i, "amount");
				
				$cust_id = mysql_result($bill_result, $i, "cust_id");
			 	$table_name = $_SESSION['db_prefix']."_customer";
				$name_query = "SELECT comp_name FROM $table_name WHERE cust_id = '".$cust_id."' ";
				$name_result = mySQL_query($name_query);
				$comp_name = mysql_result($name_result, 0, "comp_name");?>
            
    <tr>
      <td width="20"><input type="checkbox" name="line_checkbox<?php echo $i ?>" id="line_checkbox<?php echo $i ?>"  <?php if ($comp_name!='') { ?> checked="checked" <?php } ?> onMouseUp="{
				 if (document.getElementById('line_checkbox<?php echo $i ?>').checked) {
                         document.getElementById('comp_name_select<?php echo $i ?>').style.background = '#F0F0F0';
						 document.getElementById('ref_no_text<?php echo $i ?>').style.background = '#F0F0F0';
						 document.getElementById('amount_text<?php echo $i ?>').style.background = '#F0F0F0';
						 document.getElementById('comp_name_select<?php echo $i ?>').selectedIndex = 0;
						 document.getElementById('ref_no_text<?php echo $i ?>').value = '-----';
						 document.getElementById('amount_text<?php echo $i ?>').value = '0';
						 document.getElementById('comp_name_select<?php echo $i ?>').disabled = true;
						 document.getElementById('ref_no_text<?php echo $i ?>').disabled = true;
						 document.getElementById('amount_text<?php echo $i ?>').disabled = true;
                } else {
                        document.getElementById('comp_name_select<?php echo $i ?>').style.background = '#FFF';
						 document.getElementById('ref_no_text<?php echo $i ?>').style.background = '#FFF';
						 document.getElementById('amount_text<?php echo $i ?>').style.background = '#FFF';
						 document.getElementById('comp_name_select<?php echo $i ?>').selectedIndex = 1;
						 document.getElementById('ref_no_text<?php echo $i ?>').value = '';
						 document.getElementById('amount_text<?php echo $i ?>').value = '';
						 document.getElementById('comp_name_select<?php echo $i ?>').disabled = false;
						 document.getElementById('ref_no_text<?php echo $i ?>').disabled = false;
						 document.getElementById('amount_text<?php echo $i ?>').disabled = false;
                }
				}
                "/></td>
      <td width="214"><select name="comp_name_select<?php echo $i ?>"  id="comp_name_select<?php echo $i ?>" class="select200" >
        <option value=".">.</option>
        <option> </option>
        <option value="<?php echo $cust_id ?>" selected="selected"><?php echo $comp_name?></option>
        <?php
        	for ($k = 0; $k < count($arrId3); $k++) {
    		echo "<option value=" . "\"" . $arrId3[$k] . "\"" . ">" . $arrName3[$k] . "</option>";
	}
?>
      </select></td>
      <td width="205"><input  name="ref_no_text<?php echo $i ?>" type="text" class="text150" id="ref_no_text<?php echo $i ?>" value="<?php echo $reference ?>"/></td>
      <td width="144" align="right"><input  name="amount_text<?php echo $i ?>" type="text" class="text100" id="amount_text<?php echo $i ?>" value="<?php echo number_format($amount, 2, '.', '') ?>" onKeyPress="return isNumberKey(event)" onChange=" this.value = this.value.replace (/(\...).*/, '$1')" style="text-align:right"/>        <input type="hidden" name="line_num" id="line_num" value="<?php echo $bill_num ?>"></td>
    </tr>
    <?php
		$i++;
		}
		?>
  </table>
  <br>
  <input type="button" class="imagebutton" onClick="addRow('dataTable')" value="Add client" />
</div>
<div class="wizard_spacer_frame" style="height:8px;"></div>
<div class="wizard_frame">            
              <table width="675" height="50" align="center" id="table_button_bar">
                <tr>
                  <td width="117"><input type="button" name="lastprev_button" class="imagebutton" value="Prev" onClick="needToConfirm = false; MM_showHideLayers('wait','','show'); MM_goToURL('parent','qbillWizbase.php?s=&p=1');return document.MM_returnValue" /></td>
                  <td width="115">&nbsp;</td>
                  <td width="99">
                    <input name="p" type="hidden" id="p" value="2" />
                    <input name="s" type="hidden" id="s" value="2" />
                  </td>
                  <td width="125" align="right"><input name="cancelbutton" type="button" class="imagebutton" id="cancelbutton" onClick="cancel_confirmation(1, '../sales/sales_main.php?date=');" value="Cancel" /></td>
                  <td width="110" align="right"><input type="submit" name="next_button" class="imagebutton" value="Next" onClick="needToConfirm = false; MM_showHideLayers('wait','','show');" /></td>
                </tr>
              </table>

<!--form validation-->
<!--form validation-->
  <script language="JavaScript" type="text/javascript" xml:space="preserve">
  var frmvalidator  = new Validator("form1");
  frmvalidator.EnableMsgsTogether();
  //frmvalidator.addValidation("comp_name_select0","req","- client on line 1.");
//  frmvalidator.addValidation("ref_no_text0","req","- reference number  on line 1.");
//  frmvalidator.addValidation("amount_text0","req","- amount  on line 1.");
  
  for (i = 0; i < 10; i++) {
	frmvalidator.addValidation("comp_name_select"+i,"req","- client on line "+(i+1)+".");
  	frmvalidator.addValidation("ref_no_text"+i,"req","- reference number  on line "+(i+1)+".");
  	frmvalidator.addValidation("amount_text"+i,"req","- amount on line "+(i+1)+".");
}
  </script>
</div>
</div>
</form>
</body>