// JavaScript Document

/*
* @authors:		Alessandro Mazzola, Fabrizio Natoli
* @company:		Marconi Channel WebTV
* @release:		2.0
* @date:			08/06/2011
*/

var restrictionType = /[A-Z a-z 0-9 @_]/g;
var restrictionNumber = /[0-9 +]/g;

$( document ).ready( function ()
{
	$( 'a.loginStyle[ href^=# ]').click(function ()
	{
		var popID = $( this ).attr( 'rel' );
    var popURL = $( this ).attr( 'href' );
		var query = popURL.split( '?' );
		var dim = query[ 1 ].split( '&' );
		var popWidth = dim[ 0 ].split( '=' )[ 1 ];
		$( '#' + popID ).fadeIn().css({
			'width': Number( popWidth )
		}).prepend( '<a href="#" class="close"><img src="../skins/webtv/images/close.jpg" class="btn_close" title="Chiudi" alt="Chiudi" /></a>' );
		var popMargTop = ( $( '#' + popID ).height () ) / 2;
		var popMargLeft = ( $( '#' + popID ).width () ) / 2;
		$( '#' + popID).css({
			'margin-top': -popMargTop,
			'margin-left': -popMargLeft
		});
		$( 'body' ).append( '<div id="fade"></div>' );
		$( '#fade' ).css({
			'filter': 'alpha (opacity=80)'
		}).fadeIn();
        return false;
    });
    $( 'a.close, #fade' ).live( 'click', function ()
		{
			$( '#fade , .loginForm' ).fadeOut(function ()
			{
				$( '#fade, a.close' ).remove();
				document.getElementById( 'alert' ).innerHTML = '';
			});
			return false;
    });
});

function ctrlUser ()
{
	var name = document.getElementById( 'name' ).value;
	var pwd = document.getElementById( 'password' ).value;
	if ( name != '' && pwd != '' )
	{
		loginRequest ( name, pwd );
	} else {
		document.getElementById( 'alert' ).innerHTML = 'ATTENZIONE, riempire correttamente tutti i campi.';
	}
	return false;
}

function loginRequest ( name, password )
{
	if ( window.XMLHttpRequest )
	{
		xmlhttp = new XMLHttpRequest ();
	} else {
		xmlhttp = new ActiveXObject ( 'Microsoft.XMLHTTP' );
	}
	xmlhttp.onreadystatechange = function ()
	{
		if ( xmlhttp.readyState == 4 && xmlhttp.status == 200 )
		{
			var responseValue = xmlhttp.responseText;
			if ( responseValue == 'OK' )
			{
				window.location.reload ( true );
			} else if ( responseValue == 'KO' )
			{	
				document.getElementById( 'alert' ).innerHTML = 'ATTENZIONE, Username e Password errati o inesistenti.';
			}
		}
	}
	xmlhttp.open ( 'GET', 'getter_setter/login.php?username=' + name + '&password=' + password, true);
	xmlhttp.send();
}

function popup_passwordRecovery ()
{
	$( '#fade , .loginForm' ).fadeOut ( function () {
		$( '#fade, a.close' ).remove();
		document.getElementById( 'alert' ).innerHTML = '';
	});
	return false;
	document.location.href = '#?w=486';
}

function restrictCharacters ( myfield, e, target_restriction )
{
	if ( !e ) var e = window.event
	if ( e.keyCode ) code = e.keyCode;
	else if ( e.which ) code = e.which;
	var character = String.fromCharCode ( code );
	if ( character.match ( target_restriction ) )
	{
		return true;
	} else {
		return false;
	}
}
