var expandedRow = null;

function hideExpandedRow() {
	if (expandedRow != null) {
		expandedRow.parentNode.removeChild(expandedRow.nextSibling);
		expandedRow.mode = 0;
		expandedRow = null;
	}
}

function setExpandedRow(row, mode, content) {
	sameRowMode = (expandedRow == row) && (expandedRow.mode == mode);
	hideExpandedRow();

	if (!sameRowMode) {
		row.mode = mode;
		expandedRow = row;
		row.parentNode.insertBefore(content, row.nextSibling);
	}
}

function strikeExpandedRow(loadstatus) {
	if (expandedRow != null) {
        expandedRow.childNodes[0].innerHTML = loadstatus;
        expandedRow.className='hideme';
	}
}

function submitLoadSuccess(oResponse) {
	//alert(oResponse.responseText);
	if (oResponse.responseText.indexOf('Failure') >= 0) {
        if(oResponse.responseText.indexOf('DIRNOLOGIN1208689443'))
            alert("You must be logged in to add flags");
		else alert(oResponse.responseText);
	} else {
		strikeExpandedRow(oResponse.argument.loadstatus);
	}
	hideExpandedRow();
}

function submitLoadInfo(md5, me) {
    //var row = me.ParentNode.ParentNode.ParentNode;
	loadstatus = 'none';
	checkboxes = me.parentNode.parentNode.parentNode.getElementsByTagName('input');
	for (i = 0; i < checkboxes.length; i++) if (checkboxes[i].checked) loadstatus=checkboxes[i].value;

	if (loadstatus != 'none') {
		transaction = df_postLoadStatus({'status':loadstatus, 'md5sum': md5},
			{'success': submitLoadSuccess, 'failure': function (oResponse) {alert('Failure to post load status, try again later.'); hideExpandedRow();}, argument: {loadstatus:loadstatus}}
		);
	}
}

function reportLoadRowContent(row, md5) {
	newCol = document.createElement('td');
	newCol.colSpan = 13;
	newCol.innerHTML =
"<div style='margin-left: 10px; padding: 10px; border: 3px solid black; background-color:gold;'>\n" +
"<b>Please flag this load as:</b><br><table border=0 cellpadding=0 cellspacing=0><tr><td colspan=4 style='height:10px'>" +
"<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input checked type=radio name='loadreport' VALUE='Hidden'><td>&nbsp;<b>Hidden</b>" + "<td>&nbsp;&nbsp;&nbsp;(This load will not work for me, please hide it just for me)<br>" +
"<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=radio name='loadreport' VALUE='Moved'><td>&nbsp;<b>Moved</b>" + "<td>&nbsp;&nbsp;&nbsp;(Load has been moved or is no longer available)<br>" +
"<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=radio name='loadreport' VALUE='Wrong'><td>&nbsp;<b>Wrong</b>" + "<td>&nbsp;&nbsp;&nbsp;(Load and/or contact information is wrong)<br>" +
"</table><br><button  type='button' onclick='submitLoadInfo(\"" + md5 + "\", this)' />Save</button>" + 
"<button  type='button' onclick=\"expandReport(this,'" + md5 + "'); return false;\"' />cancel</button>" + 
"<br><br><i>* by flagging loads, you help save time for yourself and others.&nbsp;</i>" +
"<a id=flaginfo2 onclick=\"return setDIV('panel1b',this.href,{fixedcenter:false,modal:false," +
"underlay:'none',context:['flaginfo2','bl','tr'],width:350});\" href='/panels/flags.html?" + md5 + "'>(explain this)</a>" +
"</div>"; //added md5 to flags.html to fix bl/tr anchoring

	newRow = document.createElement('tr');
	newRow.appendChild(newCol);
	newRow.style.backgroundColor = row.style.backgroundColor;

	return newRow;
}

function reportTruckRowContent(row, md5) {
	newCol = document.createElement('td');
	newCol.colSpan = 13;
	newCol.innerHTML =
"<div style='margin-left: 10px; padding: 10px; border: 3px solid black; background-color:gold;'>\n" +
"<b>Please flag this truck as:</b><br><table border=0 cellpadding=0 cellspacing=0><tr><td colspan=4 style='height:10px'>" +
"<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input checked type=radio name='loadreport' VALUE='Hidden'><td>&nbsp;<b>Hidden</b>" + "<td>&nbsp;&nbsp;&nbsp;(This truck will not work for me, please hide it just for me)<br>" +
"<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=radio name='loadreport' VALUE='Moved'><td>&nbsp;<b>Moved</b>" + "<td>&nbsp;&nbsp;&nbsp;(Truck is no longer available)<br>" +
"<tr><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=radio name='loadreport' VALUE='Wrong'><td>&nbsp;<b>Wrong</b>" + "<td>&nbsp;&nbsp;&nbsp;(Truck and/or contact information is wrong)<br>" +
"</table><br><button  type='button' onclick='submitLoadInfo(\"" + md5 + "\", this)' />Save</button>" + 
"<button  type='button' onclick=\"expandReport(this,'" + md5 + "'); return false;\"' />cancel</button>" + 
"<br><br><i>* by flagging trucks, you help save time for yourself and others.&nbsp;</i>" +
"<a id=flaginfo2 onclick=\"return setDIV('panel1b',this.href,{fixedcenter:false,modal:false," +
"underlay:'none',context:['flaginfo2','bl','tr'],width:350});\" href='/panels/flags.html?" + md5 + "'>(explain this)</a>" +
"</div>"; //added md5 to flags.html to fix bl/tr anchoring

	newRow = document.createElement('tr');
	newRow.appendChild(newCol);
	newRow.style.backgroundColor = row.style.backgroundColor;

	return newRow;
}

function expandReport(celldiv, md5) {
	row = celldiv.parentNode.parentNode;
	if (document.location.href.match("searchpageloads")) {
		content = reportLoadRowContent(row, md5)
	}

	if (document.location.href.match("searchpagetrucks")) {
		content = reportTruckRowContent(row, md5)
	}
	
	if (content) {
		setExpandedRow(row, 1, content);
	}
}

function loadInfoContent(row, md5) {
	// fetch data and display it!
	newDiv = document.createElement('div');
    newDiv.id = "moredetails";
	newCol = document.createElement('td');
	newCol.colSpan = 13;
	newCol.appendChild(newDiv);
	newRow = document.createElement('tr');
	newRow.appendChild(newCol);
	newRow.style.backgroundColor = '#AAAAAA';
	//newRow.style.borderBottom = '1px solid black';
	newRow.style.borderBottomStyle = 'solid';
	newRow.style.borderBottomWidth = '1px';
	newRow.style.borderBottomColor = 'black';

	return newRow;
}

function expandLoadInfo(celldiv, md5) {
	row = celldiv.parentNode.parentNode;
	setExpandedRow(row, 2, loadInfoContent(row, md5));
    setDIV("moredetails","/cgi-bin/directfreight?93=DIRECTY-148739004616&1=&2=MO&3=352537891&4=");
}
