<!-- Original:  Simon Tneoh (tneohcb@pc.jaring.my) -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

var Cards = new makeArray(8);
Cards[0] = new CardType("MC", "51,52,53,54,55", "16");
var MC = Cards[0];
Cards[1] = new CardType("VISA", "4", "13,16");
var VISA = Cards[1];
Cards[2] = new CardType("AMEX", "34,37", "15");
var AMEX = Cards[2];
Cards[3] = new CardType("DinersClubCard", "30,36,38", "14");
var DinersClubCard = Cards[3];
Cards[4] = new CardType("DISC", "6011", "16");
var DISC = Cards[4];
Cards[5] = new CardType("enRouteCard", "2014,2149", "15");
var enRouteCard = Cards[5];
Cards[6] = new CardType("JCBCard", "3088,3096,3112,3158,3337,3528", "16");
var JCBCard = Cards[6];
var LuhnCheckSum = Cards[7] = new CardType();

/*************************************************************************\
CheckCardNumber(form)
function called when users click the "check" button.
\*************************************************************************/
function CheckCardNumber(form) {

var tmpyear;
if (form.txtCreditCardNumber.value.length == 0) {
alert("Please enter a Card Number.");
form.txtCreditCardNumber.focus();
return false;
}

//if (form.txtExpirationDate2.value.length == 0) {
//alert("Please enter the Expiration Year.");
//form.txtExpirationDate2.focus();
//return false;
//}

if (form.txtExpirationDate2.options[form.txtExpirationDate2.selectedIndex].value > 96)
tmpyear = "19" + form.txtExpirationDate2.options[form.txtExpirationDate2.selectedIndex].value;
else if (form.txtExpirationDate2.options[form.txtExpirationDate2.selectedIndex].value < 21)
tmpyear = "20" + form.txtExpirationDate2.options[form.txtExpirationDate2.selectedIndex].value;
else {
alert("The Expiration Year is not valid.");
return false;
}
tmpmonth = form.txtExpirationDate1.options[form.txtExpirationDate1.selectedIndex].value;
// The following line doesn't work in IE3, you need to change it
// to something like "(new CardType())...".
// if (!CardType().isExpiryDate(tmpyear, tmpmonth)) {
if (!(new CardType()).isExpiryDate(tmpyear, tmpmonth)) {
alert("This card has already expired.");
return false;
}
card = form.cboCreditCardType.options[form.cboCreditCardType.selectedIndex].value;
var retval = eval(card + ".checkCardNumber(\"" + form.txtCreditCardNumber.value +
"\", " + tmpyear + ", " + tmpmonth + ");");
cardname = "";
if (retval)



// comment this out if used on an order form
//alert("This card number appears to be valid.");
return true;

else {
// The cardnumber has the valid luhn checksum, but we want to know which
// cardtype it belongs to.
for (var n = 0; n < Cards.size; n++) {
if (Cards[n].checkCardNumber(form.txtCreditCardNumber.value, tmpyear, tmpmonth)) {
cardname = Cards[n].getCardType();
break;
   }
}
if (cardname.length > 0) {
	if (cardname == "MC"){
		cardname = "MasterCard";
	}
	if (cardname == "AMEX"){
		cardname = "American Express";
	}
	if (cardname == "VISA"){
		cardname = "Visa Card";
	}
	if (card == "MC"){
		card = "MasterCard";
	}
	if (card == "AMEX"){
		card = "American Express";
	}
	if (card == "VISA"){
		card = "Visa Card";
	}
alert("This looks like a " + cardname + " number, not a " + card + " number.");
return false;
}
else {
alert("This card number is not valid.");
return false;
      }
   }
}
/*************************************************************************\
Object CardType([String cardtype, String rules, String len, int year,
                                        int month])
cardtype    : type of card, eg: MasterCard, Visa, etc.
rules       : rules of the cardnumber, eg: "4", "6011", "34,37".
len         : valid length of cardnumber, eg: "16,19", "13,16".
year        : year of expiry date.
month       : month of expiry date.
eg:
var VisaCard = new CardType("Visa", "4", "16");
var AmExCard = new CardType("AmEx", "34,37", "15");
\*************************************************************************/
function CardType() {
var n;
var argv = CardType.arguments;
var argc = CardType.arguments.length;

this.objname = "object CardType";

var tmpcardtype = (argc > 0) ? argv[0] : "CardObject";
var tmprules = (argc > 1) ? argv[1] : "0,1,2,3,4,5,6,7,8,9";
var tmplen = (argc > 2) ? argv[2] : "13,14,15,16,19";

this.setCardNumber = setCardNumber;  // set CardNumber method.
this.setCardType = setCardType;  // setCardType method.
this.setLen = setLen;  // setLen method.
this.setRules = setRules;  // setRules method.
this.setExpiryDate = setExpiryDate;  // setExpiryDate method.

this.setCardType(tmpcardtype);
this.setLen(tmplen);
this.setRules(tmprules);
if (argc > 4)
this.setExpiryDate(argv[3], argv[4]);

this.checkCardNumber = checkCardNumber;  // checkCardNumber method.
this.getExpiryDate = getExpiryDate;  // getExpiryDate method.
this.getCardType = getCardType;  // getCardType method.
this.isCardNumber = isCardNumber;  // isCardNumber method.
this.isExpiryDate = isExpiryDate;  // isExpiryDate method.
this.luhnCheck = luhnCheck;// luhnCheck method.
return this;
}

