﻿function $(findBy){ return document.getElementById(findBy); }
function tC(commentContent){ // Toggle Content
    if ($(commentContent).style.display != 'block') $(commentContent).style.display = 'block';
    else $(commentContent).style.display = 'none';
}

function changeToTips(cm, lm, li){
    Ext.get('uxCommentsList').addClass('hidden')
    Ext.get('uxCommentsTab').removeClass('current')
    Ext.get('uxTipsList').removeClass('hidden')
    Ext.get('uxTipsTab').addClass('current')
    $('uxNewComment').href = String.format('javascript:AddTip({0},{1},{2},0)',cm,lm,li);
    $('uxNewComment').innerHTML = '+ הוספת טיפ';
}

function changeToComments(cm, lm, li){
    Ext.get('uxTipsList').addClass('hidden')
    Ext.get('uxTipsTab').removeClass('current')
    Ext.get('uxCommentsList').removeClass('hidden')
    Ext.get('uxCommentsTab').addClass('current')
    $('uxNewComment').href = String.format('javascript:AddComment({0},{1},{2},0)',cm,lm,li);
    $('uxNewComment').innerHTML = '+ הוספת תגובה';
}

function eHL(obj){ /*obj.style.backgroundColor = '#F9F9F9'; */} // Enable HightLight
function dHL(obj){ /*obj.style.backgroundColor = ''; */} // Disable HightLight

var removeCommentModuleId = 0; removeCommentId = 0;
function RemoveComment(cm, ci) {
    removeCommentModuleId = cm;
    removeCommentId = ci;
    Ext.MessageBox.confirm('אזהרה !', 'האם את/ה בטוח/ה שברצונך למחוק תוכן זה ?<br />(כן - לאישור מחיקה)', function(btn){
        if (btn == "yes") {
            var atp = new MComment();
            var r = atp.removeComment(removeCommentModuleId, removeCommentId);
            
			if (r.isSuccess) Ext.AtpUtilities.msg('סטאטוס המחיקה', '{0}', r.message);
			else Ext.MessageBox.alert('פעולת המחיקה נכשלה', r.message);
        }
    });
}

function UpdateComment(cm, ci){
    UpdateCommentDialog.showDialog('עריכת תגובה', cm, ci, $('mcT'+ci).innerHTML, $('mcC'+ci) ? $('mcC'+ci).innerHTML.replace('<BR>', '\n') : '');
}

function AddComment(cm, lm, li, pi){ 
    window.open("/Services/Comments/AddComment.aspx?module="+lm+"&itemId="+li+"&parentId="+pi,'response',
'width=550,height=550,menu=no');
    //AddCommentDialog.showDialog('תגובה חדשה', cm, lm, li, pi); 
}

