function strcmp(s1, s2, len)
{
	var idx = 0;

	if ((!len) && ((len = s1.length) == 0))
		len = s2.len;

	if (s1.length < len)
		return -1;
	else if (s2.length < len)
		return 1;

	while(idx < len)
	{
		if (s1.charAt(idx) != s2.charAt(idx))
			return -1;
		idx++;
	}
	return 0;
}

function setFormField(field_name, field_value)
{
	if (field_value == null)
		return false;

	field_name.value = field_value;
	return true;
}

function elementSetValue(elem, value)
{
	elem.value = value;
	return true;
}

function elementGet(eid)
{
	return document.all ? document.all[eid] : document.getElementById(eid);
}

function setDivValue(divID, value)
{
	if ((div = elementGet(divID)) == null)
		return false;
	else
		div.innerHTML = value;
	return true;
}

function setFieldValue(fieldID, value)
{
	if ((field = elementGet(fieldID)) == null)
		return false;
	else
		elementSetValue(field, value);
	return true;
}

function elementSetVisibility(eid, vis)
{
	var elt = document.all ? document.all[eid] :
	document.getElementById(eid);

	if (!elt)
	{
		alert(eid + ': Not found to modify visibility.');
		return;
	}

	if (vis != 1) {
		elt.style.visibility = 'hidden';
		elt.visibility = 'hidden';
		elt.style.display = 'none';
	}
	else
	{
		elt.style.visibility = 'visible';
		elt.visibility = 'visible';
		elt.style.display = 'block';
	}
}

function radioListValue(eid)
{
	if (!eid)
		return null;

	for (i=0; (i < eid.length); i++)
	{
		if (eid[i].checked)
			break;
	}
	return (i == eid.length) ? null : eid[i].value;
}

// returns index of first char in string. string.length if none.
function firstChar(string, exceptionChars)
{
	for (var i = 0; i < string.length; i++)
	{
		var intCharCode = string.charCodeAt(i);
		if (((intCharCode < 48) || (intCharCode > 57))
		&& ((exceptionChars == null) || (exceptionChars.indexOf(string.charAt(i).toString()) == -1)))
			break;
	}
	return i;
}

function menuClick(form, menuItem)
{
	if ((setFormField(form.mi, menuItem) != true)
	|| (setFormField(form.origin, '') != true)
	|| (setFormField(form.page_num, '') != true)
	|| (setFormField(form.product_id, '') != true))
	{
		alert('form submit failed.');
		return false;
	}
	return true;
}

