var glob_listRun_escapeContents=false;
var glob_highlightPriceQueries=true;
function setRowCells(runIndex,showPriceQueries){
		currentCellIndex=0;
		var thisRun=run[runIndex];
		setDateCells(0,thisRun,true);
		setDurationCell(3,thisRun);
		setDateCells(4,thisRun,false);
		if (glob_avail0edit1==1){
			setPriceCell(7, thisRun, showPriceQueries);//glob_colIndex_price
			setAvailCell(8, thisRun);
		} else {
			setPriceCellAvail(7, thisRun);//glob_colIndex_price
			if (bookMode1form0none==1){
				setBookNowCell(8, thisRun);
			}
		}
	}


function colStruc(colIndex,silentLeft, silentRight, colDesc, cellClass, borderDesc){//borderDesc can be empty/missing , Left, Right or LeftRight
	if (arguments.length<6){
		var borderDesc = "";
	}
	this.colIndex=colIndex;
	this.colDesc=colDesc;// colDesc - just for documentation
	this.silentLeft=silentLeft;
	this.silentRight=silentRight;
	this.hasLeftBorder=false;//default
	this.hasRightBorder=false;//default
	this.cellClassName=cellClass;
	this.hasLeftBorder=(borderDesc.indexOf("Left")>-1);
	this.hasRightBorder=(borderDesc.indexOf("Right")>-1);
	/*if (colIndex==8){
		alert("borderDesc="+borderDesc+", hasRightBorder="+this.hasRightBorder);
	}*/
	this.noTopBorderIfSameAsPrev=(colIndex<jgc_lastNoTopBorderCellIfSameAsPrev);//last no-top-border cell is ArriveYear
}
function setDateCells(currentCellIndex,thisRun,isArrive){
	var tS="";
	var stringCellClassAllParts="";//default
	if (isArrive){
		if (thisRun.isSameAsPrev){
			stringCellClassAllParts+= " ditto"+thisRun.currentRowClass+" ";
		}
		var thisDate=ADNtoDate(thisRun.arriveADN);
		var isSameAsPrev=thisRun.isSameAsPrev;
		var colIndexDate=0;
		var colIndexMonth=1;
		var colIndexYear=2;
	} else {//depart date
		var thisDate=ADNtoDate(aV(thisRun.arriveADN,thisRun.nights));
		var isSameAsPrev=false;//departure date will not be same as immed previous
		var colIndexDate=4;
		var colIndexMonth=5;
		var colIndexYear=6;
	}
	if (dayStringFormat01==1){//full days
		var spanClassWeekday="weekday day"+thisDate.getDay();//different class for each DOW to aid spacing
		var cellClassDayDate=" dateCell ";
	} else {
		if (isArrive){
			//var cellClassDayDate+=" clearLeft dateCellShort1 ";
			var cellClassDayDate=" cellDate ";//was dateCellShort1
		} else {
			var cellClassDayDate=" cellDate ";//was dateCellShort2
		}
		if (thisDate.getDay()==3){
			var spanClassWeekday="weekdayShort day3";
		} else {
			var spanClassWeekday="weekdayShort";
		}
	}
	
	var spanClassArriveOn="arriveText";
	var spanClassDateNumber="dateNo";
	//allert("12 about to go to stringCellBorderClasses with thisRun.runIndex="+thisRun.runIndex +" - colIndex="+colIndexDate);
	var stringCellClassDayDate=stringCellBorderClasses(thisRun,colIndexDate)+stringCellClassAllParts;
	//allert("13 about to go to stringCellBorderClasses with thisRun.runIndex="+thisRun.runIndex +" - colIndex="+colIndexMonth);
	var stringCellClassMonth=stringCellBorderClasses(thisRun,colIndexMonth)+stringCellClassAllParts;
	//allert("14 about to go to stringCellBorderClasses with thisRun.runIndex="+thisRun.runIndex +" - colIndex="+colIndexYear);
	var stringCellClassYear=stringCellBorderClasses(thisRun,colIndexYear)+stringCellClassAllParts;
	//allert("15 back from stringCellBorderClasses ");
	if (!verbose){
		if (!isArrive){
			spanClassWeekday+=" "+" padLeft";
		}
	}
	thisRun.cellClassString[currentCellIndex]=stringCellClassDayDate;
	
	//tS+="<td class='"+stringCellClassDayDate+"'>";
	//tS+=ADN;//DEBUG
	tS = "<span class='"+spanClassWeekday+"'>"+dayName[dayStringFormat01][thisDate.getDay()]+"</span>";
	tS += "<span class='"+spanClassDateNumber+"'>"+thisDate.getDate()+"</span>";
	thisRun.cellInnerHTML[currentCellIndex]=tS;
	//tS+="</td>"+lR();
	//tS+="<span class='dummy'>&nbsp;</span></td>";
	currentCellIndex++;
	
	thisRun.cellClassString[currentCellIndex]=stringCellClassMonth;
	//tS +="<td class='"+stringCellClassMonth+"'>";
	if (isArrive){
		var prefix="monthNameArrive";
	} else {
		var prefix="monthNameDepart";
	}
	tS="<span id='"+prefix+thisRun.runCode+"' >"+obMonthList[monthStringFormat01][aV(thisDate.getMonth(),1)]+"</span>";
	thisRun.cellInnerHTML[currentCellIndex]=tS;
	currentCellIndex++;
	
	//tS+="</td>"+lR();
	thisRun.cellClassString[currentCellIndex]=stringCellClassYear;
	//tS+="<td class='"+stringCellClassYear+"'>";
	tS="<span class=' year "+glob_class_sup+"'>" + thisDate.getFullYear()+"</span>";
	thisRun.cellInnerHTML[currentCellIndex]=tS;
}

