﻿/*function ShowHorseForm(HorseID) {
    open('/Owner/HorseForm.asp?HorseID=' + HorseID ,'PopUpWindow','width=720,height=650,scrollbars=yes,noresize').focus();
}

function ShowHorseTrainHist(HorseID) {
    open('/Owner/TrainingHistory.asp?HorseID=' + HorseID,'PopUpWindow','width=720,height=650,scrollbars=yes,noresize').focus();
}*///Contained in TMCommon.js already

function ApplySelection() {
    document.forms[0]['buttonapply'].value='press';
    document.forms[0].submit();
}

function ResetSelection() {
    document.forms[0].reset();
}

function ChangeCheck(aCheckBox,Product) {
    var TheForm=document.forms[0];
    var FirstLetter=aCheckBox.name.charAt(0);
    for(var i=16; i < TheForm.length; i++) {
        TargetBox = TheForm[i];
        if (TargetBox.name.charAt(0)==FirstLetter) {
            if (TargetBox.checked != aCheckBox.checked) {
                TargetBox.checked = aCheckBox.checked;
                if (TargetBox.name.charAt(1)!='o') {
                    if (TargetBox.value=='M') Product='Gelding';
                    if (TargetBox.value == 'F') Product = 'FilliesFuel';
                    if (!addProduct(TargetBox,Product)) return;
                }
            }
        }
    }
}


function CountAlumites() {
    var TheForm=document.forms[0];
    if (!window.OriginalAlumites) {
        var Stock = document.getElementById('prodowned_Alumites'); //TheForm['prodowned_Alumites'];
	    OriginalAlumites = parseInt(Stock.innerHTML);
    }
    var Count = OriginalAlumites;
    for(var i=5; i < TheForm.length; i++) {
        if (TheForm[i].name.charAt(0)=='s') {
            if (TheForm[i].options[2].text == noneTxt && TheForm[i].selectedIndex != 2) {
                Count--;
            }
        }
    }
    return Count;
}

function AlumitesChange(aSelect) {
    var OnHand = CountAlumites();
    if (OnHand < 0) {
        alert('There are no more products available to apply to this horse.');
        aSelect.selectedIndex = 2;
        return false;
    }
    var TheForm=document.forms[0];
    var Stock = document.getElementById('prodowned_Alumites'); //TheForm['prodowned_Alumites'];
	Stock.innerHTML = OnHand;
}

function ChangeAll(aSelect) {
    if (aSelect.selectedIndex==0) return;
    var OnHand = CountAlumites();
    var c=aSelect.name.charAt(4);
    var TheForm = aSelect.form;
    var Stock = document.getElementById('prodowned_Alumites'); //TheForm['prodowned_Alumites'];
    	    
    for(var i=16; i < TheForm.length; i++) {
	    if (TheForm[i].name.charAt(0)==c) {
		    var j = aSelect.selectedIndex-1;
		    if (TheForm[i].options.length <= j) {
			    j=TheForm[i].options.length-1;
		    }
		    if (TheForm[i].options[2].text=='None') {
		        var ExistValue = TheForm[i].selectedIndex;
		        if (ExistValue == 2 && j!=2) {
		            OnHand--;
		            if (OnHand < 0) {
	                    alert('There are no more products available to apply to this horse.');
                    	Stock.innerHTML = 0;
	                    return false;
		            }
		        }
		        if (ExistValue != 2 && j==2) {
		            OnHand++;
		        }
		    }
		    TheForm[i].selectedIndex = j;
	    }
    }
	Stock.innerHTML = OnHand;
}

function addProduct(aCheckBox, Product) {
    var TheForm = document.forms[0];
    var Stock = document.getElementById('prodowned_' + Product) //TheForm['prodowned_' + Product];

    var intOnHand = (Stock.innerHTML == '') ? 0 : parseInt(Stock.innerHTML);
		
    if (aCheckBox.checked && (intOnHand < 1)) {
	    aCheckBox.checked = false;
	    alert('There are no more products available to apply to this horse.');
	    return false;
    } else {
        Stock.innerHTML = intOnHand + ((aCheckBox.checked) ? -1 : 1);
        return true;
    }
}

