
var ALERT_BUTTON_TEXT = "Ok";
var ALERT_BUTTON_TEXT_OK = "Ok";
var ALERT_BUTTON_TEXT_CANCEL = "Anuluj";

// over-ride the alert method only if this a newer browser.
// Older browser will see standard alerts
/*
if(document.getElementById) {
	window.alert = function(txt) {
		createCustomAlert(txt);
	}
	
}
*/

function createContent(d, obj, txt){
    msg = obj.appendChild(d.createElement("div"));
    msg.id = 'divAlert';
    d.getElementById('divAlert').innerHTML = txt;
}

function showHideCalendars(dis){
    var newC = d.getElementById("newKalendarz");
	var editC = d.getElementById("editKalendarz");
	var contact = d.getElementById("contactSelect");
	if (newC != null){
	   newC.style.display = dis;
	}
	if (editC != null){
	   editC.style.display = dis;
	}
	if (contact != null){
	   contact.style.display = dis;
	}
}

function okAction(form){
    if (form != "") {
	    document.getElementById(form).submit();
	  }
	  showHideCalendars("block");
	  removeCustomConfirm();
	  return true; 
}
function cancelAction(form){
      if (form != "") {
	    document.getElementById(form).submit();
	  } 
	  showHideCalendars("block");
	  removeCustomConfirm();
	  return false;
}

function customConfirm(formOk, formCancel, txt, okB, cancelB, w, h, formLosowo, losowoB) {
    d = document;
	if(d.getElementById("modalContainer1")) return;
	
	showHideCalendars("none");
	mObj = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
	mObj.id = "modalContainer1";
	mObj.style.height = document.documentElement.scrollHeight + "px";
	alertObj = mObj.appendChild(d.createElement("div"));
	alertObj.id = "confirmBox";
	alertObj.style.height = h;
    alertObj.style.width = w;
    if (h == "auto") h = 100;
    if (w == "auto") w = 400;
    var top = ((findHeight()-h)/2) + "px";     
    var left = ((findWidth()-w)/2) + "px";  
    alertObj.style.top = top;
    alertObj.style.left = left;
    
	// create an H1 element as the title bar
	h1 = alertObj.appendChild(d.createElement("h1"));
	h1.appendChild(d.createTextNode(CONFIRM_TITLE));

	createContent(d, alertObj, txt);
	
    var table = "<br /><br /><center><table><tr><td>";
    if( null != formLosowo && null != losowoB ) {
    	table += "<input type='button' id='losowoBtn' onclick='okAction(\"" + formLosowo + "\");' value='" + losowoB + "' /></td><td>";
    }
    table += "<input type='button' id='okBtn' onclick='okAction(\"" + formOk + "\");' value='" + okB + "' />";
    table += "</td><td><input type='button' id='cancelBtn' onclick='cancelAction(\"" + formCancel + "\");' value='" + cancelB + "' />";
    table += "</td></tr></table></center>";
    d.getElementById('divAlert').innerHTML += table;
}


