
function Product(xml) {

 this.toString	= function() { return "[object Product]"; };

 this.xml		= xml;
 this.name		= "";
 this.category	= "";
 this.datafile	= "";
 this.image		= "";
 this.imagesm	= "";
 this.imageth	= "";
 this.unit		= "";
 this.banner	= "";
 this.popupid	= "";
 this.calendar;
 this.dates		= new Array();
 this.desc		= new Array();
 this.custom	= new Array();
 this.checklist	= new Array();
 this.rates		= new Array();
 this.available	= new Array();
 this.details	= new Array();
// this.available	= 1;
 this.onClickDate	= new Function();
 this.selclick	= new Function();

 this.setPopupid	= function(popupid) {
	this.popupid	= popupid;
 };

 this.generateRow	= function(row,options) {
	row.Product	= this;
	row.date	= options["currdate"];
	row.onclick	= function() { 
		this.Product.displayDescription(); 
	};
	row.className	= "clickable";
	var selCell	= row.insertCell(-1);
	var imgCell	= row.insertCell(-1);
	var titCell	= row.insertCell(-1);
	var desCell	= row.insertCell(-1);
	var rateCell	= row.insertCell(-1);
	var calCell	= row.insertCell(-1);
	// Building Select Cell
	var checkbox	= document.createElement("IMG");
	    checkbox.row	= row;
	    checkbox.src	= "/Icons/checkbox.png";
		checkbox.title	= "add to cart";
		checkbox.selclick	= this.selclick;
	    checkbox.onmouseover	= function() { this.src = (this.checked) ? "/Icons/checkbox_selected_hovered.png" : "/Icons/checkbox_hovered.png"; };
	    checkbox.onmouseout		= function() { this.src = (this.checked) ? "/Icons/checkbox_selected.png" : "/Icons/checkbox.png"; };
	    checkbox.onclick		= function() { 
			if (this.checked) {
				this.checked	= 0;
				this.src	= "/Icons/checkbox.png";
			} else {
				this.checked	= 1;
				this.src	= "/Icons/checkbox_selected.png";
			}
			this.selclick(this.checked);
	    };
/*		if (!this.available) {
	    	checkbox.src	= "/Icons/checkbox_disabled.png";
			checkbox.title	= "Not available";
			checkbox.onmouseover = "";
			checkbox.onmouseout = "";
			checkbox.onclick = "";
		}
*/
	selCell.style.verticalAlign	= "middle";
	if (options["select"]) {
		selCell.appendChild(checkbox);
	}
	// Building Image Cell
	var image	= document.createElement("IMG");
	    image.style.width	= "75px";
	    image.style.height	= "75px";
	    image.src	= this.imagesm;
		image.title	= "click to enlarge";
		image.alt	= this.name;
		image.row	= row;
		image.date	= options["currdate"];
	    image.displayDescription	= this.displayDescription;
/*
		image.onclick	= function() {
			if (this.style.position == "absolute") {
				this.style.top	= "";
//				this.style.top	= getObjTop(this.row.parentNode); //.offsetTop + "px";
				this.src	= this.srcsm;
				this.title	= "click to enlarge";
				this.style.zIndex	= 1;
				this.style.position	= "relative";
				this.style.height	= "75px";
				this.style.width	= "75px";
			} else {
				this.src	= this.srclg;
				this.title	= "click to close";
				this.style.zIndex	= 100;
				this.style.position	= "absolute";
				this.style.top	= getObjTop(this.row.parentNode);
				this.style.height	= "500px";
				this.style.width	= "500px";
			}
		};
*/
	imgCell.appendChild(image);
	// Building Title Cell
	var title	= document.createElement("SPAN");
	    title.innerHTML	= this.name;
	    title.style.textAlign	= "center";
	    title.style.fontWeight	= "bold";
	var banner	= document.createElement("SPAN");
	    banner.innerHTML	= this.banner;
	    banner.style.textAlign	= "center";
	titCell.appendChild(title);
	titCell.appendChild(document.createElement("BR"));
	titCell.appendChild(banner);
	titCell.style.textAlign	= "center";
	// Building Calendar Cell
	var calDiv	= document.createElement("DIV");
	    calDiv.id	= this.datafile.replace(".xml","") + this.unit + "calendar";
	if (options["calendar"]) {
		calCell.appendChild(calDiv);
	}
	var cal	= new Calendar(calDiv);
	    cal.row	= row;
	    cal.reservations	= new Reservation();
	    cal.reservations.setEventFile(this.datafile);
		cal.onClickDate	= this.onClickDate;
	    cal.onLoad	= function() {
	    	this.reservations.month = this.MONTH;
	    	this.reservations.year = this.YEAR;
	    	this.reservations.setDateElementArray(this.getCells());
		    this.reservations.loadEvents();
		};
	    cal.init();
	this.calendar	= cal;
	var rate	= document.createElement("SPAN");
	    rate.innerHTML	= (CURRDATESTR) ? this.getRate(CURRDATESTR) : "select date";
	if (options["rate"]) {
		rateCell.appendChild(rate);
	}
	return row;
 };

 this.delAvailableDate	= function(date) {
	this.dates[date]	= new Array();
	this.dates[date]["available"]	= 0;
 };

 this.addAvailableDate	= function(date) {
	this.dates[date]	= new Array();
	this.dates[date]["available"]	= 1;
 };

 this.delAvailableDate	= function(date) {
	this.dates[date]	= new Array();
	this.dates[date]["available"]	= 0;
 };

 this.isAvailable	= function(date) {
	if (!date) { return 1; }
	if (!this.dates[date]) { return 0; }
	if (this.dates[date]["available"]) { return 1; }
	return 0;
 };

 this.isCategory	= function(category) {
	if (category == "All") { return 1; }
	if (category == this.category) { return 1; }
	return 0;
 };

 this.getRate	= function(date) {
	var intDofW = str2Date(date).getDay();
	date	= parseInt(date);
	for (var i = 0; i < this.rates.length; i++) {
		var Rate    = this.rates[i];
		var rPrice  = Rate["Price"];
		var rDofW   = Rate["DofW"];
		   if (rDofW.substr(intDofW,1) != "1") { continue; }
		var rRanges = Rate["Range"].split("-");
		   if (date < parseInt(rRanges[0])) { continue; }
		   if (date > parseInt(rRanges[1])) { continue; }
//	 var rPrice  = Rate.getAttribute("price");
		return rPrice;
    }
 };

 this.displayAddToCart	= function(descTab) {
//	var product	= row.Product;
	var product	= this;
//	var date	= row.date;
	var date	= CURRDATESTR;
	var emptyrow	= descTab.insertRow(-1);
	var cartrow	= descTab.insertRow(-1);
	var addcartCell	= cartrow.insertCell(-1);
	    addcartCell.colSpan	= 2;
	    addcartCell.style.textAlign	= "center";
	var cartImage;
	if (document.getElementById("currcart").style.display != "none") {
		cartImage	= document.createElement("SPAN");
/*
		cartImage.style.fontWeight	= "bold";
		cartImage.innerHTML	= "Save Changes";
		cartImage.className	= "clickable";
		addcartCell.style.border	= "double 3px";
		cartImage.onclick	= this.displayCart;
*/
	} else if (Cart.isInCart(product,date)) {
		cartImage	= document.createElement("SPAN");
		cartImage.style.fontWeight	= "bold";
		cartImage.innerHTML	= "Item in cart<BR>Click to view";
		cartImage.className	= "clickable";
		addcartCell.style.border	= "double 3px";
		addcartCell.style.fontSize	= "14px";
		cartImage.onclick	= this.displayCart;
	} else {
		cartImage	= document.createElement("IMG");
		cartImage.src	= "/Icons/cart_red_add.gif";
		cartImage.className	= "clickable";
		cartImage.product	= product;
		cartImage.date	= date;
		cartImage.onclick	= function() {
			var options	= new Array();
			options["date"] = this.date
			options["price"] = this.product.getRate(this.date);
			options.custom	= new Array();
			for (var cIdx = 0; cIdx < this.product.custom.length; cIdx++) {
				var custopt	= new Object();
				for (var f in this.product.custom[cIdx]) {
					custopt[f] = this.product.custom[cIdx][f];
				}
				options.custom.push(custopt);
			}
		   	Cart.addItem(this.product,options);
			this.product.displayDescription();
//			this.style.display	= "none";
		};
	}
	addcartCell.appendChild(cartImage);
 };

 this.displayDescription	= function(Options) {
	if (!Options) { Options	= new Object(); }
	var descDiv	= this.deTarget;
//	    descDiv.style.padding	= "2px";
//	    descDiv.style.border	= "solid thin black";
	    descDiv.style.display	= "";
	    descDiv.innerHTML	= "";
	    descDiv.Product	= this;
	if (Options.float) {
		descDiv.style.position	= "absolute";
		descDiv.style.background	= "#FFFFFF";
	} else {
		descDiv.style.position	= "";
		descDiv.style.background	= "transparent";
	}
	// create Close button
	var closeTable	= document.createElement("TABLE");
	closeTable.width	= "100%";
	closeTable.style.borderBottom	= "solid thin black";
	var closeCell	= closeTable.insertRow(-1).insertCell(-1);
	closeCell.style.textAlign	= "right";
	closeCell.innerHTML	= "close";
	closeCell.descDiv	= descDiv;
	closeCell.className	= "clickable";
	closeCell.onclick	= function() { this.descDiv.style.display = "none"; }
	descDiv.appendChild(closeTable);
	// create description table
	var gridTable	= document.createElement("TABLE");
//gridTable.style.border = "outset 3px black";
gridTable.width	= "100%";
	var row1	= gridTable.insertRow(-1);
	var row2	= gridTable.insertRow(-1);
	var titleCell	= row1.insertCell(-1);
	    titleCell.colSpan	= 2;
	    titleCell.style.textAlign	= "center";
	var descCell	= row2.insertCell(-1);
	var imgCell		= row2.insertCell(-1);
	var title	= document.createElement("SPAN");
	    title.className	= "title";
	    title.innerHTML 	= this.name;
//	    title.style.textAlign	= "center";
//	    title.style.fontWeight	= "bold";
//	    title.style.fontSize	= "30px";
	var image	= document.createElement("IMG");
	    image.src 	= this.image
	    image.style.width	= "250px";
	    image.style.height	= "250px";
	var descTab	= document.createElement("TABLE");
	    descTab.id	= "productDetailsTable";
	    descTab.className	= "description";
	for (var fIdx = 0; fIdx < this.desc.length; fIdx++) {
		var fldObj	= this.desc[fIdx];
		var field	= fldObj.field;
		var value	= fldObj.value.toString();
		value	= value.replace(/,/g,"<BR>");
		var dRow	= descTab.insertRow(-1);
		var fCell	= dRow.insertCell(-1);
		    fCell.className = "field";
		    fCell.innerHTML	= field;
//		    fCell.style.fontWeight	= "bold";
//		    fCell.style.textAlign	= "right";
//		    fCell.style.verticalAlign	= "middle";
		var vCell	= dRow.insertCell(-1);
		    vCell.className = "value";
		    vCell.innerHTML	= value;
	}
	if (CURRDATESTR) {
		var rRow	= descTab.insertRow(-1);
		    rRow.style.color = "#FF0000";
		var fCell	= rRow.insertCell(-1);
		    fCell.className = "field";
		    fCell.innerHTML	= "Rate";
//		    fCell.style.fontWeight	= "bold";
//		    fCell.style.textAlign	= "right";
//		    fCell.style.verticalAlign	= "middle";
		var vCell	= rRow.insertCell(-1);
		    vCell.className = "value";
		    vCell.innerHTML	= this.getRate(CURRDATESTR);
//		this.displayAddToCart(gridTable);
		this.displayAddToCart(descTab);
	} else {
		var rRow	= descTab.insertRow(-1);
		    rRow.style.color = "#FF0000";
		var fCell	= rRow.insertCell(-1);
		    fCell.colSpan	= 2;
		    fCell.className = "field";
		    fCell.innerHTML	= "Choose a date to see price";
	}
	descDiv.appendChild(gridTable);
	titleCell.appendChild(title);
	imgCell.appendChild(image);
	descCell.appendChild(descTab);
	descDiv.appendChild(this.displayOptions(Options.cartitem));
	descDiv.appendChild(this.displayChecklist());
 };

 this.displayChecklist	= function() {
	var checkItems	= this.checklist;
	var checkDiv	= document.createElement("DIV");
	if (!checkItems.length) { return checkDiv; }
	var title	= document.createElement("SPAN");
	title.className	= "title";
	title.innerHTML 	= "Checklist";
	var checkList	= document.createElement("UL");
	checkList.className	= "checklist";
	for (var cIdx = 0; cIdx < checkItems.length; cIdx++) {
		var checkItem	= checkItems[cIdx];
		var listItem	= document.createElement("LI");
		listItem.className	= "item";
		listItem.innerHTML	= checkItem;
		checkList.appendChild(listItem);
	}
	checkDiv.appendChild(title);
	checkDiv.appendChild(checkList);
	return checkDiv;
 };

 this.displayOptions	= function(cartitem) {
	var item	= this;
	if (cartitem) {
		item	= cartitem.Options;
	}
	var options	= item.custom;
	var gridTable	= document.createElement("TABLE");
	gridTable.className	= "options";
	if (!options.length) { return gridTable; }
//	gridTable.style.border	= "solid thin black";
	gridTable.width	= "100%";
	var row1	= gridTable.insertRow(-1);
	var row2	= gridTable.insertRow(-1);
	var titleCell	= row1.insertCell(-1);
	var optionCell	= row2.insertCell(-1);
	titleCell.style.textAlign	= "center";
	var title	= document.createElement("SPAN");
	title.className	= "title";
	title.innerHTML 	= "Options";
	titleCell.appendChild(title);
	// create Options table
	var optionTab	= document.createElement("TABLE");
	for (var oIdx = 0; oIdx < options.length; oIdx++) {
		var row	= optionTab.insertRow(-1);
		var option	= options[oIdx];
		var fldCell	= row.insertCell(-1);
		var valCell	= row.insertCell(-1);
		fldCell.className	= "field";
		valCell.className	= "value";
//		fldCell.style.fontWeight	= "bold";
//		fldCell.style.textAlign	= "right";
//		fldCell.style.verticalAlign	= "middle";
		fldCell.innerHTML	= option.display;
		var valDiv	= document.createElement("DIV");
		if (option.type == "text") {
			var input	= document.createElement("Input");
			input.name	= "order" + option.name;
			input.item	= item;
			input.itemIdx	= oIdx;
			input.onchange	= function() {
				this.item.custom[this.itemIdx].value	= this.value;
			};
			if (option.values.length) {
				input.value	= option.values[0].value;
			}
			valDiv.appendChild(input);
		} else if (option.type == "dropdown") {
			var select	= document.createElement("Select");
			for (var vIdx = 0; vIdx < option.values.length; vIdx++) {
				var valObj	= option.values[vIdx];
				var value	= valObj.value;
				var display	= valObj.display;
				select.options[vIdx]  = new Option(display,value)
				if (option.value) {
					select.value    = option.value;
				}
			}
			if (option.defVal) {
				select.value	= option.defVal;
			}
			select.item	= item;
			select.itemIdx	= oIdx;
			select.onchange	= function() {
				this.item.custom[this.itemIdx].value	= this.value;
//alert(this.item.name);
			};
			valDiv.appendChild(select);
		} else if (option.type == "radio") {
			for (var vIdx = 0; vIdx < option.values.length; vIdx++) {
				var radio	= document.createElement("Input");
				radio.type	= "radio";
				radio.value	= option.values[vIdx];
				radio.name	= option.name;
				radio.item	= item;
				radio.itemObj	= oIdx;
				radio.onclick	= function() {
					this.itemObj.custom[this.itemIdx].value	= this.value;
				};
				var text	= document.createElement("SPAN");
				text.innerHTML	= option.values[vIdx];
				valDiv.appendChild(radio);
				valDiv.appendChild(text);
			}
		}
//		valCell.style.fontWeight	= "bold";
//		valCell.style.textAlign	= "left";
//		valCell.style.verticalAlign	= "middle";
		valCell.appendChild(valDiv);
	}
	optionCell.appendChild(optionTab);
	return gridTable;
 };

 this.init	= function() {
	this.category	= getElementValue(this.xml,"Category","");
	this.datafile	= getElementValue(this.xml,"Datafile","");
	this.name		= getElementValue(this.xml,"Name","");
	this.image		= getElementValue(this.xml,"IMG","");
	this.imagesm	= getElementValue(this.xml,"IMG75","");
	this.imageth	= getElementValue(this.xml,"IMG30","");
	this.banner		= getElementValue(this.xml,"Banner","");
	var rateXMLs	= this.xml.getElementsByTagName("Rate");
	for (var rIdx = 0; rIdx < rateXMLs.length; rIdx++) {
		var rateXML	= rateXMLs[rIdx];
		var Rate	= new Array();
		Rate["Type"]	= getElementValue(rateXML,"Type","Unspecified");
		Rate["Price"]	= getElementValue(rateXML,"Price","n/a");
		Rate["DofW"]	= getElementValue(rateXML,"DofW","1111111");
		Rate["Range"]	= getElementValue(rateXML,"Range","00000000-99999999");
		this.rates.push(Rate);
	}
	var desc		= this.xml.getElementsByTagName("Description")[0];
	for (var dIdx = 0; dIdx < desc.childNodes.length; dIdx++) {
		var field	= desc.childNodes[dIdx];
		if (field.nodeName.match("#text")) { continue; }
		var fld = field.nodeName;
		var fldisplay	= field.getAttribute("display");
		if (fldisplay) { fld = fldisplay; }
		var val = field.firstChild.nodeValue;
		var fldObj	= new Object();
		fldObj.field	= fld;
		fldObj.value	= val;
		this.desc.push(fldObj);
	}
	var checktag	= this.xml.getElementsByTagName("Checklist")[0];
	this.checklist	= new Array();
	if (checktag) {
		var checkItems	= checktag.getElementsByTagName("Item");
		for (var cIdx = 0; cIdx < checkItems.length; cIdx++) {
			var checkItem	= checkItems[cIdx];
			this.checklist.push(getElementValue(checkItem,"Text",""));
		}
	}
	var opttag	= this.xml.getElementsByTagName("Options")[0];
	if (opttag) {
		var options	= opttag.getElementsByTagName("Option");
		for (var oIdx = 0; oIdx < options.length; oIdx++) {
			var option	= options[oIdx];
			var name	= getElementValue(option,"name","");
			var display	= getElementValue(option,"display",name);
			var type	= getElementValue(option,"type","input");
//			var valArr	= getElementValues(option,"value","");
			var valTagArr	= option.getElementsByTagName("value");
			var optObj	= new Object();
			optObj.values	= new Array();
			for (var vIdx = 0; vIdx < valTagArr.length; vIdx++) {
				var valTag	= valTagArr[vIdx];
				var value	= new Object();
				value.value	= getTagValue(valTag,"");
				value.display	= valTag.getAttribute("display") || value.value;
				value.surcharge	= valTag.getAttribute("surcharge") || "$0";
				if (valTag.getAttribute("default")) {
					optObj.value	= value.value;
				}
				optObj.values.push(value);
			}
			if (!optObj.value) { optObj.value = (optObj.values.length) ? optObj.values[0].value : ""; }
			optObj.display	= display;
			optObj.name		= name;
			optObj.type		= type;
			this.custom.push(optObj);
		}
	}
 };

 this.init();

 return this;

}