function checkCreditCard (cardnumber, cardname)
{
// Array to hold the permitted card characteristics
var cards = new Array();

// Define the cards we support. You may add addtional card types.
  
//  Name:      As in the selection box of the form - must be same as user's
//  Length:    List of possible valid lengths of the card number for the card
//  prefixes:  List of possible prefixes for the card
//  checkdigit Boolean to say whether there is a check digit
  
	cards [0] = {name: "Visa", 
		length: "13,16", 
		prefixes: "4",
		checkdigit: true};
	cards [1] = {name: "MasterCard", 
		length: "16", 
		prefixes: "51,52,53,54,55",
		checkdigit: true};
	cards [2] = {name: "DinersClub", 
		length: "14,16", 
		prefixes: "300,301,302,303,304,305,36,38,55",
		checkdigit: true};
	cards [3] = {name: "CarteBlanche", 
		length: "14", 
		prefixes: "300,301,302,303,304,305,36,38",
		checkdigit: true};
	cards [4] = {name: "AmEx", 
		length: "15", 
		prefixes: "34,37",
		checkdigit: true};
	cards [5] = {name: "Discover", 
		length: "16", 
		prefixes: "6011,650",
		checkdigit: true};
	cards [6] = {name: "JCB", 
		length: "15,16", 
		prefixes: "3,1800,2131",
		checkdigit: true};
	cards [7] = {name: "enRoute", 
		length: "15", 
		prefixes: "2014,2149",
		checkdigit: true};
	cards [8] = {name: "Solo", 
		length: "16,18,19", 
		prefixes: "6334, 6767",
		checkdigit: true};
	cards [9] = {name: "Switch", 
		length: "16,18,19", 
		prefixes: "4903,4905,4911,4936,564182,633110,6333,6759",
		checkdigit: true};
	cards [10] = {name: "Maestro", 
		length: "16", 
		prefixes: "5020,6",
		checkdigit: true};
	cards [11] = {name: "VisaElectron", 
		length: "16", 
		prefixes: "417500,4917,4913",
		checkdigit: true};
               
  // Establish card type
  var cardType = -1;
  for (var i=0; i<cards.length; i++) {

    // See if it is this card (ignoring the case of the string)
    if (cardname.toLowerCase () == cards[i].name.toLowerCase()) {
      cardType = i;
      break;
    }
  }
  
  // If card type not found, report an error
  if (cardType == -1) {
     ccErrorNo = 0;
     return false; 
  }
   
  // Ensure that the user has provided a credit card number
  if (cardnumber.length == 0)  {
     ccErrorNo = 1;
     return false; 
  }
    
  // Now remove any spaces from the credit card number
  cardnumber = cardnumber.replace (/\s/g, "");
  
  // Check that the number is numeric
  var cardNo = cardnumber
  var cardexp = /^[0-9]{13,19}$/;
  if (!cardexp.exec(cardNo))  {
     ccErrorNo = 2;
     return false; 
  }
       
  // Now check the modulus 10 check digit - if required
  if (cards[cardType].checkdigit) {
    var checksum = 0;                                  // running checksum total
    var mychar = "";                                   // next char to process
    var j = 1;                                         // takes value of 1 or 2
  
    // Process each digit one by one starting at the right
    var calc;
    for (i = cardNo.length - 1; i >= 0; i--) {
    
      // Extract the next digit and multiply by 1 or 2 on alternative digits.
      calc = Number(cardNo.charAt(i)) * j;
    
      // If the result is in two digits add 1 to the checksum total
      if (calc > 9) {
        checksum = checksum + 1;
        calc = calc - 10;
      }
    
      // Add the units element to the checksum total
      checksum = checksum + calc;
    
      // Switch the value of j
      if (j ==1) {j = 2} else {j = 1};
    } 
  
    // All done - if checksum is divisible by 10, it is a valid modulus 10.
    // If not, report an error.
    if (checksum % 10 != 0)  {
     ccErrorNo = 3;
     return false; 
    }
  }  

  // The following are the card-specific checks we undertake.
  var LengthValid = false;
  var PrefixValid = false; 
  var undefined; 

  // We use these for holding the valid lengths and prefixes of a card type
  var prefix = new Array ();
  var lengths = new Array ();
    
  // Load an array with the valid prefixes for this card
  prefix = cards[cardType].prefixes.split(",");
      
  // Now see if any of them match what we have in the card number
  for (i=0; i<prefix.length; i++) {
    var exp = new RegExp ("^" + prefix[i]);
    if (exp.test (cardNo)) PrefixValid = true;
  }
      
  // If it isn't a valid prefix there's no point at looking at the length
  if (!PrefixValid) {
     ccErrorNo = 3;
     return false; 
  }
    
  // See if the length is valid for this card
  lengths = cards[cardType].length.split(",");
  for (j=0; j<lengths.length; j++) {
    if (cardNo.length == lengths[j]) LengthValid = true;
  }
  
  // See if all is OK by seeing if the length was valid. We only check the 
  // length if all else was hunky dory.
  if (!LengthValid) {
     ccErrorNo = 4;
     return false; 
  };   
  
  // The credit card is in the required format.
  return true;
}

