
// 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 no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

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 stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}



////////////////////////////////////////////////////////////
// Checks to see whether a value contains non-numeric data.
////////////////////////////////////////////////////////////
function isANumber(inputValue){
	// Assume everything is okay.
	var result = true;
	// If parseFloat() returns false, a non-numeric
	// character was detected in the first position,
	if (!parseFloat(inputValue)) {
		result = false;
	}

	// Otherwise, we still have to check the rest of
	// the digits, so step through the inputValue one
	// character at a time and set result = false
	// if any non-numeric digits are encountered.
	else {
		for (var i=0; i<inputValue.length; i++) {
			if (inputValue.charAt(i) != " ") {
				if (!parseFloat(inputValue.charAt(i))) {
					result = false;
					break;
				}
			}
		}
	}
	// Return true (inputValue is a valid number) or
	// false (its invalid).
	return result
}

//////////////////////////////////////////////////////
// Checks to see whether an input value contains @
// and .
//////////////////////////////////////////////////////
function isAValidEmail(inputValue) {
	var foundAt = false;
	var foundDot = false;
	// Step through the inputValue looking for
	// @ and .
	for (var i=0; i<=inputValue.length; i++) {
		if (inputValue.charAt(i) == "@" ) {
			foundAt = true;
		}
		else if (inputValue.charAt(i) == ".") {
			foundDot = true;
		}
	}
	// If both @ and . were found, assume
	// the e-mail address is valid; otherwise,
	// return false so the calling code knows
	// the e-mail address is invalid.
	if (foundAt && foundDot) {
		return true;
	}
	else {
		return false;
	}
}

//////////////////////////////////////////////////////
// Checks to see if an input value contains ten or more
// numbers. This approach lets users type in U.S.-
// style phone formats, such as (123)456-7890, as
// well as European-style (such as 123.456.7890).
//////////////////////////////////////////////////////
function isAValidPhoneNumber(inputValue) {
	var digitsFound = 0;
	// Step through the inputValue to see how
	// many digits it contains.
	for (var i=0; i<=inputValue.length; i++) {
		if (isANumber(inputValue.charAt(i))) {
			digitsFound++;
		}
	}
	// If inputValue contains at least 10
	// digits, assume it is a valid phone number.
	if (digitsFound >= 10) {
		return true;
	}
	else {
		return false;
	}
}

function isAValidZipcode(zip) {

   len=zip.length
   digits="0123456789"
   if(len != 5 && len != 10)
     return false;

   for(i=0; i<5; i++)
   {
      if (digits.indexOf(zip.charAt(i))<0)
         return false;
    }
 
   return true;
 }


function isAValidCreditCard(ccNumb) {  // v2.0
	var valid = "0123456789"  // Valid digits in a credit card number
	var len = ccNumb.length;  // The length of the submitted cc number
	var iCCN = parseInt(ccNumb);  // integer of ccNumb
	var sCCN = ccNumb.toString();  // string of ccNumb
	sCCN = sCCN.replace (/^\s+|\s+$/g,'');  // strip spaces
	var iTotal = 0;  // integer total set at zero
	var bNum = true;  // by default assume it is a number
	var bResult = false;  // by default assume it is NOT a valid cc
	var temp;  // temp variable for parsing string
	var calc;  // used for calculation of each digit
	
	// Determine if the ccNumb is in fact all numbers
	for (var j=0; j<len; j++) {
	  temp = "" + sCCN.substring(j, j+1);
	  if (valid.indexOf(temp) == "-1"){bNum = false;}
	}
	
	// if it is NOT a number, you can either alert to the fact, or just pass a failure
	if(!bNum){
	  /*alert("Not a Number");*/bResult = false;
	}
	
	// Determine if it is the proper length 
	if((len == 0)&&(bResult)){  // nothing, field is blank AND passed above # check
	  bResult = false;
	} else{  // ccNumb is a number and the proper length - let's see if it is a valid card number
	  if(len >= 15){  // 15 or 16 for Amex or V/MC
		for(var i=len;i>0;i--){  // LOOP throught the digits of the card
		  calc = parseInt(iCCN) % 10;  // right most digit
		  calc = parseInt(calc);  // assure it is an integer
		  iTotal += calc;  // running total of the card number as we loop - Do Nothing to first digit
		  i--;  // decrement the count - move to the next digit in the card
		  iCCN = iCCN / 10;                               // subtracts right most digit from ccNumb
		  calc = parseInt(iCCN) % 10 ;    // NEXT right most digit
		  calc = calc *2;                                 // multiply the digit by two
		  // Instead of some screwy method of converting 16 to a string and then parsing 1 and 6 and then adding them to make 7,
		  // I use a simple switch statement to change the value of calc2 to 7 if 16 is the multiple.
		  switch(calc){
			case 10: calc = 1; break;       //5*2=10 & 1+0 = 1
			case 12: calc = 3; break;       //6*2=12 & 1+2 = 3
			case 14: calc = 5; break;       //7*2=14 & 1+4 = 5
			case 16: calc = 7; break;       //8*2=16 & 1+6 = 7
			case 18: calc = 9; break;       //9*2=18 & 1+8 = 9
			default: calc = calc;           //4*2= 8 &   8 = 8  -same for all lower numbers
		  }                                               
		iCCN = iCCN / 10;  // subtracts right most digit from ccNum
		iTotal += calc;  // running total of the card number as we loop
	  }  // END OF LOOP
	  if ((iTotal%10)==0){  // check to see if the sum Mod 10 is zero
		bResult = true;  // This IS (or could be) a valid credit card number.
	  } else {
		bResult = false;  // This could NOT be a valid credit card number
		}
	  }
	}

  return bResult; // Return the results
}

//////////////////////////////////////////////////////
// Check for the existence of characters.
// (Spaces arent counted.)
//////////////////////////////////////////////////////
function exists(inputValue) {
	var aCharExists = false;
	// Step through the inputValue, using the charAt()
	// method to detect non-space characters.
	for (var i=0; i<=inputValue.length; i++) {
		if (inputValue.charAt(i) != " " && inputValue.charAt(i) != "") {
			aCharExists = true;
			break;
		}
	}
	return aCharExists;
}

