$(document).ready(function() {
	dialog.showWait();
	
	// rpc
	rpc.config(
			{
				callback: function(r,e) {
					if(r!=null){				
							dialog.removeWait();
					} else {
						errorMsg();
					}
				},
            init: false,
            url:            CONFIG.rpc_url,
            loginPage:      CONFIG.page_login
			}
		);
		
	rpc.init();	
	
	// forgot password form
	$('#LinkForgotPass').click(function() {
		$('#DivDialogForgotPass').dialog({ 
			modal: true, 
			autoOpen: true,
			draggable: false,
			buttons: {  "Enviar": function() {
							$("#email").removeClass( "ui-state-error" );
							var strEmail = $("#email").val();
							if(!validateEmail(strEmail)) {
								$("#email").addClass( "ui-state-error" );
								return;
							}
							$(this).dialog("close");
							dialog.showWait();
							rpc.json.AccessService.resetPassword(
										function(r,e) {
											top.dialog.removeWait();
											if(e) {
												rpc.showError(e);	
											} else {
												if(r) {
													dialog.error('Atención','Le enviamos un correo con su nueva contraseña a '+strEmail,
															function() {
																dialog.showWait();
																top.location.href = '/TVoIP/CallShop/login.html';
															}
														);										        
												}  else {
													dialog.error('Error','El correo '+strEmail+' no se encuentra registrado en nuestro sistema',
															function() {
																dialog.showWait();
																top.location.href = '/TVoIP/CallShop/login.html';
															}
														);																			
												}
									        }
								},strEmail);
						},				 
						"Cancelar": function() { 
							$(this).dialog("close");
						} 
					 } 
		});		
	});
	
});


function errorMsg() {
	dialog.error('Atención','Error de conexión. Presione OK para reconectarse',
			function() {
				dialog.showWait();
				top.location.href = '/TVoIP/CallShop/Login';
			}
		);	
}

function validateEmail(strEmail) { 
	   var strRegExp = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	   return strRegExp.test(strEmail);
}
