﻿

function trim(inputString) {
    // Removes leading and trailing spaces from the passed string. Also removes
    // consecutive spaces and replaces it with one space. If something besides
    // a string is passed in (null, custom object, etc.) then return the input.
    if (typeof inputString != "string") { return inputString; }
    var retValue = inputString;
    var ch = retValue.substring(0, 1);
    while (ch == " ") { // Check for spaces at the beginning of the string
        retValue = retValue.substring(1, retValue.length);
        ch = retValue.substring(0, 1);
    }
    ch = retValue.substring(retValue.length - 1, retValue.length);
    while (ch == " ") { // Check for spaces at the end of the string
        retValue = retValue.substring(0, retValue.length - 1);
        ch = retValue.substring(retValue.length - 1, retValue.length);
    }
    while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
            retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ") + 1, retValue.length); // Again, there are two spaces in each of the strings
    }
    return retValue; // Return the trimmed string back to the user
}

function SearchTextMaxLimit(InputString) {
    if (InputString.length > 150) {
        alert('Searched Text exceeds the limit.');
        return false;    
    }
    return true;
}

function SearchTextMinLimit(InputString) {
    if (InputString.length == 1) {
        alert('Please enter subject word to search.');
        return false;    
    }
    return true;
}

function SearchTextMinLimitForTwoChars(InputString) {
    if (InputString.length < 2) {
        alert('Please enter subject word to search.');
        return false;    
    }
    return true;
}

function CheckSpecialChar(InputString) {
    if (InputString.length > 0) {
        var SpecialChar = new Array("~", "!", "@", "$", "%", "^", "*", "_", "+", "|", "+", "|", "<", ">", "?", "|", "#", "/", "{", "}", "'", "[", "]", "`");
        var Counter = 0;
        for (;Counter<SpecialChar.length;Counter++) {
            if (InputString.indexOf(SpecialChar[Counter]) != -1) {
                alert('Special Characters not allowed.');
                return false;
            }
        }
    }
    return true;
}

function ReplaceAll(Source,stringToFind,stringToReplace){
    var temp = Source;
    var index = temp.indexOf(stringToFind);
    while(index != -1){
        temp = temp.replace(stringToFind,stringToReplace);
        index = temp.indexOf(stringToFind);
    }
    return temp;
}

function ReplaceSearchText(InputString) {
    InputString = ReplaceAll(InputString,")"," ");
    InputString = ReplaceAll(InputString,"("," ");
    InputString = ReplaceAll(InputString,","," ");
    InputString = ReplaceAll(InputString,"."," ");     
    return InputString;      
}

function RemoveSpecialWords(InputString) {
    if (InputString.indexOf(InputString) > -1)
        return false;
}

//The split() method is used to split a string into an array of substrings, and returns the new array.
//So in our case we will check if the new array's count is in odd number then it has even number of double quotes & can be sent to the 
//Sql Serevr for searching.
function IsEvenDoubleQuote(InputString) {
    if (InputString.length > 0) {
        //alert((InputString.trim().split('"').length)%2);
        if (((InputString.split('"').length)%2)== 0) {
            alert('Invalid search text');
            return false;
        }
        else        
            return true;
    }    
    return true;
}

function MakeStringSearchable(InputString) {
    var SplitString = InputString.split(" ");
    var TempString = "";
    
    for(var i=0; i<SplitString.length;i++){
         if(SplitString[i].replace('"','').length==1) 
            TempString = TempString + "0" + SplitString[i] + " ";
         else
            TempString = TempString + SplitString[i] + " "; 
               
           
    } 
    
    if (TempString != null && TempString != "")
        return TempString;
    else
        return InputString;
}

function IsNumeric(e) {
    var code = (e.keyCode ? e.keyCode : e.which);
    if ((code > 47 && code < 58) || (code ==13))
       return true;
    else {
       alert('No Numeric value. Please Fill Correctly.');
       return false;
    }
    
}


function IsAlphaNumericWithSpace(e) {
    var code = (e.keyCode ? e.keyCode : e.which);
    if ((code > 47 && code < 58) || (code == 32) || (code == 50) || (code > 64 && code < 91) || (code > 96 && code < 123) || (code ==13))
       return true;
    else {
       alert('No Numeric value. Please Fill Correctly.');
       return false;
    }
    
}