/// <changes>
/// 2008-07-04  IC      Added function ProcessError for handled ajax error
/// 2008-07-29  EF      Add url encoding when redirecting to error page
/// 2008-10-22  EF      Modify ProcessError() in order to redirect to notification page if necessary
/// 2008-10-27  SS      Added logic to display/hide box when is processing a request
/// 2008-10-27  SS      Added logic to handle ajaxpro timeout
/// 2008-10-27  SS      Hide all processing box when exception ocurrs
/// 2008-10-30  EF      Add ApplicationPath to image URLs
/// 2009-02-06	SS		Added condition to prevent JS error
/// </changes>

function displayError(ex, title)
{
    var msg = "";
    var stackTrace;
    var prop;
    if(title)
        msg += title + "\n";
    else
        msg += "Error detected!\n";
    if(ex[0]==undefined)
    {
        for(var i in ex)
        {
            if(i!="stack" && i!="stackTrace")
            {
                prop = i.charAt(0).toUpperCase() + i.substring(1);
                msg += prop + ": " + ex[i] + "\n";    
            }
            else
            {
                stackTrace = "\n#" + String.fromCharCode(160) + ex.stack.replace(/\n/g,"\n#" + String.fromCharCode(160));
                stackTrace = stackTrace.substr(0, stackTrace.length - 3);
                msg += "Stack Trace: " + stackTrace + "\n";
            }
        }
    }
    else
        msg += ex + "\n";
    msg += "\nPor favor, contacte a su administrador.";
    msg += "\nPlease contact your administrator.";
    alert(msg);
}

function dumpProps(obj, parent) 
{  
    // Go through all the properties of the passed-in object  
    for (var i in obj)  
    {  
        // if a parent (2nd parameter) was passed in, then use that to  
        // build the message. Message includes i (the object's property name)  
        // then the object's property value on a new line  
        if(i!="each" && i!="iterate" && i!="addRange" && i!="clear") //excludes array private properties
        {
            if (parent) 
                var msg = parent + "." + i + " = " + obj[i];  
            else  
                var msg = i + " = " + obj[i];  
            // Display the message. If the user clicks "OK", then continue. If they  
            // click "CANCEL" then quit this level of recursion  
            if (!confirm(msg)) 
                return;  

            // If this property   is an object, then recursively process the object  
            if (typeof obj[i] == "object") 
                if (parent) 
                    dumpProps(obj[i], parent + "." + i);
                else
                    dumpProps(obj[i], i);
        }
    }  
} 
function quickDumpProps(obj, parent, lv, notNull, onlyType) 
{  
    try
    {
    if(typeof(notNull) == "undefined")
        var notNull = false;
    if(typeof(lv) == "undefined")
        var lv = 0;
    // Go through all the properties of the passed-in object  
    var msg = "";
    for (var i in obj)  
    {  
        if(i!="each" && i!="iterate" && i!="addRange" && i!="clear") //excludes array private properties
        {
                if(typeof(obj[i])!="undefined" && typeof(obj[i])!="unknown" && (!notNull || obj[i]!=null) && (typeof(onlyType)=="undefined" || typeof(obj[i]) == onlyType))
            {
                if (parent) 
                    msg += parent + "." + i + " = " + obj[i] + "\n";  
                else  
                    msg += i + " = " + obj[i] + "\n";  
                if (typeof obj[i] == "object" && lv>0)
                    if (parent) 
                        msg += quickDumpPropsInterno(obj[i], parent + "." + i, lv-1);
                    else
                        msg += quickDumpPropsInterno(obj[i], i, lv-1);
            }
        }
    }  
    alert(msg);
} 
    catch(ex)
    {
        displayError(ex);
    }
} 
function quickDumpPropsInterno(obj, parent, lv)
{  
    // Go through all the properties of the passed-in object  
    var msg = "";
    for (var i in obj)  
    {  
        if(i!="each" && i!="iterate" && i!="addRange" && i!="clear") //excludes array private properties
        {
            if (parent) 
                msg += parent + "." + i + " = " + obj[i] + "\n";  
            else  
                msg += i + " = " + obj[i] + "\n";  
            if (typeof obj[i] == "object" && lv!=undefined && lv>0)
                if (parent) 
                    msg += quickDumpPropsInterno(obj[i], parent + "." + i, lv-1);
                else
                    msg += quickDumpPropsInterno(obj[i], i, lv-1);
        }
    }  
    return msg;
} 

