/*Zmienne globalne*/
var mies_tab = new Array("Styczeń","Luty","Marzec","Kwiecień","Maj","Czerwiec","Lipiec","Sierpień","Wrzesień","Październik","Listopad","Grudzień");
var tabelaHead="<table border=0 class=tbw>\n";
var ret=tabelaHead;
var wsk=false;
var wsk_a=null;
var formatDaty = "Y-m-d";
var dys_tab=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var teraz=new Date();
var zatwierdz=new Date();
var mies=teraz.getMonth();
var rok=teraz.getYear();
var counter=mies;

var v_ofs = 0;

function conf() {
		return confirm("Czy usunąć ten wpis ?");
	}

/*function oW(obj, width, height) { 
  var feature = "dialogWidth:"+width+"px;dialogHeight:"+height+"px;dialogTop:0;dialogLeft:0"
      feature+= ";edge:sunken;help:no;status:no;scroll:yes;resizable:yes;"
      //feature+= ";help:no;status:no;scroll:no;resizable:yes;";
  var edit = {};
      edit.options = "history=no;source=yes";
      edit.styledata=document.styleSheets;
  htmlarg = showModelessDialog(obj.href, edit, feature);
  //if(htmlarg == null) return true;
  return false;
}*/

 function oW(myLink, width, height)
  {
	var windowName= 'pop';
  if(! window.focus)return;
  var myWin=window.open("",windowName,"left=0, top=0, height="+height+",width="+width+",dependent=yes,resizable=yes,scrollbars=yes");
  myWin.focus();
  myLink.target=windowName;
  }

function rysuj(target)
{
	teraz=new Date();
	rok=teraz.getYear();
	wsk_a = target;
	if ((wsk_a != null)&&(wsk_a))
 	{
     	var obj = document.all['kalendarz'];
   	obj.style.left = document.body.scrollLeft+event.clientX;
           obj.style.top  = document.body.scrollTop+event.clientY + v_ofs;
   	obj.style.visibility = "visible";
          var r,m;
          r = podajRok(wsk_a.value);
          m = podajMiesiac(wsk_a.value);
	      if ((m != null) && (r != null)) { mies = m; rok = r;}
          ustaw(rok,mies);
        }
	
	ukryjSE(document.body.scrollLeft+event.clientX, document.body.scrollTop+event.clientY + v_ofs);


}


/*konfiguracja*/
function klik(log)
{
	counter++;
 	if(log == 1)
 	{
 		mies++;
	 	if(mies == 12)
	 	{
	 		mies=0;rok=rok+1;
	 	}
	}
   else
   {
   		mies--;
    	if(mies == -1) {mies=11;rok=rok-1;}
 	}

 ustaw(rok,mies);
}

function wstaw(param)
{
	var arr   = param.split("|");
	var rok  = arr[0];
	var month = arr[1];
	var data  = arr[2];
	var ptr = parseInt(data);
 	zatwierdz.setDate(ptr);
 	if ((wsk_a != null)&&(wsk_a))
 	{
  		wsk_a.value = formatuj(rok,month,data);
  		ukryj();
   }
}


function wszystkie()
{
	wsk_a.value='wszystkie';
	ukryj();

}



function ustaw(rok,month)
{
	if (rok  == null)
 	{
  		rok = _okrok(teraz);
 	}
 	if (month == null)
 	{
  		month = teraz.getMonth();
 	}
 	if (month == 1)
 	{
 		dys_tab[1]  = (przestepny(rok)) ? 29 : 28;
 	}
 	zatwierdz.setYear(rok);
 	zatwierdz.setMonth(month);
	zatwierdz.setDate(1);
 	przeladuj();
}

function przeladuj()
{
 	przelicz();
        document.all['dni'].innerHTML = ret;
 	ret = tabelaHead;
}


