function GetLoadStatus(to) 
{
	var xmlHttpReq = false;
	var self = this;
	   
	var url = "/inc/getLoadStatus.php?to="+to;
	
	 if (window.XMLHttpRequest) 
	 {
		 self.xmlHttpReq = new XMLHttpRequest();
	 }
	    // IE
	 else if (window.ActiveXObject) 
	 {
		 self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	 }
	    
	 self.xmlHttpReq.open('GET', url, false);
	    
	 self.xmlHttpReq.send(null);
	    
	 if (! isStatusGood(self.xmlHttpReq.status)) 
	 {
		    throw "Failed to get " + url + ", status: " + self.xmlHttpReq.status;
	 }
	    
		
	 
	 if ( self.xmlHttpReq.responseText == "-1")
		 return true;
	 else 
		 return false;
		//document.write(self.xmlHttpReq.responseText);
	//	return true;
		 
		 
	 	      
}
	
	
function CheckLoaded1()
{	
	
	if (GetLoadStatus(1))
	{
		AjaxLoadImg("/inc/upload_images.inc.php?img=1", "img1");			
	}
	else	
		setTimeout ( "CheckLoaded1()", 2000 );
}



function CheckLoaded2()
{		
	if (GetLoadStatus(2))
	{
		AjaxLoadImg("/inc/upload_images.inc.php?img=2", "img2");			
	}
	else	
		setTimeout ( "CheckLoaded2()", 2000 );
}

function CheckLoaded3()
{		
	if (GetLoadStatus(3))
	{
		AjaxLoadImg("/inc/upload_images.inc.php?img=3", "img3");			
	}
	else	
		setTimeout ( "CheckLoaded3()", 2000 );
}


function CheckLoaded4()
{		
	if (GetLoadStatus(4))
	{
		AjaxLoadImg("/inc/upload_images.inc.php?img=4", "img4");			
	}
	else	
		setTimeout ( "CheckLoaded4()", 2000 );
}


function SetCountWords(text, div,max)
{		
	limitNum = text.value.length;
	if (limitNum>max)
		text.value = text.value.substring(0, max);	
		
	document.getElementById(div).innerHTML = "<b>"+(max-text.value.length)+"</b>";	
}



function HandleFile (form, num) {
	var file = form.uploadedFile; //get file element
		
	//document.getElementById("upload_status").innerHTML = "WysyĹ‚anie obrazka na serwer. PROSZÄ� CZEKAÄ†!";
	 if (window.XMLHttpRequest) {
		 xmlHttp = new XMLHttpRequest();
	    }
	    // IE
	    else if (window.ActiveXObject) {
	    	xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	    }	

	if(xmlHttp!=null)
	{		
		xmlHttp.onreadystatechange=function()
		{		
			if(xmlHttp.readyState==4)
			{			
				
			}
		}
		xmlHttp.open("GET",'/inc/up'+num+'.php' ,false);
		//xmlHttp.setRequestHeader('Content-type', 'text/plain');

		xmlHttp.send(file);		
	}
}

function UploadImage1(form)
{
	HandleFile(form,1);	return true;
}
		 

function UploadImage2(form)
{
	HandleFile(form,2);	
	return true;
}
		 


function UploadImage3(form)
{
	HandleFile(form,3);	
	return true;
}


function UploadImage4(form)
{
	HandleFile(form,4);	
	return true;
}
		 
		 
function SetImage(img)
{
	document.getElementById(img).innerHTML="<img src='/img/loading.gif'>";	
}

function ShowHide(del)
{
	obj = document.getElementById(del);
	if (obj.style.visibility=='hidden')
	{
		obj.style.visibility = 'visible';
		//obj.style.height=100;
	}
	else
	{
		obj.style.visibility = 'hidden';	
		//obj.style.height=0;
	}
}


function ShowHide2(del)
{
	obj = document.getElementById(del);
	
	if (obj.style.display=='none')
		obj.style.display = 'block';
	else
		obj.style.display= 'none';	
}

