/*the base image directory location*/
var base = "images/";

/*********
switch an image with an ID
*********/
function openWindow(url,x,y,toolbar,scrollbars,resizable)
{
	new_x=x+20;
	new_y=y+20;
	var options = "toolbar=" + toolbar + ",scrollbars=" + scrollbars + ",resizable=" + resizable + ",width=" + new_x + ",height=" + new_y;
	newWindow=window.open(url,"WinOpen",options);
}

/*********
switch an image with an ID
*********/
function switchImage(thisId,thisImageSlice)
{
	//is the text "_off" inside this btn?
	if (document.getElementById(thisId).src.indexOf('_off.') < 0)
	{
		thisImg=base+thisImageSlice+thisId+'_off.gif';
	}
	else
	{
		thisImg=base+thisImageSlice+thisId+'_on.gif';
	}
	//set the image
	document.getElementById(thisId).src=thisImg;
}

/*********
expand code for both browsers
*********/
function expandIt(whichEl){
	var browser=navigator.userAgent.toLowerCase(); 
	var pos=browser.indexOf("gecko");
	var myElement = document.getElementById(whichEl);
	//do for Gecko Browsers
	if (pos>=0) {
		if (myElement.style.visibility == 'visible') {
			myElement.style.visibility = 'hidden';
			myElement.style.position = 'absolute';
			}
		else {
			myElement.style.position = 'relative';
			myElement.style.visibility = 'visible';
			}
	//do for IE
	} else {
		myElement.style.display = (myElement.style.display == "none" ) ? "" : "none";
	}
}

/*********
toggle the text inside a form on/off 
*********/
function toggleFormText(thisId, thisText)
{
	if (document.getElementById(thisId).value == thisText)
	{
		document.getElementById(thisId).value='';
	}
	else if (document.getElementById(thisId).value == '')
	{
		document.getElementById(thisId).value=thisText;
	}
}

