var xmlHttp;

function GetXmlHttpObject()
{
/*
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
*/
    if (typeof XMLHttpRequest != "undefined") {
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
      var aVersions = [ "MSXML2.XMLHttp.5.0",
        "MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0",
        "MSXML2.XMLHttp","Microsoft.XMLHttp"
      ];

      for (var i = 0; i < aVersions.length; i++) {
        try {
            return new ActiveXObject(aVersions[i]);
        } catch (oError) {
            //Do nothing
        }
      }
    }
  return null
}

function isEmpty(s) {
  return ((s == null) || (s.length == 0));
}

function checkData() {
  if (0 == document.getElementById("type").selectedIndex) {
    alert("Please select type of account");
    return false;
  }
  if (document.getElementById("check").value == "true") {
    if (isEmpty(document.getElementById("login").value)) {
      alert("Please enter your login");
      return false;
    }
    if (isEmpty(document.getElementById("pass").value)) {
      alert("Please enter your password");
      return false;
    }
  }
  return true;
}

function hideAuth() {
  document.getElementById("trLogin").style.display = "none";
  document.getElementById("trPassword").style.display = "none";
  document.getElementById("importButton").disabled = true;
}

function showAuth() {
  var IE='\v'=='v';
  var displayValue = "table-row";
  if (IE) {
  	displayValue = "block";
  }
  document.getElementById("trLogin").style.display = displayValue;
  document.getElementById("trPassword").style.display = displayValue;
  document.getElementById("importButton").disabled = false;
}

function checkAuth() {
  var typeElement = document.getElementById("type");
  //hide also for Yahoo users
  if (0 == typeElement.selectedIndex || 6 == typeElement.selectedIndex) {
    hideAuth();
  } else {
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null) {
      alert ("Browser does not support HTTP Request");
      return;
    }
    var url = "checkauthxml.jsp";
    url = url + "?type=" + typeElement.options[typeElement.selectedIndex].value;
    url = url + "&sid="+Math.random();
    xmlHttp.onreadystatechange = checkAuthHandler ;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
  }
}

function checkAuthHandler() {
  if (xmlHttp.readyState == 4 || xmlHttp.readyState=="complete") {
		xmlDoc=xmlHttp.responseXML;
		var result = ("true" == xmlDoc.getElementsByTagName("result")[0].childNodes[0].nodeValue);
    if (result) {
      hideAuth();
      document.getElementById("check").value = "false";
    } else {
      showAuth();
      document.getElementById("check").value = "true";
    }
  }
}


