
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function validate_obs_date() {
with ( document.form1 ) {      
	if ( obs_date.options[obs_date.selectedIndex].value != "") return "";
   	else return "- day is required\n";
   }
}

function validate_min() {
with ( document.form1 ) {      
	if ( min.options[min.selectedIndex].value != "") return "";
   	else return "- local time minutes is required\n";
   }
}

function validate_hour() {
with ( document.form1 ) {      
	if ( hour.options[hour.selectedIndex].value != "") return "";
   	else return "- local time hour is required\n";
   }
}

function validate_constellation() {
with ( document.form1 ) {      
	if (constellation.options[constellation.selectedIndex].value != "") return "";
   	else return "- constellation is required\n";
   }
}

/*function validate_const_hem() {
	with ( document.form1 ) {      
	if ( (constellation.options[constellation.selectedIndex].value == "Cygnus") &&  (lat.value < 0) ){
			return "- northern hemisphere observers must select Cygnus\n (Please recheck your latitude or constellation)\n";
			}
	else if ( (constellation.options[constellation.selectedIndex].value == "Sagittarius") &&  (lat.value >= 0) ){
		return "- southern hemisphere observers must select Sagittarius\n(Please recheck your latitude or constellation)\n";
	}
	else return "";
   }
	
}*/

function validate_country() {
with ( document.form1 ) {      
	if (country.options[country.selectedIndex].value != "") return "";
   	else return "- country is required\n";
   }
}

function validate_const_hem() {
	with ( document.form1 ) {      
	if (lat.value < 0) { //southern hemisphere
		  if  (constellation.options[constellation.selectedIndex].value == "Cygnus"){
		  return "- your latitude indicates you should observe Sagittarius\n (Please recheck Lat or Constellation)\n";
		  }
		 else return "";
	}
	else if	(lat.value >=0) { //northern hemisphere
		if  (constellation.options[constellation.selectedIndex].value == "Sagittarius") {
			return "- your latitude indicates you should observe Cygnus\n (Please recheck Lat or Constellation)\n";
			}
		else return "";
	}
	else return "";
   } //with	
} //function

function validate_limmag(){
	with ( document.form1 ) {  
		var radio_choice = false;
	
		for (counter = 0; counter < lim_mag.length; counter++){
			if (lim_mag[counter].checked)
				radio_choice = true; 
		}
		
		if (!radio_choice)	return "- limiting magnitude is required\n";
	
		return "";
	}
}

//check lat/lon is not (0,0)
function validate_lat_lon() {
	with ( document.form1 ) { 
		var errmess="";
		if( (lat.value ==0) && (lon.value == 0)) {
			errmess += "- Lat/Lon cannot be 0,0\n";
		}
		return errmess;
	}
}

//check lat is within US
function validate_lat_us() {
	with ( document.form1 ) { 
		var errmess="";
		if (lat.value !="") {
			if ( country.options[country.selectedIndex].value == "us"){
				//not within continental, AK, or HI
				if ( !(lat.value>18 && lat.value<72) ) {
					errmess += "- latitude entered is not within the US\n";
				}
			}
		}
		return errmess;
	}
}

//check lon is within US
function validate_lon_us(){
	with ( document.form1 ) { 
		var errmess="";
		if (lon.value !="") {
			if ( country.options[country.selectedIndex].value == "us"){
				//not within continental, not within AK&HI
				 if( (!(lon.value >= -125 && lon.value <= -66)) && (!(lon.value >= -178 && lon.value <= -129)) ) {
						errmess += "- longitude entered is not within the US\n";
				}
			}
		}
		return errmess;
	}
}

//check lat/lon is within CA
function validate_ca() {
	with ( document.form1 ) { 
		var errmess="";
		if (lat.value !="" &&  lon.value !=""){
			if ( country.options[country.selectedIndex].value == "ca"){
				//not within Canada
				if ( !(lat.value>0) ) {
					errmess += "- latitude entered is not within Canada\n";
				} else if ( !(lon.value<0) ) {
					errmess += "- longitude entered is not within Canada\n";
				}
			}
		}
		return errmess;
	}
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  
	errors += validate_obs_date();
	errors += validate_min();
	errors += validate_hour();

	
	//valdiate lat, lon, and country with MM built-in commands	
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }

	}

	//extra validation
	errors += validate_country();
	errors += validate_constellation();
	errors += validate_limmag();
	errors += validate_lat_lon();
	errors += validate_lat_us();
	errors += validate_lon_us();
	errors += validate_ca();
	errors += validate_const_hem();
	
	if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}


function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}