var defaultEmptyOK = false

//mustHaveDetailsAccount = "Vanbar_account_number Vanbar_account_password"
mustHaveDetailsAccount = "Vanbar_account_number"

mustHaveDetailsCreditCard = "Visitor_name Visitor_email_address Billing_street_address" +
"Billing_city Billing_state Billing_postcode Billing_country Card_No Exp_Date Card_Name" +
"MySubmit"

mustHaveDetailsQuote = "quote_email_address"

// whitespace characters
var whitespace = " \t\n\r";


function CheckCreditCard(formObj)
//returns 1 if ok
{
  ok = true;

  if(formObj.Card.options[formObj.Card.selectedIndex].value == "Visa")
  {
    if(!isVisa(formObj.Card_No.value))
      ok = false;
  }
  else if(formObj.Card.options[formObj.Card.selectedIndex].value == "MasterCard")
  {
    if(!isMasterCard(formObj.Card_No.value))
      ok = false;
  }
  else if(formObj.Card.options[formObj.Card.selectedIndex].value == "American Express")
  {
    if(!isAmericanExpress(formObj.Card_No.value))
      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?'))
      formObj.Card_No.focus();
    else
    {
      formObj.Card_No.value += ' *';
      ok = true;
    }
  }
      
  return ok;
}


//displays an error if any fields in the form are null
function checkFormQuote(formObj)
{
  valid = true;
  formObj.form_type.value = "quote";
  formObj.quote_only.value = "On";

  if(!formObj.quote_only.checked)
  {
    alert("Please select the quote box if you want a quote");
    formObj.quote_only.focus();
    valid = false;
  }

  if(valid && !isEmail(formObj.quote_email_address.value))
  {
    alert("You must enter a valid email address");
    formObj.quote_email_address.focus();
    valid = false;
  }
  
  if(valid)
    formObj.submit();
}


function checkFormAccount(form, form_in)
{
  valid = true;
  
  if(!isEmail(form.account_email_address.value))
  {
    valid = false;
    alert("You must enter a valid email address!");
    form.account_email_address.focus();
  }
    
  if(valid && form.Vanbar_account_number.value.length == 0)
  {
    valid = false;
    alert("You must enter your account number!");
    form.Vanbar_account_number.focus();
  }
  
  if(valid && form.Vanbar_account_password.value.length == 0)
  {
    valid = false;
    alert("You must enter your account password!");
    form.Vanbar_account_password.focus();
  }
  
  if(valid && form_in.insurance.selectedIndex == 0)
  {
    valid = false;
    alert("You must select whether you want insurance!");
    form_in.insurance.focus();
  }
  else
    form.insurance.value = form_in.insurance[form_in.insurance.selectedIndex].value;
      
  if(valid)
    form.submit();
}


function checkFormCredit(form, form_in)
{
  valid = true;
  
  //alert(form_in.insurance[form_in.insurance.selectedIndex].value);
  
  if(valid && form_in.insurance.selectedIndex == 0)
  {
    valid = false;
    alert("You must select whether you want insurance!");
    form_in.insurance.focus();
  }
  else
    form.insurance.value = form_in.insurance[form_in.insurance.selectedIndex].value;

  if(valid && form.Visitor_name.value.length == 0)
  {
    valid = false;
    alert("You must enter your name!");
    form.Visitor_name.focus();
  }
  
  if(valid && !isEmail(form.Visitor_email_address.value))
  {
    valid = false;
    alert("You must enter a valid email address!");
    form.Visitor_email_address.focus();
  }

  if(valid && form.Billing_street_address.value.length == 0)
  {
    valid = false;
    alert("You must enter a valid street address!");
    form.Billing_street_address.focus();
  }

  if(valid && form.Billing_city.value.length == 0)
  {
    valid = false;
    alert("You must enter a valid city!");
    form.Billing_city.focus();
  }

  if(valid && form.Billing_state.value.length == 0)
  {
    valid = false;
    alert("You must enter a valid state!");
    form.Billing_state.focus();
  }

  if(valid && form.Billing_postcode.value.length == 0)
  {
    valid = false;
    alert("You must enter a valid postcode!");
    form.Billing_postcode.focus();
  }

  // if(valid && form.Billing_country.selectedIndex == 0)
  // {
    // valid = false;
    // alert("You must enter a valid country!");
    // form.Billing_country.focus();
  // }
  
  
  if(valid && form.Card.selectedIndex == 0)
  {
    valid = false;
    alert("You must select a payment method!");
    form.Card.focus();
  }

  // if (form.Card.options[form.Card.selectedIndex].value != "PayPal" && form.Card.options[form.Card.selectedIndex].value != "Direct Deposit") {
      // if (valid && form.Card_No_security.value.length == 0 && form.Card.options[form.Card.selectedIndex].value != "Other") {
          // valid = false;
          // alert("You must enter a valid credit card security number!");
          // form.Card_No_security.focus();
      // }

      // if (valid && form.Exp_Date.value.length == 0 && form.Card.options[form.Card.selectedIndex].value != "Other") {
          // valid = false;
          // alert("You must enter a valid expiry date!");
          // form.Exp_Date.focus();
      // }

      // if (valid && form.Card_Name.value.length == 0 && form.Card.options[form.Card.selectedIndex].value != "Other") {
          // valid = false;
          // alert("You must enter a valid credit card name!");
          // form.Card_Name.focus();
      // }

      // if (valid && !CheckCreditCard(form))
          // valid = false;

      // if (valid && form.Card.options[form.Card.selectedIndex].value == "Other" && form.Comments.value.length == 0) {
          // valid = false;
          // alert("Please fill in the comments box describing how you wish to pay.");
          // form.Comments.focus();
      // }
  // }

  //paypal stuff here.
  // if (valid && form.Billing_country.selectedIndex != 1) {
      // form.currency_code.value = "USD";
  // }
  // else {
      // form.currency_code.value = "AUD";
  // }

  if(GUP('au') == '0')
    form.currency_code.value = "USD";
  else
    form.currency_code.value = "AUD";
  
  if (form.Card.options[form.Card.selectedIndex].value == "PayPal") {
     params = form.action.split("?");
     form.action = "paypal.php?" + params[1];
  }
    
  if(valid) {
    // if(form.Card.options[form.Card.selectedIndex].value == "Credit Card")
      // window.location = "eway.php";
    // else
      form.submit();
  }
}

