
// Framework Ajax Stuff
function AjaxTxCb (u, id, ch0, ch1, chbusy, cbfn)
{
	this.u = u;
	this.id = id;
	this.ch0 = ch0;
	this.ch1 = ch1;
	this.chbusy = chbusy;
	this.cbfn = cbfn;
	this.retrieve ();
}

AjaxTxCb.prototype.update = function (req)
{
	var t = parseInt (req.responseText);
	window.document.getElementById (this.id).src = (t == 1) ? this.ch1 : this.ch0;
	if (this.cbfn)
		this.cbfn();
}

AjaxTxCb.prototype.retrieve = function ()
{
	var tx = new Ajax.Request (this.u, {method: 'get', onComplete: this.update.bind(this)});
}

function ajaxtx_cb (u, id, ch0, ch1, chbusy, cbfn)
{
	document.getElementById (id).src = chbusy;
	var newtx = new AjaxTxCb (u, id, ch0, ch1, chbusy, cbfn);
}

// tooptip using tipster
var current_ajax_tooltip = '';
function AjaxTt (obj, u, w)
{
	current_ajax_tooltip = u;
	this.ID = 'fw_ajax_tooltip';
	this.u = u;
	this.w = w;
	this.show (obj);
	//this.retrieve ();
}

AjaxTt.prototype.update = function (req)
{
//alert (this.u);
//alert (current_ajax_tooltip);
//alert((this.u == current_ajax_tooltip));
	if (this.u == current_ajax_tooltip)
	{
		//"<div style='float:right;width:15px;'><a href='javascript:TipObj.list[\"" + this.ID + "\"].hide(\"main\");'><img src='img/close-button.gif' /></a></div>"
		TipObj.list[this.ID].tips["main"][3] = req.responseText;
//		alert(req.responseText);
//		TipObj.list[this.ID].actTip = '';
//		TipObj.list[this.ID].show("main");
//		TipObj.list[this.ID].div.write("chris hoke");
		TipObj.list["fw_ajax_tooltip"].replaceContent ("main");
//		var showStr = 'with (TipObj.list["fw_ajax_tooltip"]) {  ' +
//			'replaceContent("fw_ajax_tooltip"); ' + '/* no fade */ }';
//		alert (showstr);
		//eval(showStr);
	}
}

AjaxTt.prototype.retrieve = function ()
{
	var tx = new Ajax.Request (this.u, {method: 'get', onComplete: this.update.bind(this)});
}

function ajaxtt_hide ()
{
	if (TipObj.list['fw_ajax_tooltip'])
		TipObj.list['fw_ajax_tooltip'].hide();
}

AjaxTt.prototype.show = function (obj)
{
	if (TipObj.list[this.ID])
	{
		var tp = TipObj.list[this.ID];
//		if (!obj.onmouseout)
//			obj.onmouseout = new Function ('evt', 'TipObj.list["' + this.ID + '"].hide()');
    tp.tips.main = new Array (5, 5, this.w, '<img src="img/loadinganim.gif" style="vertical-align:middle;"> Loading...');
		//TipObj.list[this.ID].actTip = -1;
		tp.tipStick = 0;
		tp.ajaxTt = this;
		tp.showajax('main');
	}
	else
	{
		var ttdiv = document.createElement('div');
		//ttdiv.innerHTML = "<div class='infobox'><div id='div_tt_inner_" + this.ID + "' style='width:" + this.w + "px;'>Loading...</div></div>";
    ttdiv.id = this.ID + "Layer";
    ttdiv.style.position = 'absolute';
    ttdiv.style.zIndex = 10001;
    ttdiv.style.width = 'auto';
    ttdiv.style.backgroundColor = '#FFFFFF';
    document.body.appendChild(ttdiv);
		var tp = new TipObj(this.ID);
		tp.fadeInSpeed = 100;	//87;
		tp.fadeOutSpeed = 100; /* 87; */
		tp.showDelay = 450;
		tp.hideDelay = 200;
		var ie7 = (document.all && !window.opera && window.XMLHttpRequest) ? 1 : 0;
		tp.IESelectBoxFix = !ie7 & isIE;
		//document.getElementById("debugtext").innerHTML = ie7 && isIE;
    tp.template = "<div class='infoBox'><div id='div_tt_inner_" + this.ID + "' style='width:" + this.w + "px;z-index:10050;'>%3%</div></div>";
    tp.tips.main = new Array (5, 5, this.w, '<img src="img/loadinganim.gif" style="vertical-align:middle;"> Loading...');
		tp.tipStick = 0;
//		if (!obj.onmouseout)
//			obj.onmouseout = new Function ('evt', 'TipObj.list["' + this.ID + '"].hide()');
		//TipObj.list[this.ID].actTip = null;
		tp.ajaxTt = this;
		tp.showajax('main');
	}
}

function ajaxtt_show (obj, u, w)
{
	ajaxtt_hide();
	var tt = new AjaxTt (obj, u, w);
}

//Framework Ajax TableEdit object
function AjaxTxTableEdit()
{
	this.u = "TableEdit.aspx";
	this.tableName = "";
	this.keys = "";
	this.values = "";
	this.rowsAffected = -1;
	this.error = false;
	this.errorMessage = "";
	this.returnCode = null;
}

