
/** function : check null */
function isNull(tObj) {
	if (tObj == null || tObj == "undefined") return true;
	return false;
}
/** function : define mouseover image */
function imgOver(tObj) {
	if (tObj && fundSelect[tObj.id.split("_")[0]] == null || fundSelect[tObj.id.split("_")[0]] != tObj.id.split("_")[1]) chgImg(tObj, "v");
}

/** function : define mouseout image */
function imgOut(tObj) {
	if (tObj && fundSelect[tObj.id.split("_")[0]] == null ||  fundSelect[tObj.id.split("_")[0]] != tObj.id.split("_")[1]) chgImg(tObj, "x");
}

/** function : define change image */
function chgImg(tObj, mState) {
	if (tObj && tObj.src) {
		tObj.src = (mState == "v")?tObj.src.replace("x.gif","v.gif"):tObj.src.replace("v.gif","x.gif");
	}
}

function selImg(tObj, mState) {
	if (tObj && tObj.src) {
		tObj.src = (mState == "v")?((tObj.src.indexOf("x.gif")>0)?tObj.src.replace("x.gif","o.gif"):tObj.src.replace("v.gif","o.gif")):tObj.src.replace("o.gif","x.gif");
	}
}

/* function : fund table mouse over/out */
function defaultStatus(obj,i){
 if (i == 1) {obj.className = "trbg1";}
 if (i == 2) {obj.className = "trbg2";}
}
function mouseOnTD(obj) {
	obj.className = "trover";
}


function detailFundInfo(linkURL) {
	window.open(linkURL, '','width=730,height=720,screenX=0,screenY=0,titlebar=yes,scrollbars=yes');
}


function directFund() {
	this.list = []; // 상품 초기 값 지정
	this.searchCondition = {
		step1: ["펀드 수익률 순위", "최다 판매 펀드 순위", "새로 나온 펀드"],
		step2: ["조건없음", "국내", "해외"],
		step3: ["조건없음", "채권혼합형", "파생상품형", "주식형", "채권형", "MMF", "주식혼합형", "재간접형"]
	}; // 펀드 조건
	this.addFund = function(fundName, newFund, earning, sale, country, assets, inclination, infolink, pdf, buylink, buytarget) {
		this.list[this.list.length] = {
			"fundName":fundName,       // 펀드명
			"newFund":newFund,         // 새로나온 펀드 구분
			"earning":earning,         // 수익 수치
			"sale":sale,               // 판매 수치
			"country":country,         // 구적 분류
			"assets":assets,           // 자산 유형
			"inclination":inclination, // 투자성향
			"infolink":infolink,       // 펀드 정보 링크값
			"pdf":pdf,                 // PDF 자료 링크값
			"buylink":buylink,         // 매입 링크값
			"buytarget":buytarget      // 매입 링크 타켓
		}
	}; // 펀드 추가
	this.searchFund = function(params) {
		var tmplist1 = []; // step 1 임시 저장
		var tmplist2 = []; // step 2 임시 저장
		var rlist = []; // step 3 결과값
		/* 투자대상 STEP1 정렬 */
		if (params) {
			if (params["step1"] != null) {
				if (params["step1"] == 0) { // 펀드 수익률 순위
					this.list.sort(function(a, b) {
						return a.earning - b.earning;
					});
				}
				if (params["step1"] == 1) { // 최다 판매 펀드 순위
					this.list.sort(function(a, b) {
						return a.sale - b.sale;
					});
				}
				if (params["step1"] == 2) { // 새로 나온 펀드
					for (var i = 0, loopCount = this.list.length; i < loopCount; i++) {
						if (this.list[i].newFund == "Y") tmplist1[tmplist1.length] = this.list[i]; // 국내펀드 선택
					}
				} else {
					tmplist1 = this.list;
				}
			} else {
				tmplist1 = this.list;
			}
			/* 국적분류 STEP2 정렬 */
			if (params["step2"] != null && params["step2"] != 0) { // 국적분류 조건이 있는 경우
				for (var i = 0, loopCount = tmplist1.length; i < loopCount; i++) {
					if (params["step2"] == 1 && tmplist1[i].country == this.searchCondition.step2[params["step2"]]) tmplist2[tmplist2.length] = tmplist1[i]; // 국내펀드 선택
					if (params["step2"] == 2 && tmplist1[i].country == this.searchCondition.step2[params["step2"]]) tmplist2[tmplist2.length] = tmplist1[i]; // 해외펀드 선택
				}
			} else {
				tmplist2 = tmplist1;
			}
			/* 자산유형 STEP3 정렬 */
			if (params["step3"] != null && params["step3"] != 0) { // 자산유형 조건이 있는 경우
				for (var i = 0, loopCount = tmplist2.length; i < loopCount; i++) {
					if (params["step3"] == 1 && tmplist2[i].assets == this.searchCondition.step3[params["step3"]]) rlist[rlist.length] = tmplist2[i]; // 채권혼합형
					if (params["step3"] == 2 && tmplist2[i].assets == this.searchCondition.step3[params["step3"]]) rlist[rlist.length] = tmplist2[i]; // 파생상품형
					if (params["step3"] == 3 && tmplist2[i].assets == this.searchCondition.step3[params["step3"]]) rlist[rlist.length] = tmplist2[i]; // 주식형
					if (params["step3"] == 4 && tmplist2[i].assets == this.searchCondition.step3[params["step3"]]) rlist[rlist.length] = tmplist2[i]; // 채권형
					if (params["step3"] == 5 && tmplist2[i].assets == this.searchCondition.step3[params["step3"]]) rlist[rlist.length] = tmplist2[i]; // MMF
					if (params["step3"] == 6 && tmplist2[i].assets == this.searchCondition.step3[params["step3"]]) rlist[rlist.length] = tmplist2[i]; // 주식혼합형
					if (params["step3"] == 7 && tmplist2[i].assets == this.searchCondition.step3[params["step3"]]) rlist[rlist.length] = tmplist2[i]; // 재간접형
				}
			} else {
				rlist = tmplist2;
			}
			return rlist;
		} else {
			return this.list;
		}
	}; // 펀드 검색
}

