function SelectAll ( )
{
	// 기존 checkBox관련
	with ( document.InquiryForm )
	{
		for (i=1; i <= CheckBoxLength; ++i)
		{
			try{ eval("inquiry"+i); } catch ( e) { continue; }
			eval("inquiry"+i).checked = true;
		}
	}
	// top listing관련 (TopInquiryForm의 form갯수를 여기서 파악한다.)
	// 분기1: toplisting이 있는경우, 분기2 : 2개 이상인지 파악
			toplistdiv  = document.all("toplist_row");
	if(toplistdiv)
	{
		with ( document.TopInquiryForm )
		{
			// 2개 이상인 경우
			if(toplistdiv.length != null){
				for (i=0; i < toplistdiv.length; i++)
				{
					if(eval(document.TopInquiryForm.topinquiry))
					{
					eval(document.TopInquiryForm.topinquiry[i]).checked = true;
					}
					else{}
				}
			}
			// 1개인 경우
			else
			{
				if(document.TopInquiryForm.topinquiry)
				{
				document.TopInquiryForm.topinquiry.checked = true;
				}
				else{}
			}
		}
	}
}

function ClearAll ( )
{
	// 기존 checkBox관련
	with ( document.InquiryForm )
	{
		for (i=1; i <= CheckBoxLength; ++i)
		{
			try{ eval("inquiry"+i); } catch ( e) { continue; }
			eval("inquiry"+i).checked =  false;
		}
	}

	// top listing관련 (TopInquiryForm의 form갯수를 여기서 파악한다.)
	// 분기1: toplisting이 있는경우, 분기2 : 2개 이상인지 파악
	if(toplistdiv)
	{
		with ( document.TopInquiryForm )
		{
			// 2개 이상인 경우
			if(toplistdiv.length != null){
				for (i=0; i < toplistdiv.length; i++)
				{
					if(eval(document.TopInquiryForm.topinquiry))
					{
					eval(document.TopInquiryForm.topinquiry[i]).checked = false;
					}
					else{}
				}
			}
			// 1개인 경우
			else
			{
				if(document.TopInquiryForm.topinquiry){
				document.TopInquiryForm.topinquiry.checked = false;
				}
				else{}
			}
		}
	}

}

function intoTheBasket ( job )
{
	var SelectedInquiry = gatherCheckBox() ;
	if ( SelectedInquiry == null || SelectedInquiry == "")
	{
		alert('Please select an item(s).');
		return;
	}

	var inquiry = "";
	var argc = intoTheBasket.arguments.length;
	var argv = intoTheBasket.arguments;

	// 단일 Cookie( 업체 홈페이지)
	if (argc > 1)
	{
		var checkCount = getSelectCount();    // 현재의 TotalCount

		if(checkCount > 40 )
		{
			alert("Sorry! You cannot send more than 40 inquiries at once.");
			return;
		}
		else{
			inquiry = argv[1] +"~" + ( (getCookie(job)==null || getCookie(job) == "null") ? "" : getCookie(job) );
			if (ExistInBasket ( job, argv[1] ) == true)
			{
				alert ('You have already selected this item.');
				return;
			}
		}
	}
	// 다중 Cookie (Search, New-Page)
	else
	{
		inquiry = gatherCheckBox();
		if (inquiry){
			inquiry = inquiry + ( (getCookie(job)==null || getCookie(job) == "null") ? "" : getCookie(job) );
		}
		else{
			return;
		}
	}

	setCookie ( job, inquiry );
	alert('The item(s) you selected have been added to basket successfully!');
	location.reload();
}

function sendOneInq(inquiry)
{
	var URL = "http://www.ec21.com/global/basic/SendInquiry.jsp?inq_data=" + inquiry;
	window.open(URL, "small", "toolbar=1,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=300,height=400,top=100,left=200");
}