/*************************************************************************\
boolean checkCardNumber([String cardnumber, int year, int month])
return true if cardnumber pass the luhncheck and the expiry date is
valid, else return false.
\*************************************************************************/
function checkCardNumber() {
var argv = checkCardNumber.arguments;
var argc = checkCardNumber.arguments.length;
var cardnumber = (argc > 0) ? argv[0] : this.cardnumber;
var year = (argc > 1) ? argv[1] : this.year;
var month = (argc > 2) ? argv[2] : this.month;

this.setCardNumber(cardnumber);
this.setExpiryDate(year, month);

if (!this.isCardNumber())
return false;
if (!this.isExpiryDate())
return false;

return true;
}
/*************************************************************************\
String getCardType()
return the cardtype.
\*************************************************************************/
function getCardType() {
return this.cardtype;
}
/*************************************************************************\
String getExpiryDate()
return the expiry date.
\*************************************************************************/
function getExpiryDate() {
return this.month + "/" + this.year;
}
/*************************************************************************\
boolean isCardNumber([String cardnumber])
return true if cardnumber pass the luhncheck and the rules, else return
false.
\*************************************************************************/
function isCardNumber() {
var argv = isCardNumber.arguments;
var argc = isCardNumber.arguments.length;
var cardnumber = (argc > 0) ? argv[0] : this.cardnumber;
if (!this.luhnCheck())
return false;

for (var n = 0; n < this.len.size; n++)
if (cardnumber.toString().length == this.len[n]) {
for (var m = 0; m < this.rules.size; m++) {
var headdigit = cardnumber.substring(0, this.rules[m].toString().length);
if (headdigit == this.rules[m])
return true;
}
return false;
}
return false;
}

/*************************************************************************\
boolean isExpiryDate([int year, int month])
return true if the date is a valid expiry date,
else return false.
\*************************************************************************/
function isExpiryDate() {
var argv = isExpiryDate.arguments;
var argc = isExpiryDate.arguments.length;

year = argc > 0 ? argv[0] : this.year;
month = argc > 1 ? argv[1] : this.month;

if (!isNum(year+""))
return false;
if (!isNum(month+""))
return false;
today = new Date();
expiry = new Date(year, month);
if (today.getTime() > expiry.getTime())
return false;
else
return true;
}

/*************************************************************************\
boolean isNum(String argvalue)
return true if argvalue contains only numeric characters,
else return false.
\*************************************************************************/
function isNum(argvalue) {
argvalue = argvalue.toString();

if (argvalue.length == 0)
return false;

for (var n = 0; n < argvalue.length; n++)
if (argvalue.substring(n, n+1) < "0" || argvalue.substring(n, n+1) > "9")
return false;

return true;
}

/*************************************************************************\
boolean luhnCheck([String CardNumber])
return true if CardNumber pass the luhn check else return false.
Reference: http://www.ling.nwu.edu/~sburke/pub/luhn_lib.pl
\*************************************************************************/
function luhnCheck() {
var argv = luhnCheck.arguments;
var argc = luhnCheck.arguments.length;

var CardNumber = argc > 0 ? argv[0] : this.cardnumber;

if (! isNum(CardNumber)) {
return false;
  }

var no_digit = CardNumber.length;
var oddoeven = no_digit & 1;
var sum = 0;

for (var count = 0; count < no_digit; count++) {
var digit = parseInt(CardNumber.charAt(count));
if (!((count & 1) ^ oddoeven)) {
digit *= 2;
if (digit > 9)
digit -= 9;
}
sum += digit;
}
if (sum % 10 == 0)
return true;
else
return false;
}

