var viewMail = function(){
    var  viewLink, txtComment,txtSubject,txtTo;
    var tabs,  postBtn;
    var wait, error, errorMsg;
    var posting = false;
    
    return {
        init : function(){
             this.createDialog();
        },
        submitComment : function(){
            // postBtn.disable();
             maildialog.hide();
             dialog.show(postLink);
             txtTo = Ext.get('msgTo');
             txtTo.dom.value = mailFrom+",";
             txtSubject=Ext.get('msgsubject');
 			 txtSubject.dom.value="Re :"+mailSubj;
             txtMsg=Ext.get('comment');
 			 txtMsg.dom.value=mailMsg;
        },
        submitForward : function(){
            // postBtn.disable();
             maildialog.hide();
             dialog.show(postLink);
             txtSubject=Ext.get('msgsubject');
 			 txtSubject.dom.value="Fwd: "+mailSubj;
             txtMsg=Ext.get('comment');
 			 txtMsg.dom.value="---------- Forwarded message ----------<br>"+mailMsg;
        },
        submitDelete : function(){
          	maildialog.hide();
          	var rwid=document.getElementById(rowID)
			//rwid.style.display="block";
            var deleteSuccess = function(o){
                postBtn.enable();
               };
            var deleteFailure = function(o){
                postBtn.enable();
            };
            Ext.lib.Ajax.formRequest('compose-form', 'privatemsg/deletemsg.php?pid='+msgID,
                    {success: deleteSuccess, failure: deleteFailure});  
            
        },
        
        createDialog : function(){
            maildialog = new Ext.BasicDialog("mailinfo", { 
      				modal:true,
                    width:500,
                    height:320,
                    shadow:false,
                    minWidth:500,
                    minHeight:300,
                    proxyDrag: true
                   
            });
            maildialog.addKeyListener(27, maildialog.hide, maildialog);
            postBtn = maildialog.addButton('Reply', this.submitComment, this);
            postBtn = maildialog.addButton('Forward', this.submitForward, this);
            postBtn = maildialog.addButton('Delete', this.submitDelete, this);
            maildialog.addButton('Close', maildialog.hide, maildialog);
            maildialog.on('hide', function(){
            });
        }
    };
}();

