﻿//extensions & global functions
String.prototype.isDotNetField = function() {return this.match('^_');}
String.prototype.isDotNetForm = function(str) { return this.match('^' + str); }
String.prototype.startWith = function(t) {
    return (t.toLowerCase() == this.substring(0, t.length).toLowerCase());
}
String.prototype.Sanitize = function() {
    return this.replace(/"/gi, '\'').replace(/}/gi, ']').replace(/{/gi, '[').replace(/&/gi, 'and');
}
String.prototype.Pad=function(padWith,count){
    var o = this.toString();
    if (!padWith) { padWith = '0'; }
    while (o.length < count) {
        o = padWith + o;
    }
    return o;
};
Date.prototype.Format = function() {
    var date = this.getDate();
    var month = this.getMonth()+1;
    var year = this.getFullYear();
    return month.toString().Pad("0", 2) + "/" + date.toString().Pad("0", 2) + "/" + year.toString().Pad("0", 4);
}

Array.prototype.Exists = function(o) {
    for (var i = 0; i < this.length; i++)
        if (this[i] === o)
        return true;
    return false;
}
//assuming that teh array contains KeyValuePair.js objects
Array.prototype.FilterByIndex = function(ids) {
   
    var newArray = new Array();
    for (var i = 0; i <= ids.length; i++)
        newArray.push(this[i]);
        
    return newArray;
}
Array.prototype.removeItems = function(itemsToRemove) {

    if (!/Array/.test(itemsToRemove.constructor)) {
        itemsToRemove = [ itemsToRemove ];
    }

    var j;
    for (var i = 0; i < itemsToRemove.length; i++) {
        j = 0;
        while (j < this.length) {
            if (this[j] == itemsToRemove[i]) {
                this.splice(j, 1);
            } else {
                j++;
            }
        }
    }
}

function GetScrollXY() {
    var x = 0, y = 0;
    if (typeof (window.pageYOffset) == 'number') {
        // Netscape
        x = window.pageXOffset;
        y = window.pageYOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        // DOM
        x = document.body.scrollLeft;
        y = document.body.scrollTop;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        // IE6 standards compliant mode
        x = document.documentElement.scrollLeft;
        y = document.documentElement.scrollTop;
    }
    return { 'x': x, 'y': y };
}


//get coordinates using the string id of the element
// i couldnt figure out how to prototype the actual element
String.prototype.GetCoordinates = function() {
    scrollPos = GetScrollXY();
    var values = new Array();
    var obj = $(this.toString());
    var curleft = 0;

    if (obj.offsetParent)
        while (1) {
        curleft += obj.offsetLeft;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.x)
        curleft += obj.x;

    values[0] = curleft;
    //reset
    obj = $(this.toString());
    var curtop = 0;

    if (obj.offsetParent)
        while (1) {
        curtop += obj.offsetTop;
        if (!obj.offsetParent)
            break;
        obj = obj.offsetParent;
    }
    else if (obj.y)
        curtop += obj.y;

    values[1] = curtop;

    return { left: values[0] - scrollPos.x, top: values[1] - scrollPos.y };
}

function IsIE() {
    if (document.all)
        return true;
    else
        return false;
}
function GetEventSrc(event) { 
    if (IsIE()) //IE
        return event.srcElement;
    else
        return event.target;
}

String.prototype.template = function(o) {
    return this.replace(/{([^{}]*)}/g,
        function(a, b) {
            var r = o[b];
            return typeof r === 'string' || typeof r === 'number' ? r : a;
        }
    );
    };
//from http://omnicode.blogspot.com/2008/05/checkvalidate-date-string-in-javascript.html
function ValidDate(s)   
{      
    // make sure it is in the expected format   
    if (s.search(/^\d{1,2}[\/|\-|\.|_]\d{1,2}[\/|\-|\.|_]\d{4}/g) != 0)   
        return false;   
  
    // remove other separators that are not valid with the Date class              
    s = s.replace(/[\-|\.|_]/g, "/");   
               
    // convert it into a date instance   
    var dt = new Date(Date.parse(s));          
  
    // check the components of the date   
    // since Date instance automatically rolls over each component   
    var arrDateParts = s.split("/");   
    return (   
        dt.getMonth() == arrDateParts[0]-1 &&   
        dt.getDate() == arrDateParts[1] &&   
        dt.getFullYear() == arrDateParts[2]   
    );             
}   
function ValidTime(value) {
    var hasMeridian = false;
    var re = /^\d{1,2}[:]\d{2}([:]\d{2})?( [aApP][mM]?)?$/;
    if (!re.test(value)) { return false; }
    if (value.toLowerCase().indexOf("p") != -1) { hasMeridian = true; }
    if (value.toLowerCase().indexOf("a") != -1) { hasMeridian = true; }
    var values = value.split(":");
    if ((parseFloat(values[0]) < 0) || (parseFloat(values[0]) > 23)) { return false; }
    if (hasMeridian) {
        if ((parseFloat(values[0]) < 1) || (parseFloat(values[0]) > 12)) { return false; }
    }
    if ((parseFloat(values[1]) < 0) || (parseFloat(values[1]) > 59)) { return false; }
    if (values.length > 2) {
        if ((parseFloat(values[2]) < 0) || (parseFloat(values[2]) > 59)) { return false; }
    }
    return true;
}

function ValidId(array,value) {
    return array.Exists(value);
}
// $('#currencyField').formatCurrency();   
(function($) {

    $.fn.FormatCurrency = function(settings) {
        settings = jQuery.extend({
            name: "formatCurrency",
            useHtml: false,
            symbol: '$',
            global: true
        }, settings);

        return this.each(function() {
            var num = "0";
            num = $(this)[settings.useHtml ? 'html' : 'val']();
            num = num.replace(/\$|\,/g, '');
            if (isNaN(num))
                num = "0";
            sign = (num == (num = Math.abs(num)));
            num = Math.floor(num * 100 + 0.50000000001);
            cents = num % 100;
            num = Math.floor(num / 100).toString();
            if (cents < 10)
                cents = "0" + cents;
            for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
                num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));

            $(this)[settings.useHtml ? 'html' : 'val'](((sign) ? '' : '-') + settings.symbol + num + '.' + cents);
        });
    };

    // Remove all non numbers from text
    $.fn.toNumber = function(settings) {
        settings = jQuery.extend({
            name: "toNumber",
            useHtml: false,
            global: true
        }, settings);

        return this.each(function() {
            var method = settings.useHtml ? 'html' : 'val';
            $(this)[method]($(this)[method]().replace(/[^\d\.]/g, ''));
        });
    };


})(jQuery);



var KEY_CODE_VALUE_6 = 54;
var KEY_CODE_VALUE_1 = 52;
var KEY_CODE_VALUE_2 = 50;
var KEY_CODE_VALUE_3 = 51;
var KEY_CODE_VALUE_1 = 49;
var KEY_CODE_VALUE_12 = 0;
var KEY_CODE_VALUE_31 = 0;
var KEY_CODE_VALUE_0 = 0;
var KEY_CODE_VALUE_9 = 0;
var KEY_CODE_VALUE_SPACE = 32;
var KEY_CODE_VALUE_PERIOD = 46;

