/**
 * @fileoverview 弹出窗口处理 
 * port from EF
 */

var EF_SPLIT = "__";
var EF_CLASSTAG = "";
var EF_FORMDATA_SPLIT = "-";
var EF_CR_IDENTIFIER = "Â¶Â¶";
var EF_CR_HTML_IDENTIFIER = "&para;&para;";

//alert("efform");
var ef_form_init_time = new Date();
var ef_form_validate_message = new Object();
var ef_grids = new Object();

var ef_form_ename = " ";
var ef_form_cname = " ";

ef = function() {
}

//切换节点显示方式
ef.toggleDivDisplay =  function (divId){
  var node = ef.get(divId);
  if (node.style.display == "none") {
    node.style.display = "block";
  } else {
    node.style.display = "none";
  }   
}

ef.getNodeById = function(node_id) {
  var node_get = ef.get(node_id);
  if (!node_get) alert("ID[" + node_id + "] not exist");
  return node_get;
}

ef.get = function(node_id) {
  var node_get = document.getElementById(node_id);
  return node_get;
}

ef.debug = function( msg )
{
	ef.log( "[DEBUG] - " + msg ); 
}

ef.info = function( msg )
{
	ef.log( "[INFO] - " + msg ); 
}

ef.log = function(msg) {
	/*
  var msgRecNode = ef.get("efFormDetailMsg");
  if (msgRecNode!= null) 
    msgRecNode.innerHTML += msg + "<br>";
	*/
	try
	{
		if( isAvailable(__DEBUG) )
		{
			__DEBUG_MSG.push(msg);
		}
	}
	catch(ex)
	{		
	}    
}

efform = function () {
//debugger;
}
efform.onload = function () {
	
  var ef_form_load_time = new Date();
  
  ef.debug( "ef(" + ef_version + ") form took: " + 
    (ef_form_load_time.getTime() - ef_form_init_time.getTime()).toString()
	 + "ms to load" ); 
   
// window.status = "ef(" + ef_version + ") form took: " + 
//    (ef_form_load_time.getTime() - ef_form_init_time.getTime()).toString()
//	 + "ms to load" ; 
}

efform.repaint = function()
{
   ef_form_init_time = new Date();
   	for( var key in ef_grids ) 
  	{
  		ef_grids[key].paint();
   	}
  var ef_form_load_time = new Date();
  ef.debug( "ef(" + ef_version + ") form took: " + 
    (ef_form_load_time.getTime() - ef_form_init_time.getTime()).toString() 
	+ "ms to load" ); 
}

efform.alertTime = function(msg_info) {
  var ef_form_load_time = new Date(); 
  ef.debug( msg_info + 
    (ef_form_load_time.getTime() - ef_form_init_time.getTime()).toString() ); 

}

efform.init = function()
{
	window.onload = efform.onload;
	document.body.onresize = efform.repaint;
}

efform.addGrid = function( grid ) 
{
	ef_grids[grid.gridId] = grid;
}

efform.getGrid = function( grid_id )
{
	var grid = ef_grids[grid_id];
	if( !grid ) throw new Error( "Grid with id [" + grid_id + "] not exists!" );
	return grid;
}

efform.clearDiv = function( div_id )
{
	try
	{
		var div_node = document.getElementById( div_id );
		efform.clearInputField( div_node );
	}
	catch( exception )
	{
		alert( exception.message );
	}
}

efform.clearInputField = function( node )
{
	if( node.tagName == "INPUT" && node.type!="button" )
	{
		node.value = "";
	}
	else
	{
		for( var i=0;i< node.childNodes.length;i++ )
		{
			try
			{
				efform.clearInputField( node.childNodes[i] );
			}
			catch( exception )
			{}
		}
	}
}

efform.hasErrorMessage = function()
{
	for( var key in ef_form_validate_message )
	{
		return true;
	}
	return false; 
}

efform.showErrorMessage = function()
{
	var ei_message = document.getElementById("ei_message");
	if( !ei_message ) return;
		
	if( efform.hasErrorMessage() )
	{
		var message = [];		
		ei_message.style.color = "red";		
		for( var key in ef_form_validate_message )
		{
			message.push( "* " + ef_form_validate_message[key] );
		} 
		ei_message.innerText = message.join('\t');
	}
	else
	{
		ei_message.style.color = "";
		ei_message.innerText = "";
	}
}

