var total_price = 0;
var quota_total_price = 0;

function get_option_price( form, category_key )
{
	ret_val = 0;

	for( i = 1; i <= 10; i++ )
	{
		obj = form["option" + i + "[]"];

		if( typeof( obj.length ) == "number" )
		{
			option_price = obj[category_key].price;
		}
		else
		{
			option_price = obj.price;
		}

		ret_val += new Number( option_price );
	}

	return ret_val;
}

function reset_option_price( form, category_key )
{
	for( i = 1; i <= 10; i++ )
	{
		obj = form["option" + i + "[]"];

		if( typeof( obj.length ) == "number" )
		{
			obj[category_key].price = 0;
			obj[category_key].value = "";
		}
		else
		{
			obj.price = 0;
			obj.value = "";
		}
	}

	obj = document.all['option_span_' + category_key];
	obj.style.display = 'none';
}

function set_product_price( form, main_cate_no, category_key, f_price_unit1, f_price_unit2 )
{
	// unit : ´Ü°¡
	// price : »óÇ°ÀÇ ÃÑ ±Ý¾× =  ( ´Ü°¡ * ¼ö·® ) + ¿É¼Ç

	unit = 0;
	price = 0;
	is_option_pop = true;
    try {
    	if( typeof( form["tmp_product_price[]"] ) == "object" )
    	{
    		// »óÇ°ÀÌ ¿©·¯°³ÀÏ¶§
    		if( typeof( form["tmp_product_price[]"].length ) == "number" )
    		{
    			unit_index = form["product_no[]"][category_key].selectedIndex;
    
    			if( unit_index > 0 )
    			{
    				unit_name = form["product_no[]"][category_key].options[unit_index].name;
    				unit_s = unit_name.split( "=||=" );
    
    				unit = unit_s[0]
    				quantity = form["quantity[]"][category_key].value;
    				price = "" + unit * quantity;
    				form["tmp_product_price[]"][category_key].value = price;
    			}
    			else
    			{
    				form["tmp_product_price[]"][category_key].value = 0;
    			}
    
    			if( unit_index !=  form["prev_product_index[]"][category_key].value )
    			{
    				reset_option_price( form, category_key );
    			}
    			else
    			{
    				is_option_pop = false;
    			}
    
    			form["prev_product_index[]"][category_key].value = unit_index;
    		}
    		// »óÇ°ÀÌ ÇÏ³ªÀÏ¶§
    		else
    		{
    			unit_index = form["product_no[]"].selectedIndex
    
    			if( unit_index > 0 )
    			{
    				unit_name = form["product_no[]"].options[unit_index].name
    				unit_s = unit_name.split( "=||=" )
    
    				unit = unit_s[0]
    				quantity = form["quantity[]"].value;
    				price = unit * quantity;
    				form["tmp_product_price[]"].value = price;
    			}
    			else
    			{
    				form["tmp_product_price[]"].value = 0;
    			}
    
    			if( unit_index !=  form["prev_product_index[]"].value )
    			{
    				reset_option_price( form, category_key );
    			}
    			else
    			{
    				is_option_pop = false;
    			}
    
    			form["prev_product_index[]"].value = unit_index;
    		}
    
    		has_option = unit_s[6]
    
    		// »óÇ°À» ¼±ÅÃ ÇßÀ»¶§
    		if( unit_index > 0 )
    		{
    			option_price = get_option_price( form, category_key );
    			price = new String( new Number( price ) + ( option_price * quantity ) );
    
    			unit = f_price_unit1 + number_format( unit ) + f_price_unit2
    			price =  f_price_unit1 + number_format( price ) + f_price_unit2
    
    			if( has_option == "T" )
    			{
    				document.all['display_option_' + category_key].style.display = 'block';
    
    				if( is_option_pop )
    				{
    					// ¿É¼ÇÀÌ ÀÖ´Â »óÇ°ÀÌ¶ó¸é ¿É¼Ç ¼±ÅÃ Ã¢ ¶ç¿ì±â;
    					online_quotation_option_pop( form, main_cate_no, category_key );
    				}
    			}
    			else
    			{
    				document.all['display_option_' + category_key].style.display = 'none';
    			}
    		}
    		// »óÇ°À» ¼±ÅÃÇÏÁö ¾Ê¾ÒÀ»¶§
    		else
    		{
    			unit_s = new Array( "", "", "", "", "" );
    			unit = "";
    			price = "";
    
    			reset_option_price( form, category_key );
    
    			document.all['display_option_' + category_key].style.display = 'none';
    		}
    
    		if( typeof( form["main_cate_no[]"].length ) == "number" )
    		{
    			form["main_cate_no[]"][category_key].value = main_cate_no 
    			form["add_option_name[]"][category_key].value = unit_s[1]
    			form["display_group[]"][category_key].value = unit_s[2]
    			form["product_min[]"][category_key].value = unit_s[3]
    			form["product_name[]"][category_key].value = unit_s[4]
    			form["product_price[]"][category_key].value = unit_s[5]
    		}
    		else
    		{
    			form["main_cate_no[]"].value = main_cate_no 
    			form["add_option_name[]"].value = unit_s[1]
    			form["display_group[]"].value = unit_s[2]
    			form["product_min[]"].value = unit_s[3]
    			form["product_name[]"].value = unit_s[4]
    			form["product_price[]"].value = unit_s[5]
    		}
    	}
    	else
    	{
    		unit = ""
                    price = ""
    
    		form["main_cate_no[]"][category_key].value = "";
    		form["add_option_name[]"][category_key].value = "";
    		form["display_group[]"][category_key].value = "";
    		form["product_min[]"][category_key].value = "";
    		form["product_name[]"][category_key].value = "";
    		form["product_price[]"][category_key].value = "";
    	}

        unit_obj = document.all[ "unit_" + main_cate_no ];
        set_div_content( unit_obj, unit );

        price_obj = document.all[ "price_" + main_cate_no ];
        set_div_content( price_obj, price );

	    set_total_price( form, f_price_unit1, f_price_unit2 )
	} catch(e) {
	}
}

