// weather_input.js

/*
*  Client side routines that enable rapid filling of input forms
*  and validation of form fields
*/

	//*******  Enables quick copying of wind directions and forces	 ***********	
	
	function copywind(source,destination,param) {
		//param d = direction; f = force
		source = "document."+"DailyInput."+"wind"+param+source;
		destination = "document."+"DailyInput."+"wind"+param+destination; 
		x = eval(source);			//eval converts string to JavaScript object
		y = eval(destination);		
		y.value = x.value;
	}
	
	function copyboth(source,destination) {	
		copywind(source,destination,'d');
		copywind(source,destination,'f');		
	}
	
	function copyall()	{	
		for (d=2;d<=10;d++)	{
			copyboth(1,d);
		}
	}
	
	//*******  End of wind copying functions  *****************
	
	//******* Cloud 100% Auto-Entry function ****************
	
	function make100(destination)	{		
		destination = "document."+"DailyInput."+destination; 	//Create  object identification string
		x = eval(destination);													//eval converts string to JavaScript object
		x.value = '100';															//Enter 100 into selected cloud cover box
	}
	
	//******* Cloud 100% Auto-Entry function ****************
	
	//*******  Form Validation Routines ***********************
	
	// ------ isNumeric checks for valid floating point or integer
	function isNumeric(sText)	{
	   var ValidChars = "-0123456789.";
   	var IsNumber=true;
	   var Char;
		var NextChar; 	//to validate sequence
		var iPoint=0;	//To track number of point symbols entered
		
		if(isEmpty(sText))	{
			IsNumber =false; 			
		}
 		else	{
	   	for (i = 0; i < sText.length && IsNumber == true; i++) { 
		      Char = sText.charAt(i);
				NextChar = sText.charAt(i+1);
				if(Char=='.') { iPoint++ }
   	   	if (ValidChars.indexOf(Char) == -1) {
	         	IsNumber = false;
   	      }
				//Check for minus (-) after first character and multiple points (.)
				if((Char=='-'&&i>0)||iPoint>1)	{
					IsNumber = false;	
				}			
				
				//Check for double zeros as leading characters	
				if(i==0 && Char=='0'&& NextChar=='0')	{IsNumber = false; }		
      	}
		}
		
   	return IsNumber;   
   }
	
	

	// -----------------------------------------
	//			isEmpty
	//	Returns true if a field is empty
	// -----------------------------------------

	function isEmpty(textObject) {
		var empty = false;		
		if(!textObject)	{empty=true}
		return empty;
	}


	// -----------------------------------------
	//                  isMissing()
	// Validates any object passed as an argument and displays 
	// a message if not found
	// -----------------------------------------		
	
	function isMissing(object) {	
		var e = eval(object);
		var v = e.value;
		if(isEmpty(object)||v=='')	{
		 	alert("Please enter the missing data!");
			object.style.backgroundColor = "#FFFF00";			
		}
		else	{			
			object.style.backgroundColor = "#FFFFFF";
		}
	}
	// ------------------------------------------

	// -----------------------------------------
	//                  validateLength()
	// Validates any object passed as an argument and displays 
	// a message if current is set to true;
	// Returns false if data is missing, else true
	// -----------------------------------------		
	
	function validateLength(object, current, len) {				
		
		if ((object.value.length < len) || (object.value==null)) {
			object.focus();
			object.style.background =  "#ffff88";
			if (current==true) alert("Please enter missing data in the highlighted field.");
			return false;
		}		
		 else
		{
			 object.style.background =  "#ffffff";
			 return true;
		 }		 
	}
	// ------------------------------------------

	//	------------------------------------------
	//			validUKShortDate(text)
	//	Validates a string to see if represents a
	//	short date in UK format: dd/mm/yyyy
	//	------------------------------------------

	function validUKShortDate(stext) {
		var valid = true;
		var len;
		var Char;
		var ValidChars = "0123456789/";
		var i
		
		//	Check string length
		if (sText.length != 10) {
			valid= false;
   		}
   		
   		//	Check for valid characters
		for (i = 0; i < sText.length && IsNumber == true; i++) 
      	{ 
      		Char = sText.charAt(i); 
      		if (ValidChars.indexOf(Char) == -1)	{
        		 valid = false;
        	}
      	}
      	
      	//	Check Year range
      	
      	//	check Month range
      	
      	//	Check Day range
		
		
		return valid;
	}

//**************** Validate Japanese date format ******************		
	
	function validJapaneseDate(sText) {
		var valid = true;
		var char;
		var ValidChars = "0123456789-";
		var year="";
		var month="";
		var day="";
		//var dat ="";	//Debug
		
		//	Check string length
		if (sText.length !=10) {
			valid= false;			
   	}
		else {			
			for(i=0;i<10;i++)	{			
				//	Check for valid characters		
				char = sText.charAt(i);				
				if (ValidChars.indexOf(char) == -1)	{
					valid=false;
				}				
				if(i<4) { year = year+char }						//Build year				
				if(i==5||i==6) { month = month+char }			//build month string
				if(i==8||i==9) { day = day+char }				//build day string
				if((i==4||i==7)&&(char != '-')) {valid = false} //Test for - in right place
				if((i!=4) && (i!=7)&&(char == '-')) {valid = false}	//Test for - in wrong place				
				//dat = dat+char;	//Debug
			}
			
			alert("Characters for "+dat+" "+valid);	Debug
			
			if(valid) {			
				//Check year, month and day are within acceptable range
				iYear = parseInt(year);
				iMonth = parseInt(month);
				iDay = parseInt(day);
				//alert("year: "+iYear+ " month: "+iMonth+ " day: "+iDay);	//Debug
				
				if(iYear <1977||iYear>2020) {valid = false}
				if(iMonth<1||iMonth>12) {valid = false}
				if(iDay<1||iDay>31) {valid = false}
				switch(iDay) {
					case 28:
						if (iMonth!=2) {valid = false}
						break;
					case 29:
						if (iMonth!=2) {valid = false}
						break;
					case 30:
						if (iMonth==1||iMonth==2||iMonth==3||iMonth==5||iMonth==7||
							 iMonth==8||iMonth==10||iMonth==12) {valid = false}
						break;
					case 31:
						if (iMonth==2||iMonth==4||iMonth==6||iMonth==9||iMonth==11) 
						{valid = false}
						break;
				}
			}			
			//Check for leap years
			if((iYear%4)!=0 && iMonth==2 && iDay==29) {valid = false}
			//alert("The result is: "+valid);	//Debug					
		}		
		return valid;
	}
