//  Various validation functions
//

function validateVenueForm()
{
    var validText = false;
    var val = document.form1.name.value;
    for (var i = 0; i < val.length; i++)
    {
	if (val.charAt(i) != " ")
	{
	    validText = true;
	    break;
	}
    }
    if (! validText)
    {
	alert("You must enter a venue name");
	document.form1.name.focus();
	document.form1.name.select();
	return false;
    }
    return true;
}


// validate that a first name has been entered. Return
// false to prevent the form from being submitted.
function validateForm()
{
    var validText = false;
    var val = document.form1.first_name.value;
    for (var i = 0; i < val.length; i++)
    {
	if (val.charAt(i) != " ")
	{
	    validText = true;
	    break;
	}
    }
    if (! validText)
    {
	alert("You must enter a first name");
	document.form1.first_name.focus();
	document.form1.first_name.select();
	return false;
    }
    return true;
}

/////////////////////////////////////////////////////////

// validate all date menus have been set, then popup a window
// with check

function getChoice(opt) {
   for (var i = 0; i < opt.length; i++) {
      if (opt.options[i].selected == true) {
         return opt.options[i].text
      }
   }
   return null;
}

function check_avail(avail_url1)
{
    var validDate = false;
    var month = getChoice(document.form1.month);
    var day   = getChoice(document.form1.day);
    var year  = getChoice(document.form1.year);
    var avail_url = avail_url1 + "&month=" + month
	+ "&day=" + day + "&year=" + year;;

    if (badNum(day))
    {
	alert("Please select a Day value");
	return false;
    }
    if (badNum(year))
    {
	alert("Please select a Year value");
	return false;
    }
    if (badMonth(month))
    {
	alert("Please select a Month value");
	return false;
    }

    //  The input is OK, so lets check availability.

    var availWindow = window.open(avail_url,
				  "Availability",
				  'width=335,height=200,resizable=yes,menubar=no,scrollbars=yes,toolbar=no,location=no,menubar=no');
    availWindow.focus();
    return true;
}

function badNum(numStr)
{
    if (numStr == null || numStr == "")
    {
	return(true);
    }
    for (var i = 0; i < numStr.length; i++)
    {
	if (numStr.charAt(i) < "0" || numStr.charAt(i) > "9")
	{
	    return(true);
	}
    }
    return(false);
}

function badMonth(month)
{
    if (month == null || month == "")
    {
	return(true);
    }
    
    months = ["Jan", "Feb", "Mar", "Apr", "May",
	      "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
    for (var i = 0; i < months.length; i++)
    {
	if (month == months[i])
	{
	    return(false);
	}
    }
    return(true);
}

// Show a url in a general popup window

function show_popup(popup_url, width, height)
{
    var popupWindow = window.open(popup_url,
				  "Popup_Info",
				  "width=" + width +
				  ",height=" + height +
				  ",resizable=yes,menubar=no" +
				  ",scrollbars=yes,toolbar=no,location=no,menubar=no");
    popupWindow.focus();
    return true;
}


/**
 *  Popup a window showing member availability for the gig form date.
 */
function show_member_availability()
{
    var year  = document.forms[0].gig_year.value;
    var month = document.forms[0].gig_month.value;
    var day   = document.forms[0].gig_day.value;
    var duration   = document.forms[0].duration.value;        

    var loc = location.href;
    loc = loc.split('?');
    loc = loc[0];
    var url = loc 
	+ '?Action=show_personnel_availability'
	+ '&day=' + day
	+ '&month=' + month
	+ '&year=' + year
	+ '&duration=' + duration;

    show_popup(url, 500, 270);
	
}
