//레이어 visivility전환용 함수
function showhidelayer()
{
	var tmp_i, layer_location_id, v, obj, args=showhidelayer.arguments, box, top, left;
	var scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop); 
	var scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft); 
	
	for(tmp_i=0;tmp_i<args.length-2;tmp_i++)
	{
		if((obj=findobj(args[tmp_i])) != null)
		{
			v=args[tmp_i+2];
			
			if(obj.style)
			{
				layer_location_id = document.getElementById(args[tmp_i+1]);
				
				if(layer_location_id.getBoundingClientRect)
				{
					box = layer_location_id.getBoundingClientRect();
					top = box.top + scrollTop;
					left =  box.left + scrollLeft;
				}
				
				obj=obj.style;
				v = (v=="show") ? "visible" : (v="hide") ? "hidden" : v;
			}

      if(args[tmp_i+1])
			{
				obj.left = parseInt(left) + "px";
        obj.top = parseInt(top) + 20 + "px";
			}
			
			obj.visibility = v;
		}
	}
}

//레이어 추천 및 인기제품 플래쉬 추가용 함수
function pdt_opt_swf_show()
{
  var tmp_i, layer_location_id, v, obj, args=pdt_opt_swf_show.arguments, box, top, left;
  var scrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop); 
  var scrollLeft = Math.max(document.documentElement.scrollLeft, document.body.scrollLeft); 
  
  for(tmp_i=0;tmp_i<args.length-2;tmp_i++)
  {
    if((obj=findobj(args[tmp_i])) != null)
    {
      v=args[tmp_i+2];
      
      if(obj.style)
      {
        layer_location_id = document.getElementById(args[tmp_i+1]);
        
        if(layer_location_id.getBoundingClientRect)
        {
          box = layer_location_id.getBoundingClientRect();
          top = box.top + scrollTop;
          left =  box.left + scrollLeft;
        }
        
        obj=obj.style;
        v = (v=="show") ? "visible" : (v="hide") ? "hidden" : v;
      }

      if(args[tmp_i+1])
      {
        obj.left = parseInt(left-2) + "px";
        obj.top = parseInt(top-2) + "px";
      }
      
      obj.visibility = v;
    }
  }
}

//오브젝트 검색용 함수
function findobj(n, d)
{
	var p, i, x;
	
	if(!d)
	{
		d = document;
	}
	
	if((p=n.indexOf("?")) > 0 && parent.frames.length)
	{
		d = parent.frames[n.substring(p+1)].document;
		n = n.substring(0,p);
	}
	
	if(!(x = d[n]) && d.all)
	{
		x=d.all[n];
	} 
	
	for (i = 0 ; !x && i < d.forms.length ; i++)
	{
		x = d.forms[i][n];
	}
	 
	for(i = 0 ; !x && d.layers && i < d.layers.length ; i++)
	{
		x = MM_findObj(n, d.layers[i].document);
	} 
	
	if(!x && d.getElementById)
	{
		x = d.getElementById(n);
	} 
	
	return x;
}

//제품구매용 확인 함수
function ProductInfoSubmit(login, return_url, category, pid, min_buy_count)
{
	var e;
	var form = document.product_info;
  var qty = form.qty.value;
	
  try 
	{
		if (qty < 1) 
    {
			window.alert("수량을 0개 이상 입력해 주세요.");
      return false;
    }
		
		if(login == "Y")
		{
			if (window.confirm("요청하신 서비스는 회원전용입니다. 지금 로그인을 하시겠습니까?")) 
	    {
				if(category == "concern")
				{
					window.location.href="/dealer/member/login.php?return_url=/dealer/pcss/pcss_"+category+".php?md=save&pid="+pid+"&cid=0&save_method=login_after";
				}
				else
				{
					window.location.href = "/dealer/member/login.php?return_url="+return_url;
				}
				
				return false;
	    }
	    else 
	    {
	      return false;
	    }
		}
		else
		{
			if(parseInt(min_buy_count) > 0 && qty < parseInt(min_buy_count))
			{
				window.alert("해당 제품은 최소주문수량 "+parseInt(min_buy_count)+"개 이상 주문하셔야 합니다.");
				return false;
			}
			else
			{
				if(category == "buy")
	      {
	        form.md_after_md.value = "order";
	      }
	      
	      form.action = "/dealer/pcss/pcss_cart.php?return_url="+return_url;
	      form.submit();
	      return false;
			}
		}
  }
	catch(e)
  {
    window.alert(e.message);
    return false;
  }
}