// Get URL Parameter
// Usage: var frank_param = GUP( 'frank' );
function GUP( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function Create_password()
{
  var width, height, left, top
  
  width = 400;
  height = 300;
  left = (screen.width-width)/2;
  top = ((screen.height-50)-height)/2;

  window.open('create_password.php', 'win', '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=1,status=0,titlebar=1,toolbar=0,left=200,top=25,z-lock=0');
}


//returns true if the string 'str' is in the string 'arr'
function Contains(str, arr)
{
  if(arr.indexOf(str) != -1)
    return 1;
  else
    return 0;
}


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 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 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 isInteger (s)

{   
  var i;

  if (isEmpty(s)) 
     if (isInteger.arguments.length == 1) return defaultEmptyOK;
     else return (isInteger.arguments[1] == true);

  // 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 (!isDigit(c)) return false;
  }

  // All characters are numbers.
  return true;
}


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;
}

var decimalPointDelimiter = "."


function isFloat (s)
{   
  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 (!isDigit(c)) 
      return false;
  }

  // All characters are numbers.
  return true;
}


function isDigit (c)
{
  return ((c >= "0") && (c <= "9"))
}


function OnBackOrder(form, order_id)
{
  form.action = form.action + "&backorder=1&edit=" + order_id;
  form.submit();
}


function OnAddPassword(form)
{
  if(form.password.value.length < 5)
  {
    alert('You must enter a password with more than 5 characters!')
    form.password.focus();
  }
  else if(form.password.value != form.password_confirm.value)
  {
    alert('The passwords do not match!');
    form.password.value = ''
    form.password_confirm.value = '';
    form.password.focus();
  }
  else
    form.submit();
}


function OnChangeType(card) {
   if (card.options[card.selectedIndex].value == "Other") {
      card.form.action = document.URL;
      if(card.form.action.indexOf('?') <= 0)
        card.form.action += '?a=0';
      
      card.form.action = AddNameValueToQueryString("surcharge", "2", card.form.action);
      card.form.submit();
   }
   else if (card.selectedIndex == 2 || card.selectedIndex == 3) {
      //paypal and direct deposit
      card.form.action = document.URL;
      card.form.action = card.form.action.replace(/surcharge=1/, "surcharge=0");


      card.form.submit();

   } else {
      card.form.action = document.URL;
      if(card.form.action.indexOf('?') <= 0)
        card.form.action += '?a=0';

      card.form.action = AddNameValueToQueryString("surcharge", "1", card.form.action);
      card.form.action = AddNameValueToQueryString("selected_insurance", document.insurance.insurance.selectedIndex.toString(), card.form.action);

      card.form.submit();
   }
}


function OnClearAll(form)
{
  form.search.value = '';
  form.status_search.selectedIndex = 0;
  form.sort_status.selectedIndex = 0;
  
  form.submit();
}


function OnCreatePassword(form)
{
  var ok;
  ok = true;
  
  if(form.account.value.length == 0)
  {
    alert('You must enter an account!');
    form.account.focus();
    ok = false;
  }
  else if(!isEmail(form.email.value))
  {
    alert('You must enter a valid email address!');
    form.email.focus();
    ok = false;
  }
  
  if(ok)
    form.submit();
  
  return ok;
}


function OnDeleteLabOrder(ID, form)
{
  if(confirm('Are you sure you want to delete this order?'))
  {
    form.form_action.value = "delete";
    form.ID.value = ID;
    form.submit();
  }
}


function OnDeleteOrder(form, ID, catalogue_album)
{
  if(confirm('Are you sure that you want to delete this order?'))
  {
    form.action += '&delete=' + ID;
    //alert(form.action);
    form.submit();
  }
}


