﻿var loading = false;
var Voted =false;
var waitElement;
var scrollX, scrollY = -1;

function GetAjaxError(message, oContext){
     Unloading();
}
function AjaxCall(address,message,resultFunction,context){
     
       
       Loading();
       WebForm_DoCallback(address,message,resultFunction,context,GetAjaxError,false);
     
     
}

function GetCallbackResultToDemocracy(result,context)
{
        Unloading();
        document.getElementById('DemocracyBox').innerHTML =result;     
}
function GetCallbackResultToMenu(result,context){
        Unloading();
    
        var temp = result.split("<unique>");
        
        document.getElementById('right-login').innerHTML = temp[0];
        if (temp.length == 2 )
               document.getElementById('BasketMain').innerHTML = temp[1];
             
    
}
function GetCallbackResultToBasket(result,context){
        Unloading();
        document.getElementById('BasketMain').style.display ="block";
        document.getElementById('BasketMain').innerHTML = result;
       
      
 
}
function GetCallbackResultToBasket2(result,context){
         Unloading();
        
        if (result != 'nada'){
               
              document.getElementById('BasketMain').style.display ="block";
               document.getElementById('BasketMain').innerHTML = result;
       }
}
function GetCallbackResultToVoter(result,context)
{   Unloading();
    document.getElementById('voter').innerHTML = result;
    Voted = true;

}

function GetCallbackResultToDelivery(result,context)
{   
        Unloading();
        document.getElementById('deliverer').innerHTML = result;
    

}
function GetCallbackResultToBasket3(result,context){
         
       Unloading();
       if (result != 'nada'){
               if(result == "")
                        document.getElementById('BasketMain').style.display ="none";
                        else{
                        document.getElementById('BasketMain').style.display ="block";
               document.getElementById('BasketMain').innerHTML = result;
               }
       }
}

function GetCallbackResultToMiddle(result,context){
        Unloading();
       
        if(result != '')
        document.getElementById('middle-main').innerHTML = result;
       
         
}
function GetCallbackResultToRandom(result,context){
        Unloading();
        document.getElementById('RandomContainer').innerHTML = result;
        window.setTimeout(GetRandom, 30*1000); 
}

function GetCallbackResultToNews(result,context){
        Unloading();
       
        document.getElementById('NewsletterContainer').innerHTML = result;
       
}
function AjaxParam(name,objId){
        return escape(name + '=' + document.getElementById(objId).value);

}
function AjaxParamInt(name,integer){
    return escape(name + '=' + integer);

}

function AjaxParamSt(name,integer){
    return escape(name + '=' + integer);

}
function AjaxParamCB(name,objId){
        return escape(name + '=' + (document.getElementById(objId).checked ? '1':'0'));

}


function LoginSend()    { 
      AjaxCall('Logowanie','Login&' + AjaxParam('login','Login') + '&' + AjaxParam('password','Password'),GetCallbackResultToMenu,'');
 }
function GetPage(number,category){   
   scroll(0,0);  
     AjaxCall('MainContent$MainProducts','PageSelect&' + AjaxParamInt('page',number) + '&' + AjaxParamInt('category',category),GetCallbackResultToMiddle,'');   
}

function Ocen(number,id)
{
    if(!Voted)
    AjaxCall('MainContent$DetailProduct$Voter','Vote&'+AjaxParamInt('value',number)+'&'+AjaxParamInt('id',id) ,GetCallbackResultToVoter,'');   

}

function BasketAdd(number){
     scroll(0,0);
    AjaxCall('Basket','AddProduct&' + AjaxParamInt('product',number) ,GetCallbackResultToBasket,'');
}
function BasketRem(number){
    AjaxCall('Basket','RemoveProduct&' + AjaxParamInt('product',number) ,GetCallbackResultToBasket3,'');
}

function SendOrder(){
    AjaxCall('Basket','Order&',GetCallbackResultToBasket,'');
}

