/**
 * File: mChekPGvalidtaion.js
 * This files contains mChek specific functions.
 * Updated on 2009-Jun-18
 */

function isAlpha(parm) 
{
    var lwr = 'abcdefghijklmnopqrstuvwxyz ';
    var upr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ ';
    return isValid(parm,lwr+upr);
}

 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
} // Ends the "trim" function


/**
 * This function validates all the Input fields, before submitting the reques to the server
 */
function validateBillerFields()
{
    var errMsg=''; 
    var captchaId = trim(document.getElementById('captchaId').value)
    
    if (captchaId == null || captchaId == "")
    {
        errMsg+= '- Image String cannot be blank\n';   
    }
    if (captchaId.length < 6 )
    {
        errMsg+= '- Image String should be 6 character long\n';
    }
    
    var phoneNumber = trim(document.getElementById('mobileNumber').value);
    // Mobile Number Validations
    if (phoneNumber == null || phoneNumber == "")
    {
        errMsg+= '- Mobile number cannot be blank\n';   
    }
    if (isNaN(phoneNumber))
    {
        errMsg+= '- Mobile number should be numeric\n';
    }
    if (phoneNumber.length < 10 || phoneNumber.length == 11)
    {
        errMsg+= '- Mobile number should be 10 digit long\n';
    }
    if (phoneNumber.substr(0,1) != '9' && phoneNumber.substr(0,1) != '8')
    {
        errMsg+= '- Mobile Number should start with 9 or 8\n';
    }
    
    var productName = document.getElementById('productId').value;
    var amount = trim(document.getElementById('amount').value);
    var forCustomerID = trim(document.getElementById('forCustomerId').value);
    
    // Amount validations
    if (productName == 'LANDLINE')  {
        
        if(amount=='')
                errMsg+= "- Amount cannot be blank\n";
        if(isNaN(amount)==true)
                errMsg+= "- Amount should be numeric\n";
        if (amount < 1)
        {
            errMsg+= "- Minimum amount allowed is Rs 1.0\n";
        }
        if (amount > 10000)
        {
            errMsg+= "- Maximum amount allowed is Rs 10000\n";
        }
    }
    else if (productName == 'AIRTELPREPAID')  {

        if(amount=='selected')
                errMsg+= "- Amount should be selected\n";
        
    }
    else if (productName == 'AIRTEL')
    {
        if(amount!='' && isNaN(amount))
        {
                errMsg+= "- Amount should be numeric\n";
        }
        if (amount!='' && amount < 1)
        {
            errMsg+= "- Minimum amount allowed is Rs 1.0\n";
        }
        if (amount!='' && amount > 10000)
        {
            errMsg+= "- Maximum amount allowed is Rs 10,000\n";
        }
        
        if (forCustomerID != '' && amount == '')
        {
            errMsg+= "- Amount cannot be blank\n";
        }
    }
    else
    {
        errMsg+= "- Payment option cannot be blank\n";
    }
    
    // For Account Id validations
    if (productName == 'LANDLINE' )  {
            
            if(forCustomerID=='')
            {        
                errMsg+= "- Landline Number cannot be blank\n";
            }
            if(isNaN(forCustomerID)==true)
            {
                 errMsg+= "- Landline Number should be numeric\n";
            }
            if (forCustomerID.length != 11) 
            {
                errMsg+= '- Landline number should be 11 digit long\n';
            }
            if (forCustomerID.substr(0,1) != '0')
            {
                errMsg+= '- Landline number should start with 0\n';
            }
    }   
    
    else if ((productName == 'AIRTELPREPAID' || productName == 'AIRTEL') &&  forCustomerID != '')  {

            if(isNaN(forCustomerID)==true)
            {
                 errMsg+= "- Mobile Number to be Recharged should be numeric\n";
            }
            if (forCustomerID.length < 10 || forCustomerID.length == 11)
            {
                errMsg+= '- Mobile Number to be Recharged should be 10 digit long\n';
            }
            if (forCustomerID.substr(0,1) != '9' && forCustomerID.substr(0,1) != '8')
            {
                errMsg+= '- Mobile Number to be Recharged should start with 9 or 8\n';
            }
    }     
    
    if(errMsg!='')
    {
            message = '______________________________________________________\n\n'
            message += 'Following Information are Required:\n';
            message += '______________________________________________________\n\n'
            message += errMsg;
            alert(message);
            return false;
    }
    else{
      return true;
    }
}

  
