// ############# Profile ######################
var CreatePreference = function(){
	var prefdialog,prefLink,preference_tabs,inviteLayout,invSucMsg,mailAddr,Uname, Pwd;
    var abtcmt,prfChk,prefLink1;
    var addFriend,invFriend;
    var  Btn,Btn1,Btn2,BtnClose,BtnNext,submitBut;
    var wait1, error1, errorMsg1,tabID=1,errorMsg,firstname;
   
    return {
        init : function(){

             // cache some elements for quick access
             abtcmt = Ext.get('comment');
             getName = Ext.get('user_login');
             responseMsg = Ext.get('successmessage');
             invitefrnd = Ext.get('invfriend');
             errorMsg = Ext.get('errormsg');
             inviteLayout = Ext.get('inviteDesign');
			 invSucMsg = Ext.get('invSuccessMsg');
			 mailAddr=Ext.get('address');
			 Uname=Ext.get('username');
			 Pwd=Ext.get('password');
			 prfChk=Ext.get('profileChk');
			 invChk=Ext.get('inviteChk');
			 customChk=Ext.get('custChk');
			 prefLink = Ext.get('preference-id');
			 frnd=Ext.get('invite-id');
			 friendmsg=Ext.get('friend-wait');
			 firstname=Ext.get('first_name');
			 this.createDialog();
             this.createinviteDialog();
             prefLink.on('click', function(e){
             e.stopEvent();                
             preference_tabs.activate('profile-tab');
             prefdialog.show(prefLink);
                  
             });
             
             frnd.on('click', function(e){
             	//after clicking invite link
                e.stopEvent();                
				friend_tabs.activate('invitefriend-tab');	
				inviteDialog.show(frnd);
				
             });
             
             //when cme throu photo
             if((prfChk.dom.value=="1") && (invChk.dom.value=="0")) {
             	 inviteDialog.show(frnd);
			 }
          
             if(prfChk.dom.value=="0") {
             	 prefdialog.show(prefLink);
			 }
			 else
			 {
			 	 prefdialog.hide();
			 }
			 
        },
        
         // submit the comment to the server
             skipDlg : function(){
            var commentSuccess = function(o){
					if(tabID == 1) {
					 preference_tabs.activate('about-tab');				
					}
					else if(tabID == 2) {
			         preference_tabs.activate('photo-tab');
					}
					else if(tabID == 3) {
    				   if(prfChk.dom.value=="0")	
    				   {
			         prefdialog.hide();
			         inviteDialog.show(frnd);
    				   }
    				   else
    				   {
    				   	prefdialog.hide();
    				   }
					}
	        }
            
            var commentFailure = function(o){
			prefdialog.hide();
            };
			

           Ext.lib.Ajax.formRequest('profile-form', 'privatemsg/closeUpdate.php?from=preference',{success: commentSuccess, failure: commentFailure});  
			
        },
       
        skipfrndDlg : function(){
         
          var skipSuccess = function(o){  	
          		if((tabID == 1)||(tabID == 3)) {
          	         friend_tabs.activate('invitefriend-tabOther');
          	         invitefrnd.dom.style.display="block";  
			         errorMsg.dom.style.display="none";
            		 Uname.dom.value = "";
            		 Pwd.dom.value = "";
            		
					}		
					else if(tabID == 4) {
						
					inviteLayout.dom.style.display="block"
 	          		invSucMsg.dom.style.display="none"
 	                mailAddr.dom.value="";
					friend_tabs.activate('Addfriend');
					}
					else if(tabID == 5)
					{
					if((customChk.dom.value=="0"))
          		 	 {
	           	 	//for showing customize dialog
	           	 	inviteDialog.hide();
	           	 	cutomizeDialog.show();
	           	 	Ext.lib.Ajax.formRequest('profile-form', 'privatemsg/closeUpdate.php?from=custom');  
           	         }
					}
          }
           var skipFailure = function(o){
			inviteDialog.hide();
            };
		Ext.lib.Ajax.formRequest('profile-form', 'privatemsg/closeUpdate.php?from=invite',{success: skipSuccess, failure: skipFailure});  			
                  },
                  
                  
       closeDlg : function(){
            var commentSuccess = function(o){
   			if(prfChk.dom.value=="0")
 			{
 			   prefdialog.hide();	
			   inviteDialog.show(frnd);
        	 }
			 else
			 {
			 	fname=firstname.dom.value;
				var fname1=fname.replace(/^\s*|\s*$/g, "");
				if(fname1 == "")
				{
					 Ext.MessageBox.alert('Status', 'Please Enter the First name');
				}
				else
				{
				prefdialog.hide();
				}
             }
		
            };
            
            var commentFailure = function(o){
			prefdialog.hide();
            };
			

           Ext.lib.Ajax.formRequest('profile-form', 'privatemsg/closeUpdate.php?from=preference',{success: commentSuccess, failure: commentFailure});  
			
        },
        
        
        
        closefrndDlg : function(){
        	 var commentSuccess = function(o){
            	
           	 if((customChk.dom.value=="0"))
           	 {
           	 	//for showing customize dialog
           	 	inviteDialog.hide();
           	 	cutomizeDialog.show();
           	 	Ext.lib.Ajax.formRequest('profile-form', 'privatemsg/closeUpdate.php?from=custom');  
           	 }
           	 else
           	 {
           	 	inviteDialog.hide();
           	 	invitefrnd.dom.style.display="block";  
			    errorMsg.dom.style.display="none";
           	 	Uname.dom.value = "";
            	Pwd.dom.value = "";
            	sendBtn.hide();
            	invSelectedBtn.hide();
               	getFriendBtn.show();
               	inviteLayout.dom.style.display="block"
 	            invSucMsg.dom.style.display="none"
 	            mailAddr.dom.value="";
           	 }
            };
            
            var commentFailure = function(o){
			prefdialog.hide();
            };
	      Ext.lib.Ajax.formRequest('profile-form', 'privatemsg/closeUpdate.php?from=invite',{success: commentSuccess, failure: commentFailure});  
			
        },
        submitComment : function(){
             
                        var commentSuccess = function(o){
               //Ext.MessageBox.alert('Status', 'Information Successfully Updated');	
              		if(tabID == 1) {
					 preference_tabs.activate('about-tab');				
					}
					else if(tabID == 2) {
			         preference_tabs.activate('photo-tab');
					}
					else if(tabID == 3) {
			         prefdialog.hide();
			         inviteDialog.show(frnd);
			        	}		
            };
            
            var commentFailure = function(o){
            	alert("failure");
                Btn.enable();
            };
			
			if(tabID == 1) {
				fname=firstname.dom.value;
				var fname1=fname.replace(/^\s*|\s*$/g, "");
				if(fname1 == "")
				{
					 Ext.MessageBox.alert('Status', 'Please Enter the First name');
				}
				else
				{
			   Ext.lib.Ajax.formRequest('profile-form', 'privatemsg/profileUpdate.php',{success: commentSuccess, failure: commentFailure});  
				}
			}
			else if(tabID == 2) {
			   Ext.lib.Ajax.formRequest('about-frm', 'privatemsg/aboutUpdate.php',{success: commentSuccess, failure: commentFailure});  
			}
			
			
        },
        
       
        
        getAddress : function(){
            var addressSuccess = function(o){
           
                friendmsg.removeClass('active-msg');
              
                var msg = o.responseText;
                var chk=msg.search(/Invalid Login/);
               
              
          		if(chk > 0)
                {
                	
            		Ext.MessageBox.alert('Status', 'Invalid Login');
            		Uname.dom.value == "";
            		Pwd.dom.value == "";
               		invSelectedBtn.hide();
               		getFriendBtn.show();
                }
               else
                {
                 
                    errorMsg.dom.style.display="block";  
                 	errorMsg.dom.innerHTML=msg;
                	invitefrnd.dom.style.display ="none";
                	invSelectedBtn.show();
	                sendBtn.hide();
                }
               
             	       
            };
            
            var addressFailure = function(o){
            	alert("failure");
                Btn.enable();
            };
            
            
           		
                if(Uname.dom.value == "")
                {
                	
                	Ext.MessageBox.alert('Status', 'Please Enter the Username');
               		invSelectedBtn.hide();
               		getFriendBtn.show();
               		
                }
                else if(Pwd.dom.value == "")
                {
                	
                	Ext.MessageBox.alert('Status', 'Please Enter the Password');
               		invSelectedBtn.hide();
               		getFriendBtn.show();
               		
                	
                }
		  		  else 
                {
                	friendmsg.radioClass('active-msg');
                	Ext.lib.Ajax.formRequest('invForm', 'privatemsg/importaddress.php',{success: addressSuccess, failure: addressFailure}); 
                	invSelectedBtn.show();
			        getFriendBtn.hide();
                }
			
        
        

        },
        
        invSelected : function(){
             
	
            var mailSuccess = function(o){
            	
                 d = document.inviteform;
				 friendSelected = "false";
				 count = 0;
			    while ((count < d.elements.length) && ("false" == friendSelected)) {
			    if (d.elements[count].type == "checkbox") {
					if(d.elements[count].checked) { 
					 friendSelected = "true";
				  	}
	 				}
				count++;
					}
			if (friendSelected == "false"){ 
			 Ext.MessageBox.alert('Status', 'Please select a friend you want to invite');
			}
			else
			{
               	var data1 = o.responseText;
               	errorMsg.dom.style.display="block";
               	errorMsg.dom.innerHTML=data1;
               	invSelectedBtn.hide();
               	sendBtn.show();
			}                       	
             
            };
            
            var mailFailure = function(o){
            	alert("failure");
                Btn.enable();
            };
        Ext.lib.Ajax.formRequest('inviteform', 'privatemsg/mailpreview.php?flag=0',{success: mailSuccess, failure: mailFailure}); 
        inviteFrom="mailsoption";
        },
        
        
        sendEmail : function(){
             var sendSuccess = function(o){
     		 friendmsg.removeClass('active-msg');
             if(inviteFrom == "mailsoption")	
             {
	             Ext.MessageBox.alert('Status', 'Mail has been sent successfully');
	             invitefrnd.dom.style.display="block";  
			     errorMsg.dom.style.display="none";
	             Uname.dom.value = "";
	       		 Pwd.dom.value = "";
	       		 getFriendBtn.show();
	             sendBtn.hide();		
             }
             else
             {
           		 
             	 Ext.MessageBox.alert('Status', 'Mail has been sent successfully');	
             	 inviteLayout.dom.style.display="block"
 	             invSucMsg.dom.style.display="none"
 	             mailAddr.dom.value="";
  			     inviteBtn.show();
  				 sendBtn.hide();
             }	
             
            };
            
            var sendFailure = function(o){
            	alert("failure");
                Btn.enable();
            };
            friendmsg.dom.innerHTML="<img src='../../images/loading.gif' width='16' height='16' align='absmiddle' />&nbsp;<font size=2px>Sending emails please wait ...</font>";
            friendmsg.radioClass('active-msg');
                      
        Ext.lib.Ajax.formRequest('mailForm', 'privatemsg/mailpreview.php?flag=1',{success: sendSuccess, failure: sendFailure}); 
        },
        
        
        
       addAsFriend : function(){
            var addedSuccess = function(o){
                var data = renderer.parse(o.responseText);
                if(data){
                    getName.dom.value = ""
                    Ext.MessageBox.alert('Status', data.returnfriends);	
                    getName.focus();
		        }
            };
            
            var addedFailure = function(o){
             };
    
            Ext.lib.Ajax.formRequest('registerform', 'privatemsg/addFriends.php',
                    {success: addedSuccess, failure: addedFailure});          
        },
        
        invFriend : function(){
  					
 			
 				var mailid=mailAddr.dom.value;
 				if(mailid=="") {
		 		
				Ext.MessageBox.alert('Status', 'Please enter the mail address.');
				inviteBtn.show(); 
				mailAddr.focus();
 				}
 				else if((mailid.indexOf('@') < 0) || ((mailid.charAt(mailid.length-4) != '.') && (mailid.charAt(mailid.length-3) != '.'))) 
						{
						Ext.MessageBox.alert('Status','You have entered an invalid email address. Please try again.');
						} 
 				else {
 					
	       		var inviteSuccess = function(o){
				    inviteBtn.show();
		    	    var getdata = o.responseText;
  					
  					if(getdata){
  						invSucMsg.dom.style.display="block";
  						invSucMsg.dom.innerHTML = getdata;
  					    inviteLayout.dom.style.display="none";
  					    
  						inviteBtn.hide();
	  					sendBtn.show();
  					}
  					else{
  						
  					}
  				};
  			
  				var inviteFailure = function(o){
					
  				};
 
 	            Ext.lib.Ajax.formRequest('invitefriend', 'privatemsg/mailpreview.php?flag=0',
                    {success: inviteSuccess, failure: inviteFailure});          
                     inviteFrom="friendsoption";
 				}
 				 
  			},
          createDialog : function(){
            prefdialog = new Ext.BasicDialog("preference-dlg", { 
	            	modal:true,
                    autoTabs:true,
                    width:550,
                    height:590,
                    shadow:true,
                    minWidth:300,
                    minHeight:300
            });
            prefdialog.addKeyListener(27, prefdialog.hide, prefdialog);
            Btn = prefdialog.addButton('Submit', this.submitComment, this);
            BtnSkip = prefdialog.addButton('Skip', this.skipDlg, this);
            BtnClose = prefdialog.addButton('Close', this.closeDlg, this);
            // clear any messages and indicators when the dialog is closed
            prefdialog.on('hide', function(){
            if(tabID==4)
			{
				//errorMsg.dom.innerHTML= invitefrnd.dom.innerHTML;
				 errorMsg.update(invitefrnd.dom.innerHTML);
			}
            });
            // store a refeence to the tabs
            preference_tabs = prefdialog.getTabs();
            // hide the post button if not on Post tab
            preference_tabs.on('tabchange', function(panel, tab){
            	if(tab.id == 'profile-tab') {
            		tabID=1;
            	} else if(tab.id == 'about-tab') {
            		tabID=2;
            	} else if(tab.id == 'photo-tab') {
            		tabID=3;
            	} else if(tab.id == 'invitefriend-tab') {
            		tabID=4;
            		getFriendBtn.show();
            		invSelectedBtn.hide(); 
            		sendBtn.hide(); 
            	} 
                 Btn.setVisible(tab.id == 'profile-tab' || tab.id == 'about-tab');
            });
            
            // set up the comment renderer, all ajax requests for commentsList
            // go through this render
           
            
            // lazy load the comments when the view tab is activated
            var commentsLoaded = false;
            preference_tabs.getTab('about-tab').on('activate', function(){
                           });
            
            preference_tabs.getTab('photo-tab').on('activate', function(){
		
                
            });
            
             renderer = new friendsRenderer(addFriend);
            },
         
        createinviteDialog : function(){
           inviteDialog = new Ext.BasicDialog("friends-dlg", { 
	            	modal:true,
                    autoTabs:true,
                    width:610,
                    height:570,
                    shadow:true,
                    minWidth:300,
                    minHeight:300
            });
       
            getFriendBtn = inviteDialog.addButton('Get Friends', this.getAddress, this);
            invSelectedBtn = inviteDialog.addButton('Invite Selected', this.invSelected, this);
            sendBtn = inviteDialog.addButton('Send', this.sendEmail, this);
            inviteBtn = inviteDialog.addButton('Invite', this.invFriend, this);
            submitBut = inviteDialog.addButton('Add Friend', this.addAsFriend, this);
            BtnSkipfnd = inviteDialog.addButton('Skip', this.skipfrndDlg, this);
            BtnClose1 = inviteDialog.addButton('Close', this.closefrndDlg, this);
            
            invSelectedBtn.hide();
            submitBut.hide();
            getFriendBtn.show(); 
            sendBtn.hide();
            inviteBtn.hide();
       
            friend_tabs = inviteDialog.getTabs();
           
            
            friend_tabs.on('tabchange', function(panel, tab){
            	
            	 if(tab.id == 'invitefriend-tab') {
            		tabID=3;
            		getFriendBtn.show();
            		invSelectedBtn.hide(); 
            		sendBtn.hide(); 
            		BtnSkip.hide();
            		BtnSkipfnd.show();
            		
            	
            	} 
            	 if(tab.id == 'invitefriend-tabOther') {
            		tabID=4;
            		invSelectedBtn.hide(); 
            		getFriendBtn.hide();
            		sendBtn.hide(); 
            		BtnSkipfnd.show();
            		
            		} 
            	 if(tab.id == 'Addfriend') {
            		tabID=5;
            		sendBtn.hide();
            		if(prfChk.dom.value=="1")
            		{
            			BtnSkipfnd.hide();
            		}
            		         	
            	} 
                submitBut.setVisible(tab.id == 'Addfriend');
                getFriendBtn.setVisible(tab.id == 'invitefriend-tab');
                inviteBtn.setVisible(tab.id == 'invitefriend-tabOther');
            });
            }
    };
}();

