function getSelectValue(id){
    var value;
    $("#" + id + " option:selected").each(function () {
        value = $(this).val();
    });
    return value;
}

function setSelectValue(id, value){
    var isSet = false;
    $("#" + id + " option").each(function () {
        if(value == $(this).text()){
            $(this).attr("selected", "selected");
        }else{
            $(this).removeAttr("selected");
            isSet = true;
        }
    });

    if(isSet == false){
        resetSelectValue(id);
    }
}


function resetSelectValue(id){
    $("#" + id + " option").each(function () {
        if($(this).val() == "" || $(this).val() == "0" ){
            $(this).attr("selected", "selected");
        }else{
            $(this).removeAttr("selected");
        }
    });
}



var Utility = {};

Utility.GetRandomNumber = function(upperLimit) {
    highNumber = 1000000000;
    return parseInt(Math.random(Math.log(upperLimit)) * highNumber % (upperLimit)) ;
};

Utility.GetRandomTrueFalse = function() {
    if(Utility.GetRandomNumber(2) == 0)
    {
        return true;
    }
    return false;
};


Utility.LeftPadZero = function(inputValue, size) {
    var strValue = inputValue.toString();

    while(strValue.length < size)
    {
        strValue = "0" + strValue;
    }

    return strValue;
};








Array.prototype.unique = function( ){
    temp = new Array();
    for(i=0;i<this.length;i++){
        if(!temp.inArray(this[i])){
            temp.length+=1;
            temp[temp.length-1]=this[i];
        }
    }
    return temp;
}

//Array.prototype.flatten = function(returnArray) {
//    alert("input \n" + "\n" + returnArray);
//    for(i=0;i<this.length;i++){
//        if(this[i] instanceof Array){
//            alert("array found\n" + i + "\n" + this[i]);
//            returnArray = this[i].flatten(returnArray);
//            alert("returnArray\n" + i + "\n" + returnArray);
//        }else{
//            returnArray[returnArray.length]=this[i];
//            alert("returnArray\n" + i + "\n" + returnArray);
//        }
//    }
//    alert("returning \n" + "\n" + returnArray);
//    return returnArray;
//}

Array.prototype.flatten = function(returnArray) {
    for(i=0;i<this.length;i++){
        if(this[i] instanceof Array){
            returnArray = this[i].flatten(returnArray);
        }else{
            returnArray[returnArray.length]=this[i];
        }
    }
    return returnArray;
}

Array.prototype.jumbleArray = function(countToJumble) {
    if(this.length < 2)
    {
        return this;
    }

    if(countToJumble < 1 )
    {
        return this;
    }

    if(countToJumble > this.length - 1 )
    {
        countToJumble = this.length - 1;
    }


    var random1;
    var random2;
    while (countToJumble > 0)
    {
        random1 = Utility.GetRandomNumber(this.length);
        random2 = Utility.GetRandomNumber(this.length);

        if(random1 != random2)
        {
            var tempElement = this[random1];
            this[random1] = this[random2];
            this[random2] = tempElement;
            countToJumble--;
        }

    }
    return this;
}


Array.prototype.joinArray = function(obj) {
    if (obj.length == 0) return this;

    for (var i = 0; i < obj.length; i++) {
        this[this.length] = obj[i];
    }
    return this;
};

Array.prototype.copy = function(obj) {
    this.length = 0;
    if (obj.length == 0) return this;

    for (var i = 0; i < obj.length; i++) {
        this[this.length] = obj[i];
    }
    return this;
};

Array.prototype.reverseElementOrder = function(){

    if (this.length < 2) return this;

    var tempArray = new Array();
    for (var i = 0; i < this.length; i++) {
        tempArray[i] = this[this.length - i - 1];
    }

    return tempArray;
};

Array.prototype.indexOf = function(obj){

    if (this.length < 1) return -1;

    if (obj == null) return -1;

    for (var i = 0; i < this.length; i++) {
        if (this[i].compare)
        {
            if (this[i].compare(obj))
            {
                return i;
            }
        }else
        {
            if (this[i] == obj)
            {
                return i;
            }
        }
    }

    return -1;
};


Array.prototype.inArray = function(obj){
    if(this.indexOf(obj) == -1){
        return false;
    }
    return true;
}



Array.prototype.minusArray = function(arr){
    var tempArray = new Array();
    for (var i = 0; i < this.length; i++) {
        if(arr.inArray(this[i]) == false){
            tempArray[tempArray.length] = this[i];
        }
    }
    return tempArray;
}








Array.prototype.getArrayWithMatchingSubstring = function(substr){

    if (substr == null || substr == 'undefined') return this;

    var tempArray = new Array();

    for (var i = 0; i < this.length; i++) {
        if (this[i].indexOf)
        {
            if (this[i].indexOf(substr) > -1)
            {
                tempArray[tempArray.length] = this[i];
            }
        }
    }

    return tempArray;
};

Array.prototype.compare = function(testArr) {
    if (this.length != testArr.length) return false;
    for (var i = 0; i < testArr.length; i++) {
        if (this[i].compare) {
            if (!this[i].compare(testArr[i])) return false;
        }

        if (this[i] !== testArr[i] && this[i] != testArr[i] )
        {
            return false;
        }
    }
    return true;
};




