/*
* DataManagerRecord is an object that is a prototype for the MQGeoAddress
* It adds the additional fields related to an address and functionality for
* storing locationid, name, distance, routetime, routedistance.
*
* @param        recordset - a MQRecordSet
* @author       Seisan Consulting   2-14-2007
*/
DataManagerRecord.prototype = new MQGeoAddress();
DataManagerRecord.prototype.constructor = DataManagerRecord;
function DataManagerRecord(recordset){
	MQGeoAddress.call(this);

	//Address locaion id
	this.id = "";
	//Poi Name
	this.name = "";
	//Poi Phone
	this.phone = "";
	//Distance from origin, for searches by distance
	this.distance = null;
	//Route Time, for searches by minutes
	this.time = null;
	//Route Distance, for searches by minutes
	this.rDistance = null;
	//Array containing matrix of field name-value pairs
	this.fieldValues = new Array();


	//Helper functions for setting additional values
	this.setId = function(i){ this.id  = i; };
	this.setName = function(n){ this.name  =  n; };
	this.setDistance = function(d){ this.distance = d;};
	this.setRouteTime = function(t){ this.time = t;};
	this.setRouteDistance = function(rd){ this.rDistance = rd;};
	this.setPhone = function(p){ this.phone = p;};

	//Helper functions for retrieving additional values
	this.getId = function(){ return this.id; };
	this.getName = function(){ return this.name; };
	this.getDistance = function(){ return this.distance;};
	this.getRouteTime = function(){ return this.time;};
	this.getRouteDistance = function(){ return this.rDistance;};
	this.getPhone = function(){ return this.phone;};


	/*
	* Function to set the content of the infowindow based on availability of address data (Name, Street, etc) for a MQPoi.
	*
	* @return		The content for POI info window
	* @author       Barkley 8-13-2008
	*/
	this.getInfoHTML = function(linkstr){   
		var info = "<div class='poiInfo'>";
		
		// if the store is new or upcoming, add to the POI info window:
		if(this.getField("OPENSTATUS") != "Existing")
		{
			info += "<span class='newUpcoming'>" + this.getField("OPENSTATUS") + "!</span><br/>";
		}
		
		info += this.getStreet() + "<br/>";
		info += this.getCityStatePostalCodeString() + "<br/>" + this.getPhone(); 
		if(this.getDistance() != null)
		{
		  info += "<br/>"  + formatDistance(this.getDistance()) + " Miles";
		}
		if ( linkstr != null ) { info += "&nbsp;&nbsp;&nbsp;" + linkstr; }
		info += "</div>";

		return info;
	}

    /*
    * Function to set the content of the infowindow based on availability of address data (Name, Street, etc) for a MQPoi.
    *
    * @return       The content for POI info window
    * @author       Barkley 8-13-2008
    */
    this.getInfoTitleHTML = function(){
        var info = "<div class='poiInfoTitle'>"  + this.getName() + "</div>"; 
        return info;
    }

	/*
	* Function to build a string containing the City, State and Postal Code.
	*
	* @return		str- a string containing the Address's City, State and Postal Code.
	* @author       Seisan Consulting   2-14-2007
	*/
	this.getCityStatePostalCodeString = function(){
		var str = "";
		var spacer = false;
		if(this.getCity() != ""){
            //str += StringFunctions.capitalize(this.getCity());
			str += this.getCity();
			spacer = true;
		}

		if(this.getState() != ""){
			if(spacer){
				str += ", ";
			}
			str += this.getState();
			spacer = true;
		}

		if(this.getPostalCode() != ""){
			if(spacer){
				str += " ";
			}
			str += this.getPostalCode();
			spacer = true;
		}

		return str;
	}


	/*
	* Function to build the infowindow content. Displays address information and distance.
	* Builds the link to search for points of interest or hotels based upon the result type.
	* Builds the link to the detail sheets based upon the result type.
	* Builds the link to the Driving Directions.
	* Pre-Condition: none
	* Post-Condition: Html is created based on the address information and the result type and returned.
	*
	* @param		num - row number in the results list, each row is a form to be submitted when clicking links within content.
	* @param		isPoiSearch - boolean value evaluates to which type of result is being displayed, Point of Interest or Hotel Address
	* @return		str- a string containing the html for the infowindow content
	* @author       Seisan Consulting   2-14-2007
	*/
	this.getInfoContentHTML = function(num, isPoiSearch){
		return this.getInfoContentElement(num, isPoiSearch).outerHTML;
	}

	/*
	* Function to build the infowindow content. Displays address information and distance.
	* Builds the link to search for points of interest or hotels based upon the result type.
	* Builds the link to the detail sheets based upon the result type.
	* Builds the link to the Driving Directions.
	* Pre-Condition: none
	* Post-Condition: Html is created based on the address information and the result type and returned.
	*
	* @param		num - row number in the results list, each row is a form to be submitted when clicking links within content.
	* @param		isPoiSearch - boolean value evaluates to which type of result is being displayed, Point of Interest or Hotel Address
	* @return		element- an object containing the html for the infowindow content
	* @author       Seisan Consulting   2-14-2007
	*/
	this.getInfoContentElement = function(num, isPoiSearch){
		var htmlElement = document.createElement("div");
		if(this.getName() != ""){
			htmlElement.appendChild(document.createTextNode(this.getStreet()));
			htmlElement.appendChild(document.createElement("br"));
		}
		if(this.getStreet() != "" || this.getName() != ""){
			htmlElement.appendChild(document.createTextNode(this.getCityStatePostalCodeString()));
		}
		if(this.getPhone() != ""){
			htmlElement.appendChild(document.createElement("br"));
			htmlElement.appendChild(document.createTextNode("Phone: " + this.getPhone()));
		}
		if(this.getRouteTime() != null){
			htmlElement.appendChild(document.createElement("br"));
			var distance = formatDistance(this.getRouteDistance());
			htmlElement.appendChild(document.createTextNode("Distance: " + distance + " Miles"));
			if(this.getRouteTime() >= 3600)
				var time = formatTime(this.getRouteTime(),"%h hr %m min %s sec");
			else
				var time = formatTime(this.getRouteTime(),"%m min %s sec");
			htmlElement.appendChild(document.createElement("br"));
			htmlElement.appendChild(document.createTextNode("Time: " + time));
		}
		else if(this.getDistance() != null){
			htmlElement.appendChild(document.createElement("br"));
			htmlElement.appendChild(document.createTextNode("Distance: " + formatDistance(this.getDistance()) + " Miles"));
		}

		return htmlElement;
	}



	/*
	* Function to retrieve the corresponding value from its field name from the
	* matrix of field name-value pairs.
	* Pre-Condition: The field name must exist.
	* Post-Condition: The value of the corresponding field name is returned
	*
	* @param		name - a field name
	* @return	    The corresponding value if the field name is found, null if not.
	* @author       Seisan Consulting   2-14-2007
	*/
	this.getField = function(name){
		for(var i=0;i< this.fieldValues.length; i++){
			if(this.fieldValues[i][0] == name){
				return this.fieldValues[i][1];
			}
		}
		return null;
	}

	/*
	* Function to build a matrix of field names and corresponding field values when the object is created.
	*
	*
	* @author       Seisan Consulting   2-14-2007
	*/
	if(recordset){
			var fields = recordset.getFieldNames();
			var field;
			for(var i=0; i < fields.getSize(); i++){
				field = fields.getAt(i);
				this.fieldValues.push(new Array(field.toUpperCase(), recordset.getField(field)));
			}
			this.setId(this.getField("I"));
			this.setName(this.getField("N"));
			if(this.getField("PHONE") != null){
				this.setPhone(this.getField("PHONE"));
			}
			this.setStreet(this.getField("ADDRESS"));
			this.setCity(this.getField("CITY"));
			this.setState(this.getField("STATE"));
			var postal = this.getField("ZIP");
			if(postal == null)
				postal = this.getField("POSTAL");
			if(postal != null)
				this.setPostalCode(postal);
			var lat = this.getField("LAT");
			var lng = this.getField("LNG");
			this.setMQLatLng(new MQLatLng(lat, lng));
	}

}
