var i = path_OK.indexOf(";");
if (i>0) {	
  path_OK= path_OK.substring(0,i);
}   
path_main = path_OK + 'AjaxRange.do';
path_find = path_OK + 'Find.do';
path_banner = path_OK + 'baner.do';
path_children = path_OK + 'childrenList.do';
path_products = path_OK + 'productsList.do';
path_brothers = path_OK + 'brothersList.do';
path_basket =   path_OK + 'basket.do';
path_checkout =   path_OK + 'checkout.do';
path_menu = path_OK + 'menu_ajax.do';

var AFormIds = new Array();

function htmlContent(event){
    new Ajax.Request('Layouts/gadgets/AjaxImgInside.jsp',{asynchronous:true,
                                   method:'post',
                                   evalscripts:true,
                                   postBody:'event='+event, 
                                   onSuccess: function(t){$('DivAdminAjaxContent').innerHTML = t.responseText;t.responseText.evalScripts();}
                                   });
  }   
   function ajaxToDiv(src,trg,param){
   
    new Ajax.Request(src,{asynchronous:true,
                                   method:'post',
                                   evalscripts:true,
                                   postBody:param, 
                                   onSuccess: function(t){$(trg).innerHTML = t.responseText;t.responseText.evalScripts();}
                                   });
  }   
  

