/*
 * <pre>
 *  Accela Citizen Access
 *  File: CapDetail.aspx.cs
 * 
 *  Accela, Inc.
 *  Copyright (C): 2008-2009
 * 
 *  Description:
 * 
 *  Notes:
 *      $Id: Global.js 77905 2007-10-15 12:49:28Z ACHIEVO\lytton.cheng $.
 *  Revision History
 *  &lt;Date&gt;,		&lt;Who&gt;,			&lt;What&gt;
 * </pre>
*/
function showError(err){
    var msg="";
    if(err!="")
    {
        msg=msg+'<div class="ACA_Content">';
        msg=msg+'<div class="ACA_Message_Error">';
        //msg=msg+'<div title="An error has occurred." alt="An error has occurred." class="ACA_Error_Icon"/></div>';
        //msg=msg+'An error has occurred.<br/>'+err+'</div></div>';
        //msg=msg+'<div title="'+getText.global_js_showError_title+'" alt="'+getText.global_js_showError_alt+'" class="ACA_Error_Icon"/></div>';
        //msg=msg+getText.global_js_showError_title+'<br/>'+err+'</div></div>';
        msg=msg+'<table><tr><td valign="top"><div title="'+getText.global_js_showError_title+'" alt="'+getText.global_js_showError_alt+'" class="ACA_Error_Icon"/></div></td><td>';
        msg=msg+getText.global_js_showError_title+'<br/>'+err+'</td></tr></table></div></div>';
    }
    showMessage(msg);
}

function showConfirm(confirm){
    var msg="";
    if(confirm!="")
    {
        msg=msg+'<div class="ACA_Content">';
        msg=msg+'<div class="ACA_Message_Success">';
        //msg=msg+'<div title="Confirm message" alt="Confirm message" class="ACA_Success_Icon"/></div>';
        msg=msg+'<div title="'+getText.global_js_showConfirm_title+'" alt="'+getText.global_js_showConfirm_title+'" class="ACA_Success_Icon"/></div>';
        msg=msg+confirm+'</div></div>';
    }
    showMessage(msg);
}

function showNotice(notices){
    var msg="";
    if(notices!="")
    {
        msg=msg+'<div class="ACA_Content">';
        msg=msg+'<div class="ACA_Message_Notice">';
        //msg=msg+'<div title="Notice" alt="Notice" class="ACA_Notice_Icon"/></div>';
        //msg=msg+'Notice:<br/>'+notices+'</div></div>';
        msg=msg+'<div title="'+getText.global_js_showNotice_title+'" alt="'+getText.global_js_showNotice_title+'" class="ACA_Notice_Icon"/></div>';
        msg=msg+getText.global_js_showNotice_title+':<br/>'+notices+'</div></div>';
    }    
    showMessage(msg);
}

function showMessage(msg)
{
   
   var msgObj = document.getElementById("messageSpan");
   if(msg==""){
        msgObj.style.display='none';
   }else{
        msgObj.style.display='';
        msgObj.innerHTML=msg;
        goToMessageBlock();
   }
}
function hideMessage()
{
    var msgObj = document.getElementById("messageSpan");
    
    if (msgObj != null)
    {
        msgObj.style.display = 'none';
    }
}

function show(obj)
{
    var div = document.getElementById(obj);
    
    if (div != null)
    {
        div.style.display = "block";
    }
} 

function hide(obj)
{
    var div = document.getElementById(obj);
    
    if (div != null)
    {
        div.style.display = "none";
    }
} 

function goToMessageBlock()
{
    var a = $get('goTOMessage');
    if(a)
   {
        a.scrollIntoView();
   }  
}

// true: browser's type is fireFox
// false: browser's type isn't fireFox
function isFireFox()
{
    if(navigator.userAgent.indexOf("Firefox")>0)
    {
        return true;
    } 
    else
    {
        return false; 
    }  
}

//parse currency from formatted value for JavaScript calculation, such as from 123,45.67 to 12345.67 in german culture
function I18nParseCurrencyForJS(formattedCurrency){
    //if GLOBAL_SERVICE_PROVIDER_CULTURE etc. is not defined, return original value
    if(formattedCurrency==null
        || formattedCurrency.replace(/ /g,'')==''
        || typeof(GLOBAL_SERVICE_PROVIDER_CULTURE)=="undefined"
        || typeof(GLOBAL_CURRENCY_SYMBOL)=="undefined"
        || typeof(GLOBAL_CURRENCY_GROUP_SEPARATOR)=="undefined"
        || typeof(GLOBAL_CURRENCY_DECIMAL_SEPARATOR)=="undefined"
       )
    {
        return formattedCurrency;
    }
    var result = formattedCurrency.replace(new RegExp("(\\" + GLOBAL_CURRENCY_SYMBOL + ")", "g"), "");
    result = result.replace(new RegExp("(\\" + GLOBAL_CURRENCY_GROUP_SEPARATOR + ")", "g"), "") + '';
    result = result.replace(new RegExp("(\\" + GLOBAL_CURRENCY_DECIMAL_SEPARATOR + ")", "g"), ".") + '';
    return parseFloat(result);
}

//parse currency from formatted value by culture, such as from 123,45.67 to 12345,67 in german culture
function I18nParseCurrencyForCulture(formattedCurrency){
    var result = I18nParseCurrencyForJS(formattedCurrency); // refers to I18nParseCurrencyForJS
    return result.toString().replace(new RegExp("(\\.)", "g"), GLOBAL_CURRENCY_DECIMAL_SEPARATOR);
}