Array.prototype.max = function(){
    return Math.max.apply( Math, this );
};

Array.prototype.min = function(){
    return Math.min.apply( Math, this );
};

Array.prototype.maxLengthOfElements = function(){
    maxLength = 0;
    for (var i = 0; i < this.length; i++) {
        maxLength = Math.max(maxLength, this[i].length);
    }
    return maxLength;
};


Array.prototype.copy = function(){
    var newArray = new Array();
    for (var i = 0; i < this.length; i++) {
        newArray[i] = this[i];
    }
    return newArray;
};

Array.prototype.trimElements = function(){
    var newArray = new Array();
    for (var i = 0; i < this.length; i++) {
        newArray[i] = this[i].trim();
    }
    return newArray;
};


String.prototype.capitalize = function() {
    return this.charAt(0).toUpperCase() + this.slice(1);
}




String.prototype.capitalizeEachWord = function () {
    val = this;
    newVal = '';
    val = val.split(' ');
    for(var c=0; c < val.length; c++) {
        newVal += val[c].substring(0,1).toUpperCase() +
            val[c].substring(1,val[c].length) + ' ';
    }
    return newVal;
}


String.prototype.splitCSV = function(sep) {
  for (var foo = this.split(sep = sep || ","), x = foo.length - 1, tl; x >= 0; x--) {
    if (foo[x].replace(/"\s+$/, '"').charAt(foo[x].length - 1) == '"') {
      if ((tl = foo[x].replace(/^\s+"/, '"')).length > 1 && tl.charAt(0) == '"') {
        foo[x] = foo[x].replace(/^\s*"|"\s*$/g, '').replace(/""/g, '"');
      } else if (x) {
        foo.splice(x - 1, 2, [foo[x - 1], foo[x]].join(sep));
      } else foo = foo.shift().split(sep).concat(foo);
    } else foo[x].replace(/""/g, '"');
  }return foo;
};










String.prototype.replaceAll = function(strTarget, strSubString ){
    var strText = this;
    var intIndexOfMatch = strText.indexOf( strTarget );

    while (intIndexOfMatch != -1){
        strText = strText.replace( strTarget, strSubString )
        intIndexOfMatch = strText.indexOf( strTarget );
    }
    return  strText;
};

String.prototype.replaceArrayElementsWithArrayIndex = function(strArray){
    var strText = this;

    for(var i =0; i < strArray.length; i++)
    {
        if( strText.indexOf(strArray[i]) > -1)
        {
            strText = strText.replaceAll(strArray[i], "strArray[" + i + "]" );
        }
    }
    return  strText;
};

String.prototype.restoreArrayElementsWithArrayIndex = function(strArray){
    var strText = this;

    for(var i =0; i < strArray.length; i++)
    {
        if( strText.indexOf("strArray[" + i + "]") > -1)
        {
            strText = strText.replaceAll("strArray[" + i + "]" , strArray[i]);
        }
    }
    return  strText;
};



String.prototype.trim = function() {
    var temp = this;
    var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
    if (obj.test(temp)) {temp = temp.replace(obj, '$2');}
    var obj = /  /g;
    while (temp.match(obj)) {temp = temp.replace(obj, " ");}

    temp = temp.replace(/^\s+/, '');
    temp = temp.replace(/\s+$/, '');
    return temp;
}

function getJsonCount(data){
    var i = 0;
    $.each(data, function(key, val){
        i++;
    });
    return i;
}

function getJsonValue(data, key){
    var value = null;
    var i = 0;
    $.each(data, function(k, v){
        if( k == key){
            value = v;
        }
        i++;
    });
    return value;

}


function isFlashEnabled(){
    try {
        var fo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
        if(fo) return true;
    }catch(e){
        if(navigator.mimeTypes ["application/x-shockwave-flash"] != undefined) return true;
        return false;
    }
    return false;
}


function _updateCheckedValue(id){
    var checked = $("#_CHK_" + id).attr("checked");
    if(checked == true){
        $("#" + id).val("1");
    }else{
        $("#" + id).val("-1");
    }
}


function _AUTO_COMPLETE_SINGLE(id, _ac_data){
    $( "#" + id ).autocomplete({
        minLength: 0,
        source: function( request, response ) {
            // delegate back to autocomplete, but extract the last term
            response( $.ui.autocomplete.filter(
            _ac_data, request.term  ) );
        },
        focus: function() {
            // prevent value inserted on focus
            return false;
        },
        select: function( event, ui ) {
            this.value = ui.item.value;
            return false;
        }
    });
}

function _GET_AUTO_COMPLETE_DATA(_url){
    var items = [];
    if(_url == "" || _url == undefined){
        return items;
    }

    $.ajax({
        url: _url,
        dataType: 'json',
        type: 'POST',
        async: false,
        success:function(data){
            $.each(data, function(i, val){
                items.push(val);
            });
        }
    });
    return items;
}


function getRandomInt (min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}




function _AUTO_COMPLETE_SINGLE_AJAX(id, _ac_data, _url){
    $( "#" + id ).autocomplete({
        minLength: 2,
        source: _url,
        focus: function() {
            // prevent value inserted on focus
            return false;
        },
        select: function( event, ui ) {
            this.value = ui.item.key;
            $("#" + id).attr("title", ui.item.value);
            return false;
        }
    });
}


