function MyRand(range)
{
	return Math.floor(Math.random() * 0.999999 * range);
}

function GetDocWidth()
{
    return Math.max(Math.max(document.body.scrollWidth, document.documentElement.scrollWidth), Math.max(document.body.offsetWidth, document.documentElement.offsetWidth), Math.max(document.body.clientWidth, document.documentElement.clientWidth));
}

function GetDocHeight()
{
    return Math.max(Math.max(document.body.scrollHeight, document.documentElement.scrollHeight), Math.max(document.body.offsetHeight, document.documentElement.offsetHeight), Math.max(document.body.clientHeight, document.documentElement.clientHeight));
}

function GetWinWidth()
{
	if (typeof(window.innerWidth) == 'number')
		return window.innerWidth;
	else if (document.documentElement && document.documentElement.clientWidth)
		return document.documentElement.clientWidth;
	else if (document.body && document.body.clientWidth)
		return document.body.clientWidth;
}

function GetWinHeight()
{
	if (typeof(window.innerHeight) == 'number')
		return window.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight)
		return document.documentElement.clientHeight;
	else if (document.body && document.body.clientHeight)
		return document.body.clientHeight;
}

function GetScrollLeft()
{
	if (typeof(window.pageXOffset) == 'number')
		return window.pageXOffset;
	else if (document.documentElement && document.documentElement.scrollLeft)
		return document.documentElement.scrollLeft;
	else if (document.body && document.body.scrollLeft)
		return document.body.scrollLeft;
}

function GetScrollTop()
{
	if (typeof(window.pageYOffset) == 'number')
		return window.pageYOffset;
	else if (document.documentElement && document.documentElement.scrollTop)
		return document.documentElement.scrollTop;
	else if (document.body && document.body.scrollTop)
		return document.body.scrollTop;
}

function GetObjectValue(id)
{
	var obj = document.getElementById(id);
	if (obj != null)
		return obj.value;
	else
		return "";
}

function GetObjectValueEncoded(id)
{
	return encodeURIComponent(GetObjectValue(id));
}

function AddObjectValueToParams(params, name, id)
{
	if (params != "")
		params += "&";
		
	return params + name + "=" + GetObjectValueEncoded(id);
}

function GetRandomParam()
{
	var chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
	var l = chars.length;
	var result = '';
	
	for (var i = 1; i <= 64; i++)
	{
		var chr = chars.charAt(MyRand(l));
		result += chr;
	}
	
	return result;
}

function CreateXMLHttpRequest()
{
	var ax = [ "MSXML2.XMLHTTP.6.0", "MSXML2.XMLHTTP.3.0" , "MSXML2.XMLHTTP" , "Microsoft.XMLHTTP" ];
	var http = null;
	var i, c;
	
	if (window.XMLHttpRequest)
		http = new XMLHttpRequest();
	else if (window.ActiveXObject)
	{				
		c = ax.length;				
		for (i = 0; i < c; i++)
		{
			try
			{
				http = new ActiveXObject(ax[i]);
				break;
			}
			catch (e)
			{
			}
		}
	}
		
	return http;
}

function ContactPopupState(state)
{
	var objCFCloseButton = document.getElementById('idContactFormCloseButton');
	var objCFInput = document.getElementById('idContactInput');
	var objCFWaitCursor = document.getElementById('idContactWaitCursor');
	var objCFSendOk = document.getElementById('idContactSendOk');
	
	if (objCFCloseButton != null && objCFInput != null && objCFWaitCursor != null && objCFSendOk != null)
	{
		if (state == 2)
		{
			objCFCloseButton.style.display = 'none';
			objCFInput.style.display = 'none';
			objCFWaitCursor.style.display = 'block';
			objCFSendOk.style.display = 'none';
		}
		else if (state == 3)
		{
			objCFCloseButton.style.display = 'block';
			objCFInput.style.display = 'none';
			objCFWaitCursor.style.display = 'none';
			objCFSendOk.style.display = 'block';
		}
		else
		{
			objCFCloseButton.style.display = 'block';
			objCFInput.style.display = 'block';
			objCFWaitCursor.style.display = 'none';
			objCFSendOk.style.display = 'none';
		}	
	}
}

