/*
* 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
*/
DataManagerRecord.prototype = new MQGeoAddress();
DataManagerRecord.prototype.constructor = DataManagerRecord;
function DataManagerRecord(recordset){
	MQGeoAddress.call(this);
	
	//Address locaion id
	this.id = "";
	//Hotel Name or Poi Name
	this.name = "";
	//Hotel Phone or 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 title of the infowindow based on availability of address data (Name, Street, etc) for a MQPoi.
	*
	* @return		The title for the infowindow
	*/
	this.getInfoTitleHTML = function(){
		var title = "";
		//If the name and street does not exist, use the City, State and Postal Code
		if(this.getName() == "" && this.getStreet() == "")
			title = this.getCityStatePostalCodeString();
		else if(this.getName() == "")
			title = this.getStreet();
		else
			title = this.getName();
		return StringFunctions.capitalize(title);
	}


	/*
	* Function to set the title of the infowindow based on availability of address data (Name, Street, etc) for a MQPoi.
	* @return		The title for the infowindow
	*/
	this.getInfoTitleElement = function(){
		var htmlElement = document.createElement("div");
		htmlElement.appendChild(document.createTextNode(this.getInfoTitleHTML()));
		return htmlElement;
	}

	/*
	* 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.
	*/
	this.getCityStatePostalCodeString = function(){
		var str = "";
		var spacer = false;
		if(this.getCity() != ""){
			str += StringFunctions.capitalize(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
	*/
	this.getInfoContentHTML = function(num, isPoiSearch){
		return this.getInfoContentElement(num, isPoiSearch).innerHTML;
	}

	/*
	* 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
	*/
	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"));
		}
		if(num >= 0){
			htmlElement.appendChild(document.createElement("br"));
			var a = document.createElement("a");
			htmlElement.appendChild(document.createElement("br"));
			var a = document.createElement("a");
			a.href = "javascript:c._dd_POI('"+this.getStreet()+"','"+this.getPostalCode()+"','"+this.getCity()+"','"+this.getState()+"','"+this.getCountry()+"','"+this.getMQLatLng().getLatitude()+"','"+this.getMQLatLng().getLongitude()+"','1');";
			a.appendChild(document.createTextNode("Driving Directions"));
			htmlElement.appendChild(a);
		}
		return htmlElement;
	}
	/*
	* Function to build a single line string containing basic address information.
	* Contains Street, City, State, Postal Code.
	*
	* @return		str- a string containing the Address's Street, City, State and Postal Code.
	*/
	this.getSingleLineHTML = function(){
		var str = "";
		var spacer = false;
		if(this.getStreet() != ""){
			str += StringFunctions.capitalize(this.getStreet());
			str += " ";
		}
		str += this.getCityStatePostalCodeString();

		return str;
	}

	/*
	* Function to create a hidden html form element containing address information.
	*
	* @param		i - index of the result
	* @return		frm- a html hidden form element
	*/
	this.getResultForm = function(i){
		var frm, input;
		frm = document.createElement("form");
		frm.method="get";
		frm.action = "";
		frm.id = "frm" + i;
		
	
		input = createInputElement("hidden", "n0", this.getName());
		frm.appendChild(input);

		input = createInputElement("hidden", "a0", this.getStreet());
		frm.appendChild(input);

		input = createInputElement("hidden", "c0", this.getCity());
		frm.appendChild(input);

		input = createInputElement("hidden", "s0", this.getState());
		frm.appendChild(input);

		input = createInputElement("hidden", "z0", this.getPostalCode());
		frm.appendChild(input);

		input = createInputElement("hidden", "hdnlatitude", this.getMQLatLng().getLatitude());
		frm.appendChild(input);

		input = createInputElement("hidden", "hdnlongitude", this.getMQLatLng().getLongitude());
		frm.appendChild(input);

		input = createInputElement("hidden", "r0", DEFAULT_RADIUS);
		frm.appendChild(input);

		return frm;
	}


	/*
	* 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.
	*/
	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.
	*/
	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 MQA.LatLng(lat, lng));
			this.setCountry(this.getField("COUNTRY"));
	}

}
