            


function GetProductType(TypeId)
{ 
  showinfobox('Ładowanie kategorii...','true','false','no');
  try
  {
        connection = new AjaxConnection();
        opts = new Array();
        opts[0] = "Request=LoadType";
        opts[1] = "TypeId="+encodeURI(TypeId);
        connection.setOptions(opts);
        connection.connect("FillProductTypeForm");
  }
  catch(err)
  {
      txt="Błąd!\n\n";
      txt+=err.description + "\n\n";
      showinfobox(txt,'false','true','warning');
  }    
  return false;
}


function FillProductTypeForm(content,text) 
{
  try
  {
    var result = ToArray.xml2array(content,true);
    
    var TypeForm = document.forms['TypeForm'];
    TypeForm.elements['TypeName'].value = result['ProductType']['Name'];
    TypeForm.elements['EditTypeId'].value = result['ProductType']['Id'];
    TypeForm.elements['TypeDesc'].value = result['ProductType']['Description'];
    TypeForm.elements['ParentType'].value = result['ProductType']['IdParent'];
    var element = document.getElementById('typetitle');
    element.innerHTML = 'Edycja: <b>'+result['ProductType']['Name']+'</b>';
    hide_div('infobox');
    ShowDiv("NewTypeDiv");
  }
  catch(err)
  {
    txt="Błąd!\n\n";
    txt+=err.description + "\n\n";
    showinfobox(txt,'false','true','warning');
  }    
}





function GetProduct(ProductId)
{ 
  showinfobox('Ładowanie produktu...','true','false','no');	
  try
  {
        connection = new AjaxConnection();
        opts = new Array();
        opts[0] = "Request=LoadProduct";
        opts[1] = "ProductId="+encodeURI(ProductId);
        connection.setOptions(opts);
        connection.connect("FillProductForm");
  }
  catch(err)
  {
      txt="Błąd!\n\n";
      txt+=err.description + "\n\n";
      showinfobox(txt,'false','true','warning');
  }    
  return false;
}