//format number to I18n style with group symbol, such as from 12345.67 to $12,345.67 in English culture
function I18nFormatCurrencyWithGroupSymbol(num)
{
    //if GLOBAL_SERVICE_PROVIDER_CULTURE etc. is not defined, return original value
    if(num==null
        || num.replace(/ /g,'')==''
        || typeof(GLOBAL_SERVICE_PROVIDER_CULTURE)=="undefined"
        || typeof(GLOBAL_CURRENCY_SYMBOL)=="undefined"
        || typeof(GLOBAL_CURRENCY_GROUP_SEPARATOR)=="undefined"
        || typeof(GLOBAL_CURRENCY_DECIMAL_SEPARATOR)=="undefined"
       )
    {
        return num;
    }
    if(num<0) num=0-num;
    
    num = I18nParseCurrencyForJS(num); // refers to I18nParseCurrency
    
    num = Math.floor(num*100+0.50000000001);
    var cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
    cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+GLOBAL_CURRENCY_GROUP_SEPARATOR+
    num.substring(num.length-(4*i+3));

    return  GLOBAL_CURRENCY_SYMBOL + num + GLOBAL_CURRENCY_DECIMAL_SEPARATOR + cents;
}

//format number to I18n style with group symbol, such as from 12345.67 to $12345.67 in English culture
function I18nFormatCurrencyWithoutGroupSymbol(num)
{
    var result = I18nFormatCurrencyWithGroupSymbol(num); // refers to I18nParseCurrency
    return result.replace(new RegExp("(\\" + GLOBAL_CURRENCY_GROUP_SEPARATOR + ")", "g"), "") + '';
}

//Show common message like Notice display style but it has not icon and "Notice" tile. 
function showCommonMessage(message)
{
     var msg="";
     if(message!="")
     {
                msg=msg+'<div class="ACA_Content"><div class="ACA_Message_Notice">'+message+'</div></div>';
      }
      showMessage(msg); 
 }

// adjust the IFrame height according to the page height.
function AdjustHeight(newHeight)
{
    if(typeof(newHeight)=="undefined" || parseFloat(newHeight)<=0)
    {
        return false;
    }
    if(window.parent!=window)
    {
        var ifrm = parent.document.getElementById("ACAFrame");
        var h=newHeight;
        if(ifrm != null && ifrm.contentWindow==window)
        {
            if(isFireFox() == false)
            {
                ifrm.style.height = ifrm.parentNode.style.height = h +"px";
            }
            else
            {
                ifrm.height = h;
            }
            return true;
        }
    }
    return false;
}

///<Summary>
///this js class is used in SPEAR form and Confirmation page to validate multiple-supported comonent
///such as multiple contacts, multiple address
///</Summary>
function ValidationHelper()
{
    var SectionIDs = new Array();
    var CurrentValidationSectionID;
    
    this.AddSectionID = function(sectionId)
    {
        SectionIDs.push(sectionId);
    }
  
    this.SetCurrentValidationSectionID = function(sectionId)
    {
        CurrentValidationSectionID = sectionId;    
    } 
  
    this.IsNeedValidate = function(controlId)  
    {
        if(SectionIDs.length == 0)
        {
            return true;
        } 
        
        if(CurrentValidationSectionID == null || CurrentValidationSectionID == '')//don't need to validate the field in editor form
        {
            if(controlId.indexOf('4ValidateGridView') > -1)
            {
                return true; 
            } 
            
            for(var i=0; i < SectionIDs.length; i++)
            { 
                if(controlId.indexOf(SectionIDs[i]) > -1) 
                {
                    return false;
                }
            }
           
            return true;   
        }
        else// validate the fields of section which indicated by "CurrentValidationSectionID"
        {
            if(controlId.indexOf('4ValidateGridView') > -1)
            {
                return false; 
            }
            
            return controlId.indexOf(CurrentValidationSectionID) > -1;
        } 
    } 
}

var ValidationHelperInstance = null;

//Add the section ID which need to support multiple
function AddValidationSectionID(sectionID)
{
    if(!ValidationHelperInstance)
    {
        ValidationHelperInstance = new  ValidationHelper();
    } 
    ValidationHelperInstance.AddSectionID(sectionID); 
}

//Set current validation section ID to indicate current only validate that section
function SetCurrentValidationSectionID(sectionID)
{
    if(!ValidationHelperInstance)
    {
        ValidationHelperInstance = new  ValidationHelper();
    } 
    ValidationHelperInstance.SetCurrentValidationSectionID(sectionID); 
}

//check if the control need to validate
function IsNeedValidation(controlId)
{
    if(ValidationHelperInstance)
    {
        return ValidationHelperInstance.IsNeedValidate(controlId); 
    } 
   
   return  true;
}

//disable/enable a button
function DisableButton(buttonId, disabled) 
{
    if(buttonId != '')
    {
        var button = $get(buttonId);
        if(button)
        {
            button.disabled = disabled;
            if(!document.all){
                if(button.disabled){
                    button.style.color="Gray";
                }else{
                    button.style.color="";
                }
            }
        }
    }
}

function getElementTop(obj)
{
        var top = obj.offsetTop;
        
        var parentobj = obj.offsetParent;
        while (parentobj)
        {
            if (parentobj.offsetTop == undefined) break;
            top += parentobj.offsetTop;
            parentobj = parentobj.offsetParent;
        }
        
        return top;
}