/*********
validates an email address
*********/
function validateEmail(thisEmail)
{
	//initiate returnMessage variable
	var returnMessage="";
	
	if(thisEmail == '')
	{
		// alert the user
		returnMessage = "Please type in a valid email address.";
	}
	else
	{
		// function to check email address vilidity
		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);
			var checkMatch='-' + matchArray + '-';
			if (checkMatch == "-null-")
			{
			  /* Too many/few @'s or something; basically, this address doesn't
				 even fit the general mould of a valid e-mail address. */
				returnMessage = "The email address seems incorrect (check @ and .'s).";
			}
			else
			{
				var user=matchArray[1];
				var domain=matchArray[2];
				
				// See if "user" is valid 
				if (user.match(userPat)==null)
				{
					// user is not valid
					returnMessage = "The email's username doesn't seem to be valid (before the @).";
				}
				
				/* 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) {
							returnMessage = "The email's destination IP address is invalid.";
						}
					}
				}
			
				// Domain is symbolic name
				var domainArray=domain.match(domainPat)
				if (domainArray==null) {
					returnMessage = "The email's domain name doesn't seem to be valid (after the @).";
				}
				
				/* 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.
				   returnMessage = "The email must end in a four-letter domain, three-letter domain, or two letter country.";
				}
			
				// Make sure there's a host name preceding the domain.
				if (len < 2) {
				   returnMessage="This email is missing a hostname!"; 
				}
			}
		}
		// call the validation function and return its result
		val=emailCheck(thisEmail);
		// if it returns val=no_submit, stop form
	}
	return returnMessage;
}

/************
verify Contact Form
************/
function verifyContactForm()
{
	//form variables
	submitBtnMessage='Saving... Please Wait';
	formName='formContact'
	
	//initialize submitForm value
	submitForm=true;
	warningMessage="";
	formFocus="";

	//check name
	if (submitForm)
	{
		formElement="name";
		thisDOM=eval('document.' + formName + '.' + formElement);
		if (thisDOM.value == "")
		{
			warningMessage='Please type in your name.';
			formFocus=formElement;
			submitForm=false;
		}
	}
	
	//check the email
	if (submitForm)
	{
		formElement="email_address";
		thisDOM=eval('document.' + formName + '.' + formElement);
		//check the email address
		thisMessage=validateEmail(thisDOM.value);
		//if there is no message sent back, set the alert.
		if (thisMessage.length != 0)
		{
			warningMessage=thisMessage;
			formFocus=formElement;
			submitForm=false;
		}		
	}
	//check comment
	if (submitForm)
	{
		formElement="comments";
		thisDOM=eval('document.' + formName + '.' + formElement);
		if (thisDOM.value == "")
		{
			warningMessage='Please type in a comment.';
			formFocus=formElement;
			submitForm=false;
		}
	}
	
	//disable the form if the form checks out
	if (!submitForm)
	{
		alert(warningMessage);
	}

	//focus the form if necessary
	if (formFocus.length > 0)
	{
		thisDOM=eval('document.' + formName + '.' + formFocus);
		thisDOM.focus();
	}
	
	//return the boolean value whether or not we should submit this form
	return submitForm;
}

/************
verify Newsletter
************/
function verifyNewsletterSubscribe(checkboxCount)
{
	//form variables
	submitBtnMessage='Saving... Please Wait';
	formName='formNewsletterSubscribe'
	
	//initialize submitForm value
	submitForm=true;
	warningMessage="";
	formFocus="";

	//check name_first
	if (submitForm)
	{
		formElement="name_first";
		thisDOM=eval('document.' + formName + '.' + formElement);
		if (thisDOM.value == "")
		{
			warningMessage='Please type in your first name.';
			formFocus=formElement;
			submitForm=false;
		}
	}

	//check name
	if (submitForm)
	{
		formElement="name_last";
		thisDOM=eval('document.' + formName + '.' + formElement);
		if (thisDOM.value == "")
		{
			warningMessage='Please type in your last name.';
			formFocus=formElement;
			submitForm=false;
		}
	}
	
	//check the email
	if (submitForm)
	{
		formElement="email_address";
		thisDOM=eval('document.' + formName + '.' + formElement);
		//check the email address
		thisMessage=validateEmail(thisDOM.value);
		//if there is no message sent back, set the alert.
		if (thisMessage.length != 0)
		{
			warningMessage=thisMessage;
			formFocus=formElement;
			submitForm=false;
		}
	}

	//check for newsletter category checked
	if (submitForm)
	{
		newsletterSelected=false;
		formElement="newsletter_categories";
		thisDOM=eval('document.' + formName + '.' + formElement);
		// loop thru all the checkboxes in the page
		for (var i=1; i <= checkboxCount; i++)
		{
			if (document.getElementById('checkbox' + i).checked)
			{
				newsletterSelected=true;
			}
		}
		// no newsletter selected, don't submit the form
		if (!newsletterSelected)
		{
			warningMessage="Please select a newsletter to subscribe to.";
			submitForm=false;
		}
	}

	//disable the form if the form checks out
	if (!submitForm)
	{
		alert(warningMessage);
	}

	//focus the form if necessary
	if (formFocus.length > 0)
	{
		thisDOM=eval('document.' + formName + '.' + formFocus);
		thisDOM.focus();
	}
	
	//return the boolean value whether or not we should submit this form
	return submitForm;
}

/************
verify Send to a Friend
************/
function verifySendToAFriend()
{
	//form variables
	submitBtnMessage='Sending... Please Wait';
	formName='formSendToAFriend'
	
	//initialize submitForm value
	submitForm=true;
	warningMessage="";
	formFocus="";
	toEmailFound=false;

	//check friends_emails
	if (submitForm)
	{
		formElement="to_email_1";
		thisDOM=eval('document.' + formName + '.' + formElement);
		if (thisDOM.value == "")
		{
			warningMessage='Please type in an email address.';
			formFocus=formElement;
			submitForm=false;
		}
		// verify correctly formed email
		else
		{
			// set variable so next function knows a to_email has been found
			toEmailFound=true;
			//check the email address
			thisMessage=validateEmail(thisDOM.value);
			//if there is no message sent back, set the alert.
			if (thisMessage.length != 0)
			{
				warningMessage=thisMessage;
				formFocus=formElement;
				submitForm=false;
				toEmailFound=true;
			}
		}
	}
	
	//check the second email
	if (!toEmailFound)
	{
		formElement="to_email_2";
		thisDOM=eval('document.' + formName + '.' + formElement);
		if (thisDOM.value == "")
		{
			warningMessage='Please type in an email address.';
			formFocus="to_email_1";
			submitForm=false;
		}
		// verify correctly formed email
		else
		{
			// set variable so next function knows a to_email has been found
			toEmailFound=true;
			//check the email address
			thisMessage=validateEmail(thisDOM.value);
			//if there is no message sent back, set the alert.
			if (thisMessage.length != 0)
			{
				warningMessage=thisMessage;
				formFocus=formElement;
				submitForm=false;
			}
		}
	}

	//check the third email
	if (!toEmailFound)
	{
		formElement="to_email_3";
		thisDOM=eval('document.' + formName + '.' + formElement);
		if (thisDOM.value == "")
		{
			warningMessage='Please type in an email address.';
			formFocus="to_email_1";
			submitForm=false;
		}
		// verify correctly formed email
		else
		{
			//check the email address
			thisMessage=validateEmail(thisDOM.value);
			//if there is no message sent back, set the alert.
			if (thisMessage.length != 0)
			{
				warningMessage=thisMessage;
				formFocus=formElement;
				submitForm=false;
			}
		}
	}

	//check from name
	if (submitForm)
	{
		formElement="from_name";
		thisDOM=eval('document.' + formName + '.' + formElement);
		if (thisDOM.value == "")
		{
			warningMessage='Please type in your name.';
			formFocus=formElement;
			submitForm=false;
		}
	}
	
	//check the email
	if (submitForm)
	{
		formElement="from_email";
		thisDOM=eval('document.' + formName + '.' + formElement);
		//check the email address
		thisMessage=validateEmail(thisDOM.value);
		//if there is no message sent back, set the alert.
		if (thisMessage.length != 0)
		{
			warningMessage=thisMessage;
			formFocus=formElement;
			submitForm=false;
		}
	}

	//disable the form if the form checks out
	if (!submitForm)
	{
		alert(warningMessage);
	}

	//focus the form if necessary
	if (formFocus.length > 0)
	{
		thisDOM=eval('document.' + formName + '.' + formFocus);
		thisDOM.focus();
	}
	
	//return the boolean value whether or not we should submit this form
	return submitForm;
}

/************
verify Credit Card Form
************/
// form to make sure fields are filled in
function checkCardForm()
{

	// CREDIT CARD TYPE
		// loop through the type array to see if one is checked
		/*for (i = 0; i < document.one.type.length; i++)
		{
			if (document.one.type[i].checked)
			{
				// if one is checked, break loop
				break;					
			}				
			// otherwise, if this is the last loop, alert them and stop the form
			else if(i==document.one.type.length-1)
			{
				// alert the user
				alert("Please select a Credit Card Type to proceed.");
				// send back a value to stop form submission
				return false;
			}
		}*/
		// make sure the selectedIndex != 0
		if(document.one.CardType.selectedIndex==0)
		{
			// alert the user
			alert("Please select a Credit Card Type to proceed.");
			// focus field
			document.one.CardType.focus();
			// send back a value to stop form submission
			return false;
		}
	
	// CC EXPIRATION MONTH
		if(document.one.expiration_month.selectedIndex==0)
		{
			// alert the user
			alert("Please select an Expiration Month to proceed.");
			// focus field
			document.one.expiration_month.focus();
			// send back a value to stop form submission
			return false;
		}
		
	// CC EXPIRATION YEAR
		if(document.one.expiration_year.selectedIndex==0)
		{
			// alert the user
			alert("Please select an Expiration Year to proceed.");
			// focus field
			document.one.expiration_year.focus();
			// send back a value to stop form submission
			return false;
		}
	
	// CC NAME
		if(document.one.name.value=='')
		{
			// alert the user
			alert("Please input the Name on Card to proceed.");
			// focus field
			document.one.name.focus();
			// send back a value to stop form submission
			return false;
		}
		
	// CC NUMBER
		if(document.one.number.value=='')
		{
			// alert the user
			alert("Please input a valid Credit Card Number to proceed.");
			// focus field
			document.one.number.focus();
			// send back a value to stop form submission
			return false;
		}
		else
		{
			// function to check credit card number checksum
			// check this...
			/*function isValidCreditCard(number)
			{
				var total = 0;
				var flag = 0;
				for (var i=(number.length - 1);i>=0; i--)
				{
					if (flag == 1)
					{
						var digits = number.charAt(i) * 2;
						if (digits > 9) digits -= 9;
						total += digits;
						//	var reminder = digits % 10;
						//	var quotient = (digits - reminder) / 10;
						//	total = total + parseInt(reminder);
						//	total = total + parseInt(quotient);
						flag = 0;
					} 
					else
					{
						total = total + parseInt(number.charAt(i));
						flag = 1;
					}
				}
				if ((total%10) == 0)
				{
					// continue validation
					return "continue";
				}
				else
				{
					// alert user
					alert("Please enter a valid Credit Card Number.");
					// send string to stop form
					return "no_submit";
				}
			}*/
			// call the validation function and return its result
			//val=isValidCreditCard(document.one.number.value);
			val=CheckCardNumber(document.one);
			// if it returns val=no_submit, stop form
			if(val=="no_submit")
				return false;
			
		}

	
	// CC BILLING ADDRESS
		if(document.one.billing_address.value=='')
		{
			// alert the user
			alert("Please input the Credit Card Billing Address to proceed.");
			// focus field
			document.one.billing_address.focus();
			// send back a value to stop form submission
			return false;
		}
		
	// CC CITY
		if(document.one.city.value=='')
		{
			// alert the user
			alert("Please input the Billing Address City to proceed.");
			// focus field
			document.one.city.focus();
			// send back a value to stop form submission
			return false;
		}
	
	// CC STATE
		if(document.one.state.value=='')
		{
			// alert the user
			alert("Please input the Billing Address State to proceed.");
			// focus field
			document.one.state.focus();
			// send back a value to stop form submission
			return false;
		}
		
	// CC ZIP
	// make sure this checks isNumeric()
		if(document.one.zip.value=='')
		{
			// alert the user
			alert("Please input the Billing Address Zip/Postal Code to proceed.");
			// focus field
			document.one.zip.focus();
			// send back a value to stop form submission
			return false;
		}
		// zip code numeric test
		/*else if(IsNumeric(document.one.zip.value)==false)
		{
			
			alert("Your zip code contains non-numeric characters.\n" + 
			"[probably a dash '-' or parenthesis '\('\]\n" + 
			"Please include only NUMBERS in your zip code.\n\n" +
			"For example: 91016");
			document.one.zip.focus();
			document.one.zip.select();
			return false;
		}
		// if zip code is not equal to 5 characters
		else if(document.one.zip.value.length != 5)
		{
			alert("You have entered a(n) " + document.one.zip.value.length + " digit zip code.\n" + 
			"Please include only your 5 digit zip code.\n\n" + 
			"For example: 91016");
			document.one.zip.focus();
			return false;
		}*/

	// uncomment this to always stop form for debugging purposes
	// return false;
}
/****************************************************
// create isNumeric function to check for special chars
****************************************************/
function IsNumeric(sText)
{
	// acceptable characters
	var ValidChars = "0123456789";
	var IsNumber=true;
	var Char;	
	for (i = 0; i < sText.length && IsNumber == true; i++)
	{
		Char = sText.charAt(i);
		if (ValidChars.indexOf(Char) == -1)
		{
			IsNumber = false;
		}
	}
	return IsNumber;
}

/****************************************************
// create function to check for valid credit card
****************************************************/
/*Original:  Simon Tneoh (tneohcb@pc.jaring.my)
This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Begin*/
var Cards = new makeArray(8);
	Cards[0] = new CardType("MasterCard", "51,52,53,54,55", "16");
var MasterCard = Cards[0];
	Cards[1] = new CardType("VisaCard", "4", "13,16");
var VisaCard = Cards[1];
	Cards[2] = new CardType("AmExCard", "34,37", "15");
var AmExCard = Cards[2];
	Cards[3] = new CardType("DinersClubCard", "30,36,38", "14");
var DinersClubCard = Cards[3];
	Cards[4] = new CardType("DiscoverCard", "6011", "16");
var DiscoverCard = 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.number.value.length == 0) 
	{
		alert("Please enter a Card Number.");
		form.number.focus();
		return "no_submit";
	}
	if (form.expiration_year.value.length == 0) 
	{
		alert("Please enter the Expiration Year.");
		form.expiration_year.focus();
		return "no_submit";
	}
	if (form.expiration_year.value > 96)
		tmpyear = "19" + form.expiration_year.value;
	else if (form.expiration_year.value < 21)
		tmpyear = "20" + form.expiration_year.value;
	else
	{
		alert("The Expiration Year is not valid.");
		return "no_submit";
	}
	tmpmonth = form.expiration_month.options[form.expiration_month.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 "no_submit";
	}
	card = form.CardType.options[form.CardType.selectedIndex].value;
	var retval = eval(card + ".checkCardNumber(\"" + form.number.value +
	"\", " + tmpyear + ", " + tmpmonth + ");");
	cardname = "";
	if (retval)
		// comment this out if used on an order form
		//alert("This card number appears to be valid.");
		d=4;
	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.number.value, tmpyear, tmpmonth)) 
			{
				cardname = Cards[n].getCardType();
				break;
			}
		}

		if (cardname.length > 0)
		{
			alert("This looks like a " + cardname + " number, not a " + card + " number.");
			return "no_submit";
		}
		else
		{
			alert("This card number is not valid.");
			return "no_submit";
		}
   }
}


/*************************************************************************\
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;
}
