var machineid = -666;
var selectedMachine = null;
var machineFilename = null;
var selectedMachineId = null;
var machinefilename = null;
Ext.EPortal = function(){
	var msgCt;
	
	function createBox(t, s){
        return ['<div class="msg">',
                '<div class="x-box-tl"><div class="x-box-tr"><div class="x-box-tc"></div></div></div>',
                '<div class="x-box-ml"><div class="x-box-mr"><div class="x-box-mc"><h3>', t, '</h3>', s, '</div></div></div>',
                '<div class="x-box-bl"><div class="x-box-br"><div class="x-box-bc"></div></div></div>',
                '</div>'].join('');
    }
    
	return {
		msg : function(title, format){
            if(!msgCt){
                msgCt = Ext.DomHelper.insertFirst(document.body, {id:'msg-div'}, true);
            }
            msgCt.alignTo(document, 't-t');
            var s = String.format.apply(String, Array.prototype.slice.call(arguments, 1));
            var m = Ext.DomHelper.append(msgCt, {html:createBox(title, s)}, true);
            m.slideIn('t').pause(1).ghost("t", {remove:true});
        }
	
	};
	
	
	
}();

function selectMachine(machineid,machinefilename)
{
	var oMachine = Ext.get('machine_'+machineid);
	if (oMachine.id!=selectedMachine)
	{
		if ( selectedMachine!=null )
		{
			Ext.get(selectedMachine).addClass('easypack_systems').removeClass('easypack_systems_selected');
			
		} else {

		}
		selectedMachine = 'machine_'+machineid;
		selectedMachineId = machineid;
		machineFilename = machinefilename;
		oMachine.removeClass('easypack_systems');
		oMachine.addClass('easypack_systems_selected');
	}
}
function selectSystemButton(prefLang)
{
    var sBackButton = '<< Back to contact details'
    var sSystemButton = 'Next Step >>'
    var sProblem = 'There was a problem'
    var sErrMsg = 'Unfortunately, we are not able to take you order at this time.'
    var sSelSysMsg = 'Please select your system to continue to the next step'
    
    if (prefLang == 'fr')
    {
        sBackButton = '<< Retour ves vos coordonnées'
        sSystemButton = 'Etape Suivante >>'
        sProblem = 'Un probléme est survenu'
        sErrMsg = 'Malheureusement nous ne sommes pas en meusre de prendre votre commande.'
        sSelSysMsg = 'Séléctionner votre systéme avant de passer à l\'étape suivante.'
    }
    else if (prefLang == 'de')
    {
        sBackButton = '<< Zurück zu Ihrem Profil'
        sSystemButton = 'Weiter >>'
        sProblem = 'Es gab ein Problem'
        sErrMsg = 'Leider können wir Ihre Bestellung zur Zeit nicht bearbeiten.'
        sSelSysMsg = 'Bitte wählen Sie Ihr Verpackungssystem bevor Sie fortfahren.'
    }
    
    var backButton = new Ext.Button({
        el:'back_button'
        , text: sBackButton
        , handler : function(){
            document.location = '/portal/orders/1';
        }
    });
	var systemButton = new Ext.Button({
		el:'select_system'
		, text: sSystemButton
		, handler : function(){
			if ( selectedMachine!=null && machineFilename!=null )
			{
				Ext.Ajax.request({
				   url: '/portal/orders/2/store',
				   success: function(responseObject){
					if ( responseObject.responseText=='{success:false}' )
					{
						Ext.MessageBox.alert(sProblem, sErrMsg)
					}
					else {
						document.location= '/portal/orders/3';	
					}
				},
				   failure: function(){
					Ext.MessageBox.alert(sProblem, sErrMsg)
				},
				   params: { machineid: selectedMachineId,machinefilename:machineFilename }
				});
				
			} else {
				Ext.MessageBox.alert(sProblem, sSelSysMsg);
			}
		}
	});
	backButton.render();
	systemButton.render();
}