//Compose Dialog....
var dialog,postLink;
var browsedialog,profileList,bpostLink;
var ComposeMail = function(){
    var  viewLink, txtComment,txtSubject,txtTo,prvID;
    var tabs, commentsList, postBtn, renderer;
    var wait, error, errorMsg;
    var posting = false;
    return {
        init : function(){
             txtComment = Ext.get('comment');
             txtSubject = Ext.get('msgsubject');
             txtTo = Ext.get('msgTo');
             prvID = Ext.get('prvID');
             txtComment.dom.value = '';
             txtSubject.dom.value = '';
             txtTo.dom.value = '';
             prvID.dom.value = '';
             commentsList = Ext.get('comments-list');
             postLink = Ext.get('compose-mail');
             wait = Ext.get('post-wait');
             error = Ext.get('post-error');
             errorMsg = Ext.get('post-error-msg');
             this.createDialog();
	             postLink.on('click', function(e){
                 e.stopEvent();
             txtComment.dom.value = '';
             txtSubject.dom.value = '';
             txtTo.dom.value = '';
             dialog.show(postLink);
             });
        },
        submitComment : function(){
            wait.radioClass('active-msg');            
            var composeSuccess = function(o){
	         	Ext.MessageBox.alert('Status', 'Message Sent Successfully ..');	
                postBtn.enable();            	
            	wait.removeClass('active-msg');
                dialog.hide();
            };
            var composeFailure = function(o){
                postBtn.enable();
                error.radioClass('active-msg');
                errorMsg.update('Unable to connect.');
            };
            if(txtSubject.dom.value !="" && txtTo.dom.value !="" && txtComment.dom.value != ''){
			    Ext.lib.Ajax.formRequest('compose-form', 'privatemsg/composesend.php?flag=0',
	            {success: composeSuccess, failure: composeFailure});          
			}
            else{
            	wait.removeClass('active-msg');
            	Ext.MessageBox.alert('Status', 'Please fill the mandatory fields !');	            	
            }
        },
        
        draftMail : function(){
            // postBtn1.disable();
            wait.radioClass('active-msg');            
            var draftSuccess = function(o){
            	Ext.MessageBox.alert('Status', 'Message Saved Successfully ..');	
                postBtn.enable();
            	wait.removeClass('active-msg');
                dialog.hide();
            };
            var draftFailure = function(o){
                postBtn.enable();
                error.radioClass('active-msg');
                errorMsg.update('Unable to connect.');
            };
    
    	    if(txtSubject.dom.value !="" && txtTo.dom.value !="")
            {
            Ext.lib.Ajax.formRequest('compose-form', 'privatemsg/composesend.php?flag=1',
                    {success: draftSuccess, failure: draftFailure});          
            }
            else 
            {
            	wait.removeClass('active-msg');
            	Ext.MessageBox.alert('Status', 'Please fill the mandatory fields !');	            	
            }
        },
        createDialog : function(){
            dialog = new Ext.LayoutDialog("compose-dlg", { 
      				modal:true,
                    width:650,
                    height:380,
                    shadow:false,
                    x:100,
                    y:20,
                    minWidth:500,
                    minHeight:300,
                    proxyDrag: true,
                    west: {
	                        split:true,
	                        initialSize: 150,
	                        minSize: 100,
	                        maxSize: 250,
	                        titlebar: true,
	                        collapsible: true,
	                        animate: true,
	                        autoScroll:true
						},
	                    center: {
	                        autoScroll:true,
	                        tabPosition: 'top',
	                        closeOnTab: true,
	                        alwaysShowTabs: true
	                    }
            });
            dialog.addKeyListener(27, dialog.hide, dialog);
            postBtn = dialog.addButton('Send', this.submitComment, this);
            postBtn1 = dialog.addButton('Save as a Draft', this.draftMail, this);            
            dialog.addButton('Close', dialog.hide, dialog);
				var layout = dialog.getLayout();
                layout.beginUpdate();
                layout.add('west', new Ext.ContentPanel('west', {title: '<b>Friends List</b>'}));
	            layout.add('center', new Ext.ContentPanel('center', {title: 'Compose'}));
            // clear any messages and indicators when the dialog is closed
            dialog.on('hide', function(){
                wait.removeClass('active-msg');
                error.removeClass('active-msg');                
            });
	
	       var sizeTextBox = function(){
		       txtTo.setSize(dialog.size.width-200, 20);
               txtComment.setSize(dialog.size.width-200, dialog.size.height-264);
               txtSubject.setSize(dialog.size.width-200, 20);               
            };
            sizeTextBox();
            dialog.on('resize', sizeTextBox);
	    layout.endUpdate();
        }
        
    };
}();

var gID,postLinkMail,maildialog,msgID,commentsList,renderer,mailFrom,mailSubj,mailMsg,rowID;
function xx(x,id,mailFr,rid,draftFlag,imgidx)
{	
	var txtTo,txtSubject,txtMsg,imgID;
	gID=x;
	msgID=id;
	mailFrom=mailFr;
	rowID=rid;
	txtSubject = Ext.get('subj'+msgID);
	txtMsg = Ext.get('msg'+msgID);
	var msgid = document.getElementById(imgidx);	
	msgid.src="../privatemsg/images/read.jpg";
	var subj = document.getElementById('subb'+msgID);
	subj.style.fontWeight='normal';
	mailSubj=txtSubject.dom.value;
	mailMsg=txtMsg.dom.value;
	if(draftFlag=='draft')		 {
			 dialog.show(postLink);
			 txtTo = Ext.get('msgTo');
             txtTo.dom.value = mailFrom+",";
             txtPrvID=Ext.get('prvID');
             txtPrvID.dom.value=msgID;
             txtSubject=Ext.get('msgsubject');
 			 txtSubject.dom.value=mailSubj;
             txtMsg=Ext.get('comment');
 			 txtMsg.dom.value=mailMsg;
	}
	else {
			txtPrvID=Ext.get('prvID');
	        txtPrvID.dom.value="";
			postLinkMail = Ext.get(gID);
		    commentsList=Ext.get('comments-list');
		    maildialog.show(postLinkMail);	
	        renderer = new CommentRenderer(commentsList);
            var um = commentsList.getUpdateManager();
            um.setRenderer(renderer);
            var commentsLoaded = false;
    	    if(!commentsLoaded){
    	       	um.update("privatemsg/getMail.php?pid="+msgID);                   
	            commentsLoaded = true;
    	    }
	}
}

