/*Common function start*/

function trimLeft(str) {

    if (str == null) { return null; }

    for (var i = 0; str.charAt(i) == " "; i++);

    return str.substring(i, str.length);

}

function trimRight(str) {

    if (str == null) { return null; }

    for (var i = str.length - 1; str.charAt(i) == " "; i--);

    return str.substring(0, i + 1);

}

function trim(str) { return trimLeft(trimRight(str)); }

function trimLeftAll(str) {

    if (str == null) { return str; }

    for (var i = 0; str.charAt(i) == " " || str.charAt(i) == "\n" || str.charAt(i) == "\t"; i++);

    return str.substring(i, str.length);

}

function trimRightAll(str) {

    if (str == null) { return str; }

    for (var i = str.length - 1; str.charAt(i) == " " || str.charAt(i) == "\n" || str.charAt(i) == "\t"; i--);

    return str.substring(0, i + 1);

}

function trimAll(str) {

    return trimLeftAll(trimRightAll(str));

}

// isNull(value)

//   Returns true if value is null

function isNull(val) { return (val == ""); }

 

// isBlank(value)

//   Returns true if value only contains spaces

function isBlank(val) {

    return isNull(trimAll(val));

}

 

// isInteger(value)

//   Returns true if value contains all digits

function isInteger(val) {

    if (isBlank(val)) { return false; }

    for (var i = 0; i < val.length; i++) {

        if (!isDigit(val.charAt(i))) { return false; }

    }

    return true;

}

 

// isNumeric(value)

//   Returns true if value contains a positive float value

function isNumeric(val) { return (parseFloat(val) == (val * 1) && parseFloat(val) > 0); }

 

// isArray(obj)

// Returns true if the object is an array, else false

function isArray(obj) { return (typeof (obj.length) == "undefined") ? false : true; }

 

// isDigit(value)

//   Returns true if value is a 1-character digit

function isDigit(num) {

    if (num.length > 1) { return false; }

    var string = "1234567890";

    if (string.indexOf(num) != -1) { return true; }

    return false;

}

 

/* setNullIfBlank(input_object)

   Sets a form field to "" if it isBlank()

   */

 

function setNullIfBlank(obj) { if (isBlank(obj.value)) { obj.value = ""; } }

 

/*Basic function added

return true if the string is valid email string

*/

 

function isEmailChar(str) {

    for (var i = 0; i < str.length; i++) {

        c = str.charAt(i);

        if ("~!#$%^&*(),\'`:\;?<>=+\n\t \\\"".indexOf(c, 0) > 0)

            return false;

    }

    return true;

}

 

 

function isValidEmail(email) {

 

    var array = email.split("@");

    if (array.length != 2) return false;

 

    var first, last;

    first = array[0]; last = array[1];

 

    if (first == "" || last == "") return false;

    first = trimLeftAll(first); last = trimRightAll(last);

    if (!isEmailChar(first) || !isEmailChar(last))

        return false;

    return true;

}

 

 

function isInRangeOfNumber(number, from, to) {

    if (from <= number && number <= to)

        return true;

    return false;

}

 

 

function roundEx(val, digit, type) {

    var result = val;

    if (type == "L")

        result = lowerRound(val, digit);

    else if (type == "U")

        result = upperRound(val, digit);

    else

        result = round(val, digit);

    return result;

}

 

/* return round number. Ex: lowerRound(1.255, 2) = 1.26*/

function round(number, digits) {

    var num = Math.pow(10, digits);

    var result = Math.round(number * num);

    result = result / num;

    return result;

}

 

/* return round number. Ex: upperRound(1.254, 2) = 1.268*/

function upperRound(number, digits) {

    var num = Math.pow(10, digits);

    num = num + 0.5;

    var result = Math.round(number * num);

    result = result / num;

    return result;

}

 

/* return round number. Ex: lowerRound(1.256, 2) = 1.25*/

function lowerRound(number, digits) {

    var num = Math.pow(10, digits);

    var result = parseInt(number * num);

    result = result / num;

    return result;

}

/*return true if the ENTER key is pressed*/

function isEnterPressed(evt) {

    evt = (evt) ? evt : event;

    var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);

    if (charCode == 13 || charCode == 3)

        return true;

    return false;

}

 

function fncRandom(count) {

    var ran_unrounded = Math.random() * count;

    var ran_number = Math.floor(ran_unrounded);

    return ran_number;

}

function replaceSubstring(inputString, fromString, toString) {
	   // Goes through the inputString and replaces every occurrence of fromString with toString
	   var temp = inputString;
	   if (fromString == "") {
	      return inputString;
	   }
	   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
	      while (temp.indexOf(fromString) != -1) {
	         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
	         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
	         temp = toTheLeft + toString + toTheRight;
	      }
	   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
	      var midStrings = new Array("~", "`", "_", "^", "#");
	      var midStringLen = 1;
	      var midString = "";
	      // Find a string that doesn't exist in the inputString to be used
	      // as an "inbetween" string
	      while (midString == "") {
	         for (var i=0; i < midStrings.length; i++) {
	            var tempMidString = "";
	            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
	            if (fromString.indexOf(tempMidString) == -1) {
	               midString = tempMidString;
	               i = midStrings.length + 1;
	            }
	         }
	      } // Keep on going until we build an "inbetween" string that doesn't exist
	      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
	      while (temp.indexOf(fromString) != -1) {
	         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
	         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
	         temp = toTheLeft + midString + toTheRight;
	      }
	      // Next, replace the "inbetween" string with the "toString"
	      while (temp.indexOf(midString) != -1) {
	         var toTheLeft = temp.substring(0, temp.indexOf(midString));
	         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
	         temp = toTheLeft + toString + toTheRight;
	      }
	   } // Ends the check to see if the string being replaced is part of the replacement string or not
	   return temp; // Send the updated string back to the user
	} // Ends the "replaceSubstring" function