var webmailUrl = new Array();
webmailUrl['horde'] = new Array();
webmailUrl['horde']['pl'] = new Array();
webmailUrl['horde']['pl']['ssl']   = 'https://ssl0.ovh.net/horde/imp/redirect.php'
webmailUrl['horde']['pl']['plain'] = 'http://imp.ovh.pl/redirect.php';
webmailUrl['horde']['de'] = new Array();
webmailUrl['horde']['de']['ssl']   = 'https://ssl0.ovh.net/horde/imp/redirect.php';
webmailUrl['horde']['de']['plain'] = 'http://imp.ovh.de/redirect.php';
webmailUrl['horde']['fr'] = new Array();
webmailUrl['horde']['fr']['ssl']   = 'https://ssl0.ovh.net/horde/imp/redirect.php';
webmailUrl['horde']['fr']['plain'] = 'http://imp.ovh.net/redirect.php';
webmailUrl['horde']['es'] = new Array();
webmailUrl['horde']['es']['ssl']   = 'https://ssl0.ovh.net/horde/imp/redirect.php';
webmailUrl['horde']['es']['plain'] = 'http://imp.ovh.es/redirect.php';
webmailUrl['horde']['en'] = new Array();
webmailUrl['horde']['en']['ssl']   = 'https://ssl0.ovh.net/horde/imp/redirect.php';
webmailUrl['horde']['en']['plain'] = 'http://imp.ovh.co.uk/redirect.php';

webmailUrl['atmail'] = new Array();
webmailUrl['atmail']['pl'] = new Array();
webmailUrl['atmail']['pl']['ssl']   = 'https://ssl0.ovh.net/atmail/atmail.php';
webmailUrl['atmail']['pl']['plain'] = 'http://atmail.ovh.pl/atmail.php';
webmailUrl['atmail']['de'] = new Array();
webmailUrl['atmail']['de']['ssl']   = 'https://ssl0.ovh.net/atmail/atmail.php';
webmailUrl['atmail']['de']['plain'] = 'http://ns0.ovh.net/atmail/atmail.php';
webmailUrl['atmail']['fr'] = new Array();
webmailUrl['atmail']['fr']['ssl']   = 'https://ssl0.ovh.net/atmail/atmail.php';
webmailUrl['atmail']['fr']['plain'] = 'http://ns0.ovh.net/atmail/atmail.php';
webmailUrl['atmail']['es'] = new Array();
webmailUrl['atmail']['es']['ssl']   = 'https://ssl0.ovh.net/atmail/atmail.php';
webmailUrl['atmail']['es']['plain'] = 'http://ns0.ovh.net/atmail/atmail.php';
webmailUrl['atmail']['en'] = new Array();
webmailUrl['atmail']['en']['ssl']   = 'https://ssl0.ovh.net/atmail/atmail.php';
webmailUrl['atmail']['en']['plain'] = 'http://ns0.ovh.net/atmail/atmail.php';

webmailUrl['ilohamail'] = new Array();
webmailUrl['ilohamail']['pl'] = new Array();
webmailUrl['ilohamail']['pl']['ssl']   = 'https://ssl0.ovh.net/IlohaMail/source/index.php';
webmailUrl['ilohamail']['pl']['plain'] = 'http://ilohamail.ovh.pl/index.php';
webmailUrl['ilohamail']['de'] = new Array();
webmailUrl['ilohamail']['de']['ssl']   = 'https://ssl0.ovh.net/IlohaMail/source/index.php';
webmailUrl['ilohamail']['de']['plain'] = 'http://ilohamail.ovh.de/index.php';
webmailUrl['ilohamail']['fr'] = new Array();
webmailUrl['ilohamail']['fr']['ssl']   = 'https://ssl0.ovh.net/IlohaMail/source/index.php';
webmailUrl['ilohamail']['fr']['plain'] = 'http://ilohamail.ovh.fr/index.php';
webmailUrl['ilohamail']['es'] = new Array();
webmailUrl['ilohamail']['es']['ssl']   = 'https://ssl0.ovh.net/IlohaMail/source/index.php';
webmailUrl['ilohamail']['es']['plain'] = 'http://ilohamail.ovh.es/index.php';
webmailUrl['ilohamail']['en'] = new Array();
webmailUrl['ilohamail']['en']['ssl']   = 'https://ssl0.ovh.net/IlohaMail/source/index.php';
webmailUrl['ilohamail']['en']['plain'] = 'http://ilohamail.ovh.co.uk/index.php';