var viewMail = function(){
    var  viewLink, txtComment,txtSubject,txtTo;
    var tabs,  postBtn;
    var wait, error, errorMsg;
    var posting = false;
    return {
        init : function(){
             this.createDialog();
        },
        submitComment : function(){
             maildialog.hide();
             dialog.show(postLink);
             txtTo = Ext.get('msgTo');
             txtTo.dom.value = mailFrom+",";
             txtSubject=Ext.get('msgsubject');
 			 txtSubject.dom.value="Re :"+mailSubj;
             txtMsg=Ext.get('comment');
 			 txtMsg.dom.value=mailMsg;
           
        },
        submitForward : function(){
            // postBtn.disable();
             maildialog.hide();
             dialog.show(postLink);
             txtSubject=Ext.get('msgsubject');
 			 txtSubject.dom.value="Fwd: "+mailSubj;
             txtMsg=Ext.get('comment');
 			 txtMsg.dom.value="---------- Forwarded message ----------<br>"+mailMsg;
        },
        submitDelete : function(){
          	maildialog.hide();
          	var rwid=document.getElementById(rowID)
			rwid.style.display="none";
            var deleteSuccess = function(o){
                postBtn.enable();
            };
            var deleteFailure = function(o){
                postBtn.enable();
            };
            Ext.lib.Ajax.formRequest('compose-form', 'privatemsg/deletemsg.php?pid='+msgID,
                    {success: deleteSuccess, failure: deleteFailure});  
        },
        
        createDialog : function(){
            maildialog = new Ext.BasicDialog("mailinfo", { 
      				modal:true,
                    width:500,
                    height:320,
                    shadow:false,
                    x:350,
                    y:170,
                    minWidth:500,
                    minHeight:300

                   
            });
            maildialog.addKeyListener(27, maildialog.hide, maildialog);
            postBtn = maildialog.addButton('Reply', this.submitComment, this);
            postBtn = maildialog.addButton('Forward', this.submitForward, this);
            postBtn = maildialog.addButton('Delete', this.submitDelete, this);
            maildialog.addButton('Close', maildialog.hide, maildialog);
            
            maildialog.on('hide', function(){
           // error.removeClass('active-msg');                
            });
        }
    };
}();

var CommentRenderer = function(list){
    var tpl = new Ext.DomHelper.Template(
          '<li id="comment-{id}">' +
          '<div class="cheader">'+
          '<div class="cuser">{title}:</div>' +
          '<div class="commentmetadata">{dop}</div>' +
          '</div>{data}</li>'
    );
    this.parse = function(json){
        try{
            return eval('(' + json + ')');
        }catch(e){

        	alert (e);
        }
        return null;
    };
    // public render function for use with UpdateManager
    this.render = function(el, response){
        var data = this.parse(response.responseText);
        if(!data || !data.comments || data.comments.length < 1){
            el.update('There are no mail.');
            return;
        }
        // clear loading
        el.update('');
        for(var i = 0, len = data.comments.length; i < len; i++){
            this.append(data.comments[i]);
        }
    };
    // appends a comment 
    this.append = function(data){
        tpl.append(list.dom, data);
    };
};
// ############# Profile ######################
var frienddialog;
function showCompose(user) {
			frienddialog.hide();
			dialog.show(postLink);
			txtTo = Ext.get('msgTo');
			txtSubject=Ext.get('msgsubject');
 			txtMsg=Ext.get('comment');
            txtTo.dom.value=user+",";
            txtMsg.dom.value='';
            txtSubject.dom.value='';
}
var friendall = function(){
    var  browseLink,imgLink;
    var  bpostBtn;
    var posting = false;
    return {
        init : function(){
             this.createDialog();
             bpostLink = Ext.get('friend-id-mail');
	         bpostLink.on('click', function(e){
		         e.stopEvent();
	             frienddialog.show(bpostLink);
	          });
        },
        submitComment : function(){
        },
        createDialog : function(){
            frienddialog = new Ext.BasicDialog("friend-dlg", { 
      				modal:true,
                    width:500,
                    height:320,
                    shadow:true,
                    minWidth:500,
                    minHeight:300,
                    proxyDrag: true
            });
            frienddialog.addButton('Close', frienddialog.hide, frienddialog);
            frienddialog.on('hide', function(){
            });
        }
    };
}();
var browsedialog,profileList,bpostLink;
var profiledialog,imgUseLgn;
function imgShow(imgUser,imgPath) {
	imgUseLgn=imgUser
	browsedialog.hide();
	profiledialog.show(bpostLink);
	profileList=Ext.get('profile-list');
    renderer = new CommentRenderer(profileList);
    var um = profileList.getUpdateManager();
    um.setRenderer(renderer);
    var commentsLoaded = false;
    if(!commentsLoaded){
    	um.update("privatemsg/getProfile.php?user="+imgUser+"&path="+imgPath);                   
        commentsLoaded = true;
    }
}