//var s_active_adv = false;

function ActiveAdv(id, pr)
{
	//if (s_active_adv==false)
	{
		//s_active_adv = true;
		var xmlHttpReq = false;
		var self = this;
		progress = document.getElementById(pr);
		progress.innerHTML="<img src='/img/loading.gif'><br><H3>Proszę czekać...</h3>"
		
		 if (window.XMLHttpRequest) {
			self.xmlHttpReq = new XMLHttpRequest();
		 }
		 // IE
		 else if (window.ActiveXObject) {
			self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
		 }
			
		self.xmlHttpReq.open('GET', "/inc/active_adv.inc.php?id="+id, true);
		self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		self.xmlHttpReq.onreadystatechange = function() {
		if (self.xmlHttpReq.readyState == 4) {
			document.getElementById("advert").innerHTML =  self.xmlHttpReq.responseText;
			AjaxLoad('/window/category.inc.php','category'); 
			//s_active_adv = false;
			}
		}    
		   
		self.xmlHttpReq.send();
	}
}

function CheckForm(form)
{
	result = false;
	if(form.kategoria.value=="")	
		alert('Kategoria nie została ustawiona!');		
	else	
	if (form.s.value=="")	//tytul
		alert('Wprowadź tytuł ogłoszenia!');	
	else
	if (form.l.value=="")	//opis
		alert('Wprowadź treść ogłoszenia!');	
	else
	if (form.okres.value=="")	
		alert('Wybierz okres ważności ogłoszenia!');	
	else
	if (form.email.value=="")	
		alert('Wprowadź poprawdny adres email!');	
	else
	if (form.miejsc.value=="")	
		alert('Wprowadź miejscowość!');	
	else
	if (form.tel.value=="")	
		alert('Wprowadź numer telefonu!');	
	else
	if (form.cena.value=="")	
		alert('Wprowadź cenę!');	
	else
	if (form.reg.checked==false)	
		alert('Zatwierdź regulamin!');	
	else
		result = true;
	return result;
}


function SetMovie(movie)
{
	m = document.getElementById("movie");
	m.innerHTML="<center><iframe height='420' frameborder='0' width='560' allowfullscreen src='http://www.youtube.com/embed/"+movie+"'></iframe></center>";	
	
}

function AddAdvAndActive(form) {	
	if (CheckForm(form))
	{
	    var xmlHttpReq = false;
	    var self = this;
	
	    if (window.XMLHttpRequest) {
	        self.xmlHttpReq = new XMLHttpRequest();
	    }
	    // IE
	    else if (window.ActiveXObject) {
	        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	    }
	    
	    self.xmlHttpReq.open('POST', "/inc/add_adv.inc.php?active=1", true);
	    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	    self.xmlHttpReq.onreadystatechange = function() {
	        if (self.xmlHttpReq.readyState == 4) {	        	
				document.getElementById("advert").innerHTML =  self.xmlHttpReq.responseText;
				scroll(0,0);				
	        }
	    }
 
 
		x= "s=" + encodeURI(form.s.value)
		+"&l=" + encodeURI(form.l.value)
		+"&cat=" + encodeURI(form.kategoria.value)
		+"&email=" + encodeURI(form.email.value)
		+"&okres=" + encodeURI(form.okres.value)
		+"&miejsc=" + encodeURI(form.miejsc.value)
		+"&tel=" + encodeURI(form.tel.value)
		+"&cena=" + encodeURI(form.cena.value)
		+"&film_1=" + encodeURI(form.film_1.value)
		+"&film_2=" + encodeURI(form.film_2.value)
		+"&cena=" + encodeURI(form.cena.value)
		+"&update=" + encodeURI(-1);	
		//alert(x);
	
		
		self.xmlHttpReq.send(x);
	}
}



