function openPopup(url, name, w, h, perc, sc, res) 
{        
	var winX = (screen.availWidth - w)*perc*.01;        
	var winY = (screen.availHeight - h)*perc*.01;
	popupWin = window.open(url, name,'scrollbars='+sc+', resizable='+res+', width=' + w + ', height=' + h + ', left=' + winX + ', top=' + winY);
	popupWin.focus()
}

function openPopup2(url, name, w, h, perc, sc, res) 
{        
	var winX = (screen.availWidth - w)*perc*.01;        
	var winY = (screen.availHeight - h)*perc*.01;        
	popupWin = window.open(url, name,'scrollbars='+sc+',resizable='+res+',width=' + w + ',height=' + h + ',left=' + winX + ',top=' + winY);
	popupWin.focus()
}

function openPopup3(url, name, w, h, perc , sc, res) 
{
	var winX = (screen.availWidth - w)*perc*.01;        
	var winY = (screen.availHeight - h)*perc*.01;        
	popupWin = window.open(url, name, 'menubar=no, toolbar=no, location=no, scrollbars=yes, resizable=no, status=no, '+'width=' + w + ', height=' + h + ', left=' + winX + ', top=' + winY)
	popupWin.focus()
}

function openPopupSC(url, name, w, h, perc, sc) 
{        
	var winX = (screen.availWidth - w)*perc*.01;        
	var winY = (screen.availHeight - h)*perc*.01;
	popupWin = window.open(url, name,'scrollbars=yes, resizable=yes, width=' + (w+17) + ', height=' + h + ', left=' + winX + ', top=' + winY);
	popupWin.focus()
}

function simpleEmailPopup(reciever) 
{

	var name = "simpleEmail";
	var w = 435;
	var h = 675;
	var perc = 50;
	var sc = "yes";
	var res = "no";
	var winX = (screen.availWidth - w)*perc*.01;        
	var winY = (screen.availHeight - h)*perc*.01;        
	var reciever2 = "";
	if(reciever!=""){
		reciever2 = "&reciever=" + reciever;
	}else{
		reiever2 = "";
	}

	var url = "/do/simpleEmail?start=yes" + reciever2;

	popupWin = window.open(url, name, 'menubar=no, toolbar=no, location=no, scrollbars=yes, resizable=no, status=no, '+'width=' + w + ', height=' + h + ', left=' + winX + ', top=' + winY)
	popupWin.focus()
}

function simpleEmailPopup2(reciever, subject) 
{

	var name = "simpleEmail";
	var w = 435;
	var h = 575;
	var perc = 50;
	var sc = "yes";
	var res = "no";
	var winX = (screen.availWidth - w)*perc*.01;        
	var winY = (screen.availHeight - h)*perc*.01;        
	var reciever2 = "";
	

	var url = "/do/simpleEmail?start=yes&reciever="+reciever+"&subject="+subject;

	popupWin = window.open(url, name, 'menubar=no, toolbar=no, location=no, scrollbars=yes, resizable=no, status=no, '+'width=' + w + ', height=' + h + ', left=' + winX + ', top=' + winY)
	popupWin.focus()
}

//funkcja wyswietla ogolny blad przetwarzania danych
function displayGeneralError(msgElemId){
	var message = '<div id="errormsg"><div id="error_bottom"><div id="error_top"><div id="error_tekst" style="padding-top: 40px; padding-bottom: 40px;">'
  			+'Podczas przetwarzania zapytania wystąpił nieoczekiwany błąd, skontaktuj się z obsługą techniczną.'
  			+'</div></div></div></div>';
  	$(msgElemId).update(message);
}

//funkcja usuwa wszystkie opcje poza pierwsza z pola typu select
function removeOptions(selectElem){
	var select = document.getElementById(selectElem);
	var i; 
	for(i = select.options.length - 1; i > 0; i--){
		select.remove(i);
	}
}

// Walidacja daty w formacie YYYY.MM.DD
/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= ".";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strYear=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strDay=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false
	}
return true
}
//Koniec walidacji daty


//Funkcja generuje komunikat informacyjny
//type - typ komunikatu wartosci: error, ok
//msg - komunikat wyswietlany
function createMsg(type, aMsg){

	var msg = document.createElement('div');
	msg.id = 'errormsg';

	var div2 = document.createElement('div');
	div2.id = 'error_bottom';

	var div3 =  document.createElement('div');
	div3.id = 'error_top';
	
	var div4 =  document.createElement('div');
	if(type == 'ok'){
		div4.id = 'error_tekst_ok';
	}else{
		div4.id = 'error_tekst';
	}
	
	//div4.appendChild(document.createElement('br'));
	div4.appendChild(document.createElement('br'));
	div4.appendChild(document.createTextNode(aMsg));
	div4.appendChild(document.createElement('br'));
	div4.appendChild(document.createElement('br'));	
	div3.appendChild(div4);
	div2.appendChild(div3);
	msg.appendChild(div2);
	
	return msg;
}


//funkcja wyswietla lub ukryewa elementy w zaleznosci od ich stanu	
function toggleVisiblity(elem1, elem2){
	if($(elem1)!=null){
		if($(elem1).visible()){
			$(elem1).hide();
		}else
		{
			$(elem1).show();
		}
	}
	if($(elem2)!=null){
		if($(elem2).visible()){
			$(elem2).hide();
		}else
		{
			$(elem2).show();
		}
	}
}

