var debugging = false;
var requestHandlerURL = "ajax_request_handler.jsp";
var randomFragment = null;

// fallback definitions
var TXTLoginValid= 'Login valid!';
var TXTVerifyData= 'Verifying data...';
var TXTLoadingApp='Loading application...';
var TXTLoginFailed= 'Login failed!';
var TXTTimeout='Server timeout, please try again later!';

var requestTimeout;
function displayRequestTimeout() {
	stopAnim('<span style="color:darkred" id="TXTTimeout">'+TXTTimeout+'</span>');
	document.getElementById("loginButton").disabled = false;
}

function login() {
	if(debugging)debug("checking login...");
	document.getElementById("loginButton").disabled = true;
	startAnim('<span style="color:orange" id="TXTVerifyData">'+TXTVerifyData+'</span>');
	sendPostRequest("login","login_username="+document.getElementById("username").value+"&login_password="+hex_md5(document.getElementById("password").value.toUpperCase()+randomFragment),requestHandlerURL);
	requestTimeout = setTimeout("displayRequestTimeout()",35000);
}
function directLogin(usr,pwd,language,schemaname) {
	startAnim('<span style="color:orange" id="TXTVerifyData">'+TXTVerifyData+'</span>');
	schemaname = (typeof schemaname == "undefined" || schemaname == "null") ? "" : schemaname;
	sendPostRequest("login","login_username="+usr+"&login_password="+pwd+"&language="+language+"&schemaname="+schemaname,requestHandlerURL);
	requestTimeout = setTimeout("displayRequestTimeout()",35000);
}
function directLoginMD5(usr,pwd,language,schemaname) {
	startAnim('<span style="color:orange" id="TXTVerifyData">'+TXTVerifyData+'</span>');
	schemaname = (typeof schemaname == "undefined" || schemaname == "null") ? "" : schemaname;
	sendPostRequest("login","login_username="+usr+"&login_password="+pwd+"&language="+language+"&schemaname="+schemaname,requestHandlerURL);
	requestTimeout = setTimeout("displayRequestTimeout()",35000);
}

function loginValid(valid, url) {
	clearTimeout(requestTimeout);
	if(valid) {
		stopAnim('<span style="color:#00a000" id="TXTLoginValid">'+TXTLoginValid+'</span>');
		document.getElementById("loginButton").disabled = true;
		document.getElementById("username").disabled = true;
		document.getElementById("password").disabled = true;
		setText("<span style=\'color:#0000bb\' id='TXTLoadingApp'>"+TXTLoadingApp+"</span>");
		parent.document.getElementById('interpartsNest').setAttribute('src',url);
		setTimeout('waitingTime=500;startAnim("")',500);
	} else {
		stopAnim('<span style="color:darkred" id="TXTLoginFailed">'+TXTLoginFailed+'</span>');
		setTimeout('document.getElementById("loginButton").disabled = false;',3000);
	}
}

function getRandomFragment() {
	if(debugging) debug("get random fragment from server..");
	sendPostRequest("getRandomFragment",new Array(),requestHandlerURL);
}

function setRandomFragment(frag) {
	randomFragment = frag;
	document.getElementById("loginButton").disabled = false;
}

function setGUILanguage(lang) {
	sendPostRequest("languageChange","language="+lang,requestHandlerURL);
	var d = frames.languageLoaderIFrame.document;
	var custHREF = customHREF("lang/"+lang+"/lang.js");
	d.open();
	if(custHREF == null)
		d.write('<html><head><sc'+'ript src="'+ip3Path+'/lang/'+lang+'/lang.js"></sc'+'ript></head><body onload="parent.ld=ld;parent.setGUILabels()"></body></html>');
	else
		d.write('<html><head><sc'+'ript src="'+ip3Path+'/lang/'+lang+'/lang.js"></sc'+'ript><sc'+'ript src="'+custHREF+'"></sc'+'ript></head><body onload="parent.ld=ld;parent.setGUILabels()"></body></html>');
	d.close();
}
var addedCopyRightLink = false;

function setGUILabels() {
	document.getElementById("usr").firstChild.nodeValue=ld['liebherr_loginTxt4'];
    document.getElementById("pw").firstChild.nodeValue=ld['liebherr_loginTxt5'];
    document.getElementById("langOpt").firstChild.nodeValue=ld['ps_7'];
    document.getElementById("loginButton").value=ld['liebherr_loginTxt6'];

    if(ld['login_valid'] != "") TXTLoginValid= ld['login_valid'];
    if(ld['login_verify_data'] != "") TXTVerifyData= ld['login_verify_data'];
    if(ld['login_loading_app'] != "") TXTLoadingApp= ld['login_loading_app'];
    if(ld['login_failed'] != "") TXTLoginFailed= ld['login_failed'];
    if(ld['login_timeout'] != "") TXTTimeout= ld['login_timeout'];

    try  {
    	document.getElementById('linkTo').setAttribute('value',ld['maintancePasswdBtn']);
    } catch(e) {}

	var dynLabelIDs = new Array('TXTLoginValid','TXTVerifyData','TXTLoadingApp','TXTLoginFailed','TXTTimeout');
	for(Q3 in dynLabelIDs) {
		try {
			document.getElementById(dynLabelIDs[Q3]).firstChild.nodeValue=window[dynLabelIDs[Q3]];
		} catch(e) {}
	}

    var lc = document.getElementById("langChoice");
    for(var i = 0; i < lc.length;i++) {
    	var vlang = lc.options[i].value;
    	lc.options[i].text = ld[vlang];
    }
    if(!addedCopyRightLink) { // fake image with link
    	var cImage = document.createElement('<img src="../viewer/custom/images/spatsl.gif" onclick="open(\'http://www.jenbacher.com/shared/copyright.php?lang=2\',\'_blank\')" style="position:absolute;right:-1px;top:3px;cursor:hand" border="0" width="132" height="35">');
	    document.getElementsByTagName("body")[0].appendChild(cImage);
    }
    addedCopyRightLink=true;
}