function AddAdv(form,update,edycja) {	
	if (CheckForm(form))
	{
	    var xmlHttpReq = false;
	    var self = this;
	
	    if (window.XMLHttpRequest) {
	        self.xmlHttpReq = new XMLHttpRequest();
	    }
	    // IE
	    else if (window.ActiveXObject) {
	        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	    }
	    
	    self.xmlHttpReq.open('POST', "/inc/add_adv.inc.php", true);
	    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	    self.xmlHttpReq.onreadystatechange = function() {
	        if (self.xmlHttpReq.readyState == 4) {	        	
				document.getElementById("advert").innerHTML =  self.xmlHttpReq.responseText;
				scroll(0,0);
	        }
	    }
	    
 
 
		x= "s=" + encodeURI(form.s.value)
		+"&l=" + encodeURI(form.l.value)
		+"&cat=" + encodeURI(form.kategoria.value)
		+"&email=" + encodeURI(form.email.value)
		+"&okres=" + encodeURI(form.okres.value)
		+"&miejsc=" + encodeURI(form.miejsc.value)
		+"&tel=" + encodeURI(form.tel.value)
		+"&cena=" + encodeURI(form.cena.value)
		+"&film_1=" + encodeURI(form.film_1.value)
		+"&film_2=" + encodeURI(form.film_2.value)
		+"&update=" + encodeURI(update)	
		+"&edycja=" + encodeURI(edycja);
		//alert(x);
	
		
		self.xmlHttpReq.send(x);
	}
}
	
		

function DelAdv(form, admin) {		
	    var xmlHttpReq = false;
	    var self = this;
		//alert('test');
	    // Mozilla/Safari
	    
	    if (window.XMLHttpRequest) {
	        self.xmlHttpReq = new XMLHttpRequest();
	    }
	    // IE
	    else if (window.ActiveXObject) {
	        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	    }
	    
		self.xmlHttpReq.open('POST', "/inc/del_adv.inc.php", true);		
		
	    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	    self.xmlHttpReq.onreadystatechange = function() {
	        if (self.xmlHttpReq.readyState == 4) {
	        	
				document.getElementById("advert").innerHTML =  self.xmlHttpReq.responseText;
				AjaxLoad('/window/category.inc.php','category');
	        }
	    }
	       
		if (admin==true)
			self.xmlHttpReq.send("&id=" + encodeURI(form.id.value)+"&kod=" + encodeURI(form.kod.value)+"&admin=1");
		else
			self.xmlHttpReq.send("&id=" + encodeURI(form.id.value)+"&kod=" + encodeURI(form.kod.value));
	
}



function Upload(up_num)
{
	switch (up_num)
	{
		case 1:			
			
			//document.upload1.submit();
			//document.getElementById("upload1").submit();
			
			SetImage('img1');			
			CheckLoaded1();
		break;
		
		case 2:
			//document.upload2.submit();
			SetImage('img2');
			CheckLoaded2();
		break;
		
		case 3:
			//document.upload3.submit();
			SetImage('img3');
			CheckLoaded3();
		break;
		
		case 4:
			//document.upload4.submit();
			SetImage('img4');
			CheckLoaded4();
		break;
	}
}
var s_bad_adv = false;

function BadAdv(form)
{
	if (s_bad_adv==false)
	{
		s_bad_adv=true;
		var xmlHttpReq = false;
		var self = this;
		//alert(id);   
		var xmlHttpReq = false;
			var self = this;
		
			if (window.XMLHttpRequest) {
				self.xmlHttpReq = new XMLHttpRequest();
			}
			// IE
			else if (window.ActiveXObject) {
				self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
			}
			
			self.xmlHttpReq.open('POST', "/inc/badadv.inc.php", true);
			self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			self.xmlHttpReq.onreadystatechange = function() {
				if (self.xmlHttpReq.readyState == 4) {	        	
					document.getElementById("advert").innerHTML =  self.xmlHttpReq.responseText;	
					s_bad_adv=false;
				}
			}    
	 
			x= "message=" + encodeURI(form.message.value)
			+"&id=" + encodeURI(form.id.value);
			if (form.check1.checked) x+="&check1=" + encodeURI(form.check1.value);
			if (form.check2.checked) x+="&check2=" + encodeURI(form.check2.value);
			if (form.check3.checked) x+="&check3=" + encodeURI(form.check3.value);
			
			
			self.xmlHttpReq.send(x);
		}
}

