function checkMessages(tbTitle,w,h,options){
	messageElement 	= $("messages");
	Rex 			= /[a-zA-Z0-9]/;
	options			= options || {};
	
	if(messageElement &&  (messageElement.getChildren().length > 0 || messageElement.get("html").test(Rex) != "" ) ) {
		messageElement.MooDialog({
			onHide:function(){
				if(options.redirect != undefined && options.redirect != "") window.location.href = options.redirect;
			},
			drag:true,
			title:tbTitle||'System message',
			size:{width:w||400,height:h||150}
		});
	}
}

function setAutoFillFields(){
	$$("input.autofill,textarea.autofill").each(function(item){
		item.store('val',item.value);
		item.addEvent('focus',function(){
			if(item.value == item.retrieve('val')) item.value = '';
		});
		item.addEvent('blur',function(){
			if(item.value == '') item.value = item.retrieve('val');
		});
	});
}

function login(flag){
	var objReq 	= (!flag) ? {logout:''} : {login_u:$("login_u").value,login_p:$("login_p").value};
	new Request.JSON({
			url: rootPath+"login/login.php", 
			onComplete:function(response){
				if(response == null) {
					alert('A Technical Error has occured! Please try again');
					return false;
				}

				if(!response.status) {
					TB_RedirectUrl = '';
					if($("messages") == null) document.body.adopt(new Element("div",{id:'messages'}));
					$("messages").set("html",response.message);
					checkMessages('Login');
				} else location.href = location.href;
			}
			}).get(objReq);
}

function lostPwd(lang){
	new MooDialog.Iframe(rootPath+'_dialogs/lostpwd/index.php?lang='+lang,{
		title: 'Password',
		size: {
			width: 550,
			height: 300
		}
	});
}

function downloadAttachment(dPath,custom){
	var reqObj = {cmd:'chklogin'};
	new Request.JSON({
			url: rootPath+ "_ajax/index.php", 
			onComplete:function(response){
				if(response == null) {
					alert('A Technical Error has occured! Please try again');
					return false;
				}

				if(!response.status) {
					TB_RedirectUrl = '';
					if($("messages") == null) document.body.adopt(new Element("div",{id:'messages'}));
					$("messages").set("html",response.message);
					checkMessages('Login Required');
				} else location.href = rootPath+"download.php?f="+dPath;
			}
			}).get(reqObj);
}
