var defaultEmptyOK = false;
var decimalPointDelimiter = ".";
var whitespace = " \t\n\r";


function CheckCreditCard(form)
//returns 1 if ok
{
  var card_no, ok;
  
  ok = true;
  
  card_no = form.card_no.value;
  card_no = card_no.replace(/\s/, "");
  card_no = card_no.replace(/\s/, "");
  card_no = card_no.replace(/\s/, "");
  card_no = card_no.replace(/\s/, "");

  if(form.card.options[form.card.selectedIndex].value == "Visa")
  {
    if(!isVisa(card_no))
      ok = false;
  }
  else if(form.card.options[form.card.selectedIndex].value == "MasterCard")
  {
    if(!isMasterCard(card_no))
      ok = false;
  }
  else if(form.card.options[form.card.selectedIndex].value == "American Express")
  {
    if(!isAmericanExpress(card_no))
      ok = false;
  }
   
  if(!ok)
  {
    if(!confirm('The credit card number you have entered does not appear to match Vanbar\'s credit card verification formula.\nIf you are sure it is correct, we will verify the number when we receive your order.\n\nDo you wish to proceed?'))
      form.card_no.focus();
    else
    {
      card_no += ' *';
      ok = true;
    }
  }
      
  return ok;
}



function GetDollarType(form)
{
  var dollar_type;
   
  if(form.shipping_country.selectedIndex != '0')
  {
    if(form.shipping_country.options[form.shipping_country.selectedIndex].text == 'Australia')
      dollar_type = '0';
    else
      dollar_type = '1';
  }
  else if(form.billing_country.options[form.billing_country.selectedIndex].value != '0')
  {
    if(form.billing_country.options[form.billing_country.selectedIndex].text == 'Australia')
      dollar_type = '0';
    else
      dollar_type = '1';
  }
  else
    dollar_type = '0';
    
  return dollar_type;
}


function FloatValue(str)
{
  var value;

  if(IsFloat(str))
    value = parseFloat(str);
  else
    value = 0;

  return value;
}


function FormatDecimalPlaces(num, places)
{
  var i, pos, str, zeros_to_add;
    
  if(FloatValue(num) != 0)
  {
    str = num * Math.pow(10, places);
	  str = Math.ceil(str);
    str /= Math.pow(10, places);

    str = str.toString();
    str = str.replace(/,/, "");
    pos = str.search(/\./);
    
	  if(pos != -1)
	  {
	    zeros_to_add = places - (str.length - (pos + 1));

	    for(i = 0; i < zeros_to_add; i++)
	      str += "0";
	  }
	  else
	    str += ".00";
  }
  else
    str = '0.00';

  return str;
}


function IsDigit (c)
{   
  return ((c >= "0") && (c <= "9"));
}


function isEmail(s)
{   
  if(IsEmpty(s)) 
  {
    if(isEmail.arguments.length == 1) 
      return defaultEmptyOK;
   else 
     return (isEmail.arguments[1] == true);
  }
   
  // is s whitespace?
  if(isWhitespace(s)) 
    return false;
    
  // there must be >= 1 character before @, so we
  // start looking at character position 1 
  // (i.e. second character)
  var i = 1;
  var sLength = s.length;

  // look for @
  while((i < sLength) && (s.charAt(i) != "@"))
  { 
    i++
  }

  if((i >= sLength) || (s.charAt(i) != "@")) 
    return false;
  else 
    i += 2;

  // look for .
  while((i < sLength) && (s.charAt(i) != "."))
  { 
    i++
  }

  // there must be at least one character after the .
  if((i >= sLength - 1) || (s.charAt(i) != ".")) 
    return false;
  else 
    return true;
}


function IsEmpty(s)
{   
  return ((s == null) || (s.length == 0))
}


