﻿
// Credit Card Info

var ccErrorNo = 0;
var ccErrors = new Array ()

ccErrors [0] = "Unknown card type";
ccErrors [1] = "No card number provided";
ccErrors [2] = "Credit card number is in invalid format";
ccErrors [3] = "Credit card number is invalid";
ccErrors [4] = "Credit card number has an inappropriate number of digits";

function checkCreditCard (cardnumber, cardname) {

     
  // Array to hold the permitted card characteristics
  var cards = new Array();

  // Define the cards we support. You may add addtional card types.
  
  //  Name:      As in the selection box of the form - must be same as user's
  //  Length:    List of possible valid lengths of the card number for the card
  //  prefixes:  List of possible prefixes for the card
  //  checkdigit Boolean to say whether there is a check digit
  
  cards [0] = {name: "Visa", 
               length: "13,16", 
               prefixes: "4",
               checkdigit: true};
  cards [1] = {name: "MasterCard", 
               length: "16", 
               prefixes: "51,52,53,54,55",
               checkdigit: true};
  cards [2] = {name: "DinersClub", 
               length: "14,", 
               prefixes: "300,301,302,303,304,305,36,38",
               checkdigit: true};
  cards [3] = {name: "CarteBlanche", 
               length: "14", 
               prefixes: "300,301,302,303,304,305,36,38",
               checkdigit: true};
  cards [4] = {name: "AmEx", 
               length: "15", 
               prefixes: "34,37",
               checkdigit: true};
  cards [5] = {name: "Discover", 
               length: "16", 
               prefixes: "6011",
               checkdigit: true};
  cards [6] = {name: "JCB", 
               length: "15,16", 
               prefixes: "3,1800,2131",
               checkdigit: true};
  cards [7] = {name: "Enroute", 
               length: "15", 
               prefixes: "2014,2149",
               checkdigit: true};
  
               
  // Establish card type
  var cardType = -1;
  for (var i=0; i<cards.length; i++) {

    // See if it is this card (ignoring the case of the string)
    if (cardname.toLowerCase () == cards[i].name.toLowerCase()) {
      cardType = i;
      break;
    }
  }
  
  // If card type not found, report an error
  if (cardType == -1) {
     ccErrorNo = 0;
     return false; 
  }
   
  // Ensure that the user has provided a credit card number
  if (cardnumber.length == 0)  {
     ccErrorNo = 1;
     return false; 
  }
  
  // Check that the number is numeric, although we do permit a space to occur  
  // every four digits. 
  var cardNo = cardnumber
  var cardexp = /^([0-9]{4})\s?([0-9]{4})\s?([0-9]{4})\s?([0-9]{1,4})$/;
  if (!cardexp.exec(cardNo))  {
     ccErrorNo = 2;
     return false; 
  }
    
  // Now remove any spaces from the credit card number
  cardexp.exec(cardNo);
  cardNo = RegExp.$1 + RegExp.$2 + RegExp.$3 + RegExp.$4;
       
  // Now check the modulus 10 check digit - if required
  if (cards[cardType].checkdigit) {
    var checksum = 0;                                  // running checksum total
    var mychar = "";                                   // next char to process
    var j = 1;                                         // takes value of 1 or 2
  
    // Process each digit one by one starting at the right
    var calc;
    for (i = cardNo.length - 1; i >= 0; i--) {
    
      // Extract the next digit and multiply by 1 or 2 on alternative digits.
      calc = Number(cardNo.charAt(i)) * j;
    
      // If the result is in two digits add 1 to the checksum total
      if (calc > 9) {
        checksum = checksum + 1;
        calc = calc - 10;
      }
    
      // Add the units element to the checksum total
      checksum = checksum + calc;
    
      // Switch the value of j
      if (j ==1) {j = 2} else {j = 1};
    } 
  
    // All done - if checksum is divisible by 10, it is a valid modulus 10.
    // If not, report an error.
    if (checksum % 10 != 0)  {
     ccErrorNo = 3;
     return false; 
    }
  }  

  // The following are the card-specific checks we undertake.
  var LengthValid = false;
  var PrefixValid = false; 
  var undefined; 

  // We use these for holding the valid lengths and prefixes of a card type
  var prefix = new Array ();
  var lengths = new Array ();
    
  // Load an array with the valid prefixes for this card
  prefix = cards[cardType].prefixes.split(",");
      
  // Now see if any of them match what we have in the card number
  for (i=0; i<prefix.length; i++) {
    var exp = new RegExp ("^" + prefix[i]);
    if (exp.test (cardNo)) PrefixValid = true;
  }
      
  // If it isn't a valid prefix there's no point at looking at the length
  if (!PrefixValid) {
     ccErrorNo = 3;
     return false; 
  }
    
  // See if the length is valid for this card
  lengths = cards[cardType].length.split(",");
  for (j=0; j<lengths.length; j++) {
    if (cardNo.length == lengths[j]) LengthValid = true;
  }
  
  // See if all is OK by seeing if the length was valid. We only check the 
  // length if all else was hunky dory.
  if (!LengthValid) {
     ccErrorNo = 4;
     return false; 
  };   
  
  // The credit card is in the required format.
  return true;
}



