var IE = document.all?true:false
if (!IE) document.captureEvents(Event.MOUSEMOVE)

document.onmousemove = getMouseXY;

var tempX = 0;
var tempY = 0;
var startX = 0;
var startY = 0;
var moveable=0;
var modaltop =0;
function getMouseXY(e) {
	
  if (IE) { 
    tempX = event.clientX + document.body.scrollLeft;
    tempY = event.clientY + document.body.scrollTop;
  } else {  
    tempX = e.pageX;
    tempY = e.pageY;
  }  
  
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  

 
//  document.Show.MouseX.value = tempX
//  document.Show.MouseY.value = tempY
//	document.getElementById("WindowM1").innerHTML+=tempX." ";
	//document.getElementById("mousex").value=tempX;
	//document.getElementById("mousey").value=tempY;
	//document.getElementById("WindowM1_header").style.cursor="pointer";
	if (moveable==1)
	{
		
	document.getElementById("WindowM1").style.left=(tempX-startX)+"px";
	document.getElementById("WindowM1").style.top=(tempY-startY)+"px";
	document.getElementById("WindowM1_body").style.left=(tempX-startX)+"px";
	document.getElementById("WindowM1_body").style.top=(tempY-startY)+"px";
	document.getElementById("WindowM1_background").style.left=(tempX-startX+10)+"px";
	document.getElementById("WindowM1_background").style.top=(tempY-startY+10)+"px";
//	document.getElementById("WindowM1_header").style.cursor="move";
	}
	return true
}

function SetMoveable()
{

	
	if (moveable==1) 
	{
		document.getElementById("WindowM1_header").style.cursor="pointer";
		moveable=0;
	}
	else 
	{
		moveable=1;
		document.getElementById("WindowM1_header").style.cursor="move";
	}
	startX=document.getElementById("WindowM1").style.left;
	startY=document.getElementById("WindowM1").style.top;
	
	startX=startX.substring(0,startX.length-2);
	startY=startY.substring(0,startY.length-2);
	
	startX=tempX-startX;
	startY=tempY-startY;
	
	document.getElementById("mousexs").value=startX;
	document.getElementById("mouseys").value=startY;
}

var PajaxIterate=0,
	OldStyle; 
	

function ClearLayer(id)	
{
	document.getElementById(id).innerHTML="";

}
	
function PajaxDebug(data,funct)
{
    return;
	if (isDebug==0) return;
	
	teraz=new Date();
	godzina=teraz.getHours();
	if ((godzina.length)==1) godzina="0"+godzina;
	minuta=teraz.getMinutes();
	if ((minuta.length)==1) minuta="0"+minuta;

	sekunda=teraz.getSeconds();
	if ((sekunda.length)==1) sekunda="0"+sekunda+" ";

	czas=godzina+":"+minuta+":"+sekunda; 

	txt=document.getElementById("debug_layer").innerHTML;
	document.getElementById("debug_layer").innerHTML="<font class=pajaxdebug_time>"+czas+"</font>| <font class=pajaxdebug_normal> "+data+"</font> <font class=pajaxdebug_function>["+funct+"]</font> <br>"+txt;
}

function ChangeCssStyle(id,style)
{
	OldStyle=id.className;
	id.className=style;
	
}

function RestoreCssStyle(id)
{
	id.className=OldStyle;
}


function CCS(id,style)
{
	ChangeCssStyle(id,style);
}

function RCS(id)
{
	RestoreCssStyle(id);
}

function ChangeVisibility(id)
{
	if (document.getElementById(id).style.visibility=="visible") document.getElementById(id).style.visibility="hidden"
	else
	document.getElementById(id).style.visibility="visible";

}

