/* Provided as-is, no guarantees
 *  Author: Steve Speicher <sspeiche@us.ibm.com>
 *
 * Checks to see if XForms extension is installed, if not, then start
 * the install automatically. This is intended to work for both Firefox/Moz and IE.
 */

var cxfUrls = new Array();
		cxfUrls['adobe'] =  'http://www.adobe.com/products/acrobat/readstep2.html'; //download Acrobat Readera
		
		cxfUrls['ff3winpl']                           =  '/download/firefox/firefox-3.0.19-win-pl.exe';
		cxfUrls['ff3osxpl']                           =  '/download/firefox/firefox-3.0.19-osx-pl.dmg';
		cxfUrls['ff3linuxpl']                         =  '/download/firefox/firefox-3.0.19-linux-pl.tar.bz2';
		cxfUrls['ff3winen']                           =  '/download/firefox/firefox-3.0.19-win-en.exe';
		cxfUrls['ff3osxen']                           =  '/download/firefox/firefox-3.0.19-osx-en.dmg';
		cxfUrls['ff3linuxen']                         =  '/download/firefox/firefox-3.0.19-linux-en.tar.bz2';
		
		cxfUrls['ff3.6winpl']                         =  '/download/firefox/firefox-3.6.23-win-pl.exe';
		cxfUrls['ff3.6osxpl']                         =  '/download/firefox/firefox-3.6.23-osx-pl.dmg';
		cxfUrls['ff3.6linuxpl']                       =  '/download/firefox/firefox-3.6.23-linux-pl.tar.bz2';
		cxfUrls['ff3.6winen']                         =  '/download/firefox/firefox-3.6.23-win-en.exe';
		cxfUrls['ff3.6osxen']                         =  '/download/firefox/firefox-3.6.23-osx-en.dmg';
		cxfUrls['ff3.6linuxen']                       =  '/download/firefox/firefox-3.6.23-linux-en.tar.bz2';

		cxfUrls['ff3onlinepl']                        =  '/xformscheck/FirefoxOnLineFRSE.exe';
		cxfUrls['ff3.6onlinepl']                      =  '/download/firefox/FirefoxOnLineFRSEv2.exe';

		cxfUrls['ff3.6xformspluginwin']               =  'https://addons.mozilla.org/firefox/downloads/latest/824/platform:5/addon-824-latest.xpi?src=dp-btn-primary';
		cxfUrls['ff3.6xformspluginosx']               =  'https://addons.mozilla.org/firefox/downloads/latest/824/platform:3/addon-824-latest.xpi?src=dp-btn-primary';
		cxfUrls['ff3.6xformspluginlinux']             =  'https://addons.mozilla.org/firefox/downloads/latest/824/platform:2/addon-824-latest.xpi?src=dp-btn-primary';

var checkXFormsLang = 'pl'; // mozliwe  : pl en

function checkXForms( lang ) {

	if( lang == 'en' )
	{
		checkXFormsLang = 'en';
	}

	BrowserDetect.init();
	
	if (BrowserDetect.isFirefox() && (BrowserDetect.version == 3 || BrowserDetect.version == 3.6)) {

		
		//czy firefox 3
		if( BrowserDetect.version == 3) {

			if( hasFFXForms()) {
				allOk();
				return;
			} else {
				
				info(messageIncomatibleBrowser());
			}

		}
		else if(BrowserDetect.version == 3.6) {
			
			
			if( hasFFXForms()) {
				allOk();
				return;
			} else {
				messageInstallFF3XForms();
				
			}
			
		}

	} else {
		info(messageIncomatibleBrowser());
	}

	breakline();
	messageSiteRequirements();
	
	// niepozwalam na korzystanie z innych niz Firefox przegladarek
	addLoadEvent(hideMainContent);
}


function hasFFXForms()
{
	return (document.implementation.hasFeature("org.w3c.xforms.dom", "1.0"));
}


function downloadFFLink()
{
	if (BrowserDetect.isLinux()) {
		if( checkXFormsLang == 'pl' ) {
			return cxfUrls["ff3.6linuxpl"];
			
		} else {
			return cxfUrls["ff3.6linuxen"];
		}
	} else if(BrowserDetect.isMac()) {
		if( checkXFormsLang == 'pl' ) {
			return cxfUrls["ff3.6osxpl"];
		} else {
			return cxfUrls["ff3.6osxen"];
		}
	} else if(BrowserDetect.isWindows()) {
		if( checkXFormsLang == 'pl' ) {
			return cxfUrls["ff3.6onlinepl"];
		} else {
			return cxfUrls["ff3.6onlinepl"];
		}

	} else {
		// inny OS ???
		return cxfUrls["ff3.6onlinepl"];
	}

}