function IsFloat (s)
// True if string s is an signed floating point (real) number. 
{   
  var i;
  var seenDecimalPoint = false;

  if(IsEmpty(s)) 
  {
    if (IsFloat.arguments.length == 1) 
      return defaultEmptyOK;
    else 
      return (IsFloat.arguments[1] == true);
  }

  if(s == decimalPointDelimiter) 
    return false;

  // Search through string's characters one by one
  // until we find a non-numeric character.
  // When we do, return false; if we don't, return true.

  for (i = 0; i < s.length; i++)
  {   
    // Check that current character is number.
    var c = s.charAt(i);

    if ((c == decimalPointDelimiter) && !seenDecimalPoint) 
      seenDecimalPoint = true;

    else if(c == '-' && i == 0);
    //the first character is a negative, keep going

    else if (!IsDigit(c)) 
      return false;
  }

  // All characters are numbers.
  return true;
}


function isAmericanExpress(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 15) && (firstdig == 3) &&
      ((seconddig == 4) || (seconddig == 7)))
    return isCreditCard(cc);
  return false;
}

function isCreditCard(st) 
{
  // Encoding only works on cards with less than 19 digits
  if (st.length > 19)
    return (false);

  sum = 0; mul = 1; l = st.length;
  for (i = 0; i < l; i++) {
    digit = st.substring(l-i-1,l-i);
    tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
// Uncomment the following line to help create credit card numbers
// 1. Create a dummy number with a 0 as the last digit
// 2. Examine the sum written out
// 3. Replace the last digit with the difference between the sum and
//    the next multiple of 10.

//  document.writeln("<BR>Sum      = ",sum,"<BR>");
//  alert("Sum      = " + sum);

  if ((sum % 10) == 0)
    return (true);
  else
    return (false);
}


function isMasterCard(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 16) && (firstdig == 5) &&
      ((seconddig >= 1) && (seconddig <= 5)))
    return isCreditCard(cc);
  return false;
}


function isVisa(cc)
{
  if (((cc.length == 16) || (cc.length == 13)) &&
      (cc.substring(0,1) == 4))
    return isCreditCard(cc);
  return false;
}


function isWhitespace(s)
{   
  var i;

  // Is s empty?
  if (IsEmpty(s)) 
    return true;

  // Search through string's characters one by one
  // until we find a non-whitespace character.
  // When we do, return false; if we don't, return true.

  for (i = 0; i < s.length; i++)
  {   
    // Check that current character isn't whitespace.
    var c = s.charAt(i);

    if (whitespace.indexOf(c) == -1) 
      return false;
  }

  // All characters are whitespace.
  return true;
}


function OnChangeQuantity(quantity, unit_price, total, total_gst, total_usd, form_elements, form, usd_conversion)
{
  var dollar_amount;
  var abs_total = 0;
  
  total.value = "$" + FormatDecimalPlaces(parseInt(quantity.value) * unit_price, 2);
  total_gst.value = "$" + FormatDecimalPlaces(parseInt(quantity.value) * unit_price * 1.1, 2);
  total_usd.value = "$" + FormatDecimalPlaces(parseInt(quantity.value) * unit_price * usd_conversion, 2);
  
  for(i = 0; i < form_elements.length; i++)
  {
    dollar_amount = form_elements[i].value.replace(/\$/, "");
    abs_total += parseFloat(dollar_amount);
  }
  
  form.total.value = "$" + FormatDecimalPlaces(abs_total, 2);
  form.total_gst.value = "$" + FormatDecimalPlaces(abs_total*1.1, 2);
  form.total_usd.value = "$" + FormatDecimalPlaces(abs_total*usd_conversion, 2);
}


function OnCloseSelect(self, form)
{
  self.close();
  
  opener.document.view_photos.submit();
}


function OnDeleteFromBasket(form, FK_AlbumBasket)
{
  if(confirm('Are you sure that you want to delete this item?'))
  {
    form.action = "basket.php?delete=" + FK_AlbumBasket;
    form.form_action.value = "delete";
    form.submit();
  }
}


