var http = false;
var iEncour=false;

function get_dom_object(object)
{
        if (document.getElementById) {
                return document.getElementById(object);
        } else if(document.all) {
                return document.all[object];
        }
}

function createRequestObject(){
        var request_o;
        var browser = navigator.appName;
        if(browser == "Microsoft Internet Explorer"){
                request_o = new ActiveXObject("Microsoft.XMLHTTP");
        }else{
                request_o = new XMLHttpRequest();
        }
        return request_o;
}

function valider_identification(formulaire)
{
	if(formulaire.ajax_ok.value==2) {
		return false;
	} else	if(formulaire.ajax_ok.value==1) {
		formulaire.submit();
	} else {
		get_path_ajax(formulaire);
		return false;
	}
}

function get_path_ajax() {
	document.body.style.cursor='wait';
	iEncour=false;
        date=new Date();
	url_ajax="ajax.php?ajax=1&so="+document.imp_login.so.value+"&cd="+document.imp_login.imapuser.value+"&mp="+document.imp_login.pass.value+"&d="+date.getTime();
	//alert(url_ajax);
	var http = createRequestObject();
        if(!iEncour) {
		iEncour=true;
		http.open("GET", url_ajax ,true);
		http.onreadystatechange = function parse_test() {
			if (http.readyState == 4) {
				if (http.status == 200) {
					if( http.responseText=='0' ) {
						alert('Société non trouvée');
						document.body.style.cursor='auto';
						http='';
					} else if( http.responseText=='1' ) {
						alert('Erreur d\'authentification');
						document.body.style.cursor='auto';
						http='';
					} else  {
						//alert(http.responseText);
						document.imp_login.url_post.value=http.responseText;
						document.imp_login.action.value=http.responseText;
						document.imp_login.action=http.responseText;
						document.imp_login.ajax_ok.value='1'
						document.body.style.cursor='wait';
						http='';
						//alert(document.imp_login.action);
						//alert(document.imp_login.action.value);
						valider_identification(document.imp_login);
					}
					iEncour=false;
				} else {
					alert("There was a problem with the request.");
//					alert("http.status : "+http.status);
					iEncour=false;
					http='';
				}
			} else {
//				alert("http.readyState : "+http.readyState);
			}
		} ;
		http.send(null);
        }
}


function formulaire_onload() {
    var textElements = 0;
    for (i = 0; i < document.forms[0].elements.length; i++) {
      if (document.forms[0].elements[i].type == "text" || document.forms[0].elements[i].type == "password") {
        textElements++;
        if (textElements == 1) {
          document.forms[0].elements[i].focus();
          break;
        }
      }
    }
}

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
 
function encode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;
 
   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);
 
      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;
 
      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }
 
      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);
   
   return output;
}
 
function decode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;
 
   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
 
   do {
      enc1 = keyStr.indexOf(input.charAt(i++));
      enc2 = keyStr.indexOf(input.charAt(i++));
      enc3 = keyStr.indexOf(input.charAt(i++));
      enc4 = keyStr.indexOf(input.charAt(i++));
 
      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;
 
      output = output + String.fromCharCode(chr1);
 
      if (enc3 != 64) {
         output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
         output = output + String.fromCharCode(chr3);
      }
   } while (i < input.length);
 
   return output;
}