webmailUrl['roundcube'] = new Array();
webmailUrl['roundcube']['pl'] = new Array();
webmailUrl['roundcube']['pl']['ssl']   = 'https://ssl0.ovh.net/roundcube/index.php';
webmailUrl['roundcube']['pl']['plain'] = 'http://roundcube.ovh.pl/';
webmailUrl['roundcube']['de'] = new Array();
webmailUrl['roundcube']['de']['ssl']   = 'https://ssl0.ovh.net/roundcube/index.php';
webmailUrl['roundcube']['de']['plain'] = 'http://ns0.ovh.net/roundcube/index.php';
webmailUrl['roundcube']['fr'] = new Array();
webmailUrl['roundcube']['fr']['ssl']   = 'https://ssl0.ovh.net/roundcube/index.php';
webmailUrl['roundcube']['fr']['plain'] = 'http://ns0.ovh.net/roundcube/index.php'; 
webmailUrl['roundcube']['es'] = new Array();
webmailUrl['roundcube']['es']['ssl']   = 'https://ssl0.ovh.net/roundcube/index.php';
webmailUrl['roundcube']['es']['plain'] = 'http://ns0.ovh.net/roundcube/index.php';
webmailUrl['roundcube']['en'] = new Array();
webmailUrl['roundcube']['en']['ssl']   = 'https://ssl0.ovh.net/roundcube/index.php';
webmailUrl['roundcube']['en']['plain'] = 'http://ns0.ovh.net/roundcube/index.php'; 

webmailUrl['squirrelmail'] = new Array();
webmailUrl['squirrelmail']['pl'] = new Array();
webmailUrl['squirrelmail']['pl']['ssl']   = 'https://ssl0.ovh.net/squirrelmail/src/redirect.php';
webmailUrl['squirrelmail']['pl']['plain'] = 'http://ns0.ovh.net/squirrelmail/src/redirect.php';
webmailUrl['squirrelmail']['de'] = new Array();
webmailUrl['squirrelmail']['de']['ssl']   = 'https://ssl0.ovh.net/squirrelmail/src/redirect.php';
webmailUrl['squirrelmail']['de']['plain'] = 'http://ns0.ovh.net/squirrelmail/src/redirect.php';
webmailUrl['squirrelmail']['fr'] = new Array();
webmailUrl['squirrelmail']['fr']['ssl']   = 'https://ssl0.ovh.net/squirrelmail/src/redirect.php';
webmailUrl['squirrelmail']['fr']['plain'] = 'http://ns0.ovh.net/squirrelmail/src/redirect.php'; 
webmailUrl['squirrelmail']['es'] = new Array();
webmailUrl['squirrelmail']['es']['ssl']   = 'https://ssl0.ovh.net/squirrelmail/src/redirect.php';
webmailUrl['squirrelmail']['es']['plain'] = 'http://ns0.ovh.net/squirrelmail/src/redirect.php';
webmailUrl['squirrelmail']['en'] = new Array();
webmailUrl['squirrelmail']['en']['ssl']   = 'https://ssl0.ovh.net/squirrelmail/src/redirect.php';
webmailUrl['squirrelmail']['en']['plain'] = 'http://ns0.ovh.net/squirrelmail/src/redirect.php';

var emailError = Array();
emailError['pl'] = 'Błędna składnia adresu e-mail';
emailError['de'] = 'Die Email-Adresse wurde nicht korrekt eingegeben';
emailError['fr'] = "Syntaxe d'adresse email incorrecte";
emailError['es'] = 'La sintaxis de la dirección email es incorrecta';
emailError['en'] = 'Bad e-mail syntax';

