var g_bIsIE = (navigator.appVersion.indexOf("MSIE") > -1) ? true : false;
var g_wndSub ;
var g_nCtrlCount = 0;
var g_ctrlSelected;
var dtDefault ;
var dtNow = new Date();
dtDefault = dtNow.getFullYear() + "-" + ( dtNow.getMonth() + 1 ) + "-" + dtNow.getDate();

function DateInput_onclick( formEmbeded, strFieldName)
{
	document.getElementId();
	//eval( "g_ctrlSelected=formEmbeded."+strFieldName+";" );
	g_ctrlSelected = $(strFieldName);
	var strFeatures = "toolbar=no,menubar=no,scrollbar=no,resizable=no,location=no"
					 + (g_bIsIE ? ",width=200,height=220" : ",width=220,height=250");
	g_wndSub = window.open( "./Calendar.htm", "Calendar", strFeatures, false );
	g_wndSub.focus();
}
function DateInputInner_onclick(formEmbeded, strFieldName,bAllowTime)
{
	if(bAllowTime == null) {
			document.frames["IFCalendar"].document.getElementById("cbxSelectTime").disabled=false;
			document.frames["IFCalendar"].document.getElementById("allowtime").value="";
		} else if(bAllowTime==true){
			document.frames["IFCalendar"].document.getElementById("cbxSelectTime").checked=true;
			document.frames["IFCalendar"].document.getElementById("cbxSelectTime").disabled=true;
			document.frames["IFCalendar"].document.getElementById("allowtime").value=true;
			
		} else if(bAllowTime==false){
			document.frames["IFCalendar"].document.getElementById("cbxSelectTime").checked = false;
			document.frames["IFCalendar"].document.getElementById("cbxSelectTime").disabled= true;
			document.frames["IFCalendar"].document.getElementById("allowtime").value=false;
		}
	//alert("value=="+document.frames["IFCalendar"].document.getElementById("allowtime").value);	
	//eval( "g_ctrlSelected=formEmbeded."+strFieldName+";" );
	g_ctrlSelected = $(strFieldName);
	var calendarctl = document.getElementById(strFieldName);
	var point = fGetXY(calendarctl);

	DivCalendar.style.left=document.body.scrollLeft + (document.body.clientWidth - 201)/2;
	DivCalendar.style.top=document.body.scrollTop + (document.body.clientHeight - 226)/2;
//	DivCalendar.style.left = point.x - 1;
//	DivCalendar.style.top = point.y + calendarctl.offsetHeight + 1;
	DivCalendar.style.visibility='visible';
	TabCalendar.style.visibility='visible';
	IFCalendar.opener=this;
	IFCalendar.initDate();
	
}
function fGetXY(aTag){
  var pt = new Point(aTag.offsetLeft, aTag.offsetTop);
  do {
  	aTag = aTag.offsetParent;
  	pt.x += aTag.offsetLeft;
  	pt.y += aTag.offsetTop;
  } while(aTag.tagName!="BODY");
  return pt;
}
function Point(iX, iY){
	this.x = iX;
	this.y = iY;
}

function clearvalue()
{
	g_ctrlSelected.value = "";
	hideInnerCalendar();

}
function setDateFieldValue(NewValue)
{
	g_ctrlSelected.value = NewValue;
}
function getDateFieldValue()
{
	return g_ctrlSelected.value;
}
var g_bInnerCalenderCreated = false;
function insertDateInputCtrlImpl(sType, strLabel, strName, strDefault, bShowButton, sWidth)
{
	document.onclick = document.onclick=function() {$("DivCalendar").style.visibility="hidden";}
	//this
	if( strLabel == null ) strLabel = "日期";
	if( strName == null ) strName = "DateInputCtrlName" + g_nCtrlCount++;
	if( strDefault == null )
	{
		var dtNow = new Date();
	    strDefault = dtDefault;

	}
	if( bShowButton == null )
		bShowButton = true;
	var strProcessor;
	
	if( sType == "simple" )
		strProcessor = "DateSimpleInput_onclick";
	else if( sType == "complex" )
		strProcessor = "DateInput_onclick";
	else if( sType == "inner" )
	{
		strProcessor = "DateInputInner_onclick";
		if( g_bInnerCalenderCreated == false )
		{
			g_bInnerCalenderCreated = true;
			
			var loc=document.location.href;
      var s = loc.split("/");
      //var ul=s[3];
			//document.writeln( "<div id=DivCalendar style='POSITION: absolute;VISIBILITY: hidden'>\n" +
			//	"<table id=TabCalendar border=1 bgcolor='lightblue' style='VISIBILITY: hidden'>\n" +
			//	"<TR><TH width='88%' bgcolor='darkblue' style='COLOR: white ' align=left>日历</TH>\n" +
			//	"<TH align=right width='12%'><img src='/"+ul+"/images/refuse.gif' onclick='return hideInnerCalendar();' alt='关闭' style='cursor:hand;'></TH></TR>\n" +
			//	"<tr><TH colspan=2><IFRAME id=IFCalendar src='/"+ul+"/js/Calendar2.htm' frameborder=1 width=201 height=226></IFRAME></TH></tr></table></div>" );
			document.writeln( "<div id=DivCalendar style='POSITION: absolute;VISIBILITY: hidden;left:0px;top:0px'>\n" +
				"<table id=TabCalendar border=1 bgcolor='lightblue' style='VISIBILITY: hidden'>\n" +
				"<TR><TH width='88%' bgcolor='darkblue' style='COLOR: white ' align=left>日历</TH>\n" +
				"<TH align=right width='12%'><img src='" +app_path + "/images/refuse.gif' onclick='return hideInnerCalendar();' alt='关闭' style='cursor:hand;'></TH></TR>\n" +
				"<tr><TH colspan=2><IFRAME id=IFCalendar src='" +app_path + "/script/Calendar2.htm' frameborder=1 width=201 height=226></IFRAME></TH></tr></table></div>" );
		}
	}
	/*
	if( sWidth == null )
		document.writeln(  strLabel
					 + "<INPUT onfocus='return " + strProcessor + "( this.form,\""+strName+"\")' ID='" + strName + "' NAME='" + strName
					 + "' VALUE='" + strDefault +"'>" );
	else
		document.writeln(  strLabel
					 + "<INPUT style='width:" + sWidth + ";' onfocus='return DateInputInner_onclick( this.form,\""+strName+"\")' ID='" + strName + "' NAME='" + strName
					 + "' VALUE='" + strDefault +"'>" );
	*/
	if( bShowButton == true )
		document.writeln( "<INPUT ID='InteleInput' NAME='InteleInput' type='button'"
					 + " VALUE='日期' LANGUAGE='javascript' onclick='return " + strProcessor + "( this.form,\""+strName+"\")'>" );
	
}


