function manualSelect()
{
    var manualVal = document.getElementById("selectlogin").value
    var manualSplit = manualVal.split(";")
    
    switch(manualSplit[0])
    {
        case "Win SA Hosting CP":
            document.getElementById("domainspan").style.display = "";
            break;
        case "Win US Hosting CP":
            document.getElementById("domainspan").style.display = "";
            break;
        default:
            document.getElementById("domainspan").style.display = "none";
    }
}

function errCheck()
{
    var go = true
    
    if((document.getElementById("globalusername").value == "") || (document.getElementById("globalpassword").value == ""))
    {
        go = false
    }
    
    if(document.getElementById("domainspan").style.display == "")
    {
        if(document.getElementById("globaldomain").value == "")
            go = false
    }
    
    if(!go)
    {
        document.getElementById("invalidtd").innerHTML = "Please make sure you have filled in all textboxes."
        document.getElementById("invalidspan").style.display = "";
    }
    else
        loginTypeCheck()
        
    return false;
}

function loginTypeCheck()
{
    if (document.getElementById("selectlogin").value != "0" && document.getElementById("manselectloginspan").style.display != "none")
        doManual();
    else
        doAuto();
}

function doManual()
{
    var manualVal = document.getElementById("selectlogin").value
    var manualSplit = manualVal.split(";")
    var loginUrl;
    
    switch(manualSplit[0])
    {
        case "Win SA Hosting CP":
            var domain = document.getElementById("globaldomain").value;
            loginUrl = manualSplit[1].replace(/<domain>/, domain);
            ProcessGoUrl(loginUrl);
            break;
        case "Win US Hosting CP":
            var domain = document.getElementById("globaldomain").value;
            loginUrl = manualSplit[1].replace(/<domain>/, domain);
            ProcessGoUrl(loginUrl);
            break;
        case "Linux US CP":
            loginUrl = manualSplit[1].replace(/<domain>/, document.getElementById("globalusername").value);
            ProcessGoUrl(loginUrl);
            break;
        case "Linux SA CP":
            loginUrl = manualSplit[1].replace(/<domain>/, document.getElementById("globalusername").value);
            ProcessGoUrl(loginUrl);
            break;
        case "WebMail":
            var emailAddr = document.getElementById("globalusername").value;
            var emailSplit = emailAddr.split("@");
            loginUrl = manualSplit[1].replace(/<domain>/, emailSplit[1]);
            ProcessGoUrl(loginUrl);
            break;
        default:
            ProcessGoUrl(manualSplit[1]);
    }
}

function doAuto()
{
    document.getElementById("manselectloginspan").style.display = "none";
    
    var username = document.getElementById("globalusername").value
    
    var dslConsoleRegex = /.+@wadsl[\..+]?/;
    var emailRegex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    
    var IsMatchDSL = dslConsoleRegex.test(username)
    var IsMatchEmail = emailRegex.test(username)
    
    if(IsMatchDSL)
        ProcessGoUrl("http://dsl.gonline.co.za/?p=<password>&u=<username>");
    else if(IsMatchEmail)
    {
        var loginSplit = username.split("@");
        var domain = loginSplit[1];
        ProcessGoUrl("http://webmail." + domain + "/login.aspx?email=<username>&password=<password>");
    }
    else
        getLoginArea()
}

function getLoginArea()
  {
  document.getElementById("invalidspan").style.display = "none";
  document.getElementById("loadingspan").style.display = "none";
  document.getElementById("selectloginspan").style.display = "none";
  
  var xmlHttp;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      try
        {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      catch (e)
        {
        alert("Your browser does not support AJAX!");
        return false;
        }
      }
    }
    
    xmlHttp.onreadystatechange=function()
      {
      if(xmlHttp.readyState==4)
        {
            var loginArr = eval(xmlHttp.responseText)
            
            var numLogins = loginArr.length
            
            switch(numLogins)
            {
                case 0:
                    document.getElementById("invalidtd").innerHTML = "Unrecognised username and password."
                    document.getElementById("invalidspan").style.display = "";
                    break
                case 1:
                    var loginurl = loginArr[0]["url"];
                    ProcessGoUrl(loginurl)
                    break
                default:
                    var selectloginspanHTML = "<select name='selectlogin' onchange='ProcessGoUrl(this.value)' style='background-color:White'><option value='0' selected>Please Select...</option>"
                    
                    for(x in loginArr)
                    {
                        selectloginspanHTML = selectloginspanHTML + "<option value='" + loginArr[x]["url"] + "'>" + loginArr[x]["loginAreaName"]
                    }
                    
                    selectloginspanHTML = selectloginspanHTML + "</select>";
                    
                    document.getElementById("selectlogintd").innerHTML = selectloginspanHTML
                    
                    document.getElementById("selectloginspan").style.display = "";
            }
            
            document.getElementById("loadingspan").style.display = "none"
        }
        else
            document.getElementById("loadingspan").style.display = ""
      }
    
    var url = "/ajaxgloballogin/default.aspx"
    var param = "login=" + document.getElementById("globalusername").value + "&password=" + document.getElementById("globalpassword").value;
    
    xmlHttp.open("POST", url, true);
    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttp.send(param);
  }
  
  function ProcessGoUrl(url)
  {
    var loginurl = url.replace(/<username>/, document.getElementById("globalusername").value);
    loginurl = loginurl.replace(/<password>/, document.getElementById("globalpassword").value);
    
    var urlArr = loginurl.split("?")
                    
    var loginform = document.getElementById("globloginform")
    var formparams = document.getElementById("formparams")
    
    formparams.innerHTML = "";
    loginform.action = urlArr[0]
    var urlParamArr = urlArr[1].split("&")
    
    var paramSplit
    for(x in urlParamArr)
    {
        paramSplit = urlParamArr[x].split("=")
        
        formparams.innerHTML = formparams.innerHTML + "<input type='hidden' name='" + paramSplit[0] + "' value='" + paramSplit[1] + "'>"
    }
    loginform.submit();
  }
  
  function toggleManual()
  {
    if(document.getElementById("manselectloginspan").style.display=="none")
    {
        document.getElementById("manselectloginspan").style.display="";
        document.getElementById("logintypecheck").checked = 0;
        document.getElementById("selectloginspan").style.display="none";
    }
    else
    {
        document.getElementById("manselectloginspan").style.display="none";
        document.getElementById("domainspan").style.display="none";
        document.getElementById("logintypecheck").checked = 1;
    }
  }