// Initialize somewhat randomly -- a better approach would be to 
// pull data from a database. The menus on the HTML document using
// this script should be initialized from the same source. 
var varvals = new Array(12);
varvals[0] = new Array ('apcp', 'Surface');
varvals[1] = new Array ('heating', 'Surface');
varvals[2] = new Array ('pwat', 'Surface');
varvals[3] = new Array ('prmsl', 'Surface');
varvals[4] = new Array ('t2m', 'Surface');
varvals[5] = new Array ('rhum', 'Surface');
varvals[6] = new Array ('u10m', 'Surface');
varvals[7] = new Array ('v10m', 'Surface');
varvals[8] = new Array ('hgt', '850','700','500','250','150');
varvals[9] = new Array ('temp', '1000','850', '700', '500', '250','150');
varvals[10] = new Array ('uwnd', '1000','850', '700', '500', '250','150');
varvals[11] = new Array ('vwnd', '1000','850', '700', '500', '250','150');

// function Init(MyForm)
// {
// 	// note that we need to look for document representing a specific 
// 	// layer as defined in the "standard" CDC header
//         var doc = document;
//         if (document.layers) {
// 		doc = document.layers['tbody'].document;
// 	}
//         VarsObject = doc[MyForm].vars;
//         VarsObject.selectedIndex = 0;
// 	VarsObject.length = varvals.length;
// 
// 	for (ix = 0; ix < varvals.length; ix++) {
// 		if (VarsObject.options[ix] == null) {
// 			VarsObject.options[ix] = new Option(varvals[ix][0], ix);
// 		} else {
// 		      	VarsObject.options[ix].value = varvals[ix][0];
//        	         	VarsObject.options[ix].text = varvals[ix][0];
// 		}
// 	}
// 
// 	// get the list of levels for the default variable
// 	var levelvals = new Array();
// 	levelvals = varvals[0].slice(1);
// 
//         LevelsObject = doc[MyForm].levels;
//         LevelsObject.selectedIndex = 0;
// 	LevelsObject.length = levelvals.length;
// 
// 	for (ix = 0; ix < levelvals.length; ix++) {
// 			LevelsObject.options[ix] = new Option(levelvals[ix], levelvals[ix]);
// 	}
// 
// 	DayObject = doc[MyForm].start_date;
//         eDayObject = doc[MyForm].end_date;
// 
// 	DayObject.options[0] = new Option("entire month",0);
// 	eDayObject.options[0] = new Option("entire month",0);
// 	for (ix = 1; ix < 31+1; ix++) {
// 			DayObject.options[ix] = new Option(ix,ix);
// 			eDayObject.options[ix] = new Option(ix,ix);
// 	}
// }

function SetLevels(MyForm) {
	var levelvals = new Array();

	// note that we need to look for document representing a specific 
	// layer as defined in the "standard" CDC header
// First, below assumed names of elements gen'd by headers(!)
// Second, "layers" only works in netscape deriviatives.
//        var doc = document;
//        if (document.layers) {
//		doc = document.layers['tbody'].document;
//	}
//        VarsObject = doc[MyForm].vars;
        var inx ;
	for (inx = 0 ;  inx < document.forms.length ;  ++inx) {
	    if (document.forms[inx].name == MyForm) {
	        var myFormObject = document.forms[inx] ;
	    }
        }
        var VarsObject = myFormObject.refcstvars ;
        if (VarsObject.selectedIndex < 0) {
		VarsObject.selectedIndex = 0;
	}

	// get the list of levels for this variable
	levelvals = varvals[VarsObject.selectedIndex].slice(1);

//        LevelsObject = doc[MyForm].levels;
        var LevelsObject = document.refvarwanted.levels ;
	currentLevel = LevelsObject[LevelsObject.selectedIndex].text;
	LevelsObject.length = levelvals.length;
	LevelsObject.selectedIndex = 0;

	for (ix = 0; ix < levelvals.length; ix++) {
		if (LevelsObject.options[ix] == null) {
			LevelsObject.options[ix] = new Option(levelvals[ix], 
							      levelvals[ix]);
		} else {
	                LevelsObject.options[ix].value = levelvals[ix];
                	LevelsObject.options[ix].text = levelvals[ix];
		}
		if (currentLevel == levelvals[ix]) {
			LevelsObject.selectedIndex = ix;
                	LevelsObject.options[ix].selected = true;
		}
		//else{
		//     LevelsObject.selectedIndex = 0;
		//}
	}
}

