
var default_date_format = new Array();
default_date_format[0] = "dd-mm-yyyy";
default_date_format[1] = "-";

function mysql_date_format( scr_dta_format )
{
    var scr_dta_format_params = scr_dta_format.split( default_date_format[1] );
    var scr_dta_format_elems = default_date_format[0].split( default_date_format[1] );
    
    if( scr_dta_format_params.length != 3 || scr_dta_format_elems.length != 3 )
    {
        return scr_dta_format;
    }
    
    mysql_dta_param = new Array();
    
    for( key in scr_dta_format_elems )
    {
        switch( scr_dta_format_elems[key] )
        {
            case "dd":
                mysql_dta_param[2] = scr_dta_format_params[key];
            break;
            
            case "mm":
                mysql_dta_param[1] = scr_dta_format_params[key];
            break;
            
            case "yyyy":
                mysql_dta_param[0] = scr_dta_format_params[key];
            break;
        }
    }
    
    var mysql_dta_format = "";
    //~ ksort( mysql_dta_param );
    mysql_dta_format = mysql_dta_param.join( "-" );
    
    return mysql_dta_format;
}

function getHTTPObject() 
{
	var xmlhttp;
	
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    // JScript gives us Conditional compilation, we can cope with old IE versions.
    // and security blocked creation of the objects.
     try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
      try {
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
       xmlhttp = false;
      }
     }
    @end @*/

        if (!xmlhttp && typeof XMLHttpRequest != 'undefined') 
        {
            try 
            {
                xmlhttp = new XMLHttpRequest();
            } 
            catch (e) 
            {
                xmlhttp = false;
            }
        }
        return xmlhttp;
}

function is_email( str )
{
    var at = "@";
    var dot = ".";
    var lat = str.indexOf(at);
    var lstr = str.length;
    var ldot = str.indexOf(dot);
    
    if ( str.indexOf(at) == -1 )
    {
       return false;
    }

    if ( str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr )
    {
       return false;
    }

    if ( str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr )
    {
        return false;
    }

     if ( str.indexOf(at,(lat+1)) != -1 )
     {
        return false;
     }

     if ( str.substring(lat-1,lat) == dot || str.substring(lat+1,lat+2) == dot )
     {
        return false;
     }

     if ( str.indexOf(dot,(lat+2)) == -1 )
     {
        return false;
     }
    
     if ( str.indexOf(" ") != -1 )
     {
        return false;
     }

     return true;
}



//~ aici

/*STRING FILTER*/
function stringFilter (s)
{

	//~ filteredValues = "qwertyuiop[]\asdfghjkl;'zxcvbnm,/QWERTYUIOP{}|ASDFGHJKL:ZXCVBNM<>?`~-_=+!@#$%^&*()\" ";
	filteredValues = "-0123456789.";
	
	var i;
	var returnString = "";
	for (i = 0; i < s.length; i++)
	{
		var c = s.charAt(i);
		if (filteredValues.indexOf(c) != -1) returnString += c;
	}

	return returnString;
}

function ValidateNumber()
{
	var p='';
	var rest = '';
	var sign = sas.value.substr( 0 , 1 );
    
    if( sign == '-' )
    {
        var unsigned = sas.value.substr( 1 , sas.value.length );
    }
    else
    {
        sign = '';
        var unsigned = sas.value;
    }
    
    //~ var first = sas.value.split(".");
    var first = unsigned.split(".");
	
	if(first.length>1)
    {
        if(first[1].length<=2) rest="."+stringFilter(first[1]);
        else
        {
            rest=stringFilter(first[1]);
            rest="."+rest.substr(0, 2);
        }
    }
		
	p1=stringFilter(first[0]);
	
	var mijloc='';
	var k=0;
	if(p1.length>1)
	{
		for(i=0;i<p1.length;i++)
		{
			if(p1.charAt(0)==0 && p1.charAt(i)==0 && k==0)
			{
				mijloc='';
			}
			else
			{
				mijloc += p1.charAt(i);
				k++;
			}
		}
		p1 = mijloc;
	}
	
	p=p1;
	len1=p.length;
	ram=len1%3;
	grup=(len1-ram)/3;
	afisare=p.substring(0,ram);
	
	for(i=0;i<grup;i++)
	{
		if((ram==0)&&(i==0)){ad='';}
		else{ad=',';}
		
		afisare=afisare+ad+p.substring(i*3+ram,(i+1)*3+ram);
	}
	
	if(!afisare) afisare = '0';
	if(!rest) rest = '.00';
	if(rest.length == 2) rest +='0';
	if(rest.length == 1) rest +='00';
	
	if( sign == '-' )
    {
        sas.value = sign+afisare+rest;
    }
    else
    {
        sas.value = afisare+rest;
    }
}

/*GET IT*/
function make_float(m)
{
	n=m.name;
	sas=m;
	ValidateNumber(sas);
}

function clean_float(m)
{
	var rez='';
	
	rez = m.value;
	
	if(rez[0] == 0) m.value = '0.00';
	else m.value = stringFilter(m.value);
    
    m.select();
}

