var initProcessFinished = false;
function fillForm( jsonValues, idForm )
{
	values = $.parseJSON( jsonValues );
	if( values == null ) return;
	$.each( values, 
		function( key, value )
		{
			$('#' + idForm).find( '[name="' + key + '"][type!="radio"][type!="file"][type!="password"]' ).val( value );
			$('#' + idForm).find( '[name="' + key + '"][type!="radio"][type!="file"][type!="password"]' ).change();
			$('#' + idForm).find( '[name="' + key + '"][type="radio"]' ).each(
				function( key2, element )
				{
					if( value == element.value )
					{
						element.checked = true;
					}
					else
					{
						element.checked = false;
					}
				}
			);
		}
	);
	initProcessFinished = true;
}

function _fillFormForeachValues( index, value )
{
	
}

$(document).ready( 
	function()
	{
		applyExtraStyles();
		if( location.href.indexOf( "iframe" ) > -1 && window.opener == null )
		{
			location.href = location.href.replace( "iframe", "main" );
		}
		
		if( !isAdmin )
			initPopupBuy();
	}
);


function applyExtraStyles()
{
	$('div.boxTitleTop').each( function(i, value){
		$(value).append( $('<div></div>').addClass( 'boxTitleTopLeft' ) );	
		$(value).append( $('<div></div>').addClass( 'boxTitleTopRight' ) );	
	});
	
	$('div.boxContent').each( function(i, value){
		var height = $(value).height();
		$(value).append( $('<div></div>').addClass( 'boxContentLeft' ).height( height ) );	
		$(value).append( $('<div></div>').addClass( 'boxContentRight' ).height( height ) );	
	});
	
	$('div.boxTitle').each( function(i, value){
		$(value).append( $('<div></div>').addClass( 'boxTitleLeft' ) );	
		$(value).append( $('<div></div>').addClass( 'boxTitleRight' ) );	
	});
	
	$('div.boxClose').each( function(i, value){
		$(value).append( $('<div></div>').addClass( 'boxCloseLeft' ) );	
		$(value).append( $('<div></div>').addClass( 'boxCloseRight' ) );	
	});
}


var popupBg 		= null;
var popup 			= null;
var popupContent	= null;
var popupOfflineContent = null;
var popupIframe		= null;
var loadingImage	= null;
var closeButton		= null;

function initPopupBuy()
{
	popupBg = $('<div></div>')
	.attr( "id", "popupBg" )
	.css("opacity", 0.7)
	.css("border-width", "1px");
	
	
	popup = $('<div></div>')
	.attr( "id", "popup" )
	.attr( "position", "relative" )
	.css( "width", "350px" );

	closeButton = $('<div></div>')
	.css( "text-align", "right" )
	.css( "right", "10px" )
	.css( "top", "10px" )
	.css( "position", "absolute" )
	.append(
		$('<img></img>')
		.attr( "src", WS_ROOT + "templates/images/v3_inchide_12x11.png" )
		.click( closeButtonClick )
		.css( "cursor", "pointer" )
	);
	
	loadingImage = $('<div></div>')
	.css( "margin-top", "80px" )
	.css( "margin-bottom", "100px" )
	.css( "text-align", "center" )
	.append( $('<img src="' + WS_ROOT + 'templates/images/loader.gif">') );
	
	popupContent = $('<div></div>').attr( "id", "popupContent");
	popupContent.append( loadingImage );
	
	popupOfflineContent = $('<div></div>').attr( "id", "popupOfflineContent");
	
	popupIframe = $('<iframe></iframe>')
	.attr( "id", "popupIframe" )
	.attr( "frameborder", 0 )
	.attr( "scrolling", "no" )
	.attr( "border", 0 )
	.css( "overflow", "hidden" );
	
	popup.append( closeButton );
	popup.append( popupContent );
	popup.append( popupOfflineContent );
	popup.append( popupIframe );
	
	
	$('body').append( popupBg );
	$('body').append( popup );
}

function showPopup( href )
{
	popup.width( 350 );
	popupIframe.width( 350 );
	popupBg.width( $(document).width() );
	popupBg.height( $(document).height() );
	popupBg.show();
	
	popupStartLoading();
	
	popup.css( "top", $(document).scrollTop() + 100 );
	popup.css( "left", Math.ceil( $(document).width() / 2 ) - 175 );
	popup.show();
	
	popupIframe.attr( "src", WS_ROOT + href );
}

function showPopupInfo( message, type )
{
	if( type == 'info' )
	{
		var title = "Mesaj";
		var textContainer = "textInfo";
	}
	else
	{
		var title = "Eroare";
		var textContainer = "textError";
	}
	
	
	popup.width( 420 );
	popup.height( 183 );
	
	popupBg.width( $(document).width() );
	popupBg.height( $(document).height() );
	popupBg.show();
	
	popupContent.hide();
	popupIframe.hide();
	
	popupOfflineContent.empty();
	
	popupOfflineContent.append( "<h1>" + title + "</h1>" );
	popupOfflineContent.append( "<div class='hr'></div>" );
	popupOfflineContent.append( "<div class='text " + textContainer + "'><table style='height: 100%;'><tr><td style='vertical-align: middle;'>" + message + "</td></tr></table></div>" );
	popupOfflineContent.append( "<a class='close' href='javascript:closePopupBuy();'></a>" );
	
	popupOfflineContent.show();
	
	popup.css( "top", $(document).scrollTop() + 100 );
	popup.css( "left", Math.ceil( $(document).width() / 2 ) - 210 );
	popup.show();
}

function popupCenter( width )
{
	popup.width( width );
	popup.css( "top", $(document).scrollTop() + 100 );
	popup.css( "left", Math.ceil( ( $(document).width() - width ) / 2 ) );
	popup.show();
}
function popupStartLoading()
{
	closeButton.hide();
	popupContent.show();
	popupIframe.hide();
}

function popupStopLoading()
{
	closeButton.show();
	popupContent.hide();
	popupIframe.show();
}

function onShowPopupBuySuccess( data )
{
	popupContent.empty();
	popupContent.append( data );
}

function closeButtonClick()
{
	closePopupBuy();
	
	location.href = location.href;
}

function closePopupBuy()
{
	popupBg.hide();
	popupOfflineContent.hide();
	popup.hide();
	
}

function validateBuy()
{
	$('#validateBuyLoading').show();
	$('#validateError').hide();
	params = {};
	params.page 		= "buy";
	params.action 		= "doBuy";
	params.code 		= $('#code').val();
	params.id_editions 	= $('#id_editions').val();
	
	$.get( "index.php", params, onValidateBuySuccess );
	
	return false;
}

function onValidateBuySuccess( data )
{
	$('#validateBuyLoading').hide();
	
	if( data == "0" )
	{
		$('#validateError').show();
	}
	else
	{
		popupContent.empty();
		popupContent.append( data );
	}
}