function ModalWindow(id,left,top,width,height)
{
	moveable=0;
	modaltop=top;
	document.getElementById("WindowM1").style.left=left+"px";
	document.getElementById("WindowM1").style.top=top+"px";
	document.getElementById("WindowM1").style.width=width+"px";
	document.getElementById("WindowM1").style.height=height+"px";
	document.getElementById("WindowM1_background").style.left=left+10+"px";
	document.getElementById("WindowM1_background").style.top=top+10+"px";
	document.getElementById("WindowM1_background").style.width=width+10+"px";
	document.getElementById("WindowM1_background").style.height=height+10+"px";
	
	ChangeVisibility('modallayer');
	ChangeVisibility(id+'_body');
	ChangeVisibility(id+'_header');
	ChangeVisibility(id+'_background');
	ChangeVisibility(id);
}


function ReloadPage(adres,target,component,modalaction,question,caption) {
        
			if (question==1)
			{
				 ans=window.confirm(caption); 
				 if (ans==false) return;
				 

			}      
			advAJAX.get({
              url : adres,
              onInitialization : function(obj) { 
              									 document.getElementById('busylayer').innerHTML="<img src='library/progressbar.gif'> Initializacja ";
              									 document.body.style.cursor='wait';
              									},
              onLoading : function(obj) { 
              									 document.getElementById('busylayer').innerHTML="<img src='library/progressbar.gif'> Przetwarzanie danych";},
              onError : function(obj) {
              									 document.getElementById(target).innerHTML="<b style='color:red'>Error while loading page "+adres+"<br><br>";
              									 		 document.body.style.cursor='default';
              							},
              							
              onSuccess : function(obj) { document.getElementById(target).innerHTML = obj.responseText;
              							 temp=obj.responseText;
 
										
              							
			              			document.getElementById('busylayer').innerHTML="OK";
			              			document.body.style.cursor='default';
              							 if ((modalaction==1))
										 {
										 	ModalWindow('WindowM1',0,0,0,0);
										 }
                                        data = temp.split('\n');
                                        arraylen = data.length;
                                        var i = 0;
                                        while(i < arraylen)
                                        {
                                        if( data[i].indexOf("<script") > -1)
                                        {
                                            i++;
//                                            alert(i);
                                            while(data[i].indexOf("/script>") < 0)
                                            {
//                                            alert(data[i]);
                                            eval(data[i]);
                                            i++;
                                            }
                                        }
                                        i++;
                                        }
                                        
              }
                                        }
            							
            );
        }


function ReloadMainPage(adres,target,component,title,nr)
{
    
    ReloadPage(adres,target,component);
    document.getElementById("ptitle").innerHTML=title;
    
}

        
function CalcKeyCode(aChar) {
  var character = aChar.substring(0,1);
  var code = aChar.charCodeAt(0);
  return code;
}        


function ReloadForm(id,target,component,modalaction,question,caption)
{
	if (question==1)
			{
				 ans=window.confirm(caption); 
				 if (ans==false) return;
				 

			}      
	advAJAX.submit(document.getElementById(id), {
              onInitialization : function(obj) { 
              									 document.getElementById('busylayer').innerHTML="<img src='library/progressbar.gif'> Przetwarzanie danych";
												document.body.style.cursor='wait';

              },
              onError : function(obj) {
              							document.getElementById('busylayer').innerHTML="<b style='color:red'>Error while loading page "+obj.responseText;
										document.body.style.cursor='default';
              },
              onSuccess : function(obj) { document.getElementById(target).innerHTML = obj.responseText;
              							 
										document.getElementById('busylayer').innerHTML="OK";
										document.body.style.cursor='default';
										 if (modalaction==1) 
										 {
										 	
										 	ModalWindow('WindowM1',0,0,0,0);
										 }
              							 if ((modalaction==1))
										 {
										 	ModalWindow('WindowM1',0,0,0,0);
										 }
/*										data = temp.split('\n');
										arraylen = data.length;
										var i = 0;
										while(i < arraylen)
										{
										if( data[i].indexOf("script") > -1)
										{
											i++;
											while(data[i].indexOf("/script") < 0)
											{
											eval(data[i]);
											i++;
											}
										}
										i++;
										}
*/
              				}
	}); 
}

function ChangeCA(id,target,component,modalaction,question,caption,fcon,fact)
{
	
	document.getElementById('controller').value=fcon;
	document.getElementById('caction').value=fact;
	ReloadForm(id,target,component,modalaction,question,caption);
}