//*********************** End of Japanese date validation *******************	
	
	function limits(caller,max,min,val)	{
		if(val>max||val<min||!isNumeric(val))	{
			alert("The value "+val+" is beyond the permitted range: "+min+" to "+max);			
			caller.value = "";		//change cell colour also
			//e = eval(caller); //not needed as caller is already an object
			caller.style.backgroundColor = "#FFFF00";
		}	//check also for non-numeric data
		else {
			if(val.charAt(0)=='.') {
				caller.value = '0' + caller.value;	
			}
			caller.style.backgroundColor = "#FFFFFF";
		}
	}
	
	function prescomp(caller,val) {
		if(!isNumeric(val))	{
			alert("This is not a valid pressure");			
			caller.value = "";		//change cell colour also
			//e = eval(caller); //not needed as caller is already an object
			caller.style.backgroundColor = "#FFFF00";
		}	//check also for non-numeric data 
		else {
			if (val.length<3) {
				caller.style.backgroundColor = "#FFFFFF";
				if (val<55) {					
					caller.value = 1000 + parseInt(val);						
				}
				else {					
					caller.value = 900 + parseInt(val);					
				}				
			}
			
			else {
				if(val<920||val>1060) {
					alert(val + " is not a valid pressure");			
					caller.value = "";		//change cell colour also			
					caller.style.backgroundColor = "#FFFF00";
				}
				else {
					caller.style.backgroundColor = "#FFFFFF";
				}	
			}			
		}		
	}
	
	
	//******* End of Validation Routines **********************
	
	//*******  Clear Form  ************************************
	
	function clearform() {		
		document.DailyInput.day.value = '';
		document.DailyInput.month.value = '';
		document.DailyInput.year.value = '';	
		document.DailyInput.maxtemp.value = '';
		document.DailyInput.mintemp.value = '';
		document.DailyInput.gmin.value = '';
		document.DailyInput.pressure0800.value = '';
		document.DailyInput.pressure2000.value = '';
		document.DailyInput.db0800.value = '';
		document.DailyInput.wb0800.value = '';
		document.DailyInput.db1700.value = '';
		document.DailyInput.wb1700.value = '';
		document.DailyInput.precipitation.value = '';
		document.DailyInput.weatheram.value = '';
		document.DailyInput.weatherpm.value = '';
		document.DailyInput.vis0800.value = '';
		document.DailyInput.vis3.checked='';
		document.DailyInput.vis4.checked='';
		document.DailyInput.vis7.checked='';
		document.DailyInput.vis9.checked='';
		document.DailyInput.windd1.value='';
		document.DailyInput.windd2.value='';
		document.DailyInput.windd3.value='';
		document.DailyInput.windd4.value='';
		document.DailyInput.windd5.value='';
		document.DailyInput.windd6.value='';
		document.DailyInput.windd7.value='';
		document.DailyInput.windd8.value='';
		document.DailyInput.windd8.value='';
		document.DailyInput.windd9.value='';
		document.DailyInput.windd10.value='';
		document.DailyInput.windf1.value='';
		document.DailyInput.windf1.value='';
		document.DailyInput.windf2.value='';
		document.DailyInput.windf3.value='';
		document.DailyInput.windf4.value='';
		document.DailyInput.windf5.value='';
		document.DailyInput.windf6.value='';
		document.DailyInput.windf7.value='';
		document.DailyInput.windf8.value='';
		document.DailyInput.windf9.value='';
		document.DailyInput.windf10.value='';
		document.DailyInput.cc0800.value='';
		document.DailyInput.cc1700.value='';
		document.DailyInput.snowf.checked='';
		document.DailyInput.snowl.checked='';
		document.DailyInput.hailunder5.checked='';
		document.DailyInput.hailover5.checked='';
		document.DailyInput.thunder.checked='';	
	}
	
	function updateVis()	{	//Automatically updates the vis checkboxes
		var vis = document.DailyInput.vis0800.value;		
		if (vis=='X'||vis=='E'||vis=='0'||vis=='1'||vis=='2'||vis=='3')	{
			document.DailyInput.vis3.checked = true;
			document.DailyInput.vis4.checked = true;
		}		
		if (vis=='4')	{
			document.DailyInput.vis4.checked = true;
		}
		if (vis=='7'||vis=='8'||vis=='9')	{
			document.DailyInput.vis7.checked = true;
		}		
		if (vis=='9')	{
			document.DailyInput.vis9.checked = true;
		}		
	}
	