//-------------- End Of Credit Card ---------------






// Phone number validations

var isNN = (navigator.appName.indexOf("Netscape")!=-1);
    function autoTab(input,len, e) 
    {
        var keyCode = (isNN) ? e.which : e.keyCode; 
        var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
        if((input.value.length >= len) && (!containsElement(filter,keyCode))) 
        {
        input.value = input.value.slice(0, len);
        input.form[(getIndex(input)+1) % input.form.length].focus();
        }
    }
    function containsElement(arr, ele)
    {
        var found = false, index = 0;
        while(!found && index < arr.length)
        if(arr[index] == ele)
        found = true;
        else
        index++;
        return found;
    }
    function getIndex(input) 
    {
        var index = -1, i = 0, found = false;
        while (i < input.form.length && index == -1)
        if (input.form[i] == input)index = i;
        else i++;
        return index;
    }
    
    // ---------- End of phone Validations-----------------//

// To Validate ------- IsNumeric------------
 function isNumber(ctrlID,alerttxt)
    {
        var ctrl = document.getElementById(ctrlID);
       if(ctrl!=null)
       { 
        if(isNaN(ctrl.value))
        {
            alert(alerttxt);
            ctrl.value="";
            ctrl.focus();
            return false;
        }
       }
        return true;
        
    }
    function NumLength(CtrlID,len,alertTxt)
    {
        var ctrl = document.getElementById(CtrlID);
        if(ctrl.value.length != len)
        {
            alert(alertTxt);
            ctrl.value="";
            ctrl.focus();
            return false;
        }
        return true;
    }
    function CardRanges(CtrlID,mins,maxs,alertText)
    {
        var ctrl =document.getElementById(CtrlID);
        if((ctrl.value.length < mins)||(ctrl.value.length > maxs))
        {
            alert(alertText);
            ctrl.value="";
            ctrl.focus();
            return false; 
        }
        else
        { 
            return true;
        }
    }
    
// Compare Field Validations    
    function compareFields(ctrl1,ctrl2,alerttxt)
    {
        var ctrlSrc = document.getElementById(ctrl1);
        var ctrlCmp = document.getElementById(ctrl2);
        if(ctrlSrc.value != ctrlCmp.value)
        {
            alert(alerttxt);
            ctrlCmp.focus();
            return false;
        }
        return true;
    }
  
    
 // Required Field Validations
    function txtBox(ctrlID,ctrlName)
    {
           var ctrl = document.getElementById(ctrlID);
     //  alert(ctrl.value);
        if(ctrl.value=="")
        {
            alert(ctrlName);
            ctrl.focus();
            return false;
        }
        return true;
    
    }
// Dropdown validatons ---  
// Add your dropdown value like "Select"
    function drpVerfy(ctrlID,alerttxt)
    {
        var ctrl = document.getElementById(ctrlID);
        if((ctrl.options[ctrl.selectedIndex].value=="Select Year")||(ctrl.options[ctrl.selectedIndex].value=="Select Month")||(ctrl.options[ctrl.selectedIndex].value=="Select")||(ctrl.options[ctrl.selectedIndex].value=="Select an Option") ||(ctrl.options[ctrl.selectedIndex].value=="Select State"))
        {
            alert(alerttxt);
            ctrl.focus();
            return false;
        }
        return true;
    }
    
    function drpOtherVerfy(ctrlID,alerttxt,otherCtrlID)
    {
        var ctrl = document.getElementById(ctrlID);
        var Other = document.getElementById(otherCtrlID);
        if(ctrl.options[ctrl.selectedIndex].innerText=="Others")
        {
            if(Other.value=="")
            {
                alert(alerttxt);
                Other.focus();
                return false;
            }
        }        
        return true;
    }
    