efform.clearErrorMessage = function()
{
	for( var key in ef_form_validate_message )
	{
		delete ef_form_validate_message[key];
	}
	var ei_message = document.getElementById("ei_message");
	if( !ei_message ) return;
	ei_message.style.color = "";
	ei_message.innerText = "";
}

efform.windowOnError = function( msg, url, line )
{
	alert( "出现javascript异常: " + msg);
	return true;
}

efform.togglenav = function( nav )
{
	try
	{
		if( isAvailable(__DEBUG) )
		{
			efdebugger.show();
		}
	}
	catch(ex)
	{		
	} 
}

efform.formRefresh = function()
{

}

efform.formPrint = function()
{

}

efform.getSubGridDiv = function()
{
	var div_id = "ef_subgrid";
	var div_node = document.getElementById( div_id );
	if( !div_node )
	{
		div_node = efform.createDivWindow( div_id, "por-Window" );
	}
	return div_node;
}

efform.createDivWindow = function( div_id, styleClassName )
{
	var ef_subwindow = document.getElementById( div_id );
	if( ef_subwindow ) 
		throw new Error( "Element with id [" + div_id + "] already exists!" );
	ef_subwindow = document.createElement("div");
	ef_subwindow.id = div_id;
	ef_subwindow.className = styleClassName;
	ef_subwindow.style.overflow = "hidden";
	ef_subwindow.style.display = "none";
	ef_subwindow.innerHTML = "&nbsp;"
	
//	document.forms[0].appendChild( ef_subwindow );
	document.body.appendChild( ef_subwindow );  
	return ef_subwindow;
}

efform.showSubGridWindow = function( component_id, column )
{
	var component = document.getElementById( component_id );
	if( !component ) return;
	
	var div_node = efform.getSubGridDiv();
//	var column_new = div_node.getAttribute( "efDisplayCol" );
	var column_new = column;
	var column_old = div_node.getAttribute( "efDisplayingCol" );
	if( column_old!= null && column_old == column_new )
	{
		PopWindow.show( component, div_node.id );
		return;
	}	
	div_node.setAttribute( "efDisplayingCol", column_new );	
	var inner_html = "";
	var column_length = column_new.columnListLabel.length;
	inner_html += "<TABLE cellspacing='0' cellpadding='1'>";
	inner_html += "<TR class=por-Caption>";
	inner_html += "<TD align='left' id='containerOuter'>&nbsp;"+ column_new.getCname() +"&nbsp;</TD>";
	inner_html += "<TD align='right' id='containerOuter'><IMG src='" + PorGlobals.IMAGES_PATH + "efcalendar_close.gif' onclick='javascript:PopWindow.hide();'/></TD>";
	inner_html += "</TR>";
	inner_html += "<TR><TD colspan=2>";
	inner_html += "<TABLE class='por-Table' hidefocus cellspacing = 1 cellpadding = 1 onmouseover='javascript:efform.subgrid_onmouseover()' onmouseout='javascript:efform.subgrid_onmouseout()' onclick='javascript:efform.subgrid_onclick()'>";
	inner_html += "<TR class=por-TableHeader>";
	for( var j=0; j<column_length; j++ )
	{
		inner_html += "<TD>"+ column_new.columnListLabel[j] +"</TD>";
	}
	inner_html +="</TR>";
	for( var i=0; i<column_new.columnListValue.length; i++ )
	{
		var row_i = column_new.columnListValue[i];
		inner_html += "<TR class='por-TableRow" + (i&1) + "'>";
		for( var j=0; j<column_length; j++ )
		{
			inner_html += "<TD>"+ row_i[j] +"</TD>";
		}
		inner_html += "</TR>";
	}
	inner_html += "</TABLE>";	
	$(div_node).html(inner_html);
	//div_node.innerHTML = inner_html;	
	PopWindow.show( component, div_node.id );
}

