﻿if (!Array.prototype.indexOf){
  Array.prototype.indexOf = function(elt /*, from*/){
    var len = this.length;
    var from = Number(arguments[1]) || 0;
    from = (from < 0)? Math.ceil(from):Math.floor(from);
    if (from < 0)from += len;
    for (; from < len; from++)
      if (from in this && this[from] === elt)return from;
    return -1;
  };
}

CommAjaxOptimizationUtilities = {
    MAX_URL_LENGTH : 2048,
    doGet : function(getFunctionName, getParameters){
        if(CommAjaxOptimizationUtilities._getRequestLength(getFunctionName, getParameters) < CommAjaxOptimizationUtilities.MAX_URL_LENGTH)
            AjaxGetHandler.Get(getFunctionName, getParameters);
        else
            CommAjaxOptimizationUtilities._doSplitGet(getFunctionName, getParameters);
        
    },
    _getRequestLength : function(getFunctionName, getParameters){
        var urlLength = 0;
        urlLength += ("http://" + location.hostname + "/").length;
        urlLength += AjaxGetHandler.get_path().length;
        urlLength += "Get?functionName=&parameters=".length;
        urlLength += ("" + getFunctionName).length;
        urlLength += escape("[" + getParameters.toString() + "]").length;
        return urlLength;
    },
    _doSplitGet : function(getFunctionName, getParameters){
        var tempParams = [];
        for(var i=0;i<getParameters.length;i++){
            tempParams.push(getParameters[i]);
            if(CommAjaxOptimizationUtilities._getRequestLength(getFunctionName, tempParams) >= CommAjaxOptimizationUtilities.MAX_URL_LENGTH){
                tempParams.pop();
                AjaxGetHandler.Get(getFunctionName, tempParams);
                tempParams = [];
                i--;
            }                
        }
        if(tempParams.length > 0)
            AjaxGetHandler.Get(getFunctionName, tempParams);   
    },
    findDivsByClassName : function(className){
        var divs = document.getElementsByTagName("div");
        var retDivs = [];
        for(var i=0;i<divs.length;i++){
            if(divs[i].className && divs[i].className == className)
                retDivs.push(divs[i]);
        }
        return retDivs;
    }    
}