// function SetDayOfMonth(MyForm) 
// {
// 	var dayvars = new Array();
// 	
// 	// note that we need to look for document representing a specific 
// 	// layer as defined in the "standard" CDC header
//         var doc = document;
//         if (document.layers) {
// 		doc = document.layers['tbody'].document;
// 	}
// 	YObject = doc[MyForm].start_year;
// 	eYObject = doc[MyForm].end_year;
//         YmonObject = doc[MyForm].month;
// 
// 	yr = YObject.value;
// 	eyr = eYObject.value;
// 	mon = YmonObject.value;
//         
// 
// 
//         if (YmonObject.selectedIndex < 0) {
// 		YmonObject.selectedIndex = 0;
// 	}
// 
// 	var td=31;
// 	if (mon == 'Apr' || mon == 'Jun' || mon  == 'Jul' || mon  == 'Nov') td=30;
// 
// 	if (mon == 'Feb'){
// 		td = 28;
// 		if(eyr==yr){
// 			if (yr == '2000' || yr == '1996' || yr == '1992' || yr == '1988' || yr == '1984' || yr == '1980'){ 
// 				td=29;
// 			}
// 		}
// 	}
// 	//alert(td);
// 	for(i=0;i<td;i++) {dayvars[i] = i+1;}
// 	
//         DayObject = doc[MyForm].start_date;
//         eDayObject = doc[MyForm].end_date;
// 	currentday = DayObject[DayObject.selectedIndex].text;
// 	currentEday = eDayObject[DayObject.selectedIndex].text;
// 	DayObject.length = td;
// 	DayObject.selectedIndex = 0;
// 	eDayObject.length = td;
// 	eDayObject.selectedIndex = 0;
// 
// 	//alert(yr+mon);
// 
// 	DayObject.options[0].value = 0;
//         DayObject.options[0].text = "entire month";
// 	eDayObject.options[0].value = 0;
//         eDayObject.options[0].text = "entire month";
// 
// 	for (ix = 1; ix < td+1; ix++) {
// 		if (DayObject.options[ix] == null) {
// 			DayObject.options[ix] = new Option(ix,ix);
// 		} 
// 		if (eDayObject.options[ix] == null) {
// 			eDayObject.options[ix] = new Option(ix,ix);
// 		} 
// 
// 		else {
// 	                DayObject.options[ix].value = ix;
//                 	DayObject.options[ix].text = (ix).toString();
// 			eDayObject.options[ix].value = ix;
//                 	eDayObject.options[ix].text = (ix).toString();
// 			
// 		}
// 		//if (currentday == dayvals[ix]) {
// 		//	alert("currentday");
// 		//	DayObject.selectedIndex = ix;
//                 //	DayObject.options[ix].selected = true;
// 		//}
// 	}
// 
// 
// 
// }

function Verify(MyForm) {
        var doc = document;
	var emailFilter=/^.+@.+\..{2,3}$/;

// See my comment in function SetLevels
//         if (document.layers) {
// 		doc = document.layers['tbody'].document;
// 	}
        var inx ;
	for (inx = 0 ;  inx < document.forms.length ;  ++inx) {
	    if (document.forms[inx].name == MyForm) {
	        var myFormObj = document.forms[inx] ;
	    }
	}
        var sh = myFormObj.start_hr.selectedIndex ;
	var eh = myFormObj.end_hr.selectedIndex ;
	var email = myFormObj.email.value;
	var sy = myFormObj.start_year.value;
	var ey = myFormObj.end_year.value;
	var sd = myFormObj.start_date.value;
	var ed = myFormObj.end_date.value;
        var sm = myFormObj.start_month.selectedIndex ;
	var em = myFormObj.end_month.selectedIndex ;

	for (i=0, n=myFormObj.download_type.length; i<n; i++) {
   		if (myFormObj.download_type[i].checked) {
      			var checkvalue = myFormObj.download_type[i].value;
      			break;
   		}
	}

	msg ='' ;
	if (sy > ey) {
		msg = "end year must be no less than start yr\n";
	}
//     if (sm > em) {
//       msg = msg + "end month must be no less than start month\n";
//     }
//     if (sd > ed) {
//       msg = msg+"end date must be no less than start date\n";
//     }

	if ((sd+ed)!=0 && (sd*ed)==0) {
		msg = msg+"start date and end date must be in the same format(both entire month /date of month)\n";
	}

	if (sh > eh) {
		msg = msg+"end hour must be no less than start hour\n";
	}

// This is stupid, as it's always that.
//	if (sh%12 != 0) {
//		msg = msg+"start hour must be multiple of 12\n";
//	}
// Ditto
//	if (eh%12 != 0) {
//		msg = msg+"end hour must be multiple of 12\n";
//	}
	
	if (!(emailFilter.test(email))) { 
       		msg = msg+"Please enter a valid email address.\n";
	}	

// Should never occur.
//    if (sh=='' || eh=='') {
//       msg = msg+"start hour and end hour are required field\n";
//    }
      	if (!(checkvalue)) {
       		msg = msg+"Please check download type.\n";
    	}
	if (sy=='None' && sd=='') {
		msg = msg+"you have to select a time range"
	}

	if (msg) {
		alert(msg);
		return false;
	} else {
		return true;
	}
}
