/*
	shared.js
	v1.0 - 16/jan/2007 - Initial version.
	All rights reserved.

	Usefull urls:
	- http://www.sergiopereira.com/articles/prototype.js.html
	- http://wiki.script.aculo.us/scriptaculous/show/CoreEffects
	- http://blogs.ebusiness-apps.com/jordan/pages/Prototype%20Library%20Info.htm
	- http://www.onjava.com/pub/a/onjava/2005/10/26/ajax-handling-bookmarks-and-back-button.html
*/

function checkCode(link_) {
	if (__logged_in) return true;

	var url = 'form.login.php?target=' + escape(location.href);
	url += '&rn=' + Math.random();

	// Due to the fact that javascript variables are reset when the user presses the back button
	// of the browser, we need to check a server-side script to see if the user has successfully
	// logged in during this session.
	new Ajax.Request('is.logged.in.php?rn=' + Math.random(), {
		method:'get',
		onComplete:function(req_) {
			switch(req_.responseText) {
				case '1':
					__logged_in = true;
					location.href = link_;
					break;
				case '0':
				default:
					dialog.open( {
						title:'Toegang tot Hetefilms',
						modal:true,
						width:550,
						duration:0.5,
						background:'#370B00',
						source:url,
						buttons:'Sluiten',
						button_cancel:'Sluiten'
					}, function() {
						try {
							if ($('field_phone')) $('field_phone').focus();
						} catch(e_) { /* do nothing :-) */ }
					} );
					break;
			}
		}
	} );
	return false;
}

function onCodeKeyPress(e_) {
	var key = window.event ? e_.keyCode : e_.which;
	if (key == 13) {
		if ($('loginForm')) $('loginForm').submit();
	}
}

function onFriendsFormSubmit() {
	new Ajax.Request(
		$('friendForm').getAttribute('action'), {
			method:'post',
			parameters:Form.serialize('friendForm'),
			onComplete:function(req_) {
				if (req_.responseText != 'OK') {
					dialog.open( {
						title:'Fout tijdens versturen formulier',
						background:'#59120B',
						width:350,
						duration:0,
						content:req_.responseText,
						buttons:'Sluiten',
						button_cancel:'Sluiten'
					} );
				} else {
					dialog.open( {
						title:'Formulier is verstuurd',
						background:'#59120B',
						width:350,
						duration:0,
						content:'Het formulier is verstuurd.',
						buttons:'Sluiten',
						button_cancel:'Sluiten',
						callback:function() {
							location.href = './';
						}
					} );
				}
			}
		}
	);
}
