/*  

Project:  content.life 3 / Login
Version:  1.0
Autor:    Sindre Wimberger / wimberger@echonet.at
Company:  echonet communication gmbh
		  schottenfeldgasse 24, 1070 vienna
	  	  http://www.echonet.at
Created:  29.01.2007
Modified: 
 
 */


var Engine = {
  detect: function() {
    var UA = navigator.userAgent;
    this.isKHTML = /Konqueror|Safari|KHTML/.test(UA);
    this.isGecko = (/Gecko/.test(UA) && !this.isKHTML);
    this.isOpera = /Opera/.test(UA);
    this.isMSIE  = (/MSIE/.test(UA) && !this.isOpera);
    this.isMSIE7 = this.isMSIE && !(/MSIE 6\./.test(UA) && !this.isOpera);
    this.isMSIE6 = this.isMSIE && !this.isMSIE7;
  }
}

// Load Function on DOM.Contentload - crossbrowser
function onContent(f){//(C)webreflection.blogspot.com
var b=navigator.userAgent,d=document,w=window,
c="__onContent__",e="addEventListener",o="opera",r="readyState",
s="<scr".concat("ipt defer src='//:' on",r,"change='if(this.",r,"==\"complete\"){this.parentNode.removeChild(this);",c,"()}'></scr","ipt>");
w[c]=(function(o){return function(){w[c]=function(){};f(o?o():o)}})(w[c]);
if(d[e])d[e]("DOMContentLoaded",w[c],false);
if(/WebKit|Khtml/i.test(b)||(w[o]&&parseInt(w[o].version())<9))
(function(){/loaded|complete/.test(d[r])?w[c]():setTimeout(arguments.callee,1)})();
else if(/MSIE/i.test(b))d.write(s);
};

function loginInit() {
	// Abfrage ab prototype und script.aculo.us richtig eingebunden im html
    if((typeof Prototype=='undefined') || 
       (typeof Element == 'undefined') || 
	   (typeof Effect == 'undefined')  || 
       (typeof Element.Methods=='undefined') ||
       parseFloat(Prototype.Version.split(".")[0] + "." +
                  Prototype.Version.split(".")[1]) < 1.5){
       alert("content.life 3 Login requires the Prototype JavaScript framework >= 1.5.0 and script.aculo.us >= 1.7.0");	
	} else {
    	Engine.detect(); 
		if($('login')) {
			Element.show('login');
			if (typeof ie5check == 'undefined') Form.focusFirstElement('login');
			Event.observe('login', 'submit', function(event){
							loginDo();
							Event.stop(event);
			});
			Event.observe('login', "keypress", function(event) {
							if (event.keyCode == Event.KEY_RETURN) {
								loginDo();
								Event.stop(event);
							}
			});
		}	
	}
}
function loginDo() {
	var f, values;
    if(!self.Ajax){

	} else {
        f = $('login');
	// Eigener Request für IE 5.5
		if(Form.serialize(f)== "") {	
			var url= "?RDTSID="+Form.Element.getValue('RDTSID')+ "&username="+Form.Element.getValue('username')+"&password="+Form.Element.getValue('password')+"";
			new Ajax.Request('/lib/loginajax_intern.php', {
				method: 'post',
				parameters: url,
				onComplete: function(resp) {loginAjaxReturn(resp.responseText);}
			});
		} else {
			new Ajax.Request('/lib/loginajax_intern.php', {
				method: 'post',
				parameters: Form.serialize(f),
				onFailure: function(resp) {loginAjaxReturn('error');},
				onException: function(resp) {loginAjaxReturn('error');},
				onComplete: function(resp) {loginAjaxReturn(resp.responseText);}
			});
		
		}
        Form.disable(f);
    }
}
function loginAjaxReturn(response) {
	switch (response) {
		// Wenn login Korrekt
		case "OK":
	        Element.hide('inhalt');
			Element.hide('login');	 
			new Effect.Appear('inhalt', {from:0, to:1, duration:1,
							  beforeStart:function(){
									Element.removeClassName('c-content', 'secure');
									Element.removeClassName('c-content', 'indent');
									Element.update('title','Login erfolgreich, System wird gestartet.');
									if (Engine.isMSIE6) {
										Element.update('body','<img src="/admin/assets/style/gfx/cl_login_schloss_offen.jpg" alt="Schloss offen" />');
									} else {
										Element.update('body','<img src="/admin/assets/style/gfx/cl_login_schloss_offen.png" alt="Schloss offen" />');
									}
									Element.removeClassName('title', 'error');
									Element.addClassName('title', 'center');
									Element.addClassName('body', 'center');
									new Insertion.Bottom('c-content', '<p class="center"><img src="../admin/assets/style/gfx/load-admin.gif" alt="Loading Animation" /></p><p class="center">Wenn Sie nicht automatisch weitergeleitet werden, <br /><a href="/intern/">dann klicken Sie bitte auf diesen Link</a>.</p>'); 		
							  },
							  afterFinish:function(){
								var timer = window.setTimeout(function(){
															window.location.href = '/intern/index.php';
//															window.location.reload();
															window.clearTimeout(timer);
														}, 3000);
							  }
			});												
			break;
		// Wenn login falsch
		case "NOTOK":
			Element.update('title','Fehler! +++ Fehler! +++ Fehler!');
			Element.addClassName('title', 'error');
			new Effect.Pulsate('body');
			Element.update('body','Login war <span class="special">nicht erfolgreich</span>, Benutzername und/oder Passwort stimmen nicht überein. Bitte versuchen Sie es erneut.');	
            var f = $('login');
            Form.enable(f);	
			Form.reset(f);
			Form.focusFirstElement('login');					
			break;
		// Wenn Fehler im Verbindungsaufbau zum server
		case "error":
			Element.update('title','Fehler! +++ Fehler! +++ Fehler!');
			Element.addClassName('title', 'error');
			Element.update('body','Fehler beim Verbindungsaufbau.');		
			new Effect.Pulsate('body');
            var f = $('login');
			Form.enable(f);	
			break;
		// Wenn falsche Serverantwort
		default:
			Element.update('title','Fehler! +++ Fehler! +++ Fehler!');
			Element.addClassName('title', 'error');
			Element.update('body','Fehlerhafte Serverantwort.');	
			new Effect.Pulsate('body');
            var f = $('login');
			Form.enable(f);	
			break;			
	}
}

onContent(function(){loginInit()});
