/* 상단 메뉴의 계정 선택 */
function selectAccount(selval)
{
	var setmodule	= 'areport';
	var temp		= selval.split("&");
	var acctype		= $("#acctype").val() == undefined ? '' : $("#acctype").val();
	var params		= "";
	for(i = 0 ; i < temp.length ; i++)
	{
		var data = temp[i].split("=");
		if (data[0] == 'acctype')
		{
			params = data[1];
			break;
		}
	}
	if ((acctype == 'ACT001') || (acctype == 'ACT002') || (acctype == 'ACT007'))
		setmodule = 'campaign';
	
	//if ((currentModule == 'default') || (currentModule == 'monitor'))
	//if (currentModule == 'monitor')
	//	currentModule = "monitor";
	switch(currentModule)
	{
		case 'dashboard':
		case 'campaign':
		case 'landing':
		case 'monitor':
		case 'interest':
		case 'brand':
			setmodule = currentModule;
			break;
		case 'report':
		case 'areport':
		case 'creport':
		case 'greport':
		case 'kreport':
			setmodule = "areport";
			break;
		case 'advertiser':
		case 'account':
		case 'staff':
		case 'agency':
		case 'logs':
			setmodule = currentModule;
			break;
		case 'board':
		case 'codes':
			setmodule = "campaign";
			break;
		case 'adreport':
			setmodule = currentModule;
			break;
	}
	/*
	if ((setmodule == "campaign") || (setmodule == "report"))
	{
		if (selval == '')
		{
			document.location.href = "/";
			return;
		}
	}
	*/
	/*
		광고관리index, 관리에서 오버추어, 구글 선택시 광고관리 해당 매체로 이동
		광고관리index, 관리에서 네이버, 다음 선택시 리포트 해당 매체로 이동 
	*/

	if (setmodule == "dashboard")
	{
		if ($("#pagetype").val() == "campaign")
		{
			if ((params == "ACT001") || (params == "ACT002") || (params == "ACT007"))
				setmodule	= "campaign";
		}
		else if ($("#pagetype").val() == "report")
		{
			setmodule	= "areport";
		}
	}
	else if (setmodule == "campaign")
	{
		setmodule	= "areport";
		if ((params == "ACT001") || (params == "ACT002") || (params == "ACT007"))
			setmodule	= "campaign";
	}
	if (selval != '')
		var location = "/" + setmodule + "/index?" + selval;
	else
		var location = "/" + setmodule + "/index?acctype=" + acctype;	
	if ((currentModule == "monitor") || (currentModule == "brand") || (currentModule == "interest"))
	{
		_val	= $("#volume").val();
		if ((_val == undefined) || (typeof(_val) == undefined)) _val	= "";
		location += "&status=" + $("#status").val() + "&volume=" + _val;
	}

	document.location.href = location;
}

/* 로그아웃 */
function logout()
{
	if (confirm("현재 세션을 종료 하시겠습니까?"))
		$.ajax({
			type: "POST",
			url: "/session/logout",
			data: "",
			success: function(result)
			{
				if ("true" == result)
					location.reload(true);//window.location.href = "/";
				else
					alert("세션이 종료되어 현재 세션을 종료 할수 없습니다.\n현재 화면을 다시 로드 합니다.");
			}
		});
}

/* 검색 날짜 조정 */
function setPeriod(startdate, enddate, form)
{
	$("#startdate").val(startdate);
	$("#enddate").val(enddate);
	form.submit();
}

function printScreen( url)
{
	printwin = window.open( url ,"print","left=10px;top=10px;height=800,width=1200,scrollbars=yes,toolbar=yes,menubar=yes"); 
	printwin.focus(); 

}