//숫자에 콤마찍기
function number_format(price)
{ 
  price=new String(price);
	
  var tstrLen=price.length;
  var okstr=new String();
  var ii=2;
  var comma=3;
	
  for(var i=tstrLen;i>=0;--i)
	{
    if(ii%comma==0&&i!=tstrLen)
		{
      okstr=","+okstr;
      ii=0;
    }
		
    okstr=price.charAt(i)+okstr;
    ++ii;
  }
	
  okstr=okstr.substring(0,parseInt(okstr.length)-1);
	
  return okstr;
}

//패키지 제품 옵션 변경
var change_category_price = new Array;

function change_pkg_option(pkg_category_no, pkg_product_price, select_pkg_opt_product, select_product_qty, select_product_ori_qty)
{
	var pkg_select_product, select_pkg_opt_price, product_price, product_price_top, product_price_bottom;
	
	if(select_product_qty == "")
	{
		if(pkg_category_no == 4)
		{
			select_product_qty = parseInt(document.getElementById("pkg_opt_qty_"+pkg_category_no).value);
		}
		else
		{
			select_product_qty = 1;
		}
	}
	else
	{
		select_product_qty = parseInt(select_product_qty);
		select_product_ori_qty = parseInt(select_product_ori_qty);
	}
	
	pkg_select_product = document.getElementById("pkg_select_product_"+pkg_category_no);
	product_price = document.getElementById("product_price");
	product_price_top = document.getElementById("product_price_top");
	product_price_bottom = document.getElementById("product_price_bottom");
	
	select_pkg_opt_price = select_pkg_opt_product.split(" | ");
	
	if(pkg_product_price == "")
	{
		pkg_product_price = "0";
	}
	
	if(select_pkg_opt_price[0] == "")
  {
    select_pkg_opt_price[0] = "0";
  }
	
	if(change_category_price[pkg_category_no] > 0)
	{
		product_price.value = parseInt(product_price.value) - parseInt(change_category_price[pkg_category_no]);
	}
	else if(change_category_price[pkg_category_no] < 0)
	{
		product_price.value = parseInt(product_price.value) + (parseInt(change_category_price[pkg_category_no]) * (-1));
	}
	
	product_price.value = (parseInt(product_price.value) + ((parseInt(select_pkg_opt_price[0]) * select_product_qty) - parseInt(pkg_product_price * select_product_ori_qty)));
	product_price_top.innerHTML = ": &nbsp;&nbsp;  "+number_format(product_price.value)+"원";
	product_price_bottom.innerHTML = ": &nbsp;&nbsp;  "+number_format(product_price.value)+"원";
	
	change_category_price[pkg_category_no] = ((parseInt(select_pkg_opt_price[0]) * select_product_qty) - parseInt(pkg_product_price * select_product_ori_qty));
	
	if(select_pkg_opt_price[3] != "")
  {
		if(change_category_price[pkg_category_no] > 0)
		{
			pkg_select_product.innerHTML = "<p><a href=\"javascript:\" title=\"제품명\" onclick=\"javascript:product_viewDisplay('"+select_pkg_opt_price[1]+"', 'pinfo');\" >"+select_pkg_opt_price[2]+" "+select_pkg_opt_price[3]+" +"+number_format(change_category_price[pkg_category_no])+"</a></p>";
		}
		else if(change_category_price[pkg_category_no] < 0)
		{
			pkg_select_product.innerHTML = "<p><a href=\"javascript:\" title=\"제품명\" onclick=\"javascript:product_viewDisplay('"+select_pkg_opt_price[1]+"', 'pinfo');\" >"+select_pkg_opt_price[2]+" "+select_pkg_opt_price[3]+" "+number_format(change_category_price[pkg_category_no])+"</a></p>";
		}
		else
		{
			pkg_select_product.innerHTML = "<p><a href=\"javascript:\" title=\"제품명\" onclick=\"javascript:product_viewDisplay('"+select_pkg_opt_price[1]+"', 'pinfo');\" >"+select_pkg_opt_price[2]+" "+select_pkg_opt_price[3]+"</a></p>";
		}
  }
  else
  {
    pkg_select_product.innerHTML = "<p></p>";
  }
	
	document.getElementById("change_product_img_"+pkg_category_no).src = select_pkg_opt_price[5];
}

