﻿/*-----------------------------------------------------------------------
 *   earth.EzyEdit - User friendly targeted text edit.
 -----------------------------------------------------------------------*/
var earth;
if(earth == null) earth = {};
earth.EzyEdit = function(){};
/**********************************************************************  
*   Naming provide access to each of the element full id.
*   Parameters:
*   (string) id - an unique idenitifer to identify HTML element id.
***********************************************************************/
earth.EzyEdit.Naming = function(id){
    this.layerId = id + '_ezyEdit_editLayer';
    this.valueId = id + '_ezyEdit_value';
    this.modifyInputId = id + '_ezyEdit_modifyInput';
    this.cancelId = id + '_ezyEdit_cancel';
    this.okId = id + '_ezyEdit_ok';  
    this.errorId = id + '_ezyEdit_error';   
};
/**********************************************************************  
*   GetValue return changed text.
*   Parameters:
*   (string) id - an unique idenitifer to identify HTML element id.
***********************************************************************/
earth.EzyEdit.prototype.GetValue = function(id){
    var naming = new earth.EzyEdit.Naming(id);
    return document.getElementById(naming.modifyInputId).value;
};
/**********************************************************************  
*   ShowChanges display text changes after the OK funcion is executed.
*   Parameters:
*   (string) id - an unique idenitifer to identify HTML element id.
***********************************************************************/
earth.EzyEdit.prototype.ShowChanges = function(id)
{
    var naming = new earth.EzyEdit.Naming(id);
    var modtxt = document.getElementById(naming.modifyInputId).value;
    if(modtxt == "")
        document.getElementById(naming.valueId).innerHTML = '&nbsp;';
    else
        document.getElementById(naming.valueId).innerHTML = IncludeLineBreak(modtxt);
        
    document.getElementById(naming.layerId).style.display = 'none';
    document.getElementById(naming.valueId).style.display = '';
    
    function IncludeLineBreak(input)
    {
        var s = "";
        var arr = input.split(/\r\n|\r|\n/);
        for(i=0; i<arr.length; i++)
            s += arr[i] + "<br/>";
        return s; 
    }
};
/**********************************************************************  
*   ShowError display error message when error occurs.
*   Parameters:
*   (string) id - an unique idenitifer to identify HTML element id.
*   (string) err - custom js function after OK link button is clicked.
***********************************************************************/
earth.EzyEdit.prototype.ShowError = function (id, err)
{
    var naming = new earth.EzyEdit.Naming(id);
    document.getElementById(naming.errorId).innerHTML = err;
    document.getElementById(naming.errorId).style.display = '';
};
/**********************************************************************  
*   ShowEzyEdit display EzyEdit edit input.
*   Parameters:
*   (string) id - an unique idenitifer to identify HTML element id.
*   (function) okfunc - custom js function after OK link button is clicked.
*   (function) cancelfunc - custom js function after CANCEL link buttin is clicked.
***********************************************************************/
earth.EzyEdit.prototype.ShowEzyEdit = function(id, okfunc, cancelfunc){
    var naming = new earth.EzyEdit.Naming(id);
    Display();
    SetButtonEvent();
    function SetButtonEvent(){
        document.getElementById(naming.okId).onclick = function(){ 
            if(okfunc != null)
                okfunc();
        };
        document.getElementById(naming.cancelId).onclick = function(){ 
            if(cancelfunc != null)
                cancelfunc();
            Hide();
        };  
    }
    function Display(){
        document.getElementById(naming.layerId).style.display = '';
        document.getElementById(naming.valueId).style.display = 'none';
        document.getElementById(naming.modifyInputId).focus();
        document.getElementById(naming.modifyInputId).select();
    }
    function Hide(){
        document.getElementById(naming.layerId).style.display = 'none';
        document.getElementById(naming.valueId).style.display = '';
    };
};
var ezyEdit = new earth.EzyEdit(); 
