$j(document).ready(function(){
    $j('div#idRecomanare').live('click',function() 
    {
        $j('div#idRecomandareContainer').slideToggle('fast');
    });
});

$j(document).ready(function(){
    $j('#idRecomandareButton').live('click',function(){
        var targetEmail = $j('#idRecomandareTarget').attr('value');
        var prodId      = $j('#prodId').val();
        var shopName      = $j('#shopName').val();
        
        // Get product No.
        /*var magMe = $j('.magMe').get(0);      
        var fullPath = magMe.parentNode.href;
        var pos1     = fullPath.lastIndexOf('/');
        var pos2     = fullPath.lastIndexOf('/', pos1-1);
        var prodNo   = fullPath.substr(pos2+1, pos1-pos2-1);
        */
               	
        if( targetEmail == "" ){
            //show alert
            errorText = escape("Introdu emailul prietenului tau!");
            var alertURL = webroot + "messages/general-error/text/" + errorText + "/";
            ALERT_show('', alertURL);
            //eof show alert 
            return;        
        }
        if( ! verifyEmail( targetEmail ) )
        { 
            errorText = escape(targetEmail + ', nu este un email valid');
            var alertURL = webroot + "messages/general-error/text/" + errorText + "/";
            ALERT_show('', alertURL);  
            return;
        }
        

        var xhr = $j.get('/default/product/recomanda-unui-prieten-process/tEmail/'+targetEmail+'/prodId/'+prodId+'/shopName/'+shopName, 
        {},
        function( )
        {
         if( xhr.status == 200 ) 
         {
            errorText = escape('Emailul a fost trimis cu succes.');
            var alertURL = webroot + "messages/general-error/text/" + errorText + "/";
            ALERT_show('', alertURL); 
         }
         else 
         {
            errorText = escape('Eroare - emailul nu a fost trimis.');
            var alertURL = webroot + "messages/general-error/text/" + errorText + "/";
            ALERT_show('', alertURL); 
         }   
        });
    });
});

function verifyEmail(email)
{
    var status = false;     
    var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
    if (email.search(emailRegEx) == -1) return false;
    else return true;
}