﻿function scrollTo(elementId) {
    jQuery.scrollTo('#' + elementId, 'fast');
}

// Replaces all instances of the given substring.
String.prototype.replaceAll = function(
                                        strTarget, // The substring you want to replace
                                        strSubString // The string you want to replace in.
                                        )
 {
    var strText = this;
    var intIndexOfMatch = strText.indexOf(strTarget);

    // Keep looping while an instance of the target string
    // still exists in the string.
    while (intIndexOfMatch != -1) {
        // Relace out the current instance.
        strText = strText.replace(strTarget, strSubString)

        // Get the index of any next matching substring.
        intIndexOfMatch = strText.indexOf(strTarget);
    }

    // Return the updated string with ALL the target strings
    // replaced out with the new substring.
    return (strText);
}

String.format = function(text) {
    //check if there are two arguments in the arguments list
    if (arguments.length <= 1) {
        //if there are not 2 or more arguments there's nothing to replace
        //just return the original text
        return text;

    }
    
    //decrement to move to the second argument in the array
    var tokenCount = arguments.length - 2;
    for (var token = 0; token <= tokenCount; token++) {
        //iterate through the tokens and replace their placeholders from the original text in order
        text = text.replace(new RegExp("\\{" + token + "\\}", "gi"),
                                                arguments[token + 1]);

    }

    return text;
};
/*
* 
* Textarea Maxlength Setter JQuery Plugin 
* Version 1.0
* 
* 
Copyright (c) 2008 Viral Patel
* website : http://viralpatel.net/blogs
* 
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
* 
*/
jQuery.fn.maxlength = function() {
    $("textarea[maxlength]").keypress(function(event) {
        var key = event.which;

        //all keys including return.
        if (key >= 33 || key == 13) {
            var maxLength = $(this).attr("maxlength");
            var length = this.value.length;
            if (length >= maxLength) {

                event.preventDefault();
            }
        }
    });

}

function disableElement(eleId, isDisabled) {
    jQuery('#' + eleId).attr("disabled", isDisabled);
}

function isEnterPressed(evt) {
    evt = (evt) ? evt : event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode == 13)
        return true;

    return false;
}

function maximizeWindow() {
    /***********************************************
    * Auto Maximize Window Script- © Dynamic Drive (www.dynamicdrive.com)
    * This notice must stay intact for use
    * Visit http://www.dynamicdrive.com/ for this script and 100's more.
    ***********************************************/
    top.window.moveTo(0, 0);
    if (document.all) {
        top.window.resizeTo(screen.availWidth, screen.availHeight);
    }
    else if (document.layers || document.getElementById) {
        if (top.window.outerHeight < screen.availHeight || top.window.outerWidth < screen.availWidth) {
            top.window.outerHeight = screen.availHeight;
            top.window.outerWidth = screen.availWidth;
        }
    }
}

function getItemIndexByText(strText, eleOption) {
    if (strText == "" || strText == "")
        return -1;

    var index = -1;
    strText = strText.toLowerCase();
    for (i = 0; i < eleOption.options.length; i++) {
        if (strText == eleOption.options[i].text.toLowerCase()) {
            index = i;
            break;
        }
    }

    return index;
}

function getFilenameFromUrl(url) {
    if (url == null)
        return "";

    var parts = url.split("/");
    return parts[parts.length - 1];
}

function isAbsoluteUrl(url) {
    if (null == url)
        return false;

    return url.indexOf("http://") != -1 || url.indexOf("https://") != -1;
}

function cancelDefault(e) {
    var evt = e || window.event; // IE compatibility
    if (evt.preventDefault) {
        evt.preventDefault();
    } else {
        evt.returnValue = false;
        evt.cancelBubble = true;
    }
    //Processing   
}

/* Exclude #emailListForm */
function getMainForm() {
    return jQuery("form").not("#formEmailList");
}

// Limit number of characters
function applyCharactersCounter(txtboxID, maxChars) {
     var options2 = {
                'maxCharacterSize': 1500,
                'originalStyle': 'originalTextareaInfo',
                'warningStyle': 'warningTextareaInfo',
                'warningNumber': 1499,
                'displayFormat': '#input/#max | #words words'
            };
    $('#' + txtboxID).textareaCount(options2);
}