/*************************************************************************\
ArrayObject makeArray(int size)
return the array object in the size specified.
\*************************************************************************/
function makeArray(size) {
this.size = size;
return this;
}

/*************************************************************************\
CardType setCardNumber(cardnumber)
return the CardType object.
\*************************************************************************/
function setCardNumber(cardnumber) {
this.cardnumber = cardnumber;
return this;
}

/*************************************************************************\
CardType setCardType(cardtype)
return the CardType object.
\*************************************************************************/
function setCardType(cardtype) {
this.cardtype = cardtype;
return this;
}

/*************************************************************************\
CardType setExpiryDate(year, month)
return the CardType object.
\*************************************************************************/
function setExpiryDate(year, month) {
this.year = year;
this.month = month;
return this;
}

/*************************************************************************\
CardType setLen(len)
return the CardType object.
\*************************************************************************/
function setLen(len) {
// Create the len array.
if (len.length == 0 || len == null)
len = "13,14,15,16,19";

var tmplen = len;
n = 1;
while (tmplen.indexOf(",") != -1) {
tmplen = tmplen.substring(tmplen.indexOf(",") + 1, tmplen.length);
n++;
}
this.len = new makeArray(n);
n = 0;
while (len.indexOf(",") != -1) {
var tmpstr = len.substring(0, len.indexOf(","));
this.len[n] = tmpstr;
len = len.substring(len.indexOf(",") + 1, len.length);
n++;
}
this.len[n] = len;
return this;
}

/*************************************************************************\
CardType setRules()
return the CardType object.
\*************************************************************************/
function setRules(rules) {
// Create the rules array.
if (rules.length == 0 || rules == null)
rules = "0,1,2,3,4,5,6,7,8,9";

var tmprules = rules;
n = 1;
while (tmprules.indexOf(",") != -1) {
tmprules = tmprules.substring(tmprules.indexOf(",") + 1, tmprules.length);
n++;
}
this.rules = new makeArray(n);
n = 0;
while (rules.indexOf(",") != -1) {
var tmpstr = rules.substring(0, rules.indexOf(","));
this.rules[n] = tmpstr;
rules = rules.substring(rules.indexOf(",") + 1, rules.length);
n++;
}
this.rules[n] = rules;
return this;
}

function trim(str){

	str = str.replace(/(^\s+|\s+$)/,"");

	return str;
}

function zipOK(zip){
	var Error = "";
	var myReg = /(^\d{5}$|^\d{5}-\d{4}$)/;

	if (!zip.match(myReg)){
	    Error = "US customers must provide a valid zip code.\n";
	}

	return Error;
}

function phoneOK(phone,num){
	var Error = "";
	var myReg = /^[+]*[0-9]*[ .-]*[(]*[0-9]*[0-9]*[0-9]*[)]*[-. ]*[0-9]*[0-9]*[0-9]*[ .-]*[0-9]*[0-9]*[0-9]*[0-9]*$/;

	if (!phone.match(myReg)){
	    Error = "The " + num  + " you entered does not appear to be valid. Be sure not to use letters in your number.\n";
	}

	return Error;
}

function emailCheck (emailStr) {
/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address.
   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	//alert("Email address seems incorrect (check @ and .'s)")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid
if (user.match(userPat)==null) {
    // user is not valid
    //alert("The username doesn't seem to be valid.")
    return false
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        //alert("Destination IP address is invalid!")
		return false
	    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	//alert("The domain name doesn't seem to be valid.")
    return false
}

/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 ||
    domArr[domArr.length-1].length>4) {
   // the address must end in a two letter or three letter word.
   //alert("The address must end in a three-letter domain, or two letter country.")
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="This address is missing a hostname!"
   //alert(errStr)
   return false
}

// If we've gotten this far, everything's valid!
return true;
}

function emailOK(email){
	var Error = "";

	if (!emailCheck (email)){
	    Error = "The email address you entered does not appear to be valid.\n";
	}

	return Error;
}

