/* Popup window */
var openPopupRes = new Array();

/**
 * Handles server's response
 */
function serverSerponseHandler(request){

	freezeInputs(false);

	if (request.responseText == 'valid'){
		Set_Cookie('email', document.getElementById('input_email_edit_id').value, 365, '/');

		if (processUrl(document.getElementById('hidden_url_id').value, 1))
			closeDialog();
		else showDirectLink();
		
	} // if

	if (request.responseText == 'invalid'){
		alert("This is not valid registered email address. Sorry.");
		return false;

	} // if
	return true;

} // function

/**
 * Displays message with email validation form inside.
 * message can check if entered email exists.
 * returns true if valid email was enteres, otherwise false
 */
function displayEnterMessage(process_url){

	/*
	 *	If it already opened, close it
	 */
	closeDialog();

	var alreadyExists = Get_Cookie('email');
	if (alreadyExists && (alreadyExists.length > 0)){

		processUrl(process_url, 0);
		return true;

	} // if


	var divContainer = document.createElement('div');
	divContainer.setAttribute('id', 'mailInputContainer');

	divContainer.style.position = 'absolute';

	divWidth = 400;
	divHeight = 170;

	divContainer.style.left = (screen.width/2 - divWidth/2);
	divContainer.style.top =  (screen.height/2 - divHeight/2)  - 100;

	divContainer.style.width = divWidth + 'px';
	divContainer.style.height = divHeight + 'px';

	divContainer.style.backgroundColor = 'white';

	containerBody = '																		\
		<table style="border: 1px outset #D5C6B0; margin: 3px; padding:3px; " cellpadding="2" cellspacing="2" width="100%" height="100%" border="0">														\
			<tr>																			\
				<td style="margin: 3px; padding:3px; ">																		\
					<img title="Close" onclick="processCancel(document.getElementById(\'input_email_edit_id\'));" style="cursor: Hand; float: right;" src="fileadmin/stuff/images/hide.gif">																							\
					<br />																				\
					<b>You are going to access special material</b>. It does not require 			\
					authorization, but You have to be our registered user to go ahead with it.		\
					<b>Please provide Your email address</b>.																	\
					<br /><br />																				\
				</td>																		\
			</tr>																			\
			<tr>																			\
				<td align="center" valign="center" style="background-color: #FFF2DD;">																		\
				<br />																											\
					<input type="text" id="input_email_edit_id" value="">											\
					<input type="button" id="input_email_button_id" onclick="processInput(document.getElementById(\'input_email_edit_id\'));" value=" Ok ">											\
					<input type="hidden" id="hidden_url_id" value="'+ process_url +'">											\
				<br /><br />																												\
				</td>																		\
			</tr>																			\
			<tr>																			\
				<td align="center" colspan="2">																		\
					Not registered yet? Please <a target="_blank" href="login/register/">register</a>.																				\
				</td>																		\
																							\
			</tr>																			\
			<tr id="noPopupsLineId" style="display: none;">																			\
				<td align="center" colspan="2">																		\
					<b>Your software does not permit popup windows</b>. <br /> <blink>Please use direct </blink> <a target="_blank" href="' + prependRelativeUrl(process_url) + '" onclick="closeDialog();">link</a> 	\
				</td>																		\
																							\
			</tr>																			\
		</table>																			\
	';

	divContainer.innerHTML = containerBody;

	document.body.appendChild(divContainer);

	document.getElementById('input_email_edit_id').focus();

	scrollToElem('top_of_page');

	return true;
	
} // function

/**
 * Freeze / unfreeze inputs
 */
function freezeInputs(freeze){

	document.getElementById('input_email_edit_id').disabled = freeze;
	document.getElementById('input_email_button_id').disabled = freeze;

	return true;

} // function

/**
 * Used to close dialog message.
 */
function closeDialog(){

	var elem = document.getElementById('mailInputContainer');
//	alert(elem);
	if (elem)
		document.body.removeChild(elem);

} // function

/**
 * Process close button click
 */
function processCancel(){

	closeDialog()
	return true;

} // function

/**
 * Process Ok button on welcome message.
 */
function processInput(element){
	if (!element){

		alert("Empty element! Return.");
		return false;

	} // if

	var re = new RegExp('@');
	if (!element.value){

		alert("Empty input. Sorry.");
		return false;

	} // if

	if (!element.value.match(re)){
		alert("Input does not match regualr expression");
		return false;

	} // if

	// Going to process ajax
	freezeInputs(true); 

	mailValidatorUrl = prependRelativeUrl('mail-validator/');

	var ajaxEl = new ajax (mailValidatorUrl, 
			{
				postBody: 'email=' + element.value,
				onComplete: serverSerponseHandler
			}
	);

	return true;

} // function

function prependRelativeUrl(url){

	if (!isAbsoluteUrl(url)){
		var base_href = baseUrl();
		if (base_href && base_href.length > 0)
			if (base_href.charAt(base_href.length - 1) != '/')
				base_href += '/';

		if (base_href)
			url = base_href + url;
	} // if

	return url;

} // 

/**
 * Processing happy url
 */
function processUrl(url, middleWindow){

	url = prependRelativeUrl(url);

	if (middleWindow == 1){
		var Swidth = 600;
		var Sheight = 250;
	}
	else {
		var Swidth = 400;
		var Sheight = 300;
	} 

	var wndNum = openPopupRes.length;
	var name = 'processingWnd' + wndNum;

	var leftPos = wndNum * 50;
	var topPos = wndNum * 50;

	if (leftPos > 825)
		leftPos %= 825;

	if (topPos > 825)
		topPos %= 825;

	var props = 'menubar=yes,status=no,scrollbars=yes,toolbar=no,resizable=no,width='+ Swidth +',height='+ Sheight +',left=' + leftPos + ',top=' + topPos;

	/**
	 * Due to IE stranes, append preview page
	 */
	if (middleWindow == 1){

		if (url.indexOf('?') >=0)
			url += ':preview=1';
		else url += ':preview=1';

	} // if

	var openPopup = window.open(url, name, props);

	if (!openPopup)
		return false;

	if (openPopup.closed)
		return false;

	openPopupRes[openPopupRes.length] = openPopup;

	return true;

} // function

/**
 * Displays direct link to client if popups are bloked
 */
function showDirectLink(){
	document.getElementById("noPopupsLineId").style.display = '';
	return true;

} // function
