
  ////////////
 // CChannel
//
function CChannel(id, aspSource, paramsArray)
{
	// defaults
	if ( arguments.length < 3 ) paramsArray = [];

	this.id = id;
	this.aspSource = aspSource;
	this.params = paramsArray;
	this.values = [];
	this.isStatic = false;

	this.show = false;

	this.aspFunction = "";
	this.pageSize = 10;
	this.xslData = "";
	this.xslError = "";

	this.pageNumber = 0;
	this.pageCount = 0;
	this.recordCount = 0;

	this.orderBy = "";
	this.searchExpression = "";

	// Gestori eventi
	this.onquery = function() {};
	this.onquerySubject = null;

	// Catching errori
	this.bErrorAlert = true;
}


//
// CChannel_writeHTML
//
function CChannel_writeHTML()
{
	document.write(this.buildHTML());
}
CChannel.prototype.writeHTML = CChannel_writeHTML;


//
// CChannel_buildHTML
//
function CChannel_buildHTML()
{
	var HTML = "";
	var hiddenStyle = 'STYLE="position: absolute; z-index:100; top:0; left:0; width:0; height:0; overflow: hidden;"';
	var id = this.id;
	var idName;
   
	HTML += '\n<DIV ID="'+ id +'_DIV" _style_>';
	HTML += '\n<FORM ID="'+ id + '_FORM" NAME="'+ id + '_FORM" TARGET="' + id +'_IFRAME" METHOD="POST" ACTION="" onsubmit="return false;">';
	HTML += '\n<TEXTAREA ID="'+ id + '_request" NAME="'+ id + '_request"></TEXTAREA>';

	HTML += '\n<INPUT ID="'+ id +'_aspFunction" NAME="'+ id +'_aspFunction">';
	HTML += '\n<INPUT ID="'+ id +'_pageNumber" NAME="'+ id +'_pageNumber">';
	HTML += '\n<INPUT ID="'+ id +'_pageSize" NAME="'+ id +'_pageSize">';
	HTML += '\n<INPUT ID="'+ id +'_xslData" NAME="'+ id +'_xslData">';
	HTML += '\n<INPUT ID="'+ id +'_xslError" NAME="'+ id +'_xslError">';
	HTML += '\n<INPUT ID="'+ id +'_orderBy" NAME="'+ id +'_orderBy">';
	HTML += '\n<INPUT ID="'+ id +'_searchExpression" NAME="'+ id +'_searchExpression">';

	HTML += '\n<DIV INPUT ID="'+ this.id +'_DIVparams">';
   
	for (var i=0; i<this.params.length; i++)
	{
 		idName = id + '_' + this.params[i];
 		HTML += '\n<TEXTAREA ID="'+ id +'_'+ this.params[i] +'" NAME="'+ id +'_'+ this.params[i] +'"></TEXTAREA>';
	}
	HTML += '\n</FORM></DIV>';
	HTML += '\n<IFRAME ID="'+ id +'_IFRAME" NAME="'+ id +'_IFRAME" NORESIZE=resize SCROLLING=1 FRAMEBORDER=1 WIDTH=100% HEIGHT=200></IFRAME></DIV>';

	if ( this.show )
  		HTML = HTML.replace(/_style_/g, "");
	else
  		HTML = HTML.replace(/_style_/g, hiddenStyle);

	return HTML;   
}
CChannel.prototype.buildHTML = CChannel_buildHTML;


//
// CChannel.staticHTML
//
function CChannel_staticHTML(page)
{
	if ( IsEmpty(page) ) page = 1;

	this.isStatic = true;
	this.pageNumber = page;

	var id = this.id;
	var queryString = "";
	queryString += this.aspSource;
	queryString += "?channel=" + escape(id);
	queryString += "&pageNumber=" + escape(this.pageNumber);
	queryString += "&pageSize=" + escape(this.pageSize);
	queryString += "&searchExpression=" + escape(this.searchExpression);
	queryString += "&orderBy=" + escape(this.orderBy);
	queryString += "&xslData=" + escape(this.xslData);
	queryString += "&xslError=" + escape(this.xslError);

	for (var i=0; i<this.params.length; i++)
		if ( !IsEmpty(this.values[this.params[i]]) )		
   			queryString += "&" + this.params[i] + "=" + escape(this.values[this.params[i]]);
   
	var target = this.getDocument();
	if ( target == null )
	{
		var HTML = "";
		var hiddenStyle = 'STYLE="position: absolute; z-index:100; top:0; left:0; width:0; height:0; overflow: hidden;"';
		HTML += '<DIV ID="'+ id +'_DIV" _style_>';
		HTML += '<IFRAME ID="'+ id +'_IFRAME" NAME="'+ id +'_IFRAME"'; 
		HTML += ' SRC="'+ queryString +'"';
		HTML += ' NORESIZE=resize SCROLLING=1 FRAMEBORDER=1 WIDTH=100% HEIGHT=200">';
		HTML += '</IFRAME></DIV>';
		if ( this.show )
			HTML = HTML.replace(/_style_/g, "");
		else
			HTML = HTML.replace(/_style_/g, hiddenStyle);
		document.write(HTML);
	}
	else
		this.getDocument().location.replace(queryString);
}
CChannel.prototype.staticHTML = CChannel_staticHTML;