/*
 * dobiera najodpowiedniejsza wersje plugina XForms
 */

function downloadXformsLink()
{

	if (BrowserDetect.isLinux())
	{
		return cxfUrls['ff3.6xformspluginlinux'];
	}
	if (BrowserDetect.isMac())
	{
		return cxfUrls['ff3.6xformspluginosx'];
	}
	if (BrowserDetect.isWindows())
	{
		return cxfUrls['ff3.6xformspluginwin'];		
	}

}



function messageInstallFF3XForms()
{
	var msg;
	if( checkXFormsLang == 'en' ) {
		msg =
			'<p class="warning">ATTENTION: This page requires XForms extension.</p>'+
			'<p>In order to adjust your browser to demanded service requirements :'+
			'<ol>'+
			'<li>install extension <a href="'+downloadXformsLink()+'">Mozilla XForms version 0.8.7</a></li>'+
			'<li>if the installation process is interrupted or blocked and a pop-up window appears,  click "<b>Change options...</b>" i "<b>Allow</b>" for installation from current domain, then retry installation process</li>'+
			'<li>when information on installing XForms plugin appears, click "<b>Install</b>"</li>'+
			'<li>once the installation process is completed, click "<b>Re-start Firefox</b>"</li>'+
			'</ol>'+
			'</p>';

	} else {
		msg =
			'<p class="warning">UWAGA: Ta strona wymaga rozszerzenia XForms.</p>'+
			'<p>Aby dostosować przeglądarkę do wymagań serwisu:'+
			'<ol>'+
			'<li>zainstaluj rozszerzenie <a href="'+downloadXformsLink()+'">Mozilla XForms w wersji 0.8.7</a></li>'+
			'<li>jeśli na górze przeglądarki pojawi się informacja o zablokowaniu instalacji, kliknij "<b>Zmień opcje...</b>" i "<b>Zezwól</b>" na instalację z aktualnej domneny, następnie ponów próbę instalacji</li>'+
			'<li>pojawi się informacja o instalacji wtyczki XForms, kliknij "<b>Zainstaluj</b>"</li>'+
			'<li>po zakończonej instalacji kliknij "<b>Uruchom ponownie Firefox</b>"</li>'+
			'</ol>'+
			'</p>';
	}

	info( msg );

}

function messageIncomatibleBrowser()
{

	var msg;
	msg = msg + warning('<p><strong>UWAGA!</strong> System dokumentów OnLine działa wyłącznie w przeglądarce Mozilla Firefox 3.6 z dodatkiem Mozilla XForms 0.8.7</p>');
	if( checkXFormsLang == 'pl' ) {
		msg =
			
			'<p class="warning">Aby korzystać z serwisu powinieneś wykonać poniższą instrukcję:</p>'+
			
			'<p><strong>Użytkownicy systemu Windows</strong>, mogą zainstalować przeglądarkę ' +
			'<a href="'+cxfUrls['ff3.6onlinepl']+'">Firefox - OnLine FRSE</a> ze zintegrowanym pluginem XForms.</p>'
			+
			'<p>Po instalacji na \'Pulpicie\' i w menu \'Start\' zostaną utworzone skróty <br /> <center><img style="margin: 5px;" alt="Ikona Firefox - OnLine FRSE" src="favicon_alt.png" width="64" height="64"/></center> pozwalające uruchomić program \'Firefox - OnLine FRSE\' gotowy do współpracy z naszym systemem.</p>'
			+
			'<p><strong>Użytkownicy systemu MAC OS X lub Linux\'a</strong> mogą:</p>'
			+
			'<ol>'+
			'<li>zainstalować przeglądarkę <a href="'+downloadFFLink()+'" target="out">Firefox</a> w odpowiedniej wersji</li>'+
			'<li>następnie przy użyciu przeglądarki Firefox doinstalować wtyczkę <a href="'+downloadXformsLink()+'" target="out">Mozilla XForms</a></li>'+
			'<li>zrestartować przeglądarkę Firefox i ponownie odwiedzić serwis</li>'+
			
			'</ol>'	;
		
	} else {
		msg =
			'<p>In order to use this service you should do the following: </p>'+
			'<ol>'+
			'<li>install <a href="'+downloadFFLink()+'" target="out">Firefox browser</a></li>'+
			'<li>then, using Firefox browser instal plugin <a href="'+downloadXformsLink()+'" target="out">Mozilla XForms</a></li>'+
			'<li>restart your Firefox browser and once again open the website</li>'+
			'</ol>'+
			'<p>Sorry for any inconvenience this may cause</p>';

		msg = msg + '<p>Za utrudnienia przepraszamy</p>';
	}
	return msg;
}


