        // total number of rows used for calculation
        var rows_number = 24;
		
		// scv generation script URL
		var csv_url = '';
        
        function check_validity(field_name, rownum) {
            
			if(document.all[field_name+rownum].value == "") return;

			// check input data validity (numeric values)
            if((new Number(document.all[field_name+rownum].value)).toString() == "NaN") {
                alert("Invalid number format");
                document.all[field_name+rownum].value = (field_name=='takeout' ? "100":"");
				document.all[field_name+rownum].focus();
				return;
            }

            // check input data validity (positive values only)
            if(document.all[field_name+rownum].value <= 0) {
                alert("Invalid field value");
                document.all[field_name+rownum].value = (field_name=='takeout' ? "100":"");
				document.all[field_name+rownum].focus();
				return;
            }
		}

		function reset_data() { 
            for(var i=1; i<=rows_number; i++) {
                document.all["ownodds"+i].value = "";
                document.all["compodds"+i].value = "";
            }
            document.all["takeout"].value = "100";
        }

		function round_value(val) {
			if(Math.round(val) == Math.ceil(val)) return Math.round(val);
			else return Math.floor(val) + 0.5;
		}

		function calculate(rownum) {
        
            clear_results();

			// check input data
			for(var i=1; i<=rows_number; i++) 
				if(document.all["ownodds"+i].value == '' && document.all["compodds"+i].value != '') {
					alert("Own Odds field is empty in row: "+i);
					return;
				}
            
            var result_page = '<form name="data" action="'+csv_url+'" method="post" enctype="multipart/form-data">';
			result_page += '<table width="100%" cellspacing="1" cellpadding="2" class="TrGameEven" border="0" align="center">';
			result_page += '<tr class="sports-betting-h2">';
			result_page += '<td colspan="7" align="center">Quinella Odds (Results)</td>';
			result_page += '</tr>';
			result_page += '<!-- Top row & buttons -->';
			result_page += '<tr>';
			result_page += '<td colspan="6">';
			result_page += '<b>Date:</b>&nbsp;<input type="text" name="race_date" value="" maxlength="10" class="input2" text-align:left;" readonly>&nbsp;&nbsp;';
			result_page += '<b>Race Code:</b>&nbsp;<input type="text" name="race_code" value="" maxlength="10" class="input2" text-align:left;" readonly>';
			result_page += '<input type="hidden" name="race_takeout" value="">';
			result_page += '</td>';
			result_page += '<td class="table_cell" align="right"><!--<input type="submit" value="Save" style="width:100"></td>-->';
			result_page += '</tr>';
			result_page += '<tr>';
			result_page += '<td class="sports-betting-h3" align="center" width="10%">Bet No.</td>';
			result_page += '<td class="sports-betting-h3" align="center" width="15%">TAB No.<br>Combination</td>';
			result_page += '<td class="sports-betting-h3" align="center" width="15%">Rated Odds</td>';
			result_page += '<td class="sports-betting-h3" align="center" width="15%">Bet to return<br>($'+parseInt(document.all["takeout"].value)+')</td>';
			result_page += '<td class="sports-betting-h3" align="center" width="15%">Comparative Odds</td>';
			result_page += '<td class="sports-betting-h3" align="center" width="15%">Bet to return<br>($'+parseInt(document.all["takeout"].value)+')</td>';
			result_page += '<td class="sports-betting-h3" align="center" width="15%">Overlay, %</td>';
			result_page += '</tr>';
			
			// results generation
			var total_ooret = 0;
			var total_coret = 0;
			var cur_bet = 1;

			for(var i=1; i<=rows_number; i++) 
			for(var j=i+1; j<=rows_number; j++)
			if(document.all["ownodds"+i].value != '' && 
			   document.all["ownodds"+j].value != '' ) {

				var oo_i = parseInt(document.all["ownodds"+i].value);
				var oo_j = parseInt(document.all["ownodds"+j].value);
				
				var co_i = parseInt(document.all["compodds"+i].value == '' ? 0 : document.all["compodds"+i].value);
				var co_j = parseInt(document.all["compodds"+j].value == '' ? 0 : document.all["compodds"+j].value);
				
				var oo = (oo_i*(oo_j-1)+oo_j*(oo_i-1))/4;
				var co = (co_i==0 || co_j==0) ? 0 : (co_i*(co_j-1)+co_j*(co_i-1))/4;
				
				if((co <= oo) && (document.all["value_bets_only"].checked)) continue;

				if(document.getElementById("opt_own_odds").checked) {
					
					if((document.all["minodds"].value != '') && (round_value(Math.round(oo*100)/100) < parseInt(document.all["minodds"].value)))
						continue;
					if((document.all["maxodds"].value != '') && (round_value(Math.round(oo*100)/100) > parseInt(document.all["maxodds"].value)))
						continue;

				} else if(document.getElementById("opt_comp_odds").checked) {
				
					if((document.all["minodds"].value != '') && (round_value(Math.round(co*100)/100) < parseInt(document.all["minodds"].value)))
						continue;
					if((document.all["maxodds"].value != '') && (round_value(Math.round(co*100)/100) > parseInt(document.all["maxodds"].value)))
						continue;
				}

				var oor = parseInt(document.all["takeout"].value)/oo;
				var cor = (co == 0) ? 0 : parseInt(document.all["takeout"].value)/co;

				var co_disp = round_value(Math.round(co*100)/100);
				var oo_disp = round_value(Math.round(oo*100)/100);
				var overlay = Math.round(co_disp/oo_disp*10000-10000)/100;

				total_ooret += oor;
				total_coret += cor;

				result_page += '<tr class="TrGameEven">';
				result_page += '<td><input type="text" name="bet_'+i+'_'+j+'" value="'+(cur_bet++)+'" maxlength="8" style="width:100%; text-align:center;" class="input2" readonly></td>';
				result_page += '<td><input type="text" name="comb_'+i+'_'+j+'" value="'+i+'-'+j+'" maxlength="8" style="width:100%; text-align:center;" class="input2" readonly></td>';
				result_page += '<td><input type="text" name="ownodds_'+i+'_'+j+'" value="'+oo_disp+'" maxlength="8" style="width:100%; text-align:left;" class="input2" readonly></td>';
				result_page += '<td><input type="text" name="ooret_'+i+'_'+j+'" value="'+round_value(Math.round(oor*100)/100)+'" maxlength="8" style="width:100%; text-align:left;" class="input2" readonly></td>';
				result_page += '<td><input type="text" name="compodds_'+i+'_'+j+'" value="'+((co_disp==0 || co_disp=='') ? '-' : co_disp)+'" maxlength="8" style="width:100%; text-align:left;" class="input2" readonly></td>';
				result_page += '<td><input type="text" name="coret_'+i+'_'+j+'" value="'+((cor==0) ? '-' : round_value(Math.round(cor*100)/100))+'" maxlength="8" style="width:100%; text-align:left;" class="input2" readonly></td>';
				result_page += '<td><input type="text" name="overlay_'+i+'_'+j+'" value="'+overlay+'" maxlength="8" style="width:100%; text-align:left;'+((overlay >= 0)?'color:green;':'color:red; font-weight:bold;')+'" class="input2" readonly></td>';
				result_page += '</tr>';
			}

			result_page += '<tr class="TrGameEven">';
			result_page += '<td>&nbsp;</td>';
			result_page += '<td>&nbsp;</td>';
			result_page += '<td>&nbsp;</td>';
			result_page += '<td><input type="text" name="ooret_total" value="" maxlength="8" style="width:100%; text-align:left;" class="input2" readonly></td>';
			result_page += '<td>&nbsp;</td>';
			result_page += '<td><input type="text" name="coret_total" value="" maxlength="8" style="width:100%; text-align:left;" class="input2" readonly></td>';
			result_page += '<td>&nbsp;</td>';
			result_page += '</tr>';
			result_page += '</table>';
			result_page += '</form>';
        
			document.all['results'].innerHTML = result_page;
			//alert(document.all['results'].innerHTML);

			// save date/code/takeout values
			document.all['results'].all['race_date'].value = document.all['rdate'].value;
			document.all['results'].all['race_code'].value = document.all['rcode'].value;
			document.all['results'].all['race_takeout'].value = document.all['takeout'].value;
			
			// save sum values
			document.all['results'].all['ooret_total'].value = round_value(Math.round(total_ooret*100)/100);
			document.all['results'].all['coret_total'].value = round_value(Math.round(total_coret*100)/100);
			
			// show results
			document.all['results'].style.visibility = 'visible';
        }
        
        function clear_results() {
            document.all['results'].innerHTML = "";
            document.all['results'].style.visibility = 'hidden';
        }
