/*
	IN THIS JS FILE:
	
		functions changelink_switch,changeback_switch,changelink_blue,changeback_blue
			powered by blueswitch color changer functions
		
		function updateMixtures()
			javascript for working with the custom platter
			
		function validatePlatter()
			validates a custom platter by ensuring a platter is selected and has enough ingredients
			to fill it, but not overfill it
			
		function popUp(url)
			creates a pop-up window and navigates to the given url
	
*/

//powered by blueswitch style changer
blueSwitchOriginalColor = "black"
function changelink_switch(){
	document.all._switch.style.color='#FF9933'; 
	//document.all._switch.style.textDecoration='underline';
	document.all._blue.style.color='#00479C'; 
	//document.all._blue.style.textDecoration='underline';
}

function changeback_switch(){
	document.all._switch.style.color=blueSwitchOriginalColor; 
	//document.all._switch.style.textDecoration='none';
	document.all._blue.style.color=blueSwitchOriginalColor; 
	//document.all._blue.style.textDecoration='none';
}

	function changelink_blue(){
	document.all._blue.style.color='#00479C'; 
	//document.all._blue.style.textDecoration='underline';
	document.all._switch.style.color='#FF9933'; 
	//document.all._switch.style.textDecoration='underline';
}

function changeback_blue(){
	document.all._blue.style.color=blueSwitchOriginalColor; 
	//document.all._blue.style.textDecoration='none';
	document.all._switch.style.color=blueSwitchOriginalColor; 
	//document.all._switch.style.textDecoration='none';
}

//customize your own platter functions (2)
function updateMixtures(){
	var numMixtures	= document.productAdder.mixture.length;
	var numPlatters = document.productAdder.productid.length;
	var maxSections = document.productAdder.selectedMixture.length;
	var sections;
	var thisProduct, thisSections, thisProductName;
	var currSection = 0;
		
	//check to see which platter is selected from productid input radio fields
	//and populate the "platter" and hidden "sections"
	//also set sections variable
	
	for (i = 0; i < numPlatters; i++) {
		isProductSelected	= document.productAdder.productid[i].checked;
		thisSections		= document.productAdder.sections[i].value;
		thisProductName		= document.productAdder.productName[i].value;

		if (isProductSelected) {
			document.productAdder.platter.value				= thisProductName;
			document.productAdder.selectedSections.value	= thisSections;
			sections										= thisSections;
		}
	}
	
	//if a platter is not selected, display error message and unclick the checkbox by returning false
	if (sections == null) {
		alert('Please select a platter before selecting your mixtures.');
		return false;
	}
	
	//check to see if there are too many mixtures selected
	for (i = 0; i < numMixtures; i++) {
		thisMixture = document.productAdder.mixture[i];

		if(thisMixture.checked) {
			currSection++;
		}
	}
	
	for (i = 0; i < sections; i++) {
		document.productAdder.selectedMixture[i].style.display = 'inline';
	}

	for (i = sections; i < maxSections; i++) {
		document.productAdder.selectedMixture[i].style.display = 'none';
		document.productAdder.selectedMixture[i].value = '';
	}

	currSection = 0;
	
	//for each mixture option
	for (i = 0; i < numMixtures; i++) {
		thisMixture = document.productAdder.mixture[i];

		//if the mixture is checked and there are still selections left, add it
		if (thisMixture.checked && currSection < sections) {
			document.getElementsByName("mixturePicture")[i].style.display = "inline";
			document.productAdder.selectedMixture[currSection].value = thisMixture.value;
			currSection++;
		} else if (thisMixture.checked && currSection < sections) {
			thisMixture.checked = false;
			document.getElementsByName("mixturePicture")[i].style.display = 'none';
		//if the mixture is not checked hide it
		} else {
			thisMixture.checked = false;
			document.getElementsByName("mixturePicture")[i].style.display = 'none';
		} 
	}
	
	for (i = currSection; i < sections; i++) {
		document.productAdder.selectedMixture[i].value = '';
	}
	
	return true;
	
}
 

function popUp(url) {
	options = 'toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=600,height=400';
	sealWin = window.open(url,"win", options);
	self.name = "mainWin";
}

function determineAllVariants() {
	
	var allLevels
	var arrVariants
	var variantIDString = ''

	//get all levels into a string variable
	allLevels = document.getElementsByName("allLevels")[0].value

	//if all levels is empty, exit and return true to allow submission of productAdder
	if (allLevels.length == 0)
		return true;

	//split the levels into an array
	allLevels = allLevels.split(',');
	
	//for each size variant except the last (last element is from ending coma when creating the list)
	for (i=0;i <= allLevels.length - 1; i++) {
		arrVariants = document.getElementsByName("variant" + allLevels[i]);
		
		for (j=0; j < arrVariants.length; j++) {
			if (arrVariants[j].checked || arrVariants[j].name == "variantSize")
				variantIDString += arrVariants[j].value + ',';
				alert(arrVariants[j].name + ": " + arrVariants[j].value + "\n" + variantIDString);
		}

	}

	variantIDString = variantIDString.substr(0,variantIDString.length - 1);
	
	document.getElementsByName("allVariantIDs")[0].value = variantIDString;
	
	//alert(variantIDString);
	
	return false;
}


<!--
var Cards = new makeArray(8);
Cards[0] = new CardType("MasterCard", "51,52,53,54,55", "16");
var MasterCard = Cards[0];
Cards[1] = new CardType("Visa", "4", "13,16");
var Visa = Cards[1];
Cards[2] = new CardType("AmericanExpress", "34,37", "15");
var AmericanExpress = Cards[2];
Cards[3] = new CardType("DinersClubCard", "30,36,38", "14");
var DinersClubCard = Cards[3];
Cards[4] = new CardType("Discover", "6011", "16");
var Discover = 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 (document.forms.theform.card_num.value.length == 0) {
alert("Please enter a Card Number.");
form.card_num.focus();
return;
}
if (document.forms.theform.card_exp_y.value.length == 0) {
alert("Please enter the Expiration Year.");
form.card_exp_y.focus();
return;
}
tmpyear = document.forms.theform.card_exp_y.options[form.card_exp_y.selectedIndex].value;
tmpmonth = document.forms.theform.card_exp_m.options[form.card_exp_m.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(document.forms.theform.card_exp_y.value, tmpmonth)) {
alert("This card has already expired in " + document.forms.theform.card_exp_y.value + ".");
return;
}
card = document.forms.theform.card_type.options[document.forms.theform.card_type.selectedIndex].value;
var retval = eval(card + ".checkCardNumber(\"" + document.forms.theform.card_num.value +
"\", " + tmpyear + ", " + tmpmonth + ");");
cardname = "";
if (retval)
{
//document.theform.submit();

// 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(document.forms.theform.card_num.value, tmpyear, tmpmonth)) {
cardname = Cards[n].getCardType();
break;
   }
}
if (cardname.length > 0) {
alert("This looks like a " + cardname + " number, not a " + card + " number.");
}
else {
alert("This card number " + document.forms.theform.card_num.value + " expires " + tmpmonth + "/" + tmpyear + " is not valid.");
      }
   }
}
/*************************************************************************\
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;
}