// Regular Expression validations for Email Address
    function EmailVrfy(ctrlID,alerttxt)
    {
        var emailPat =/(^[a-z]([a-z0-9_\.]*)@([a-z][a-z0-9-_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z0-9_\.]*)@([a-z][a-z0-9-_\.]*)(\.[a-z]{2,4})(\.[a-z]{2})*$)/i;    ///^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
        var emailid=document.getElementById(ctrlID).value;
         
        var matchArray = emailid.match(emailPat);
        if (matchArray == null)
        {
            alert(alerttxt);
            document.getElementById(ctrlID).focus();
            return false;
        }
        return true;
    }
   
// Check Box Validations   
    function chkFiels(ctrlID,alerttext)
    {
        var ctrlSrc = document.getElementById(ctrlID);
        if(!ctrlSrc.checked)
        {
            alert(alerttext);
            return false;
        }
            
            return true;
    }
    
// FAQ Toggle - Collapse and Expand Option
    function toggleMe(a){
      var e=document.getElementById(a);
      if(!e)return true;
      if(e.style.display=="none"){
        e.style.display="block"
      } else {
        e.style.display="none"
      }
      return true;
    }
    //***************************************************************************************************************
    //Phone number format validation(###)-###-####
    //all things including extension
    function isInteger(s)
    {   var i;
        for (i = 0; i < s.length; i++)
        {   
            // Check that current character is number.
            var c = s.charAt(i);
            if (((c < "0") || (c > "9")))
            
             return false;
        }
        // All characters are numbers.
        return true;
    }
    
     function isInt(s,msg)
    {   var i;
        for (i = 0; i < s.length; i++)
        {   
            // Check that current character is number.
            var c = s.charAt(i);
            if (((c < 0) || (c > 9)))
            {
             alert(c);
             alert(msg);
             return false;
             }
        }
        // All characters are numbers.
        return true;
    }
    
function ValidateTextBoxForPhone(Phone,length,bn)
{
                var s;    
                if (bn==1)
                    {
                    s="first ";
                    }
                if (bn==2)
                    {
                    s="Second ";
                    }
                if (bn==3)
                    {
                    s="Third";
                    }



    if ((Phone.value==null)||(Phone.value==""))
        {
        alert("Your Pone format should be like (###-###-####) .\n Please submit a valid phone number");//fill the "+ s +" box for phone number");
		Phone.focus();
		return false;
		}
     if ((!isInteger(Phone.value)) || (Phone.value.length != length))
		{
				
		alert("Your format should be like (###-###-####) .\n Please submit a valid phone number")//enter Only "+length+" Numbers in "+ s +" box of the phone number");
		Phone.value='';
		Phone.focus();
		return false;
		}	
    // Now it is Ok 
       return true;
       
    
}
function ValidateExt(extnctl)
{
var ext=document.getElementById(extnctl);
    if((ext.value==null) || (ext.value=="")) 
        {
//            alert("Please enter Extension for the phone No");
//            ext.focus();
//            return false;
                return true;
        }
     else
        {
        if ((!isInteger(ext.value)) || (ext.value.length != 3))
     	    { 
     	    alert("Please Enter  3 numbers  for extension");
     	    ext.value='';
		    ext.focus();
		    return false;
		    } 
		}
		return true;

}
//call this to validate phone with extension with isext 1 if no extension validation isext is 0
function ValidateForPhone(Phone1ctl,Phone2ctl,Phone3ctl,extctl,isext)
{
//all controls with ctl00$ContentPlaceHolder1$ name eg "ctl00$ContentPlaceHolder1$+txtName"
   
   
   var Phone1=document.getElementById(Phone1ctl);
   var Phone2=document.getElementById(Phone2ctl);
   var Phone3=document.getElementById(Phone3ctl);
   
   if ((Phone1.value=="")&& (Phone2.value=="") && (Phone3.value==""))
   {
   alert("please enter Your phone number in this format(###-###-####)");
   Phone1.focus();
   return false;
   }


   if (ValidateTextBoxForPhone(Phone1,3,1))
    {   
        //Phone=document.getElementById('<%=txtPhoneNo2.ClientID%>');
        if (ValidateTextBoxForPhone(Phone2,3,2))
            { 
                //Phone=document.getElementById('<%=txtPhoneNo3.ClientID%>');
                
                if(ValidateTextBoxForPhone(Phone3,4,3))
                {
                   if(isext==1)
                        {
                        return  true;//ValidateExt(extctl) // Made Changes Here, 
                        }
                    else
                        {
                        return true;
                        }
                                       
                }
                else
                {
                    return false;
                }
                
            }
        else
            {                 
                 return false;
            }
    }
    else
    {
         return false;
    }
            
   return true;
   
             
}
// end of phone validation 
//******************************************************************************************************************

