//********************************************************************************
//JAVASCRIPT FUNCTIONS
//********************************************************************************

//--------------------------------------------------------------------------------
// Variables
//--------------------------------------------------------------------------------

//Declaring required variables
var digits = "0123456789";

//non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";

//characters which are allowed in international phone numbers (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";

//Minimum number of digits in an international phone number
var minDigitsInIPhoneNumber = 10;

//--------------------------------------------------------------------------------
// Functions
//--------------------------------------------------------------------------------

function IsNumeric(strValue)
{
    //Initialize variables
    var blnIsNumber = true;
    var ValidChars = "0123456789.";   
    var Char;

    //Cycle through all the character(s) to verify that it is valid
    for (i = 0; i < strValue.length && blnIsNumber == true; i++) 
    {
        //Gather the character      
        Char = strValue.charAt(i);
        
        //CHECK : Verify that the character is in the 'ValidChars' string
        if (ValidChars.indexOf(Char) == -1) 
        {
            //Set the value
            blnIsNumber = false;
        }
    }
   
    return blnIsNumber;   
}

function Trim(strValue)
{   
    //Initialize variables
    var strTrimString = "";
    var Char;
    
    //Cycle through all the character(s) removing any whitespace
    for (i = 0; i < strValue.length; i++)
    {   
        //Gather the character      
        Char = strValue.charAt(i);
    
        //CHECK : Verify that the character isn't whitespace        
        if (Char != " ") 
        {
            strTrimString += Char;
        }
    }
    
    return strTrimString;
}

function stripCharsInBag(strValue, bag)
{  
    //Initialize variables
    var strReturnString = "";
    var Char;
    
    //Cycle through all the character(s)
    for (i = 0; i < strValue.length; i++)
    {   
        //Gather the character      
        Char = strValue.charAt(i);
        
        alert(bag.indexOf(Char));
        
        //CHECK : Verify that the character is not in the 'Bag'
        if (bag.indexOf(Char) == -1)
        {
            strReturnString += Char;
        }
    }
    
    return strReturnString;
}

function CheckInternationalPhone(strPhoneNumber)
{
    //Initialize variables 
    var blnIsValidPhoneNumber = true;   
    var bracket = 3;
    
    //Trim the data
    strPhoneNumber = Trim(strPhoneNumber);
    
    alert(strPhoneNumber);
    
    if(strPhoneNumber.indexOf("+") > 1) 
    {
        //Return the value
        return false;
    }

    if(strPhoneNumber.indexOf("-") != -1)
    {
        bracket = bracket + 1;
    }

    if(strPhoneNumber.indexOf("(") != -1 && strPhoneNumber.indexOf("(") > bracket)
    {
        //Return the value
        return false;
    }
    
    var brchr = strPhoneNumber.indexOf("(");
    
    if(strPhoneNumber.indexOf("(") != -1 && strPhoneNumber.charAt(brchr + 2) != ")")
    {
        //Return the value
        return false
    }
    
    if(strPhoneNumber.indexOf("(") == -1 && strPhoneNumber.indexOf(")") != -1)
    {
        //Return the value
        return false
    }

    s = stripCharsInBag(strPhoneNumber, validWorldPhoneChars);
    
    alert(s);
    
    return (IsNumeric(s) && s.length >= minDigitsInIPhoneNumber);    
}

