
// Wizard settings
var wizardVersion = "1.1.4.0";
var prefix = 'http://v7fsp';
var errorClassName = "wizerror";
var notInstalledClassName = "wiz";
var installedClassName = "wizinstalled";
var installVisible = true;

var wizardRules = 
{
	'a.wiz' : function(e)
	{
		if(auth_wizardEnabled() && getWizard())
		{		
			e.onclick = function()
			{
				if(!checkRequirements(this)) return;
		 		installWizardItem(this);
			}	
			// is it installed?
			var fileParams = e.getAttribute("wizard_params");
			var title = e.getAttribute("wizard_title");
			var typeId = e.getAttribute("wizard_type");
			var fileSize = e.getAttribute("wizard_filesize");
			var itemId = e.getAttribute("wizard_itemid");
			isInstalled(1, itemId, fileSize, typeId, title, fileParams, 1);
			e.style.display = "block";
		}
	}
}
Behaviour.register(wizardRules);

var allChecked = false;
function selectAllWizardItems()
{
	allChecked = !allChecked;
 	var items = $('bwin').getElementsByTagName("input");
 	for(var i=0;i<items.length;i++)
 	{
 	 	if(items[i].name=="itemId[]")
 	 		items[i].checked = allChecked;
	}
}

function installWizardItem(item)
{
	var fileParams = item.getAttribute("wizard_params");
	var title = item.getAttribute("wizard_title");
	var typeId = item.getAttribute("wizard_type");
	var fileSize = item.getAttribute("wizard_filesize");
	var itemId = item.getAttribute("wizard_itemid");
	installWithWizard(1, itemId, fileSize, typeId, title, fileParams);
}

var cancelInstall = false;

function cancelInstallAllBasketItems()
{
	cancelInstall = true;
}

function checkBasketItems()
{
	if(!auth_wizardEnabled()) return;
	var allItems = $('items').getElementsByTagName("tr");
	if(!getWizard())
	{
	 	//alert('Wizard can not be used in your browser.');
	 	return false;
	}
	var itemsToInstall = new Array();
	var item = null;
	var allOk = true;
	var messages = "";
	var count = 1;
	for(var i=0;item=allItems[i];i++)
	{
		if(item.className=="basketitem")
		{
			var i_fileParams = item.getAttribute("wizard_params");
			var i_title = item.getAttribute("wizard_title");
			var i_typeId = item.getAttribute("wizard_type");
			var i_fileSize = item.getAttribute("wizard_filesize");
			var i_itemId = item.getAttribute("wizard_itemid");		
			itemsToInstall.push(
			{
				fileParams : i_fileParams,
				title : i_title,
				typeId : i_typeId,
				fileSize : i_fileSize,
				itemId : i_itemId,
				tag : item
			});
		}
	}
	for(var i=0;item=itemsToInstall[i];i++)
	{
		try
		{ 				                                
			if(checkIsInstalled(item.itemId, item.fileSize, item.typeId, item.title, item.fileParams))
			{
			 	$('wizButton_' + item.itemId).className = installedClassName;
			}
		}
		catch(e)
		{
		}
	}
}

function checkIsInstalled(itemID, filesize, type, header, params)
{
	var wiz = getWizard();
	if(!wiz)
	{
        return;
    }
    try
    {
        //var archive = '/download/get.php?id=' + itemID + '&wizard=1&wid=' + getSessionVar("TsrID");
        var archive = '/' + itemID + '.zip?wizard=1&wid=' + getSessionVar("MemberID");
        wiz.setData(prefix,archive,filesize,itemID,type,header,params);
    }
    catch(e)
    {
        return(false);
    }
    if(!isConfigured(wiz,true))
    {
        return(false);
    }
    try
    { 
        var r = wiz.IsInstalled; 
        return(r==1);
	}
	catch(e)
	{
	
	} 
}

