// 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["ratedodds"+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["ratedodds"+i].value == '' && document.all["compodds"+i].value != '') {
					alert("Rated 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="3" cellpadding="2" border="0" class="TrGameEven">';
			result_page += '<tr class="sports-betting-h2">';
			result_page += '<td colspan="6" align="center">Trifecta Odds (Results)</td>';
			result_page += '</tr>';
			result_page += '<!-- Top row & buttons -->';
			result_page += '<tr>';
			result_page += '<td colspan="5">';
			result_page += '<b>Date:</b>&nbsp;<input type="text" name="race_date" value="" maxlength="10" style="width:100; text-align:left;" class="input2" readonly>&nbsp;&nbsp;';
			result_page += '<b>Race Code:</b>&nbsp;<input type="text" name="race_code" value="" maxlength="10" style="width:100; text-align:left;" class="input2" readonly>';
			result_page += '<input type="hidden" name="race_takeout" value="">';
			result_page += '</td>';
			result_page += '<td 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="18%">Trifecta Comb.</td>';
			result_page += '<td class="sports-betting-h3" align="center" width="18%">Rated Odds</td>';
			result_page += '<td class="sports-betting-h3" align="center" width="18%">Comparative Odds</td>';
			result_page += '<td class="sports-betting-h3" align="center" width="18%">Outlay, $</td>';
			result_page += '<td class="sports-betting-h3" align="center" width="18%">Overlay, %</td>';
			result_page += '</tr>';
			
			// results generation
			var total_roret = 0;
			var cur_bet = 1;

			for(var i=1; i<=rows_number; i++) 
			for(var j=1; j<=rows_number; j++)
			for(var k=1; k<=rows_number; k++)
			if((i != j) && (j != k) && (k != i) &&
			   document.all["standout"+i+"_1"].checked && 
			   document.all["standout"+j+"_2"].checked && 
			   document.all["standout"+k+"_3"].checked && 
			   document.all["ratedodds"+i].value != '' && 
			   document.all["ratedodds"+j].value != '' && 
			   document.all["ratedodds"+k].value != '' ) {

				var ro_i = parseInt(document.all["ratedodds"+i].value) - 1;
				var ro_j = parseInt(document.all["ratedodds"+j].value) - 1;
				var ro_k = parseInt(document.all["ratedodds"+k].value) - 1;
				
				var co_i = parseInt(document.all["compodds"+i].value == '' ? 0 : (document.all["compodds"+i].value - 1));
				var co_j = parseInt(document.all["compodds"+j].value == '' ? 0 : (document.all["compodds"+j].value - 1));
				var co_k = parseInt(document.all["compodds"+k].value == '' ? 0 : (document.all["compodds"+k].value - 1));
				
				var ro = (ro_i * (ro_i * ro_j - 1) * (ro_k + 1)) / (ro_i + 1);
				var co = (co_i==0 || co_j==0 || co_k==0) ? 0 : (co_i * (co_i * co_j - 1) * (co_k + 1)) / (co_i + 1);
				
				if((co <= ro) && (document.all["overlays_only"].checked)) continue;

				if(document.getElementById("opt_rated_odds").checked) {
					
					if((document.all["minodds"].value != '') && (round_value(Math.round(ro*100)/100) < parseInt(document.all["minodds"].value)))
						continue;
					if((document.all["maxodds"].value != '') && (round_value(Math.round(ro*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 ror = parseInt(document.all["takeout"].value)/ro;
				var cor = (co == 0) ? 0 : parseInt(document.all["takeout"].value)/co;

				var co_disp = round_value(Math.round(co*100)/100);
				var ro_disp = round_value(Math.round(ro*100)/100);
				var overlay = Math.round(co_disp/ro_disp*10000-10000)/100;

				total_roret += ror;

				result_page += '<tr>';
				result_page += '<td><input type="text" name="bet_'+i+'_'+j+'_'+k+'" 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+'_'+k+'" value="'+i+'-'+j+'-'+k+'" maxlength="8" style="width:100%; text-align:center;" class="input2" readonly></td>';
				result_page += '<td><input type="text" name="ratedodds_'+i+'_'+j+'_'+k+'" value="'+ro_disp+'" maxlength="8" style="width:100%; text-align:left;" class="input2" readonly></td>';
				result_page += '<td><input type="text" name="compodds_'+i+'_'+j+'_'+k+'" 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="roret_'+i+'_'+j+'_'+k+'" value="'+((ror==0) ? '-' : round_value(Math.round(ror*100)/100))+'" maxlength="8" style="width:100%; text-align:left;" class="input2" readonly></td>';
				result_page += '<td><input type="text" name="overlay_'+i+'_'+j+'_'+k+'" 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>';
			result_page += '<td>&nbsp;</td>';
			result_page += '<td>&nbsp;</td>';
			result_page += '<td>&nbsp;</td>';
			result_page += '<td>&nbsp;</td>';
			result_page += '<td><input type="text" name="roret_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['roret_total'].value = round_value(Math.round(total_roret*100)/100);
			
			// show results
			document.all['results'].style.visibility = 'visible';
        }
        
        function clear_results() {
            document.all['results'].innerHTML = "";
            document.all['results'].style.visibility = 'hidden';
        }