function messageSiteRequirements()
{
	var msg;
	if( checkXFormsLang == 'en' ) {
		msg = '<p>Service requirements:</p>' +
		'<p>Browser <a href="'+downloadFFLink()+'" target="inne">Firefox</a> version 3.0.* or 3.6.* with extension <a href="' + downloadXformsLink() + '" target="inne">Mozilla XForms</a>. To print out your documents, you will need <a href="'+cxfUrls["adobe"]+'" target="inne">Adobe Reader</a></p>'
	} else {
		msg = '<p>Wymagania serwisu:</p>' +
		'<p>Przeglądarka <a href="'+downloadFFLink()+'" target="inne">Firefox</a> w wersji 3.0.* lub 3.6.* z rozszerzeniem <a href="' + downloadXformsLink() + '" target="inne">Mozilla XForms</a>. Do wydruku dokumentów przydatny będzie <a href="'+cxfUrls["adobe"]+'" target="inne">Adobe Reader</a></p>';
	}
	message( msg );

}


/*function messageIncomatibleBrowser()
{
	var msg;
	if( checkXFormsLang == 'en' ) {
		msg = 'ATTENTION: You are using untypical web browser and, therefore, the service may not work properly.';
	} else {
		msg = 'UWAGA: Używasz nietypowej przeglądarki www w związku z tym serwis może nie działać prawidłowo.';
	}
	info(
			'<p class="warning">'+msg+'</p>'+
			messageIncomatibleBrowser()
	);
}*/
//}}}
//{{{ messageFirefoxVersion
function messageFirefoxVersion()
{
	var msg;
	if( checkXFormsLang == 'en' ) {
		msg = '<p class="warning">ATTENTION: This service requires XForms extension.</p>'+
		'<p>You are using Firefox browser, version '+BrowserDetect.versionFull+'</p>'+
		'<p>XForms extension is not available for this browser version or doesn\'t work properly.</p>'
	} else {
		msg = '<p class="warning">UWAGA: Ten serwis wymaga rozszerzenia XForms.</p>'+
		'<p>Używasz przeglądarki Firefox w wersji <strong>'+BrowserDetect.versionFull+'</strong></p>'+
		'<p class="warning">Rozszerzenie XForms nie jest dostępne dla tej wersji przeglądarki, lub działa na niej nieprawidłowo.</p>';
	}
	info(

	);
}
//}}}


//{{{ breakline
function breakline()
{
	message( '<hr />');

}
//}}}
//{{{ info
function info(msg)
{
	message(
			'<div class="info">'+msg+'</div>'
	);

}
//}}}
//{{{ warning
function warning(msg)
{
	message(
			'<div class="warning">'+msg+'</div>'
	);

}
//}}}
//{{{ message(msg)
function message(msg)
{
	/* Comment out alert() or put conditional around it if you script to not be
     so noisy */
	//alert(msg);

	document.getElementById('xformscheck-content').innerHTML += msg;
}
//}}}

//{{{ messageZainstalujFirefox


//allOk {{{
function allOk()
{
	// ukrywam div-a z komunikatami o braku JS
	document.getElementById('xformscheck-content').innerHTML =
		'Używasz Firefox-a z zainstalowanym rozszerzeniem XForms.';
	document.getElementById('xformscheck').style.display = 'none';
}
//}}}
/* hideMainContent {{{
 * ukrywa glowna tresc serwisu
 *
 */
function hideMainContent()
{
	var maincontent = document.getElementById('maincontent');
	if( maincontent )
	{
		maincontent.style.display='none';
	}
}
//}}}
//{{{ addLoadEvent
/*
 * funkcja dodajaca bezpiecznie nowe zdarzenie do onLoad
 * http://simonwillison.net/2004/May/26/addLoadEvent/
 * addLoadEvent(nameOfSomeFunctionToRunOnPageLoad);
 * addLoadEvent(function() {
 *   // more code to run on page load
 * });
 */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}
//}}}