var browseUsersList = function(){
    var  browseLink,imgLink;
    var  bpostBtn;
    var posting = false;
    return {
        init : function(){
             this.createDialog();
             bpostLink = Ext.get('browse-id-mail');
	         bpostLink.on('click', function(e){
		         e.stopEvent();
	             browsedialog.show(bpostLink);
	          });
        },
        submitComment : function(){
        },
        createDialog : function(){
            browsedialog = new Ext.BasicDialog("browse-dlg", { 
      				modal:true,
                    width:925,
                    height:420,
                    shadow:true,
                    minWidth:500,
                    minHeight:300,
                    proxyDrag: true
                   
            });
            browsedialog.addKeyListener(27, browsedialog.hide, browsedialog);
            browsedialog.addButton('Close', browsedialog.hide, browsedialog);
            browsedialog.on('hide', function(){
           });
        }
    };
}();

function showNetworkDlg() 
{
	var bpostLink1 = Ext.get('browse-id1');
	browsedialog.show(bpostLink1);	
}
var profileView = function(){
    var posting = false;
    return {
        init : function(){
             this.createDialog();
        },
        backBrowser : function(){
			 profiledialog.hide();
		     browsedialog.show(bpostLink);
        },
        submitComment : function(){
            profiledialog.hide();
            var txtTo = Ext.get('msgTo');
			txtTo.dom.value = imgUseLgn;
			txtSubject=Ext.get('msgsubject');
 			txtMsg=Ext.get('comment');
            txtMsg.dom.value='';
            txtSubject.dom.value='';
            dialog.show(postLink);
        },
        submitForward : function(){
            var commentSuccess = function(o){
            	Ext.MessageBox.alert("Status",o.responseText)
                profiledialog.hide();
            };
            var commentFailure = function(o){
            };
            Ext.lib.Ajax.formRequest('compose-form', 'privatemsg/addfriend.php?invusr='+imgUseLgn,
                    {success: commentSuccess, failure: commentFailure});          
        },
        createDialog : function(){
            profiledialog = new Ext.BasicDialog("profile-dlg", { 
      				modal:true,
                    width:500,
                    height:500,
                    shadow:true,
                    minWidth:500,
                    minHeight:350,
                    proxyDrag: true
            });
            profiledialog.addKeyListener(27, profiledialog.hide, profiledialog);
            postBtn = profiledialog.addButton('Back', this.backBrowser, this);
            postBtn = profiledialog.addButton('Send Message', this.submitComment, this);
            postBtn = profiledialog.addButton('Add a friend', this.submitForward, this);
            profiledialog.addButton('Close', profiledialog.hide, profiledialog);
            maildialog.on('hide', function(){
            });
        }
    };
}();
var profileRenderer = function(list){
    var tpl = new Ext.DomHelper.Template(
          '<li id="comment-{id}">' +
          '<div class="cheader">' +
          '<div class="cuser">{author}:</div>' +
          '</div>{text}</li>'
    );
    this.parse = function(json){
        try{
            return eval('(' + json + ')');
        }catch(e){}
        return null;
    };
    this.render = function(el, response){
        var data = this.parse(response.responseText);
        if(!data || !data.comments || data.comments.length < 1){
            el.update('.Empty.');
            return;
        }
        el.update('');
        for(var i = 0, len = data.comments.length; i < len; i++){
            this.append(data.comments[i]);
        }
    };
    this.append = function(data){
        tpl.append(list.dom, data);
    };
};
loadPvtMsg = function(mailChk){
	var my_pm = Ext.get("center4");
	cp4renderer = new privmsgRender(my_pm, "privateMessage");
    var updater = my_pm.getUpdateManager();
    updater.setRenderer(cp4renderer);
    userid=hdenForms.activeUsrID.value
    if(mailChk==0) {
	    updater.update('/privatemsg/index.php?userid='+userid);   	   
    }
    else {
        updater.update('/privatemsg/index.php?flag=1&userid='+userid);
    }
}

var commentSuccess = function(o){
var inbox=o.responseText;
pvtMessDet=document.getElementById("pvtMessDetails");
pvtMessDet.innerHTML=inbox;
};
            
var commentFailure = function(o){
 };