function checkNumber(val) {
  var strPass = val.value;
  var strLength = strPass.length;
  var lchar = val.value.charAt((strLength) - 1);
  var cCode = CalcKeyCode(lchar);
 
 
  if ((cCode < 48 || cCode > 57 )) {
    var myNumber = val.value.substring(0, (strLength) - 1);
    val.value = myNumber;
  }
  return false;
}

function NextFocus(e,nex)
{
	var character;
	var Key;

 
    Key = e.keyCode;
    
    
	if (Key==13) document.getElementById(nex).focus();
  

}
   


function checkDate(val) {
  var strPass = val.value;
  var strLength = strPass.length;
  var lchar = val.value.charAt((strLength) - 1);
  var cCode = CalcKeyCode(lchar);
 
  if ((cCode < 48 || cCode > 57 ) && (cCode!=45)) 
  {
    var myNumber = val.value.substring(0, (strLength) - 1);
    val.value = myNumber;
  }
  return false;
}


function checkFloat(val) {
  var strPass = val.value;
  var strLength = strPass.length;
  var lchar = val.value.charAt((strLength) - 1);
  var cCode = CalcKeyCode(lchar);
 
  
  if ((cCode < 48 || cCode > 57 ) && (cCode !=44) && (cCode !=46)) 
  {
    var myNumber = val.value.substring(0, (strLength) - 1);
    val.value = myNumber;
    
  }
  
  if (cCode==44) 
  {
    var myNumber = val.value.substring(0, (strLength) - 1);
    val.value = myNumber + ".";
  }
  
  return false;
}

function checkDate(val) {
  var strPass = val.value;
  var strLength = strPass.length;
  var lchar = val.value.charAt((strLength) - 1);
  var cCode = CalcKeyCode(lchar);
 
  
  if ((cCode < 48 || cCode > 57 ) && (cCode !=45)) 
  {
    var myNumber = val.value.substring(0, (strLength) - 1);
    val.value = myNumber;
    
  }
  
  
  return false;
}

function ShowModalURLForm(url,l,t,w,h)
{

	
	document.getElementById("WindowM1_body").innerHTML="<img src='library/progressbar.gif'> Prosze czekac ... ";
	ReloadPage(url,'WindowM1_body','PajaxLink');
	ModalWindow('WindowM1',l,t+self.pageYOffset,w,h);
	ph=window.innerHeight != null? window.innerHeight: document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight:document.body != null? document.body.clientHeight:null;
	ph1=window.pageYOffset != 'undefined' ? window.pageYOffset:document.documentElement && document.documentElement.scrollTop? document.documentElement.scrollTop: document.body.scrollTop?document.body.scrollTop:0;

	document.getElementById("modallayer").style.height=document.body.offsetHeight+"px";


//	alert (document.body.offsetHeight);
}

function scrollermodal()
{
	
	document.getElementById("WindowM1").style.top=window.pageYOffset+"px";

	document.getElementById("WindowM1").style.top=modaltop+window.pageYOffset+"px";
	document.getElementById("WindowM1_background").style.top=modaltop+10+window.pageYOffset+"px";

}

function okno(value)
{
    document.getElementById("INPUT[numer]").value=value;


	return false;
}

function WriteValueById(element,value)
{
    document.getElementById(element).value=value;
	
}

function WriteValueByName(element,value)
{
    document.getElementById(element).value=value;
	
}

function SetFocus(element,style)
{
	element.className=style;
}

function jsUpload(upload_field,filtr)
{
	
//    re_text = /\.csv|\.xls|\.zip/i;
    re_text=filtr;
    var filename = upload_field.value;

    if (filename.search(re_text) == -1)
    {
        alert("Błędne rozszerzenie");
        upload_field.form.reset();
        return false;
    }

    upload_field.form.submit();
    document.getElementById('status1').innerHTML = "Status przetwarzania : Ładowanie pliku...";
    return true;
}


function PrintPage(){
   if (!window.print){
      alert("Twoja przeglądarka nie drukuje!")
   return 0;
   }
 window.print(); 
}