function validate_float( m , e , strCheck)
{
	var strCheck = '-0123456789.';
    var key = '';

	var whichCode = (window.Event) ? e.which : e.keyCode;
	
	key = String.fromCharCode(whichCode);
	
	if(e.ctrlKey) return true;
	if(e.keyCode==8 || e.keyCode==35 || e.keyCode==36 || e.keyCode==37 || e.keyCode==39 || e.keyCode==46 || e.keyCode==9) return true;

	if (strCheck.indexOf(key) != -1) return true;
	
	return false;
}


function validate_int( m , e , strCheck)
{
    var strCheck = '0123456789';
    var key = '';

	var whichCode = (window.Event) ? e.which : e.keyCode;
	
	key = String.fromCharCode(whichCode);

	if(e.ctrlKey) return true;
	//~ if(e.keyCode==8 || e.keyCode==35 || e.keyCode==36 || e.keyCode==37 || e.keyCode==39 || e.keyCode==46 || e.keyCode==9) return true;
	if(e.keyCode==8 || e.keyCode==35 || e.keyCode==36 || e.keyCode==37 || e.keyCode==39 || e.keyCode==9) return true;

	if (strCheck.indexOf(key) != -1) return true;
	
	return false;
}

function validate_break( m , e , strCheck)
{
    var strCheck = "0123456789abcdefghijklmnopstuvxyzABCDEFGHIJKLMNOPRSTUVXYZ()<>'_-. ";
    var key = '';

	var whichCode = (window.Event) ? e.which : e.keyCode;
	
	key = String.fromCharCode(whichCode);

	if(e.ctrlKey) return true;
	//~ if(e.keyCode==8 || e.keyCode==35 || e.keyCode==36 || e.keyCode==37 || e.keyCode==39 || e.keyCode==46 || e.keyCode==9) return true;
	if(e.keyCode==8 || e.keyCode==35 || e.keyCode==36 || e.keyCode==37 || e.keyCode==39 || e.keyCode==9) return true;

	if (strCheck.indexOf(key) != -1) return true;
	
	return false;
}

// window popup,centrat pe mijlocul paginii
function myopen(filename , w , h)
{

	var l = (screen.width / 2) - (w / 2);
	var t = (screen.height / 2) - (h / 2);

    win = window.open( filename, 'Write_message', 'height='+h+',width='+w+',left='+l+',top='+t+',resizable=no,scrollbars=yes' );
    
	win.moveTo( screen.width/2-w/2, screen.height/2-h/2);
	win.focus();
	
    
}

function count_array( vect )
{
    var elem = 0;
    
    for( x in vect )
    {
        if( vect[x] != '' ) ++elem;
    }
    
    return elem;
}

function array_first_elem( vect )
{
    for( x in vect )
        return vect[x];
}

function array_join( vect )
{
    var first_time = 0;
    var join_vect = "";
    
    for( x in vect )
    {
        if( vect[x] != '' || vect[x] == 0 )
        {
            if( first_time == 0 )
            {
                join_vect += vect[x];
                ++first_time;
            }
            else
            {
                join_vect += ","+vect[x];
            }
        }
    }
    
    return join_vect;
}

function getLastValidElement( _vect , _elem )
{
    if ( _elem )
    {
        var nr_elem = _elem;
    }
    else
    {
        var nr_elem = _vect.length - 2;
    }
    
    if ( _vect[nr_elem] || _vect[nr_elem] == 0 )
    {
        return _vect[nr_elem];
    }
    else if ( nr_elem > 0 )
    {
        return getLastValidElement( _vect , nr_elem - 1 );
    }
    else
    {
        return -1;
    }
}

function encodeUtf8( string ) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    }

function setLanguage( _lng_id )
{
    $.get( site_url + "language/" + _lng_id + "/set-language.html" , function(data){
      window.location.href=window.location.href;
	  
      return false;
    });
}

function setLanguage2( _lng_id )
{
    $.get( site_url + "language/" + _lng_id + "/set-language.html" , function(data){
      window.location.href='index.php';
	  
      return false;
    });
}


function setCurrency()
{
    var cur_id = $( '#currency_selection' ).val();
    
    $.get( site_url + "currency/" + cur_id + "/set-currency.html" , function(data){
      window.location.href=window.location.href;
      return false;
    });
}


function registerEmail()
{
    var email_address = $('#news_email_address').val();
        email_address = encodeUtf8( email_address );
    
    $.getJSON('index.php?page=28&email_address=' + email_address , function( data ){
            $('#email_success_message').fadeOut('fast');
            $('#email_wrong_message').fadeOut('fast');
            $('#email_exists_message').fadeOut('fast');
            
            $('#' + data['received_text']).fadeIn('fast');
        });
}

function registerEmailNewsletter()
{
    var email_address = $('#newsletter_news_email_address').val();
        email_address = encodeUtf8( email_address );
    
    $.getJSON('index.php?page=28&email_address=' + email_address + '&source=newsletter', function( data ){
            $('#newsletter_email_success_message').fadeOut('fast');
            $('#newsletter_email_wrong_message').fadeOut('fast');
            $('#newsletter_email_exists_message').fadeOut('fast');
            
            $('#' + data['received_text']).fadeIn('fast');
        });
}