/** Title:			CTRSAction.js
 *  Description:
 *		Define CTRSAction Object
 *  Copyright: 		www.trs.com.cn
 *  Company: 		TRS Info. Ltd.
 *  Author:			CH
 *  Created:		2004-11-24 08:38
 *  Vesion:			1.0
 *  Last EditTime:	2004-11-24/2004-11-24
 *	Update Logs:
 *		CH@2004-11-24 Created File
 *	Note:
 *		
 *	Depends:
 *		CTRSReqeustParam.js
 *		CTRSHashtable.js
 *
 *	Examples:
 *		see CTRSAction_test.html
 *			
 */
function CTRSAction_doAction(){
	window.location.href = this.getActionURL();
}	

function CTRSAction_getActionURL(){
	var sURL		= this.sActionURL;
	var nStartPose  = sURL.indexOf("?");
	if(nStartPose >= 0)
		sURL += "&";
	else
		sURL += "?";

	return sURL + this.oActionParam.toURLParameters();
}

function CTRSAction_isSelfLocation(_sURL){
	if(_sURL == null)return true;

	var sSelfURL		= window.location.href;
	var nStartPose		= sSelfURL.indexOf("?");
	if(nStartPose >= 0)
		sSelfURL = sSelfURL.substring(0, nStartPose);
	
	nStartPose		= sSelfURL.lastIndexOf("/");
	if(nStartPose >= 0)
		sSelfURL = sSelfURL.substring(nStartPose+1);
	
	return (sSelfURL == _sURL);
}


function CTRSAction_getLocationURL(){
	var sURL		= window.location.href;
	var nStartPose  = sURL.indexOf("?");
	if(nStartPose >= 0)
		sURL = sURL.substring(0, nStartPose);

	return sURL;
}

function CTRSAction_getReferLocation(){
	var sURL		= window.location.href;
	var nStartPose  = sURL.indexOf("?");
	if(nStartPose >= 0)
		sURL = sURL.substring(0, nStartPose);
	
	nStartPose  = sURL.indexOf("?");
	if(nStartPose >= 0)
		sURL += "&";
	else
		sURL += "?";

	sURL += TRSRequestParam.toURLParameters()

	return sURL;
}

function CTRSAction_doDialogAction(_nWidth, _nHeight, _ifscroll, _oArgs){
	//1.verify parameters
	var nWidth	= _nWidth	|| 200;
	var nHeight = _nHeight	|| 200;

	var nLeft	= (window.screen.availWidth - nWidth)/2;
	var nTop	= (window.screen.availHeight - nHeight)/2;

	var ifscroll = _ifscroll || "Yes";
	//2.Construct parameters for dialog
	var sFeatures		= "dialogHeight: "+nHeight+"px; dialogWidth: "+nWidth+"px; "
						+ "dialogTop: "+nTop+"; dialogLeft: "+nLeft+"; "
						+ "center: Yes; scroll:"+ifscroll+";help: No; resizable: No; status: No;";
	//3.display Dialog
	return window.showModalDialog(this.getActionURL(), _oArgs, sFeatures);	
}



function CTRSAction_doXMLHttpAction(_sPostData){
	//1.verify parameters
	var oHttp = new ActiveXObject("Microsoft.XMLHTTP"); //��bXMLHTTP����
	
	oHttp.open("POST", this.getActionURL(), false);
	
	//��ʼ�������.............��..
	oHttp.send(_sPostData);

	delete oHttp;
	return oHttp.responseText;		
}

function CTRSAction_inheritParameters(){
	this.oActionParam.setAllParameters(TRSRequestParam);
}

//Self Action 1----refreshMe
	function CTRSAction_refreshMe(){
		var oTRSAction = new CTRSAction();
		oTRSAction.doAction();
	}

//Self Action 2----gotoPage
	function CTRSAction_gotoPage(_nPageIndex){
		var oTRSAction = new CTRSAction();
		oTRSAction.setParameter("PageIndex", _nPageIndex);
		oTRSAction.doAction();	
	}


//Self Action 3----doSearch
	function CTRSAction_doSearch(_oForm){
		var oForm = _oForm;
		if(_oForm == null)
			oForm = document.frmSearch;
		if(oForm.SearchKey == null || oForm.SearchValue == null){
			alert("Search Form Invalid!");
			return;
		}

		var oTRSAction = new CTRSAction();
		
		oTRSAction.setParameter("SearchKey", oForm.SearchKey.value);
		oTRSAction.setParameter("SearchValue", oForm.SearchValue.value);
		
		oTRSAction.setParameter("PageIndex", 1);
		
		oTRSAction.doAction();
	}

//Self Action 4----doOrderBy
	function CTRSAction_doOrderBy(_sOrderField, _sOrderType){
		var oTRSAction = new CTRSAction();
		
		oTRSAction.setParameter("OrderField", _sOrderField);
		oTRSAction.setParameter("OrderType", _sOrderType);
		
		oTRSAction.setParameter("PageIndex", 1);
		
		oTRSAction.doAction();
	}


function CTRSAction_doOpenWinAction(_nWidth, _nHeight){
	var args = [];
	args[0] = this.getActionURL();
	args[1] = _nWidth;
	args[2] = _nHeight;
	args[3] = window.top;

	window.showModalDialog('../include/open_win.jsp', args,'dialogWidth:1px;dialogHeight:1px;dialogTop:1;dialogLeft:1;');
}

function CTRSAction(_sActionURL){
//Define Properties

	//Init Action Param
	var __sActionURL = encodeURI(_sActionURL);		                
	//alert(_sActionURL+"\r\nCTRSAction\r\n"+__sActionURL);
	_sActionURL = __sActionURL;
	
	try{
		this.oActionParam = new CTRSRequestParam();
	}catch(e){
		alert("Not include file CTRSRequestParam.js!");
		return;
	}	

	//Set Action Parameter
	this.setParameter		= function(_sParameterName, _sValue){
		return this.oActionParam.setParameter(_sParameterName, _sValue);
	}

	//If Self Action
	if(CTRSAction_isSelfLocation(_sActionURL)){
		this.sActionURL = CTRSAction_getLocationURL();
		this.oActionParam.setAllParameters(TRSRequestParam);		
	}else{
		//this.setParameter("TRSRefer", CTRSAction_getReferLocation());
		this.sActionURL = _sActionURL;
	}	

//Define Methods
	this.getActionURL		= CTRSAction_getActionURL;
	this.doAction			= CTRSAction_doAction;
	this.doDialogAction		= CTRSAction_doDialogAction;
	this.doOpenWinAction	= CTRSAction_doOpenWinAction;
	this.doXMLHttpAction	= CTRSAction_doXMLHttpAction;
	this.getRemoteData		= CTRSAction_doXMLHttpAction;
	this.inheritParameters	= CTRSAction_inheritParameters;

}

//Define Methods 	

