    
    byId = function(id) {
        return document.getElementById (id) ;
    }

	
	function createCookie (name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+encodeURIComponent(value)+expires+"; path=/";
	} 
	
	function readCookie (name) {
	     var nameEQ = name + "=";
	     var ca = document.cookie.split(';');
	     for(var i=0;i < ca.length;i++) {
		     var c = ca[i];
		     while (c.charAt(0)==' ') c = c.substring(1,c.length);
		     if (c.indexOf(nameEQ) == 0) return decodeURIComponent(c.substring(nameEQ.length,c.length));
		     }
	     return null;
	}
	
	function randomize_box() {
		var host = window.location.host.replace ('www\.', '') ;
		var random = Math.floor(Math.random()*100);
		if (host == 'poczta.o2.pl' || host == 'poczta10.o2.pl') {
			domain = (random % 2) ? 'poczta10.o2.pl' : 'poczta.o2.pl' ;
		} else {
			return host ;
		}
		return domain ;		
	}
	
	var domain = randomize_box () ;
	var ssl = readCookie ('ssl') ;
	if (ssl || window.location.protocol == 'https') {
        var ssl_element = byId('ssl');
        if (ssl_element) {
    		byId('ssl').checked = 'checked' ;
        }
	}

	if (domain) {
		var p = byId('preload') ;
        window.setTimeout(function() {
            p.src = window.location.protocol + '//' + domain + WEBMAIL_PATH+'preload.html' ;
        }, 1000) ;    
	}

	if (byId('ssl') && ssl === '0') {
		byId('ssl').checked = false ;
	}
    
    if(byId('login')) {
        window.setTimeout(function() {
            byId('login').focus() ;
        }, 500) ;    
    }
	
	function onLogin () {
        var action = byId('login_form').getAttribute('action') ; 
		if (byId('ssl')) {
			var ssl = byId('ssl').checked;
            
			if(ssl) {
				if( action.indexOf( 'http' ) == -1 ){
					//dwagner - insert https in place of request protocol in login form action
					//byId('login_form').action = byId('login_form').action.replace(document.location.protocol, 'https:');
                    if (action.indexOf( 'http' ) == -1 ){
                        //dwagner - old login via https
                        byId('login_form').action = "https://"+domain+"/" + action ;
                    }        
				}
				createCookie ('ssl', '1', 1) ;
			} else {
                if (domain != document.location.host.replace ('www\.', '') && action.indexOf( 'http' ) == -1) {
                    byId('login_form').action =  document.location.protocol + "//" +domain + "/" + action;
                }    
                createCookie ('ssl', '0', 1) ;
			}

		} else {
			if (domain != document.location.host.replace ('www\.', '') && action.indexOf( 'http' ) == -1) {
				byId('login_form').action =  document.location.protocol + "//" +domain + "/" + action;
			}    
			createCookie ('ssl', '0', 1) ;
		}	
        
        var logcount = parseInt(readCookie('logcount') || 0) ;
        createCookie('logcount', logcount+1, 7) ; 
	}
	function onUserDomainLogin () {
        var action = byId('login_form').getAttribute('action') ; 
		if (byId('ssl')) {
			var ssl = byId('ssl').checked;
			if(ssl) {
                byId('login_form').action = "https://"+domain + action ; 

				createCookie ('ssl', '1', 1) ;
			} else {
                byId('login_form').action =  window.location.protocol + "//" +domain + action;

                createCookie ('ssl', '0', 1) ;
			}

		}	
	}

	var ls = byId('language-select') ;
	if (ls) {
		var lang = readCookie ('lang') || WEBMAIL_LANG ;
		if (lang) {
			var o = ls.options ;
			for (var i=0; i<o.length; i++) {
				if (o[i].value == lang) {
					ls.selectedIndex = i ;
					break ;
				}
			}
		}

		ls.onchange = function () {
			onLangSelect () ;
			byId('lang-form').submit () ;
		}
	}

	function onLangSelect () {
		var lang = byId('language-select').value ;
		createCookie ('lang', lang) ;
	}

function clear_input( inputElement, defaultInputValue ){
    if( inputElement.value == defaultInputValue ){
        inputElement.value = '';
    }
}
function default_input( inputElement, defaultInputValue ){
    if( inputElement.value == '' ){
        inputElement.value = defaultInputValue;
    }
}
