// Vagipe GUI Helpers (DE)

// Create an xmlHTTP object for server communication
function createXmlHttp()
{
	var xmlhttp;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	return xmlhttp;
}


function modifyClass(theClass,element,value) {
	var cssRules;
	
	var added = false;
	for (var S = 0; S < document.styleSheets.length; S++)
	{
	
		if (document.styleSheets[S]['rules']) {
			cssRules = 'rules';
		} else if (document.styleSheets[S]['cssRules']) {
			cssRules = 'cssRules';
		} else {
		//no rules found... browser unknown
	}
	
	for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
		if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
			if(document.styleSheets[S][cssRules][R].style[element]) {
				document.styleSheets[S][cssRules][R].style[element] = value;
				added=true;
				break;
			}
		}
	}
	
	if(!added) {
		if(document.styleSheets[S].insertRule) {
			  document.styleSheets[S].insertRule(theClass+' { '+element+': '+value+'; }',document.styleSheets[S][cssRules].length);
			} else if (document.styleSheets[S].addRule) {
				document.styleSheets[S].addRule(theClass,element+': '+value+';');
			}
		}
	}
}

function runIfExists(func)
{
	try	{
		eval(func);
	}
	catch(e) {
		//alert("no");
	}
}

function parentLocation(loc)
{
	if (loc == "myguestbook")
		{ parent.location="/edit_profile_de.php?m=guestbook"; }
	if (loc == "mymail")
		{ parent.location="/mail_de.php"; }
}

var windowid = 0;
function openChat(room)
{
	var size = 1;
	var roomadd = "";
	if (typeof room == "undefined")
	{
		var chatsize = document.getElementById("chatsize");
		size = chatsize.options[chatsize.selectedIndex].value;
	} else roomadd = "&setroom="+room;

	//if (windowid) windowid.close();

	if (size == 2) windowid = window.open('/webcam_window_trans_xxl.php?lang=deutsch'+roomadd, 'VagipeChat', 'resizable,width=1190,height=837');
	else if (size == 3) windowid = window.open('/webcam_window_trans_wide.php?lang=deutsch'+roomadd, 'VagipeChat', 'resizable,width=1335,height=790');
	else if (size == 4) windowid = window.open('/webcam_window_trans_small_wide.php?lang=english'+roomadd, 'VagipeChat', 'resizable,width=1192,height=719');
	else windowid = window.open('/webcam_window_trans.php?lang=deutsch'+roomadd, 'VagipeChat', 'resizable,width=1000,height=675');
	windowid.focus();
}

function openMediaView(media, where)
{
	var mwin;
	if (typeof where == "undefined" || !where)
	{
		mwin = window.open('/profile_media_viewer.php?m=g&p='+media, '_blank', 'WIDTH=810,HEIGHT=650,SCROLLING=AUTO');
	} else {
		mwin = window.open('/profile_media_viewer.php?m=p&p='+media, '_blank', 'WIDTH=810,HEIGHT=650,SCROLLING=AUTO');
	}
	mwin.focus();
}

function openLevelWin(user)
{
	var lwin;
	lwin = window.open('/user_level_de.php?uid='+user, "VAGLevel"+encodeURIComponent(name.replace(/[^a-zA-Z0-9]/g, "")), 'WIDTH=280,HEIGHT=230,SCROLLING=AUTO');
	lwin.focus();
}


function foldElement(ele, oh, skipscroll)
{
	var target = document.getElementById(ele);
	var inner = document.getElementById(ele+'inner');
	var button = document.getElementById(ele+'butt');
	if (target.offsetHeight < inner.offsetHeight)
	{
		var stepping = Math.round( (inner.offsetHeight - target.offsetHeight) / 10)
		if (stepping > 500) stepping = (inner.offsetHeight - target.offsetHeight);
		button.src = '/gfx/butt_up.png';
		resWin(target, inner, stepping, 0, skipscroll);
		//target.style.height = inner.style.height;
	} else if (target.offsetHeight > oh && inner.offsetHeight > oh) {
		button.src = '/gfx/butt_down.png';
		var stepping = Math.round( (target.offsetHeight-oh) / 10)
		if (stepping > 500) stepping = (target.offsetHeight-oh);
		resWin(target, inner, stepping, oh, skipscroll);
	}
}

function resWin(outer, inner, offset, rev, skipscroll)
{
	if (!rev) var newh = outer.offsetHeight+offset;
	else var newh = outer.offsetHeight-offset;
	if (!rev && newh > inner.offsetHeight) newh = inner.offsetHeight;
	else if (rev && newh < rev) newh = rev;
	outer.style.height = newh+"px";
	var next = function () { resWin(outer, inner, offset, rev, skipscroll); }
	if (!rev && newh < inner.offsetHeight) setTimeout(next,1);
	else if (rev && newh > rev) setTimeout(next,1)
	else if (!skipscroll) outer.scrollUpdate();
}

