﻿// 09/05/2009 Rao PageMethod if window.loc.href has request parameters we need to remove them when constructing pageurl
// 04/28/2010 Rao Added findComponentTemp. We need to refactor our javascript code

$telerik.$.namespace = function() {
    var a = arguments, o = null, i, j, d;
    for (i = 0; i < a.length; i = i + 1) {
        d = a[i].split(".");
        o = window;
        for (j = 0; j < d.length; j = j + 1) {
            o[d[j]] = o[d[j]] || {};
            o = o[d[j]];
        }
    }
    return o;
};
$telerik.$.namespace('VistaSG.Common');
function PrepareBuffer() {
    var objNew = document.createElement('p');
    var objHidden = document.createElement('input');

    objHidden.setAttribute('type', 'hidden');
    objHidden.setAttribute('value', '1');
    objHidden.setAttribute('id', 'virtualbufferupdate');
    objHidden.setAttribute('name', 'virtualbufferupdate');

    objNew.appendChild(objHidden);
    document.body.appendChild(objNew);
}


function UpdateBuffer() {
    var objHidden = document.getElementById('virtualbufferupdate');

    if (objHidden) {
        if (objHidden.getAttribute('value') == '1')
            objHidden.setAttribute('value', '0');
        else
            objHidden.setAttribute('value', '1');
    }
}

function PageMethod(methodName, paramArray, onSuccess, onFail) {
    //Create list of parameters in the form:   
    //{"paramName1":"paramValue1","paramName2":"paramValue2"}   
    var paramList = '';
    if (paramArray.length > 0) {
        for (var i = 0; i < paramArray.length; i += 2) {
            if (paramList.length > 0) paramList += ',';
            paramList += '"' + paramArray[i] + '":"' + paramArray[i + 1] + '"';
        }
    }
    paramList = '{' + paramList + '}';
    var loc = window.location.href;
    var parameterMarkerPos = loc.indexOf("?");
    if ( parameterMarkerPos > -1) {
        loc = loc.substr(0, parameterMarkerPos); 
    }
    loc = (loc.substr(loc.length - 1, 1) == "/") ? loc + "default.aspx" : loc;
    var pageurl = loc + "/" + methodName;

    //Call the page method
    $telerik.$.ajax({
        type: "POST",
        url: pageurl,
        contentType: "application/json; charset=utf-8",
        data: paramList,
        dataType: "json",
        success: onSuccess,
        fail: onFail
    });
}