function FillProductForm(content,text) 
{
  try
  {
    var result = ToArray.xml2array(content,true);
    var ProductForm = document.forms['ProductForm'];
    ProductForm.elements['ProductName'].value = result['Product']['Name'];
    ProductForm.elements['ProductId'].value = result['Product']['Id'];
    ProductForm.elements['Description'].value = result['Product']['Description'];
    if (result['Product']['IsRecommended']=='1') ProductForm.elements['IsRecommend'].checked = true
    else ProductForm.elements['IsRecommend'].checked = false;
    if (result['Product']['IsForSale']=='1') ProductForm.elements['IsForSale'].checked = true
    else ProductForm.elements['IsForSale'].checked = false;
    ProductForm.elements['ParentType'].value = result['Product']['IdType'];
    ProductForm.elements['Price'].value = result['Product']['Price'];
    if (result['Product']['IsProm']=='1') ProductForm.elements['IsProm'].checked = true
    else ProductForm.elements['IsProm'].checked = false;
    ProductForm.elements['PromPrice'].value = result['Product']['PromPrice'];
    var quant = result['Product']['Quantity'];
    if (quant<0) 
    {
        ProductForm.elements['Quantity'].value = '1';
        ProductForm.elements['UnlimitedQuantity'].checked = true;
    }
    else
    {
        ProductForm.elements['Quantity'].value = quant;
        ProductForm.elements['UnlimitedQuantity'].checked = false;
    }
    if (result['Product']['IsFreeShipp']=='1') ProductForm.elements['IsFreeShipp'].checked = true
    else ProductForm.elements['IsFreeShipp'].checked = false;
    
    var phcount = (result['Product']['PhotoCount']);
    var imglist = '';
    if (phcount>0) 
    { 
        imglist = '<div class="panelheader">Zdjęcia dodane do produktu</div><br/><table>';
        var tdcnt=0;
        for(var i=1;i<=phcount;i++)
        {
            if (tdcnt == 0) imglist += '<tr>';
            var key = 'Photo'+i;
            if ((result['Product'][key]!='undefined') && (result['Product'][key]!=null))
            {
                imglist += '<td><img src="images/icon/'+result['Product'][key]+'"><br/>Usuń: <input type="checkbox" title="Zaznacz zdjęcie do usunięcia..." name="DelPhoto'+i+'" value="'+result['Product'][key]+'"><br/>W prawo: <input type="checkbox" title="Obróc zdjęcie w prawo..." name="PhotoRotR'+i+'" value="'+result['Product'][key]+'"><br/>W lewo: <input type="checkbox" title="Obróć zdjęcie w lewo..." name="PhotoRotL'+i+'" value="'+result['Product'][key]+'"></td>';
                tdcnt +=1;
            }
            if (tdcnt==6) { tdcnt=0; imglist += '</tr>'; }
        }
        if (tdcnt<6) imglist += '</tr>';
        imglist += '</table><br/>';
    }
    var imgs = document.getElementById('addedPhotos');
    imgs.innerHTML = imglist;
    
    var commcount = result['Product']['CommentCount'];
    var commlist = '';
    if (commcount>0) 
    {
        commlist +='<div class="panelheader">Komantarze dodane do produktu</div><br/><table class="listtable"><tr class="head"><td><b> Lp. </b></td><td><b> Klient </b></td><td><b> Komentarz </b></td><td><b> Zazn. do usunięcia </b></td></tr>';
        var key=''; var userid=0; var comm=''; var username=''; var commid=0;
        for(var i=1;i<=commcount;i++)
        {
            key = 'Comment'+i;
            commid = result['Product'][key]['Id'];
            userid = result['Product'][key]['UserId'];
            username = result['Product'][key]['UserName'];
            comm = result['Product'][key]['Comment'];
            commlist += '<tr><td>'+i+'</td>';
            commlist += '<td><a href="admin.php?action=customers&id='+userid+'" title="Pokaż dane klienta...">'+username+'</a></td>';
            commlist += '<td>'+comm+'</td>';
            commlist += '<td><input type="checkbox" value="1" name="DelComm'+commid+'" title="Zaznacz komentarz do usunięcia..." /></td>';
            commlist += '</tr>';
        }
        
        commlist += '</table><br/>';
    }
    var comms = document.getElementById('addedComments');
    comms.innerHTML = commlist;
    
    var element = document.getElementById('proedtitle');
    element.innerHTML = '<b>.:'+result['Product']['Name']+':.</b>';
    hide_div('infobox');
    window.scroll(0,-3000);
    ShowDiv("NewProductDiv");
  }
  catch(err)
  {
    txt="Błąd!\n\n";
    txt+=err.description + "\n\n";
    showinfobox(txt,'false','true','warning');
  }    
}



function SetOrderStatus(statecombo,orderid)
{
      try
	  {
	        var state = statecombo.value;
	        connection = new AjaxConnection();
	        opts = new Array();
	        opts[0] = "Request=SetOrderState";
	        opts[1] = "OrderId="+encodeURI(orderid);
	        opts[2] = "NewState="+encodeURI(state);
	        connection.setOptions(opts);
	        connection.connect("ParseResult");
	  }
	  catch(err)
	  {
	      txt="Błąd!\n\n";
	      txt+=err.description + "\n\n";
	      showinfobox(txt,'false','true','warning');
	  }    
	  return true;    
}


function ParseResult(content,text)
{
  try
  {
    var result = ToArray.xml2array(content,true);   
    if (result['Result']!='ok')
    showinfobox(result['Result'],'false','true','warning');	
  }
  catch(err)
  {
    txt="Błąd!\n\n";
    txt+=err.description + "\n\n";
    showinfobox(txt,'false','true','warning');
  } 
}


