/*
* 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 = "";
	//Location Name or Poi Name
	this.name = "";
	//Location Brand or Poi Brand
	this.brand = "";
	//Location 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;
	//Sunday store open timing
	this.sunopen = null;
	//Sunday store close timing
	this.sunclose = null;
	//Monday store open timing
	this.monopen = null;
	//Monday store close timing
	this.monclose = null;
	//Sat store open timing
	this.satopen = null;
	//Sat store close timing
	this.satclose = null;
	//Tues store close timing
	this.tuesclose = 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.setBrand = function(b){ this.brand  =  b; };
	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;};
	this.setSunOpen = function(so){ this.sunopen = so;};
	this.setSunClose = function(sc){ this.sunclose = sc;};
	this.setMonOpen = function(mo){ this.monopen = mo;};
	this.setMonClose = function(mc){ this.monclose = mc;};
	this.setSatOpen = function(sto){ this.satopen = sto;};
	this.setSatClose = function(stc){ this.satclose = stc;};
    this.setTuesClose = function(tue){ this.tuesclose = tue;};
	

	//Helper functions for retrieving additional values
	this.getId = function(){ return this.id; };
	this.getName = function(){ return this.name; };
	this.getBrand = function(){ return this.brand; };
	this.getDistance = function(){ return this.distance;};
	this.getRouteTime = function(){ return this.time;};
	this.getRouteDistance = function(){ return this.rDistance;};
	this.getPhone = function(){ return this.phone;};
    this.getSunOpen = function(){ return this.sunopen;};
    this.getSunClose = function(){ return this.sunclose;};
    this.getMonOpen = function(){ return this.monopen;};
    this.getMonClose = function(){ return this.monclose;};
    this.getSatOpen = function(){ return this.satopen;};
    this.getSatClose = function(){ return this.satclose;};
    this.getTuesClose = function(){ return this.tuesclose;};


	/*
	* 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);
	}

	this.getCurrentState = function() {
	    var state = "";
	    state = this.getState();
	    return StringFunctions.capitalize(state);
	}


	/*
	* 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;
	    }

	    //alert('getState [' + this.getState() + ']');

	    if (this.getState() != "") {
	        if (spacer) {
	            str += ", ";
	        }
	        str += this.getState();
	        spacer = true;

	    }

	    if (this.getPostalCode() != "") {
	        if (spacer) {
	            str += " ";
	        }
	        str += this.getPostalCode();
	        spacer = true;
	    }

	    //alert('str ['+str+']');
	    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).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
	*/
	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()));
		}

        //alert ('getPhone ['+this.getPhone()+']');
		if(this.getPhone() != ""){
			htmlElement.appendChild(document.createElement("br"));
			htmlElement.appendChild(document.createTextNode("Phone: " + this.getPhone()));
			htmlElement.appendChild(document.createElement("br"));
		}
		
		if(this.getSunOpen() != ""){
//			htmlElement.appendChild(document.createElement("br"));
//			htmlElement.appendChild(document.createTextNode(this.getSunOpen()));
//			htmlElement.appendChild(document.createElement("br"));
		}
		if(this.getSunClose() != ""){
//			htmlElement.appendChild(document.createElement("br"));
//			htmlElement.appendChild(document.createTextNode(this.getSunClose()));
//			htmlElement.appendChild(document.createElement("br"));
		}
		if(this.getMonOpen() != ""){
//			htmlElement.appendChild(document.createElement("br"));
//			htmlElement.appendChild(document.createTextNode(this.getMonOpen()));
//			htmlElement.appendChild(document.createElement("br"));
		}
		if(this.getMonClose() != ""){
//			htmlElement.appendChild(document.createElement("br"));
//			htmlElement.appendChild(document.createTextNode(this.getMonClose()));
//			htmlElement.appendChild(document.createElement("br"));
		}
		if(this.getSatOpen() != ""){
//			htmlElement.appendChild(document.createElement("br"));
//			htmlElement.appendChild(document.createTextNode(this.getSatOpen()));
//			htmlElement.appendChild(document.createElement("br"));
		}
		if(this.getSatClose() != ""){
//			htmlElement.appendChild(document.createElement("br"));
//			htmlElement.appendChild(document.createTextNode(this.getSatClose()));
//			htmlElement.appendChild(document.createElement("br"));
		}

		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");
			if(isPoiSearch){
				//If PoiSearch is true, the result is a Hotel Address
				a.href = "javascript:submitForm('frm" + num + "', 'searchlocation.html');"
			}
			else {
				//If PoiSearch is false, the result is a Point of Interest
				a.href = "javascript:submitForm('frm" + num + "', 'surroundingarea.html');"
			}
			if(isPoiSearch)
				a.appendChild(document.createTextNode("Find Hotels"));
			else
				a.appendChild(document.createTextNode("Surrounding Area"));
			htmlElement.appendChild(a);

			if(!isPoiSearch){
				htmlElement.appendChild(document.createElement("br"));
				var a = document.createElement("a");
				a.href = "details.html?pid=" + this.getId();
				a.appendChild(document.createTextNode("Property Overview"));
				htmlElement.appendChild(a);
			}
            */

			htmlElement.appendChild(document.createElement("br"));
			var a = document.createElement("a");
			a.href = "javascript:submitForm('frm" + num + "', 'drivingdirection.aspx');"
			a.appendChild(document.createTextNode("Driving Directions"));
			htmlElement.appendChild(a);

                      
			htmlElement.appendChild(document.createElement("br"));
			a = document.createElement("a");
			a.href = "StoreLocator.aspx";
			a.appendChild(document.createTextNode("Search Again"));
			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", "txtName", this.getName());
		frm.appendChild(input);

		input = createInputElement("hidden", "txtBrand", this.getBrand());
		frm.appendChild(input);

		input = createInputElement("hidden", "txtAddress", this.getStreet());
		frm.appendChild(input);

		input = createInputElement("hidden", "txtCity", this.getCity());
		frm.appendChild(input);

		input = createInputElement("hidden", "selStateProvince", this.getState());
		frm.appendChild(input);

		input = createInputElement("hidden", "txtPostalCode", 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", "hdnType", type);
		frm.appendChild(input);

		input = createInputElement("hidden", "rdoUnit", DEFAULT_UNIT);
		frm.appendChild(input);

		input = createInputElement("hidden", "txtDistance", 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("BRAND"));
			this.setBrand(this.getField("BRAND"));
			if(this.getField("PHONE_NUM") != null)
				this.setPhone(this.getField("PHONE_NUM"));
				
			if(this.getField("SUN_OPEN_HOUR") != null)
				this.setSunOpen(this.getField("SUN_OPEN_HOUR"));
			if(this.getField("SUN_CLOSE_HOUR") != null)
				this.setSunClose(this.getField("SUN_CLOSE_HOUR"));
				
			if(this.getField("MON_OPEN_HOUR") != null)
				this.setMonOpen(this.getField("MON_OPEN_HOUR"));
			if(this.getField("MON_CLOSE_HOUR") != null)
				this.setMonClose(this.getField("MON_CLOSE_HOUR"));
				
			if(this.getField("SAT_OPEN_HOUR") != null)
				this.setSatOpen(this.getField("SAT_OPEN_HOUR"));
			if(this.getField("SAT_CLOSE_HOUR") != null)
				this.setSatClose(this.getField("SAT_CLOSE_HOUR"));			

            if(this.getField("TUES_CLOSE_HOUR") != null)
				this.setTuesClose(this.getField("TUES_CLOSE_HOUR"));					
				
			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));
	}

}
