﻿function ReplaceSingularity(str, isSingular) {
    var patt = /{{[\w|]*}}/;
    var result = patt.exec(str);
    var safeCounter = 0;
    while (result) {
        if (isSingular) {
            patt2 = /{{[\w]*/;
            var result2 = patt2.exec(str).toString();
            str = str.replace(result.toString(), result2.substr(2, result2.length - 2));
        }
        else {
            patt2 = /[\w]*}}/;
            var result2 = patt2.exec(str).toString();
            str = str.replace(result.toString(), result2.substr(0, result2.length - 2));
        }
        result = patt.exec(str);
        safeCounter++;
        if (safeCounter > 10) return;
    }
    return str;
}
function GetReplacedStringFromTemplateString(num, origninalText) {
    if (isNaN(num)) {
        num = 0;
    }
    var str = '';
    str = origninalText.replace('{{1}}', num.toString());
    if (num == 1) {
        str = ReplaceSingularity(str, true);
    }
    else {
        str = ReplaceSingularity(str, false);
    }
    return str;
}
/**
* @description This function converts a string that represent time in standard format into a string that represents time in military format
* @example 1:00:00 PM is converted into 13:00:00 and returned as a string
* @param {String} time T as string in standart time format (12:00:00 AM) that needs to be converted          
* @returntype String militaryTime which is the input string converted into military time (24:00:00) 				 
* @createdBy Krste Stojkoski               				 
* @createdOn 22.09.2010               					
*/

function ConvertToMilitaryTime(T) {
    var time = T.toString();
    var hours = time.substring(0, 2);
    var intHours = parseInt(hours);
    if (T.indexOf("12:") == 0) {
        if (T.indexOf("PM") > 0 && T.substring(3, 5) == "00") {
            var militaryHours = intHours;
            militaryTime = T.replace(hours, militaryHours.toString());
            militaryTime = militaryTime.replace(" PM", ":00");
        }
        else if (T.indexOf("AM") > 0 && T.substring(3, 5) == "00") {
            var militaryHours = intHours - 12;
            militaryTime = T.replace(hours, militaryHours.toString());
            militaryTime = militaryTime.replace(" AM", ":00");
        }
        else if (T.indexOf("AM") > 0 && T.substring(3, 5) != "00") {
            var militaryHours = intHours - 12;
            militaryTime = T.replace(hours, militaryHours.toString());
            militaryTime = militaryTime.replace(" AM", ":00");

        }
        else if (T.indexOf("PM") > 0 && T.substring(3, 5) != "00") {
            var militaryHours = intHours;
            militaryTime = T.replace(hours, militaryHours.toString());
            militaryTime = militaryTime.replace(" PM", ":00");

        }
    }
    else {
        if (T.indexOf("PM") > 0 && T.indexOf("12") <= 0) {
            var militaryHours = intHours + 12;
            var militaryTime = T.replace(intHours.toString(), militaryHours.toString());
            militaryTime = militaryTime.replace(" PM", ":00");
        }
        else {
            var militaryHours = intHours;
            //militaryTime = T.replace(hours, militaryHours.toString());
            militaryTime = T.replace(" AM", ":00");
        }
    }
    return (militaryTime);
}
/**
* @description This function converts a string that represent time in military format into a string that represents time in standard format
* @example 13:00:00  is converted into 1:00PM and returned as a string
* @param {String} time T as string in military time format (14:00:00) that needs to be converted          
* @returntype String standardTime which is the input string converted into standard time (11:00 AM) 				 
* @createdBy Krste Stojkoski               				 
* @createdOn 23.09.2010               					
*/

function ConvertToStandardTime(T) {
    var time = T.toString();
    var hours = time.substring(0, 2);
    var intHours = parseInt(hours);
    if (intHours > 12) {
        var standardHours = intHours - 12;
        var standardTime = T.replace(hours, standardHours.toString());
        standardTime = standardTime.substr(0, standardTime.length - 3);
        standardTime = standardTime + " PM";
    }
    else {
        var standardHours = intHours;
        standardTime = T;
        standardTime = standardTime.replace(T.length - 2, "AM");
    }
    return (standardTime);
}

