// Padillatech.com 2007

//Observation constructor
function Observation(){

	//member variables
	var _self = this;
	_self.myDate = new obsDate($('#datum').val());
	_self.mySpecies = new Species(get("species"), get('latinName'));
	_self.myLocation = new Location('#lat', '#long', '#location_name');
	_self.myObserver = $("#observer").val();
	_self.myPhenology = new Pheno(myPhenology.values());
	_self.myInvalidElems = new Array();
	_self.myProject = $('#projects');
	
	//represent the number of required fields in form
	var reqFields = 6;

	//member methods
	
	//.... returns the values of the member variables of the Observation object
	_self.tostring = function(){
			var months = new Array('jan', 'feb', 'mar', 'apr', 'maj', 'jun', 'jul', 'aug', 'sep', 'okt', 'nov', 'dec');
			var month = 0;
			var day = this.myDate.day;
			//convert string to number
			for(var index = 0; index < months.length; index++){
				if(months[index] == this.myDate.month){
					month = index+1;
					break;
				}			
			}
			if(month < 10)
				month = '0'+month;
			if(day < 10)
				day = '0'+day;
				
			var rtn = _self.mySpecies.commonName + "," + _self.mySpecies.latinName + "," +  _self.myDate.year + month + day + "," + _self.myLocation.toString() + ","+ _self.myObserver+ ","+ _self.myProject.val();
			
			return rtn;
		}
		 
	_self.clear = function(clearParam){
		
		if(clearParam != "maintain"){
			$('#datum').val("");
			$("#address").val("");
			_self.myLocation.clear();
		}
	
		$("#species").val("");
		$('#latinName').val("");
	
		myPhenology.clear();
		}	
		
	//returns true if the form was only partially filled out ... false otherwise 	
	_self.partial = function(){
			return this.myInvalidElems.length > 0 && this.myInvalidElems.length < reqFields;
		}	
	
	//displays a red border in the textbox in those elements that are in the myInvalidElems array
	_self.showInvalid = function(){
			/*for(var index = 0; index < this.myInvalidElems.length; index++){
				this.myInvalidElems[index].style.borderColor = 'red';	
			}*/
		}
	_self.resetBG = function(){
		get("species").style.border = '2px inset #eeffff';
		get('latinName').style.border = '2px inset #eeffff';
		get("observer").style.border = '2px inset #eeffff';
		get('phenology').style.border = '2px inset #eeffff';
		get('datum').style.border = '2px inset #eeffff';
	}	
	
	_self.valid = function(){
		this.resetBG();
		var rtn = true;
		if(_self.mySpecies.commonName == ""){
			$('#species').css('border-color', '#ff0000');
			rtn = false;
		}	
		if(_self.mySpecies.latinName == ""){
			$('#latinName').css('border-color', '#ff0000');
			rtn = false;
		}
		if(_self.myObserver == ""){
			$('#observer').css('border-color', '#ff0000');
			rtn = false;
		}
		if($('#datum').val() == "" || _self.myDate.value() == ' ' || isFutureDate($('#datum').val())){
			$('#datum').css('border-color', '#ff0000');
			rtn = false;	
		}	
		if(_self.myPhenology.values.length <= 0){
			$('#phenology').css('border-color', '#ff0000');
			rtn = false;
		}
		
		//is the location valid?
		if(!_self.myLocation.isValid()){
			rtn = false;
		}
		
		return rtn;
	}	
		
	_self.edit = function(){
		get('datum').value = this.myDate.day+" "+this.myDate.month+", "+this.myDate.year;
		get("species").value = this.mySpecies.commonName;
		get('latinName').value = this.mySpecies.latinName;
		get("observer").value = this.myObserver;
		this.myPhenology.setValues();
		_self.myLocation.edit();
		map.addOverlay(new GMarker(new GLatLng(_self.myLocation.latitudeInitVal, _self.myLocation.longitudeInitVal), icon));
	}	
		
}

//phenology constructor
function Pheno(values){
	var _self = this;
	_self.values = values;
	
	_self.tostring = function(){
		var rtn = "";
		
		for(var index = 0; index < _self.values.length; index++){
			rtn += _self.values[index] + ",";
		}
		
		return rtn.slice(0, -1);
	}
	
	_self.setValues = function(){
		myPhenology.setValues(_self.values);
	}
	
}