//
// CChannel_changeParams
//
function CChannel_changeParams(paramsArray)
{
try
{
	var HTML = "";
	this.params = paramsArray;
		for (var i=0; i<this.params.length; i++)
	HTML += '<TEXTAREA ID="'+ this.id +'_'+ this.params[i] +'" NAME="'+ this.id +'_'+ this.params[i] +'"></TEXTAREA>';
	document.getElementById(this.id +"_DIVparams").innerHTML = HTML;
}
catch(e)
{
   window.alert("CChannel.changeParams():\nErrore in fase variazione parametri");
}
}
CChannel.prototype.changeParams = CChannel_changeParams;


//
// CChannel_setParam
//
function CChannel_setParam(name, value)
{
	if ( IsEmpty(value) ) value = "";
		this.values[name] = value;
}
CChannel.prototype.setParam = CChannel_setParam;


//
// CChannel_query
//
function CChannel_query(page)
{
	// defaults
	if ( IsUndefined(page) ) page = 1;
	
	if ( this.isStatic )
		return this.staticHTML(page);		

   this.pageNumber = page;
   this.request("");
}
CChannel.prototype.query = CChannel_query;


//
// CChannel_callback
//
function CChannel_callback()
{	
	this.pageNumber = Trim(this.responseHTML("PageNumber"));
	this.pageCount = Trim(this.responseHTML("PageCount"));
	this.recordCount = Trim(this.responseHTML("RecordCount"));
	this.replaceMacro();
	
	if ( this.catchErrorStatus() )
		return;
	
	if ( this.onquerySubject != null )
    	this.onquerySubject.callback();
	else
		this.onquery();
}
CChannel.prototype.callback = CChannel_callback;


//
// CChannel_responseHTML
//
function CChannel_responseHTML(element)
{
try
{
	// default
	if ( arguments.length < 1 )
		return window.frames[this.id + "_IFRAME"].document.body.innerHTML;
	else
		return window.frames[this.id + "_IFRAME"].document.getElementById(element).innerHTML;
}
catch(e)
{
	window.alert("CChannel.responseHTML():\nErrore nei dati di risposta.\nRicaricare la pagina.");
	return "";
}
}
CChannel.prototype.responseHTML = CChannel_responseHTML;


//
// CChannel_replaceMacro
//
function CChannel_replaceMacro()
{
try
{
	var target = window.frames[this.id + "_IFRAME"].document.getElementById("Data");
	target.innerHTML = target.innerHTML.replace(/\s*MACROempty\s*/g, "&nbsp;");
	target = window.frames[this.id + "_IFRAME"].document.getElementById("XMLError");
	if ( this.bErrorAlert )
		target.innerHTML = target.innerHTML.replace(/\s*MACROempty\s*/g, "");
	else
		target.innerHTML = target.innerHTML.replace(/\s*MACROempty\s*/g, "&nbsp;");
}
catch(e)
{
	return;
}
}
CChannel.prototype.replaceMacro = CChannel_replaceMacro;

//
// CChannel.catchErrorStatus
//
function CChannel_catchErrorStatus()
{
try
{
	if ( !this.bErrorAlert )
		return false;

	var id = this.id;	
	if ( window.frames[id + "_IFRAME"].document.getElementById("ErrorStatus").innerHTML == 0 )
		return false;
	var errString = "";
	errString += "\nErrore: " + Trim(window.frames[id + "_IFRAME"].document.getElementById("Error_idTD_Number").innerHTML);
	errString += "\nOrigine: " + Trim(window.frames[id + "_IFRAME"].document.getElementById("Error_idTD_Source").innerHTML);
	errString += "\nDescrizione: " + Trim(window.frames[id + "_IFRAME"].document.getElementById("Error_idTD_Description").innerHTML);
	errString += "\n\n" + Trim(window.frames[id + "_IFRAME"].document.getElementById("Error_idTD_ErrMsg").innerHTML);
	window.alert(errString);
	return true;
}
catch(e)
{
	window.alert("CChannel.responseHTML():\nErrore nei dati di risposta.\nRicaricare la pagina.");
	return true;
}
}
CChannel.prototype.catchErrorStatus = CChannel_catchErrorStatus;


  ////////////////////
 //   CChannel_request
//
function CChannel_request(requestString)
{
try
{
	// defaults
	if ( arguments.length == 0 ) requestString = "";

	var id = this.id;
	var targetForm = document.getElementById(id + "_FORM");	
	document.getElementById(id +"_aspFunction").value = this.aspFunction;
	document.getElementById(id +"_pageNumber").value = this.pageNumber;
	document.getElementById(id +"_pageSize").value = this.pageSize;
	document.getElementById(id +"_request").value = requestString;
	document.getElementById(id +"_xslData").value = this.xslData;
	document.getElementById(id +"_xslError").value = this.xslError;
	document.getElementById(id +"_orderBy").value = this.orderBy;
	document.getElementById(id +"_searchExpression").value = this.searchExpression;
	targetForm.action = this.aspSource +"?channel="+ id;
   
	for (var i=0; i<this.params.length; i++)
	{
		if ( !IsEmpty(this.values[this.params[i]]) )		
			document.getElementById(id +"_"+ this.params[i]).value = this.values[this.params[i]];
		else
			document.getElementById(id +"_"+ this.params[i]).value = "";
	}
	targetForm.submit();
}
catch(e)
{
	window.alert("CChannel.request():\nErrore in fase di interrogazione");
}
}
CChannel.prototype.request = CChannel_request;

//
// CChannel.getDocument
//
function CChannel_getDocument()
{
try
{
	var result = window.frames[this.id +"_IFRAME"].document;
	if ( IsEmpty(result) )
		result = null;
	return result;
}
catch(e)
{
	return null;
}
}
CChannel.prototype.getDocument = CChannel_getDocument;