function ProcessError(res)
{
//    alert('message: ' + res.error.Message + ', token: ' + c_TokenNotificationExceptionMessage);
    var tokenLength = c_TokenNotificationExceptionMessage.length;

    // 2008-10-27  SS      Added logic to hide box when is processing a request
    HideAllProcessingBox();
    
    if (res.error.Message.length < tokenLength || res.error.Message.substring(0, tokenLength) != c_TokenNotificationExceptionMessage)
    {
        window.location = "./Error.aspx?value=" + URLEncode(encode64(res.error.Message));
    }
    else
    {
        window.location = "./Notificacion.aspx?value=" + URLEncode(encode64(res.error.Message));
    }
    
}

// 2008-10-27  SS      Added logic to display box when is processing a request
// 2008-10-30  EF      Add ApplicationPath to image URLs
// optional msg, optional idDiv, optional msgError
function ShowProcessingBox(msg,idDiv, msgError)
{


   
    var ni = document.getElementById('divProcessBox');
    //SS In case of params are empty creates a generic div and keeps the ordinal (id) in a hidden field to identified it
    
    var numi = document.getElementById('hidCurrent');
    var num; // = (document.getElementById('hidCurrent').value -1)+ 2;
    var newdiv;
    var divIdName;
    var divHTML = "";
    if ( ni != null && numi != null )
    {
        num = (document.getElementById('hidCurrent').value -1)+ 2;
        numi.value = num;
        newdiv = document.createElement('div');
        divIdName = 'divProcessingBoxDiv_'+num;
        
        divHTML +="\n      <input type=\"hidden\" value=\"" + msgError + "\" id=\"hid"+(idDiv != null && idDiv.length > 0 ? idDiv : divIdName)+ "\" />";
        divHTML +="\n       <span style=\"color: #36528F; font-size: 10px; font-weight: bold; text-align: justify; \">";
        divHTML +="\n      <img style=\"float: left; padding-top: 5px;\" src=\"" + c_ApplicationPath + "App_Images/Generic/loading_wheel.gif\" alt=\"Procesando solicitud.\" />";
        divHTML +="\n       " + (msg != null && msg.length > 0 ? msg : "Espere mientras se procesa su solicitud.") + "</span>";
       
        //SS If param idDiv is null then use the generic div id divProcessingBoxDiv_ plus an ordinal number
        newdiv.setAttribute('id', (idDiv != null && idDiv.length > 0 ? idDiv : divIdName));
        newdiv.innerHTML = divHTML;
        //SS Use of DOM to set styles attributes of the splash sreen
        newdiv.style.display = "block"; 
        newdiv.style.backgroundColor = "White";
        newdiv.style.border = "1px solid #CCCCCC";
        newdiv.style.position = "fixed";
        newdiv.style.left ="42%";
        newdiv.style.top = "47%";
        newdiv.style.width = "200px";
        newdiv.style.height ="50px";
//        newdiv.style.overflow = "auto";
        newdiv.style.zindex = "10000";
        ni.appendChild(newdiv);
    }    
}
// 2008-10-27  SS      Added logic to hide box when is processing a request
// Pass number of div to hide or id 
function HideProcessingBox(divNum, idDiv )
{
    //SS Use of DOM to remove the splash screen
    var olddiv = (divNum != null ? document.getElementById('divProcessingBoxDiv_' + divNum) : document.getElementById(idDiv));
    if (olddiv != null) 
    {
        var d = document.getElementById('divProcessBox');
        document.getElementById('hidCurrent').value = (document.getElementById('hidCurrent').value -1);
        d.removeChild(olddiv);
    }
}
// 2008-10-27  SS      Added logic to handle ajaxpro timeout
function HandleTimeout()
{
    var divMain = document.getElementById('divProcessBox');
    var divChildren = divMain.getElementsByTagName("div"); 
    var msgError = "";
   
    for (var i=0; i < divChildren.length; i++)
    {
        var hidErrorMsgs = divChildren[i].getElementsByTagName("input");
        if (hidErrorMsgs != null)
        {
            msgError += hidErrorMsgs[0].value + "\n"; 
        }
    }
   
    HideAllProcessingBox(); 
    alert(msgError);
//    window.location = "./Error.aspx?value=" + URLEncode(encode64(msgError));
}
// 2008-10-27  SS      Hide all processing box when a exception ocurrs 
function HideAllProcessingBox()
{
   //SS Use DOM to remove all elements  
   var div = document.getElementById('divProcessBox') ;
   // 2009-02-06	SS		Added condition to prevent JS error
   if (div != null)
   {
	   while (div.firstChild)
	   {
			div.removeChild(div.firstChild)
	   }
   }
}

