var contextOfferPath = "/offers_test";
var contextProductPath = "/ec-market_test";

/**
* 페이지 이동
* @param num
*/
function gotoPage( num ) {
	var f = document.forms["searchForm"];

	f.pageNum.value = num;
	f.submit();
}

function searchKwd() {

	var obj = document.forms["GNBsearchForm"];
	var obj2 = document.forms["searchForm"];

	var keyword = "";
	keyword = obj.keyword.value;

    if ( keyword == "" ) {
    	alert( "Please enter a keyword.");
        return false;
	}

	if ( !isKwdValid( keyword) ) {
		return false;
	}
	obj.submit();
}

/**
* 검색어 길이제한 및 <, > 입력차단
* @param kwd
*/
function isKwdValid( kwd) {
	var isValid = true;

	if ( kwd.length > 256 ) {
		alert("You can enter up to 256 letters for your keyword.");
		isValid = false;
	} else if ( kwd.indexOf(">") > -1 || kwd.indexOf("<") > -1 ) {
		alert("Please do not enter these signs <  ,  >  in the keyword.");
		isValid = false;
	}
	return isValid;
}

/**
* 카테고리 조건 검색
* @param categoryID
*/
function goCategory( categoryID ) {
	var f = document.forms["searchForm"];
	f.category.value = categoryID;
	f.submit();
}

/**
* 국가 조건 검색
* @param countryCd
*/
function goCountry( countryCd ) {
	var f = document.forms["searchForm"];
	f.countryCd.value = countryCd;
	f.submit();
}

/**
* 그룹 리스트 toggle
* @param isGroupList
*/
function goGroupLeads( isGroupList ) {
	var f = document.forms["searchForm"];
	if(isGroupList == "Y") {
		f.isGroupList.value = "N";
	} else {
		f.isGroupList.value = "Y";
	}
	f.submit();
}

/**
* 날짜 구간 검색
*
*/
function goDate() {

	var f = document.forms["searchForm"];

	for (var i = 0; i < document.getElementById('sdate').options.length; i++) {
		if (document.getElementById('sdate').options[i].selected) {
				f.startDate.value = document.getElementById('sdate').options[i].value;
				break;
		}
	}
	premiumChk();
	f.submit();
}

/**
* Certification 검색
*
*/
function goCertification() {

	var f = document.forms["searchForm"];

	for (var i = 0; i < document.getElementById('ctfct_cd').options.length; i++) {
		if (document.getElementById('ctfct_cd').options[i].selected) {
				f.certification.value = document.getElementById('ctfct_cd').options[i].value;
				break;
		}
	}
	premiumChk();
	f.submit();
}

/**
* 프리미엄 검색
*
*/
function premiumChk() {
	var f = document.forms["searchForm"];

	if(document.getElementById('pm').checked == true) {
		f.premium.value = "Y";
	} else {
		f.premium.value = "N";
	}
}

/**
* OFFER ROW별 그룹핑 리스트 페이지 호출
* @param memberID
*/
function goOfferComList( memberID ) {
	var f = document.forms["searchForm"];

	keyword = f.keyword.value;
	categoryID = f.categoryID.value;
	countryCd = f.countryCd.value;
	startDate = f.startDate.value;
	premium = f.premium.value;
	offerDiv = f.offerDiv.value;

	url = contextOfferPath + "/OffersListCom.jsp?";
	url += "memberID=" + memberID;
	url += "&keyword=" + keyword;
	url += "&categoryID=" + categoryID;
	url += "&countryCd=" + countryCd;
	url += "&startDate=" + startDate;
	url += "&premium=" + premium;
	url += "&offerDiv=" + offerDiv;

	location.href = url;
}

/**
* PRODUCT ROW별 그룹핑 리스트 페이지 호출
* @param memberID
*/
function goProductComList( memberID ) {
	var f = document.forms["searchForm"];

	keyword = f.keyword.value;
	categoryID = f.categoryID.value;
	countryCd = f.countryCd.value;
	startDate = f.startDate.value;
	premium = f.premium.value;

	url = contextProductPath + "/ECMarketListCom.jsp?";
	url += "memberID=" + memberID;
	url += "&keyword=" + keyword;
	url += "&categoryID=" + categoryID;
	url += "&countryCd=" + countryCd;
	url += "&startDate=" + startDate;
	url += "&premium=" + premium;

	location.href = url;
}


function srchOpt()
{
	var f  = document.forms["searchForm"];

  if(document.getElementById('sdate') != null)
  {
		for (var i = 0; i < document.getElementById('sdate').options.length; i++) {
			if (document.getElementById('sdate').options[i].selected) {
					f.startDate.value = document.getElementById('sdate').options[i].value;
					break;
			}
		}
  }

  if(document.getElementById('ctfct_cd') != null)
  {
	  for (var i = 0; i < document.getElementById('ctfct_cd').options.length; i++) {
			if (document.getElementById('ctfct_cd').options[i].selected) {
					f.certification.value = document.getElementById('ctfct_cd').options[i].value;
					break;
			}
		}
  }

  if ( document.getElementById('cotype') != null )
  {
	  for (var i = 0; i < document.getElementById('cotype').options.length; i++)
		{
			if ( document.getElementById('cotype').options[i].selected )
			{
				f.company_type.value = document.getElementById('cotype').options[i].value;
				break;
			}
		}
  }
	if ( document.getElementById('pm').checked == true)
	{
		f.premium.value = "Y";
	}
	else
	{
		f.premium.value = "N";
	}

	f.pageNum.value = "1";

	f.submit();
}