function set_total_price( form, f_price_unit1, f_price_unit2 )
{
	total_price = 0;
	quota_total_price = 0;

	if( typeof( form["tmp_product_price[]"] ) == "object" )
	{
		if( typeof( form["tmp_product_price[]"].length ) == "number" )
		{
			tmp_product_price_length = form["tmp_product_price[]"].length

			for( t_i = 0; t_i < tmp_product_price_length; t_i++ )
			{
				// »óÇ° ±Ý¾×
				price = new Number( form["tmp_product_price[]"][t_i].value )

				// »óÇ° ¼ö·®
				quantity = new Number( form["quantity[]"][t_i].value );

				// ¿É¼Ç ±Ý¾×
				option_price = get_option_price( form, t_i );

				total_price += price + ( option_price * quantity );
				try {
				    quota_total_price +=  (new Number( form["quota_price[]"][t_i].value ) * quantity );
				} catch(e) {
				}
			}
		}
		else
		{
			if( form["product_no[]"].selectedIndex > 0 )
			{
				// »óÇ° ±Ý¾×
				price = new Number( form["tmp_product_price[]"].value );

				// »óÇ° ¼ö·®
				quantity = new Number( form["quantity[]"].value );

				// ¿É¼Ç ±Ý¾×
				option_price = get_option_price( form, 0 );

				total_price = price + ( option_price * quantity );
				try {
				    quota_total_price +=  (new Number( form["quota_price[]"].value ) * quantity );
				} catch(e) {
				}
			}
		}
	}

	/*
	if( typeof( form['option_item_price[]'] ) == "object" )
	{
		option_item_name_length = form['option_item_name[]'].length;

		if( typeof( option_item_name_length ) == "number" )
		{
			for( i = 0; i < option_item_name_length; i++ )
			{
				if( form['option_item_price[]'][i].value  )
				{
					option_price = new Number( form["option_item_price[]"][i].value );
					total_price = ( total_price + option_price );
				}
			}
		}
		else
		{
			if( form['option_item_price[]'].value  )
			{
				option_price = new Number( form["option_item_price[]"].value );
				total_price = ( total_price + option_price );
			}
		}
	}
	*/

	total_price = "" + total_price
	total_price = f_price_unit1 + number_format( total_price ) + f_price_unit2
	quota_total_price = "" + quota_total_price
	quota_total_price = f_price_unit1 + number_format( quota_total_price ) + f_price_unit2

	total_price_obj = document.all["total_price"]
	quota_total_price_obj = document.all["quota_total_price"]
    set_div_content( total_price_obj, total_price );
    set_div_content( quota_total_price_obj, quota_total_price );
	total_price = 0;
	quota_total_price = 0;
}

