function getFlashVersion(){
  // ie
  try {
    try {
      var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
      try { axo.AllowScriptAccess = 'always'; }
      catch(e) { return '6,0,0'; }
    } catch(e) {}
    return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1];
  // other browsers
  } catch(e) {
    try {
      if(navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin){
        return (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace(/\D+/g, ",").match(/^,?(.+),?$/)[1];
      }
    } catch(e) {}
  }
  return '0,0,0';
}

function updatecountry(selectedcitygroup, cleararea){
if(cleararea==1){
$("#select-areaid").html('<option value="0">- - - - - - - - - - - - - - - - - -</option>');
}
citieslist.options.length=0
for (i=0; i<cities[selectedcitygroup].length; i++)
citieslist.options[citieslist.options.length]=new Option(cities[selectedcitygroup][i].split("|")[0], cities[selectedcitygroup][i].split("|")[1])

}

function updatecity(cityid,areaid){

	document.getElementById('frmPost').cityid.value=cityid;
	if(areaid>0){
	getareaid ="&actareaid="+areaid;
	}else{
	getareaid =""
	}
	$(function(){
			 $.ajax({
				   type: "GET",
				   url: "index.php",
				   data: "response&getarealist="+cityid+getareaid,
				   success: function(data){
				    $("#select-areaid").html(data);
				   }
				 });
	
		})
	
}

$(function(){

			$("select#select-cityid").change(function(){
				if($(this).val()>0){
				 $.ajax({
				   type: "GET",
				   url: "index.php",
				   data: "response&getarealist="+$(this).val(),
				   success: function(data){
				     	$("#select-areaid").html(data);
				   }
				 });
				}
			})			
		})

function selectedsubcatid(){
	var subcatid = ($('#select-subcatid').val());
	if(subcatid==38 || subcatid==39  || subcatid==11  || subcatid==12 || subcatid==22  || subcatid==23|| subcatid==24  || subcatid==25 || subcatid==26)
	{
		$('#type-form').hide();
	}
	else
	{
		$('#type-form').show();		
	}
}




// walidacja forma
// ===========================
function validate_form(thisform){
  var add  = true;
	
  with (thisform){ 
 	if(thisform.adtitle){
	 if(adtitle.value==null || adtitle.value==""){
				$('#adtitle-form input').addClass("error");
				$('#adtitle-form label').addClass("error");
				$('#adtitle-form span').css('display', 'inline');
      add  = false;
    } else{ 
		 	$('#adtitle-form input').removeClass("error");
			$('#adtitle-form label').removeClass("error");
			$('#adtitle-form span').css('display', 'none');
		}
	}
	
  if($('#desc-form textarea').val()=="")
	{
	 $('#desc-form textarea').addClass("error");
	 $('#desc-form label').addClass("error");
	 $('#desc-form span').css('display', 'inline');
	 add  = false;
	}
	else
	{ 
	    $('#desc-form textarea').removeClass("error");
	    $('#desc-form label').removeClass("error");  
	    $('#desc-form span').css('display', 'none');
	}	
	
	
	

	if(thisform.email){	
		if (email.value==null || email.value==""){ 
		    $('#email-form input').addClass("error");
		    $('#email-form label').addClass("error");
		    $('#email-form span.text_1').css('display', 'inline');
		  	add  = false;
			}else{
				  a_pozycja = email.value.indexOf("@");
	    		dot_pozycja = email.value.lastIndexOf(".");
	    		if (a_pozycja<1 || dot_pozycja-a_pozycja<2){ 
		  			$('#email-form input').addClass("error");
				    $('#email-form label').addClass("error");
				    $('#email-form span.text_1').css('display', 'none');
				    $('#email-form span.text_2').css('display', 'inline');
						add = false; 
					}else{ 
						$('#email-form input').removeClass("error");
				    $('#email-form label').removeClass("error");
				    $('#email-form span.text_1').css('display', 'none');
				    $('#email-form span.text_2').css('display', 'none');
					}	
			}	
 	}	  
  
	if(thisform.captcha){
	 if(captcha.value==null || captcha.value==""){
				$('#captcha-form input').addClass("error");
				$('#captcha-form label').addClass("error");
				$('#captcha-form span.hint').addClass("error");
      add  = false;
    } else{ 
		 	$('#captcha-form input').removeClass("error");
			$('#captcha-form label').removeClass("error");
			$('#captcha-form span.hint').removeClass("error");
		}
	}	  
  
 	if(thisform.agree){
	 if($('input#agree').is(':checked')==false){
	 
				$('#agree-form span').addClass("error");
				$('span#agree-check').addClass("error-border	");
      add  = false;
    } else{ 
		 	$('#agree-form span').removeClass("error");
			$('span#agree-check').removeClass("error-border	");
		}
	}	   
  
  
  if(thisform.countryid){
	 if(countryid.value==null || countryid.value==""){
	 	$('#select-countryid').addClass("error");
		}else{
		$('#select-countryid').removeClass("error");
		}
	}

  if(thisform.cityid){
	 if(cityid.value==null || cityid.value==""){
	 	$('#select-cityid').addClass("error");
	 	$('#loc-form label').addClass("error");
	 	$('#loc-form span').css('display', 'inline');
	 	add  = false;
		}else{
		$('#select-cityid').removeClass("error");
		$('#loc-form label').removeClass("error");
	 	$('#loc-form span').css('display', 'none');
		}
	}

  if(thisform.areaid){
	 if(areaid.value==null || areaid.value==""){
	 	$('#select-areaid').addClass("error");
	 	$('#loc-form label').addClass("error");
	 	$('#loc-form span').css('display', 'inline');
	 	add  = false;
		}else{
		$('#select-areaid').removeClass("error");
		$('#loc-form label').removeClass("error");
	 	$('#loc-form span').css('display', 'none');
		}
	}


  if(thisform.subcatid)
	{
	 if(subcatid.value==null || subcatid.value==0 || subcatid.value==""){
	 	$('#select-subcatid').addClass("error");
	 	$('#cat-form label').addClass("error");
	 	$('#cat-form span').css('display', 'inline');
	 	add  = false;
		}else{
		$('#select-subcatid').removeClass("error");
		$('#cat-form label').removeClass("error");
	 	$('#cat-form span').css('display', 'none');
		}
	}

}
  if(add==false)
	{
  scroll(0,120);
  alert	('Uzupełnij brakujące pola w formularzu!');
  }
  return add ;
}

