
// functie generala de popup:
function LoadPopupBase( url, w, h, namepop )
{
	if (!w) w=400;
	if (!h) h=400;
	if (!namepop) namepop="Popup";

	//check and add base href
	var url_l = url.toLowerCase();
	var theBase = document.getElementsByTagName("base");
	var base_href = theBase[0].href.toLowerCase();
	if ( url.charAt(0) != '/' && url.substr(0, 7) != 'http://' && url.search(theBase[0].href) == -1 )
	{
		url = base_href + url;
	}

	var top = (screen.height-h)/2
	var left = (screen.width-w)/2;

	win = window.open( url, namepop, 'scrollbars=yes,status=no,toolbar=0,menubar=0,titlebar=0,resizable=0,dependent=1,z-lock=1,width='+w+',height='+h+',left='+left+',top='+top );

	if ( !win )
	{
		alert('It appears that pop-up blockers are enabled on your computer, which may prevent\nsome components from functioning properly. Please temporarily disable any\npop-up blockers.\n\nTo disable the Windows XP pop-up blocker, from the Tools menu in Internet Explorer,\nselect Pop-up Blocker, then Turn Off Pop-up Blocker. To disable other pop-up blockers\nthat are part of a search engine or toolbar, right-click on a toolbar in Internet Explorer\nand click to deselect all toolbars except Standard Buttons, Address Bar, and Links.');
	}
	else
	{
		win.focus();
	}
}


// functie generala de popup:
function LoadPopupBaseResize( url, w, h, namepop )
{
	if (!w) w=400;
	if (!h) h=400;
	if (!namepop) namepop="Popup";

	var top = (screen.height-h)/2
	var left = (screen.width-w)/2;

  	win = window.open( url, namepop, 'scrollbars=yes,status=no,toolbar=0,menubar=0,titlebar=0,resizable=1,dependent=1,z-lock=1,width='+w+',height='+h+',left='+left+',top='+top );

	if ( !win )
	{
		alert('It appears that pop-up blockers are enabled on your computer, which may prevent\nsome components from functioning properly. Please temporarily disable any\npop-up blockers.\n\nTo disable the Windows XP pop-up blocker, from the Tools menu in Internet Explorer,\nselect Pop-up Blocker, then Turn Off Pop-up Blocker. To disable other pop-up blockers\nthat are part of a search engine or toolbar, right-click on a toolbar in Internet Explorer\nand click to deselect all toolbars except Standard Buttons, Address Bar, and Links.');
	}
	else
	{
		win.focus();
	}
}

// functie pentru logare automata in forum:
function forum_set_login_form( userName, userPassword )
{
    userName = unescape( userName );

    var uname = window.frames['itest'].document.getElementById('navbar_username');
    if ( uname ) uname.value = userName;

    var upass = window.frames['itest'].document.getElementById('navbar_password');
    if ( upass ) upass.value = userPassword;

    window.frames['itest'].document.getElementById('flogin').submit();
}

str_host = "[0-9a-zA-Z]+(\.[-0-9a-zA-Z]+)+";
str_email = "[-_.0-9a-zA-Z]+([+][-_.0-9a-zA-Z]+)*@"+str_host;
pcre_email = new RegExp("^"+str_email+"$");

function check_subscribe()
{
    var pemail = document.getElementById('email_newsletter').value;
    if ( !pemail )
    {
		alert('Introduceti o adresa de email pentru a va putea abona!');
		return false;
    }
	if ( !pcre_email.test( pemail ) )
	{
		alert('Adresa de email este invalida!');
		return false;
	}
	return true;
}

function check_cautare()
{
    var s = document.getElementById('search').value;
    var f = document.getElementById('fsearch');

    if ( !s )
    {
		alert( 'Va rugam introduceti un text pentru cautare' );
        return false;
    }
	f.submit();
    return true;
}


function formatCurrency(num)
{
    //num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
    cents = "0" + cents;

    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));

    var newnumber = ((sign)?'':'-') + num;
    //if ( cents != '00' ) newnumber += '.' + cents;
    newnumber += '.' + cents;
    return ( newnumber );
}

function formatCurrencyINT(num)
{
    if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    num = Math.floor(num/100).toString();

    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));

    var newnumber = ((sign)?'':'-') + num;
    return ( newnumber );
}

