function insert_order_row(id,number,size_type_id,product_size_id,name,cnt,price,comment)
{
	var el;
	if(cnt>0)
		cnt=cnt;
	else
		cnt=1;

	row=insert_row("order_tbl",0);
	nr = row.id;
	
	cell=insert_cell(row);
	el=insert_form_element("text", "p_number["+nr+"]",number,cell,'','input-0');
	if( el.attachEvent )
		el.onchange= function(){makedirty()};
	else
		el.onchange= function(e){makedirty()};

	cell=insert_cell(row);
	el=insert_form_element("select", "p_size_type_id["+nr+"]",size_type_id,cell,'product_type_list','select-1');
	if( el.attachEvent )
		el.onchange= function(){c_size(this);makedirty();};
	else
		el.onchange= function(e){c_size(this);makedirty()};
	
	cell=insert_cell(row);
	el=insert_form_element("select", "p_size_id["+nr+"]",product_size_id,cell,'product_size_list','select-s');
	if( el.attachEvent )
		el.onchange= function(){makedirty();};
	else
		el.onchange= function(e){makedirty()};

	cell=insert_cell(row);
	el=insert_form_element("text", "p_name["+nr+"]",name,cell,'','input-2');

	if( el.attachEvent )
		el.onchange= function(){makedirty();};
	else
		el.onchange= function(e){makedirty()};

	cell=insert_cell(row);
	el=insert_form_element("text", "p_cnt["+nr+"]",cnt,cell,'','input-n');
	
	if(el.attachEvent)
		el.onkeyup= function(){update_lvl_price(this,this.value);};
	else
		el.onkeyup= function(e){update_lvl_price(this,this.value);};

	if( el.attachEvent )
		el.onchange= function(){makedirty();};
	else
		el.onchange= function(e){makedirty()};

	cell=insert_cell(row);
	el=insert_form_element("text", "p_price["+nr+"]",price,cell,'','input-n');
	
	if(el.attachEvent)
		el.onkeyup= function(){set_lvl_price(this,this.value);};
	else
		el.onkeyup= function(e){set_lvl_price(this,this.value);};
	
	if( el.attachEvent )
		el.onchange= function(){makedirty();};
	else
		el.onchange= function(e){makedirty()};

	
	cell=insert_cell(row);
	var div=document.createElement('div');
	div.id="p_price_lvl["+nr+"]";

	cell.appendChild(div);
	price_lvl=get_lvl_price(price);
	if(price>0)
		insert_innerHTML(div,price_lvl*cnt);

	insert_form_element("hidden","p_id["+nr+"]",id,cell,'','');
	
	cell=insert_cell(row);
	
	el=insert_form_element("button", "",'',cell,'','button-delete');
	
	if(id!='')
	{
		var url=location.href+"&delete_item="+id;

		if( el.attachEvent )
			el.onclick= function(){if (confirm('Spied OK lai dzēstu!')) {location.href=url;}};
		else
			el.onclick= function(e){if (confirm('Spied OK lai dzēstu!')) {location.href=url;}};
	}
	else
	{
		if( el.attachEvent)
			el.onclick= function(){delete_field(this);};
		else
			el.onclick= function(e){delete_field(this);};

	}
	

}

function set_lvl_price(el,val)
{
	var b=el.id.indexOf('\[');
	var e=el.id.indexOf('\]');

	var suff=el.id.substring(b+1,e);
	val=get_lvl_price(val);

	var cnt=document.getElementById("p_cnt["+suff+"]").value;

	if(val>0)
		insert_innerHTML("p_price_lvl["+suff+"]",val*cnt);	
	else
		insert_innerHTML("p_price_lvl["+suff+"]",val*cnt);	

}

function update_lvl_price(el,val)
{
	var b=el.id.indexOf('\[');
	var e=el.id.indexOf('\]');

	var suff=el.id.substring(b+1,e);

	var newprice=document.getElementById("p_price["+suff+"]").value;
	
	newprice=get_lvl_price(newprice);

	newprice=newprice*val;

	if(newprice>0)
		insert_innerHTML("p_price_lvl["+suff+"]",newprice);	
	else
		insert_innerHTML("p_price_lvl["+suff+"]",newprice);	

}


function delete_field(currCell)
{
	var row=currCell.parentNode.parentNode;
	currCell.parentNode.parentNode.parentNode.deleteRow(row.rowIndex);
}