function GotoWebmail() {

var ifSSL = document.getElementById('secure_login');
var method = '';
// fetch language information from 
var lang  = document.getElementById('int_lang').value;

// array for atmail lang selection
var langArray = Array();
langArray['pl'] = 'polish';
langArray['de'] = 'german';
langArray['fr'] = 'french';
langArray['es'] = 'espanol';
langArray['en'] = 'english';

// check if secure channel should be used
if(ifSSL.checked == true){
	method = 'ssl';
} else {
	method = 'plain';
}

var username = document.getElementById('webmail_username').value;
if(!checkMail(username))
{
     return false;
}
var webmailType = document.getElementById('webmailType').value;

//alert(webmailType);
document.getElementById('logon').action = webmailUrl[webmailType][lang][method];

//alert(document.getElementById('logon').action);

  switch(webmailType) {
    case "horde":
      document.getElementById('imapuser').value=document.getElementById('webmail_username').value;
      document.getElementById('pass').value=document.getElementById('password').value;
      document.getElementById('new_lang').value=document.getElementById('new_lang').value;
//      document.getElementById('submit').click();
     // document.getElementById('logon').submit;
    break;
    case "squirrelmail":
      document.getElementById('login_username').value=document.getElementById('webmail_username').value;
      document.getElementById('secretkey').value=document.getElementById('password').value;
     // document.getElementById('submit').click();
  //    document.getElementById('logon').submit;
    break;
    case "ilohamail":
      document.getElementById('user').value=document.getElementById('webmail_username').value;
      document.getElementById('password').value=document.getElementById('password').value;
      document.getElementById('new_lang').value=document.getElementById('new_lang').value;   
     // document.getElementById('submit').click();
  //    document.getElementById('logon').submit;
    break;
    case "roundcube":
      document.getElementById('_user').value=document.getElementById('webmail_username').value;
      document.getElementById('_pass').value=document.getElementById('password').value;
     // document.getElementById('submit').click();
  //    document.getElementById('logon').submit;
    break;
    case "atmail":
      if(username.indexOf('@') == -1){
	      var user_array = username.split('%');
      } else {
	      var user_array = username.split('@');
      }
      document.getElementById('username').value=user_array[0];
      document.getElementById('pop3host').value=user_array[1];
      document.getElementById('MailType').value='imap';
      document.getElementById('NewWindow').value='1';
      document.getElementById('Language').value=langArray[lang];
   //   document.getElementById('logon').submit;
    break;
  }
}

function checkMail(emailString)
{
	//alert('email: '+emailString);
	var webmailLang=document.getElementById('int_lang').value;
	emailRegExp = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*(@|%)[a-z0-9-]+(\.[a-z0-9-]+)*(\.([a-z]){2,6})$/;
	if(emailRegExp.test(emailString)){
		return true;
	}else{
		alert(emailError[webmailLang]);
		return false;
	}
}

//
// CONFIG
//
// id of default webmail
var webmailDefault = "roundcube";

var messages = new Array();
messages['horde']        = 'Horde IMP';
messages['squirrelmail'] = 'Squirrelmail';
messages['atmail']       = 'Atmail';
messages['roundcube']    = 'Roundcube';
messages['ilohamail']    = 'Ilohamail';

var sources = new Array();
sources['horde']        = 'horde3_icon.png';
sources['squirrelmail'] = 'squirrelmail_icon.jpg';
sources['atmail']       = 'atmail_icon.gif';
sources['roundcube']    = 'roundcube_icon.png';
sources['ilohamail']    = 'ilohamail_icon.jpg';

var sources_fade = new Array();
sources_fade['horde']        = 'horde3_icon_fade.png';
sources_fade['squirrelmail'] = 'squirrelmail_icon_fade.png';
sources_fade['atmail']       = 'atmail_icon_fade.png';
sources_fade['roundcube']    = 'roundcube_icon_fade.png';
sources_fade['ilohamail']    = 'ilohamail_icon_fade.png';


function showMessage(webmail) {
	var webMails = getElementsByName_iefix('img',"webButton");
	for (var i=0; i < webMails.length; i++) {
	  //  webMails[i].className = "button";
	    var web_id = webMails[i].id;
        webMails[i].src = 'gfx/' + sources_fade[web_id];
        
		if(webMails[i].id == webmail)
		{
			webMails[i].src = 'gfx/' + sources[web_id];
		}
	}	
}	

// back to default highlight when mouse leave button
function turnOfButton() {
	showMessage(document.getElementById('interface').value);	
}	

var BrowserVer = new BrowserVerChk();

// set default highlight on page load
function setOnLoad() {
    document.getElementById('interface').value = webmailDefault;
     changeInterface(webmailDefault);
//    turnOfButton();
    
	var webmailLang=document.getElementById('int_lang').value;
	document.getElementById('logon').action=webmailUrl[webmailDefault][webmailLang]['ssl'];

	// set default webmailType
	document.getElementById('webmailType').value=webmailDefault;
	document.getElementById('webmail_username').focus();
}

function submitForm(webmailType) {
	document.getElementById('webmailType').value = webmailType;
//	document.getElementById('submit').click();
	GotoWebmail();
}


function changeInterface(webmail) {
    webmail_type = webmail;

    if (webmail == 'atmail')
    {
        document.getElementById('fade_text').className = '';
        document.getElementById('fade_select').className = '';
        document.getElementById('LoginType1').disabled = false;
    }
    else
    {
        document.getElementById('fade_text').className = 'fade';
        document.getElementById('fade_select').className = 'fade';
        document.getElementById('LoginType1').disabled = true;
    }
    //showMessage(webmail);
}