//수량변경시 가격 변경
function change_pkg_option_qty(pkg_category_no, pkg_product_price, pkg_opt_qty, pkg_pdt_ori_qty)
{
  var select_pkg_opt_product, str;

	select_pkg_opt_product = document.getElementById("pkg_opt_"+pkg_category_no);
  str = select_pkg_opt_product.options[select_pkg_opt_product.selectedIndex].value;
	change_pkg_option(pkg_category_no, pkg_product_price, str, pkg_opt_qty, pkg_pdt_ori_qty);
}

//제품출력 카테고리 변경
function category_submit(kind)
{
	var change_category;
	
	change_category = document.getElementById(kind+"_category").value;
	
	if(change_category != "none")
	{
		if(kind == "first")
		{
			window.location.href = "/dealer/product/pmain.php?cid="+change_category;
		}
		else if(kind == "second")
		{
			window.location.href = "/dealer/product/plist.php?cid="+change_category;
		}
	}
	else
	{
		window.alert("유효한 카테고리를 선택하세요.");
	}
}

//제품상세보기시 레이어 제품정보 레이어 출력 조정
function pinfo_view_layer(layer_id)
{
	if(layer_id == "pdt_content_view")
	{
		document.getElementById("pdt_content_view").style.display = "block";
		document.getElementById("pdt_content_view_img").src = "/imgs/dealer/product/img_010.gif";
		document.getElementById("delivery_info_view").style.display = "none";
		document.getElementById("delivery_info_view_img").src = "/imgs/dealer/product/img_011.gif";
		document.getElementById("as_info_view").style.display = "none";
		document.getElementById("as_info_view_img").src = "/imgs/dealer/product/img_012.gif";
	}
	else if(layer_id == "delivery_info_view")
	{
		document.getElementById("pdt_content_view").style.display = "none";
		document.getElementById("pdt_content_view_img").src = "/imgs/dealer/product/img_010_01.gif";
    document.getElementById("delivery_info_view").style.display = "block";
		document.getElementById("delivery_info_view_img").src = "/imgs/dealer/product/img_011_01.gif";
    document.getElementById("as_info_view").style.display = "none";
		document.getElementById("as_info_view_img").src = "/imgs/dealer/product/img_012.gif";
	}
	else if(layer_id == "as_info_view")
	{
		document.getElementById("pdt_content_view").style.display = "none";
		document.getElementById("pdt_content_view_img").src = "/imgs/dealer/product/img_010_01.gif";
    document.getElementById("delivery_info_view").style.display = "none";
		document.getElementById("delivery_info_view_img").src = "/imgs/dealer/product/img_011.gif";
    document.getElementById("as_info_view").style.display = "block";
		document.getElementById("as_info_view_img").src = "/imgs/dealer/product/img_012_01.gif";
	}
}

//제품이미지 상세보기 및 확대보기 이미지 변환하기
function pinfo_img_change(pid, pdt_wdate, img_num, size, img_id)
{
	var pdt_img_first_dir = parseInt(pid/500);
	
	if(size)
	{
		size = "-"+size;
	}
	
	if(img_num)
	{
		document.getElementById(img_id).src = "/cc_img_dir/"+pdt_img_first_dir+"/"+pid+"/"+pdt_wdate+"-"+img_num+size;
	}
	else
	{
		document.getElementById(img_id).src = "/cc_img_dir/"+pdt_img_first_dir+"/"+pid+"/"+pdt_wdate+size;
	}
}
