//Configurazione browser InfocarWeb 2
var domain = 'infocarweb2.org';
var protocol = 'http';
var website = 'www.infocarweb2.org';
var port = ':80';



//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_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 == 'infocarweb2.org')
	{
		shell.RegWrite(L_REG_HKCU_DOMAINS + 'infocarweb2.it' + "\\" + protocol, 2, "REG_DWORD");
		shell.RegWrite(L_REG_HKCU_POPUP + 'www.infocarweb2.it', 0 ,"REG_BINARY");
		shell.RegWrite(L_REG_HKCU_DOMAINS + 'infocarweb2.com' + "\\" + protocol, 2, "REG_DWORD");
		shell.RegWrite(L_REG_HKCU_POPUP + 'www.infocarweb2.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*$/, '') + "\\";
		}
		
		//preferenze utente
		var userPrefs = dir + "user.js";

		var canWriteFFConfig = false;
		var canWriteFFConfigIW2Mkt = 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("//IW2 Configuration") == -1)
			{
				canWriteFFConfig = true;
				canWriteFFConfigIW2Mkt = true;
				f = fso.OpenTextFile(userPrefs, 8, true);
			}
			else
				if (existing.indexOf("//IW2M Configuration") == -1)
				{
					canWriteFFConfigIW2Mkt = true;
					f = fso.OpenTextFile(userPrefs, 8, true);
				}
		}
		else
		{
			f = fso.CreateTextFile(userPrefs, true);
			canWriteFFConfig = true;
			canWriteFFConfigIW2Mkt = true;
		}
    
		if (canWriteFFConfig)
		{
			f.WriteLine("");
			f.WriteLine("//IW2 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("//IW2 Configuration end");
		}
		if (canWriteFFConfigIW2Mkt)
		{
			if (port == ':80')
				port = '';
			f.WriteLine("");
			f.WriteLine("//IW2M 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("//IW2M Configuration end");
		}
		if (canWriteFFConfig || canWriteFFConfigIW2Mkt)
			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("#IW2 Configuration start"); 
			f.WriteLine("host\tpopup\t1\t" + website); 
			f.WriteLine("#IW2 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;

