function integerKeys(e)
{
	var ASCII;
	if (document.all)	{	ASCII = event.keyCode;	}
	else				{	ASCII = e;					}
	//CORP2466 ASCII 8 - Backspace
	if (((ASCII >= 48)&&(ASCII <= 57))||(ASCII == 8))     //[ (,-./)=(44-47), (0-9)=(48-57)
	{	return true;	}
	return false;
}
function isEmail(str)
{
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
	var tempStr = "a";
	var tempReg = new RegExp(tempStr);
	if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^([a-zA-Z0-9_\.\-])+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

function EmailValidate(object) {
	var validEmail = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/i;
	if (object) {
		if (object.value=='') {
			alert('You must enter a valid email address');
			object.select();
			return false;
		}
		else {
			if (!validEmail.test(object.value)) {
				alert('You must enter a valid email address');
				object.select();
				return false;
			}	
			else {
				return validateEmail(object);
			}
		}
	}
	else {
		return false;
	}
}


function EmailValidateTwo(EmailFrom, EmailTo) {
	var validEmail = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/i;
	if (EmailFrom) {
		if (EmailFrom.value=='') {
			alert('You must enter a valid email address');
			EmailFrom.select();
			return false;
		}
		else {
			if (!validEmail.test(EmailFrom.value)) {
				alert('You must enter a valid email address');
				EmailFrom.select();
				return false;
			}	
			else {
				if (!validEmail.test(EmailTo.value)) {
				alert('You must enter a valid email address');
				EmailTo.select();
				return false;
				}	
				else {
					return (validateEmail(EmailFrom) && validateEmail(EmailTo));
			}
			}
		}
	}
	else {
		return false;
	}
}

//Only for use called from EmailValidate
function validateEmail(object) {
	var misspell = /@(hotmal|hotmial|homail|hotmil|htmail|hotmai[^l]|hotmia|hotmail[^.]+)/i;
	var hotmail = /@hotmail\./i;
	var com = /\.co(m|\.uk)$/i;
	var email = /.+@.+\..+/i;
	
	if (!(object.value=='') && !email.test(object.value)) {
		alert('You must enter a valid email address');
		object.select();
		return false;
	}
	
	if (misspell.test(object.value)||((hotmail.test(object.value) && !com.test(object.value)))) {
		var newemail = String(object.value);
		newemail = newemail.substr(0,newemail.indexOf('@')) + '@hotmail.com';
		
		if (confirm('"' + object.value + '" may be a miss-spelt "...@hotmail.com" address.\nWould you like to change it to "' + newemail + '"?')) {
			object.value = newemail;
			return true;
		}		
		return false; 
	}

	if (object.value.indexOf('www.')==0) 
		alert( 'Your email address appears to be incorrect, please double-check it' );
		

	
	return true;
}

// Copyright (c) 1998 Sudhakar Chandrasekharan (thaths@netscape.com)
// Funtion to return the type of credit card
function typeOfCard(number) {
	/* 
	//	Card Prefixes
	//
	//	Mastercard	51-55
	//	Visa		4
	//	AmEx		34,37
	//	Discover	6011
	*/

	var firstNumber = number.substring(0,1);
	var firstThreeNumbers = number.substring(0,3);

	if ((firstNumber == 4) && ((number.length == 16) || (number.length == 13))) {
		return "VISA";
	} 

	var firstTwoNumbers = number.substring(0,2);
	if ((firstTwoNumbers > 50 && firstTwoNumbers < 56) && (number.length == 16)) {
		return "MASTERCARD";
	}

	if (firstTwoNumbers == 34 || firstTwoNumbers == 37) {
		return "AMEX";
	}

	var firstFourNumbers = number.substring(0,4);
	if (firstFourNumbers == 6011) {
		return "DISCOVER";
	}
}

// Function that determines whether a credit card number is valid
// Please note that a valid credit card number is not essentially a
// credit card in good standing.


function isValidCardNumber(s) {
	var v = "0123456789";
	var w = "";
	for (var i=0; i < s.length; i++) {
		x = s.charAt(i);
		if (v.indexOf(x,0) != -1)
			w += x;
	}
	var j = w.length / 2;
	if (j < 6.5 || j > 8 || j == 7) return false;
	var k = Math.floor(j);
	var m = Math.ceil(j) - k;
	var c = 0;
	for (var i=0; i<k; i++) {
		a = w.charAt(i*2+m) * 2;
		c += a > 9 ? Math.floor(a/10 + a%10) : a;
	}
	for (var i=0; i<k+m; i++) c += w.charAt(i*2+1-m) * 1;
	return (c%10 == 0);
}

function isValidCreditCard(number, type) {
	var strValidChars = "0123456789";
	var strChar;
	var blnResult = true;
	var ctype;

    if (number.length == 0) return false;

    //  test number consists of valid characters listed above
    for (i = 0; i < number.length && blnResult == true; i++)
        {
        strChar = number.charAt(i);
        if (strValidChars.indexOf(strChar) == -1)
            {
            blnResult = false;
            }
        }
    if (blnResult == false) return false;
    //check if number is of card type selected
    if (type != "1" && type != "2" && type != "4") return false;
    ctype = typeOfCard(number);  
    if ((type == "1") && ( ctype != "VISA")) return false;
    if ((type == "2") && ( ctype != "MASTERCARD")) return false;
    //DELTA
    if ((type == "4") && ( number.length != 16)) return false;
        return isValidCardNumber(number);
}

