/*
 * Utils.java
 *
 * Created on October 12, 2007, 9:39 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 *
 * @author lluis
 */
public class Utils {
    
    /** Creates a new instance of Utils */
    public Utils() {
    }
    
	public static String unicodeToHTML( String text )
	{
		if( text == null || text.length() < 6 )
		{
			return text;
		}

		String output = text;
		output = output.replace( "\\u0009", " " );
		output = output.replace( "\\u000a", "<br>" );

		output = output.replace( "\\u00a0", "&nbsp;" );
		output = output.replace( "\\u00aa", "&ordf;" );
		output = output.replace( "\\u00ba", "&ordm;" );

		output = output.replace( "\\u00c0", "À" );
		output = output.replace( "\\u00c1", "Á" );
		output = output.replace( "\\u00c2", "Â" );
		output = output.replace( "\\u00c3", "Ã" );
		output = output.replace( "\\u00c7", "Ç" );
		output = output.replace( "\\u00c8", "È" );
		output = output.replace( "\\u00c9", "É" );
                
		output = output.replace( "\\u00ca", "Ê" );
		output = output.replace( "\\u00cc", "Ì" );
		output = output.replace( "\\u00cd", "Í" );
		output = output.replace( "\\u00ce", "Î" );
		output = output.replace( "\\u00d2", "ò" );
		output = output.replace( "\\u00d3", "ó" );
		output = output.replace( "\\u00d4", "ô" );
		output = output.replace( "\\u00d5", "õ" );
		output = output.replace( "\\u00d9", "Ù" );
		output = output.replace( "\\u00da", "Ú" );
		output = output.replace( "\\u00db", "Û" );

		output = output.replace( "\\u00e0", "à" );
		output = output.replace( "\\u00e1", "á" );
		output = output.replace( "\\u00e2", "â" );
		output = output.replace( "\\u00e3", "ã" );
		output = output.replace( "\\u00e7", "ç" );
		output = output.replace( "\\u00e8", "è" );
		output = output.replace( "\\u00e9", "é" );
		output = output.replace( "\\u00ea", "ê" );
		output = output.replace( "\\u00ec", "ì" );
		output = output.replace( "\\u00ed", "í" );
		output = output.replace( "\\u00ee", "î" );
		output = output.replace( "\\u00f2", "ò" );
		output = output.replace( "\\u00f3", "ó" );
		output = output.replace( "\\u00f4", "ô" );
		output = output.replace( "\\u00f5", "õ" );
		output = output.replace( "\\u00f9", "ù" );
		output = output.replace( "\\u00fa", "ú" );
		output = output.replace( "\\u00fb", "û" );

		output = output.replace( "\\u0153", "&#156;" );

		output = output.replace( "\\u2013", "-" );
		output = output.replace( "\\u2014", "-" );
		output = output.replace( "\\u2018|\\u2019", "'" );
		output = output.replace( "\\u201c|\\u201d", "\"" );

		output = output.replace( "\\u2022", "*" );
		output = output.replace( "\\u2026", "..." );

		return output;
	}    
        
    public static String getPageFrom(String referer)
    {
        String pageFrom = referer.substring(referer.lastIndexOf("/")+1);
        return pageFrom;
    } 
    
    public static boolean isValidEmail(String email){
        boolean res = true;
        
        int indexOfAtChar=email.indexOf("@");

        if(indexOfAtChar > 0)
        {
            int indexOfDotChar =
                email.indexOf(".",indexOfAtChar);
            if(indexOfDotChar > 0)
            {
              res = true;
            }
            else
            {
                res = false;
            }
        }
        else{
            res = false;
        }        
        return res;
    }        
    
    public static String dateToYYYYMMDD(Date ddate)
    {
        Calendar cal = new GregorianCalendar();
        cal.setTime(ddate);  
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        String date_str = dateFormat.format(cal.getTime());
        return date_str;
    }
    
}