/* Browser Detect {{{
   Script pobrany ze strony :
   http://www.quirksmode.org/js/detect.html

   Dostepne sa zmienne :
 * Browser name:     BrowserDetect.browser     Firefox
 * Browser version:  BrowserDetect.version     2
 * OS name:          BrowserDetect.OS          Linux
 */
var BrowserDetect = {
		init: function () {
	this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
	this.version = this.searchVersion(navigator.userAgent)
	|| this.searchVersion(navigator.appVersion)
	|| "an unknown version";
	this.versionFull = this.searchVersionFull(navigator.userAgent)
	|| this.searchVersionFull(navigator.appVersion)
	|| "an unknown version";
	this.OS = this.searchString(this.dataOS) || "an unknown OS";

	// alert(  "Przegladarka : "+this.browser+"\n"+
	//        "Wersja : "+this.version+"\n"+
	//         "OS : "+this.OS
	// );
},
searchString: function (data) {
	for (var i=0;i<data.length;i++)	{
		var dataString = data[i].string;
		var dataProp = data[i].prop;
		this.versionSearchString = data[i].versionSearch || data[i].identity;
		if (dataString) {
			if (dataString.indexOf(data[i].subString) != -1)
			{
				return data[i].identity;
			}
		}
		else if (dataProp)
			return data[i].identity;
	}
},
searchVersionFull: function (dataString) {
	var index = dataString.indexOf(this.versionSearchString);
	var ver = '';
	if (index == -1) return;

	else if(this.browser == 'Firefox' )
	{
		var match = /Firefox[\/\s](\d+(\.\d+)+)[a-z]*/.exec(dataString);
	    ver =  match[1];
	    
	}
	else
	{
		ver = dataString.substring(index+this.versionSearchString.length+1);
	}

	//alert(match[1]);

	return ver;
},
searchVersion: function (dataString) {
	return parseFloat(this.searchVersionFull( dataString ));
},

isFirefox: function () {
	if( this.browser == "Firefox" )  { return true; } return false;
},
isMozilla: function () {
	if( this.browser == "Mozilla" )  { return true; } return false;
},
isIE: function () {
	if( this.browser == "Explorer" ) { return true; } return false;
},
isOpera: function () {
	if( this.browser == "Opera" )    { return true; } return false;
},
isSafari: function () {
	if( this.browser == "Safari" )   { return true; } return false;
},
isWindows: function () {
	if( this.OS == "Windows" )   { return true; } return false;
},
isLinux: function () {
	if( this.OS == "Linux" )   { return true; } return false;
},
isMac: function () {
	if( this.OS == "Mac" )   { return true; } return false;
},

dataBrowser: [
              { 	string: navigator.userAgent,
            	  subString: "OmniWeb",
            	  versionSearch: "OmniWeb/",
            	  identity: "OmniWeb"
              },
              {
            	  string: navigator.vendor,
            	  subString: "Apple",
            	  identity: "Safari"
              },
              {
            	  prop: window.opera,
            	  identity: "Opera"
              },
              {
            	  string: navigator.vendor,
            	  subString: "iCab",
            	  identity: "iCab"
              },
              {
            	  string: navigator.vendor,
            	  subString: "KDE",
            	  identity: "Konqueror"
              },
              {
            	  string: navigator.userAgent,
            	  subString: "Firefox",
            	  identity: "Firefox"
              },
              {
            	  string: navigator.vendor,
            	  subString: "Camino",
            	  identity: "Camino"
              },
              {		// for newer Netscapes (6+)
            	  string: navigator.userAgent,
            	  subString: "Netscape",
            	  identity: "Netscape"
              },
              {
            	  string: navigator.userAgent,
            	  subString: "MSIE",
            	  identity: "Explorer",
            	  versionSearch: "MSIE"
              },
              {
            	  string: navigator.userAgent,
            	  subString: "Gecko",
            	  identity: "Mozilla",
            	  versionSearch: "rv"
              },
              { 		// for older Netscapes (4-)
            	  string: navigator.userAgent,
            	  subString: "Mozilla",
            	  identity: "Netscape",
            	  versionSearch: "Mozilla"
              }
              ],
              dataOS : [
                        {
                        	string: navigator.platform,
                        	subString: "Win",
                        	identity: "Windows"
                        },
                        {
                        	string: navigator.platform,
                        	subString: "Mac",
                        	identity: "Mac"
                        },
                        {
                        	string: navigator.platform,
                        	subString: "Linux",
                        	identity: "Linux"
                        }
                        ]

};
//BrowserDetect.init();


/* }}} */