function selectAll( nameSelect, act )
 {
	var sel = document.getElementById( nameSelect );

	for( i = 0 ; i < sel.length ; i++ )
	{
		sel.options[i].selected = act;
	}
 }


// popup pentru manager de fisiere:
function popupMyFiles ( link_mfiles )
{
	return window.open( link_mfiles, "popMyFiles", "width=575, height=425, status=no");
}


// popup pentru vizualizare imagine:
function viewImage ( camp_imagine )
{
	var el = document.getElementById( camp_imagine );
	if ( el.value )
		return window.open( el.value, "Image", "width=575, height=425, status=no");
	else
		return false;
}


// id-ul pentru campul de tip imagine:
var camp_imagine;

// preluare link imagine din popup-up:
function sendImage( link_fisier )
{
	if ( camp_imagine )
	{
		var el = document.getElementById( camp_imagine );
		el.value = link_fisier;
	}

	return false;
}

str_empty = "\\s*";
pcre_empty = new RegExp("^"+str_empty+"$");

function cautareFRM()
{
    var elem = document.getElementById( 'cautare' );
    var formS = document.getElementById( 'cautare_generala' );
    if ( elem && formS )
    {
        if ( !pcre_empty.test( elem.value ) )
        {
            document.location = './index.php?page=12&cautare_txt=' + elem.value;
			return false;
        }
    }
    return false;
}


function show_adresa_livrare( nr )
{
	var idAL = 'user_adresa_livrare_' + nr;
	var idACK = 'user_adresa_livrare_use_' + nr;
	var elemAL = document.getElementById( idAL );
	var elemACK = document.getElementById( idACK );

	if ( elemAL && elemACK )
	{

		if ( elemACK.checked )
		{
			elemAL.value = '';
			elemAL.disabled = true;
		}
		else
		{
			elemAL.disabled = false;
		}
	}
}

function show_date_firma()
{

	var elemPF = document.getElementById('pf');
	var elemPJ = document.getElementById('pj');
	var elemACK = document.getElementsByName('user_tip');

	if ( elemACK && elemACK[0].checked )
	{
		elemPF.style.display = 'block';
		elemPJ.style.display = 'none';
	}
	else
	{
		elemPF.style.display = 'none';
		elemPJ.style.display = 'block';
	}
}

function show_menu( id )
{
	var elemSec = document.getElementById( id + '_second' );
	var elemSh = document.getElementById( id + '_show' );

	if ( !elemSec )
	{
		return false;
	}
	if ( elemSec.style.display == 'none' )
	{
		elemSec.style.display = 'block';
		if ( elemSh ) elemSh.innerHTML = 'Ascunde categoriile';
	}
	else
	{
		elemSec.style.display = 'none';
		if ( elemSh ) elemSh.innerHTML = 'Toate categoriile';
	}
}


function activate_menu( id )
{
	var elemSec = document.getElementById( id + '_second' );
	var elemSh = document.getElementById( id + '_show' );

	if ( !elemSec )
	{
		return false;
	}
	elemSec.style.display = 'block';
	if ( elemSh ) elemSh.innerHTML = 'Ascunde categoriile';
}


// alerte pentru campurile lasate libere in formularul de login
function check_login( )
{
    var mail = document.getElementById('email_login').value;
    var parola = document.getElementById('parola_login').value;
	var f = document.getElementById( 'loginf' ).value;

    if ( !mail )
    {
		alert( 'Introduceti adresa de email' );
        return false;
    }
	else
	if ( !pcre_email.test( mail ) )
	{
		alert('Adresa de email este invalida!');
		return false;
	}

	if ( !parola )
	{
		alert( 'Introduceti parola' );
        return false;
	}

    return true;
}


// alerte pentru campurile lasate libere in formularul de autentificare intermediara
function check_auth( )
{
    var mail = document.getElementById('auth_user').value;
    var parola = document.getElementById('auth_pass').value;
	var f = document.getElementById( 'firstForm2' ).value;

    if ( !mail )
    {
		alert( 'Introduceti adresa de email' );
        return false;
    }
	else
	if ( !pcre_email.test( mail ) )
	{
		alert('Adresa de email este invalida!');
		return false;
	}

	if ( !parola )
	{
		alert( 'Introduceti parola' );
        return false;
	}

    return true;
}


String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}