
Ext.onReady(function()
{
   Ext.QuickTips.init();

	var txtUserName = new Genesis.TextBox({		
		applyTo: 'together_togetherContent_txtUserName'
	});
	
	var txtPassword = new Genesis.TextBox({		
		applyTo: 'together_togetherContent_txtPassword'
	});
	txtUserName.focus();
	document.getElementById('searchpanel').style.display='none';
});


function CheckAuthentication()
{
		var userid	=encodeURIComponent(document.getElementById('together_togetherContent_txtUserName').value);
		var pwd		=encodeURIComponent(document.getElementById('together_togetherContent_txtPassword').value);
		//var pwd		=document.getElementById('together_togetherContent_txtPassword').value;
		var dvError=document.getElementById('together_togetherContent_dvError');
		dvError.innerHTML='';
		var url= '../Handlers/Login.ashx?login=login&user='+userid+'&pwd='+pwd;		
        Ext.Ajax.request({
           url:url,
           success: function(action, o){
			var value=action.responseText;
			if(value.split('|')[0]=='newuser')			
				changePasswordWindow(value.split('|')[1]);
			else if(value.split('|')[0] =='expire') 
				changePasswordWindow(value.split('|')[1]);
			else if(value.split('|')[0]=='exist')
			{
					Ext.MessageBox.confirm('Confirm', 'Your password is about to expire in '+ value.split('|')[2] + ' days.<br/> Do you want to change your password now?' ,function(btn)
					{
							if(btn=='yes')
							{
								changePasswordWindow(value.split('|')[1]);
							}
					});
			
			}
			else if(value=='1')
				window.location='Dashboard.aspx';
			else if(value=='2')
				window.location='AttendanceRecord.aspx';
			else if(value=='3')
				window.location='SearchCertificate.aspx';
			else
				dvError.innerHTML=value;
			
           },
           failure: function(action, o){
                  //  Ext.MessageBox.alert('FAIL', 'fail');
			}
    });  
    return false;
}

var changePasswordWindow=function(id)
{
	var  win = new Ext.Window({
				layout      : 'fit',
				title:'Change Password',
                width       : 310,
                height      : 175,
              //  closeAction :'hide',
                modal:true,
                autoScroll: true,
                items:[{
				xtype : "form",
				id	:'form1',
				border : false,
                	items:[{			    
					  layout : "column",
					  items : [{
						  width : 10,				 
						  border : true
						},{
						  items : [{
							  xtype : "fieldset",
							  title:'',
							  autoHeight : true,
							  items : [{
								html:'<a href="#" style="margin-left:125px;margin-bottom:20px;" class="linkstext" onclick="return openWindow(\'../Html/Passwordrule.htm\');">(Rules for password)</a>',
							//specific, because defaultType = textfield; this must be a panel
							    xtype:'label'              
			            
						},{
								  xtype : "gentextbox",
								  name : "txtoldpwd",
								  id : "txtoldpwd",
								  visibilityType:'R',
								  fieldLabel : "old/temporary password",
								  inputType:'password'
								  
							  },{
								  xtype : "gentextbox",
								  name : "txtnewpwd",
								  id : "txtnewpwd",
								  visibilityType:'R',						  
								  fieldLabel : "New Password",
								  inputType:'password'
								  
							  },{
								  xtype : "gentextbox",
								  name : "txtconfirmpwd",
								  id : "txtconfirmpwd",
								  visibilityType:'R',							  
								  fieldLabel : "Confirm Password",
								  inputType:'password',
								  vtype: 'password',
								  initialPassField: 'txtnewpwd'
							  }],
							  labelWidth : 120	,
							  buttons : [{
								text:'Submit',
								handler  : function(){ 
									var form1=Ext.getCmp('form1');
									var txtoldpwd=Ext.getCmp('txtoldpwd');
									var txtnewpwd=Ext.getCmp('txtnewpwd');
									var txtconfirmpwd=Ext.getCmp('txtconfirmpwd');
									var oldpwd=encodeURIComponent(txtoldpwd.getValue());
									var newpwd=encodeURIComponent(txtnewpwd.getValue());
									
									if(form1.getForm().isValid())
									{
										var url= '../Handlers/Login.ashx?login=change&txtoldpwd=' + oldpwd + '&txtnewpwd=' + newpwd ;
										Ext.Ajax.request({
												   url:url,
												   params:{
//														txtoldpwd:encodeURIComponent(txtoldpwd.getValue()),
//														txtnewpwd:encodeURIComponent(txtnewpwd.getValue()),
//														txtconfirmpwd:encodeURIComponent(txtconfirmpwd.getValue())	
														},
												   success: function(action, o){
													var value=action.responseText;
													if(value=='1')
														window.location='Dashboard.aspx';
													else if(value=='2')
														window.location='AttendanceRecord.aspx';
													else if(value=='3')
														window.location='SearchCertificate.aspx';
													else
														Ext.MessageBox.alert('', value);
													
												   },
												   failure: function(action, o){
														  //  Ext.MessageBox.alert('FAIL', 'fail');
													}
											}); 
									}
									else
									{
										if(txtnewpwd.getValue() != txtconfirmpwd.getValue())
											Ext.MessageBox.alert('', 'Password does not match.');
									}				   
											
								}
							},{
								text:'Cancel',
								handler	:function(){win.close();}
							}]
						}]
					}]
			}]
		}]
	});
	win.show();
	
	return false;
}

function HandleEnterKey(event) {
//	if(event.keyCode==13)
//		document.getElementById('together_togetherContent_btnLogin').focus();
}