// JavaScript Document
var onFocusUsr = true;
var onFocusPsw = true;
var response = null;
var screenW = '';
var screenH = '';
//var mobileUrl = 'mobile.visinnova.com';
var mobileUrl = 'http://www.illocalizzatore.com/mobileHome.htm';
var mobileFailUrl = 'http://www.illocalizzatore.com/mobileFail.htm';

if(screen){
  screenW =  screen.width;
  screenH = screen.height;
}

if(screenW < 900 && location.href != mobileUrl) window.location = mobileUrl;

function setUsername(obj){
  if(onFocusUsr){
    obj.value = "";
    onFocusUsr = false;
  }
}

function setPassword(obj){
  if(onFocusPsw){
    obj.value = "";
    onFocusPsw = false;
  }
}

function showFocus(obj){
  if(!document.getElementById) return;
  obj.style.border = "1px solid #FFCC66";
}

function showBlur(obj){
  if(!document.getElementById) return;
  //obj.style.border = "1px solid #6699CC";
	obj.style.border = "1px solid #cccccc";
} 

var LoginMgr = {
  labelId: '',
  successText: null,
  faultText: '<font color="#C00"><b>Nome utente o password errati</b></font>',
  checkingText: '<b>VERIFICA...</b>',
  warnText: '<b>Inserire nome utente e password</b>', 
  sizeTxt: 'Attenzione! L\'applicazione richiede una risoluzione dello schermo di almenno 1280x760 pixel.',
  
  login: function(id,txt){
    this.labelId = id;
    this.successText = '<b>'+txt+'</b>' || null;
    var usr = document.LoginUsr.username.value;
    var pwd = document.LoginPwd.password.value;
    var qry = 'http://gestioneflotta.visonline.it/sso/login?'; 
    if(usr != '' && pwd != ''){
	  if(screenW < 1280 && location.href != mobileUrl) alert(this.sizeTxt);
	  document.getElementById(this.labelId).innerHTML = this.checkingText;
	  if(location.href == mobileUrl){
        var fail = escape(mobileFailUrl);
        qry+='username='+usr+'&password='+pwd+'&serviceID=1&output=application&urlKO='+fail+'&rnd='+Math.floor(Math.random()*1000);
	    window.location = qry;		  
	  }else{
        qry+='username='+usr+'&password='+pwd+'&serviceID=1&output=ajax&rnd='+Math.floor(Math.random()*1000);
        this.checkScript(qry);
	  }
      onFocusUsr = true;
      onFocusPsw = true;
    }else{
	  document.getElementById(this.labelId).innerHTML = this.warnText;
	}
  },
  
  checkScript: function(qry){
    var elements = document.getElementsByTagName('script');
    var i, js, patternIdx;
    for(i = 0; i < elements.length; i++) {
      if(!elements[i].src) continue;
      src = elements[i].src;
      patternIdx = src.lastIndexOf('login?');
      if(patternIdx >-1){
        js = elements[i]; 
        document.getElementsByTagName('head')[0].removeChild(js);        
        break;
      }
    }
    this.addScriptToDOM(qry);
  },

  addScriptToDOM: function(src) {
    var js = document.createElement('script');
    js.type = 'text/javascript';
    js.src = src;
    var head = document.getElementsByTagName('head')[0];
    head.appendChild(js);
  },
  
  parseResponse: function(){
    try{
      if(response.error){
        document.getElementById(this.labelId).innerHTML = this.faultText;
      }else{
        document.getElementById(this.labelId).innerHTML = this.successText ? this.successText : '';
        var width = screen.width-40, height = screen.height-40;
        var left = 20;
        var top = 20;
        var styleStr = 
          'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbar=no,resizable=yes,copyhistory=yes,'+
          'width='+width+','+
	      'height='+height+','+
	      'left='+left+',top='+top+',screenX='+left+',screenY='+top;
		var url = response.link+'&screenWidth='+screenW+'&screenHeight='+screenH+'&loginSite='+location.href;
        if(screenW < 1024) window.location = url;
		else newWindow = window.open(url,'VFlotte',styleStr);
      }
    }catch(e){
      alert('ex: '+e.message);
    }   
  }
}