function refresh()
{
	var loc_old = window.location.href;
	var i = loc_old.indexOf("#");
 
	if (i>0)
	{	
  	loc_old= loc_old.substring(0,i);
	}
	var new_loc = loc_old+ (window.location.href.indexOf("?") == -1 ? "?" : "&") + (new Date()).getTime();
        window.location.href = new_loc;
}
function GotoLogin()
{
		var new_loc = '/pl_shop/start.do?LoginPage=YES';
        window.location.href = new_loc;
}

 function FindExecute(event) {
   new Ajax.Request(path_find,{asynchronous:true,
                                           method:'post',
                                           evalscripts:true,
                                           postBody:'event='+event+'&FindBodyShort='+$F('FindInputText'), 
                                           onLoading: function(t){$('DivFind').innerHTML = '<tr><td class="T_FIND_tablemiddle"><img border="0" src="'+url_skin+'img/icons/general/loading_animation_liferay.gif"></td></tr>'},
                                           on500: function(t){$('DivFind').innerHTML = '<tr><td class="T_FIND_tablemiddle">'+on500Text+'</td></tr>';return;},
                                           on404: function(t){$('DivFind').innerHTML = '<tr><td class="T_FIND_tablemiddle">'+on404Text+'</td></tr>';return;},
                                           onSuccess: function(t){$('DivFind').innerHTML = t.responseText;t.responseText.evalScripts();}
                                           });
                    
 }
 
  function getHistoryContent(href){
    if(href==null){return}
    
    var IDD = href.substring(href.indexOf("ID="));
    var LNK = href.substring(href.indexOf("Typ=LNK")); 
    var Brothers = (href.indexOf("Brothers=YES"));                         
    var Products = (href.indexOf("Typ=SHO"));
  
    
    if (LNK == "Typ=LNK")
    {                            
      i = href.indexOf("?");                            
      LNK= href.substring(0,i);                         
      window.open(LNK);
      return;
    }
    
     new Effect.Opacity('DivContent', {duration:0.5, from:1.0, to:0.3});
     new Effect.Opacity('bodyChildren', {duration:0.5, from:1.0, to:0.3});
     new Effect.Opacity('bodyBrothers', {duration:0.5, from:1.0, to:0.3});
     new Effect.Opacity('bodyProducts', {duration:0.5, from:1.0, to:0.3});
    if (LNK != "Typ=LNK")
    {                          
      
      new Ajax.Request(path_main,{asynchronous:true,
                                           method:'post',
                                           evalscripts:true,
                                           postBody:'event=TestAjax&'+IDD+'&TILES=YES&Main=YES&Children=YES',
                                           on500: function(t){$('DivContent').innerHTML = on500Text;return;},
                                           on404: function(t){$('DivContent').innerHTML = on404Text;return;},
                                           onSuccess: function(t){$('DivContent').innerHTML = t.responseText;t.responseText.evalScripts();new Effect.Opacity('DivContent', {duration:0.5, from:0.3, to:1.0});},
                                            onComplete: function(t){new Effect.Opacity('DivContent', {duration:0.5, from:0.3, to:1.0});}
                                           });
      new Ajax.Request(path_children,{asynchronous:true,
                                           method:'post',
                                           evalscripts:true,
                                           postBody:'event=TestAjax&'+IDD+'&TILES=YES&Main=YES&Children=YES', 
                                           on500: function(t){$('bodyChildren').innerHTML = on500Text;return;},
                                           on404: function(t){$('bodyChildren').innerHTML = on404Text;return;},
                                           onSuccess: function(t){$('bodyChildren').innerHTML = t.responseText;t.responseText.evalScripts();new Effect.Opacity('bodyChildren', {duration:0.5, from:0.3, to:1.0});},
                                            onComplete: function(t){new Effect.Opacity('bodyChildren', {duration:0.5, from:0.3, to:1.0});}
                                           });

      if (Brothers!=-1)
      {                          
        new Ajax.Request(path_brothers,{asynchronous:true,
                                           method:'post',
                                           evalscripts:true,
                                           postBody:'event=TestAjax&'+IDD+'&TILES=YES&Main=YES&Children=YES&Brothers=YES', 
                                           on500: function(t){$('bodyBrothers').innerHTML = on500Text;return;},
                                           on404: function(t){$('bodyBrothers').innerHTML = on404Text;return;},
                                           onSuccess: function(t){$('bodyBrothers').innerHTML = t.responseText;t.responseText.evalScripts(); new Effect.Opacity('bodyBrothers', {duration:0.5, from:0.3, to:1.0});},
                                            onComplete: function(t){new Effect.Opacity('bodyBrothers', {duration:0.5, from:0.3, to:1.0});}
                                           });
      }
      else
      {
        $('bodyBrothers').innerHTML = "<img src='"+url_skin+"img/icons/general/spacer.gif' alt='' />";
      }
      if (Products != -1)
      {
        new Ajax.Request(path_products,{asynchronous:true,
                                           method:'post',
                                           evalscripts:true,
                                           postBody:'event=TestAjax&'+IDD+'&TILES=YES&Main=YES&Children=YES&Products=YES',
                                           on500: function(t){$('bodyProducts').innerHTML = on500Text;return;},
                                           on404: function(t){$('bodyProducts').innerHTML = on404Text;return;},
                                           onSuccess: function(t){$('bodyProducts').innerHTML = t.responseText;t.responseText.evalScripts(); new Effect.Opacity('bodyProducts', {duration:0.5, from:0.3, to:1.0});},
                                            onComplete: function(t){new Effect.Opacity('bodyProducts', {duration:0.5, from:0.3, to:1.0});}
                                           });
      }
      else
      {
       $('bodyProducts').innerHTML = "<img src='"+url_skin+"img/icons/general/spacer.gif' alt='' />";
      }
    } 
  
     


    
    return(false);
  }
  
  function get_content(href) {
   
    if(href==null){return}
  
  	var IDD = href.substring(href.indexOf("ID="));
    var LNK = href.substring(href.indexOf("Typ=LNK")); 
    var Brothers = (href.indexOf("Brothers=YES"));                         
    var Products = (href.indexOf("Typ=SHO"));
    
    dhtmlHistory.add("get_content_"+IDD, href); 
    //alert(Products);
    if (LNK == "Typ=LNK")
    {                            
      i = href.indexOf("?");                            
      LNK= href.substring(0,i);                         
      window.open(LNK);
      return;
    }
    
    new Effect.Opacity('DivContent', {duration:0.5, from:1.0, to:0.3});
     new Effect.Opacity('bodyChildren', {duration:0.5, from:1.0, to:0.3});
     new Effect.Opacity('bodyBrothers', {duration:0.5, from:1.0, to:0.3});
     new Effect.Opacity('bodyProducts', {duration:0.5, from:1.0, to:0.3});                      
    if (LNK != "Typ=LNK")
    {                          
      
      new Ajax.Request(path_main,{asynchronous:true,
                                           method:'post',
                                           evalscripts:true,
                                           postBody:'event=TestAjax&'+IDD+'&TILES=YES&Main=YES&Children=YES', 
                                           on500: function(t){$('DivContent').innerHTML = on500Text;return;},
                                           on404: function(t){$('DivContent').innerHTML = on404Text;return;},
                                           onSuccess: function(t){$('DivContent').innerHTML = t.responseText;t.responseText.evalScripts(); new Effect.Opacity('DivContent', {duration:0.5, from:0.3, to:1.0});},
                                           onComplete: function(t){new Effect.Opacity('DivContent', {duration:0.5, from:0.3, to:1.0});}
                                           });
      new Ajax.Request(path_children,{asynchronous:true,
                                           method:'post',
                                           evalscripts:true,
                                           postBody:'event=TestAjax&'+IDD+'&TILES=YES&Main=YES&Children=YES', 
                                           on500: function(t){$('bodyChildren').innerHTML = on500Text;return;},
                                           on404: function(t){$('bodyChildren').innerHTML = on404Text;return;},
                                           onSuccess: function(t){$('bodyChildren').innerHTML = t.responseText;t.responseText.evalScripts();new Effect.Opacity('bodyChildren', {duration:0.5, from:0.3, to:1.0});},
                                            onComplete: function(t){new Effect.Opacity('bodyChildren', {duration:0.5, from:0.3, to:1.0});}
                                           });

      if (Brothers!=-1)
      {                          
        new Ajax.Request(path_brothers,{asynchronous:true,
                                           method:'post',
                                           evalscripts:true,
                                           postBody:'event=TestAjax&'+IDD+'&TILES=YES&Main=YES&Children=YES&Brothers=YES',
                                           on500: function(t){$('bodyBrothers').innerHTML = on500Text;return;},
                                           on404: function(t){$('bodyBrothers').innerHTML = on404Text;return;},
                                           onSuccess: function(t){$('bodyBrothers').innerHTML = t.responseText;t.responseText.evalScripts();new Effect.Opacity('bodyBrothers', {duration:0.5, from:0.3, to:1.0});},
                                            onComplete: function(t){new Effect.Opacity('bodyBrothers', {duration:0.5, from:0.3, to:1.0});}
                                           });
      }
      else
      {
        $('bodyBrothers').innerHTML = "<img src='"+url_skin+"img/icons/general/spacer.gif' alt='' />";
      }
      if (Products != -1)
      {
        new Ajax.Request(path_products,{asynchronous:true,
                                           method:'post',
                                           evalscripts:true,
                                           postBody:'event=TestAjax&'+IDD+'&TILES=YES&Main=YES&Children=YES&Products=YES',
                                           on500: function(t){$('bodyProducts').innerHTML = on500Text;return;},
                                           on404: function(t){$('bodyProducts').innerHTML = on404Text;return;},
                                           onSuccess: function(t){$('bodyProducts').innerHTML = t.responseText;t.responseText.evalScripts(); new Effect.Opacity('bodyProducts', {duration:0.5, from:0.3, to:1.0});},
                                            onComplete: function(t){new Effect.Opacity('bodyProducts', {duration:0.5, from:0.3, to:1.0});}
                                           });
      }
      else
      {
       $('bodyProducts').innerHTML = "<img src='"+url_skin+"img/icons/general/spacer.gif' alt='' />";
      }

    }                               
  return(false);
}

