/*
 * 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.replaceAll( "\\\\u0009", " " );
		output = output.replaceAll( "\\\\u000a", "<br>" );

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

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

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

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

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

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

		return output;
	} 
        
	public static String textToUnicode( String text )
        {
		String output = text;    
                
		output = output.replaceAll( "À", "\\\\\\\\\\\\\\\\u00c0" );
		output = output.replaceAll( "Á", "\\\\\\\\\\\\\\\\u00c1" );
		output = output.replaceAll( "Â", "\\\\\\\\\\\\\\\\u00c2" );
		output = output.replaceAll( "Ã", "\\\\\\\\\\\\\\\\u00c3" );
		output = output.replaceAll( "Ç", "\\\\\\\\\\\\\\\\u00c7" );
		output = output.replaceAll( "È", "\\\\\\\\\\\\\\\\u00c8" );
		output = output.replaceAll( "É", "\\\\\\\\\\\\\\\\u00c9" );
		output = output.replaceAll( "Ó", "\\\\\\\\\\\\\\\\u00d3" );
		output = output.replaceAll( "Ò", "\\\\\\\\\\\\\\\\u00d2" );                
                
		output = output.replaceAll( "Ê", "\\\\\\\\\\\\\\\\u00ca" );
		output = output.replaceAll( "Ì", "\\\\\\\\\\\\\\\\u00cc" );
		output = output.replaceAll( "Í", "\\\\\\\\\\\\\\\\u00cd" );
		output = output.replaceAll( "Î", "\\\\\\\\\\\\\\\\u00ce" );
		output = output.replaceAll( "ò", "\\\\\\\\\\\\\\\\u00d2" );
		output = output.replaceAll( "ó", "\\\\\\\\\\\\\\\\u00d3" );
		output = output.replaceAll( "ô", "\\\\\\\\\\\\\\\\u00d4" );
		output = output.replaceAll( "õ", "\\\\\\\\\\\\\\\\u00d5" );
		output = output.replaceAll( "Ù", "\\\\\\\\\\\\\\\\u00d9" );
		output = output.replaceAll( "Ú", "\\\\\\\\\\\\\\\\u00da" );
		output = output.replaceAll( "Û", "\\\\\\\\\\\\\\\\u00db" );

		output = output.replaceAll( "à", "\\\\\\\\\\\\\\\\u00e0" );
		output = output.replaceAll( "á", "\\\\\\\\\\\\\\\\u00e1" );
		output = output.replaceAll( "â", "\\\\\\\\\\\\\\\\u00e2" );
		output = output.replaceAll( "ã", "\\\\\\\\\\\\\\\\u00e3" );
		output = output.replaceAll( "ç", "\\\\\\\\\\\\\\\\u00e7" );
		output = output.replaceAll( "è", "\\\\\\\\\\\\\\\\u00e8" );
		output = output.replaceAll( "é", "\\\\\\\\\\\\\\\\u00e9" );
		output = output.replaceAll( "ê", "\\\\\\\\\\\\\\\\u00ea" );
		output = output.replaceAll( "ì", "\\\\\\\\\\\\\\\\u00ec" );
		output = output.replaceAll( "í", "\\\\\\\\\\\\\\\\u00ed" );
		output = output.replaceAll( "î", "\\\\\\\\\\\\\\\\u00ee" );
		output = output.replaceAll( "ò", "\\\\\\\\\\\\\\\\u00f2" );
		output = output.replaceAll( "ó", "\\\\\\\\\\\\\\\\u00f3" );
		output = output.replaceAll( "ô", "\\\\\\\\\\\\\\\\u00f4" );
		output = output.replaceAll( "õ", "\\\\\\\\\\\\\\\\u00f5" );
		output = output.replaceAll( "ù", "\\\\\\\\\\\\\\\\u00f9" );
		output = output.replaceAll( "ú", "\\\\\\\\\\\\\\\\u00fa" );
		output = output.replaceAll( "û", "\\\\\\\\\\\\\\\\u00fb" );    
                
		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;
    }
    
    public static String timeToHHMM(String ttime) throws Exception
    {
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");
        //String s = formatter.format(date);  
        Date date = (Date)formatter.parse(ttime);  
        return formatter.format(date);
    }
    
}
