var wm = new Array();
var wmid = 0;

function throwErrorWindow(what) {

	wm[0] = new windowManager();
	wm[0].createWindow({
		'type':'error',
		'content':lang[what]
	});
}



window.addEvent('domready', function() {
        $$('.m_newsletter2-frontButtonAddMe').each(function(elm){
                elm.addEvent('click',function(e1){
                        var filter  = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
                        if($('signUpEmail').get('value') == ''){
                                throwErrorWindow('noEmail');
                        }
                        else if(!filter.test($('signUpEmail').get('value'))){
                                throwErrorWindow('incorrectEmail');
                        }                         
                        else if($('selectNL') && $('selectNL').selectedIndex == 0){
                                throwErrorWindow('noNLselected');
                        }
                        /*else if(!$('NLcheckAgree').checked){
                                throwErrorWindow('notAgreed');
                        }         */
                        else{
                                var idNL = 0;
                                if($('hiddenIdNL')) idNL = $('hiddenIdNL').get('value');
                                else if($('selectNL')) idNL = $('selectNL').options[$('selectNL').selectedIndex].value;
                                else throwErrorWindow('errorIdNL');

                                var nodeId = $('nodeId').get('value');
                                var request = new Request.JSON({
                                    url : '/id/'+nodeId+'?json=m_newsletter2&get=addEmail',
                                    onComplete : function(jsonObj) {
                                            if(jsonObj == 'ok') {
                                                    throwErrorWindow('emailAdded');
                                                    $('signUpEmail').set('value','');
                                                   /* $('NLcheckAgree').set('checked',false);*/
                                                    if($('selectNL')) $('selectNL').selectedIndex = 0;
                                            }
                                            else if(jsonObj == 'errorInvitEmail'){
                                                    throwErrorWindow('errorInvitEmail');
                                                    $('signUpEmail').set('value','');
                                                   /* $('NLcheckAgree').set('checked',false);*/
                                                    if($('selectNL')) $('selectNL').selectedIndex = 0;
                                            }
                                            else if(jsonObj == 'alreadyInDb'){
                                                    throwErrorWindow('alreadyInDb');
                                                    $('signUpEmail').set('value','');
                                                    /*$('NLcheckAgree').set('checked',false);*/
                                                    if($('selectNL')) $('selectNL').selectedIndex = 0;
                                            }
                                            else{
                                                    throwErrorWindow('emailNotAdded');
                                            }
                                    }
                                }).post({
                                        'email' : $('signUpEmail').get('value'),
                                        'idNL' : idNL
                                });
                        }

                });
        });


        $$('.m_newsletter2-frontButtonRemoveMe').each(function(elm){
                elm.addEvent('click',function(e1){
                        e1.stop();
                        var filter  = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
                        if($('signUpEmail').get('value') == ''){
                                throwErrorWindow('noEmail');
                        }
                        else if(!filter.test($('signUpEmail').get('value'))){
                                throwErrorWindow('incorrectEmail');
                        }
                        else if($('selectNL') && $('selectNL').selectedIndex == 0){
                                throwErrorWindow('noNLselected');
                        }
                        else{
                                var idNL = 0;
                                if($('hiddenIdNL')) idNL = $('hiddenIdNL').get('value');
                                else if($('selectNL')) idNL = $('selectNL').options[$('selectNL').selectedIndex].value;
                                else throwErrorWindow('errorIdNL');
                                var nodeId = $('nodeId').get('value');
                                var email =$('signUpEmail').get('value');

                                var winDiv = new Element('div',{});
                                var winText = new Element('div',{
                                    'text': lang['reallyDeleteEmail']+"\""+email+'\" z Newsletteru?'
                                });
                                var yesButton = new Element('input',{
                                    'type':'button',
                                    'class':'m_newsletter2-button',
                                    'accesskey':'y',
                                    'value':lang['yesBtn']
                                });

                                var noButton = new Element('input',{
                                    'type':'button',
                                    'class':'m_newsletter2-button',
                                    'accesskey':'n',
                                    'value':lang['noBtn']
                                });
                                var br = new Element('br');

                                winDiv.adopt(winText);
                                winDiv.adopt(br);
                                winDiv.adopt(yesButton);
                                winDiv.adopt(new Element('span',{'text':'  '}))
                                winDiv.adopt(noButton);

                                yesButton.addEvent('click', function(){
                                        var request = new Request.JSON({
                                            url : '/id/'+nodeId+'?json=m_newsletter2&get=unsubscribeEmail',
                                            onComplete : function(jsonObj) {
                                                    wm[1].closeWindow();
                                                    if(jsonObj == 'ok') {
                                                            throwErrorWindow('sentRmvEmail');
                                                            $('signUpEmail').set('value','');
                                                            if($('selectNL')) $('selectNL').selectedIndex = 0;
                                                    }
                                                    else if(jsonObj == 'noUser'){
                                                            throwErrorWindow('noUser');
                                                    }
                                                    else{
                                                            throwErrorWindow('notSentRmvEmail');
                                                    }
                                            }
                                        }).post({
                                                'email' : email,
                                                'idNL' : idNL
                                        });
                                });

                                wm[1] = new windowManager();
                                wm[1].createWindow({
                                    'type':'html',
                                    'title':lang['titleUnsubscribe'],
                                    'content':winDiv,
                                    'closeElement': noButton
                                });
                        }
                });
        });
});