function sendInquiry ( job )
{
	var SelectedInquiry = gatherCheckBox() ;

	if ( SelectedInquiry == null || SelectedInquiry == "")
	{
		alert('Please select an item(s).');
		return;
	}

	var URL = "http://www.ec21.com/global/basic/SendInquiry.jsp";
	var Param = "";
	var Mulit = "";

//		Param = "fromwhere="+job+"&inq_data="+ SelectedInquiry;
	if(SelectedInquiry.substring(0,SelectedInquiry.length-1).indexOf("~") > 0 )
	{
		Multi = "multi=Y";
	}
	else
	{
		Multi = "";
	}
	Param = "&inq_data="+ SelectedInquiry.substring(0,SelectedInquiry.length-1);
	URL = URL + "?" + Multi +Param ;
	open_win ( URL, 300, 400 );
}

/* 체크된 리스트 가져옴 */
function gatherCheckBox (  )
{
	//alert("gather");
	var checkCount = getSelectCount();    // 현재의 TotalCount
	var Inquiries = "";

	//일반 인콰이어리 폼
	if ( document.InquiryForm )
	{
		for (i=0; i <document.InquiryForm.length; i++)
		{
			// 2009.10.05 hyuk2
			if (document.InquiryForm[i].name != "allChk" && document.InquiryForm[i].type=="checkbox" && document.InquiryForm[i].checked)
			{
				Inquiries += document.InquiryForm[i].value +"~";
				checkCount ++;
			}
		}
	}


	//toplist featured form
	toplistdiv  = document.getElementById("toplist_row");

	if (toplistdiv)
	{
		var topCheckbox = document.TopInquiryForm.topinquiry;

		for (i=0; i<topCheckbox.length; i++)
		{
			if (topCheckbox[i].checked)
			{
				Inquiries += topCheckbox[i].value +"~";
				checkCount ++;
			}
		}
	}


	// basket에 담을 수 있는 갯수 : Max 40
	if(checkCount > 40)
	{
		alert("Sorry! You cannot send more than 40 inquiries at once.");
		return;
	} 
	else 
	{
		return Inquiries;
	}
}

function getSelectCount ( )
{
	var TotalCount = 0;
	var inquiries = "";
	inquiries = getCookie ( "ProductInquiry" );
	collect = parseString ( inquiries, "~" );
	TotalCount += collect.length;

	inquiries = getCookie ( "TradeInquiry" );
	collect = parseString ( inquiries, "~" );
	TotalCount += collect.length;

	inquiries = getCookie ( "CompanyInquiry" );
	collect = parseString ( inquiries, "~" );
	TotalCount += collect.length;
	return TotalCount;
}

function ExistInBasket ( job, inquiry )
{
	inquiries = getCookie ( job );
	collect = parseString ( inquiries, "~" );
	for (var i=0; i < collect.length; ++i)
	{
		if (collect[i] == inquiry)
		{
			return true;
		}
	}
	return false;
}

function deleteInquiryInBasket ( job, inquiry )
{
	var NewInquiry = "";
	var key = inquiry.substring(0,inquiry.indexOf("~"));

	inquiries = getCookie ( job );
	collect = parseString ( inquiries, "~" );
	for (var i=0; i < collect.length; ++i)
	{

		if (collect[i] == key)
		{
			collect[i] = "";
		}
		NewInquiry += ( collect[i] == "" ) ? "" : collect[i]+"~";
	}

	setCookie ( job, NewInquiry );
}

function getInquiryCountinBasket ( job )
{
	inquiries = getCookie ( job );
	collect = parseString ( inquiries, "~" );
	return collect.length;
}

function getTotalInquiryCountinBasket (  )
{
	var totalcount = 0;
	totalcount = getInquiryCountinBasket ( 'ProductInquiry' );
	totalcount += getInquiryCountinBasket ( 'TradeInquiry' );
	totalcount += getInquiryCountinBasket ( 'CompanyInquiry' );
	return totalcount;
}

function clearBasket ( job )
{
	setCookie(job, "");
	location.reload();
}

function viewBasket ( job )
{
	alert(getCookie ( job ));
}

function isLoginUser ( )
{
	var user_id = getCookie ( "user" );
	if ( user_id == null || user_id == "" || user_id == "null" )
		return false;
	else
		return true;
}

function isChargedUser ( )
{
	var role = getCookie ( "ec_role" );
	if ( role == null || role == "" || role == "null" )
		return false;
	else
		return true;
}