﻿/* File:            HTML_utils.js 
*  Author:          Alessandro Piccione
*  Last revision:   18/09/2008
*/

var bgColor = "#FFF";
var textColor = "#000";

var divMessage_baseBackgroundColor = "#FFF";

var ctrlErrorBackgroundColor = "#F00";
var ctrlErrorForeColor = "#FFF";
var is_IE = /msie/i.test(navigator.userAgent);


function getObj(objId) 
{
    return document.getElementById(objId);
}

function setControlInErrorMode(ctrl)
{
    ctrl.style.backgroundColor = ctrlErrorBackgroundColor;
    ctrl.style.color = ctrlErrorForeColor;
    ctrl.fontWeight = "bold";
}

function setControlInNormalMode(ctrl)
{
    ctrl.style.backgroundColor = "";
    ctrl.style.color = "";
    ctrl.fontWeight = "";
}

function isNumeric(value)
{
    var validChars = "0123456789.,-";
    var result=true;
    var char;
 
    //for (i = 0; i < value.length && result == true; i++) 
    for (i = 0; i < value.length; i++) 
    { 
        char = value.charAt(i); 
        if (validChars.indexOf(char) == -1) 
        {
            result = false;
            break;
        }
    }
    return result;
} 



/***  DIV message ***/
/// parameter text: the text to show.
/// parameter type: null = normal message rendering , "warning" = warning rendering, "error" = error rendering.
function showMessage(text ,type ,seconds)
{
    //debugger;
    var divMessage = document.getElementById("divClientMessage");
    if (divMessage != null)
    {
        var blinkColorStart;
        var blinkColorEnd;
                
        //divMessage.style.remove["background-color"]; 
        //divMessage.style.remove["display"];
        divMessage.style.display = "";
        //divMessage.style.backgroundColor = divMessage_baseBackgroundColor;
        divMessage.style.color = "#000";
        
        divMessage.innerHTML = text;
        
        if (type != null)
        {
            if (type == "error")
            {
                blinkColorStart = "#EC3E00";
                blinkColorEnd = "Red"
            }
            else if (type == "warning")
            {
                blinkColorStart = "#EB8900";
                blinkColorEnd = "#DE6809";
            }
        }
        
        blink("divClientMessage", blinkColorStart, blinkColorEnd, 8);
    }
    else
    {
        alert("Error in HTML_utils.js - showMessage(): ");
    }
    
    if(seconds)
    {
        setTimeout("clearMessage();" ,seconds*1000);
    } 
}
                  

function clearMessage()
{
    var divMessage = document.getElementById("divClientMessage");
    divMessage.style.display = "none";
}



/// parameter title can be null or empty string, it was not render.
/// parameter autoCloseSeconds set the second after the autoclose is maked, pass 0 is OFF.
function showWindowMessageCentered(title, text, width, height, closeButtonText, autoCloseSeconds, modal)
{
//debugger;

    var id = "window_message_" + getRandomInt(999999999).toString();
    
    var left = (getClientWidth() - width)/2;
    var top = (getClientHeight() - height)/2;
    top -= 20;
    
    var tableStyle = "width:"+width+"px; border-width:2px; background-color:"+bgColor+";";
    var tableCssClass = "borderColor_7"  ;
    
    var tr_title = "";
    
    var span = document.createElement("SPAN");
    
    if (title && title != "")
    {
        tr_title = "<tr class=\"tr_title\"><td style=\"text-align:center;\">"+title+"</td></tr>"
    }
    
    var s_table = "<TABLE id=\""+id+"\" class=\""+tableCssClass+"\" style=\""+tableStyle+"\" width=\""+width+"\" height=\""+height+"\">"
        + tr_title
        + "<tr class=\"tr_content\" style=\"height:90%;background-color:"+bgColor+"; color:"+textColor+";\"><td style=\"text-align:center;\">"+text+"</td></tr>";
        
    if (closeButtonText && (closeButtonText != ""))
    {
        s_table += "<tr class=\"tr_subtitle\"><td class=\"hand\" style=\"text-align:center;\"><div onclick=\"hideWindowMessage('"+id+"');\">"+closeButtonText+"</div></td></tr>";
    }
    
    s_table += "</TABLE>";
 
    //alert("table: " + s_table);   
    span.innerHTML = s_table;
    
    span.style.position = "absolute";
    span.style.left=left + "px";
    span.style.top=top + "px";
    
    
    /// Create a empty iframe for hide all the combobox of IE
    var iframeMessage = document.createElement("IFRAME");
    iframeMessage.setAttribute("id", "iframe_" + id);
        
    iframeMessage.style.overflow = "hidden";
    iframeMessage.style.position = "absolute";  
   
    
    if(modal)
    {
        iframeMessage.style.left = "0px";
        iframeMessage.style.top = "0px";
        iframeMessage.style.border = "0";
        //iframeMessage.style.border = "solid 5px #FF9933";
        //iframeMessage.style.visibility = "hidden";
        iframeMessage.width = getClientWidth();
        iframeMessage.height = getClientHeight();
        
        /// Alessandro 28/02/2008
        /// prevent page scroll pass the transparent background
        iframeMessage.style.position = "absolute";

          
        if(is_IE || 1==1) /// is IE
        {
            iframeMessage.setAttribute("allowtransparency", "true");
            /// style=filter: chroma(color="#FFFFFF"); ///
            iframeMessage.style.filter = "chroma(color=\"#FFFFFF\")";

            iframeMessage.allowtransparency = "true";
            /// *** HARD CODED PATH
            iframeMessage.src = "ModalBackground.html";
            //iframeMessage.style.background = "#555555";
        }
        else
        {
            /// *** HARD CODED PATH
            iframeMessage.style.backgroundImage = "url(\"Images/bg_trasp_50_gray_128.gif\")";
        }
                
    }
    else
    {
        iframeMessage.style.left = left + "px";
        iframeMessage.style.top = top + "px";
        iframeMessage.style.border = "0";
        //iframeMessage.style.border = "solid 5px Red";
        iframeMessage.width = width;
        iframeMessage.height = height;
    }
    
    iframeMessage.frameborder = "0";
    
   
    document.body.appendChild(iframeMessage);
    document.body.appendChild(span);

    //alert(span.height);

    try
    {
        var ms = autoCloseSeconds*1000;
        
        //alert("seconds=" + seconds);
        
        if(ms > 0)
        {
        //debugger;
            setTimeout("hideWindowMessage('"+id+"');",ms);
        }
    }
    catch(error)
    { }
    
    return id;
}