/** function : add onload function */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof(window.onload) != "function") {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

function getSearchParam(sVal) {
	var sValArray = [];
	var tmpArray = [];
	var rtnArray = [];
	if (sVal != null) {
		sValArray = sVal.split("&");
		for (var i = 0, loopCount = sValArray.length; i < loopCount; i++) {
			tmpArray = sValArray[i].split("=");
			if (tmpArray[0] == "sp1" || tmpArray[0] == "sp2" || tmpArray[0] == "sp3") {
				rtnArray[rtnArray.length] = {key:tmpArray[0], value:tmpArray[1]};
			}
		}
	}
	return (rtnArray.length < 1)?"":rtnArray;
}

function validSearchParam(sArr, sVal) {
	if (sArr && sArr.length > 0) {
		for (var i = 0, loopCount = sArr.length; i < loopCount; i++) {
			if (sArr[i].key == sVal) return i;
		}
	}
	return -1;
}

var initSearchFund = true;
/** function : view searched fund */
function searchFundList(sIdx, sVal) {
	try {
		var tmpSearch = document.location.search;
		var initSearch;
		var tmpSIdx;
		if (initSearchFund) {
			initSearch = getSearchParam(tmpSearch.substring(tmpSearch.indexOf("?")+1));
			initSearchFund = !initSearchFund;
		}
		if (sIdx == "step1" || initSearch) {
			if (fundSelect.step1 != null) selImg(document.getElementById("step1_"+fundSelect.step1), "x");
			tmpSIdx = validSearchParam(initSearch, "sp1");
			if (initSearch && tmpSIdx != -1) sVal = initSearch[tmpSIdx].value;
			fundSelect.step1 = sVal;
			selImg(document.getElementById("step1_"+sVal), "v");
		}
		if (sIdx == "step2" || initSearch) {
			if (fundSelect.step2 != null) selImg(document.getElementById("step2_"+fundSelect.step2), "x");
			tmpSIdx = validSearchParam(initSearch, "sp2");
			if (initSearch && tmpSIdx != -1) sVal = initSearch[tmpSIdx].value;
			fundSelect.step2 = sVal;
			selImg(document.getElementById("step2_"+sVal), "v");
		}
		if (sIdx == "step3" || (initSearch && initSearch[2] != null)) {
			if (fundSelect.step3 != null) selImg(document.getElementById("step3_"+fundSelect.step3), "x");
			tmpSIdx = validSearchParam(initSearch, "sp3");
			if (initSearch && tmpSIdx != -1) sVal = initSearch[tmpSIdx].value;
			fundSelect.step3 = sVal;
			selImg(document.getElementById("step3_"+sVal), "v");
		}
		var rDiv = document.getElementById("resultlist");
		var tmplist = [];
		var tmpStr = "";
		if (rDiv) {// && fundSelect.step1 != null && fundSelect.step2 != null && fundSelect.step3 != null) { // 검색 조건이 선택 되었을 때 실행
			tmplist = fundList.searchFund({step1:fundSelect.step1, step2:fundSelect.step2, step3:fundSelect.step3});
			//alert(tmplist);
			tmpStr += "<table class=\"fsch_tbl\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\" border=\"0\">\n";
			tmpStr += "<thead>\n";
			tmpStr += "<tr>\n";
			tmpStr += "	<th class=\"text_01 st_01\">펀드명</th>\n";
			tmpStr += "	<th class=\"text_01 st_03\">투자위험성향</th>\n";
			tmpStr += "	<th class=\"text_01 st_04\">자산유형</th>\n";
			tmpStr += "	<th class=\"text_01 st_02\">매입</th>\n";
			tmpStr += "</tr>\n";
			tmpStr += "</thead>\n";
			tmpStr += "<tbody>\n";

			if (tmplist.length > 0) {
				var tmpCidx;
				for (var i = 0, loopCount = tmplist.length; i < loopCount; i++) {
					tmpCidx = (i%2 == 0)?1:2;
					tmpStr += "<tr onmouseover=\"mouseOnTD(this);\" onmouseout=\"defaultStatus(this,"+tmpCidx+");\" class=\"trbg"+tmpCidx+"\">\n";
					tmpStr += "	<td class=\"text_01 st_01\" style=\"position:relative;";
					if (tmplist[i].pdf != "") tmpStr += "padding-right:24px;";
					tmpStr += "\">";

					if (tmplist[i].pdf != "") tmpStr += "<a href=\""+tmplist[i].pdf+"\" target=\"_blank\" style=\"position:absolute;top:5px;right:5px;\"><img src=\"/1/PA_1_1_S5/content/image_cam0/ut_images/ico_pdf.gif\" border=\"0\" align=\"absmiddle\" /></a>"; // PDF 표시
					tmpStr += "<a href=\"#\" onclick=\"detailFundInfo('"+tmplist[i].infolink+"');return false\">";
					if (tmplist[i].sale <= 5) tmpStr += "<img src=\"/1/PA_1_1_S5/content/image_cam0/ut_images/ico_sale"+tmplist[i].sale+".gif\" border=\"0\" align=\"absmiddle\" style=\"padding-right:5px;\" />"; // 판매순위 표시
					if (tmplist[i].newFund == "Y") tmpStr += "<img src=\"/1/PA_1_1_S5/content/image_cam0/ut_images/ico_new.gif\" border=\"0\" align=\"absmiddle\" style=\"padding-right:5px;\" />"; // 새로나온 펀드 표시
					tmpStr += tmplist[i].fundName;
					tmpStr += "</a></td>\n";
					tmpStr += "	<td class=\"text_01 st_03\">"+tmplist[i].inclination+"</td>\n";
					tmpStr += "	<td class=\"text_01 st_04\">"+tmplist[i].assets+"</td>\n";
					tmpStr += "	<td class=\"text_01 st_02\">";
					if (tmplist[i].buylink != null && tmplist[i].buylink != "") {
						tmpStr += "<a href=\""+tmplist[i].buylink+"\"><img src=\"/1/PA_1_1_S5/content/image_cam0/ut_images/fund_sch_btn_buy.gif\" border=\"0\" title=\"매입\"></a>";
					} else {
						tmpStr += "&nbsp;";
					}
					tmpStr += "</td>\n";

					tmpStr += "</tr>\n";
				}
			} else {
				tmpStr += "<tr class=\"trbg1\">\n";
				tmpStr += "	<td class=\"text_02\" colspan=\"4\">검색된 펀드가 없습니다</td>\n";
				tmpStr += "</tr>\n";
			}
			tmpStr += "</tbody>\n";
			tmpStr += "</table>\n";

			rDiv.innerHTML = tmpStr;
		}
	} catch(e) { alert(e); }
}

addLoadEvent(searchFundList);