function c_size(pTypeList)
{

	afterFunction=function(){createSizeList(pTypeList)};
	url="xml_data.php?product_type_id="+pTypeList.value;

	if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = document.implementation.createDocument("", "", null);
		xmlDoc.onload = function(){afterFunction()};
	}
	else if (window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange = function () {
			if (xmlDoc.readyState == 4) 
				afterFunction()
		};
 	}
	else
	{
		alert('Your browser can\'t handle this script');
		return;
	}
	
	xmlDoc.load(url);

}

function createSizeList(pTypeList)
{
	var x = xmlDoc.getElementsByTagName('root');
	
	/**/
	var b=pTypeList.id.indexOf('\[');
	var e=pTypeList.id.indexOf('\]');
	var suff=pTypeList.id.substring(b+1,e);

	var elem=document.getElementById("p_size_id["+suff+"]");

	elem.innerHTML="";
	
	elem.options.add(document.createElement("OPTION"));
	elem.options[0].text ='nav';
	elem.options[0].value =99;
	
	for (i=0; i<x[0].childNodes.length; i++)
	{
		if(x[0].childNodes[i].nodeType!=3)
		{
			var opt = document.createElement("OPTION");
			opt.value = x[0].childNodes[i].getAttribute("value");
			opt.text = x[0].childNodes[i].firstChild.nodeValue;
					 
			elem.options.add(opt); 
		
			/*if (opt.value==get_value('selected_stock_id'))
			{
				opt.selected="selected";
			}	*/	
		}
		
	}
}

function changeOrderStatus(val)
{
	var status;
	status=val;

	cUrl =location.href+"&update_status="+status; 
	
	window.location=cUrl;
	//parseURL(cUrl);
}

function changeOrderItemStatus(cb,val)
{
	var status=1;

	var np=document.getElementById('navpieejamscb');
	var bo=document.getElementById('backordercb');
	
	if(cb.checked==true )
	{
		np.checked=false;
		bo.checked=false;
		
		status=val;
		cb.checked=true;
	}

	cUrl =location.href+"&update_item_status="+status+"&item_id="+cb.value; 
	window.location=cUrl;
	
	//parseURL(cUrl);
}



function get_lvl_price_b(val)
{
//A3>31;A3*0.85;A3>30;*0.87;A3>16;A3*0.9;IF(A3>15;A3*0.95;   IF(A3>8;A3*1;IF(A3>3;A3*1+1;IF(A3>0.001;5;0)))))));0)
//IF(F6>61;F6*0.85; IF(F6>30;F6*0.87; IF(F6>19;F6*0.95; IF(F6>15;F6*1; IF(F6>8;F6*1+1; IF(F6>3;F6*1+2;IF(F6>0.001;6;0)))))));0

	val = val.replace(/,/, '.');
	val=val*1;

	if (val>0)
	{

		if(val>61)
		{
			val=val*0.85;
		}
		else
		{
			if(val>30)
			{
				val=val*0.87;
			}
			else
			{
				if(val>19)
				{
					val=val*0.95;
				}
				else
				{
					if(val>15)
					{
						val=val*1;
					}
					else
					{
						if(val>8)
						{
							val=val*1+1;
						}
						else
						{
							if(val>3)
							{
								val=val*1+2;
							}
							else
							{
								if(val>0.001)
								{
									val=6;
								}
								else
								{
									val=0;

								}
							}
						}
					}
				}
			}
		}
	}

	return Math.round(val);

}

function get_lvl_price(val)
{
//=ROUND(IF(F6>60;F6*0.89;IF(F6>30;F6*0.92;IF(F6>19;F6*0.98;IF(F6>15;F6*1;IF(F6>8;F6*1+1;IF(F6>3;F6*1+2;IF(F6>0.001;6;0)))))));0)

	val = val.replace(/,/, '.');
	val=val*1;

	if (val>0)
	{

		if(val>60)
		{
			val=val*0.89;
		}
		else
		{
			if(val>30)
			{
				val=val*0.92;
			}
			else
			{
				if(val>19)
				{
					val=val*0.98;
				}
				else
				{
					if(val>15)
					{
						val=val*1;
					}
					else
					{
						if(val>8)
						{
							val=val*1+1;
						}
						else
						{
							if(val>3)
							{
								val=val*1+2;
							}
							else
							{
								if(val>0.001)
								{
									val=6;
								}
								else
								{
									val=0;

								}
							}
						}
					}
				}
			}
		}
	}

	return Math.round(val);

}