loadRefresh = function(inboxflag,next,prev) {
	var pvtMessDet=document.getElementById("pvtMessDetails");	
	Ext.lib.Ajax.formRequest('compose-form', 'privatemsg/getInbox.php?flag='+inboxflag+'&next='+next+'&prev='+prev,
                   {success: commentSuccess, failure: commentFailure});  
	
}
/* ****************************************Added for vote_view ************************** */
function imgchange1()
{
		up.src="/template/images/tuOn1.gif";
}
function imgchange2()
{
		dw.src="/template/images/tdOn1.gif"
}
function imgdefault1()
{
	up.src="/template/images/hand_up.jpg";
}
function imgdefault2()
{
	dw.src="/template/images/hand_dn.jpg";
}    
var votecommentSuccess = function(o){
	var successmsg=o.responseText;
	document.getElementById('voteMSG').style.display="none";
	var votemsg=document.getElementById('msgsuc');
	votemsg.innerHTML=successmsg;
	};
var votecommentFailure = function(o){
};
voteFunction1 = function()
{
	var uid=document.getElementById("uid").value;	
	var vid=document.getElementById("vid").value;
	Ext.lib.Ajax.request('GET', 'userlayout/vote_store.php?uid='+uid+'&vid='+vid+'&va=1&res=1',
                   {success: votecommentSuccess, failure: votecommentFailure}); 
	}
	function voteFunction2()
	{
	var uid=document.getElementById("uid").value;	
	var vid=document.getElementById("vid").value;
	
	Ext.lib.Ajax.request('GET', 'userlayout/vote_store.php?uid='+uid+'&vid='+vid+'&va=-1&res=1',
                   {success: votecommentSuccess, failure: votecommentFailure}); 
	}

/************ Delete & Spam Mail **********************/
var mailDelete = function(){
		    var chk1=document.getElementsByName("chk[]");
		    var len=chk1.length;
		    var delChk=0;
			for(var i=0;i<len;i++) {
				if(chk1[i].checked==true) {
					delChk=1;
					break;
				}
			}
			if(delChk==1) {
	        Ext.MessageBox.confirm('Confirm', 'Are you sure you want to delete ?', deleteResult);
			}
			else {
	        Ext.MessageBox.alert('Stauts', 'Select ..........');
			}
		
			
    };
    
    function deleteResult(btn){
    	if(btn=="yes") {
   		 Ext.lib.Ajax.formRequest('delmessage', 'privatemsg/deletemsg.php',
		     {success: delmsgSuccess, failure: delmsgFailure});     
		    var chk1=document.getElementsByName("chk[]");
		    var len=chk1.length;

			for(var i=0;i<len;i++) {
				if(i!=0) {
					if(chk1[i].checked==true) {
						var rwid=document.getElementById('row'+i)
						rwid.style.display="none";
					}
				}
			}
						loadRefresh('inbox',-1,'');
    	}
    	else {
    	}
    };
    var delmsgSuccess = function(o){
    };
    var delmsgFailure = function(o){
    	alert("Failure...");
    };
var spamDelete = function(){
	 var chk1=document.getElementsByName("chk[]");
		    var len=chk1.length;
		    var delChk=0;
			for(var i=0;i<len;i++) {
				if(chk1[i].checked==true) {
					delChk=1;
					break;
				}
			}
			if(delChk==1) {	
	        Ext.MessageBox.confirm('Confirm', 'Spam......... ?', spamResult);
	        }
			else {
	        Ext.MessageBox.alert('Stauts', 'Select ..........');
			}
    };
    function spamResult(btn){
    	if(btn=="yes") {
    		 Ext.lib.Ajax.formRequest('delmessage', 'privatemsg/spamuser.php',
		     {success: spamSuccess, failure: spamFailure});     
		    var chk1=document.getElementsByName("chk[]");
		    var len=chk1.length;
			for(var i=0;i<len;i++) {
				if(chk1[i].checked==true) {
					var rwid=document.getElementById('row'+i)
					rwid.style.display="none";
				}
			}
    	}
    	else {
    	}

    };
    
    var spamSuccess = function(o){
    };
    var spamFailure = function(o){
    	alert("Failure...");
    };
function limitlength(obj, length){
	var maxlength=length
	if (obj.value.length>maxlength)
	obj.value=obj.value.substring(0, maxlength)
}
//Netword (Next & Previous Concept
var networkSuccess = function(o){
var inbox=o.responseText;
pvtMessDet=document.getElementById("networkDetails");
pvtMessDet.innerHTML=inbox;
};
            
var networkFailure = function(o){
 };

networkRefresh = function(next,prev) {
	Ext.lib.Ajax.formRequest('compose-form', 'privatemsg/browseDialog.php?next='+next+'&prev='+prev,
                   {success: networkSuccess, failure: networkFailure});  
	
}