// Deleter
function deleter(did,picid,aid,picdate)
{	
	$('#pic-err').html('')
	if(aid)
	{
		var d = "did="+did+"&deleditpic="+picid+"&aid="+aid;
	}
	else
	{
		var d = "picdate="+picdate+"&picdel="+picid;
	}
	 $.ajax({
	   type: "POST",
	   url: "index.php?response",
	   data: d,
	   dataType: "json",
	   success: function(data)
		 {
		 		if(data.get==2)
		 		{
		 			 $('#pic-err').html('<span class="error">Błąd usuwania zdjęcia. Spróbuj ponownie. </span>');
		 		}
		 		else
		 		{
	     		$("#pic_"+did).remove();
	     		$('#progressstatus').hide();
	     		$('.uploadactive').show(); 		
	   		}
		 }	
	 });
}
// Uploader
function uploader()
{
	$('#pic-err').html('')
	$("#loader").show();
	var options = 
	{
		beforeSubmit: showRequest,
		dataType:  'json', 
		success:  showResponse,
		url: 'index.php?response'
	};
	
	$('#frmPost').ajaxSubmit(options);
	return false;

	function showRequest(formData, jqForm, options)
	{
		$('#frmPost').append($('<input type="hidden" name="upload" value="1" />'));
		return true;
	} 
	function showResponse(data, statusText)  
	{
		if (statusText == 'success') 
		{
			if(data.get== '1')
			{


				spanDel = '<span class="deleter" id="deleter_'+data.id+'">usuń</span>';
				var pic =	$('<div></div>').append($('<img/>').attr({ src : data.src , alt : ''}),spanDel).addClass("pic").attr({ id: 'pic_'+data.id}); ;		
				if(data.end<1)
				{
					$('#form-upload').hide();
				}
				$('#pictures').append(pic);
				if(data.aid == '0')
				{
					$('#deleter_'+data.id).click(function(){
					  deleter(data.id,data.file,'',data.date)
					});
				}
				else
				{
					$('#deleter_'+data.id).click(function(){
					 deleter(data.id,data.file,data.aid)
					});
				}
			}
			else if (data.get== '2')
			{
				$('#pic-err').html('<span class="error">Niepoprawne rozszerzenie zdjęcia (dozwolone tylko jpg/gif/png)</span>');
			}
			else if (data.get== '3')
			{
				$('#pic-err').html('<span class="error">Rozmiar zdjęcia jest za duży (max '+data.maxsize+'MB)</span>');
			}
			else if (data.get=='4')
			{
				alert('Dodałeś już max. ilość zdjęć');
				$('#form-upload').hide();
			}
			else
			{
					$('#pic-err').html('<span class="error">Błąd wczytywania zdjęcia. Spróbuj ponownie.</span>');	
			}
			
		}
		else 
		{
			$('#pic-err').html('<span class="error">Błąd wczytywania zdjęcia. Spróbuj ponownie.</span>');
		}
	  $('#file').html('<input type="file" name="pic" size="42" class="file"  onchange="uploader()"/>');
	  $("#loader").hide();
		$("#frmPost").unbind("ajaxStart ajaxStop");
	} 
}
function confirmSubmit(Text)
{
	var agree=confirm(Text);
	if (agree)
	{
		return true ;
	}else
	{
		return false ;
	}
} 
$(function(){   
$('#price').keyup(function () { 
    this.value = this.value.replace(/[^0-9,\ ]/g,'');
});

$('#phone').keyup(function () { 
    this.value = this.value.replace(/[^0-9- ]/g,'');
});

$('.t1').mouseover(function() {
  $(".t1").addClass('highlight');
  $(".t2, .t3 ,.t4").removeClass('highlight');
});

$('.t2').mouseover(function() {
  $(".t2").addClass('highlight');
  $(".t1, .t3 ,.t4").removeClass('highlight');
});

$('.t3').mouseover(function() {
  $(".t3").addClass('highlight');
  $(".t1, .t2 ,.t4").removeClass('highlight');
});

$('.t4').mouseover(function() {
  $(".t4").addClass('highlight');
  $(".t1, .t2 ,.t3").removeClass('highlight');
});
})
