﻿
function ChangeAll(aSelect) {
    var si = aSelect.selectedIndex;
    if (si == 0) return;
	var c=aSelect.name.charAt(4);
	var TheForm = aSelect.form;
	var targetValue = aSelect.options[si].value;
	for(var i=0; i < TheForm.length; i++) {
	    if (TheForm[i].name.charAt(0) == c) {
    	    var tfo = TheForm[i].options;
    	    for (var j = 0; j < tfo.length; j++) {
	            if (targetValue == tfo[j].value) TheForm[i].selectedIndex = j;
	        }
		}
	}
}

function ChangeCheck(aCheckBox) {
	var TheForm=aCheckBox.form;
	for(var i=0; i < TheForm.length; i++) {
		if (TheForm[i].name.charAt(0)=='e') {
			TheForm[i].checked = aCheckBox.checked;
			TheForm[i].value = (TheForm[i].checked) ? 'Y' : 'N';
		}
	}
}

// Defaults Submit
function SubmitMemberDetails(aButton) {
	var TheForm=document.forms[0];
	TheForm['isUpdate'].value='MEMBER';
	ButtonDisable(aButton);
	TheForm.submit();
}
	
// Auto Train Submit
function SubmitTraining(aButton,HorseCount ) {
	var TheForm=document.forms[0];
	var Err = '';
	for(var i=1; i <= HorseCount; i++) {
		var HorseID = TheForm['h'+i].value;
		var Track = TheForm['t'+HorseID].options[TheForm['t'+HorseID].selectedIndex].text;
		var Going = TheForm['g'+HorseID].options[TheForm['g'+HorseID].selectedIndex].text;
		IsTurfTrack = (Track.indexOf('turf')) > -1;
		IsTurfGoing = (Going.indexOf('turf')) > -1;
		IsDirtTrack = (Track.indexOf('dirt')) > -1;
		IsDirtGoing = (Going.indexOf('dirt')) > -1;
		if ((IsTurfTrack && IsDirtGoing) || (IsDirtTrack && IsTurfGoing)){
			Err += 'Horse # ' + i+ ' has Going: ' + Going +' which is not valid for Track: '+Track+'\n\r';
		}
	}
	
	if (Err=='') {
	    TheForm['isUpdate'].value = 'HORSE';
	    if (document.getElementById('btnContainer') != null) {
	        document.getElementById('btnContainerDiv').className = 'StableBTNDisabled';
	        document.getElementById('btnContainerLink').onclick = null;
	    }
		TheForm.submit();
	} else {
		Err = 'Please correct the following and click again for your horses to be Auto Trained.\n\r\n\r'+Err;
		alert(Err);
	}
}

// Normal Training submit
function SubmitForm(aButton,HorseCount ) {
	var TheForm=document.forms[0];
	document.forms[0].Command.value = 'TRAIN';
	var Err = '';
	var RunCount = 0;
	
	for(var i=1; i <= HorseCount; i++) {
		var HorseID = TheForm['h'+i].value;
		var Track = TheForm['t'+HorseID].options[TheForm['t'+HorseID].selectedIndex].text;
		var Going = TheForm['g'+HorseID].options[TheForm['g'+HorseID].selectedIndex].text;
		var Runs = TheForm['r'+HorseID].value;
		if (Runs != '0') RunCount++;
		IsTurfTrack = (Track.indexOf('turf')) > -1;
		IsTurfGoing = (Going.indexOf('turf')) > -1;
		IsDirtTrack = (Track.indexOf('dirt')) > -1;
		IsDirtGoing = (Going.indexOf('dirt')) > -1;
		if ((IsTurfTrack && IsDirtGoing) || (IsDirtTrack && IsTurfGoing)){
			Err += 'Horse # ' + i+ ' has Going: ' + Going +' which is not valid for Track: '+Track+'\n\r';
		}
	}
	
	if (RunCount==0) Err= 'You have not selected any Runs for any of the horses.\n\r'+Err; 
	if (Err=='') {
	    document.getElementById('btnContainerDiv').className = 'StableBTNDisabled';
	    document.getElementById('btnContainerLink').onclick = null;
		TheForm.submit();
	} else {
		Err = 'Please correct the following and click on RUN '+BarrierRace+'S again for your horses to be trained.\n\r\n\r'+Err;
		alert (Err);
	}
}