function ContactPopupError(error)
{
	var objCFErrorMessage = document.getElementById('idContactFormFehler');
	if (objCFErrorMessage != null)
	{
		if (error == '')
			error = '&nbsp;';
			
		objCFErrorMessage.innerHTML = error;
	}
}

function ContactPopupOpen()
{
	var objCFCloseButton = document.getElementById('idContactFormCloseButton');
	var objCFBackground = document.getElementById('idContactFormPopupBackground');
	var objCFWindow = document.getElementById('idContactFormPopupWindow');

	if (objCFCloseButton != null && objCFBackground != null && objCFWindow != null)
	{
		var objCFNachricht = document.getElementById('idContactNachricht');
		if (objCFNachricht != null)
			objCFNachricht.value = '';
	
		ContactPopupError('');
		ContactPopupState(1);

		objCFBackground.style.height = (GetDocHeight()) + 'px';
		
		objCFBackground.style.display = 'block';
		objCFWindow.style.display = 'block';
		objCFCloseButton.style.display = 'block';
		
		objCFWindow.style.left = ((GetWinWidth() - objCFWindow.offsetWidth) / 2) + 'px';
		objCFWindow.style.top = (GetScrollTop() + (GetWinHeight() - objCFWindow.offsetHeight) / 2) + 'px';
		objCFCloseButton.style.left = (objCFWindow.offsetLeft + objCFWindow.offsetWidth - objCFCloseButton.offsetWidth - 10) + 'px';
		objCFCloseButton.style.top = (objCFWindow.offsetTop + 10) + 'px';		
	}
}

function ContactPopupClose()
{
	var objCFBackground = document.getElementById('idContactFormPopupBackground');
	var objCFWindow = document.getElementById('idContactFormPopupWindow');
	var objCFCloseButton = document.getElementById('idContactFormCloseButton');

	if (objCFBackground != null && objCFWindow != null && objCFCloseButton != null)
	{
		objCFBackground.style.display = 'none';
		objCFWindow.style.display = 'none';
		objCFCloseButton.style.display = 'none';
	}
}

var httpContact = null;

function ContactReceiveData()
{
	if (httpContact.readyState == 4)
	{
		//alert("Test");
		//alert("(" + httpContact.responseText + ")");
		var data = eval("(" + httpContact.responseText + ")");
		if (data.length > 0)
		{
			var res = data[0];
			if (res.result == 'ok')
			{
				ContactPopupState(3);
			}
			else
			{
				ContactPopupState(1);
				ContactPopupError(res.message);
			}
		}
		else
		{
			ContactPopupState(1);
			ContactPopupError('Ein Fehler ist aufgetreten. Bitte versuchen Sie es erneut!');
		}		
	}
}

function ContactSend(baseURL)
{
	var params = '';
	params = AddObjectValueToParams(params, 'kategorie', 'idContactKategorie');
	params = AddObjectValueToParams(params, 'seitenname', 'idContactSeitenname');
	params = AddObjectValueToParams(params, 'seitenurl', 'idContactSeitenURL');
	params = AddObjectValueToParams(params, 'name', 'idContactName');
	params = AddObjectValueToParams(params, 'firma', 'idContactFirma');
	params = AddObjectValueToParams(params, 'email', 'idContactEMail');
	params = AddObjectValueToParams(params, 'strasse', 'idContactStrasse');
	params = AddObjectValueToParams(params, 'nachricht', 'idContactNachricht');
	
	ContactPopupError('');
	ContactPopupState(2);	

	httpContact = CreateXMLHttpRequest();
	httpContact.open("POST", baseURL + 'services/query.php5?query=contactsend&rnd=' + GetRandomParam(), true);
	httpContact.onreadystatechange = ContactReceiveData;
	httpContact.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	httpContact.setRequestHeader("Content-length", params.length);
	httpContact.setRequestHeader("Connection", "close");
	httpContact.send(params);	
}