var AddCommentDialog = function() {
    StopTimedRefresh('timeOut');
    var dialog, formData, commentModule, linkModule, linkItem, parentId;
    return {
        showDialog: function(title, cm, lm, li, pi, sub, msg) {
            commentModule = cm; linkModule = lm; linkItem = li; parentId = pi;
            
            if (!sub) sub = '';
            if (!msg) msg = '';
            
            if (!this.dialog) {
		        Ext.QuickTips.init();
    			Ext.form.Field.prototype.msgTarget = 'side';
		        this.dialog = new Ext.LayoutDialog("dlgMCaC", { 
                    width:394, height:270, shadow:true, draggable: true, resizable: false, fixedcenter: true, buttonAlign: 'left', center: { autoScroll:false, alwaysShowTabs: false }
                });
                
    	        this.formData = new Ext.form.Form({ labelAlign: 'left', buttonAlign: 'left', labelWidth: 50 });

		        this.formData.fieldset({style:'clear:both;'},
			        new Ext.form.TextField({ fieldLabel: 'שם',   id: 'comment_userName', name: 'comment_userName', maxLength: 100, width: 285, allowBlank:false }),
        	        new Ext.form.TextField({ fieldLabel: 'נושא', id: 'comment_subject', name: 'comment_subject', maxLength: 100, value: sub, width: 285, allowBlank:false }),
        	        new Ext.form.TextArea({ fieldLabel: 'תוכן',  id: 'comment_message', name: 'comment_message', maxLength: 2500, value: msg, width: 289, height: 90 })
		        );
        		
		        this.formData.render('dlgMCaF');
        		
	            var panel = new Ext.ContentPanel('dlgMCaF', {'title': 'test', fitToFrame:true, closable:true});
	            this.dialog.getLayout().add('center', panel);     

                this.dialog.addKeyListener(27, this.dialog.hide, this.dialog);
                this.dialog.addButton('שלח', this.sendMessage, this);
                this.dialog.addButton('סגור', this.dialog.hide, this.dialog);
                
            }
            this.formData.reset();
            
            //this.formData.setValues({ 'email_subject': '', 'email_message': '' });
            this.dialog.setTitle(title);
            this.dialog.show();
        },  
        
        sendMessage: function () {
            if (!this.formData.isValid()){
                Ext.MessageBox.alert ('אימות נתונים', 'נא בדוק את הנתונים ונסה שוב');
                return;
            }
			
			window.open("/Services/Comments/AddComment.aspx?module="+linkModule+"&itemId="+linkItem+"&parentId="+parentId);
			
//			
//            var atp = new MComment();
//            var r = atp.addComment(
//                commentModule, linkModule, linkItem, parentId,
//                Ext.get('comment_userName').getValue(),
//                Ext.get('comment_subject').getValue(),
//                Ext.get('comment_message').getValue()
//            );
//            
//			if (r.isSuccess) {
//			    this.dialog.hide();
//				//Ext.AtpUtilities.msg('סטאטוס ההוספה', '{0}', r.message);
//				location.href="#comments";
//				location.reload(true);
//			} else Ext.MessageBox.alert('פעולת ההוספה נכשלה', r.message);
        }
    };
}();

var UpdateCommentDialog = function() {
    var dialog, formData, moduleId, itemId;
    return {
        showDialog: function(title, mid, iid, sub, msg) {
            moduleId = mid;
            itemId = iid;
            
            if (!sub) sub = '';
            if (!msg) msg = '';
            
            if (!this.dialog) {
		        Ext.QuickTips.init();
    			Ext.form.Field.prototype.msgTarget = 'side';
		        this.dialog = new Ext.LayoutDialog("dlgMCuC", { 
                    width:394, height:260, shadow:true, draggable: true, resizable: false, fixedcenter: true, buttonAlign: 'left', center: { autoScroll:false, alwaysShowTabs: false }
                });
                
    	        this.formData = new Ext.form.Form({ labelAlign: 'left', buttonAlign: 'left', labelWidth: 50 });

		        this.formData.fieldset({style:'clear:both;'},
			        new Ext.form.TextField({ fieldLabel: 'נושא', id: 'comment_usubject', name: 'comment_usubject', maxLength: 100, width: 285, allowBlank:false }),
        	        new Ext.form.TextArea({  fieldLabel: 'תוכן', id: 'comment_umessage', name: 'comment_umessage', maxLength: 2500, width: 289, height: 120 })
		        );
        		
		        this.formData.render('dlgMCuF');
        		
	            var panel = new Ext.ContentPanel('dlgMCuF', {'title': 'test', fitToFrame:true, closable:true});
	            this.dialog.getLayout().add('center', panel);     

                this.dialog.addKeyListener(27, this.dialog.hide, this.dialog);
                this.dialog.addButton('עדכן', this.sendMessage, this);
                this.dialog.addButton('סגור', this.dialog.hide, this.dialog);
                
            } this.formData.reset();
            
    		$('comment_usubject').value = sub;
    		$('comment_umessage').value = msg;
            
            this.dialog.setTitle(title);
            this.dialog.show();
        },  
        
        sendMessage: function () {
            if (!this.formData.isValid()){
                Ext.MessageBox.alert ('אימות נתונים', 'נא בדוק את הנתונים ונסה שוב');
                return;
            }
            
            var atp = new MComment();
            var r = atp.updateComment( moduleId, itemId,
                Ext.get('comment_usubject').getValue(),
                Ext.get('comment_umessage').getValue()
            );
            
			if (r.isSuccess) {
			    this.dialog.hide();
				Ext.AtpUtilities.msg('סטאטוס העדכון', '{0}', r.message);
			} else Ext.MessageBox.alert('פעולת העדכון נכשלה', r.message);
        }
    };
}();