function hideWindowMessage(windowId)
{
    /// reset display of combo
    /// if(is_IE) resetVisibilityOfIECombos(windowId);

    try
    {
        var windowMessage = document.getElementById(windowId);
        windowMessage.style.display = "none";
        
        var iframeWindowMessage = document.getElementById("iframe_" + windowId);
        iframeWindowMessage.style.display = "none";
    }
    catch(error) {/*alert(error);*/}
}


var hiddenIECombosId;
/// hide the  ///
function HideIECombos(windowId)
{
    var arSelect = document.getElementsByTagName("select");
    hiddenIECombosId = new Array(arSelect.length);
    
    var select;
    for (x=0; x<arSelect.length; x++)
    {
        select = arSelect[x];
        hiddenIECombosId[x] = select.id.toString() + "@" + select.style.display;
        alert(hiddenIECombosId[x]);
        select.style.display = "none";   
    }
}

/// reset the display style of the combos ///
function resetVisibilityOfIECombos(windowId)
{
    var select, id, display;
    for (x=0; x<hiddenIECombosId.length; x++)
    {
        var data = hiddenIECombosId[x].split("@");
        alert(data);
        select = document.getElementById(data[0]);
        if (select.display == "none")
        {
            select.style.display = data[1];
        }
    }
}


// showAjaxWorkingMessage()

function addEvent(element, eventName, functionName, useCapture)
// addEvent and removeEvent
// cross-browser event handling for IE5+,  NS6 and Mozilla
// By Scott Andrew
{
  if (element.addEventListener){
    element.addEventListener(eventName, functionName, useCapture);
    return true;
  } else if (element.attachEvent){
    var r = element.attachEvent("on"+eventName, functionName);
    return r;
  } else {
    alert("Handler could not be removed");
  }
} 

/*** Alessandro: 18/09/2008 
*    Select all the checkbox of a ASP.NET CheckBoxlist control
*   optionsListId = ClientID of the CheckBoxlist control
***/
function selectOptions(optionsListId, select) {
  
    var options = document.getElementById(optionsListId).getElementsByTagName("input");
    
    for(x=0; x<options.length; x++) {
        options[x].checked = select; 
    }    
}


/*************************************
 Textarea max length controls 
 use: <TEXTAREA onkeyup="javascript: truncateTextInput(this, maxChars);" id=txtInfo name=txtInfo rows=5 cols=60></TEXTAREA> 
 ************************************/

/// default value, set it in the page for changing
var maxTextLengthReached = "Massimo numero di caratteri raggiunto: {0}";

function truncateTextInput(obj, maxChars) {
	if((obj.value.length) > maxChars) 
	{
	    alert(formatString(maxTextLengthReached, new Array(maxChars.toString())));
        //alert("more than " +maxChars + " chars [" + obj.value.length + "]");
        obj.value = obj.value.substr(0, maxChars );
        return false;
	}
}