function SendSearch(){
    
    AjaxCall('MainContent$SearchResult','Search&'+AjaxParam('query','SearchTB'),GetCallbackResultToMiddle,'');
    
}
function Newsletter(Direction){
    
    AjaxCall('Newsletter',Direction+'&'+AjaxParam('email','NewsBox'),GetCallbackResultToNews,'');
   

}

function ChangeDelivery(value)
{
    if (document.getElementById(value))
    {
        AjaxCall('Basket$Delivery','SelectPayment&' + AjaxParam('platnosc',value),GetCallbackResultToDelivery,'');
    
    }


 
}
function RecallCart()
{

    var max = document.getElementById('Counter').value;
    var command='';
    var h=1;
    for (h=1;h<=max;h++)
    {    
       command+='AjaxParamInt(\''+h+'\',\''+document.getElementById('basket'+h).value+'\')+ \'&\'+';
    }
    command+='\'\'';
    AjaxCall('Basket','Recall&' + eval(command) ,GetCallbackResultToBasket3,'');     

}

function Loading(){
          document.getElementById("Loader").style.visibility = 'visible'; 
}

function Unloading(){

var Loader = document.getElementById("Loader");
        if(Loader.style.visibility == 'visible')
            Loader.style.visibility = 'hidden';
}




function Initalize()
{
     
       if (window.addEventListener) {
	   window.addEventListener('scroll', MoveWaitElement, false);
	   window.addEventListener('resize', MoveWaitElement, false);
       }
       else if (window.attachEvent) {
	        window.attachEvent('onscroll', MoveWaitElement);
	        window.attachEvent('onresize', MoveWaitElement);
        }
        MoveWaitElement();

    
    
        
        
       
}
function MoveWaitElement() {
	var scrollYT, scrollXT;
	waitElement = document.getElementById("Loader");
	if (typeof(window.pageYOffset) == "number") { 
		scrollYT = window.pageYOffset; 
		scrollXT = window.pageXOffset; 
	} 
	else if (document.body && document.documentElement && document.documentElement.scrollTop) { 
		scrollYT = document.documentElement.scrollTop; 
		scrollXT = document.body.scrollLeft;
	}
	else if (document.body && typeof(document.body.scrollTop) == "number") { 
		scrollYT = document.body.scrollTop; 
		scrollXT = document.body.scrollLeft; 
	} 
	if (scrollX != scrollXT || scrollY != scrollYT) {
		scrollX = scrollXT;
		scrollY = scrollYT;
		var width = document.body.clientWidth;
		waitElement.style.top = scrollYT + "px";
		waitElement.style.right = -scrollXT +  "px";
	}
}


function Submitter(e){

if (!e) var e = window.event
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;

if (code ==13)
    return true;
else return false;

}

function ShowBig(url) {
     Win=window.open('/Picture.aspx?src='+url,'displayWindow','width=50,height=50,titlebar=no,toolbar=no,resizable=yes,scrollbars=no,status=no,menubar=no,location=no,directories=no,copyhistory=no' );
  
}

function ShowInfo(id,type) {
     Win=window.open('/Opis.aspx?id='+id+'&type='+type,'displayWindow','width=210,height=175,titlebar=no,toolbar=no,resizable=yes,scrollbars=auto,status=no,menubar=no,location=no,directories=no,copyhistory=no' );
  
}

function CallInfo(DropID,type)
{
    ShowInfo(document.getElementById(DropID).value,type);
}

function rb()
{
    
    firma = document.getElementById('FirmaBox');
    nip = document.getElementById('NipBox');
    
    if (firma.style.display == '')
    {
        firma.style.display = 'none';
        nip.style.display = 'none';
    
    }else
    {
        firma.style.display = '';
        nip.style.display = '';
    }

}


function SetCookie(Ank_ID) {
     
     var today = new Date();
     var expire = new Date();
     
     expire.setTime(today.getTime() + 3600000*24*30);
     document.cookie = "Ankieta="+escape(Ank_ID)
                     + ";expires="+expire.toGMTString();
}

function SendDemocracy(id,pos)
{
   SetCookie(id);
   AjaxCall('Democracy','Login&' + "id="+id+"&"+"pos="+pos ,GetCallbackResultToDemocracy,'');
}

