String.prototype.trim=function(){
        return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.ltrim=function(){
        return this.replace(/(^\s*)/g,"");
}
String.prototype.rtrim=function(){
        return this.replace(/(\s*$)/g,"");
}

function getElement(id) {
	return document.getElementById(id);
}

function createElement(type) {
	return document.createElement(type);
}
function removeElement(element) {
	try {
		if (element) {
			element.parentNode.removeChild(element);
		}
	}
	catch (e) {
	}
}

function hideElement(element) {
	if (element) {
		element.style.display = "none";
	}
}

function hideElementV(element) {
	if (element) {
		element.style.visibility = "hidden";
		element.style.display = "";
	}
}


function showElement(element) {
	if (element) {
		element.style.display = "";
		element.style.visibility = "";
	}
}

function clearElement(element) {
	for (var i in element) {
		try {
			
			element[i] = null;
		}
		catch (e) {
			continue;
		}
	}
}

var needFlush=false;
var context_path = "";
var add_favorite_fund = context_path+"/favorite/addFavoriteFund.htm";
var delete_favorite_fund = context_path+"/favorite/deleteFavoriteFund.htm";
var myfund_maintenance_record = context_path+"/myfund/maintenanceTradeRecord.htm";
var favorite_fund_info = context_path+"/favorite/favoriteFundInfo.htm";
var favorite_fund_list = context_path+"/favorite/favoriteFundList.htm";
var success_code='0000';
	
/**
  * 删除我关注的基金
  */
function deleteFavoriteFund(id,fundName){
	var popup = popups.createPopup("ConfirmDeleteMyfund", fundName ,
		function(){
			window.fundCode=id;
			var paras = '{url:"'+delete_favorite_fund+'?id='+id+'",callBack:favoriteFundResult}';
			needFlush = true;
			ajaxRequest(paras);
		}
	);
	popup.show();
}


/**
  * 查询开放式基金买卖记录
  */
function queryOTradeRecord(id){
	var paras = '{url:"'+myfund_query_record+'?method=editTradeRecord&id='+id+'",callBack:queryOTradeRecordResult}';
	ajaxRequest(paras);
}

/**
  * 添加关注的基金
  */
function addFavoriteFund(fundCode,flush){
	if (username==""){
		popups.createPopup("AlertError", "您尚未登录，您在登录后才能添加您关注的基金").show();
		return;
	}
		
  	if (flush)
  		needFlush = true;
  	else
  		needFlush = false;
  	window.fundCode=fundCode;
  	var uri=add_favorite_fund + "?fundcode="+fundCode;
	var bindArgs = {
	    url: uri,
	    error: favoriteFundResult,
	    load: favoriteFundResult,
	    mimetype: "text/plain",
	    encoding:   "UTF-8"
	};
	dojo.io.bind(bindArgs);
}
function addFavoriteFund1(frm){
	addFavoriteFund(frm.fundCode.value,true);
}

function favoriteFundResult(type, data, evt){
	if (type=="error"){
		alert (data.message);
		return;
	}
	data = data.trim();
	var arrayData = data.split("|");
	if (arrayData.length==4){
		var alertMessage="添加";
		
		if (arrayData[0]=='0000'){
			if (arrayData[1]=='01'){
				alertMessage="添加";
			}else if(arrayData[1]=='02'){
				alertMessage="修改";
			}else if(arrayData[1]=='03'){
				alertMessage="删除";
			}
			alertMessage +=arrayData[3]+"成功。";
			popups.createPopup("AlertSuccess", alertMessage).show();
			if (arrayData[1]=='01'){
				addFavoriteFundHook();
			}else if(arrayData[1]=='03'){
				deleteFavoriteFundHook();
			}else{
				reloadFavoriteFund();
			}
			window.fundCode="";
		}else{
			alertMessage ="操作失败，原因为：" + arrayData[3];
			popups.createPopup("AlertError", alertMessage).show();
		}
	}
}
function addFavoriteFundHook(){
}

function deleteFavoriteFundHook(){
}


/**
  * 重新load我的关注的基金数据
  */
function reloadFavoriteFund(){
	if (!needFlush)
		return;
	needFlush = false;
	//开放式基金
	var paras = '{url:"'+favorite_fund_info+'?method=OpenFundList",callBack:reloadOpenFundList}';
	ajaxRequest(paras);
	var paras = '{url:"'+favorite_fund_info+'?method=CloseFundList",callBack:reloadCloseFundList}';
	ajaxRequest(paras);
	
	//基金新闻
	var paras = '{url:"'+favorite_fund_info+'?method=FavoriteFundInfo",callBack:reloadFavoriteFundInfo}';
	ajaxRequest(paras);
}

function myFavoriteFundList(){
	var paras = '{url:"'+favorite_fund_list+'",callBack:myFavoriteFundListResult}';
	ajaxRequest(paras);
}

function myFavoriteFundListResult(type, data, evt){
}

function reloadOpenFundList(type, data, evt){
	if (type=="error"){
		alert (data.message);
		return;
	}
	getElement("openFundList").innerHTML=data;
}

function reloadCloseFundList(type, data, evt){
	if (type=="error"){
		alert (data.message);
		return;
	}
	getElement("closeFundList").innerHTML=data;
}

function reloadFavoriteFundInfo(type, data, evt){
	if (type=="error"){
		alert (data.message);
		return;
	}
	getElement("fundInfo").innerHTML=data;
}

function ajaxRequest(datastr) {
	var obj="";
	eval("obj = "+datastr+";");
	
	var bindArgs = {
	    url: obj.url,
	    useCache: false,
	    preventCache: true,
	    error: obj.callBack,
	    load: obj.callBack,
	    mimetype: "text/plain",
	    encoding:   "UTF-8"
	};
	dojo.io.bind(bindArgs);
}

function ajaxFormRequest(datastr,frm) {
	var obj="";
	eval("obj = "+datastr+";");
	var bindArgs = {
	    url: obj.url,
	    useCache: false,
	    preventCache: true,
	    error: obj.callBack,
	    load: obj.callBack,
	    mimetype: "text/plain",
	    encoding:   "UTF-8",
	    formNode: frm
	};
	dojo.io.bind(bindArgs);
}

function showRecordList(fundCode){
	var showLabel = getElement("showLabel_"+fundCode);
	if (!showLabel)
		return;
	if (showLabel.innerHTML=="点击显示"){
		showLabel.innerHTML="点击隐藏";
		getElement("opensign_"+fundCode).className="opensign1";
		showElement(getElement("record_"+fundCode));
	}else{
		showLabel.innerHTML="点击显示";
		getElement("opensign_"+fundCode).className="opensign2";
		hideElement(getElement("record_"+fundCode));
	}
}


	
function main () {
	window.exponentController = new ExponentController();
	exponentController.start();
	window.timeZone = new TimeZone("timeZone");
	window.timeZone.count();
	window.mask = new Mask();
	window.popups = new Popups("popupContainer");
}
