	function takeMeToSky(url) {
		var SkyBank=window.open(url, 'SkyBank', 'width=' + (screen.width) + ',height=' + (screen.height-25) + ', scrollbars=auto, resizable=yes,top=0,left=0').focus();
	}
	
	function getValue(id){
		return document.getElementById(id).value;
	}
	
	var attemptCount=1;
	var oldUserName='';
	function loginMe(){
		if(oldUserName!=getValue('username'))
			attemptCount=1;
		oldUserName=getValue('username');
		var login=CreateXHR();
		var uri="GlobalLoginProcess.do";
		login.onreadystatechange = function() {
			if (login.readyState == 4) {
				if (login.status == 200) {
					var mes=eval("("+login.responseText+")");
					showMsg(mes,login.responseText);
					attemptCount+=1;
				}
			}
		}
		var data="dispatch=globleLogin&comId="+getValue('comId')+"&comCode="+getValue('comCode')+"&username="+getValue('username')+"&key="+escape(cry(getValue('password')))+"&atm="+attemptCount+"&rand="+Math.random()*19870808;
		login.open("POST", uri, true);
		login.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
		login.send(data);
	}
	
	function checkMe(){
		if(getValue('comCode')!=undefined && getValue('comCode')!=''){
			var check=CreateXHR();
			var uri="GlobalLoginProcess.do";
			check.onreadystatechange = function() {
				if (check.readyState == 4) {
					if (check.status == 200) {
						var mes=eval("("+check.responseText+")");
						showMsg(mes,check.responseText);
					}
				}
			}
			var data="dispatch=getComCode&field=comCode&code="+getValue('comCode');
			check.open("POST", uri, true);
			check.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
			check.send(data);
		}
	}
	
	document.onkeydown=function(e){
		if(!e && window.event)
		    e=window.event;
		var srcEl = (e.srcElement)?e.srcElement:e.target; 
		if(e.keyCode==13 && document.getElementById('login')){
		   loginMe();
		}
	}
	
	function logoutMe(id){
		if(document.getElementById('logout')){
			var logout=CreateXHR();
			var uri="GlobalLoginProcess.do";
			logout.onreadystatechange = function() {
				if (logout.readyState == 4) {
					if (logout.status == 200) {
						var mes=eval("("+logout.responseText+")");
						if(mes['logout']){
							try{
								logouted();
							}catch(e){
							
							}
						}else if("error"){
							//document.getElementById('secondRow').innerHTML="<font style=\"font-size:8pt\" color=\"red\" >"+mes['error']+"</font>";
							alert(mes["error"]);
						}
					}
				}
			}
			var data="dispatch=logout";
			if(id!=undefined)
				data+="&userId="+id;
			logout.open("POST", uri, true);
			logout.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
			logout.send(data);
		}
	}
	
	function clearSessionOnLoad(){
		var logout=CreateXHR();
		var uri="GlobalLoginProcess.do";
		logout.onreadystatechange = function() {
			if (logout.readyState == 4) {
				if (logout.status == 200) {
				
				}
			}
		}
		var data="dispatch=logout";
		if(id!=undefined)
			data+="&userId="+id;
		logout.open("POST", uri, true);
		logout.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
		logout.send(data);
	}
	
	function callLogout(id){
		setTimeout('logoutMe('+id+')',1);
	}
	
	function showCompanys(){
		clearComDiv();
		clearUserDiv();
		clearPassDiv();
		var com=CreateXHR();
		var uri="GlobalLoginProcess.do";
		com.onreadystatechange = function() {
			if (com.readyState == 4) {
				if (com.status == 200) {
					var mes=eval("("+com.responseText+")");
					if(mes['error']){
						alert(mes['error']);
					}else{
						var divt=document.getElementById('comWindow');
						divt.innerHTML="";
						divt.className="comWindowList";
						var v="<table cellspacing=\"0\" cellpadding=\"0\" width=\"100%\"><tbody>"
						for(var f=0;f<mes.length;f++){
							v+="<tr><td><div id=\""+f+"\" class=\"normal\" onclick=\"setComCode(this)\" onmousemove=\"this.className='on'\" onmouseout=\"this.className='normal'\"><table cellspacing=\"0\" cellpadding=\"0\" class=\"list\"><tbody><tr><td width=\"40px\" ><span id=\"code"+f+"\">"+mes[f]['Com']+"</span></td><td><span id=\"name"+f+"\">"+"-  "+mes[f]['Name']+"&nbsp;&nbsp;&nbsp;</span></td></tr></tbody></table></div></td></tr>";
						}
						v+="</tbody></table>";
						divt.innerHTML=v;
					}
				}
			}
		}
		var data="dispatch=getAllCompanys";
		com.open("POST", uri, true);
		com.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
		com.send(data);
	}
	
	function showMsg(msg,res){
		for(var i=0;i<msg.length;i++){
			if( msg[i]['comCode']){
				document.getElementById('divComCode').innerHTML = msg[i]['comCode'];
				document.getElementById('comCode').value ="";
				document.getElementById('comName').value ="";
				document.getElementById('comId').value ="";
		    }else if( msg[i]['username']){
				document.getElementById('username').value="";
				document.getElementById('divUsername').innerHTML = msg[i]['username'];
			}else if( msg[i]['key']){
				document.getElementById('password').value="";
				document.getElementById('divPassword').innerHTML = msg[i]['key'];
			}else {
				alert(res);
			}
		}
		if( msg['loginFail']){
			document.getElementById('secondRow').innerHTML="<font style=\"font-size:8pt\" color=\"red\" >"+msg['loginFail']+"</font>";
		}else if(msg['invalidattempt']){
			document.getElementById('secondRow').innerHTML="<font style=\"font-size:8pt\" color=\"red\" >"+msg['invalidattempt']+"</font>";
		}else if(msg['officerlocked']){
			document.getElementById('secondRow').innerHTML="<font style=\"font-size:8pt\" color=\"red\" >"+msg['officerlocked']+"</font>";
		}else if( msg['error']){
			if( msg['error']=="record.notFound"){
				document.getElementById('divComCode').innerHTML ="Company Code Not Found";
				document.getElementById('comCode').value ="";
			}else{
				alert(msg['error']);
				document.getElementById('divComCode').innerHTML ="";
				document.getElementById('comCode').value =""; 
				document.getElementById('comName').value =""; 
				document.getElementById('comId').value ="";    
				document.getElementById('username').value="";
				document.getElementById('divUsername').innerHTML =""; 
				document.getElementById('password').value =""; 
				document.getElementById('divPassword').innerHTML ="";   
			}
		}else if(msg['comCode'] && msg['desc']){
			document.getElementById('comCode').value =msg['comCode']; 
			document.getElementById('comName').value =msg['desc']; 
			document.getElementById('comId').value =msg['ID'];    
			document.getElementById('divComCode').innerHTML ='';   
		}else if(msg['Login Status']){
			attemptCount=1;
			var oPVal=escape(cry(getValue('password')));
			welcome(msg['userName'],msg['comName'],msg['brn']);
			if(msg['isFirstLogin']!=''){
				document.getElementById('oP').value=oPVal;
				if(winChangePassword!=undefined){
					winChangePassword.show();
					document.getElementById('nP').focus();
					//setPortals(msg['portals']);
					portals=msg['portals'];
				}
			}else if(msg['showPasswordRenewalMessage']){
				alert(msg['showPasswordRenewalMessage']);
				setPortals(msg['portals']);
			}else{
				setPortals(msg['portals']);
			}
			//takeMeToSky('TreeAction.do?dispatch=systemTree');
			attemptCount=1;
		}
	}
	var portals;
	
	function setComCode(itm){
		var id=itm.id;
		document.getElementById('comCode').value=document.getElementById('code'+id).innerHTML;
		clearList();
		checkMe();
	}
	
	function upperCase(id){
		document.getElementById(id).value=document.getElementById(id).value.toUpperCase();
	}
	
	function clearThis(id){
		document.getElementById(id).value="";	
	}
	
	function clearComDiv(){
		document.getElementById('divComCode').innerHTML="";
		document.getElementById('secondRow').innerHTML='';
	}  
	
	function clearUserDiv(){
		document.getElementById('divUsername').innerHTML="";
		document.getElementById('secondRow').innerHTML='';
	}  
	
	function clearPassDiv(){
		document.getElementById('divPassword').innerHTML="";
		document.getElementById('secondRow').innerHTML='';
	}
	
	function setTime(func){
		if(func=='comList'){
			setTimeout('clearList();',200);
		}
	}
	function cry(v){var str="";for(var i=0;i<v.length;i++){str+=v.charCodeAt(i++).toString(16);}for(var i=1;i<v.length;i++){str+=v.charCodeAt(i++).toString(16);}return str;}
	function clearList(){
		document.getElementById('comWindow').innerHTML="";
		document.getElementById('comWindow').className="comWindow";
	}
	
	function welcome(userName,comName,brns){
		var welcomeMsg="<table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><tbody><tr ><td class=\"textTd\"><b>"+userName+"</b></td><td width=\"5px\"></td><td align=\"right\"><input type=\"button\" value=\"Logout\" class=\"btn\" id=\"logout\" name=\"logout\" onclick=\"setPortals('');logoutMe();\" /></td></tr></tbody></table>";
		document.getElementById('firstRow').innerHTML=welcomeMsg;
		welcomeMsg="<table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><tbody><tr ><td class=\"textTd\"><b>Welcome to "+comName+"  "+"</b></td><td width=\"5px\"></td></tr></tbody></table>"
		document.getElementById('secondRow').innerHTML='';
		document.getElementById('secondRow').innerHTML=welcomeMsg;
	}
	
	function logouted(){
		var txt="<table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><tbody><tr ><td class=\"textTd\" >Company</td><td width=\"5px\"></td><td><input type=\"hidden\" id=\"comId\" name=\"comId\" value=\"\" /></td><td width=\"3px\"></td><td><input type=\"text\" tabindex=\"1\" class=\"textField\" name=\"comCode\" id=\"comCode\" maxlength=\"3\" size=\"5\" value=\"\"  onfocus=\"clearComDiv();\" onblur=\"upperCase('comCode');checkMe();\" onkeyup=\"upperCase('comCode');\" /><br><div class=\"validate\" id=\"divComCode\"></div><div class=\"comWindow\" id=\"comWindow\" style=\"overflow:auto;\"></div><input type=\"hidden\" class=\"textField\" name=\"comName\" id=\"comName\" value=\"\" /></td><td><input type=\"button\" tabindex=\"-1\" class=\"downArrow\" name=\"comBrowse\" id=\"comBrowse\" value=\"\" onclick=\"showCompanys();\" /></td><td width=\"5px\"></td><td class=\"textTd\" >User Name</td><td width=\"5px\"></td><td><input type=\"text\" tabindex=\"2\" class=\"textField\" name=\"username\" id=\"username\" value=\"\" size=\"15\" maxlength=\"20\" onfocus=\"clearUserDiv();\" /><br><div class=\"validate\" id=\"divUsername\"></div></td><td width=\"5px\"></td><td class=\"textTd\" align=\"center\">Password</td><td width=\"5px\"></td><td><input type=\"password\" tabindex=\"3\" class=\"textField\" name=\"password\" id=\"password\" size=\"15\" onfocus=\"clearPassDiv();\" /><br><div class=\"validate\" id=\"divPassword\"></div></td><td width=\"5px\"></td><td align=\"right\"><input type=\"button\" tabindex=\"4\" class=\"btn\" name=\"login\" id=\"login\" value=\"Login\" onmouseup=\"loginMe();\" /></td></tr></tbody></table>";
		document.getElementById('firstRow').innerHTML=txt;
		document.getElementById('secondRow').innerHTML='';
//		clearAllDivs();
	}
	
	function setPortals(list){
		var corporates=document.getElementById('Corporates');
		var Supplies=document.getElementById('Supplies');
		var MarketingAgents=document.getElementById('MarketingAgents');
		var InsuranceBrokers=document.getElementById('InsuranceBrokers');
		var Financiers=document.getElementById('Financiers');
		var Clients=document.getElementById('Clients');
		var types=list.split(",");
		if(list.indexOf('OFF')>-1){
			corporates.onclick=function(){takeMeToSky('TreeAction.do?dispatch=systemTree&rand='+Math.random()*99999);};
			corporates.className='portal';
			takeMeToSky('TreeAction.do?dispatch=systemTree&rand='+Math.random()*99999);
		}else{
			corporates.onclick=function(){};
			corporates.className='noAccess';
		}
		if(list.indexOf('SUP')>-1){
			Supplies.className='portal';
		}else {
			Supplies.className='noAccess';
		}
		if(list.indexOf('BRO')>-1){
			InsuranceBrokers.className='portal';
		}else {
			InsuranceBrokers.className='noAccess';
		}
		if(list.indexOf('AGN')>-1){
			MarketingAgents.className='portal';
		}else {
			MarketingAgents.className='noAccess';
		}
		if(list.indexOf('FNC')>-1){
			Financiers.className='portal';
		}else{
			Financiers.className='noAccess';
		}
		if(list.indexOf('CLI')>-1){
			Clients.className='portal';
		}else {
			Clients.className='noAccess';
		}
	}
	
	function submitPassword(){
		var oP=getValue('oP');
		var nP=escape(cry(getValue('nP')));
		var cP=escape(cry(getValue('cP')));
		var data='dispatch=changePassword&oP='+oP+'&nP='+nP+'&cP='+cP+'&rand='+Math.random()*1987;
		var mySearchRequest = new ajaxObject("GlobalLoginProcess.do");
		mySearchRequest.callback = function(responseText, responseStatus, responseXML) {
			if (responseStatus==200) {
				var message =  eval('(' + responseText + ')');
       			if(message['error']){
           	 		document.getElementById('divError').innerHTML=message['error'];
           	 	}else if(message['updateSuccess']){
           	 		alert(message['updateSuccess']);
           	 		winChangePassword.hide();
           	 		setPortals(portals);
           	 		clearPassword();
           	 	}else{
					for(var i=0;i<message.length;i++){
						if(message[i]['nP'])
							document.getElementById('divError').innerHTML=message[i]['nP'];
						else if(message[i]['cP'])
							document.getElementById('divError').innerHTML=message[i]['cP'];
					}
       			}
			}
		}
		mySearchRequest.update(data,'POST');
	}
	
	function clearPassword(){
		document.getElementById('nP').value='';
		document.getElementById('cP').value='';
		document.getElementById('divError').innerHTML='';
	}
	
	function clearAllDivs(){
		document.getElementById('CorporateDiv').className="enable";
		document.getElementById('SuppliesDiv').className="enable";
		document.getElementById('InsuranceBrokersDiv').className="enable";
		document.getElementById('MarketingAgentsDiv').className="enable";
		document.getElementById('FinanciersDiv').className="enable";
		document.getElementById('ClientsDiv').className="enable";
	}
	
	function executeFunc(func){
		func();
		//alert(func);
	}
	
	function unSubscribe(companyId,userId){
		amq.removeListener(companyId+'.'+userId+'.queue','channel://'+companyId+'.'+userId);
	}
	
	function clearWebClient(){
		var client=CreateXHR();
		var uri="GlobalLoginProcess.do";
		client.onreadystatechange = function() {
			if (client.readyState == 4) {
				if (client.status == 200) {
					
				}
			}
		}
		var data="dispatch=clearJMSClient";
		client.open("POST", uri, true);
		client.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
		client.send(data);
	}
	function callClearWebClient(){setTimeout('clearWebClient()',10);}
