function OpenWindow(theURL,winName,w,h,scroll){
  LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
  TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
  settings ='height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',noresizable'
  window.open(theURL,winName,settings);
}

function OpenFormatWindow(theURL,winName,w,h,scroll){
  LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
  TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
  settings ='height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',';
  pop = window.open(theURL,winName,settings);
  pop.document.write('<html><head><title>GALERIA</title>')
  pop.document.write('</head><body bgcolor="white" topmargin="0" leftmargin="0"><center>');
  pop.document.write('<a href="javascript:window.close();"><img src="'+theURL+'" border="0"></a></center>');
  pop.document.write('</body></html>');
}

function textCounter(field, countfield, maxlimit){
  if (field.value.length > maxlimit)
   field.value = field.value.substring(0, maxlimit);
  else
   countfield.value = maxlimit - field.value.length;
}

function trim(t){
  return t.replace(/^\s+/g,'').replace(/\s+$/g,'')
}

function sprawdz(wart){
  if (isNaN(wart.value) || trim(wart.value)=="" || trim(wart.value)<=0)
  {
    alert("Uwaga! \r\n"+wart.value+" nie jest dozwolon wartoci.");
    wart.value="";
  }
  return true;
}

function selecturl(s){
  var gourl = s.options[s.selectedIndex].value;	
  window.self.location.href = gourl;
}

//dodane dla potrzeb Komisu
function delOfe(x){
	if (confirm("Czy na pewno usunąć ofertę?")) {
       makeRequest('config/akcja.php',x,'d','del');
    } else {
       return false;
    }
}

function panShow(x){
	dis = document.getElementById(x).style.display;
	if(dis == 'block')
		document.getElementById(x).style.display='none';
	else
		document.getElementById(x).style.display='block';	
}

function sprPowt(){
	wartosc = document.getElementById('wartosc').value;
	tabObj = document.getElementById('exists').value.split(', ');
	for(i=0;i<tabObj.length;i++){
		if(tabObj[i].toLowerCase().replace(' ','')==wartosc.toLowerCase().replace(' ','')){
			alert(wartosc + ' istnieje ju¿ w bazie');
			return false;
		}
	}
}

//dodane przez Mateusz'a dla potrzeb castmarketu
function showPgP(typ){
  id = document.getElementById("subgroup").value;
  //alert(id);
  ajaxShowInDiv(typ,id);
}

function doConfirm(url){
  if(confirm("Czy na pewno usunąć ten element oraz podrzędne?"))
    window.location = url;
}

function doConfirm_2(id,xxx,katalog,rodzaj){
  if(confirm("Czy na pewno usunąć odznaczony element?"))
      ajaxFill(5,id,katalog,rodzaj);
}

function doConfirmEasy(url){
  if(confirm("Czy na pewno wykonać działanie?"))
    window.location = url;
}

function confirmLink_Text(url,txt){
  if(confirm(txt))
    window.location = url;
}

// PANEL - przekierowanie
function gotolink(where) {
  location.href = where;
}



// Przekierowanie do podstrony o produkcie
function Info(id) {
	window.location = "view,"+id;
}

// Ukrywanie okna informacji skróconych
function hide_Info(id) {
		var d = document.getElementById('i'+id);
		var olddiv = document.getElementById('si'+id);
		if(olddiv!=null)
			d.removeChild(olddiv);
}

function GetKeyCode(event)
{
  var code;
  if (event.keyCode) 
  {
    code = event.keyCode;
  } 
  else if (event.which) 
  {
    code = event.which;
  }
  return code;
}

function FormatOO(zm){
  if(zm.length==0) zm = '0';
  zm = zm + '';
  var tab = zm.split(".");
  if(tab.length==1){
    zm = zm+'.00';
  }else{
    if(tab[1].length==1) zm = zm+'0';
  }
  //zaokraglanie 2 zn po przecinku
  var tab = zm.split(".");
  if(tab[1].length>2){
    var fo = tab[1].substr(0,2);
    var so = tab[1].substr(2);
    var foso = fo+"."+so;
    var foso2 = Math.round(foso*1);
    zm = tab[0] + "." + foso2;
  }  
  return zm;
}

