
function checkTime(myField) {
	if (myField.value != "" && myField.value != "tba" && myField.value != "TBA") {
        if(myField.name.search(/start/i) != -1){
            name = "Start";
            chkBoxGroup = document.adminForm.start_pm;
        }
        else{
            name = "End";
            chkBoxGroup = document.adminForm.end_pm;
        }

        pmUsed=false;
        amUsed=false;
        no_hours=false;

        var time = myField.value;
        // if value begins with an optional leading 0 followed by a delimiter, assume only minutes being specified
        if(time.search(/^\s*0?[\.\-\+:]/) != -1) no_hours=true;
        time = time.replace(/[-\.,_=\+:;]/g, "");
        time = time.replace(/^\s+/,"");
        if(time.search(/^\d+/) != -1){
        if(time.search(/^0+\D*$/) != -1) time = '0';
        // leading zeros may indicate 24 hr format
        else time = time.replace(/^0+(\d{4})/,"$1");
        time = time.replace(/\s+$/,"");
            //time = time.replace(/([^1,2]\d{2})\d+/,"$1");
        //time = time.replace(/((1|2)\d{3})\d+/,"$1");
        num = time.replace(/^(\d+).*/, "$1");

        if(num*1 <= 2359){
            // pad the entered numer with zeros on the right to make it 4 digits
            if(no_hours){
                num = num.replace(/^(\d)$/,"0" + "$1");
                num = '00' + num + '00';
                // Konqueror and Safari couldn't cope before!!!
                num = num.replace(/^(\d\d\d\d).*$/,"$1");
            }
            num = num.replace(/^(\d)$/,"$1" + "00");
            num = num.replace(/^((1|2)\d)$/,"$1" + "00");
            num = num.replace(/^(\d\d)$/,"$1" + "0");

            if (document.all) mins = num.slice(-2);
            else mins = num.substr(-2);
            //alert('mins are: '+ mins);
            if(mins*1 < 60){
                num *= 1;

                // need to determine here if am/pm being used
                if(time.search(/(a|p)m?$/i) != -1){
                    // using std time for entry
                    // if pm, don't allow number to exceed 1200
                    if(time.search(/p(m)?$/i) != -1){
                        pmUsed=true;
                        if(num < 1200) num += 1200;
                    } else {
                        amUsed=true;
                        if(num >= 1200 && num < 1300) num -= 1200;
                    }
                }
                if(num < 60) hrs = '0';
                else {
                    num = num + '';
                    hrs = num.substr(0,num.length - mins.length);
                }

                // now put the time back into the correct format for the input control depending upon the mode
                                            // 24hr military time format, add a leading 0 onto nums < 1000
                if(hrs.length == 1) hrs = '0' + hrs;
                time = hrs + ':' + mins;

                // sucessful field edit.  update the old field value with the new one
                myField.oldValue = myField.value;
                myField.value = time;

                // TODO force end_time to be after start time when dates match
                return true;
            }
        }
        else if (num*1 <= 2400){
        	alert("for midnight please set 00:00 on the following day");
        	return true;
        }
        }
        // bad input format, alert user, reset field value
        alert('Please enter the Game Time in hh:mm {am|pm}');
        if(myField.oldValue) myField.value = myField.oldValue;
        else myField.value = '';
        window.globalObj = myField;
        var t = setTimeout('window.globalObj.focus();',100);
        return true;
	}
}

function calcRunsHome(n) {
	var runs = 0;
	for (i = 1; i <= n; i++) {
		runs += parseInt(document.getElementById("inninghome" + i).value);
	}
	document.getElementById("runshome").value = runs;
	return;
}

function calcRunsAway(n) {
	var runs = 0;
	for (i = 1; i <= n; i++) {
		runs += parseInt(document.getElementById("inningaway" + i).value);
	}
	document.getElementById("runsaway").value = runs;
	return;
}

function checkWLS(e) {
	var form = document.adminForm;
	if (e.id == 'pitching_w') {
		form.pitching_l.checked = false;
		form.pitching_s.checked = false;
	}
	else if (e.id == 'pitching_l') {
		form.pitching_w.checked = false;
		form.pitching_s.checked = false;
	}
	else if (e.id == 'pitching_s') {
		form.pitching_w.checked = false;
		form.pitching_l.checked = false;
	}
	return;
}

function confirmClear() {
	var confirmed = confirm("Are you sure you want to clear the Game Stats for this Player?");
	if (confirmed) {
		return true;
	}
	else{
		return false;
	}
}

function addField() {
	table = document.getElementById("ftable");
	var newRow = table.tBodies[0].rows[0].cloneNode(true);
	newRow.cells[1].getElementsByTagName("input")[0].value = "";
	table.tBodies[0].appendChild(newRow);
}

function addDivision() {
	var table = document.getElementById("dtable");
	var newRow = table.tBodies[0].rows[0].cloneNode(true);
	newRow.cells[1].getElementsByTagName("input")[0].value = "";
	table.tBodies[0].appendChild(newRow);
}

function wopen(url, name, w, h) {
	w += 32;
	h += 96;
	var left = parseInt((screen.availWidth/2) - (w/2));
	var top = parseInt((screen.availHeight/2) - (h/2));
	var win = window.open(url, name, 'width=' + w + ', height=' + h + ', ' + 'location=no, menubar=no, ' + 'status=no, toolbar=no, scrollbars=no, resizable=no, left=' + left + ',top=' + top + 'screenX=' + left + ',screenY=' + top);
	win.resizeTo(w, h);
 	win.focus();
}

