//Configurazione browser InfocarWeb 3
var domain = 'quattroruotepro.it';
var protocol = 'http';
var protocols = 'https';
var website = 'iw3.quattroruotepro.it';
var port = ':443';



//INTERNET EXPLORER
var L_REG_HKCU_DOMAINS = "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ZoneMap\\Domains\\";
var L_REG_HKCU_POPUP = "HKCU\\Software\\Microsoft\\Internet Explorer\\New Windows\\Allow\\";
var L_REG_HKCU_TRUSTEDZONE = "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\2\\";

var cookie_all = "1A10";
var cookie_persistent = "1A02";
var cookie_session = "1A03";
var cookie_name_first_party = "1A05"
var cookie_name_third_party = "1A06"
var download_automatic_prompt = "2200"

var shell = null;
try
{
	shell = new ActiveXObject("WScript.Shell");

	shell.RegWrite(L_REG_HKCU_DOMAINS + domain + "\\" + protocol, 2, "REG_DWORD");
	shell.RegWrite(L_REG_HKCU_DOMAINS + domain + "\\" + protocols, 2, "REG_DWORD");
	shell.RegWrite(L_REG_HKCU_POPUP + website, 0 ,"REG_BINARY");
	shell.RegWrite(L_REG_HKCU_TRUSTEDZONE + cookie_all, "00000000" ,"REG_DWORD");
	shell.RegWrite(L_REG_HKCU_TRUSTEDZONE + cookie_persistent, "00000000" ,"REG_DWORD");
	shell.RegWrite(L_REG_HKCU_TRUSTEDZONE + cookie_session, "00000000" ,"REG_DWORD");
	shell.RegWrite(L_REG_HKCU_TRUSTEDZONE + cookie_name_first_party, "00000000" ,"REG_DWORD");
	shell.RegWrite(L_REG_HKCU_TRUSTEDZONE + cookie_name_third_party, "00000000" ,"REG_DWORD"); 
	shell.RegWrite(L_REG_HKCU_TRUSTEDZONE + download_automatic_prompt, "00000000" ,"REG_DWORD"); 

	//INFOCARWEB2.ORG
	if (domain == 'infocarweb3.org')
	{
		shell.RegWrite(L_REG_HKCU_DOMAINS + 'infocarweb3.it' + "\\" + protocol, 2, "REG_DWORD");
		shell.RegWrite(L_REG_HKCU_DOMAINS + 'infocarweb3.it' + "\\" + protocols, 2, "REG_DWORD");
		shell.RegWrite(L_REG_HKCU_POPUP + 'www.infocarweb3.it', 0 ,"REG_BINARY");
		shell.RegWrite(L_REG_HKCU_DOMAINS + 'infocarweb3.com' + "\\" + protocol, 2, "REG_DWORD");
		shell.RegWrite(L_REG_HKCU_DOMAINS + 'infocarweb3.com' + "\\" + protocols, 2, "REG_DWORD");
		shell.RegWrite(L_REG_HKCU_POPUP + 'www.infocarweb3.com', 0 ,"REG_BINARY");
	}

	//FIREFOX
	var sPath = shell.ExpandEnvironmentStrings("%AppData%") + "\\Mozilla\\Firefox\\";

	var fso = new ActiveXObject("Scripting.FileSystemObject");
	if (fso.FolderExists(sPath))
	{
		var f = fso.OpenTextFile(sPath + "profiles.ini", 1);
		var dir = f.ReadAll();
		f.Close();

		//Nota: modifica solamente il primo profilo di FireFox
		var isRelativePath = dir.substring(dir.indexOf("IsRelative") + 11, dir.indexOf("IsRelative") + 12);
		if (isRelativePath == "1")
		{
			dir = dir.substring(dir.indexOf("Path") + 5).replace("/","\\");
			dir = dir.replace(/^\s\s*/, '').replace(/\s\s*$/, '') + "\\";
			dir = sPath + dir;
		}
		else
		{
			dir = dir.substring(dir.indexOf("Path") + 5).replace("/","\\");
			dir = dir.replace(/^\s\s*/, '').replace(/\s\s*$/, '') + "\\";
		}

// MIME TYPE: inizio
	        var mimeTypesFile = dir + "mimeTypes.rdf";
	        if (fso.FileExists(mimeTypesFile))
	        {
	          //backup file
	            var mimeTypesBackUp = dir + "mimeTypes_old.rdf";
	            var fb = fso.CreateTextFile(mimeTypesBackUp, true);
	            fb.Close();	    
	            fso.CopyFile(mimeTypesFile, mimeTypesBackUp, true);
	             
	            //lettura file
	            f = fso.OpenTextFile(mimeTypesFile, 1, true);
	        	var fileMimeBuffer = f.ReadAll();
	        	f.Close();
	        	
	        	// sostituzione
	        	var searchString = "<RDF:Description RDF:about=\"urn:mimetype:handler:application/pdf\"([^>])*/>";
	        	var resultString = "<RDF:Description RDF:about=\"urn:mimetype:handler:application/pdf\" NC:useSystemDefault=\"true\" NC:alwaysAsk=\"false\" />";
	        	fileMimeBuffer = fileMimeBuffer.replace(new RegExp(searchString),resultString);
	        	
	        	f = fso.CreateTextFile(mimeTypesFile, true);
	        	f.write(fileMimeBuffer);
	        	f.close();
	        }
	        // MIME TYPE: fine

		//preferenze utente
		var userPrefs = dir + "user.js";

		var canWriteFFConfig = false;
		var canWriteFFConfigIW3Mkt = false;
 		var embeddedPdfFF19AlreadyExist = true;
		var FileDaAprire = false;

		if (fso.FileExists(userPrefs))
		{
			//backup file
			var userPrefsBackUp = dir + "user_old.js";
			var fb = fso.CreateTextFile(userPrefsBackUp, true);
			fb.Close();	    
			fso.CopyFile(userPrefs, userPrefsBackUp, true);

			//controllo se gia' configurato		    
			f = fso.OpenTextFile(userPrefs, 1, true);
			var existing = '';
			if (!f.AtEndOfStream)
				existing = f.ReadAll();
			f.Close();
 			if (existing.indexOf("//IW3 PDF Download Configuration") == -1 || 
			    existing.indexOf("plugin.disable_full_page_plugin_for_types") == -1)
			{
			    embeddedPdfFF19AlreadyExist = false;
		            FileDaAprire = true;
			}			

			if (existing.indexOf("//IW3 Configuration") == -1)
			{
				canWriteFFConfig = true;
				canWriteFFConfigIW3Mkt = true;
				FileDaAprire = true;
			}
			else
			{
				if (existing.indexOf("//IW3M Configuration") == -1)
				{
					canWriteFFConfigIW3Mkt = true;
					FileDaAprire = true;
				}
			}
			if (FileDaAprire)
			{
			    f = fso.OpenTextFile(userPrefs, 8, true);
			}
		}
		else
		{
			f = fso.CreateTextFile(userPrefs, true);
			canWriteFFConfig = true;
			canWriteFFConfigIW3Mkt = true;
			embeddedPdfFF19AlreadyExist = false;
		}
    
		if (canWriteFFConfig)
		{
			f.WriteLine("");
			f.WriteLine("//IW3 Configuration start");
			f.WriteLine("user_pref(\"dom.allow_scripts_to_close_windows\", true);			//allow close login window");
			f.WriteLine("user_pref(\"dom.disable_window_open_feature.location\", false);	//disable location toolbar on window.open");
			f.WriteLine("user_pref(\"network.cookie.cookieBehavior\", 0);					//enable cookies");
			f.WriteLine("//IW3 Configuration end");
		}

		if (!embeddedPdfFF19AlreadyExist)
		{
		    f.WriteLine("");
			f.WriteLine("//IW3 PDF Download Configuration");
            	    f.WriteLine("user_pref(\"plugin.disable_full_page_plugin_for_types\", \"\");      //apre pdf in browser");
            	    f.WriteLine("//IW3 PDF Download Configuration end");
   	}


    if (canWriteFFConfigIW3Mkt)
		{
			if (port == ':80')
				port = '';
			f.WriteLine("");
			f.WriteLine("//IW3M Configuration start");
			f.WriteLine("user_pref(\"capability.policy.policynames\", \"localfilelinks\");	//add new policy");
			f.WriteLine("user_pref(\"capability.policy.localfilelinks.sites\", \"http://" + website + port + "\");	//add name of site");
			f.WriteLine("user_pref(\"capability.policy.localfilelinks.checkloaduri.enabled\", \"allAccess\");	//add local file access");
			f.WriteLine("//IW3M Configuration end");
		}
		if (canWriteFFConfig || canWriteFFConfigIW3Mkt || !embeddedPdfFF19AlreadyExist)
			f.Close();
		
		var popupPrefs = dir + "hostperm.1";
		canWriteFFConfig = false;
		if (fso.FileExists(popupPrefs))
		{
			//backup file
			var popupPrefsBackUp = dir + "hostperm.1.old";
			var fb = fso.CreateTextFile(popupPrefsBackUp, true);
			fb.Close();	    
			fso.CopyFile(popupPrefs, popupPrefsBackUp, true);

			//controllo se gia' configurato		    
			f = fso.OpenTextFile(popupPrefs, 1, true);
			var existing = f.ReadAll();
			f.Close();

			if (existing.indexOf("host\tpopup\t1\t" + website) == -1)
			{
				canWriteFFConfig = true;
				f = fso.OpenTextFile(popupPrefs, 8, true);
			}
		}
		else
		{
			f = fso.CreateTextFile(popupPrefs, true);
			canWriteFFConfig = true;
		}

		if (canWriteFFConfig)
		{
			f.WriteLine("#IW3 Configuration start"); 
			f.WriteLine("host\tpopup\t1\t" + website); 
			f.WriteLine("#IW3 Configuration end"); 
			f.Close();
		}
		
		f = null;
	}
	fso = null;
	shell.Popup('Configurazione del browser avvenuta con successo');
}
catch (e)
{
	shell.Popup('Errore durante la configurazione del browser: ' + e);
}
shell = null;