function installAllBasketItems()
{
	var allItems = $('items').getElementsByTagName("tr");
	if(!getWizard())
	{
	 	alert('Wizard can not be used in your browser.');
	 	return false;
	}
	var itemsToInstall = new Array();
	var item = null;
	var allOk = true;
	var messages = "";
	var count = 1;
	for(var i=0;item=allItems[i];i++)
	{
		if(item.className=="basketitem")
		{
			var i_fileParams = item.getAttribute("wizard_params");
			var i_title = item.getAttribute("wizard_title");
			var i_typeId = item.getAttribute("wizard_type");
			var i_fileSize = item.getAttribute("wizard_filesize");
			var i_itemId = item.getAttribute("wizard_itemid");		
			itemsToInstall.push(
			{
				fileParams : i_fileParams,
				title : i_title,
				typeId : i_typeId,
				fileSize : i_fileSize,
				itemId : i_itemId
			});
		}
	}
	LightBox.Show("<a href=\"javascript:void(0);\" class=\"oclb\" onclick=\"LightBox.Close()\" title=\"Close this\">&nbsp;</a><p>Installing item <span class=\"installCounter\">1</span> of " + itemsToInstall.length + "</p><div style=\"height:20px;width:300px;border:1px solid black;\"><div style=\"background:red;height:20px;width:0px;\"></div></div>");
	for(var i=0;item=itemsToInstall[i];i++)
	{
		try
		{ 				
			if(cancelInstall)                        
				throw "Installation aborted by user.";
			$('lightboxcontent').getElementsByTagName('span')[0].innerHTML = count;
			installItemBgBasket(1, item.itemId, item.fileSize, item.typeId, item.title, item.fileParams);
			$('lightboxcontent').getElementsByTagName('div')[1].style.width = ((count / itemsToInstall.length) * 100) + '%';
			count++;
		}
		catch(e)
		{
			alert(e);
			allOk = false;
			messages = messages + '<br/>' + e;
		}
	}
	if(allOk)
	{
		LightBox.Show("<a href=\"javascript:void(0);\" class=\"oclb\" onclick=\"LightBox.Close()\" title=\"Close this\">&nbsp;</a>All items installed without problems.");
		setTimeout(function() { $('basket').onclick() }, 3000);
	}
	else
	{
		LightBox.Show("<a href=\"javascript:void(0);\" class=\"oclb\" onclick=\"LightBox.Close()\" title=\"Close this\">&nbsp;</a>Install all failed<br /><br />Message:<br />" + messages);
	}	
}

/**
 *
 * Installs a item in full display mode. Used on item/details and in browse mode.
 *
 */
function installWithWizard(typeID, itemID, filesize, type, header, params)
{
	var wiz = getWizard();
	if(!wiz)
	{
        try
        {
            wizError();
        }
        catch(e)
        {
            ;
        }
        return;
    }
    try
    {
        //var archive = '/download/get.php?id=' + itemID + '&wizard=1&wid=' + getSessionVar("TsrID");
        var archive = '/' + itemID + '.zip?wizard=1&wid=' + getSessionVar("MemberID");
        wiz.setData(prefix,archive,filesize,itemID,type,header,params);
        if(!isConfigured(wiz,true))
        {
            $('wizButton_' + itemID).className = errorClassName;
            $('wizButton_' + itemID).title = 'Wizard not configured';        
            return;
        }
        if(wiz.IsInstalled)
        {
            if(!confirm('Item already installed, do you want to UnInstall this item?'))
            {
                return;
            }
		}
        var iRes = installVisible ? wiz.installVisible() : wiz.install();
		if(iRes == 1)
		{
			if(!wiz.IsInstalled)
			{
				classNameStr = notInstalledClassName;
				titleStr = 'Install this file with the TSR Wizard';
			}
			else
			{
				classNameStr = installedClassName;
				titleStr = 'Uninstall this file with the TSR Wizard';
			}
		}
		else
		{
			classNameStr = errorClassName;
			titleStr = '';
		}
		if($('wizButton_' + itemID))
		{
			$('wizButton_' + itemID).className = classNameStr;
			$('wizButton_' + itemID).title = titleStr;    
		}
    }
    catch(e)
    {
        alert('An error occurred when setting up wizard.\n\n' + e.message);
        return;
    }
}

// Call the wizard to install an item in bg mode
function installItemBgBasket(typeID,itemID,filesize,type,header,params,doUninstall,catchException)
{    
    var wiz = getWizard();
    if(!wiz)
    {
        try
        {
            wizError();
		}
		catch(e)
		{
            if(catchException)
                throw("wizerror");
        }
        return;
    }
    //var archive = '/download/get.php?id=' + itemID + '&wizard=1&wid=' + getSessionVar("TsrID");
    var archive = '/' + itemID + '.zip?wizard=1&wid=' + getSessionVar("MemberID");
    wiz.setData(prefix, archive, filesize, itemID, type, header, params);
    if(!isConfigured(wiz,true))
    {
        throw "Wizard not configured error";
    }
    if(doUninstall)
    {
        window.status = 'UnInstalling item ' + header;
        if(wiz.IsInstalled)
        {
            if(!confirm('Item already installed, do you want to UnInstall this item?'))
            {
                return;
            }
        }        
    }
    if(!wiz.IsInstalled || doUninstall)
    {
        window.status = 'Installing item ' + header;
        var iRes = wiz.install();
        if(iRes == 1)
        {
            if(!wiz.IsInstalled)
                throw "Item " + header + " not installed";
        }
        else
			throw "Item " + header + " not installed";
    }
}

