/// <changes>
/// 2008-09-10  SS      Added   function for text formating
/// 2008-09-12  AP      Added Function ControlText
/// 2008-10-20  EF      Fix BUG (one extra character was inserted at word breaks) in ControlText() function
/// 2008-10-20  EF      Use <wbr/> tag instead of white space to break lines in ControlText() function
///	2008-10-24	EF		Process correctly html encoded texts in ControlText() function
/// 2008-10-30  SS      Added logic to put '...' at end of text
/// </changes>
function DateStringToDate(dateString, dateFormat)
{
    var date = new Date();
    var fstTemp = new String();
    if(dateString.substr(0,1) == '0')
        fstTemp = parseInt(dateString.substr(1,1));
    else
        fstTemp = parseInt(dateString.substr(0,2));
    var secTemp = new String();
    if(dateString.substr(3,1) == '0')
        secTemp = parseInt(dateString.substr(4,1));
    else
        secTemp = parseInt(dateString.substr(3,2));
    var trdTemp = parseInt(dateString.substr(6,4));
    
    if (dateFormat.substr(0,5) == 'dd/mm' || dateFormat.substr(0,5) == 'dd-mm')
    {
        date.setMonth(secTemp - 1);
        date.setDate(fstTemp);
        date.setMonth(secTemp - 1);
    }
    else
    {
        date.setMonth(fstTemp - 1);
        date.setDate(secTemp);
        date.setMonth(fstTemp - 1);
    }
    date.setFullYear(trdTemp);

    return date;
}

function DateToDateString(date, dateFormat)
{
    var dateString;
    if (dateFormat.substr(0,5) == 'dd/mm' || dateFormat.substr(0,5) == 'dd-mm')
        dateString = PadLeft(date.getDate(), 2, '0') + "/" + PadLeft((date.getMonth() + 1), 2, '0') + "/" + date.getFullYear();
    else
        dateString = PadLeft((date.getMonth() + 1), 2, '0') + "/" + PadLeft(date.getDate(), 2, '0') + "/" + date.getFullYear();
    if(dateFormat.substr(11,5) == 'hh:mm')
        dateString += " " + PadLeft(date.getHours(), 2, '0') + ":" + PadLeft(date.getMinutes(), 2, '0');
    if(dateFormat.substr(16,3) == ':ss')
        dateString += ":" + PadLeft(date.getSeconds(), 2, '0');
    return dateString;
}

//2008-07-22    AP  :This function validate if a string is a number.
function IsIntegerNumber(strString)
{
   var strValidChars = "0123456789";
   var strChar;
   var bResult = true;

   if (strString.length == 0) 
    return false;
    
   //  test strString consists of valid characters listed above
    for (i = 0; i < strString.length && bResult == true; i++){
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         bResult = false;
    }
   return bResult;
}

// 2008-09-10   SS      Added   function for text formating
 function Trim(obj)
{
    obj.value = obj.value.replace(/^\s+|\s+$/g, '');
} 



//  2008-08-19    AP  : Control the format of string.
//  2008-10-20  EF      Fix BUG (one extra character was inserted at word breaks)
//  2008-10-20  EF      Use <wbr/> tag instead of white space to break lines
//	2008-10-24	EF		Process correctly html encoded texts in ControlText() function
//	2008-10-30	SS		Added optional param nMaxlength to insert '...' at end of the string
function ControlText(sText, nLong, nMaxlength)
{
//    return ControlTextFull(sText, nLong, true);
      return ControlTextFull(sText, nLong, nMaxlength, true);
}