function PageValidator() {
    var _numberofErrors = 0 ;
    var _numberofWarnings = 0 ;
    var _errorControlToSetFocusTo = null;
    var _errorMessage = "Please review the following error(s) and correct them.  The focus will be set to first error in the list:";
    var _warningMessage = "Please review the following warning(s).";
    var dispplayError = function(errorMessage ,errorControlToSetFocusTo) {alert(errorMessage); setFocusToControl(errorControlToSetFocusTo) ;} ;  
    var setFocusToControl = function(errorControlToSetFocusTo) { if (typeof errorControlToSetFocusTo == "function") { errorControlToSetFocusTo(); } else { errorControlToSetFocusTo.focus();if ( errorControlToSetFocusTo.select) {errorControlToSetFocusTo.select();}}} ;  
    var IsValidEmail = function(emailAddress){var validAddress = false;if (emailAddress == "" || emailAddress.trim() == "") {validAddress = true;}else {var emailPattern = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/ ;if (emailAddress.toLowerCase().match(emailPattern)) { validAddress = true } else { validAddress = false; }}return (validAddress);} ;     
    this.getNumberofErrors = function(){ return _numberofErrors ; } ;
    this.checkRequiredFiedValue = function (controlId, validationMessage) {var control = $telerik.$("[id$=" + controlId + "]").get(0);var controlValue = control.value;if (controlValue == "" || controlValue.trim() == "") {if (_errorControlToSetFocusTo == null) _errorControlToSetFocusTo = control;_errorMessage = _errorMessage + "\n" + validationMessage;_numberofErrors++;}} ;
    this.checkEmailAddressFieldValue = function (controlId, validationMessage) {var control = $telerik.$("[id$=" + controlId + "]").get(0);var controlValue = control.value;if (!IsValidEmail(controlValue)) {_errorMessage = _errorMessage + "\n" + validationMessage;if (_errorControlToSetFocusTo == null) _errorControlToSetFocusTo = control;_numberofErrors++;}} ;
    this.checkRequiredListValue = function (controlId, validationMessage) {var control = $telerik.$("[id$=" + controlId + "]").get(0);if (control.selectedIndex <= 0 ) {if (_errorControlToSetFocusTo == null) _errorControlToSetFocusTo = control;_errorMessage = _errorMessage + "\n" + validationMessage;_numberofErrors++;}};
    this.showErrors = function() {if (_numberofErrors > 0 &&  _errorControlToSetFocusTo != null) { dispplayError(_errorMessage , _errorControlToSetFocusTo) ;}} ;
    this.showWarnings = function(additionalWarningMessage) {var ignoreWarnings = true ;if (_numberofWarnings > 0 ) {var ignoreWarnings =  confirm(_warningMessage + additionalWarningMessage);if ( ignoreWarnings == false ) { setFocusToControl(_errorControlToSetFocusTo) } ; }return ( ignoreWarnings) ;}  ;
    this.setValidationResponse = function(validationResponse) {if ( validationResponse.Errors != null ){for (i = 0; i < validationResponse.Errors.length; i++) {_errorMessage = _errorMessage + "\n" + validationResponse.Errors[i];_numberofErrors ++ ;}_errorControlToSetFocusTo = $telerik.$("[id$=" + validationResponse.DataFiledToSetFocus + "]").get(0) ;}if ( validationResponse.Warnings != null ){for (i = 0; i < validationResponse.Warnings.length; i++) {_warningMessage = _warningMessage + "\n" + validationResponse.Warnings[i];_numberofWarnings ++ ;}_errorControlToSetFocusTo = $telerik.$("[id$=" + validationResponse.DataFiledToSetFocus + "]").get(0) ;}} ;
    this.addError = function(validationMessage, control) {_errorMessage = _errorMessage + "\n" + validationMessage;_numberofErrors++; if (_errorControlToSetFocusTo == null) _errorControlToSetFocusTo = control;};
    this.addWarning = function(warningMessage, control) {_warningMessage = _warningMessage + "\n" + warningMessage;_numberofWarnings++; if (_errorControlToSetFocusTo == null) _errorControlToSetFocusTo = control;};    
} 

// This Function Returns Last Element Node of an panel like div
function getLastChild(n) { lastChildNode = n.lastChild;  while (lastChildNode.nodeType != 1) {lastChildNode = lastChildNode.previousSibling;}return lastChildNode;}
// This Function Returns Previous  Element Node of a given node ignoring textnode
function getPreviousSibling(n) { previousNode = n.previousSibling; while (previousNode.nodeType != 1) {previousNode = previousNode.previousSibling; }return previousNode;}
function getNextSibling(n) { nextNode = n.nextSibling; while (nextNode.nodeType != 1) { nextNode = nextNode.nextSibling;}return nextNode;}

function findComponentTemp(id) {
        id = id.toLowerCase();
        var matchingComponent = null;
        var allComponents = Sys.Application.getComponents();
        for (var i = 0; i < allComponents.length; i++) {
            var componentId = allComponents[i].get_id().toLowerCase();
            if (componentId.match(id + "$") == id) {
                matchingComponent = allComponents[i];
                break;
            }
        }
        return (matchingComponent);
}

function ShowMasterNotificationPopup() {  if (typeof _masterNotificationPopupControl != 'undefined' && _masterNotificationPopupControl != null)   {  _masterNotificationPopupControl.show(); }}
function HideMasterNotificationPopup() {if (typeof _masterNotificationPopupControl != 'undefined' && _masterNotificationPopupControl != null) { _masterNotificationPopupControl.hide();} }
