/*

    JavaScript Utilities for Earth and Moon Viewer
    
*/

/*  validateCustomRequest

    Validate a custom request, checking for such lunacy
    as a request for a view of the Moon from an Earth
    satellite.  Note that no harm will be done if such a
    request slips through (for example, because JavaScript
    is absent or disabled), but it's better to nip it in the
    bud and explain why we're rejecting it.
    
*/

function validateCustomRequest(f)
{
    var i, imgname, isMoon;
    
    for (i = 0; i < f.img.length; i++) {
    	if (f.img[i].checked) {
	    imgname = f.img[i].value;
	    break;
	}
    }
    
    isMoon = imgname.substring(0, 4) == "Moon";
    if (f.opt[5].checked && isMoon) {
    	alert("Sorry, you cannot view the Moon from an Earth satellite.");
	return false;
    }
    
    if (f.dynimg.checked) {
    
    	/*  If we're composing a dynamic request, clear any
	    text fields which aren't relevant to the request
	    (determined by which adjacent boxes are checked)
	    to reduce the size of the request.  */
	    
	if (!(f.opt[4].checked)) {  	// If not "view above",
	    f.lat.value = "";	    	//  clear latitude, longitude, altitude
	    f.ns[0].checked = false;
	    f.ns[1].checked = false;
	    f.lon.value = "";
	    f.ew[0].checked = false;
	    f.ew[1].checked = false;
	    f.alt.value = "";    
    	}
	
	if (!(f.opt[5].checked)) {    // If not "From satellite", clear elements
	    f.tle.value = "";
	    f.bird.options.length = 0;
	}
	
	if (!(f.date[1].checked)) {   // If not UTC date, clear field
	    f.utc.value = "";
	}
	
	if (!(f.date[2].checked)) {   // If not Julian date, clear field
	    f.jd.value = "";
	}
	
//    	alert("Dyno!  Dyno!");
	
    }
    
    return true;
}