function get_checkout() {       
  $('bodyChildren').innerHTML = "<img src='"+url_skin+"img/icons/general/spacer.gif' alt='' />";
  $('bodyBrothers').innerHTML = "<img src='"+url_skin+"img/icons/general/spacer.gif' alt='' />";                        
  $('bodyProducts').innerHTML = "<img src='"+url_skin+"img/icons/general/spacer.gif' alt='' />";
  CheckoutNext('BASKET');
}

function AddItem(id,cntid,bytkod,itemno){
    new Ajax.Request(path_basket,{asynchronous:true,
                                  method:'post',
                                  postBody:'event=addToCart&id='+id+'&cntid='+cntid+'&bytkod='+bytkod+'&ItemNo='+itemno,
                                  evalscripts:true,
                                  on500: function(t){$('status-basket').innerHTML = on500Text;return;},
                                  on404: function(t){$('status-basket').innerHTML = on404Text;return;},
                                  onSuccess: function(t){
                                       $('status-basket').innerHTML =ItemAddedKey;
                                       $('status-basket').setStyle({display:'inline'});
                                       Effect.Fade('status-basket',{duration:2,queue:'end'});
                                  }
                                 });
  var tmp_div = $('AddItemDiv_'+id).innerHTML;
  
  $('AddItemDiv_'+id).innerHTML =ItemAddedKey;
  var div = 'AddItemDiv_'+id;
  setTimeout('$(\''+div+'\').innerHTML = '+tmp_div,2000);  
  //
  return false;
}