function downloadContents(getUrl)
{
	var downType = $("#conv_value > option:selected").val();
	if((downType == "NetInsight_0") && ($("#AllDownReport").val() != undefined))
	{
		if (confirm("리포트를 전환 항목별로 받으시겠습니까?"))
		{
			$("#content").append('<div id="wait_sync_msg" class="ui-widget-overlay" style="width: 100%; height: 100%; z-index: 1001; background:#fff;"><table width="100%" height="90%"><tr><td width="100%" align="center"><img src="/images/common/loading.gif" /><p><h2><strong style="font-color:#0000ff; background:#fff;">해당 리포트를 생성중입니다.<br />잠시만 기다려 주십시요...</strong></h2></p></td></tr></table></div>');
			var j = 0;
			var advertiserId = $("#advertiserId").val();
			$.getJSON("/json/getRoi?advertiserId=" + advertiserId + "&reCaching=" + new Date().getTime(), function(data) {
				$.each(data, function(i, item) {
					roiCount = data.length;
					$.post(getUrl, {'conv_value':'NetInsight_'+item.roiId , 'advertiserId':advertiserId}, function(getHtml) {
						$.ajax({
							type: "POST",
							url: "/html/makeFile",
							data: "html="+getHtml+"&roiId="+item.roiId+"&advertiserId="+advertiserId,
							success: function(fileName)
							{
								j++;
								if (fileName != "") {
									if (j == roiCount) {	// 3초후에 파일저장
										setTimeout('setTimeoutDownload('+advertiserId+')', 300);
									}
								}
							},
							error: function(e){
								alert("처리도중 오류가 발생하였습니다.\n다시 확인 바랍니다.");
						    }
						});
					});
				});
			});
		} else {
			location.href = getUrl;
		}
		return;
	}
	location.href = getUrl;
	return;
}

function setTimeoutDownload(advertiserId)
{
	$("#wait_sync_msg").remove();
	location.href = "/html/makeFile?type=zip&advertiserId="+advertiserId;
}

function checkSelectPeriodType(selval)
{
	var disabletype = {"period" : true, "date" : false , "submitBtn" : false };
	if (selval == 1)
	{
		disabletype["period"]		= false;
		disabletype["date"]			= true;
		disabletype["submitBtn"]	= true;
	}
	document.getElementById("period").disabled		= disabletype["period"];
	document.getElementById("startdate").disabled	= disabletype["date"];
	document.getElementById("enddate").disabled		= disabletype["date"];
	document.getElementById("submitBtn").disabled	= disabletype["submitBtn"];
}

function submitPeriodSearchForm(selval ,form)
{
	var setday = selval.split("_");
	document.getElementById("startdate").disabled	= false;
	document.getElementById("enddate").disabled		= false;
	setPeriod(setday[0], setday[1], form);
}

function rawSelect(form)
{
	if (document.getElementsByName('selectPeriodType').length > 0) {
		//라디오버튼 체크 
		for(var i=0; i<document.getElementsByName('selectPeriodType').length; i++){
			if(document.getElementsByName('selectPeriodType')[i].checked==true){
				checkSelectPeriodType(i+1);
			}
		}
		//날짜값
		var startdate	= document.getElementById('startdate').value;
		var enddate		= document.getElementById('enddate').value;
		var period		= startdate + "_" + enddate;
		submitPeriodSearchForm(period ,form);
	} else {
		form.submit();
	}
}

//검색필드 sumbit
function setSearchValue(form)
{
	document.getElementById("name").value = $("#query").val();
	rawSelect(form);
	form.submit();
	return false;
}
//캠페인 날짜 검색 하위 셀렉트 박스 onchange
function changeCampaign(form)
{
	rawSelect(form);
	form.submit();
}

//광고그룹 날짜 검색 하위 셀렉트 박스 onchange
function changeAdgroup(adgroupId ,form)
{
	var adgroupInfo = adgroupId.split("_");
	$("#campaignId").val(adgroupInfo[0]);
	$("#adgroupId").val(adgroupInfo[1]);
	rawSelect(form);
	form.submit();
}

//키워드 날짜 검색 하위 셀렉트 박스 onchange
function changerKeyword(keywordId ,form)
{
	var keywordInfo = keywordId.split("_");
	$("#adgroupId").val(keywordInfo[0]);
	$("#keywordId").val(keywordInfo[1]);
	rawSelect(form);
	form.submit();
}

//전환 셀렉트 박스 onchange
function changeConv(thisForm, val)
{
	rawSelect(thisForm);
	thisForm.submit();
}

//네이버 노출 영역 onchange
function changeArea(thisForm, val)
{
	rawSelect(thisForm);
	thisForm.submit();
}