function getElementsByName_iefix(tag, name)
{
    var elem = document.getElementsByTagName(tag);
    var arr = new Array();
    for(i = 0,iarr = 0; i < elem.length; i++)
    {
        att = elem[i].getAttribute("name");
        if(att == name) {
            arr[iarr] = elem[i];
            iarr++;
        }
    }
    return arr;
}


function submitFormNew() {
    submitForm(document.getElementById('interface').value);
}

function BrowserVerChk() {
	this.Type = false;
	this.TypeLong = false;
	this.Version = false;
	this.LateGen = false;

	if (navigator.appVersion.indexOf("MSIE") != -1) {
		this.Type = "MSIE";
		this.TypeLong = "Internet Explorer";
		var TempArray = navigator.appVersion.split("MSIE");
		this.Version = parseFloat(TempArray[1]);
		if (this.Version >= 6) this.LateGen = true;
	} else if (navigator.userAgent.indexOf("Firefox") != -1) {
		this.Type = "Firefox";
		this.TypeLong = "Firefox";
		var VersionIndex = navigator.userAgent.indexOf("Firefox") + 8;
		this.Version = (navigator.userAgent.charAt(VersionIndex) + "." + navigator.userAgent.charAt(VersionIndex + 2)) * 1;
		if (this.Version >= 1.5) this.LateGen = true;
	} else if(navigator.userAgent.indexOf("Safari") != -1)	{
		this.Type = "Safari";
		this.TypeLong = "Safari";
		this.LateGen = true;
	} else if(navigator.userAgent.indexOf("Iceweasel") != -1) {
		this.Type = "Iceweasel";
		this.TypeLong = "Iceweasel";
		var VersionIndex = navigator.userAgent.indexOf("Iceweasel") + 10;
		this.Version = (navigator.userAgent.charAt(VersionIndex) + "." + navigator.userAgent.charAt(VersionIndex + 2)) * 1;
		if (this.Version >= 1.5) this.LateGen = true;
	} else if(window.opera) {
		this.Type = "Opera";
		this.TypeLong = "Opera";
		this.Version = window.opera.version();
		this.LateGen = true;
	}
//	alert(navigator.userAgent);
}

function BrowserVsInterface() {
	//document.body.style.cursor = "wait";
	var SelectedInterface = document.getElementById('LoginType1').selectedIndex;
	var ConfirmUser = false;
	if (SelectedInterface > 0) {
		if (SelectedInterface == 1 && BrowserVer.LateGen != true) {
			if (BrowserVer.Type == "Firefox") {
				ConfirmUser = "Firefox Version 1.5 or later";
			} else {
				ConfirmUser = "Internet Explorer Version 6 or later";
			}
		} else if (SelectedInterface == 2 && (BrowserVer.Type != "MSIE" || BrowserVer.LateGen != true)) {
			ConfirmUser = "Internet Explorer Version 6 or later";
		} else if (SelectedInterface == 3 && BrowserVer.Type != "Firefox" && BrowserVer.Type != "Iceweasel") {
			ConfirmUser = "Firefox Version 1.0 or later";
		} else {
			ConfirmUser = false;
		}
	}

	if (ConfirmUser != false) {
		if (confirm ("Your Browser does not match the recommended requirements for this Interface.\nYou are using " + BrowserVer.TypeLong + " Version " + BrowserVer.Version + ", You need " + ConfirmUser + ".\nAre you sure you want to continue logging in?") == true) {
			ConfirmUser = false;
		}
	}

//	if (ConfirmUser == false) {
//		document.loginPage.submit();
//	} else {
//		document.body.style.cursor = "";
//	}
}

function injectDomain() {
    var user_domain = document.domain;
    var domain_regex = /^(mail\.)(.*)/;
    var test_domain = user_domain.replace(domain_regex, "$1");
    if (test_domain == 'mail.') {
        user_domain = user_domain.replace(domain_regex, "@$2");
        document.getElementById('webmail_username').value = user_domain;
        setSelectionRange(document.getElementById('webmail_username'),0,0);
    }
}

function setSelectionRange(input, selectionStart, selectionEnd) {
    if (input.setSelectionRange) {
        input.focus();
        input.setSelectionRange(selectionStart, selectionEnd);
    }
    else if (input.createTextRange) {
        var range = input.createTextRange();
        range.collapse(true);
        range.moveEnd('character', selectionEnd);
        range.moveStart('character', selectionStart);
        range.select();
    }
}