function Domain(domain){
	Error = false;
	var myReg1 = /^[0-9a-zA-Z-]+$/;
	var myReg2 = /^-/;
	var myReg3 = /-$/;
	var myReg4 = /-{2}/;

	if (!domain.match(myReg1) ||
	   domain.match(myReg2) ||
	   domain.match(myReg3) ||
	   domain.match(myReg4)){
		Error = "You appear to have entered an invalid domain name. Please try again.\n";
	}

	return Error;
}

function Contact(form){
	Error = "";

	var fields = new Array("txtFirstName","txtLastName","txtEmailAddress",
					"txtWorkNumber","txtCompany","txtAddress1","txtCity","txtMailRegion","cboCountry",
					"txtMailCode");

	var labels =  new Array();
	labels["txtFirstName"]="First Name";
	labels["txtLastName"]="Last Name";
	labels["txtEmailAddress"]="Email Address";
	labels["txtWorkNumber"]="Phone Number";
	labels["txtCompany"]="Company Name";
	labels["txtAddress1"]="Address";
	labels["txtCity"]="City";
	labels["txtMailRegion"]="State/Province";
	labels["cboCountry"]="Country";
	labels["txtMailCode"]="Postal Code";


	if (form.txtEmailAddress.value != ""){
		Error += emailOK(form.txtEmailAddress.value);
	}

	if (form.txtWorkNumber.value != ""){
		Error += phoneOK(form.txtWorkNumber.value,"phone");
	}


	var count = fields.length;
	var zip = form.txtMailCode.value;
	var countryindex = form.cboCountry.selectedIndex;
	var country = form.cboCountry[countryindex].value;

	for (var x = 0; x < count; x++){
		var value = form.fields[x].value
		if (trim(value) == ""){
			label = labels[value];
			Error += "You must provide your " + label + ".\n";
		}
	}

	if ((country == "United States") &&
	    (zip != "")){
	    Error += zipOK(zip);
	}
	return Error;
}

function Order(form){
	Error = "";

	var fields = new Array("Cont_Password","OLD_Cont_Password","Cont_FirstName","Cont_LastName","Cont_EmailAddress",
					"Cont_WorkNumber","Cont_AddressLine1","Cont_City","Cont_MailRegion","Cont_Country",
					"Cont_MailCode","txtCreditCardName");

	var labels =  new Array();
	labels["Cont_Password"]="Password 1";
	labels["OLD_Cont_Password"]="Password 2";
	labels["Cont_FirstName"]="First Name";
	labels["Cont_LastName"]="Last Name";
	labels["Cont_EmailAddress"]="Email Address";
	labels["Cont_WorkNumber"]="Phone Number";
	labels["Cont_AddressLine1"]="Address";
	labels["Cont_City"]="City";
	labels["Cont_MailRegion"]="State/Province";
	labels["Cont_Country"]="Country";
	labels["Cont_MailCode"]="Postal Code";
	labels["txtCreditCardName"]="Name of Credit Card";

	var fields2 = new Array ("Cont2_FirstName","Cont2_LastName","Cont2_EmailAddress","Cont2_WorkNumber",
					"Cont2_AddressLine1","Cont2_City","Cont2_MailRegion","Cont2_Country","Cont2_MailCode");

	// billing contacts
	labels["Cont2_FirstName"]="Billing First Name";
	labels["Cont2_LastName"]="Billing Last Name";
	labels["Cont2_EmailAddress"]="Email Address";
	labels["Cont2_WorkNumber"]="Billing Phone Number";
	labels["Cont2_AddressLine1"]="Billing Address";
	labels["Cont2_City"]="Billing City";
	labels["Cont2_MailRegion"]="Billing State/Province";
	labels["Cont2_Country"]="Billing Country";
	labels["Cont2_MailCode"]="Billing Postal Code";

	labels["txtCreditCardName"]="Name on Credit Card";
	labels["cboCreditCardType"]="";
	labels["txtCreditCardNumber"]="Credit Card Number";
	labels["txtExpirationDate1"]=""; //month
	labels["txtExpirationDate2"]=""; //year
	labels["iagree"]="Agree to Terms and Conditions";

	if ((form.Cont_Password.value.length < 6) || (form.Cont_Password.value.length > 12)){
		Error += "Your password must be between 6 and 12 characters.\n";
	}

	if (form.Cont_Password.value != form.OLD_Cont_Password.value){
		Error += "Your password and confirmed password do not match.\n";
	}

	if (!form.iagree.checked){
		Error += "You must agree to our Terms and Conditions to continue.\n";
	}

	if (form.Cont_EmailAddress.value != ""){
		Error += emailOK(form.Cont_EmailAddress.value);
	}

	if (form.Cont_WorkNumber.value != ""){
		Error += phoneOK(form.Cont_WorkNumber.value,"phone");
	}

	var count = fields.length;
	var zip = form["Cont_MailCode"].value;
	var countryindex = form.Cont_Country.selectedIndex;
	var country = form.Cont_Country[countryindex].value;

	for (var x = 0; x < count; x++){
		var index = fields[x];
		var field_value = form[index].value;

		if (trim(field_value) == ""){
			var label = labels[index];
			Error += "You must provide your " + label + ".\n";
		}
	}

	if ((country == "United States") &&
	    (zip != "")){
	    Error += zipOK(zip);
	}

	if (form.billing_same){
		if (!form.billing_same.checked){
			var zip = form["Cont2_MailCode"].value;
			var countryindex = form.Cont2_Country.selectedIndex;
			var country = form.Cont2_Country[countryindex].value;
			var count = fields2.length;

			for (var x = 0; x < count; x++){
				var index = fields2[x];
				var field_value = form[index].value;

				if (trim(field_value) == ""){
					var label = labels[index];
					Error += "You must provide your " + label + ".\n";
				}
			}

			if ((country == "United States") &&
			    (zip != "")){
			    Error += zipOK(zip);
			}

			if (form.Cont2_EmailAddress.value != ""){
				Error += emailOK(form.Cont2_EmailAddress.value);
			}

			if (form.Cont2_WorkNumber.value != ""){
				Error += phoneOK(form.Cont2_WorkNumber.value,"phone");
			}
		}
	}
	return Error;
}

