/**
 * Shows a dialog box
 * @param title
 * @param content
 * @param style
 * @return
 */
function showMessageDialogBox(title, content, style){
	var id = "messageDialogBox";
    if (dijit.byId(id)) {
		dijit.byId(id).destroy();
    }
    var dlg = new dijit.Dialog({
        id: id,
        title: title,
        style: style
    });
    // set the content of the dialog:
    var okButtonHandler = "dijit.byId('" + id + "').hide()";
    var htmlContent = '<table>' +
    '<tr>' +
    '<td width="100%">' +
    content +
    '</td>' +
    '</tr>' +
    '<tr>' +
    '<td width="100%" align="center">' +
    '<button dojoType="dijit.form.Button" onClick="' +
    okButtonHandler +
    '">OK</button>' +
    '</td>' +
    '</tr>' +
    '</table>';
    dlg.attr("content", htmlContent);
    dlg.show();
}
/**
 * Hides the message dialog box
 * @return
 */
function hideMessageDialogBox(){
    dijit.byId('messageDialogBox').hide();
}

function showProgressBarDialog(title, style){
	
	var id = "progressBarDialogBox";
    if (dijit.byId(id)) {
		dijit.byId(id).destroy();
    }
    var dlg = new dijit.Dialog({
        id: id,
        title: title,
        style: style
    });
    // set the content of the dialog:
    var okButtonHandler = "dijit.byId('" + id + "').hide()";
    var htmlContent = '<table width="200px" align="center">' +
    '<tr>' +
    '<td width="100%" align="center">' +
    '<img src="' + CONTEXT_PATH + '/images/progress.gif" />'
    '</td>' +
    '</tr>' +
    '<tr>' +
    '<td width="100%" align="center">' +
    '<button dojoType="dijit.form.Button" onClick="' +
    okButtonHandler +
    '">OK</button>' +
    '</td>' +
    '</tr>' +
    '</table>';
    dlg.attr("content", htmlContent);
    dlg.show();
}

function hideProgressBarDialog(){
    dijit.byId('progressBarDialogBox').hide();
}

function messageDialogBox(id, title, content, style){
    if (dijit.byId(id)) {
        return;
    }
    var dlg = new dijit.Dialog({
        id: id,
        title: title,
        style: style
    });
    // set the content of the dialog:
    var okButtonHandler = "dijit.byId('" + id + "').hide()";
    var htmlContent = '<table>' +
    '<tr>' +
    '<td width="100%">' +
    content +
    '</td>' +
    '</tr>' +
    '<tr>' +
    '<td width="100%" align="center">' +
    '<button dojoType="dijit.form.Button" onClick="' +
    okButtonHandler +
    '">OK</button>' +
    '</td>' +
    '</tr>' +
    '</table>';
    dlg.attr("content", htmlContent);
}

function showDialogBox(id){
    dijit.byId(id).show();
}

function hideDialog(id){
    dijit.byId(id).hide();
}

function destroyDialog(id){
    dijit.byId(id).destroy();
}

function pageWidth(){
    return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
}

function pageHeight(){
    return window.innerHeight != null ? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null ? document.body.clientHeight : null;
}

function posLeft(){
    return typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
}

function posTop(){
    return typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
}

function posRight(){
    return posLeft() + pageWidth();
}

function posBottom(){
    return posTop() + pageHeight();
}

function trim(str, chars){
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars){
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars){
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

 
   function doGetCaretPosition (oField) {

     // Initialize
     var iCaretPos = 0;

     // IE Support
     if (document.selection) { 

       // Set focus on the element
       oField.focus ();
  
       // To get cursor position, get empty selection range
       var oSel = document.selection.createRange ();
  
       // Move selection start to 0 position
       oSel.moveStart ('character', -oField.value.length);
  
       // The caret position is selection length
       iCaretPos = oSel.text.length;
     }

  // Firefox support
     else if (oField.selectionStart || oField.selectionStart == '0')
       iCaretPos = oField.selectionStart;

     // Return results
     return (iCaretPos);
   }


   
   function doSetCaretPosition (oField, iCaretPos) {

	   // IE Support
     if (document.selection) { 

    	// Set focus on the element
       oField.focus ();
  
       // Create empty selection range
       var oSel = document.selection.createRange ();
  
       // Move selection start and end to 0 position
       oSel.moveStart ('character', -oField.value.length);
  
       // Move selection start and end to desired position
       oSel.moveStart ('character', iCaretPos);
       oSel.moveEnd ('character', 0);
       oSel.select ();
     }

  // Firefox support
     else if (oField.selectionStart || oField.selectionStart == '0') {
       oField.selectionStart = iCaretPos;
       oField.selectionEnd = iCaretPos;
       oField.focus ();
     }
   }