function setDurationCell(currentCellIndex,thisRun){
	thisRun.cellClassString[currentCellIndex]=lTrim(stringCellBorderClasses(thisRun,3));
	var tS="<span class='span_stayNights'>"+thisRun.nights+"</span>";
	tS+="<span class='stayText2 "+glob_class_sup+"'> nights</span>";
	thisRun.cellInnerHTML[currentCellIndex]=tS;
}
function setCellsAndRepaint_TableRow(thisRunIndex,thisRowIndex, addDividerBottom, skipCellSetting, callingRef){
	//alert("810: setCellsAndRepaint_TableRow called with thisRunIndex="+thisRunIndex+" and callingRef="+callingRef+lR()+"table before change:"+lR()+debug_stringRunTableList());
	var thisRun=run[thisRunIndex];
	if (skipCellSetting){
		//allert("840: NOT calling setRowCells for thisRunIndex="+thisRunIndex+lR()+"skipCellSetting="+skipCellSetting);
	} else {
		//allert("841: calling setRowCells for thisRunIndex="+thisRunIndex);
		setRowCells(thisRunIndex,glob_highlightPriceQueries);
	}
	var tBod=gObj("mainTableBody");
	if (tBod.rows.length>thisRowIndex){
		tBod.deleteRow(thisRowIndex);
		//allert("811 DELETEROW: thisRunIndex="+thisRunIndex+lR()+debug_stringRunTableList());
	} else {
		//allert("812: thisRunIndex="+thisRunIndex+lR()+"NO ROW TO DELETE"+lR()+debug_stringRunTableList());
	}
	//if (thisRunIndex>27){
		//allert("813: thisRunIndex="+thisRunIndex+lR()+"run.length="+run.length+lR()+debug_stringRunTableList());
	//}
	var tr = tBod.insertRow(thisRowIndex);
	if (thisRunIndex=tBod.rows.length-1){
		//last row
		//allert("814: LAST ROW"+lR()+"date="+thisRun.dateString+lR()+debug_stringRunTableList());
	}
	setThisAttribute(tr, "id","run"+thisRun.runCode);
	//tr.setAttribute("id",thisRun.runCode);
	
	setThisAttribute(tr, "class","row"+thisRun.currentRowClass);
	//tr.setAttribute("class","row"+thisRun.currentRowClass);
	for (var a=0; a<thisRun.cellClassString.length; a++){
		var td=tr.insertCell(a);
		//td.innerHTML =thisRun.cellInnerHTML[a];
		//td.setAttribute("class",thisRun.cellClassString[a]);
		repaint_TableCell(td,thisRun,a,addDividerBottom,true);
	}
	
	//gObj("mainTable").style.width="51em";
	//allert("815: at end of setCellsAndRepaint_TableRow - first 3 cells are:"+lR()+debug_stringRunTableList());
}
function repaint_TableCell(thisTableCell,thisRun,thisCellIndex, addDividerBottom,changeHTML){
	/*if (thisCellIndex==glob_colIndex_price){
		alert("791: setting HTML to ~"+thisRun.cellInnerHTML[thisCellIndex]+"~ for runIndex="+thisRun.runIndex+lR()+"currentHTML is ~"+thisTableCell.innerHTML+"~");
	}*/
	if (changeHTML){
		thisTableCell.innerHTML =thisRun.cellInnerHTML[thisCellIndex];
	}
	var thisCellClassString=thisRun.cellClassString[thisCellIndex];
	if (addDividerBottom){
		thisCellClassString+=" dividerBottom"
	}
	setThisAttribute(thisTableCell,"class",thisCellClassString);
	//thisTableCell.setAttribute("class",thisRun.cellClassString[thisCellIndex]);
}
function setThisAttribute(node, thisName, thisValue) {
	if ((navigator.appName == 'Microsoft Internet Explorer')||(navigator.appName == 'Microsoft Internet Explorer')){
		for (var i = 0; i < node.attributes.length; i++){
			if (node.attributes[i].name == thisName){
				node.attributes[i].value = thisValue;
				break;
			}
		}
	} else {
		//td.innerHTML =thisRun.cellInnerHTML[a];
		node.setAttribute(thisName,thisValue);
		
	}
}
function debug_stringRunTableList(){
	var tS="";
	var tBod=gObj("mainTableBody");
	for (var a=0; a<tBod.rows.length; a++){
		tS+="Row "+ a+":";
		if (tBod.rows[a].cells.length==0){
			tS+="[EMPTY ROW]";
		} else {
			for (var b=0; b<3; b++){
				if (tBod.rows[a].cells[b]){
					tS+=tBod.rows[a].cells[b].innerHTML+" ";
				} else {
					tS+="[no element "+b+"] ";
				}
			}
		}
		tS+=lR();
	}
	return tS;
}
function listRun(arrayRef, repeatHeaderCount,colCountBeforeRepeatingCol0){
	//alert("885: listRun");
	//repeatHeaderCount=2;repaint header row every x rows
	//colCountBeforeRepeatingCol0=6; repeat column 0 every y columns
	if (arrayRef==1){
		var thisArray=run;
	}
	if (arrayRef==2){
		var thisArray=page;
	}
	if (arrayRef==3){
		var thisArray=mainTableStruc;
	}

	//alert("886: array length is "+thisArray.length);
	var headerRow="<tr>";
	var a=0;
	/*for (property in thisArray[0]){
		//run through first row to get name each of attributes
		if (a==0){
			var headerProperty=property;
		}
		if (a % colCountBeforeRepeatingCol0==0){
			headerRow+="<td class='debugContrast'>";
			headerRow+=headerProperty;
			headerRow+="</td>";
		}
		if (a>0){ 
			headerRow+="<td class='debugContrast'>";
			headerRow+=property;
			headerRow+="</td>";
		}
		a++;
	}
	headerRow+="</tr>";*/
	//allert("headerRow="+headerRow);
	var tS="<table border='1'>";
	//for (var a=0; a<thisArray.length; a++){
	/*for (a in thisArray){
		if (a % repeatHeaderCount == 0){
			tS+=headerRow;
		}
		tS+="<tr>";
		var b=0;
		for (property in thisArray){
			if (b==0){
				var colZeroString=entryCell(thisArray[a][property],true);
			}
			if (b % colCountBeforeRepeatingCol0==0){
				tS+=colZeroString;
			}
			if (b>0){
				//if (isArray(thisArray[a])){
					if (isArray(thisArray[a][property])){
						tS+="<td>"+arrayListString(thisArray[a][property])+"</td>";
					} else {
						tS+=entryCell(thisArray[a][property]);
					}
				//} else {
				//tS+=entryCell(thisArray[a]);
				//}
			}
			b++;
		}
		tS+="</tr>"
	}*/
	var a=0;
	var headerRow="<tr>";
	for (element in thisArray){
		for (property in thisArray[element]){
			//run through first row to get name each of attributes
			if (a==0){
				var headerProperty=property;
			}
			if (a % colCountBeforeRepeatingCol0==0){
				headerRow+="<td class='debugContrast'>";
				headerRow+=headerProperty;
				headerRow+="</td>";
			}
			if (a>0){ 
				headerRow+="<td class='debugContrast'>";
				headerRow+=property;
				headerRow+="</td>";
			}
			a++;
		}
		break;//only interested in first element, as others will have same structure
	}
	headerRow+="</tr>";
	var index=0;
	//alert("887 headerRow="+headerRow);
	for (a in thisArray){
		if (index % repeatHeaderCount == 0){
			tS+=headerRow;
		}
		tS+="<tr>";
		var b=0;
		for (property in thisArray[a]){
			if (b==0){
				var colZeroString=entryCell(thisArray[a][property],true);
			}
			if (b % colCountBeforeRepeatingCol0==0){
				tS+=colZeroString;
			}
			if (b>0){
				//if (isArray(thisArray[a])){
					if (isArray(thisArray[a][property])){
						tS+="<td>";
						if (glob_listRun_escapeContents){
							tS+=jgChangeTags(arrayListString(thisArray[a][property]));
						} else {
							tS+=arrayListString(thisArray[a][property]);
						}
						tS+="</td>";
					} else {
						if (glob_listRun_escapeContents){
							tS+=jgChangeTags(entryCell(thisArray[a][property]));
						} else {
							tS+=entryCell(thisArray[a][property]);
						}
					}
				//} else {
				//tS+=entryCell(thisArray[a]);
				//}
			}
			b++;
		}
		tS+="</tr>";
		index++;
	}
	tS+="</table>";
	//gObj("debug"+arrayRef).innerHTML=tS;
	writeInnerHTML(gObj("debug"+arrayRef),tS, "listRun");
}
function jgChangeTags(inString){
	var regExp=/\</g;
	var newString=String.fromCharCode(123);
	inString = inString.replace(regExp,newString);
	var regExp=/\>/g;
	var newString=String.fromCharCode(125);
	inString = inString.replace(regExp,newString);
	return inString;
}

function padStringLeft(inVal,nDigits,padChar){
	var inValString=inVal.toString();
	for (a= inValString.length; a<nDigits;a++){
		inValString=padChar+inValString
	}
	return inValString;
}