function OnEnlargePhoto(id, width, height)
{
  var width, left, top1;
  
  width = 1.2*width + 50;  height = 1.2*height + 100;    left = (screen.width - width)/2;  top1 = (screen.height - height)/8;    //  window.open('enlarge.php?id=' + id, 'Album', 'width=' + width + ',height=' + height + ',alwaysLowered=1,alwaysRaised=0,channelmode=0,dependent=0,directories=0,fullscreen=0,hotkeys=1,location=0,menubar=0,resizable=0,scrollbars=0,status=0,titlebar=0,toolbar=0,left=' + left + ',top=' + top1 + ',z-lock=0');  window.open('enlarge.php?id=' + id, 'Album', 'width=' + width + ',height=' + height + ',alwaysLowered=1,alwaysRaised=0,channelmode=0,dependent=0,directories=0,fullscreen=0,hotkeys=1,location=0,menubar=0,resizable=1,scrollbars=1,status=0,titlebar=1,toolbar=0,left=' + left + ',top=' + top1 + ',z-lock=0');}


function OnNextPhotoScreen(form, start)
{
  form.start.value = start;
  form.submit();
}


function OnOrder(FK_AlbumPhoto, shopper_id)
{
  width=500;
  
  left = (screen.width-width)/2;
  window.open('selectphotos.php?shopper_id=' + shopper_id + '&FK_AlbumPhoto=' + FK_AlbumPhoto, 'win', 'width=' + width + ',alwaysLowered=1,alwaysRaised=0,channelmode=0,dependent=0,directories=0,fullscreen=0,hotkeys=1,location=0,menubar=0,resizable=0,scrollbars=1,status=0,titlebar=0,toolbar=0,left=' + left + ',z-lock=0');
}


function OnPassword(form)
{
  var ok;
  
  ok = true;
  
  if(form.password.value.length == 0)
  {
    alert('You must enter a password!');
    form.password.focus();
    ok = false;
  }
  
  if(ok)
    form.submit();
}


function OnSaveBasket(form)
{
  form.form_action.value = "save";
  form.submit();
}


function OnSubmitOrder(form)
{
  var ok;
  ok = true;
  
  if(ok && form.name.value.length == 0)
  {
    alert('You must enter a value for the name!');
    form.name.focus();
    ok = false;
  }
  
  else if(ok && !isEmail(form.email.value))
  {
    alert('You must enter a valid email address!');
    form.email.focus();
    ok = false;
  }
  
  else if(ok && form.billing_street.value.length == 0)
  {
    alert('You must enter a value for the billing street!');
    form.billing_street.focus();
    ok = false;
  }

  else if(ok && form.billing_city.value.length == 0)
  {
    alert('You must enter a value for the billing city!');
    form.billing_city.focus();
    ok = false;
  }

  else if(ok && form.billing_state.value.length == 0)
  {
    alert('You must enter a value for the billing state!');
    form.billing_state.focus();
    ok = false;
  }

  else if(ok && form.billing_postcode.value.length == 0)
  {
    alert('You must enter a value for the billing postcode!');
    form.billing_postcode.focus();
    ok = false;
  }

  else if(ok && form.billing_country.selectedIndex == 0)
  {
    alert('You must enter a value for the billing country!');
    form.billing_country.focus();
    ok = false;
  }
  
  else if(ok && form.paper_type.selectedIndex == 0)
  {
    alert('You must enter a paper type!');
    form.paper_type.focus();
    ok = false;
  }
    
  else if(ok && !CheckCreditCard(form))
  {
    form.card.focus();
    ok = false;
  }
  
  else if(ok && form.card_no.value.length == 0)
  {
    alert('You must enter a value for the credit card number!');
    form.card_no.focus();
    ok = false;
  }
  
   else if(ok && form.card_no_last_3.value.length == 0)
  {
    alert('You must enter a value for the credit card number\'s last 3 digits!');
    form.card_no_last_3.focus();
    ok = false;
  }
  
  else if(ok && form.expiration.value.length == 0)
  {
    alert('You must enter a value for the credit card expiration!');
    form.expiration.focus();
    ok = false;
  }
  
  else if(ok && form.card_name.value.length == 0)
  {
    alert('You must enter a value for the name on your credit card!');
    form.card_name.focus();
    ok = false;
  }
    
  
  if(ok)
  {
    form.dollar_type.value = GetDollarType(form);
    form.submit();
  }
  
}