/**
* @description This function converts a DateTime that represent date and time into a string that represents time in standard format
* @example 22.12.2010 13:00:00  is converted into 1:00PM and returned as a string
* @param {Date} date and time DT as string in military time format (22.12.2010 14:00:00) that needs to be converted          
* @returntype String standardTime which is the input date converted into standard time (11:00 AM) 				 
* @createdBy Daniel Nikolovski
* @createdOn 22.12.2010               					
*/
function GetClockTime(DateInput) {
    var hour = DateInput.getHours();
    var minute = DateInput.getMinutes();
    //var second = DateInput.getSeconds();
    var ap = "AM";
    if (hour > 11) { ap = "PM"; }
    if (hour > 12) { hour = hour - 12; }
    if (hour == 0) { hour = 12; }
    if (hour < 10) { hour = "0" + hour; }
    if (minute < 10) { minute = "0" + minute; }
    //if (second < 10) { second = "0" + second; }
    var timeString = hour +
                            ':' +
                            minute +
                            //':' +
                            //second +
                            " " +
                            ap;
    return timeString;
}

/**
* @description This function validate if a certain string is an integer
* @example "11" -true  "12G" false
* @param {String} string that needs to be checked          
* @returntype bool true or false
* @createdBy Krste Stojkoski               				 
* @createdOn 23.09.2010               					
*/
function isInteger(s)
{ return parseInt(s, 10) === s; }

/**
* @description This function validate if a certain string is an integer. It is taking in mind the negative number also. 
* IF YOU ALLOW NEGATIVE NUMBERS USE THIS INSTEAD OF PREVIOUS
* @example "-11" -true  "12G" false "11" -true
* @param {String} string that needs to be checked          
* @returntype bool true or false
* @createdBy Krste Stojkoski               				 
* @createdOn 23.09.2010               					
*/
function isUnsignedInteger(s) {
    return (s.toString().search(/^[0-9]+$/) == 0);
}
/**
* @description This function validate if a certain string is null or empty.
           					
*/
function IsNullOrEmpty(value) {
  var isNullOrEmpty = true;
  if (value) 
  {
   if (typeof (value) == 'string') 
   {
    if (value.length > 0)
     isNullOrEmpty = false;
   }
  }
  return isNullOrEmpty;
 }