var s_kontakt = false;
function Kontakt(form, mail, id)
{
	if (s_kontakt==false)
	{
		s_kontakt=true;
		var xmlHttpReq = false;
		var self = this;
		//alert(id);   
		var xmlHttpReq = false;
			var self = this;
		
			if (window.XMLHttpRequest) {
				self.xmlHttpReq = new XMLHttpRequest();
			}
			// IE
			else if (window.ActiveXObject) {
				self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
			}
			
			self.xmlHttpReq.open('POST', "/inc/kontakt.inc.php", true);
			self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			self.xmlHttpReq.onreadystatechange = function() {
				if (self.xmlHttpReq.readyState == 4) {	        	
					document.getElementById("advert").innerHTML =  self.xmlHttpReq.responseText;
					s_kontakt=false;
				}
			}    
	 
			x= "message=" + encodeURI(form.message.value)
			+"&mail=" + encodeURI(mail)
			+"&id=" + encodeURI(id)
			+"&emailod=" + encodeURI(form.emailod.value);
		
			
			
			self.xmlHttpReq.send(x);

			}
	}



function Erase(id,img) {
	var xmlHttpReq = false;
	var self = this;
	//alert(id);   
	var url = "/inc/erase.inc.php?id="+id+"&img="+img;
	
	 if (window.XMLHttpRequest) 
	 {
		 self.xmlHttpReq = new XMLHttpRequest();
	 }
	    // IE
	 else if (window.ActiveXObject) 
	 {
		 self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	 }
	    
	 self.xmlHttpReq.open('GET', url, false);
	    
	 self.xmlHttpReq.send(null);
	    
	 if (! isStatusGood(self.xmlHttpReq.status)) 
	 {
		    throw "Failed to get " + url + ", status: " + self.xmlHttpReq.status;
	 }
	   
	 document.getElementById("img"+img).innerHTML="";	   	 	      
	}
	
	
function checkEmail(email) 
{
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email.value)) 
	{
		//email.focus;
		alert('Wprowadź prawidłowy adres e-mail!');
		
		return false;
	}
	else return true;
}



	
function AddEmailToNewsletter(form)
{
	if (form.regulamin.checked)
	{
		var grupy="";
		
		var node_list = document.getElementsByTagName('input');
 
		for (var i = 0; i < node_list.length; i++) 
		{
    		var node = node_list[i];
 
    		if ((node.getAttribute('type') == 'checkbox') && (node.getAttribute('name') != 'regulamin') ) 
    		{
        		// do something here with a <input type="text" .../>
        		// we alert its value here
        		if (node.checked==true)
        			grupy+=node.getAttribute('name')+',';
    		}
		}
	//	alert(grupy);
		if ((checkEmail(form.email))&&(grupy!=""))
		{
			var xmlHttpReq = false;
			var self = this;
			
			 if (window.XMLHttpRequest) {
				self.xmlHttpReq = new XMLHttpRequest();
			 }
			 // IE
			 else if (window.ActiveXObject) {
				self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
			 }
				
			self.xmlHttpReq.open('POST', "/inc/newsletter.inc.php", true);
			self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			self.xmlHttpReq.onreadystatechange = function() {
			if (self.xmlHttpReq.readyState == 4) {
				document.getElementById("advert").innerHTML =  self.xmlHttpReq.responseText;
			//	AjaxLoad('/window/category.inc.php','category'); 
				//s_active_adv = false;
				}
			}    
			  
			x = "email="+encodeURI(form.email.value)
			+ "&grupy="+encodeURI(grupy);
			
			self.xmlHttpReq.send(x);
		}
		
	}else
			alert('Musisz zatwierdzić regulami!');
}
	