function ShowBasket()
{
  new Ajax.Request(path_basket,{asynchronous:true,
                                  method:'post', 
                                  evalscripts:true,                                
                                  on500: function(t){$('DivBasket').innerHTML = on500Text;return;},
                                  on404: function(t){$('DivBasket').innerHTML = on404Text;return;},
                                  onSuccess: function(t){$('DivBasket').innerHTML = t.responseText;t.responseText.evalScripts();}
                                 });
}

function CheckoutNext(action) {

  if(AFormIds.length>0){
    for (i=0;i<AFormIds.length;i++) {AFormIds.pop();}
  }
  new Effect.Opacity('DivContent', {duration:0.5, from:1.0, to:0.3});
  new Ajax.Request(path_checkout,{asynchronous:false,
                                  method:'post', 
                                  evalscripts:true,
                                  postBody:'event=CheckoutNext&ACTION='+action,
                                  on500: function(t){$('DivContent').innerHTML = on500Text;return;},
                                  on404: function(t){$('DivContent').innerHTML = on404Text;return;},
                                  onSuccess: function(t){$('DivContent').innerHTML = t.responseText;t.responseText.evalScripts();}
                                 });
  new Effect.Opacity('DivContent', {duration:0.5, from:0.3, to:1.0});
}

function SaveBasketAddresses() {

 for (i=0;i<AFormIds.length;i++) {
  var valid = new Validation(AFormIds[i], {useTitles:true});
   if(!valid.validate())
   {
   return false;
   }
  }
 
  try{
  var AFormIds_ile = AFormIds.length;
  for (i=0;i<AFormIds_ile;i++) {
 
    var id = AFormIds.pop();
    var forma = $(id);
    
    postBody = Form.serialize(forma);
    //alert(postBody);
    new Ajax.Request('dataCollector.do', 
                     {asynchronous:false,
                      evalscripts:true,
                      postBody:postBody,
                      onSuccess: function(t){t.responseText.evalScripts();}
                      });
  }
  } catch(err) { 
      alert('Error occurred, please try again !');
    }
    CheckoutNext('DC');
    
}

function AddAddressFormId(ID){
i = AFormIds.length;
AFormIds[i]=ID;
}

function SubmitBasket(f,p) {
   var qty = f.qty.value;
   var notes = f.notes.value;
   new Effect.Opacity('DivContent', {duration:0.5, from:1.0, to:0.3});
   new Ajax.Request(path_checkout,{asynchronous:true,
                                  method:'post', 
                                  evalscripts:true,
                                  postBody:p+'&notes='+notes+'&qty='+qty,
                                  on500: function(t){$('DivContent').innerHTML = on500Text;return;},
                                  on404: function(t){$('DivContent').innerHTML = on404Text;return;},
                                  onSuccess: function(t){$('DivContent').innerHTML = t.responseText;t.responseText.evalScripts();}
                                 });
   //ShowBasket();
   new Effect.Opacity('DivContent', {duration:0.5, from:0.3, to:1.0});
}