function allChange(id)
{
	if ( id == 'allChk' )
	{
		if ( document.getElementById(id).checked == true )
		{
			document.getElementById('allChk2').checked = true;
			SelectAll();
		}
		else if ( document.getElementById(id).checked == false )
		{
			document.getElementById('allChk2').checked = false;
			ClearAll();
		}
	}
	else if ( id == 'allChk2' )
	{
		if ( document.getElementById(id).checked == true )
		{
			document.getElementById('allChk').checked = true;
			SelectAll();
		}
		else if ( document.getElementById(id).checked == false )
		{
			document.getElementById('allChk').checked = false;
			ClearAll();
		}
	}
}

function getCategoryEnmForUrl(sTar)
{
    sTar = sTar.replace(/\'s/g, "s");
	  sTar = sTar.replace(/[/()+:, &]/g, "_");
	  sTar = sTar.replace(/__*/g, "_");
	  sTar = sTar.replace(/^-*/g, "");

	  return sTar;
}

function getKeywordForUrl(sTark)
{
    sTark = sTark.replace(/[ "]/g, "_");
    sTark = sTark.replace(/__*/g, "_");
	  sTark = escape(sTark);

	  return sTark;
}

function SearchGNB()
{
	  var f  = document.forms["GNBsearchForm"];

	  var keyword  = f.keyword.value;
	  var category = f.category.value;
	  var category_enm = f.category.options[f.category.selectedIndex].text;
	  var l_path;

	  if(keyword.length < 1)
	  {
	      alert('Please input a keyword.');
	      f.keyword.focus();
	      return false;
	  }
	  else
	  {
				keyword = trim_keyword(keyword);

				if ( keyword.length <1 )
				{
					alert('Pleas input a keyword.');
					f.keyword.focus();
					return false;
			  }

	    	if ( !isKwdValid( keyword) )
			     return false;
	  }

	  keyword = getKeywordForUrl(keyword);
	  category_enm = getCategoryEnmForUrl(category_enm);

	  if(keyword.length > 0 && category.length > 1)
	  	  l_path = category_enm + '--' + category + '/1/' + keyword + '.html';
	  else if(keyword.length > 0 && category.length < 2)
	  	  l_path = keyword + '.html';

	  if(f.preKwd != null)
	      l_path = l_path + "?preKwd=" + f.preKwd.value;

		if(f.st.value =='0')
		{
	    location.href = 'http://www.ec21.com/ec-market/'+ l_path;
			return false;
		}
		else if(f.st.value =='1')
		{
	    location.href = 'http://www.ec21.com/offers/' + l_path;
			return false;
		}
	  else if(f.st.value =='2')
		{
	    location.href = 'http://importer.ec21.com/buy_leads/' + l_path;
			return false;
		}
		else if(f.st.value =='3')
		{
	    location.href = 'http://www.ec21.com/companies/' + l_path;
			return false;
		}

		return false;
}

function ArchiveSearchGNB()
{
	  var f  = document.forms["GNBsearchForm"];

	  var keyword  = f.keyword.value;
	  var category = f.category.value;
	  var category_enm = f.category.options[f.category.selectedIndex].text;
	  var l_path;

	  if(keyword.length < 1)
	  {
	      alert('Please input a keyword.');
	      f.keyword.focus();
	      return false;
	  }
	  else
	  {
				keyword = trim_keyword(keyword);

				if ( keyword.length <1 )
				{
					alert('Pleas input a keyword.');
					f.keyword.focus();
					return false;
			  }

	    	if ( !isKwdValid( keyword) )
			     return false;
	  }

	  keyword = getKeywordForUrl(keyword);
	  category_enm = getCategoryEnmForUrl(category_enm);

	  if(keyword.length > 0 && category.length > 1)
	  	  l_path = category_enm + '--' + category + '/1/' + keyword + '.html';
	  else if(keyword.length > 0 && category.length < 2)
	  	  l_path = keyword + '.html';

	  if(f.preKwd != null)
	      l_path = l_path + "?preKwd=" + f.preKwd.value;

		if(f.st.value =='0')
		{
	    location.href = 'http://www.ec21.com/ec-market/'+ l_path;
			return false;
		}
		else if(f.st.value =='1')
		{
	    location.href = 'http://www.ec21.com/a_offers/' + l_path;
			return false;
		}
	  else if(f.st.value =='2')
		{
	    location.href = 'http://importer.ec21.com/a_buy_leads/' + l_path;
			return false;
		}
		else if(f.st.value =='3')
		{
	    location.href = 'http://www.ec21.com/companies/' + l_path;
			return false;
		}

		return false;
}

function changeSearchSection(sType)
{
		//select box
		var form = document.searchForm;
		var typeSel = document.getElementById('selsection');

		switch(sType)
		{
			case("product"):{
				typeSel.value="0"; break;}
			case("sell"):{
				typeSel.value="1"; break;}
			case("buy"):{
				typeSel.value="2"; break;}
			case("company"):{
				typeSel.value="3"; break;}
		}

		//boxing - reset
		var searchTabProduct = document.getElementById("searchTabProduct");
		var searchTabSell = document.getElementById("searchTabSell");
		var searchTabBuy = document.getElementById("searchTabBuy");
		var searchTabCompany = document.getElementById("searchTabCompany");

		searchTabProduct.className = "";
		searchTabSell.className = "";
		searchTabBuy.className = "";
		searchTabCompany.className = "";

		switch(sType)
		{
			case("product"):{ searchTabProduct.className = "current"; break; }
			case("sell"):{ searchTabSell.className = "current"; break; }
			case("buy"):{ searchTabBuy.className = "current"; break; }
			case("company"):{ searchTabCompany.className = "current"; break; }
		}
}