﻿//========================================================================================================//
//.....................This javascript file is used only for mobile number validation.....................//
//========================================================================================================//
function fn_mobileMain(mobileno)
{
    // These lines of code delete extra commas.
    checkComma(mobileno); // Delete first comma, convert multiple comma to single one.
    mobileno.value = lastComma(mobileno.value);  // Delete last comma.
        
    var arrMobile = new Array();
    arrMobile = mobileno.value.split(/[\,]/);
    
    // These lines of code start validating all comma seperated mobile.
    for(var i=0; i<arrMobile.length; i++)
    {
        if(trim(arrMobile[i]) != "")
        {
            if (!checkMobile_12(arrMobile[i]))
            {
                mobileno.focus();
                return false;
            }
        }
    }
    return true;
}


// This function is used to check mobile which is of 12 digits, starts with 91.
function checkMobile_12(sMobile)
{
    if(trim(sMobile) == "") // Check mobile number is blank.
    {
        alert("Mobile number should not be blank.")
        return false;
    }
    if(!IsNumeric(trim(sMobile)))  // Check Numeric.
    {
        alert("Mobile number(" + sMobile +") must be numeric.")
        return false;
    }
    if(trim(sMobile).length != 12) // Check length 12 digits.
    {
        alert("Mobile number(" + sMobile +") must be of 12 digits.")
        return false;
    }
    
    // Check mobile start with 919 or 918
    if(!((trim(sMobile).charCodeAt(0) == 57 && trim(sMobile).charCodeAt(1) == 49 && trim(sMobile).charCodeAt(2) == 57)
        ||(trim(sMobile).charCodeAt(0) == 57 && trim(sMobile).charCodeAt(1) == 49 && trim(sMobile).charCodeAt(2) == 56))) 
    {
        alert("Mobile number(" + sMobile +") must start with 919 or 918.")
        return false;
    }
    
     // Check mobile start with 919
//    if(!((trim(sMobile).charCodeAt(0) == 57 && trim(sMobile).charCodeAt(1) == 49 && trim(sMobile).charCodeAt(2) == 57)))
//    {
//        alert("Mobile number(" + sMobile +") must start with 919.")
//        return false;
//    }
    return true;
}


// This function is used to check mobile which is of 10 digits, does not starts with 91(at some place '91' is in combo and check without concatenation).
function checkMobile_10(sMobile)
{
    if(sMobile == "") // Check mobile number is blank.
    {
        alert("Mobile number should not be blank.")
        return false;
    }
    if(!IsNumeric(sMobile))  // Check Numeric.
    {
        alert("Mobile number(" + sMobile +") must be numeric.")
        return false;
    }
    if(sMobile.length != 10) // Check length 10 digits.
    {
        alert("Mobile number(" + sMobile +") must be of 10 digits.")
        return false;
    }
    
    // Check mobile start with 9 or 8
    if(!((trim(sMobile).charCodeAt(0) == 57)||(trim(sMobile).charCodeAt(0) == 56)))
    {
        alert("Mobile number(" + sMobile +") must start with 9 or 8.")
        return false;
    }
    
     // Check mobile start with 9
//    if(!((trim(sMobile).charCodeAt(0) == 57)))
//    {
//        alert("Mobile number(" + sMobile +") must start with 9.")
//        return false;
//    }
    return true;
}


// This function is used to restrict user to enter only numeric and comma
function KeyNumber()
{
    if (!((window.event.keyCode >= 48 && window.event.keyCode < 58) || window.event.keyCode == 44))
       window.event.keyCode = 0;
}


// This function is used to remove first comma, last comma and also convert multiple consecutive commas to single one.
function checkComma(sMobile)
{
    while(sMobile.value.indexOf(",,")>=0)
    {
        sMobile.value = replaceChar(sMobile.value, ",,",",");
    }
    
    if(sMobile.value.indexOf(",")==0)
        sMobile.value=sMobile.value.substring(1,sMobile.value.length);

    //return sMobile;
}


// This function is used to find last comma in givne mobile no.
function lastComma(sMobile)
{
    if(sMobile.lastIndexOf(",")==sMobile.length-1)
        sMobile=sMobile.substring(0,sMobile.length-1);
    
    return sMobile;
}


// This function is used to check numeric value only
function IsNumeric(sText)
{
  var ValidChars = "0123456789";
  var IsNumber=true;
  var Char;
  for (i = 0; i < sText.length && IsNumber == true; i++) 
  { 
  Char = sText.charAt(i); 
  if (ValidChars.indexOf(Char) == -1) 
     {
     IsNumber = false;
     }
  }
  return IsNumber;
} 


// This function is used to trim the string
function trim(str) {
    return ltrim(rtrim(str));
}

// This function is used to trim the left portion of string
function ltrim(str) { 
    for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
    return str.substring(k, str.length);
}

// This function is used to trim the right portion of string
function rtrim(str) {
    for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
    return str.substring(0,j+1);
}

// This function is used to check white space for trim
function isWhitespace(charToCheck) {
    var whitespaceChars = " \t\n\r\f";
    return (whitespaceChars.indexOf(charToCheck) != -1);
}

// This function is used to replace string by given string value
function replaceChar(string,text,by)
{
    // Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) 
        return string;
    if (i == -1) 
        return string;

    var newstr = string.substring(0,i) + by;

    if ((i+txtLength) < strLength)            
        newstr = newstr + replaceChar(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

//Function to check comma seprated mobileno list
function checkMobileList(toMobile)
{
    toVal=replaceChar(toMobile.value, defaulttotext,"");
    
    while(toVal.indexOf(" ")>0)
    {
        toVal = replaceChar(toVal, " ","");
    }
    if(toVal.substring(toVal.length-1 ,toVal.length)== ",")
    {
         toVal = toVal.substring(0 ,toVal.length-1);
         toMobile.value = toVal;
    }
    var arrTotalcontact = toVal.split(",");                   
    
    for(var i=0; i < arrTotalcontact.length ; i++)
    {                
        if (!checkMobile_12(arrTotalcontact[i]))
        {
            if(toVal=="")
            toMobile.value=defaulttotext;
            return false;
        }
    }
    return true;
}