function addValues(id){
  if(id>0){
    var cena = document.getElementById('cena_'+id).innerHTML*1;
    var waga = document.getElementById('waga_'+id).innerHTML*1;
    var szt = document.getElementById('szt_'+id).value;
    if(!isNaN(szt)){
      document.getElementById('pcena_'+id).innerHTML = FormatOO(szt*cena);
      document.getElementById('pwaga_'+id).value = FormatOO(szt*waga);
    addValues(-1);
    }
  }else{
    var sumaCena = 0;
    var sumaWaga = 0;
    var par = document.getElementById('koszyksum').elements;
    for(i=0;i<par.length;i++){
      if(par[i].type!="hidden" && !isNaN(par[i].value)){
        var getid = par[i].id.split("_");
        sumaCena = sumaCena + document.getElementById('pcena_'+getid[1]).innerHTML*1;
        sumaWaga = sumaWaga + document.getElementById('pwaga_'+getid[1]).value*1;
      }
    }
    document.getElementById('tot_cena').innerHTML = FormatOO(sumaCena);
    document.getElementById('tot_waga').innerHTML = FormatOO(sumaWaga);
    document.getElementById('intot_cena').value = FormatOO(sumaCena);
    document.getElementById('intot_waga').value = FormatOO(sumaWaga);
  }
}

function showDiv(id,div) {
  var DivIn = document.getElementById(div);
  var tabDivIn = DivIn.getElementsByTagName("DIV");
  var idDivIn = 'form_'+id;
  for(i=0;i<tabDivIn.length;i++){
    if(tabDivIn[i].id == idDivIn)
      tabDivIn[i].style.display = "block";
    else
      if(tabDivIn[i].parentNode.id == div)
        tabDivIn[i].style.display = "none";
  }
}

function showHist(id,div) {
  var DivIn = document.getElementById(div);
  var idDivIn = document.getElementById('form_'+id);

  var styleattribute = idDivIn.getAttribute('style');
  if (typeof styleattribute == 'string') { // what Firefox returns
    if(styleattribute == 'display: none;')
      idDivIn.style.display = "block";
    else
      idDivIn.style.display = "none";
  } else if (typeof styleattribute == 'object') { // what IE returns
      styleattribute = styleattribute.cssText;
      if(styleattribute == 'DISPLAY: none')
        idDivIn.style.display = "block";
      else
        idDivIn.style.display = "none";
  }
}

function showDivCheck(id, idToShow, optId) {
  if(document.getElementById(id).checked == true){
    document.getElementById(idToShow).style.display = "block";
    if(optId) document.getElementById(optId).style.display = "none";
  }else{
    document.getElementById(idToShow).style.display = "none";
    if(optId) document.getElementById(optId).style.display = "block";
  }
}

function show_photo(val) {
  var gallDiv = document.createElement('div');
  var doc = document.body;
       
  var cont = document.getElementById('container');
  gallDiv.id = 'f_gallery';
  //document.body.appendChild(gallDiv);
  doc.insertBefore(gallDiv, cont);
  
  document.getElementById('f_gallery').style.position = 'absolute';
  document.getElementById('f_gallery').style.zIndex = '10000';
  document.getElementById('f_gallery').style.top = '0px';
  document.getElementById('f_gallery').style.width = '100%';
  document.getElementById('f_gallery').style.height = document.body.clientHeight +'px';

  //var so = new SWFObject("includes/f_gallery.swf", "viewers", "100%", "100%", "9", "#181818");
  var so = new SWFObject("includes/mat_gallery.swf", "viewers", "100%", "100%", "9", "#181818");	
  so.addVariable("wmode", "opaque");
  so.addParam("wmode", "transparent");
  so.addParam("salign", "t");  
  so.addVariable("id", val);
  so.write("f_gallery");	
  window.location =  '#top';
}
function hide_photo() {
  var d = document.body;
  var olddiv = document.getElementById('f_gallery');
  d.removeChild(olddiv);
}
function comunikat(suc1,lan1,tre1) {
  var par_success = document.getElementById('panel_pl');
  var success = document.createElement("DIV");
  if(suc1=='1')
    idlogin = 'success';
  else
    idlogin = 'nosuccess';
    
  success.id = idlogin;
  success.innerHTML = '<div id="insuccess">' + tre1 + '</div>';
  par_success.appendChild(success);    
  setTimeout("destroy('"+idlogin+"')", 10000);
}

//sprawdza czy wszystkie radio sa niezaznaczone
function empty_radio(frm,names,com){
  var tabElems = document.getElementById(frm).elements;
  var licz = 0
  for(i=0;i<(tabElems.length);i++){
    if((tabElems[i].name == names) && tabElems[i].checked == true) licz++;
  }
  if(licz==0){
    alert(com);
    return false;
  }
}