function opennew(src) {
  window.open(src, "IMG",
"toolbar=0,location=0,directories=0,menubar=0,status=0,scrollbars=0,resizable=0,copyhistory=0,width=450,height=400");
}

function openprom(src, width, height) {
  window.open(src, "IMG", "toolbar=0,location=0,directories=0,menubar=0,status=0,scrollbars=0,resizable=0,copyhistory=0,width="+width+",height="+height);
}

if (parseInt(navigator.appVersion.charAt(0))>=4){
	var isNN=(navigator.appName=="Netscape")?1:0;
	var isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;
	var isOP=(navigator.userAgent.indexOf("Opera")!=-1)?1:0;
}
var optNN='scrollbars=no,width=500,height=500,left=100,top=100';
var optIE='scrollbars=no,status=no,width=100,height=100,left=100,top=100';

function oi(imageURL,imageTitle){
 if (!isNN && !isIE) isNN=true;
 if (isNN){imgWin=window.open('about:blank','mbig',optNN);}
 if (isIE){imgWin=window.open('about:blank','mbig',optIE);}

 with (imgWin.document){

  writeln('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN">');
  writeln('<html><head><title>'+ imageTitle +'</title><style>body {margin: 0px; padding: 0px;}</style>');
  writeln('<sc'+'ript language="JavaScript" type="text/javascript">');

  writeln('var isNN,isIE;');
  writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
  writeln('isNN=(navigator.appName=="Netscape")?1:0;');
  writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
  writeln('if (!isNN && !isIE) isIE=true;');
	
  writeln('function reSizeToImage(){');
	  
    writeln('if (isIE){');
        writeln('window.resizeTo(100,100);');
	writeln('window.resizeTo(document.images[0].width+10,document.images[0].height);');
	writeln('window.resizeTo(document.images[0].width+10,document.images[0].height + Math.abs(document.documentElement.clientHeight - document.images[0].height));');
    writeln('}');
    writeln('if (isNN){');
	writeln('window.resizeTo(500,500);');
	writeln('window.innerWidth=document.images["big"].width;');
	writeln('window.innerHeight=document.images["big"].height;');
    writeln('}}');
    writeln('</sc'+'ript>');
    writeln('</head><body onload="reSizeToImage(); self.focus();" onFocus="reSizeToImage();" bgcolor="#000000" margintop="0" marginleft="0" marginwidth="0" marginheight="0" scroll="no">');
    writeln('<a href="javascript:self.close()"><img name="big" src='+imageURL+' style="display:block" border="0" alt="Zamknij"></a></body></html>');
  close();
  }
}


  function initProgress() {
    Element.show('progressMsg');
  }
  
  function resetProgress(request) {
    Effect.Fade('progressMsg');
  }
  
  function reportError() {
    $('errorMsg').innerHTML = "Portlet busted!";
    Element.show('errorMsg');
    setTimeout("Effect.DropOut('errorMsg')", 2500);
  }

  function getWidth()
  {
    return screen.width;
  }
  
  function isNotEmpty(elem) {
	var str = elem.value;
    var re = /.+/;
    if(!str.match(re)) {
      //  alert("Please fill in the required field.");
      //  setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
        return false;
    } else {
        return true;
    }
}
// batch validation router
function validateForm(form) {
 var ok = true;

     ok = isNotEmpty(form.NewBodyShort);
      if (!ok)
     {
      alert("Please fill in the required field.");     
      return false;
      }
   
    return true;
}

 function isCancel(elem) {
   //uzywam submit - i ustawilem wymagane - jezeli jest pusto to wpisuje wartosc aby mozna by?o ominac sprawdzanie
    document.forms["newPage"].NewBodyShort.value ='not empty';
   return true;  
   }
   