function sendMail(recip, ticket, tid)
{
	var mwin;
	if (typeof ticket == "undefined" || !ticket)
	{
		mwin = window.open("/mail_send_de.php?recip="+encodeURIComponent(recip),"VAGmessage","scrollbars,resizable,WIDTH=920,HEIGHT=650");
	} else {
		mwin = window.open("/mail_send_de.php?ticket="+ticket+"&tid="+tid+"&recip="+encodeURIComponent(recip),"VAGmessage","scrollbars,resizable,WIDTH=920,HEIGHT=650");
	}
	mwin.focus();
}

function openPicture(name)
{
	var pwin;
	pwin = window.open("/userpics/"+name,"VAGpic","scrollbars,resizable,WIDTH=800,HEIGHT=600");
	pwin.focus();
}

function openMap(long, lat)
{
	var mapwin;
	mapwin = window.open("/user_map_de.php?long="+long+"&lat="+lat,"VAGmap","scrollbars,resizable,WIDTH=800,HEIGHT=600");
	mapwin.focus();
}

function cupHist(ID)
{
	var cwin;
	cwin = window.open("/profile_cup_hist_de.php?c="+ID,"VAGcups","scrollbars,resizable,WIDTH=550,HEIGHT=500");
	cwin.focus();
}	

// Flash helper function
function openURL(url)
{
	var fwin = window.open(url);
	fwin.focus();
}


function openProfile(name)
{
	var pwin;
	pwin = window.open("/profile_view_de.php?nomen=1&user="+encodeURIComponent(name),"VAGprofile"+encodeURIComponent(name.replace(/[^a-zA-Z0-9]/g, "")),"scrollbars=yes,resizable=no,menubar=no,resizable,WIDTH=805,HEIGHT=600");
	pwin.focus();
}

function openAboutMatch(myitem, type)
{
	window.open('/profile_about_match_de.php?item='+encodeURIComponent(myitem)+"&type="+type, '_blank', 'WIDTH=320,HEIGHT=350,SCROLLING=AUTO');
}

function openCamChat()
{

	xmlhttp.open("POST", "/auth_login.php", false);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	var loginString = "u="+encodeURIComponent(document.getElementById('user').value)+"&p="+encodeURIComponent(document.getElementById('password').value)+"&lang=deutsch&keys="+fpHelper()+"&lid="+lid+"&lna="+lna+"&lb="+lb;
	if (window.localStorage && window.localStorage.LSt) loginString += "&LSt="+window.localStorage.LSt;
	xmlhttp.send(loginString);

	if (xmlhttp.readyState==4)
	{
		var response = xmlhttp.responseText;
		if (xmlhttp.status == 200 && response.match("access=") == "access=")
		{
			var introspan = document.getElementById("introspan");

			if ( response.match("access=yes") == "access=yes" )
			{
				introspan.innerHTML = "Login best&auml;tigt!";

				if (document.getElementById('user'))
					var user = document.getElementById('user').value;
				else var user = "undefined";
				if (document.getElementById('lang'))
					var lang = document.getElementById('lang').options[document.getElementById('lang').selectedIndex].value;
				else var lang = "undefined";
				user = user.replace(/[\<\>]/g, "");
				parent.location.href = '/profile_de.php?loggedin=1';

			} else if (response.match("banned=yes") == "banned=yes") {
				introspan.innerHTML = "<span class='warn'>Du bist noch gebannt. Versuch es sp&auml;ter nocheinmal.</span>";
			} else if (response.match("perma=yes") == "perma=yes") {
				introspan.innerHTML = "<span class='warn'>Dein Konto wurde gesperrt. Geh weg!</span>";
				parent.location = "/permaBanned_de.php?nick="+document.getElementById('user').value;
			} else if (response.match("proxy=yes") == "proxy=yes") {
				introspan.innerHTML = "<span class='warn'>Anonymisierer oder offene Proxies sind nicht gestattet!</span>";
			} else if (response.match("inactive=yes") == "inactive=yes") {
				introspan.innerHTML = "<span class='warn'>Bitte aktiviere dein Konto mit der E-Mail, die wir dir geschickt haben!</span>";
			} else if (response.match("passfail=yes") == "passfail=yes") {
				introspan.innerHTML = "<span class='warn'>Name oder Passwort falsch!</span>";
			} else if (response.match("unsupportedLocation=yes") == "unsupportedLocation=yes") {
				introspan.innerHTML = "<span class='warn'>Vagipe ist an deinem Ort leider nicht verfügbar.</span>";
			} else {
				introspan.innerHTML = "<span class='warn'>Ein unbekannter Fehler ist aufgetreten. Bitte wende dich an webmaster@vagipe.com!</span>";
			}
		} else {
			introspan.innerHTML = "<span class='warn'>Anmeldeserver nicht verfügbar. Bitte versuche es später nocheinmal!</span>";
		}

	}
}