efform.showTextAreaWindow = function( component_id )
{
	var component = document.getElementById( component_id );
	if( !component ) return;
		
	var div_node = efform.getSubGridDiv();
	
	var show_value = component.value;
	var	index=show_value.indexOf(EF_CR_IDENTIFIER);   
  	while(index!=-1)   
  	{	   
  		show_value=show_value.substring(0,index)+"\r\n"+show_value.substring(index+2);   
  		index=show_value.indexOf(EF_CR_IDENTIFIER);   
  	} 
	
	var inner_html = "";
	inner_html += "<TABLE cellspacing='0' cellpadding='1'>";
	inner_html += "<TR class=por-Caption>";
	inner_html += "<TD align='left' id='containerOuter'>&nbsp;详情&nbsp;</TD>";
	inner_html += "<TD align='right' id='containerOuter'><IMG src='" + PorGlobals.IMAGES_PATH + "efcalendar_close.gif' onclick='javascript:PopWindow.hide();'/></TD>";
	inner_html += "</TR>";
	inner_html += "<TR onmouseover='javascript:efform.nullfunction();' onclick='javascript:efform.nullfunction();' onmousemove='javascript:efform.nullfunction();' ><TD colspan=2>";
	inner_html += "<textarea wrap=\"hard\" rows=10 cols=50 id=\"subwindow_textarea\" class=por-InputField>"+show_value+"</textarea>";
	inner_html += "</TD></TR>";
	inner_html += "<TR onmouseover='javascript:efform.nullfunction();' onclick='javascript:efform.nullfunction();' onmousemove='javascript:efform.nullfunction();'><TD colspan=2 align=center>";
	inner_html += "<input class=por-Button type=\"button\" value=\"保存\" onclick='javascript:efform.subgrid_save_onclick();' />";
	inner_html += "</TD></TR>";
	inner_html += "</TABLE>";
	$(div_node).html(inner_html);
	//div_node.innerHTML = inner_html;	
	PopWindow.show( component, div_node.id );
}

efform.subgrid_onmouseover = function()
{
	if( event.srcElement.tagName != 'TD' )
		return;
	var td_node = event.srcElement;
	if (td_node && efgrid.getRowIndex(td_node.parentNode) >0 ) 
	{
		td_node.parentNode.className = 'por-RowHighlight';
	}
}

efform.subgrid_onmouseout = function()
{
	if( event.srcElement.tagName != 'TD' )
		return;
		
	var td_node = event.srcElement;
	if (td_node && efgrid.getRowIndex(td_node.parentNode) >0 ) 
	{
		td_node.parentNode.className = 'por-TableRow' + (efgrid.getRowIndex(td_node.parentNode) & 1);
	}
}

efform.subgrid_onclick = function()
{
	if( event.srcElement.tagName != 'TD' )
		return;
		
	var td_node = event.srcElement;
	if ( !td_node ) return;
	var row_index = efgrid.getRowIndex(td_node.parentNode);
	if( row_index <=0 ) return;

	if( column_obj )
	{
		cell_value = column_obj.columnListValue[row_index-1][0];
	}
	PopWindow.setValue( cell_value );
}

efform.subgrid_save_onclick = function()
{
	var text_area = document.getElementById("subwindow_textarea");
	var cell_value = "";
	if( text_area )
	{
		cell_value = text_area.value;
	}
	PopWindow.setValue( cell_value );
}

efform.nullfunction = function()
{
}

efform.evalFunction = function( functionName )
{
	if((typeof functionName) == "function")  
	{
		try
		{
			eval( functionName(arguments[1],arguments[2],arguments[3],arguments[4],arguments[5],arguments[6],arguments[7],arguments[8],arguments[9],arguments[10]) );
		}
		catch(ex)
		{}		 
	} 	
}

efform.newWindow = function( input_id, div_node, row_index, col_index )
{
	var component = document.getElementById( input_id );
	if( !component ) return;
	
	var url = "about:blank";
	try
	{
		url = efgrid_getNewWindowUrl( div_node, row_index, col_index, component.value );
	}
	catch( exception )
	{
		alert("efgrid_getNewWindowUrl 未定义或参数错误"); 
		return;
	}
	var returnValue;
	if ($.browser.msie){
		returnValue = window.showModalDialog( url, "","dialogWidth:400px; dialogHeight:300px;resizable:yes");
		if( !returnValue || !returnValue["value"] )
		{
			alert("返回值未定义!"); 
			return;
		}
		div_node.firstChild.value = returnValue["value"];
		$(div_node.firstChild).change();
//		efgrid.setCellValue( input_id,returnValue["value"],returnValue["label"]);
	} else {
		win = window.open( url, "","dialogWidth:400px; dialogHeight:300px;resizable:yes");
		win.target = div_node;
		win.onunload = function(){
			if (this.returnValue){
				this.target.firstChild.value = this.returnValue["value"];
				$(this.target.firstChild).change();
			}
		};
	}
	
}
	var column_obj = efform.getSubGridDiv().getAttribute( "efDisplayingCol" );
	var cell_value = "";
