// --------------------------------------------------------------------
// File: orderCommon.js
// Desc: javascript common to online ordering pages 
//
// 17-Feb-2005 John Grover
//   Separated javascript into its own file
//
// 23-Mar-2005 John Grover
//   Minimum online donation changed to = suggested donation
// --------------------------------------------------------------------
browser_name = navigator.appName;
browser_version = parseFloat(navigator.appVersion); 
subnum = 0;
count = 0;
var suggestion = "";
roll = 'false';

if (browser_name == "Netscape" && browser_version >= 2.0) {
  roll = 'true';
}
else if (browser_name == "Microsoft Internet Explorer" && browser_version >= 2.0) {
  roll = 'true';
}
else if (browser_version >= 4.0) {
  roll = 'true';
}
else {
  roll = 'false';
}

// Verify input is numeric
function isanumber(input) {
  var ok = true;
  for (var i = 0; ((i < input.length) && ok); i++) {
    var chr = input.charAt(i);
    ok = (ok && (chr >= "0") && (chr <= "9"));
  }
  return ok;
}

// Verify input is a float
function isfloat(input) {
  var ok = true;
  var periods = 0;
  for (var i = 0; ((i < input.length) && ok); i++) {
    var chr = input.charAt(i);
    if (chr == ".") {
      periods++;
    }
    else {
      ok = (ok && (chr >= "0") && (chr <= "9"));
    }
  }
  return (ok && (periods <= 1));
}

// Verify input is properly formatted dollar amount
function testdollar(dollar, theform) {
  if (roll == 'true') {
    if (dollar.value != "") {
      if (isfloat(dollar.value)) {
          dollar.value = strdollars(sfloat(dollar.value));
      } else {
        alert("Sorry, that isn't a valid dollar amount. You must enter the amount like this: 1.23 or .45 or 6.");
        dollar.focus();
        dollar.select();
      }
    }
  }
}

// Convert string to integer
function sint(thing) {
  var number = parseInt(thing, 10);
  if (!(number>0) || (number==0)) { return 0 } else { return number }
}

// Convert string to float
function sfloat(thing) {
  var number = parseFloat(thing);
  if (!(number>0) || (number==0)) { return 0 } else { return number }
}

// Format amount to 999999.99
function strdollars(amount) {
  var total = 0;
  var dollars = 0;
  var cents = 0;
  var strcents = "";
  var strtotal = "";

  total = Math.floor(amount * 100);
  cents = total % 100;
  strcents = (cents < 10) ? ("0" + cents) : ("" + cents);
  dollars = (total - cents) / 100;
  strtotal = "" + dollars + "." + strcents;
  return strtotal;
}

// Only allow numeric characters in input, recalc when ok
function test(input, theform) {
  if (roll == 'true') {
    if (!isanumber(input.value)) {
      alert("Please enter only numbers in this box");
      input.focus();
      input.select();
    }
    else {
      addtapes(theform); 
    }
  }
}

// Verify input is >= minimum required donation
function check(input, theform) {
  if (roll == 'true') {
    txt = "To offset the overhead of online processing the minimum online " +
          "donation for these " + 
          sint(theform.belttl.value) + " items is $" + theform.minimum.value + "." +
          "\n\nIf you want to avoid the minimum online donation, you may use our " +
          "printable order form and mail or fax it to us.\n\n -The Saint Philomena Foundation"
    if (sfloat(input.value) < sfloat(theform.minimum.value)) {
      alert(txt)
      input.focus()
      return false
    }else{
      return true 
    }
  }
}

// Calulate and update shipping for order
function calcShipping(form){
  var x = new String();
  
  if (form.sameship.checked){
    x = form.billcountry.value
  } else {
    x = form.shipcountry.value
  }
  
  x=x.toUpperCase();
  
  prevShp=sfloat(form.shipping.value);
  prevTot=sfloat(form.donation.value);
  
  if (x != "USA" && x != "UNITED STATES" && x != "US" && x != "" ) {
    txt="You have indicated you want your order shipped outside the United States (to " + x + ")\n" +
        "For shipping to Canada, we require an additional minimum of $5.00 plus $1 (USD) per CD/tape "+
        "or for shipping outside the US and Canada we require an additional $7.00 plus $2 per CD/tape (USD).\n"+
        "The shipping amount and order total have been automatically updated"
    alert(txt);
    if (x == "CANADA") {
      currShip = 5 + ( form.shipitems.value * 1 )
    }else{
      currShip = 7 + ( form.shipitems.value * 2 )
    }
  } else {
    currShip = 0
  }
  
  // Ah.. but shipping may change 
  currTot = prevTot - prevShp + currShip;
  form.shipping.value = strdollars(currShip)
  form.donation.value = strdollars(currTot)
}

//
function validateClick(form) {
  addtapes;
  if (check(form.donation, form)){ 
    if (sint(form.donation.value)==0){
      alert ("You didn't order anything")
    }else{
      form.submit() 
    }
  } else { 
    form.donation.focus() 
  }
}

// Avoid multiple form submissions
function submitclick(form) {
 if (subnum==0) {
  subnum++;
  return true;
 } else {
  alert("You have already clicked the submit button. Please wait for the results of your order (30 to 60 seconds).");
  return false;
 }
}

// Validate credit card number format
function MycheckCreditCard (cardnumber, cardname) {
  txt = "The Credit card number you have entered is not a valid " +
         cardname.value + " number, please correct it and try again"
  if (cardnumber.value != ""){
    if (!checkCreditCard(cardnumber.value, cardname.value)) alert (txt)
    cardnumber.focus()
    cardnumber.select()
  } 
}

// Validate the expiration date
function checkExpiration (expDate) {

  if (expDate.value == "") return
  msg=""
  err=false
  
  s = new String (expDate.value)
  i = s.indexOf("/")
  a = new Array ();
  a=s.split("/")
  
  if ((i<=0) || (a.length != 2)){
    err=true
    msg="Invalid date format"
  } else {
  
    if (!isanumber(a[0]) || !isanumber(a[1])){
      err=true
      msg="Invalid date!"
    } else {
  
      mm = sint(a[0]) - 1; 
      yy = sint(a[1])
      yy = (yy > 40) ? yy += 1900 : yy += 2000
    
      if ((!err) && (mm < 0 || mm > 11)){
        err=true
        msg="Invalid month!"
      } else {
  
        t = new Date()
        d = new Array(31,28,31,30,31,30,31,31,30,31,30,31)
        x = new Date( yy, mm, d[mm]) // leap year issues ...

        if ( x < t ){
          err=true
          msg="Expired!?\nThe expiration date entered is past!"
        }
      }
    }
  }

  if (err) {
    msg += "\nplease enter in MM/YY format i.e. 12/06 for December 2006"
    alert(msg)
    expDate.focus()
    expDate.select()
  }
}


function validEmail(addy) {
// validate email address format
  var at="@"
  var dot="."
  var lat=addy.indexOf(at)
  var lstr=addy.length
  var ldot=addy.indexOf(dot)
		
  if (addy.indexOf(at)==-1 || addy.indexOf(at)==0 || addy.indexOf(at)==lstr){ return false }
  if (addy.indexOf(dot)==-1 || addy.indexOf(dot)==0 || addy.indexOf(dot)==lstr){ return false }
  if (addy.indexOf(at,(lat+1))!=-1){ return false }
  if (addy.substring(lat-1,lat)==dot || addy.substring(lat+1,lat+2)==dot){ return false }
  if (addy.indexOf(dot,(lat+2))==-1){ return false }
  if (addy.indexOf(" ")!=-1){ return false }

  return true					
}