function quotation_reset( form )
{

	form.reset();

	if( typeof( form["tmp_product_price[]"] ) == "object" )
	{
		category_length = form["main_cate_nos[]"].length;

		if( typeof( category_length ) == "number" )
		{
			for( var i = 0; i < category_length; i++ )
			{
				main_cate_no = form["main_cate_nos[]"][i].value;

				unit_obj = document.all[ "unit_" + main_cate_no ];
				set_div_content( unit_obj, "" );

				price_obj = document.all[ "price_" + main_cate_no ];
				set_div_content( price_obj, "" );
			}
		}
		else
		{
			/*
			unit_obj = document.all[ "unit_" + main_cate_no ];
			set_div_content( unit_obj, "" );

			price_obj = document.all[ "price_" + main_cate_no ];
			set_div_content( price_obj, "" );
			*/
		}

		tmp_product_price_length = form["tmp_product_price[]"].length;

		if( typeof( tmp_product_price_length ) == "number" )
		{
			for( var i = 0; i < tmp_product_price_length; i++ )
			{
				reset_option_price( form, i );
			}
		}
		else
		{
			reset_option_price( form, 0 );
		}
	}

	/*
	if( typeof( form['option_item_name[]'] ) == "object" )
	{
		if( typeof( form['option_item_name[]'].length ) == "number" )
		{
			option_item_price_length = form['option_item_price[]'].length;

			for( i = 0; i < option_item_price_length; i++ )
			{
				price_obj = document.all[ "option_item_price_" + i ];
				set_div_content( price_obj, "" );
			}
		}
		else
		{
			price_obj = document.all[ "option_item_price_0" ];
			set_div_content( price_obj, "" );
		}
	}
	*/

	total_price_obj = document.all["total_price"]
        set_div_content( total_price_obj, "" );
}

function set_option_price( form, option_key, f_price_unit1, f_price_unit2 )
{
	// price : ±Ý¾×

	if( typeof( form['option_item_name[]'].length ) == "number" )
	{
		option_item_name_obj = form["option_item_name[]"][option_key];
		obj = form["option_item_price[]"][option_key];
	}
	else
	{
		option_item_name_obj = form["option_item_name[]"];
		obj = form["option_item_price[]"];
	}

	unit_index = obj.selectedIndex;

	if( unit_index )
	{
		obj_text = obj.options[unit_index].text
		option_item_name_obj.value = obj_text
		obj_value = number_format( obj.options[unit_index].value );
		price = f_price_unit1 + obj_value + f_price_unit2;
	}
	else
	{
		option_item_name_obj.value = "";
		price = "";
	}

        price_obj = document.all[ "option_item_price_" + option_key ];
        set_div_content( price_obj, price );

	set_total_price( form, f_price_unit1, f_price_unit2 )
}

function set_div_content( obj, html )
{

        obj.innerHTML = html

} // end of set_div_content()()


function number_format( number )
{
	strlen = number.length - 1
	ret_val = "";

	for( i = 1; 0 <= strlen; strlen--, i++ )
	{
		if( !( i % 3 ) && strlen )
		{
			ret_val = "," + number.substr( strlen, 1 ) + ret_val 
		}
		else
		{
			ret_val = number.substr( strlen, 1 ) + ret_val 
		}
	}

	return ret_val;
}

function online_quotation_print_pop( form )
{

	 option = "'toolbar=no," +
	                "location=no," +
	                "directories=no," +
	                "status=no," +
	                "menubar=no," +
	                "scrollbars=yes," +
	                "resizable=yes," +
	                "width=437," +
	                "height=267," +
	                "top=0," +
	                "left=200"

		open_window( '', option, "online_quotation_print_pop" )

		form.target = "online_quotation_print_pop";
		form.action = "/front/php/online_quotation/online_quotation_userinfo.php?type=online_quotation_print";
		form.submit()
		form.target = "";

}