function OnLogIn(form)
{
  var ok;
  ok = true;
  
  if(form.username.value.length == 0)
  {
    alert('You must enter a username!');
    ok = false;
    form.username.focus();
  }

  else if(form.password.value.length == 0)
  {
    alert('You must enter a password!');
    ok = false;
    form.password.focus();
  }
  
  if(ok)
    form.submit();
}


function OnProcess(form, ID, insurance)
{
  var valid;
  
  valid = true;

  if(!isFloat(form.process_shipping.value))
  {
    alert('You must enter a number greater than 0!');
    valid = false;
    form.process_shipping.focus();
  }
  else if(form.process_shipping.value <= 0)
  {
    alert('You must enter a number greater than 0!');
    valid = false;
    form.process_shipping.focus();
  }
  else if(form.courier_type_other.value.length > 0 && form.courier_type.options[form.courier_type.selectedIndex].text != "Other")
  {
    alert("You can only enter a description for \'Other\' if you select a Courier Type of \'Other\'!");
    valid = false;
    form.courier_type.focus();
  }
  else if(insurance == 1 && form.process_insurance.value <= 0)
  {
    alert('You must enter a number greater than 0!');
    valid = false;
    form.process_insurance.focus();
  }
    
  if(valid)
  {
    form.action += '&shipping=' + form.process_shipping.value + '&status=3' + '&edit=' + ID;
    form.submit();
  }
}


function OnProcessNoItems(form, ID, insurance)
{
  var valid;
  
  valid = true;
    
  if(valid)
  {
    form.action += '&no_items=1&status=3' + '&edit=' + ID;
    form.submit();
  }
}


function OnProcessPriorToConfirm(form, ID)
{
  var valid;
  
  valid = true;

  //if(form.shipping_details.value.length > 255)
  //{
  //  alert('2Comments can only be up to 255 characters in length!\nYour comments are ' + form.shipping_details.value.length + ' characters long.');
  //  valid = false;
  //  form.shipping_details.focus();
  //}
    
  if(valid)
  {
    form.action += '&status=3&edit=' + ID;
    form.submit();
  }
}


function OnProcessFinal(form, ID)
{
  var valid;
  
  valid = true;

  //if(form.shipping_details.value.length > 255)
  //{
  //  alert('Comments can only be up to 255 characters in length!\nYour comments are ' + form.shipping_details.value.length + ' characters long.');
  //  valid = false;
  //  form.shipping_details.focus();
  //}
    
  if(valid)
  {
    form.action += '&status=3&edit=' + ID;
    form.submit();
  }
}


function OnSearch1(form)
{
  var ok;
  
  ok = true;

  if(form.search_type.selectedIndex == '0' && !isInteger(form.search.value))
  {
    alert('You must enter an Order number to search for!');
    form.search.focus();
    ok = false;
  }
  
  if(ok)
    form.submit();
}


function OnSendPassword(form)
{
  var ok;
  ok = true;
  
  
  if(form.username_send.value.length == 0)
  {
    alert('You must enter an Account Number!');
    ok = false;
    form.username_send.focus();
  }
  else if(ok && !isEmail(form.email.value))
  {
    alert('You must enter a valid email address!');
    ok = false;
    form.email.focus();
  }
  
  if(ok)
    form.submit();
}


function OnSendToCustomer(form, ID, insurance)
{
  var valid;
  
  valid = true;
  
  if(!isFloat(form.shipping_shipping.value))
  {
    alert('You must enter a number greater than 0!');
    valid = false;
    form.shipping_shipping.focus();
  }
  else if(form.shipping_shipping.value <= 0)
  {
    alert('You must enter a number greater than 0!');
    valid = false;
    form.shipping_shipping.focus();
  }
  //else if(form.shipping_comments.value.length > 255)
  //{
  //  alert('Comments can only be up to 255 characters in length!\nYour comments are ' + form.shipping_comments.value.length + ' characters long.');
  //  valid = false;
  //  form.shipping_comments.focus();
  //}
  else if(insurance == 1 && form.shipping_insurance.value <= 0)
  {
    alert('You must enter a number greater than 0!');
    valid = false;
    form.shipping_insurance.focus();
  }
  
  else if(form.courier_type_other.value.length > 0 && form.courier_type.options[form.courier_type.selectedIndex].text != "Other")
  {
    alert("You can only enter a description for \'Other\' if you select a Courier Type of \'Other\'!");
    valid = false;
    form.courier_type.focus();
  }
  
  if(valid)
  {
    form.action += '&shipping=' + form.shipping_shipping.value + '&status=1&edit=' + ID;
    //alert(form.action);
    form.submit();
  }
}


function OnStatusReceived(form, ID)
{
  if(confirm('Are you sure that you want to change this order\'s status?'))
  {
    form.action += '&status_received=' + ID + '&edit=' + ID;
    form.submit();
  }
}


function OnViewOrder(form, ID)
{
    form.action = "view_orders.php?edit=" + ID;

  form.submit();
}


function OnViewOrderAlbum(form, ID)
{
  form.action = "../album/album_order.php?edit=" + ID;
  form.submit();
}