function insertDateInputCtrl( strLabel, strName, strDefault, bShowButton, sWidth )
{
	insertDateInputCtrlImpl( "complex", strLabel, strName, strDefault, bShowButton, sWidth );
}
function insertInnerDateInputCtrl(strLabel, strName, strDefault, bShowButton, sWidth )
{
	insertDateInputCtrlImpl( "inner",strLabel, strName, strDefault, bShowButton, sWidth );
}
//simple date control
function DateSimpleInput_onclick( formEmbeded, strFieldName )
{
	//get date field control
	//eval( "g_ctrlSelected=formEmbeded."+strFieldName+";" );
	g_ctrlSelected = $(strFieldName);
	//prepare parameters.
	var varParas = new Array();
	varParas[0] = "InputDate";//function type show be invoked.
	varParas[1] = getDateFieldValue();//current value in control
	
	setDateFieldValue( window.showModalDialog( "InputDateDialog.htm", varParas, "dialogHeight:80px;dialogWidth:370px" ) );
}
function insertSimpleDateInputCtrl( strLabel, strName, strDefault, bShowButton, sWidth )
{
	insertDateInputCtrlImpl( "simple", strLabel, strName, bShowButton, sWidth );
}
var g_ctrlCode = null;
var g_ctrDesc = null;
function CodeInput_onclick( formEmbeded, strName, lClientID, lCodeType, nFields, lLanguageID,nProjectID)
{
	//eval( "g_ctrlCode=formEmbeded."+strName +";");
	//eval( "g_ctrlDesc=formEmbeded.txt_"+strName+";" );
	g_ctrlDesc = $("formEmbeded.txt_"+strName);
	g_ctrlSelected = $(strFieldName);
	var strURL = "CodeXDesc.jsp"
				+ "?lClientID=" + lClientID +"&lCodeType=" + lCodeType + "&nFields=" + nFields + "&lLanguage=" + lLanguageID + "&nProjectID=" + nProjectID;
	var strFeatures = "toolbar=no,menubar=no,scrollbar=no,resizable=no,location=no"
					+ (g_bIsIE ? ",width=446,height=354":",width=460,height=356");
	g_wndSub = window.open( strURL, "CodeXDesc", strFeatures, false );
	g_wndSub.focus();
}
function insertCodeExchangeCtrl( strLabel, strName, strButtonText, lCodeType, nFields, lLanguageID, lDefault, lClientID, strDefault,nProjectID )
{
	if( strLabel == null ) strLabel = "";
	if( strName == null ) strName = "CodeInputCtrlName" + g_nCtrlCount++;
	if( strButtonText == null ) strButtonText = "日期";
	if( lCodeType == null ) lCodeType = 1;
	if( nFields == null ) nFields = 0;
	if( lLanguageID == null ) lLanguageID = 0;
	if( lDefault == null ) lDefault = 0;
	if( lClientID == null ) lClientID = 0;
	if( strDefault == null ) strDefault = "----";
	if(nProjectID==null) nProjectID=1;
	document.writeln(  strLabel
					 + "<INPUT TYPE='HIDDEN' VALUE='" + lDefault + "' ID='" + strName + "' NAME='" + strName + "'>"
					 + "<INPUT onfocus='this.blur()' ID='txt_" + strName + "' NAME='txt_" + strName
					 + "' VALUE='" + strDefault + "'>"
					 + "<INPUT ID='InteleInput' NAME='InteleInput' TYPE='button' LANGUAGE='javascript' "
					 + "onclick='return CodeInput_onclick(this.form,\""+strName+"\","+ lClientID+","+lCodeType+","+nFields+","+lLanguageID+"," + nProjectID +")' "
					 + "VALUE='" + strButtonText + "'>");
}
function setIDandDesc( nID, nLanguageID, sName )
{
	g_ctrlCode.value = nID;
	g_ctrlDesc.value = sName;	
}
function hideInnerCalendar()
{
	DivCalendar.style.visibility='hidden';
	TabCalendar.style.visibility='hidden';
}