//Location constructor
function Location(lat, lon, name){
	var _self = this;
	_self.latitude = $(lat);
	_self.latitudeInitVal = _self.latitude.val();
	_self.longitude = $(lon);
	_self.longitudeInitVal = _self.longitude.val();
	_self.myName = $(name);
	_self.myNameInitVal = _self.myName.val().replace(',','');
	
	//returns true if both lat and long are not null, returns false and sets the background of the null valued text element(s) to red otherwise
	_self.isValid = function(){
		var rtn = true;//assume ok
		/*_self.latitude.css('border-color', '#fff');
		_self.longitude.css('border-color', '#fff');
		_self.myName.css('border-color', '#fff');*/
		_self.latitude.css('border', '2px inset #eeffff');
		_self.longitude.css('border', '2px inset #eeffff');
		_self.myName.css('border', '2px inset #eeffff');
			
		if(_self.latitude.val() == ""){
			_self.latitude.css('border-color', 'red');
			rtn = false;
		}
			
		if(_self.longitude.val() == ""){
			_self.longitude.css('border-color', 'red');
			rtn = false;
		}
		
		if(_self.myName.val() == ""){
			_self.myName.css('border-color', 'red');
			rtn = false;
		}
				
		return rtn;
	}
	
	_self.toString = function(){
		return _self.latitudeInitVal+","+_self.longitudeInitVal+","+_self.myNameInitVal;
	}
	
	_self.clear = function(){
	    _self.latitude.val("");
		_self.longitude.val("");
		_self.myName.val("");
	}
	
	_self.edit = function(){
		_self.latitude.val(_self.latitudeInitVal);
		_self.longitude.val(_self.longitudeInitVal);
		_self.myName.val(_self.myNameInitVal);
	}
}

//Species constructor
function Species(cName, lName){
	
	
	this.commonName = cName.value;
	this.latinName = lName.value;
	
	//returns true if both names are not null, returns false and sets the background of the null valued text element(s) to red otherwise
	this.valid = function(){
			
		get("species").style.backgroundColor = 'white';
		get('latinName').style.backgroundColor = 'white';
			
		if(this.commonName == "" && this.latinName == ""){
			get("species").style.backgroundColor = 'red';
			get('latinName').style.backgroundColor = 'red';
			return false;
		}
		
		if(this.commonName == ""){
			get("species").style.backgroundColor = 'red';	
			return false;
		}	
			
		if(this.latinName == ""){
			get('latinName').style.backgroundColor = 'red';
			return false;
		}
			
		return true;
	}
}

//date constructor
function obsDate(datum){//ex. 31 jan, 2009
	var num = 2;
	var beg = 0;
	if(datum.length < 12){
		num = 1;
	}
	
	this.day = datum.substr(beg, num);
	beg += ++num;
	num = 3; 
	this.month = datum.substr(beg, num);
	beg += (num+ 2);
	num = 4;
	this.year = datum.substr(beg, num);
	
	this.value = function(){return this.tostring()};
	
	this.tostring = function(){
		var months = new Array('jan', 'feb', 'mar', 'apr', 'maj', 'jun', 'jul', 'aug', 'sep', 'okt', 'nov', 'dec');
			var month = 0;
			var day = this.day;
			//convert string to number
			for(var index = 0; index < months.length; index++){
				if(months[index] == this.month){
					month = index+1;
					break;
				}			
			}
			
			if(month < 10)
				month = '0' + month;
				
			if(day < 10)
				day = '0' + day;
			return day + "/" + month + "/" + this.year;
		}
}


//Observations constructor
function Observations(){
	var _self = this;
	_self.observations = new Array();
	
	//adds a new observation to the observations array
	//assumes the oberservation is valid
	_self.add = function(observation, clearParam){
			if(observation.valid()){
				_self.observations.push(observation);
				observation.clear(clearParam);
				map.clearOverlays(); 
			}else
				observation.showInvalid();
		}
	//removes the selected observation from the observations array
	_self.removeObservation = function(index){
		
			if(_self.observations.length == 1){
				_self.observations = new Array();
				return;
			}
			
			var numObs = _self.observations.length;
			var temp = new Array(numObs -1);
			var i = 0;
			var j = 0;
			
			while(i < numObs){
				if(index != i){
					temp[j++] = _self.observations[i++];
				}
				else
					i++;
			}
			
			_self.observations = temp;
			/*for(k=0; k< _self.observations.length; k++)
				alert(_self.observations[k].tostring());*/
		}
		
		
	_self.confirm = function(observation){
			if(observation.valid()){
				_self.add(observation);
				new Confirm(_self);
			}else	
				if(_self.observations.length > 0 && !observation.partial()){
					new Confirm(_self);
			}
			else
				observation.showInvalid();
		}	
	
		
	_self.tostring = function(){
			var rtn = "";
		
			for(var index = 0; index < _self.observations.length; index++){
				rtn += _self.observations[index].tostring() + ";";
			}
			
			return rtn;
		}
		
	_self.submit = function(){
		var pheno = "";
		for(var index = 0; index < _self.observations.length;index++){
			pheno += _self.observations[index].myPhenology.tostring() + '^';
		}
		
		query = 'observations=' +_self.tostring().slice(0, -1) + '&stages=' + pheno.slice(0, -1);
		
		$.post('backend/insertobservation.php', query, function(response){
			showNewLocations(response);
		});
	}
}