/**
* @description This function receives number of secunds in integer and converts them into military time HH:MM:SS format
* @example convertSecondsToHHMMSS(86400) will return 24:00:00
* @param {Integer} number of secunds to be converted          
* @return string 
* @createdBy Krste Stojkoski               				 
* @createdOn 23.09.2010               					
*/
function convertSecondsToHHMMSS(intSecondsToConvert) {
    var hours = convertHours(intSecondsToConvert);
    var minutes = getRemainingMinutes(intSecondsToConvert);
    minutes = (minutes == 60) ? "00" : minutes;
    var seconds = getRemainingSeconds(intSecondsToConvert);
    return hours + ":" + minutes;
}
/**
* @description This function receives number of secunds in integer and converts them into Hours. 
* It returns finite number of hours, you will need to convert the remaining minutes and seconds separately using:
* getRemainingMinutes and getRemainingSeconds functions
* @example convertHours(50400) will return 14 which is the number of total hours
* @param {Integer} number of seconds to be converted          
* @return Integer- the number of total hours 
* @createdBy Krste Stojkoski               				 
* @createdOn 23.09.2010               					
*/
function convertHours(intSeconds) {
    var minutes = convertMinutes(intSeconds);
    var hours = Math.floor(minutes / 60);
    return hours;
}
/**
* @description This function receives number of secunds in integer and converts them into minutes. 
* It returns finite number of minutes, you will need to convert the remaining secundsseparately using getRemainingSeconds function
* @example convertMinutes(50400) will return 14 which is the number of total hours
* @param {Integer} number of seconds to be converted          
* @return Integer- the number of total minutes 
* @createdBy Krste Stojkoski               				 
* @createdOn 23.09.2010               					
*/
function convertMinutes(intSeconds) {
    return Math.floor(intSeconds / 60);
}
/**
* @description This function receives number of seconds in integer and returns integer representing the number of secunds which
* cannot be converted into hours or minutes 
* @example getRemainingSeconds(50466) will return 6 which is the number of seconds which cannot be converted into hours (14 H, 1M and 6S)
* @param {Integer} number of seconds to be converted          
* @return Integer- the number of remaining seconds
* @createdBy Krste Stojkoski               				 
* @createdOn 23.09.2010               					
*/
function getRemainingSeconds(intTotalSeconds) {
    return (intTotalSeconds % 60);
}
/**
* @description This function receives number of seconds in integer and returns integer representing the number of converted minutes which 
* are remaining and cannot be converted into hours
* @example getRemainingMinutes(50466) will return 1 which is the number of minutes which cannot be converted into hours (14 H, 1M and 6S)
* @param {Integer} number of seconds to be converted          
* @return Integer- the number of remaining seconds
* @createdBy Krste Stojkoski               				 
* @createdOn 23.09.2010               					
*/
function getRemainingMinutes(intSeconds) {
    var intTotalMinutes = convertMinutes(intSeconds);
    return (intTotalMinutes % 60);
}
/**
* @description This function receives time in string, formated in military time (HH:MM:SS) and convert it into total number of seconds
* @example HMStoSec1('24:00:00') will return 86400 which is the number of seconds in 24 hours
* @param {String}  Time in Military format. IT DOESNT WORK FOR STANDARD TIME (AM/PM)      
* @return Integer- the number of remaining seconds
* @createdBy Krste Stojkoski               				 
* @createdOn 23.09.2010               					
*/
function HMStoSec1(T) { // h:m:s
    var A = T.split(/\D+/); return (A[0] * 60 + +A[1]) * 60 + +A[2]
}

function ResetOnDEL(s, e) {
    if (e.htmlEvent.keyCode == 46) {
        s.SetValue(null);
    }
}

function numbersOnly(s, e) {
    var evt = (e) ? e : window.event;
    var key = (evt.htmlEvent.keyCode) ? evt.htmlEvent.keyCode : evt.htmlEvent.which;

    if (key != null) {
        key = parseInt(key, 10);

        if ((key < 48 || key > 57) && (key < 96 || key > 105)) {
            if (!isUserFriendlyChar(key, false))
                return _aspxPreventEvent(e.htmlEvent);
        }
        else {
            if (key > 95 && key < 106) {
               return _aspxPreventEvent(e.htmlEvent);
            }
            if (evt.htmlEvent.shiftKey)
                return _aspxPreventEvent(e.htmlEvent);
        }
    }

    return true;
}

function isUserFriendlyChar(val, isFloat) {
    if (isFloat) {
        if (val == 46) { //tocka
            return true;
        }
    }

    if ((val > 34 && val < 41) || val == 45 || val == 46)
        return false;

    // Backspace, Tab, Enter, Insert, and Delete
    if (val == 8 || val == 9 || val == 13 || val == 45 || val == 46)
        return true;

    // Ctrl, Alt, CapsLock, Home, End, and Arrows
    if ((val > 16 && val < 21) || (val > 34 && val < 41))
        return true;

    // The rest
    return false;
}