function ccardDefinition(ctype)
{
	// Array to hold the permitted card characteristics
	var cards = new Array();

	// Define the cards we support. You may add addtional card types.
  
	//  Name:      As in the selection box of the form - must be same as user's
	//  Length:    List of possible valid lengths of the card number for the card
	//  prefixes:  List of possible prefixes for the card
	//  checkdigit Boolean to say whether there is a check digit
  
	cards [0] = {name: "Visa", 
		length: "13,16", 
		prefixes: "4",
		checkdigit: true};
	cards [1] = {name: "MasterCard", 
		length: "16", 
		prefixes: "51,52,53,54,55",
		checkdigit: true};
	cards [2] = {name: "DinersClub", 
		length: "14,16", 
		prefixes: "300,301,302,303,304,305,36,38,55",
		checkdigit: true};
	cards [3] = {name: "CarteBlanche", 
		length: "14", 
		prefixes: "300,301,302,303,304,305,36,38",
		checkdigit: true};
	cards [4] = {name: "AmEx", 
		length: "15", 
		prefixes: "34,37",
		checkdigit: true};
	cards [5] = {name: "Discover", 
		length: "16", 
		prefixes: "6011,650",
		checkdigit: true};
	cards [6] = {name: "JCB", 
		length: "15,16", 
		prefixes: "3,1800,2131",
		checkdigit: true};
	cards [7] = {name: "enRoute", 
		length: "15", 
		prefixes: "2014,2149",
		checkdigit: true};
	cards [8] = {name: "Solo", 
		length: "16,18,19", 
		prefixes: "6334, 6767",
		checkdigit: true};
	cards [9] = {name: "Switch", 
		length: "16,18,19", 
		prefixes: "4903,4905,4911,4936,564182,633110,6333,6759",
		checkdigit: true};
	cards [10] = {name: "Maestro", 
		length: "16", 
		prefixes: "5020,6",
		checkdigit: true};
	cards [11] = {name: "VisaElectron", 
		length: "16", 
		prefixes: "417500,4917,4913",
		checkdigit: true};
               
	// Establish card type
	var cardType = -1;
	for (var i=0; (i<cards.length); i++)
	{
		// See if it is this card (ignoring the case of the string)
		if (ctype.toLowerCase () == cards[i].name.toLowerCase())
		{
			cardType = i;
			break;
		}
	}
	if (i == cards.length)
		return null;
	return cards[i];
}

function ccardDefinitionCheck(ccnumber, ccdef)
{
	var LengthValid = false;
	var PrefixValid = false; 
	var undefined; 

	// We use these for holding the valid lengths and prefixes of a card type
	var prefix = new Array ();
	var lengths = new Array ();
    
	// Load an array with the valid prefixes for this card
	prefix = ccdef.prefixes.split(",");
      
	// Now see if any of them match what we have in the card number
	for (i=0; (i < prefix.length); i++)
	{
		var exp = new RegExp("^" + prefix[i]);
		if (exp.test(cardNo))
			PrefixValid = true;
	}
      
	// If it isn't a valid prefix there's no point at looking at the length
	if (!PrefixValid)
		return 4; 
    
	// See if the length is valid for this card
	lengths = ccdef.length.split(",");
	for (j=0; (j < lengths.length); j++)
	{
		if (ccnumber.length == lengths[j])
			LengthValid = true;
	}
  
	// See if all is OK by seeing if the length was valid. We only check the 
	// length if all else was hunky dory.
	if (!LengthValid)
		return 5;

	return 0;
}