AjaxTxTableEdit.prototype.update = function (req)
{
	var s = "" + req.responseText;
	if (s.length < 1)
	{
		this.error = true;
		this.errorMessage = "No value returned from AJAX update.";
	}
	else
	{
		var rv = s.split("&");
		this.returnCode = parseInt (rv[0]);
		//alert(rv.length);
		if (this.returnCode != 1)
		{
			this.error = true;
			this.errorMessage = "Unspecified error.";
		}
		var i=0;
		for (i=0; i<rv.length; i++)
		{
			var v = rv[i].split("=");
			if (v[0] == "rowsaffected")
				this.rowsAffected = parseInt (v[1]);
			else if (v[0] == "error")
				this.errorMessage = v[1];
			else
			{
				//do something with junk
			}
		}
	}
}

AjaxTxTableEdit.prototype.save = function ()
{
	var postBody = "z=0";
	postBody += "&inpTableName=" + this.tableName;
	postBody += "&inpKeys=" + escape (this.keys);
	postBody += "&inpValues=" + escape (this.values);
	var tx = new Ajax.Request (this.u, {asynchronous: false, method: 'post', postBody: postBody, onComplete: this.update.bind(this)});
}

// Date Control
function DateRangeParam2_DateOnClick (o, fieldID, btnID)
{
	Calendar.setup( {inputField : fieldID, ifFormat : '%m/%d/%Y', button : btnID, electric: false} );
	o.onclick();
}

function DateRangeParam2_DateChanged (o, fieldID, otherFieldID)
{
	window.document.getElementById(fieldID + "_display").innerHTML = window.document.getElementById(fieldID).value;
	if (otherFieldID)
	{
		var el = window.document.getElementById(otherFieldID).value;
		if (window.document.getElementById(otherFieldID).value == "")
		{
			window.document.getElementById(otherFieldID).value = window.document.getElementById(fieldID).value;
			window.document.getElementById(otherFieldID + "_display").innerHTML = window.document.getElementById(otherFieldID).value;
		}
	}
}

//excel export stuff
function dumpTableToExcel (id, opt)
{
	var el = document.getElementById(id);
	if (!el)
		return;
	var tbltext = el.innerHTML;
	var oldtarg = document.forms[0].target;
	var oldact = document.forms[0].action;
	var sopt = opt.split ("&");
	var i;
	for (i = 0; i < sopt.length; i++)
	{
		var soptitem = sopt[i].split("=");
		var optionname = soptitem[0].toLowerCase();
		if (optionname == "filename")
		{
			if (!document.getElementById("__dump_table_to_excel_filename"))
			{
				currentElement = document.createElement("input");
				currentElement.setAttribute("type", "hidden");
				currentElement.setAttribute("name", "__dump_table_to_excel_filename");
				currentElement.setAttribute("id", "__dump_table_to_excel_filename");
				document.forms[0].appendChild(currentElement);
			}
			document.getElementById("__dump_table_to_excel_filename").value = soptitem[1];
		}
	}
	if (!document.getElementById("__dump_table_to_excel"))
	{
		currentElement = document.createElement("input");
		currentElement.setAttribute("type", "hidden");
		currentElement.setAttribute("name", "__dump_table_to_excel");
		currentElement.setAttribute("id", "__dump_table_to_excel");
		document.forms[0].appendChild(currentElement);
	}
	if (!document.getElementById("__dump_table_to_excel"))
		return;	//bad news
	document.getElementById("__dump_table_to_excel").value = tbltext;
	document.forms[0].target = "_new";
	document.forms[0].action = "dumptabletoexcel.aspx";
	__doPostBack("","");
	document.forms[0].__EVENTTARGET.value = "";
	document.forms[0].__EVENTARGUMENT.value = "";
	document.forms[0].target = oldtarg;
	document.forms[0].action = oldact;
}

var station_filter_timeout = -1;

function StationFilter (stnfilterid, reportid, mktid)
{
	this.stnfilterid = stnfilterid;
	this.reportid = reportid;
	this.mktid = mktid;
	this.retrieve();
}

StationFilter.prototype.update = function (req)
{
	var html = req.responseText;
	if (window.document.getElementById (this.stnfilterid))
	{
		window.document.getElementById (this.stnfilterid).innerHTML = html;
	}
}

StationFilter.prototype.retrieve = function ()
{
	var tx = new Ajax.Request ("tt/stnfilter.aspx?mktid=" + this.mktid + "&reportid=" + this.reportid, {method: 'get', onComplete: this.update.bind(this)});
}

function toggle_station_filter (stnid, reportid)
{
	if (station_filter_timeout != -1)
		clearTimeout (station_filter_timeout);
/*
	if (document.getElementById(reportid))
	{
		var el = document.getElementById(reportid);
		el.innerHTML = "<img src='img/loading_animation_liferay.gif' class='vmiddle'>&nbsp;<i>Updating report...</i>";
	}
*/
	ajaxtx_cb ("ajtx/togglestnfilter.aspx?stnid=" + stnid, "chimg" + stnid, "icons/ch0.gif", "icons/ch1.gif", "icons/chbusy.gif", station_filter_done);
}

function station_filter_done()
{
	//station_filter_timeout = setTimeout ("document.forms[0].submit();", 3000);
	var el = document.getElementById("stnfiltergobutton");
	if (el)
	{
		el.style.display = "inline";
		el.style.visibility = "visible";
	}
}

function toggle_div(el1name, el2name)
{
	var el1 = window.document.getElementById (el1name);
	var el2 = window.document.getElementById (el2name);
	if (el1.className == "div-show")
	{
		el1.className = "div-hide";
		if (el2)
			el2.className = "div-show";
	}
	else
	{
		if (el2)
			el2.className = "div-hide";
		el1.className = "div-show";
	}
}

//
function openmorehelp()
{
	var o = window.document.getElementById('dvmorehelp');
	if (!o)
		return;
	toggle_div('');
}
