var xmlhttp;

function GetXmlHttpObject()
{
  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    return new XMLHttpRequest();
  }
  if (window.ActiveXObject) {
    // code for IE6, IE5
    return new ActiveXObject("Microsoft.XMLHTTP");
  }
  return null;
}

function SimpleAjaxTask(url)
{
  xmlhttp=GetXmlHttpObject();
  if (xmlhttp==null) { alert ("Browser does not support HTTP Request"); return; }
  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
}

//  InnerHtmlAjaxTask
function InnerHtmlAjaxTask(html_id, url)
{
  xmlhttp=GetXmlHttpObject();
  if (xmlhttp==null) { alert ("Browser does not support HTTP Request"); return; }
  
  xmlhttp.onreadystatechange= function(){InnerHtmlCallback(html_id);};
  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
}
function InnerHtmlCallback(html_id)
{
  if (xmlhttp.readyState==4) {
  
    var newdiv = document.createElement("div");
    newdiv.innerHTML = xmlhttp.responseText;
    var container = document.getElementById(html_id);
    if ( container.hasChildNodes() ) {
      while ( container.childNodes.length >= 1 )
      {
        container.removeChild( container.firstChild );       
      } 
    }   
    container.appendChild(newdiv);
  }
}
function InnerHtmlAjaxTaskCP2(html_id, url, id)
{
  xmlhttp=GetXmlHttpObject();
  if (xmlhttp==null) { alert ("Browser does not support HTTP Request"); return; }

  xmlhttp.onreadystatechange= function(){ InnerHtmlCallbackCP2(html_id,id); };
  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
}
function InnerHtmlCallbackCP2(html_id,id)
{
  if (xmlhttp.readyState==4) {
    document.getElementById(html_id+id).innerHTML=xmlhttp.responseText;
  }
}

function $(id) {
    return document.getElementById(id);
}

function show_flyelement(idel) {
	$(idel).style.display="block";
}

function hide_flyelement(idel) {
	$(idel).style.display="none";
}

// Ajax Tasks
function bannerClick(id)
{
  SimpleAjaxTask("ajax_operation.php?id="+id+"&op=banner_click");
}

function ajax_deletefiledb(id, table_name) {	 
    advAJAX.get({
        url : "/ajax_operation.php?op=delete_file_db&id="+id+"&table_name="+table_name,
        onInitialization : function() {
        },
        onComplete: function () {
        },
        onError : function() {
        	alert('Wystąpił błąd!');
        },
        onSuccess : function(obj) {
        	switch (obj.responseText) {
        		case 'ok':
        		  document.getElementById('result').innerHTML = "<span style='font-size:12px;color:blue;'>Zjęcie zostało usunięte, zaczekaj na przedładowanie okna...</span>";
							window.location.href=window.location.href;
        			break;
        		case 'error':
        			alert('Nie udało się skasować zdjęcia');
        			break;
        		case 'null':
        			alert('Wystąpił błąd! Nie udało się skasować zdjęcia');
        		  break;
        		default:
        			break;
        	}
        }
    });
}

function deleteFileDB(id, table_name) {
	if (confirm('Czy na pewno chcesz usunąć wybrane zdjęcie?')) ajax_deletefiledb(id, table_name);
}

function startUpload(){

  var ext = document.upload_form.myfile.value;    
  ext = ext.substring(ext.length-3,ext.length);
  ext = ext.toLowerCase();
  if(ext != 'jpg') {
    alert('Nieobsługiwany format, wybierz plik z rozszerzeniem .jpg');
    return false;
  }
  else {
    show_flyelement('upload_process');
    document.getElementById('result').innerHTML = '';
    return true;
  }
}

function stopUpload(success){
  var result = '';
  if (success == 0) {
    document.getElementById('result').innerHTML = "<span style='font-size:12px;color:blue;'>Przesyłanie zakończone, zaczekaj na przedładowanie okna...</span>";
    window.location.href=window.location.href;
  }
  else document.getElementById('result').innerHTML = success;

  hide_flyelement('upload_process');
  return true;
}

function advertiseFoto(id, number) {
  var url="/ajax_operation.php";
  url=url+"?id="+id+"&op=advertise_foto";  
  InnerHtmlAjaxTask("foto_"+number, url);
}

function businessLogo(id) {
  var url="/ajax_operation.php";
  url=url+"?id="+id+"&op=business_logo";  
  InnerHtmlAjaxTask("business_logo", url);
}

function changeContent(id, gdzie, w, h, path){ 
  var x=1;
  var div = document.getElementById(gdzie);
  div.innerHTML = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" width="'+w+'" height="'+h+' id="flvplayer"><param name="movie" value="/flvplayer.swf?link='+path+'" /><param name="quality" value="high" /><param name="allowFullScreen" value="true" /><param name="wmode" value="transparent" /><param name="bgcolor" value="#000000" /><param name="menu" value="0" /><param name="allowScriptAccess" value="*" /><embed src="/flvplayer.swf?link='+path+'" width="'+w+'" height="'+h+'" quality="high" bgcolor="#000000" allowScriptAccess="*" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowfullscreen="true" menu="0" wmode="transparent" name="flvplayer"></embed></object>';

  var url="/ajax_operation.php";
  url=url+"?id="+id+"&op=movie_description";  
  InnerHtmlAjaxTask("moviedescription", url);
}

function showAdvertiseFoto(id) {  
  document.getElementById('show_advertise_foto').innerHTML='';
  var url="/ajax_operation.php";
  url=url+"?id="+id+"&op=show_advertise_foto";  
  InnerHtmlAjaxTask("show_advertise_foto", url);
}

function splitComments(id_content, current) {
  var url="/ajax_operation.php";
  url=url+"?id_content="+id_content+"&current="+current+"&op=split_comments"; 
  InnerHtmlAjaxTask("comments", url);  
}

//-->

function key_enter(e)
{
  var keycode;
  if (window.event) keycode = window.event.keyCode;
  else if (e) keycode = e.which;
  else return false;
  if (keycode == 13)
  {
    return true;
  }
  else return false;
}
function UpdateField(e)
{
  if (key_enter(e)==true) {
    document.basket_form.submit();
  }
}