var friendsRenderer = function(list){
    // create a template for each JSON object
    //alert("inside renderer");
    
    var tpl = new Ext.DomHelper.Template(
          '<li><div>{data}:</div></li>'
    );
    
    this.parse = function(json){
        //alert("json:"+json);
        try{
            return eval('(' + json + ')');
        }catch(e){
        	//alert ("inside e");
        	//alert (e);
        }
        //alert ('return null');
        return null;
    };
    
    // public render function for use with UpdateManager
    this.render = function(el, response){
    	var data = this.parse(response.responseText);
        // clear loading
        el.update('');
        
    };
    
    // appends  
    this.append = function(data){
        tpl.append(list.dom, data);
    };
};


Ext.EventManager.onDocumentReady(CreatePreference.init, CreatePreference, true);
// YAHOO.util.Event.onAvailable("preference-id", CreatePreference.init, CreatePreference, true);

function cbevent()
{

	d = document.inviteform;
	friendSelected = "false";
	count = 0;
	while ((count < d.elements.length) && ("false" == friendSelected)) {
		   if (d.elements[count].type == "checkbox") {
			if(d.elements[count].checked) { 
				 friendSelected = "true";
				 
			 }
	 	}
		count++;
	}
	if (friendSelected == "false"){ 
		alert("Please select a friend you want to invite");
		return false;	
	}
	return true;
}

function togglechecked()
{ 
 for (var i = 0; i < document.inviteform.elements.length; i++) 
 {
   var e = document.inviteform.elements[i];
   if ((e.disabled == false) && (e.name != 'allbox') && (e.type == 'checkbox'))
    {
     e.checked = document.inviteform.allbox.checked;
    }
  }
}
function toggleselect()
{ 
document.inviteform.allbox.checked = !document.inviteform.allbox.checked;
togglechecked();
}

function invitefrd()
{
	var frrnd=Ext.get('invitefriend-tab');
	inviteDialog.show(frrnd);
	
}