function floatNumbersOnly(s, e) {
    var evt = (e) ? e : window.event;
    var key = (evt.htmlEvent.keyCode) ? evt.htmlEvent.keyCode : evt.htmlEvent.which;

    if (key != null) {
        key = parseInt(key, 10);

        if ((key < 48 || key > 57) && (key < 96 || key > 105)) {
            if (!isUserFriendlyChar(key, true))
                return _aspxPreventEvent(e.htmlEvent);
        }
        else {
            if (key > 95 && key < 106) {
                return _aspxPreventEvent(e.htmlEvent);
            }
            if (evt.htmlEvent.shiftKey)
                return _aspxPreventEvent(e.htmlEvent);
        }
    }

    return true;
}

/**
* @description This function receives number and round it to the needed number of decimals
* @example RoundNumber(24.486,2) will return 24.49
* @param num- the numbber which needs to be rounded, dec- number of decimals    
* @return rounded number
* @createdBy Krste Stojkoski               				 
* @createdOn 21.04.2011               					
*/
function roundNumber(num, dec) {
    var result = Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
    return result;
}

/**
* @description Converting a currency string without "$" to number with Javascript
* @param {int} Currency string without "$" which needs to be converted    
* @return Float - converted number
* @createdBy Daniel Nikolovski
* @createdOn 17.08.2011
*/
function fromCurrencyToNumber(currencyString) {
    return Number(currencyString.replace(/[^0-9\.]+/g, ""));
}

/**
* @description Converting a number to currency with Javascript
* @param {int}  Number which needs to be converted    
* @return String- converted number
* @createdBy Krste Stojkoski               				 
* @createdOn 27.04.2010               					
*/
function toCurrency(num) {
    num = num.toString().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))
    }

    return (((sign) ? '' : '-') + '$' + num + '.' + cents)
}

/**
* @description Validates credit card number according to its type
* @param {string type}  Name of the Credit Card Type  
* @param {string ccnum} Credit Card Number
* @return Bool- Returns true if the number is valid, false otherwise
* @createdBy Krste Stojkoski               				 
* @createdOn 14.08.2011               					
*/
function isValidCreditCard(type, ccnum) {
    if (type == "Visa") {
        // Visa: length 16, prefix 4, dashes optional.
        var re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
    } else if (type == "MC") {
        // Mastercard: length 16, prefix 51-55, dashes optional.
        var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
    } else if (type == "Disc") {
        // Discover: length 16, prefix 6011, dashes optional.
        var re = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
    } else if (type == "AmEx") {
        // American Express: length 15, prefix 34 or 37.
        var re = /^3[4,7]\d{13}$/;
    } else if (type == "Diners") {
        // Diners: length 14, prefix 30, 36, or 38.
        var re = /^3[0,6,8]\d{12}$/;
    }
    if (!re.test(ccnum)) return false;
    // Remove all dashes for the checksum checks to eliminate negative numbers
    ccnum = ccnum.split("-").join("");
    // Checksum ("Mod 10")
    // Add even digits in even length strings or odd digits in odd length strings.
    var checksum = 0;
    for (var i = (2 - (ccnum.length % 2)); i <= ccnum.length; i += 2) {
        checksum += parseInt(ccnum.charAt(i - 1));
    }
    // Analyze odd digits in even length strings or even digits in odd length strings.
    for (var i = (ccnum.length % 2) + 1; i < ccnum.length; i += 2) {
        var digit = parseInt(ccnum.charAt(i - 1)) * 2;
        if (digit < 10) { checksum += digit; } else { checksum += (digit - 9); }
    }
    if ((checksum % 10) == 0) return true; else return false;
}
function IsValidYear(dateFilled) {
    var d = new Date();
    //var d2 = new Date();
    var dateFilledString = "Aug 9," + dateFilled;
    //d2 = Date.parse(dateFilledString);
    var curr_date = d.getDate();
    var curr_month = d.getMonth();
    var curr_year = d.getFullYear();
    //var filled_year = d2.getFullYear();
    //var added_year=dateFilled.getFullYear();
    if (curr_year > dateFilled || dateFilled > (curr_year + 15)) {

        return false;
    }
    return true;
}