function przelicz()
{
	var rok  = _okrok(zatwierdz);
	var month = zatwierdz.getMonth();
	var data  = 1;
	var dzisiaj = teraz.getDay();
	var day   = (zatwierdz.getDay()-1);
	var len   = dys_tab[month];
	var bgr,cnt,tmp = "";
	var j,i;
        ret += "<tr><td colspan=1><div align=center>"+"<a href='javascript:klik(0)'><img border=0 src=\"images/arowleft.gif\" width=11 height=11></a></td>"+"<td colspan=5 class=a2b>"+rok+" "+mies_tab[mies]+"</td>"
	       +"<td colspan=1><a href='javascript:klik(1)'><img border=0 src=\"images/arow.gif\" width=11 height=11></a>"+"</div></td></tr>";
   ret  += "<tr><td colspan=7><tr align=center class=a1 ><td width='20'>Pon</td><td width='20'>Wt</td><td width='20'>Sr</td><td width='20'>Czw</td><td width='20'>Pt</td><td width='20'>Sb</td><td width='20'>Nd</td></tr></td></tr>";
  for (j = 0; j < 7; j++)
 {
  	if (data > len) {break;}
  	for (i = 0; i < 7; i++)
  	{
        if(day==-1) {day=6;}
   	bgr = (i == 6 || i == 5) ? "#EAFAFD" : "#FFFFFF";

   	if ( ((j == 0) && (i < day)) ||(data > len))
   	{
          tmp  += generuj(bgr,rok,month,0);
   	}
        else
        {
          tmp  += generuj(bgr,rok,month,data);data++;
        }
    }
   ret += "<tr align=\"center\">\n" + tmp + "</tr>\n";tmp = "";}
   ret += "<tr><td colspan=7 class=a1b><div align=center><a href='javascript:ukryj()' class=u> Zamknij&nbsp;</a></div></td></tr>";
   ret += "</table>\n";
}

function generuj(bgr,rok,month,sdate)
{
	 var param = "\'"+rok+"|"+month+"|"+sdate+"\'";

	 var td1 = "<td class=a1 width=\"20\" bgcolor=\""+bgr+"\" ";
	 var td2 = "</span></td>\n";
	 var evt = "onMouseOver=\"this.style.backgroundColor=\'#FF0000\'\" onMouseOut=\"this.style.backgroundColor=\'"+bgr+"\'\" onMouseUp=\"wstaw("+param+")\" ";
	 var ext = "<span Style=\"cursor: hand\">";
	 var lck = "<span Style=\"cursor: default\">";
	 var lnk = "<a href=\"javascript:wstaw("+param+")\" onMouseOver=\"window.status=\' \';return true;\">";
	 var cellValue = (sdate != 0) ? sdate+"" : "&nbsp;";


	 if ((teraz.getDate() == sdate )&&(teraz.getMonth() == month)&&(_okrok(teraz) == rok))
	 {
	 	 cellValue = "<b><font color='#FF8818'>"+cellValue+"</font></b>";
	 }

//	 if ((teraz.getDate()>sdate )&&(teraz.getMonth() == month)&&(_okrok(teraz) == rok))
//	 {
//	 	 cellValue = "<font color='#CACACA'>"+cellValue+"</font>";
//	 }

	 var cellCode = "";
//	 if (sdate == 0 || (teraz.getDate()>sdate )&&(teraz.getMonth() == month)&&(_okrok(teraz) == rok) )
	 if (sdate == 0 )
	 {
	  	 cellCode = td1+"Style=\"cursor: default\">"+lck+cellValue+td2;
	 }

	 else
	 {
	  	    cellCode = td1+evt+"Style=\"cursor: hand\">"+ext+cellValue+td2;
	 }
	 return cellCode;
 }


function ukryj() {
 document.all['kalendarz'].style.visibility = "hidden";
 mies=teraz.getMonth();
 wsk = false;
 wsk_a = null;
 pokazSE();
 }


function przestepny(rok)
{
 if ((rok%400==0)||((rok%4==0)&&(rok%100!=0)))
 {
 	return true;
 }
 else
 {
 	return false;
	}
}

function _okrok(obj)
 {
 return obj.getYear();
 }

function form_data(data) {
 var reply = true;

  var mode = arr[0];
  var arg  = arr[1];
  var key  = arr[2].charAt(0).toLowerCase();
  if (key != "d") {
   var day = zatwierdz.getDay();
   var orn = isEvenOrOdd(data);
   reply = (mode == "[^]") ? !((day == arg)&&((orn == key)||(key == "a"))) : ((day == arg)&&((orn == key)||(key == "a")));}
  else {reply = (mode == "[^]") ? (data != arg) : (data == arg);}
 return reply;}

