var xmlHttp = null;
var checkLoginNameTimeOut = null;
var checkDisplayNameTimeOut = null;
var wysiwygWindow = null;
var regExpDisplayName = /^[a-zA-Z0-9 ]+$/;
var regExpLoginName = /^[a-zA-Z0-9]+$/;
var regNum = /([0-9]{1,})/;

function init(){
	//main menu
	window.setTimeout("$('#main-menu li:eq(3)').fadeIn('slow');",100);
	window.setTimeout("$('#main-menu li:eq(2)').fadeIn('slow');",200);
	window.setTimeout("$('#main-menu li:eq(1)').fadeIn('slow');",300);
	window.setTimeout("$('#main-menu li:eq(0)').fadeIn('slow');",400);
}

function num(val){
	var num = regNum.exec(val);
	
	if(!num){
		return 0;
	}
	
	return num[0] || 0;
}

function checkDisplayName(val){
	window.clearTimeout(checkDisplayNameTimeOut);
	
	if(val){
		if(regExpDisplayName.test(val)){
			$("#registration-displayname-check").html("<img src='../img/framework/loading.gif' alt='loading.gif' width='16' height='16' title='Verf&uuml;gbarkeit wird &uuml;berpr&uuml;ft' />");
	
			var respond = function(){
				if(xmlHttp.readyState==4){
					if(xmlHttp.responseText=="true"){
						$("#registration-displayname-check").html("<img src='../img/framework/available.png' alt='available.gif' width='16' height='16' title='Anzeigename ist verf&uuml;gbar' />");
					}else{
						$("#registration-displayname-check").html("<img src='../img/framework/not-available.png' alt='not-available.gif' width='16' height='16' title='Anzeigename ist NICHT verf&uuml;gbar' />");
					}
				}
			};
			
			val = encodeURI(val);
			checkDisplayNameTimeOut = window.setTimeout(function(){ openRequest("get","../lanyse/displayNameAvailable.php?displayName="+val,null,respond); },1000);
		}else{
			$("#registration-displayname-check").html("<img src='../img/framework/not-available.png' alt='not-available.gif' width='16' height='16' title='Anzeigename ist nicht g&uuml;ltig (nur a-Z, 0-9 und Leerzeichen)' />");
		}
	}else{
		$("#registration-displayname-check").html("<img src='../img/framework/help.png' alt='help.gif' width='16' height='16' />");
	}
}

function checkLoginName(val){
	window.clearTimeout(checkLoginNameTimeOut);

	if(val){
		if(regExpLoginName.test(val)){
			$("#registration-loginname-check").html("<img src='../img/framework/loading.gif' alt='loading.gif' width='16' height='16' title='Verf&uuml;gbarkeit wird &uuml;berpr&uuml;ft' />");
		
			var respond = function(){
				if(xmlHttp.readyState==4){
					if(xmlHttp.responseText=="true"){
						$("#registration-loginname-check").html("<img src='../img/framework/available.png' alt='available.gif' width='16' height='16' title='Loginname ist verf&uuml;gbar' />");
					}else{
						$("#registration-loginname-check").html("<img src='../img/framework/not-available.png' alt='not-available.gif' width='16' height='16' title='Loginname ist NICHT verf&uuml;gbar' />");
					}
				}
			};
			
			val = encodeURI(val);
			checkLoginNameTimeOut = window.setTimeout(function(){ openRequest("get","../lanyse/loginNameAvailable.php?loginName="+val,null,respond); },1000);
		}else{
			$("#registration-loginname-check").html("<img src='../img/framework/not-available.png' alt='not-available.gif' width='16' height='16' title='Loginname ist nicht g&uuml;ltig (nur a-Z und 0-9)' />");
		}
	}else{
		$("#registration-loginname-check").html("<img src='../img/framework/help.png' alt='help.gif' width='16' height='16' />");
	}
}

function submitRegistration(){
	var displayName = $("#registration-displayname").attr("value");
	var loginName = $("#registration-loginname").attr("value");
	var password = $("#registration-password").attr("value");
	
	if(!displayName.length || !regExpDisplayName.test(displayName)){
		$("#displayNameInfo").dialog("open");
		return false;
	}
	
	if(!loginName.length || !regExpDisplayName.test(loginName)){
		$("#loginNameInfo").dialog("open");
		return false;
	}
	
	if(!password.length){
		$("#passwordInfo").dialog("open");
		return false;
	}
	
	return true;
}

function openRequest(type,url,data,respond){
	try{
		xmlHttp = new XMLHttpRequest();
	}catch(e){
		try{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(e){
			try{
				xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e){
				xmlHttp = null;
			}
		}
	}

	if(xmlHttp){
		xmlHttp.onreadystatechange = respond;
		xmlHttp.open(type,url,true);
		xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		xmlHttp.send(data);
	}
}

function editWYSiWYG(editField){
	wysiwygWindow = window.open("../editor/index.php?editField="+editField,"wysiwygWindow","width=800,height=600");
}

function getMousePos(e){
	var x = 0;
	var y = 0;
	var ev = e || window.event;

	if(ev.pageX || ev.pageY){
		x = ev.pageX;
		y = ev.pageY;
	}else if(ev.clientX || ev.clientY){
		x = ev.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		y = ev.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}

	return {x: x, y: y};
}

function saveComment(){
	var commentName = $("#comment-name").attr("value");
	var commentText = $("#comment-text").attr("value");
	var analysisId = $("#analysisId").attr("value");

	if(commentName && commentText && analysisId && $("#slider").slider("value")==100){
		var data = "commentName="+encodeURI(commentName)+"&commentText="+encodeURI(commentText)+"&analysisId="+encodeURI(analysisId);
		
		var respond = function(){

			if(xmlHttp.readyState==4){
				var text = xmlHttp.responseText;

				if(text!="false"){
					try{
						var html = "";
						var textArr = text.split("@@@@@@@@@@@@@@@@@@@@");
						var id = textArr[0];
						var date = textArr[1];
						var name = textArr[2];
						var comment = textArr[3];
						
						html += "<div class='comment-item' style='display:none;' id='comment_"+id+"'>";
						html += "<div class='comment-name'><img src='../img/framework/user.png' alt='user.png' width='16' height='16' /> "+name+"</div>";
						html += "<div class='comment-date'>"+date+"</div>";
						html += "<div class='comment-text'>"+comment+"</div>";
						html += "</div>";
						
						$("#commentList").html(html+$("#commentList").html());
						$("#comment_"+id).show("blind");
						$("#comment-count").html(parseInt($("#comment-count").html()*1+1));
					}catch(e){
						$("#commentInfo").dialog("open");
					}
				}else{
					$("#commentInfo").dialog("open");
				}
			}
		}

		openRequest("post","../search/save.php",data,respond);
		
		$("#comment-text").attr("value","");
		$("#slider").slider("value",0);
		
		return false;
	}else{
		$("#commentInfo").dialog("open");
	}
}