function findHeight(){
   var myHeight = 0;
   if( typeof( window.innerWidth ) == 'number' ) {    //Non-IE     
          myHeight = window.innerHeight;  
   } else if( document.documentElement &&      
           ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {    //IE 6+ in 'standards compliant mode'       
          myHeight = document.documentElement.clientHeight;  
   } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {    //IE 4 compatible       
          myHeight = document.body.clientHeight;  
   }
   return myHeight;
}

function findWidth(){
   var myWidth = 0;
   if( typeof( window.innerWidth ) == 'number' ) {    //Non-IE    
          myWidth = window.innerWidth;    
   } else if( document.documentElement &&      
           ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {    //IE 6+ in 'standards compliant mode'    
          myWidth = document.documentElement.clientWidth;    
   } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {    //IE 4 compatible    
          myWidth = document.body.clientWidth;    
   }
   return myWidth;
}

function createCustomAlert(txt, w, h) {
   createCustomAlert(txt, false, w, h);
}

function createCustomAlert(txt, form, w, h) {
    d = document;
	if(d.getElementById("modalContainer")) return;
	showHideCalendars("none");
	mObj = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
	mObj.id = "modalContainer";
	mObj.style.height = document.documentElement.scrollHeight + "px";
	alertObj = mObj.appendChild(d.createElement("div"));
	alertObj.id = "alertBox";
	alertObj.style.height = h;
    alertObj.style.width = w;
    if (h == "auto") h = 100;
    if (w == "auto") w = 400;
    var top = ((findHeight()-h)/2) + "px";     
    var left = ((findWidth()-w)/2) + "px";  
    alertObj.style.top = top;
    alertObj.style.left = left;
    
	h1 = alertObj.appendChild(d.createElement("h1"));
	h1.appendChild(d.createTextNode(ALERT_TITLE));

	createContent(d, alertObj, txt);

	// create an anchor element to use as the confirmation button.
	btn = alertObj.appendChild(d.createElement("a"));
	btn.id = "closeBtn";
	btn.appendChild(d.createTextNode(ALERT_BUTTON_TEXT));
	btn.href = "#";
	// set up the onclick event to remove the alert when the anchor is clicked
	btn.onclick = function() { if (form) document.getElementById('okForm').submit();showHideCalendars("block");removeCustomAlert();return false; }
}

function showNewPopup(show, title, txt, f, ok) {
	var popUp = $('#popupBackground');
	
	popUp.find('.zamknij a').click(function(event) {
		event.preventDefault();
		popUp.hide();
	});
	popUp.find('#cancelButton').click(function(event) {
		event.preventDefault();
		popUp.hide();
	});
	popUp.find('#okButton').click(function(event) {
		event.preventDefault();
		popUp.hide();
	});
	popUp.find('#submitButton').click(function(event) {
		event.preventDefault();
		$('#' + f).submit();
	});
	
	if (f == null && (ok == null || ok === false)) {
		popUp.find('#submitButton').hide();
		popUp.find('#okButton').hide();
		popUp.find('#cancelButton').show();
		popUp.find('#komok').hide();
	} else if (f == null) {
		popUp.find('#submitButton').hide();
		popUp.find('#okButton').show();
		popUp.find('#cancelButton').hide();
		popUp.find('#komok').hide();
	} else {
		popUp.find('#submitButton').show();
		popUp.find('#okButton').hide();
		popUp.find('#cancelButton').show();
		popUp.find('#komok').show();
	}
	
	popUp.find('.title').html(title);
	popUp.find('#komunikat').html(txt);
	
	if (show) {
		popUp.show();
	} else {
		$('#' + f).submit();
	}
}

jQuery(document).ready(function($) {
	$('a.buySoundPopup').each(function(index, Element) {
		$(this).click(function(event) {
			if (isDuringMigration) {
				event.preventDefault();
				showMigrationPopup();
			} else {
				doWait(this);
			}
		});
	});
});

function createHelpAlert(txt, w, h) {
    d = document;
	if(d.getElementById("modalContainer3")) return;
	showHideCalendars("none");
	mObj = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
	mObj.id = "modalContainer3";
	mObj.style.height = document.documentElement.scrollHeight + "px";
	alertObj = mObj.appendChild(d.createElement("div"));
	alertObj.id = "helpBox";
    alertObj.style.height = h;
    alertObj.style.width = w;
    if (h == "auto") h = 100;
    if (w == "auto") w = 400;
    var top = ((findHeight()-h)/2) + "px";     
    var left = ((findWidth()-w)/2) + "px"; 
    alertObj.style.top = top;
    alertObj.style.left = left;
    
	// create an H1 element as the title bar
	h1 = alertObj.appendChild(d.createElement("h1"));
	h1.appendChild(d.createTextNode("Pomoc"));

	createContent(d, alertObj, txt);

	// create an anchor element to use as the confirmation button.
	btn = alertObj.appendChild(d.createElement("a"));
	btn.id = "closeBtn";
	btn.appendChild(d.createTextNode(ALERT_BUTTON_TEXT));
	btn.href = "#";
	// set up the onclick event to remove the alert when the anchor is clicked
	btn.onclick = function() {removeCustomAlert3();showHideCalendars("block");return false; }
}

function removeCustomAlert3() {
	document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer3"));
}

function removeCustomAlert() {
	document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));
}

function removeCustomConfirm() {
	document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer1"));
}

function removeCustomConfirm1() {
	document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer2"));
}