function ControlTextFull(sText, nLong, nMaxlength, isHtmlEncoded)
{
    var test= 0;
    var escapedMaxlength = 5;
 
// alert('controltext: sText = "' + sText + '"');
//    alert('controltext: sText = "' + sText + '", nLong = ' + nLong + ', isHtmlEncoded = "' + isHtmlEncoded + '"');
//    if (isHtmlEncoded)
//    {
//        alert('encoded');
//    }
    if (StringLength(length) <= nLong)
    {
	    return sText;
    }
    var count = 0;
    var lengthAll = 0;
    var character;
    var i;
    var ret = '';
    var escapeSequence = false;
    var escapeSequence2 = false;
   
    for(var i = 0; i < sText.length; i++)
    {
    
        character = sText.charAt(i);
        
        if (character == '&' && isHtmlEncoded)
        {
            escapeSequence = true;
        }
        else if (character == ';' && escapeSequence)
        {
            escapeSequence = false;
        }
        
        if (character == '<' && isHtmlEncoded)
        {
            escapeSequence2 = true;
        }
        else if (character == '>' && escapeSequence2)
        {
            escapeSequence2 = false;
        }
        
        ret += character;
        
//        if (i < 30)
//        {
//            alert('char: ' + character + ', escapeSequence: ' + escapeSequence + ', i: ' + i + ', ret: ' + ret);
//        }
//        
        if (escapeSequence || escapeSequence2)
        {
            
            continue;
        }
        
        lengthAll++;
        
//        if(character == ' ')
//        if(character == '&hairsp;')
        if(character == ' ')
        {
            count = 0;
        }
        else
        {
            count ++;
        }
        
        if(count >= nLong)
        {
            ret += '<wbr />';
//            ret += '&hairsp;';
//            ret += ' ';
//            ret += character + ' ';
            count = 0;
        }   
    }
    // SS Remove excedent BRs
    ret = RemoveExtraLineBreaks(ret);
    if (typeof(nMaxlength) != "undefined")
    {
      
        if (lengthAll >= nMaxlength)
        {
            ret = StringDotted(ret, nMaxlength  );
        }
    }
     

    return ret;
}

function StringLength(sText, bIsHtmlEncoded)
{
	if (bIsHtmlEncoded)
	{
		var count = 0;
		var character;
		var escapeSequence = false;
		var escapeSequence2 = false;

		for (var i = 0; i < sText.length; i++)
		{
			character = sText.charAt(i);

			if (character == '&' && bIsHtmlEncoded)
			{
				escapeSequence = true;
			}
			else if (character == ';' && escapeSequence)
			{
				escapeSequence = false;
			}

            if (character == '<' && bIsHtmlEncoded)
            {
                escapeSequence2 = true;
            }
            else if (character == '>' && escapeSequence2)
            {
                escapeSequence2 = false;
            }
			
			if (!escapeSequence && !escapeSequence2)
			{
				count++;
			}
		}

		return count;
	}
	else
	{
		return sText.length;
	}
}

//2008-10-30    SS      Added logic to put '...' at end of text
function StringDotted(sText, nMaxlength)
{
    if (StringLength(sText, true) <= nMaxlength)
    {
        return sText;
    }
    else
    {
  
        var mappedMaxlength = MapStringPosition(sText, nMaxlength, true);
        var character = sText.substring(mappedMaxlength, mappedMaxlength + 1); 
        // see if after cut are ; or >      
        if (character == ">" || character == ";" )
        {
            sText = sText.substring(0, mappedMaxlength + 1);
        }
        else
        {
            sText =  sText.substring(0, MapStringPosition(sText, nMaxlength - 3, true) + 1); 
        }     
      
        return sText + "...";
        
    }
       
}


function MapStringPosition(sText, nPosition, bIsHtmlEncoded)
{
	if (bIsHtmlEncoded)
	{
		var count = 0;
		var character;
		var escapeSequence = false;
		var escapeSequence2 = false;
		var i;

		for (i = 0; i < sText.length; i++)
		{
			character = sText.charAt(i);

			if (character == '&' && bIsHtmlEncoded)
			{
				escapeSequence = true;
			}
			else if (character == ';' && escapeSequence)
			{
				escapeSequence = false;
			}

            if (character == '<' && bIsHtmlEncoded)
            {
                escapeSequence2 = true;
            }
            else if (character == '>' && escapeSequence2)
            {
                escapeSequence2 = false;
            }
			
			if (escapeSequence || escapeSequence2)
			{
			    continue;
			}
			
			if (count == nPosition)
			{
			    //  Position found, return real character position
				return i;

			}
			
			count++;
		}

        //  If nPosition was not found (string was shorter than expected), return nPosition

		return nPosition;
	}
	else
	{
		return nPosition;
	}
}

function RemoveExtraLineBreaks(sText)
{
    //SS    Remove excedent BRs
    var patron = "(<\\s*[bB][rR]\\s*/{0,1}>){2,}";
    return sText.replace(new  RegExp(patron, "g"), "<br />") ;
   
}