function get_banner(){
   new Ajax.Request(path_banner,{asynchronous:true,
                                  method:'post',
                                  postBody:'width=760',
                                  on500: function(t){$('banner').innerHTML = on500Text;return;},
                                  on404: function(t){$('banner').innerHTML = on404Text;return;},
                                  onSuccess: function(t){$('banner').innerHTML = t.responseText;t.responseText.evalScripts();}
                                 });
   }


function get_banner_static(){ 
   new Ajax.Request(CssPath+'/banner.html',{asynchronous:true,
                                  method:'get',
                                  on500: function(t){$('banner_static').innerHTML = on500Text;return;},
                                  on404: function(t){$('banner_static').innerHTML = on404Text;return;},
                                  onSuccess: function(t){$('banner_static').innerHTML = t.responseText;t.responseText.evalScripts();}
                                 });
   }

function get_left_static(){
   new Ajax.Request(CssPath+'/left_top.html',{asynchronous:true,
                                  method:'get', 
                                  on500: function(t){$('left_static').innerHTML = on500Text;return;},
                                  on404: function(t){$('left_static').innerHTML = on404Text;return;},
                                  onSuccess: function(t){$('left_static').innerHTML = t.responseText;t.responseText.evalScripts();}
                                 });
   }

function get_right_static(){
   new Ajax.Request(CssPath+'/right_top.html',{asynchronous:true,
                                  method:'get', 
                                  on500: function(t){$('right_static').innerHTML = on500Text;return;},
                                  on404: function(t){$('right_static').innerHTML = on404Text;return;},
                                  onSuccess: function(t){$('right_static').innerHTML = t.responseText;t.responseText.evalScripts();}
                                 });
   }

function get_footer_static(){
   new Ajax.Request(CssPath+'/footer.html',{asynchronous:true,
                                  method:'get', 
                                  on500: function(t){$('footer_static').innerHTML = on500Text;return;},
                                  on404: function(t){$('footer_static').innerHTML = on404Text;return;},
                                  onSuccess: function(t){$('footer_static').innerHTML = t.responseText;t.responseText.evalScripts();}
                                 });
   }

function validateCaptcha(cid,ID){
  var vcap = false;
  var captcha = $F('CaptchaImg_'+cid);
  new Ajax.Request('valcaptcha',{asynchronous:false,
                                  method:'post',
                                  postBody:'Captcha='+captcha+'&NavId='+ID,
                                  onSuccess: function(t){vcap = t.responseText;}
                                 });
  if(vcap.indexOf('true')>=0){
    return true;
  } else {
    // przerysuj obrazek
    var rand_no = Math.random();
    rand_no = rand_no * 1000;
    rand_no = Math.ceil(rand_no);
    $('captcha_'+cid).src='captcha\captcha_'+rand_no+'.jpg?CAID='+ID;
    Element.show('CaptchaAlert_'+cid);
    $('CaptchaImg_'+cid).value='';
  }
  return false;
}

function sendParamAndCaptcha(ajax_path,event,ID,FormWdg,FormCTX,div_to_be_updated,cid){
  if(validateCaptcha(cid,ID)){
    //alert('ok');
    sendParam(ajax_path,event,ID,FormWdg,FormCTX,div_to_be_updated);
  }
}

function sendParam(ajax_path,event,ID,FormWdg,FormCTX,div_to_be_updated) {
   var postBody = 'event='+event;
   var valid = new Validation(FormWdg, {useTitles:true});
   if(!valid.validate())
   {
   return false;
   }

   
     postBody = postBody +'&'+Form.serialize(FormWdg);
    if ($(FormCTX)!=null)
    {
      postBody = postBody +'&'+Form.serialize(FormCTX);
    }
    else
    {
      postBody = postBody +'&CTX_CMD1=CONTENT';
    }
    Form.disable(FormWdg);  
    if(ID==null){return false;}
    new Ajax.Updater(div_to_be_updated, 
                     ajax_path, 
                     {asynchronous:true,
                     evalscripts:true,
                       postBody:postBody});     
        
       
    return false;    
    }