function ValidateText(term, blnFullTextTerm)
{
    //Initialize variables
    var searchtoken;
    var tempstring;
    
    //Set the values
    searchtoken = term
    tempstring = searchtoken
    
    if (blnFullTextTerm == 'undefined')
    {
        blnFullTextTerm = false;
    }    
                                    
    if (tempstring == '')
    {
        alert("Search is empty. Please try again.")        
        return false;
    }
                                    
    if (tempstring.length < 3)
    {
        alert("Search must have at least 3 letters. Please try again.")        
        return false;
    }  
    
    //CHECK : Determine if FullText term supplied
    if (blnFullTextTerm == true)
    {
        if (tempstring.indexOf("\"") >= 0)
        {
            alert("Invalid Query. Please remove any quotes from your full text entry.")                
            return false;
        }
        
        if (tempstring.indexOf("'") >= 0)
        {
            alert("Invalid Query. Please remove any quotes from your full text entry.")                
            return false;
        }
    }                                  
                                    
    // Check for invalid characters    
    tempstring = Trim(tempstring.replace(/[\w\*\#\.\+\-\&" ]+?/gi,""));     
    tempstring = Trim(tempstring.replace("\u00C0",""));
    tempstring = Trim(tempstring.replace("\u00C1",""));
    tempstring = Trim(tempstring.replace("\u00C2",""));
    tempstring = Trim(tempstring.replace("\u00C3",""));
    tempstring = Trim(tempstring.replace("\u00C4",""));
    tempstring = Trim(tempstring.replace("\u00C5",""));
    tempstring = Trim(tempstring.replace("\u00C6",""));
    tempstring = Trim(tempstring.replace("\u00C7",""));                      
    tempstring = Trim(tempstring.replace("\u00C8",""));
    tempstring = Trim(tempstring.replace("\u00C9",""));
    tempstring = Trim(tempstring.replace("\u00CA",""));
    tempstring = Trim(tempstring.replace("\u00CB",""));
    tempstring = Trim(tempstring.replace("\u00CC",""));
    tempstring = Trim(tempstring.replace("\u00CD",""));
    tempstring = Trim(tempstring.replace("\u00CE",""));
    tempstring = Trim(tempstring.replace("\u00CF",""));
    tempstring = Trim(tempstring.replace("\u00D0",""));
    tempstring = Trim(tempstring.replace("\u00D1",""));
    tempstring = Trim(tempstring.replace("\u00D2",""));
    tempstring = Trim(tempstring.replace("\u00D3",""));
    tempstring = Trim(tempstring.replace("\u00D4",""));
    tempstring = Trim(tempstring.replace("\u00D5",""));
    tempstring = Trim(tempstring.replace("\u00D6",""));
    tempstring = Trim(tempstring.replace("\u00D7",""));
    tempstring = Trim(tempstring.replace("\u00D8",""));
    tempstring = Trim(tempstring.replace("\u00D9",""));
    tempstring = Trim(tempstring.replace("\u00DA",""));
    tempstring = Trim(tempstring.replace("\u00DB",""));
    tempstring = Trim(tempstring.replace("\u00DC",""));
    tempstring = Trim(tempstring.replace("\u00DD",""));
    tempstring = Trim(tempstring.replace("\u00DE",""));
    tempstring = Trim(tempstring.replace("\u00DF",""));
    tempstring = Trim(tempstring.replace("\u00E0",""));
    tempstring = Trim(tempstring.replace("\u00E1",""));
    tempstring = Trim(tempstring.replace("\u00E2",""));
    tempstring = Trim(tempstring.replace("\u00E3",""));
    tempstring = Trim(tempstring.replace("\u00E4",""));
    tempstring = Trim(tempstring.replace("\u00E5",""));
    tempstring = Trim(tempstring.replace("\u00E6",""));
    tempstring = Trim(tempstring.replace("\u00E7",""));
    tempstring = Trim(tempstring.replace("\u00E8",""));
    tempstring = Trim(tempstring.replace("\u00E9",""));
    tempstring = Trim(tempstring.replace("\u00EA",""));
    tempstring = Trim(tempstring.replace("\u00EB",""));
    tempstring = Trim(tempstring.replace("\u00EC",""));
    tempstring = Trim(tempstring.replace("\u00ED",""));
    tempstring = Trim(tempstring.replace("\u00EE",""));
    tempstring = Trim(tempstring.replace("\u00EF",""));
    tempstring = Trim(tempstring.replace("\u00F0",""));
    tempstring = Trim(tempstring.replace("\u00F1",""));
    tempstring = Trim(tempstring.replace("\u00F2",""));
    tempstring = Trim(tempstring.replace("\u00F3",""));
    tempstring = Trim(tempstring.replace("\u00F4",""));
    tempstring = Trim(tempstring.replace("\u00F5",""));
    tempstring = Trim(tempstring.replace("\u00F6",""));
    tempstring = Trim(tempstring.replace("\u00F7",""));
    tempstring = Trim(tempstring.replace("\u00F8",""));
    tempstring = Trim(tempstring.replace("\u00F9",""));
    tempstring = Trim(tempstring.replace("\u00FA",""));
    tempstring = Trim(tempstring.replace("\u00FB",""));
    tempstring = Trim(tempstring.replace("\u00FC",""));
    tempstring = Trim(tempstring.replace("\u00FD",""));
    tempstring = Trim(tempstring.replace("\u00FE",""));
    tempstring = Trim(tempstring.replace("\u00FF",""));      
                                                    
    if (tempstring != '') 
    {
        alert(tempstring + " is invalid in search. Please try again.");           
        return false;
    }
                                                                    
    tempstring = searchtoken;
                                    
    //CHECK : Valid for balanced double quotes
    tempstring = tempstring.replace(/\"[^\"]+?\"/gi,"")       
                                                    
    if (tempstring.indexOf('""') > -1) 
    {                            
        alert('Double quotes are empty. Please try again.');         
        return false;
    }
                    
    if (tempstring.indexOf('"') > -1) 
    {                              
        alert('Double quotes are not balanced. Please try again.');            
        return false;
    }
                    
    return true;
}

//--------------------------------------------------------------------------------