// Call the wizard to check if an item is installed
function isInstalled(typeID, itemID, filesize, type, header, params, iw)
{
	var wiz = getWizard();
	if(!wiz)
	{
        return;
    }
    try
    {
        //var archive = '/download/get.php?id=' + itemID + '&wizard=1&wid=' + getSessionVar("TsrID");
        var archive = '/' + itemID + '.zip?wizard=1&wid=' + getSessionVar("MemberID");
        wiz.setData(prefix,archive,filesize,itemID,type,header,params);
    }
    catch(e)
    {
        return(false);
    }
    if(!isConfigured(wiz,true))
    {
        if(iw==1)
        {
            $('wizButton_' + itemID).className = errorClassName;
            $('wizButton_' + itemID).title = 'Wizard not configured.';        
        }
        else if(iw==2)
        {
            $("wbasket_" + itemID).className = 'wizarddisabled';
            $("basket_" + itemID).title = 'Wizard not configured.';        
            $('wizButton_' + itemID).title = 'Wizard not configured.';                    
        }             
        return(false);
    }
    try
    { 
        var r = wiz.IsInstalled;
        if(r==1)
        {
            if(iw==1)
            {
                $('wizButton_' + itemID).className = installedClassName;
                $('wizButton_' + itemID).title = 'Uninstall this file with the TSR Wizard';        
			}
			else if(iw==2)
			{
                $("basket_" + itemID).className = 'installed';
                $('wizButton_' + itemID).title = 'Uninstall this file with the TSR Wizard';        
            }
		}
		else
		{
            if(iw==1)
            {
                $('wizButton_' + itemID).className = notInstalledClassName;
                $('wizButton_' + itemID).title = 'Install this file with the TSR Wizard';        
            }
            else if(iw==2)
            {
                $("basket_" + itemID).className = 'notinstalled';
                $('wizButton_' + itemID).title = 'Install this file with the TSR Wizard';        
            }
        }
    }
    catch(e)
    {
        //alert(e);
    }
    return r;
}

// Wizard init stuff
var wiz = null;
var noWizardWarning = false;
function getWizard()
{
    if(wiz == null)
    {
        try
        {
            if(window.ActiveXObject)
            {          
            	try
            	{ 
			    	wiz = new ActiveXObject("TSRFileMangerControl.TSRFileManagerXControl");		
				}
				catch(e)
				{
					if($('activexwarning_ie'))
             			$('activexwarning_ie').style.display = "block";
				}
            }
            else if(!noWizardWarning)
            {
            	if($('activexwarning'))
             		$('activexwarning').style.display = "block"; //('ActiveX controls are not available in your browser. The wizard is only supported in Internet Exporer.');
             	noWizardWarning = true;
			}
        }
        catch(e)
        {
            window.status = e.message;
            wiz = null;    
        }        
        if(!wiz) return null;
    }
    if(wiz === 2) return false;
    return wiz;
}

var tryConfigure = true;
var wizardConfigured = false;
function isConfigured(wiz,forceConfigure)
{
    if(!tryConfigure) return(false);
    if(tryConfigure && wizardConfigured)
    {
        return(true);
    }
    else if( (wiz!=null && tryConfigure && !wizardConfigured) || (wiz!=null && forceConfigure) )
    {
        wizardConfigured = (wiz.configured==1)?true:false;
        tryConfigure = wizardConfigured;
        return(wizardConfigured);
    }
    return(false);
}

function wizError()
{
    try
    {
        var wiz = new ActiveXObject("TSRFileMangerControl.TSRFileManagerXControl");
    }
    catch(e)
    {
        if(navigator.appName.match("Internet Explorer"))
        {
            if(confirm("Can not start wizard, is it installed?\n\nDo you want to download and install it now?"))
            {
                document.location.href = "/wizarddownload.php";
            }
            throw new Exception("Err.");
            return;
        }
    }
	if(noWizardWarning)
	{
		alert('The wizard can not be be used in this webbrowser. The wizard is only supported in Internet Explorer.');
		return;
	}
    if(confirm("You have not enabled the Installation Wizard.\n\nDo you want to enable it now?"))
    {
        turnOnWizard();
    }
    throw("nowizard");
}

function disableWizard()
{
 	new Ajax.Request("/account/change-wizard",
 	{
 		postBody : "&DisableWizard=1",
 		onSuccess : function()
 		{
 		 	$('activexwarning').style.display = "none";
		},
		onFailure : function(e)
		{
		 	alert(e.responseText);
		}
	});
}