function add_basket(arg)
{
	var f = document.frm;
	product_no = f["product_no[]"];
	tmp_product_price_length = f["tmp_product_price[]"].length;
	is_option_check = true;
	product_name = "";

	if( typeof( tmp_product_price_length ) == "number" )
	{
		is_product_selected = false;

		for( i = 0; i < tmp_product_price_length; i++ )
		{
			unit_index = f["product_no[]"][i].selectedIndex;

			if( unit_index == 0 )
			{
				continue;
			}

			is_product_selected = true;

			unit_name = product_no[i].options[unit_index].option_check;
			product_name_s = product_no[i].options[unit_index].name;
			product_name = product_name_s.split( "=||=" )[4];

			if( unit_name == "F" )
			{
				is_option_check = false;
				break;
			}
		}

		if( !is_product_selected  )
		{
			alert( '»óÇ°À» ¼±ÅÃÇØÁÖ¼¼¿ä' );

			return false;
		}
	}
	else
	{
		unit_index = f["product_no[]"].selectedIndex;
		product_name_s = product_no.options[unit_index].name;
		product_name = product_name_s.split( "=||=" )[4];

		if( unit_index == 0 )
		{
			alert( '»óÇ°À» ¼±ÅÃÇØÁÖ¼¼¿ä.' );

			return false;
		}
		else
		{
			unit_name = product_no.options[unit_index].option_check;

			if( unit_name == "F" )
			{
				is_option_check = false;
			}
			else
			{
				is_option_check = true;
			}
		}
	}

	if( !is_option_check )
	{
		alert( "»óÇ° '" + product_name + "'ÀÇ ¿É¼ÇÀ» ¼±ÅÃÇÏ¼Å¾ß ÇÕ´Ï´Ù." );

		return false;
	}

	//var f = document.frm;
	f.action="/front/php/online_quotation_basket.php";
	f.submit();	
}

function set_image_zoom( form, category_key )
{
	obj = form["tmp_product_price[]"];

	if( typeof( obj ) == "object" )
	{
		obj_length = obj.length

		if( typeof( obj_length ) == "number" )
		{
			key = form['product_no[]'][category_key].selectedIndex;

			if( key == 0 )
			{
				alert( '»óÇ°À» ¼±ÅÃÇØÁÖ¼¼¿ä.' );

				return false;
			}

			product_no = form["product_no[]"][category_key].options[key].value
			main_cate_no = form["main_cate_no[]"][category_key].value;
			product = form["product_no[]"][category_key].options[key].name;
			product_s = product.split( "=||=" );
			display_group = product_s[2];
		}
		else
		{
			key = form['product_no[]'].selectedIndex;

			if( key == 0 )
			{
				alert( '»óÇ°À» ¼±ÅÃÇØÁÖ¼¼¿ä.' );

				return false;
			}

			product_no = form["product_no[]"].value;
			main_cate_no = form["main_cate_no[]"].value;
			product = form["product_no[]"].options[key].name;
			product_s = product.split( "=||=" );
			display_group = product_s[2];
		}

		image_zoom( product_no, main_cate_no, display_group );
	}
}

function online_quotation_option_pop( form, main_cate_no, category_key )
{
	obj = form["tmp_product_price[]"];

	if( typeof( obj ) == "object" )
	{
		obj_length = obj.length

		if( typeof( obj_length ) == "number" )
		{
			key = form['product_no[]'][category_key].selectedIndex;

			if( key != 0 )
			{
				product_no = form["product_no[]"][category_key].options[key].value
			}
		}
		else
		{
			key = form['product_no[]'].selectedIndex;

			if( key != 0 )
			{
				product_no = form["product_no[]"].value;
			}
		}

		if( key == 0 )
		{
			alert( '»óÇ°À» ¼±ÅÃÇÏ¼¼¿ä.' );
		}
		else
		{
			option = "'toolbar=no," +
				"location=no," +
				"directories=no," +
				"status=no," +
				"menubar=no," +
				"scrollbars=yes," +
				"resizable=yes," +
				"width=436," +
				"height=284," +
				"top=100," +
				"left=200'";

			page = "/front/php/online_quotation/online_quotation_option.php?"
				+ "product_no=" + product_no
				+ "&main_cate_no=" + main_cate_no
				+ "&category_key=" + category_key;

			window.open( page, "online_quotation_option_pop", option );
		}
	}
}