// GETTING the credit card definition is meant to happen with a call to ccardDefinition().
// Then it's validation is emant to happe with ccardDefinitionCheck(). However, i know not how to pass custom structures.
// No doubt it's easy but i no know.
function ccardValidate(ccnumber, cctype)
{
	if (ccnumber.length == 0)
		return 2;

	// Get the credit card definition.
	// Array to hold the permitted card characteristics
	var cards = new Array();

	// Define the cards we support. You may add addtional card types.
  
	//  Name:      As in the selection box of the form - must be same as user's
	//  Length:    List of possible valid lengths of the card number for the card
	//  prefixes:  List of possible prefixes for the card
	//  checkdigit Boolean to say whether there is a check digit
  
	cards [0] = {name: "Visa", 
		length: "13,16", 
		prefixes: "4",
		checkdigit: true};
	cards [1] = {name: "MasterCard", 
		length: "16", 
		prefixes: "51,52,53,54,55",
		checkdigit: true};
	cards [2] = {name: "DinersClub", 
		length: "14,16", 
		prefixes: "300,301,302,303,304,305,36,38,55",
		checkdigit: true};
	cards [3] = {name: "CarteBlanche", 
		length: "14", 
		prefixes: "300,301,302,303,304,305,36,38",
		checkdigit: true};
	cards [4] = {name: "AmEx", 
		length: "15", 
		prefixes: "34,37",
		checkdigit: true};
	cards [5] = {name: "Discover", 
		length: "16", 
		prefixes: "6011,650",
		checkdigit: true};
	cards [6] = {name: "JCB", 
		length: "15,16", 
		prefixes: "3,1800,2131",
		checkdigit: true};
	cards [7] = {name: "enRoute", 
		length: "15", 
		prefixes: "2014,2149",
		checkdigit: true};
	cards [8] = {name: "Solo", 
		length: "16,18,19", 
		prefixes: "6334, 6767",
		checkdigit: true};
	cards [9] = {name: "Switch", 
		length: "16,18,19", 
		prefixes: "4903,4905,4911,4936,564182,633110,6333,6759",
		checkdigit: true};
	cards [10] = {name: "Maestro", 
		length: "16", 
		prefixes: "5020,6",
		checkdigit: true};
	cards [11] = {name: "VisaElectron", 
		length: "16", 
		prefixes: "417500,4917,4913",
		checkdigit: true};
               
	// Establish card type
	for (var i = 0; (i < cards.length); i++)
	{
		// See if it is this card (ignoring the case of the string)
		if (cctype.toLowerCase () == cards[i].name.toLowerCase())
			break;
	}
	if (i == cards.length)
		return 1;
	ccdef = cards[i];

	// Now remove any spaces from the credit card number
	ccnumber = ccnumber.replace (/\s/g, "");
  
	// Check that the number is numeric
	var cardNo = ccnumber
	var cardexp = /^[0-9]{13,19}$/;
	if (!cardexp.exec(cardNo))
	{
		return 3;
	}
       
	// Now check the modulus 10 check digit - if required
	if (ccdef.checkdigit)
	{
		var checksum = 0;                                  // running checksum total
		var mychar = "";                                   // next char to process
		var j = 1;                                         // takes value of 1 or 2
  
		// Process each digit one by one starting at the right
		var calc;
		for (i = cardNo.length - 1; (i >= 0); i--)
		{
			// Extract the next digit and multiply by 1 or 2 on alternative digits.
			calc = Number(cardNo.charAt(i)) * j;
    
			// If the result is in two digits add 1 to the checksum total
			if (calc > 9)
			{
				checksum = checksum + 1;
				calc = calc - 10;
			}
    
			// Add the units element to the checksum total
			checksum = checksum + calc;
    
			// Switch the value of j
			if (j ==1) {j = 2} else {j = 1};
		} 
  
		// All done - if checksum is divisible by 10, it is a valid modulus 10.
		// If not, report an error.
		if (checksum % 10 != 0)
		{
			return 4;
		}
	}

	// Check the card-specific checks we undertake.
	var LengthValid = false;
	var PrefixValid = false; 
	var undefined; 

	// We use these for holding the valid lengths and prefixes of a card type
	var prefix = new Array ();
	var lengths = new Array ();
    
	// Load an array with the valid prefixes for this card
	prefix = ccdef.prefixes.split(",");
      
	// Now see if any of them match what we have in the card number
	for (i=0; (i < prefix.length); i++)
	{
		var exp = new RegExp("^" + prefix[i]);
		if (exp.test(cardNo))
			PrefixValid = true;
	}
      
	// If it isn't a valid prefix there's no point at looking at the length
	if (!PrefixValid)
		return 4; 
    
	// See if the length is valid for this card
	lengths = ccdef.length.split(",");
	for (j=0; (j < lengths.length); j++)
	{
		if (ccnumber.length == lengths[j])
			LengthValid = true;
	}
  
	// See if all is OK by seeing if the length was valid. We only check the 
	// length if all else was hunky dory.
	if (!LengthValid)
		return 5;

	return 0;
}

function ccErrorString(errid)
{
	if (errid == 1)
		return "Unknown card type";
	else if (errid == 2)
		return "No card number provided";
	else if (errid == 3)
		return "Credit card number is in invalid format. Please enter only digits.";
	else if (errid == 4)
		return "Credit card number is invalid";
	else if (errid == 5)
		return "Credit card number has an inappropriate number of digits";
	else
		return "Bad error code.";
}
