/*----------------------------*/
// POPUP FORM
function showPopup(message, width) {
  var divPopup = $('<div id="popupProgress" style="display:none; cursor:default;" />');
    var container = $('<div class="container" style="width:' + width + 'px;" />');
    var divBody = $('<div class="body" style="width:' + width + 'px;" />');
    var imageLoader = $('<img src="/images/psa/ajax-loader.gif" style="padding-top:10px;" align="top" />');
    var message = $('<h2 class="dialogMessage">' + message + '</h2>');

    divBody.append(imageLoader);
    divBody.append(message);
    container.append(divBody);
    divPopup.append(container);
    $('body').append(divPopup);
}

function removePopup() {
    $('#popupProgress').remove();    
}
/*----------------------------*/

/*----------------------------*/
// AjaxEndRequest
function AjaxEndRequest(sender, args) {
  this.sender = sender;
  this.controlName= '';
  this.prefix = '';
  this.controlId = '';
  
  this.initialize = function() {
    this.controlId = this.sender._postBackSettings.sourceElement.id; //get the id of the element that triggers the event.
    this.controlName = this.controlId.substring(this.controlId.lastIndexOf("_") + 1, this.controlId.length); //get the server id of the element.
    this.prefix = this.controlId.substring(0, this.controlId.lastIndexOf("_") + 1);
  }
}
/*----------------------------*/

/*----------------------------*/
// OPEN WINDOW
function openWindow(src, width, height, withScrollbar) {
    var top = ((screen.height - height) / 2) - 50;
    var left = ((screen.width - width) / 2);

    if (width == null) {
        width = screen.width - 10;
        left = 0;
    }

    if (height == null) {
        height = screen.height - 95;
        top = 0;
    }

    var features = "scrollbars=" + (withScrollbar == "true" ? "yes" : "no") + ",toolbar=no,status=no,menubar=no,resizable=no,directories=no,location=no," +
                           "top=" + top + "px,left=" + left + "px,height=" + height + "px,width=" + width + "px";
    windowObject = window.open(src, "Window", features);
    windowObject.focus();
}

/*----------------------------*/


/*----------------------------*/
// CONFIRMATIONS
function saveRecord(validationGroup) {
  if (validationGroup == "") 
    return confirm('Are you sure you want to save record?');
  else {
    if (validateEntry(validationGroup)) {
      return confirm('Are you sure you want to save record?');
    }
    else {
      return false;
    }
  }
}

function updateRecord(validationGroup) {
  if (validationGroup == "")
    return confirm('Are you sure you want to update record?');
  else {
    if (validateEntry(validationGroup)) {
      return confirm('Are you sure you want to update record?');
    }
    else {
      return false;
    }
  }
}

function deleteRecord() {
  return confirm('Are you sure you want to delete record?');
}
/*----------------------------*/

/*----------------------------*/
/* VALIDATION */
function validateEntry(validationGroup) {
    var isValidEntry = true;
    if (typeof (Page_Validators) != 'undefined') {
        for (var i = 0; i < Page_Validators.length; i++) {
            if (Page_Validators[i].validationGroup == validationGroup) {
                // call validator function
                var func = Page_Validators[i].evaluationfunction;
                Page_Validators[i].isvalid = func(Page_Validators[i]);
                if (!Page_Validators[i].isvalid) {
                    isValidEntry = false;
                    Page_Validators[i].style.visibility = '';
                    Page_Validators[i].style.display = '';
                }
            }
        }
    }
    return isValidEntry;
}
/*----------------------------*/