function formatuj(rok,month,data)
{
	if (formatDaty == null) {formatDaty = "m/d/Y";}
	var day = zatwierdz.getDay();
 	var crt = "";
 	var str = "";
 	var chars = formatDaty.length;
 	for (var i = 0; i < chars; i++)
 	 {
 		 crt = formatDaty.charAt(i);
  		switch (crt)
  		{
   		case "M": str += mies_tab[month]; break;
   		case "m": str += (month<9) ? ("0"+(++month)) : ++month; break;
   		case "Y": str += rok; break;
   		case "y": str += rok.substring(2); break;
  		   case "d": str += ((formatDaty.indexOf("m")!=-1)&&(data<10)) ? ("0"+data) : data; break;
         default: str += crt;
       }
    }
 return str;
}

function formatuj(rok,month,data)
{
	if (formatDaty == null) {formatDaty = "m/d/Y";}
	var day = zatwierdz.getDay();
 	var crt = "";
 	var str = "";
 	var chars = formatDaty.length;
 	for (var i = 0; i < chars; i++)
 	 {
 		 crt = formatDaty.charAt(i);
  		switch (crt)
  		{
   		case "M": str += mies_tab[month]; break;
   		case "m": str += (month<9) ? ("0"+(++month)) : ++month; break;
   		case "Y": str += rok; break;
   		case "y": str += rok.substring(2); break;
  		   case "d": str += ((formatDaty.indexOf("m")!=-1)&&(data<10)) ? ("0"+data) : data; break;
         default: str += crt;
       }
    }
 return str;
}


function zrobDate(data)
{
  // uwaga !!!! funkcja działa tylko dla formatów liczbowych
  if (formatDaty == null) {formatDaty = "m/d/Y";}
  var crt = "";
  var str = data;
  var _m,_d,_y;
  var chars = formatDaty.length;
  for (var i = 0; i < chars; i++)
  {
     crt = formatDaty.charAt(i);
     switch (crt)
     {
       case "m": _m = str.substr(0,2);str = str.substr(2); break;
       case "Y": _y = str.substr(0,4);str = str.substr(4); break;
       case "y": _y = "20"+str.substr(0,2);str = str.substr(2); break;
       case "d": _d = str.substr(0,2);str = str.substr(2); break;
       default: str = str.substr(1);
     }
  }

  if (_m.charAt(0) == "0") _m = _m.substr(1);
  if (_d.charAt(0) == "0") _d = _d.substr(1);

  _y = parseInt(_y);
  _m = parseInt(_m)-1;
  _d = parseInt(_d);
  if (isNaN(_y) || isNaN(_m) || isNaN(_d))
    return null;
  else
    return new Date(_y,_m,_d)
}

function podajRok(d)
{
  dt = zrobDate(d);
  if (dt !=null)
    return dt.getYear();
  else
    return null;
}

function podajMiesiac(d)
{
  dt = zrobDate(d);
  if (dt !=null)
    return dt.getMonth();
  else
    return null;
}

function podajDzien(d)
{
  dt = zrobDate(d);
  if (dt !=null)
    return dt.getDay();
  else
    return null;
}




function kalendarz_start()
{
	document.writeln("<div id=\"kalendarz\" style=\"overflow:visible;position:absolute; left:0px; top:0px; z-index:7; width:1px; height:77px; visibility: hidden; background-color: #FF0000; #FF0000\";>");
	document.writeln("<div id=\"miesiace\" style=\"overflow:visible;position:absolute; left:0px; top:0px; z-index:9; width:181px; height:27px;\">");
	document.writeln("<div id=\"dni\" style=\"overflow:visible;position:absolute; left:0px; top:0px; z-index:8; width:176px; height:17px; background-color: #FFFFFF; border: 1px none #000000\">&nbsp;</div></div>");
	document.writeln("</div>");
}

function sprawdz()
{
	if (document.forma3.il_pok.value == "" ||
      document.forma3.data_od.value == "rr-mm-dd" ||
    document.forma3.data_do.value == "rr-mm-dd")
	{
		alert('Wpisz poprawne dane');
		return 1;
	}

  sd = sprawdz_date();
  if (sd == 0) return 0;

  if (sd == 2)
	{
		alert('Wpisane daty są nieprawidłowe')
		return 1;
	}
  if (sd == 1)
	{
		alert('Nieprawidłowa data - skalkulowanie ceny jest niemożliwe')
		return 1;
	}

};