function Add_Email(form){
	var email = form.Email_Name.value;
	var Error = "";

	email = email + "@domain.com";

	Error += emailOK(email);
	var password = trim(form.PassWord.value);
	if ((password.length < 6) || (password.length > 12)){
		Error += "Your password must be between 6 and 12 characters.\n";
	}

	if (form.PassWord.value != form.ConfPassWord.value){
		Error += "Your password and confirmed password do not match.\n";
	}

	return Error;
}

function Email_Forwarding(form){

	var Error = "";
	var forward = trim(form.ForwardAdd.value);
	var name = trim(form.EmailName.value);

	Error += emailOK(forward);

	if (name == ""){
		Error += "You must provide a name for your email address.\n";
	}

	return Error;
}

function Autoresponder(form){

	var name = "none";

	if (form.emailname){
		name = trim(form.emailname.value);
	}
	var forward = trim(form.ForwardTo.value);
	var reply = trim(form.ReplyTo.value);
	var subject = trim(form.Subject.value);
	var body = trim(form.Body.value);
	var Error = "";

	Error += emailOK(reply);

	if (forward){
		Error += emailOK(forward);
	}

	if (name == ""){
		Error += "You must provide a name for your email address.\n";
	}

	if (subject.length > 80){
		Error += "Your subject cannot exceed 80 characters.\n";
	}

	if (body == ""){
		Error += "The body of your autoresponder appears empty.\n";
	}

	if (body.length > 1000){
		Error += "Your email body cannot exceed 1000 characters.\n";
	}

	return Error;
}

function CheckForm(form) {

	var Error = false;
	var mode = form.name;
	var Display = true;

	switch (mode){
		case "domain_ext" :
			domain = form.DOMAIN.value;
			domain = domain.split(".");
			if (domain.length == 2){
				domain = domain[0];
				Error = Domain(domain);
			}
			else{
				Error = "extension missing";
			}
			break;
		case "new_domain" :
			domain = form.DOMAIN.value;
			Error = Domain(domain);
			break;
		case "transfer_domain" :
			domain = form.DOMAIN.value;
			Error = Domain(domain);
			break;
		case "billing_form":
			CheckCardNumber(form);
			var Display = false;
			break;
		case "order":
				if (CheckCardNumber(form)){
				//if (1){
					Error = Order(form);
				}
				else{
					Error = true;
					Display = false;
				}
			break;
		case "contact":
			Error = Contact(form);
			break;
		case "add_email":
			Error = Add_Email(form);
			break;
		case "email_forwarding":
			Error = Email_Forwarding(form);
			break;
		case "autoresponder":
			Error = Autoresponder(form);
			break;
	}

	if ((Error == false) || (Error == "")) {
			return true;
		}
	else {
			if (Display){
				alert(Error);
			}
			return false;
		}
}