function sprawdz_date()
{
	data_od = document.forma3.data_od.value;
	data_do = document.forma3.data_do.value;
	dl1 = data_od.length;
	dl2 = data_do.length;

	i = 0
	d1 = "";d2 = "";
	m1 = "";m2 = "";
	r1 = "";r2 = "";

	while (data_od.charAt(i) != "-")
			{
        if ((r1 == "") && (data_od.charAt(i) == "0")) i++;
				else
				  {
          r1 = r1 + data_od.charAt(i);
					i++;
				  }
			}
	i++;
	while (data_od.charAt(i) != "-")
			{
				if ((m1 == "") && (data_od.charAt(i) == "0")) i++;
				else
				  {
					m1 = m1 + data_od.charAt(i);
					i++;
				  }
			}
	i++
	while (i < dl1 )
			{
        if ((d1 == "") && (data_od.charAt(i) == "0")) i++;
				else
				  {
          d1 = d1 + data_od.charAt(i);
					i++;
				  }
			}

	i = 0;
	while (data_do.charAt(i) != "-")
			{
        if ((r2 == "") && (data_do.charAt(i) == "0")) i++;
				else
				  {
          r2 = r2 + data_do.charAt(i);
					i++;
				  }
			}
	i++;
	while (data_do.charAt(i) != "-")
			{
				if ((m2 == "") && (data_do.charAt(i) == "0")) i++;
				else
				  {
					m2 = m2 + data_do.charAt(i);
					i++;
				  }
			}
	i++
	while (i < dl2 )
			{
        if ((d2 == "") && (data_do.charAt(i) == "0")) i++;
				else
				  {
          d2 = d2 + data_do.charAt(i);
					i++;
				  }
			}

	r1 = parseInt(r1);r1 = 2000 + r1;
	m1 = parseInt(m1);m1 = m1 - 1;
	d1 = parseInt(d1);
	r2 = parseInt(r2);r2 = 2000 + r2;
	m2 = parseInt(m2);m2 = m2 - 1;
	d2 = parseInt(d2);

	pom = new Date()

	data1 = new Date(r1, m1, d1);
	data2 = new Date(r2, m2, d2);

	if (Date.parse(data1) < Date.parse(pom)) return 1;//blad
	if (Date.parse(data1) > Date.parse(data2)) return 2;//blad
	return 0;

};

/**
 * Sets/unsets the pointer and marker in browse mode
 *
 * @param   object    the table row
 * @param   interger  the row number
 * @param   string    the action calling this script (over, out or click)
 * @param   string    the default background color
 * @param   string    the color to use for mouseover
 * @param   string    the color to use for marking a row
 *
 * @return  boolean  whether pointer is set or not
 */
function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
    var theCells = null;

    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3

    // 4. Defines the new color
    // 4.1 Current color is the default one
    if (currentColor == ''
        || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor              = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
        }
    }
    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
             && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
        if (theAction == 'out') {
            newColor              = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor              = (thePointerColor != '')
                                  ? thePointerColor
                                  : theDefaultColor;
            marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
                                  ? true
                                  : null;
        }
    } // end 4

    // 5. Sets the new color...
    if (newColor) {
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            } // end for
        }
        // 5.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

    return true;
} // end of the 'setPointer()' function

var cmOverlap=new Array;

function	ukryjSE(left, top)
{

	var x = left;
	var y = top;
	var w = 300;
	var h = 300;

	var tagName="select";
	for (i = 0; i < document.all.tags(tagName).length; ++i)
	{
		var obj = document.all.tags(tagName)[i];
		if (!obj || !obj.offsetParent)
			continue;

		// check if the object and the subMenu overlap

		var ox = cmGetX (obj);
		var oy = cmGetY (obj);
		var ow = obj.offsetWidth;
		var oh = obj.offsetHeight;

		if (ox > (x + w) || (ox + ow) < x)
			continue;
		if (oy > (y + h) || (oy + oh) < y)
			continue;

		cmOverlap[cmOverlap.length]=obj;
		obj.style.visibility = "hidden";
	}

}

function pokazSE()
{
	var i;
		for (i = 0; i < cmOverlap.length; ++i)
			cmOverlap[i].style.visibility = "";
	cmOverlap = null;
	cmOverlap=new Array;

}
function cmGetX (obj)
{
	var x = 0;
	while (obj.offsetParent)
	{
		x += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return x;
}

function cmGetY (obj)
{
	var y = 0;
	while (obj.offsetParent)
	{
		y += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return y;
}

