////////////////////////////////////////////////////////////////////////////////
// JavaScript for POS software                                                  
//                                                                              
// Created      : 12/8/2007 6:28:33 PM                                                  
// Last updated : 12/8/2007 6:28:35 PM                                      
//                                                                              
// Author       : Shawn McKinley                                                
//                shawnm <at> ezinvoice.com                                     
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Function button
//   ac - action
//   ca - catagory
//   ty - type
//   de - detail
//   ye - year
//   mo - month
//   da - day
//   ta - target
////////////////////////////////////////////////////////////////////////////////
function button(pos_ac,pos_ca,pos_ty,pos_de,pos_ye,pos_mo,pos_da,pos_ta,pos_id) {
  var windowName = pos_ta;
  main.document.pos.action.value   = pos_ac;
  main.document.pos.category.value = pos_ca;
  main.document.pos.type.value     = pos_ty;
  main.document.pos.detail.value   = pos_de;
  main.document.pos.year.value     = pos_ye;
  main.document.pos.month.value    = pos_mo;
  main.document.pos.date.value     = pos_da;
  main.document.pos.target.value   = pos_ta;
  main.document.pos.id.value       = pos_id;
  window.windowName.location.href =                               //  
    'http://127.0.0.1/' + CGIPath +                                         //  
    '?section=Orders' +                                                     //  
    '&action=Orders_Step_2' +                                               //  
    '&session=' + getSession() +                                            //  
    oString;                                                                //  
  // parent.document.pos.submit();
}

function flood_day_boxes() {
  var inputs = document.getElementsByTagName('input');
  var myregex = /_d$/;
  for (var i = 0; i < inputs.length; i++) {
    if (inputs[i].type == 'checkbox' && inputs[i].name.match(/_d$/)) {
      if(inputs[i].checked == true) {
        inputs[i].checked = false;
      } else {
        inputs[i].checked = true;
      }
    }
  }
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
  document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_findObj(n, d) { //v4.01
var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
  d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function getScrollX() {
  var scrOfX = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfX = document.documentElement.scrollLeft;
  }
  return scrOfX;
}

function getScrollY() {
  var scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  }
  return scrOfY;
}

function MM_showHideLayers() { //v6.0
var i,p,v,obj,args=MM_showHideLayers.arguments;
for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) {
  v=args[i+2];
  if (obj.style) {
//alert('1: '+document.body.scrollLeft+' 2: '+document.body.scrollTop);
var dsocleft = getScrollX();
var dsoctop  = getScrollY();
obj.style.left = (parseInt(dsocleft)+10)+'px';
obj.style.top  = (dsoctop+125)+'px';
obj=obj.style;
v=(v=='show')?'visible':(v=='hide')?'hidden':v;

}
  obj.visibility=v;
}
}

function isblank(s) {
  for(var i = 0; i < s.length; i++) {
    var c = s.charAt(i);
    if ((c != ' ') && (c != '\n') && (c != '\t')) {
      return false;
    }
  }
  return true;
}

function verify(f) {
  var empty_fields = '';
  var errors = '';
  var debug = '';
  for(var i = 0; i < f.length; i++) {
    var e = f.elements[i];
    var n = e.name;     // variable name
    var t = e.type;     // variable type
    var v = e.value;    // vairable value
    var o = e.optional; // vairable option
    var mi = e.min;     // variable minimum
    var ma = e.max;     // variable maximum
    if (t != 'hidden') {
      debug += "\n - "+n+' ( '+v+' ) [ '+t+' ]';
      if (t == 'select-one' && !o) {
        if (v == null || v == 'Select' || v == 'Select One' || isblank(v)) {
          var errName = '';
          if (n == 'bstate'   || n == 'state')          errName = 'State';
          else if (n == 'bcountry' || n == 'country')   errName = 'Country';
          else if (n == 'ccard')                        errName = 'Credit Card Type';
          else if (n == 'expmonth')                     errName = 'Expiration Month';
          else if (n == 'expyear')                      errName = 'Expiration Year';
          else if (n == 'greeting_program_application') errName = 'Application';
          else if (n == 'expected_participants')        errName = 'Number of Expected Participants';
          else if (n == 'hear_about_us')                errName = 'How did you heard about us';
          else if (n == 'subject')                      errName = 'Subject';
          if(errName == null || errName == '') { continue; }
          empty_fields += "\n  * " + errName + ' - Please make a selection';
        }
        continue;
      }
      if (t == 'textarea' && !o) {
        if (v == null || isblank(v)) {
          var errName;
          if (n == 'comments')        errName = 'Comments';
          else if (errName == null || errName == '') { continue; }
          empty_fields += "\n  *" + errName + ' - Please fill out this field';
        }
        continue;
      }
      if (t == 'text' && !o) {
        if (v == null || isblank(v)) {
          var errName;
          if (n == 'address1')        errName = 'Address';
          else if (n == 'city')       errName = 'City';
          else if (n == 'zip')        errName = 'Zip Code';
          else if (n == 'first_name') errName = 'First Name';
          else if (n == 'last_name')  errName = 'Last Name';
          else if (n == 'phone1')     errName = 'Phone Number';
          else if (n == 'email')      errName = 'Email Address';
          else if (n == 'email')      errName = 'Email Address';
          if(errName == null || errName == '') { continue; }
          if (empty_fields.match(errName)) { continue; }
          empty_fields += "\n  *" + errName + ' - Please fill out this field';
          continue;
        }
        if (e.numeric || mi != null || ma != null) {
          var i = parseFloat(v);
          if (isNaN(i) || (mi != null && i < mi) || (ma != null && i > ma)) {
            errors += '- The field ' + n + ' must be a number';
            if (mi != null) {
              errors += ' that is greater than ' + mi;
            }
            if (ma != null && mi != null) {
              errors += ' and less than ' + ma;
            } else if (ma != null) {
              errors += ' that is less than ' + ma;
            }
            errors += ".\n";
          }
          continue;
        }
        if (n == 'email') {
          var regex=v.match(/^\w+\@\S+\.\w{2,5}$/);
          var period = v.match(/\./);
          var at = v.match(/\@/);
          if (regex == null) {
            errors += "\n  *Email address is not valid"; continue;
          }
          continue;
        } else if(n == 'your_email') {
          var period = v.match(/\./);
          var at = v.match(/\@/);
          if (period == null || at == null) {
            errors += "\n  *Your email address is not valid"; continue;
          }
          continue;
        } else if(n == 'tell_one_email') {
          var period = v.match(/\./);
          var at = v.match(/\@/);
          if (period == null || at == null) {
            errors += "\n  *Your first friend's email address is not valid"; continue;
          }
          continue;
        } else if(n == 'tell_two_email') {
          var period = v.match(/\./);
          var at = v.match(/\@/);
          if (period == null || at == null) {
            errors += "\n  *Your second friend's email address is not valid"; continue;
          }
          continue;
        } else if(n == 'tell_three_email') {
          var period = v.match(/\./);
          var at = v.match(/\@/);
          if (period == null || at == null) {
            errors += "\n  *Your third friend's email address is not valid"; continue;
          }
          continue;
        } else if(n == 'phone1') {
          var phone = v;
          phone = phone.split("-").join('');
          var bad = 0;
          for (var h = 0; h <= phone.length; h++) {
            var c = phone.charAt(h);
            if(!(c >= 0 && c <= 9)) { bad++; }
          }
          if (bad) {
            errors += "\n  *Phone number (xxx-xxx-xxxx)";
            errors += "\n     must contain only numbers and dashes";
            continue;
          }
          if(phone.length != 10) {
            errors += "\n  *Phone number (xxx-xxx-xxxx)";
            errors += "\n     most likely needs area code";
            continue;
          }
        } else if (n == 'bzip_code' || n == 'zip_code') {
          var zip = v;
          zip = zip.split('-').join('');
          if(zip.length < 1) {
            errors += "\n  *Zip code - empty (xxxxx or xxxxx-xxxx)";
            continue;
          }
          var bad = 0;
          for (var g = 0; g <= zip.length; g++) {
            var c = zip.charAt(g);
            if(!(c >= 0 && c <= 9)) { bad++; }
          }
          if (bad) {
            errors += "\n  *Zip code (xxxxx or xxxxx-xxxx)";
            errors += "\n     must contain only numbers and dashes";
            continue;
          }
          if(zip.length<5) {
            errors += "\n  *Zip code (xxxxx or xxxxx-xxxx)";
            errors += "\n     must be at least 5 numbers";
            continue;
          }
          if(zip.length>9) {
            errors += "\n  *Zip code (xxxxx or xxxxx-xxxx)";
            errors += "\n     can't be more than 9 numbers";
            continue;
          }
          if(zip.length != 5 && zip.length != 9) {
            errors += "\n  *Zip code invalid (xxxxx or xxxxx-xxxx)";
            continue;
          }
        } else if (n == 'ccnumber') {
          var ccnum = v;
          if (ccnum.length < 13) {
            errors += "\n  *Credit Card Number too short";
            continue;
          }
          if (ccnum.length > 16) {
            errors += "\n  *Credit Card Number too long";
            continue;
          }
          var c;
          var sum = 0;
          var nul = 1;
          // possible problem start
          if (!isValidCC(ccnum)) {
            errors += "\n   *Credit Card Number is invalid";
            continue;
          }
          // possible problem end
          if((sum % 10) != 0) {
            errors += "\n  *Credit Card Number is invalid";
            continue;
          } else {
            var mc = ccnum.substring(0,2);
            var vi = ccnum.substring(0,1);
            var ae = ccnum.substring(0,2);
            if (!((mc > 50 && mc < 56) || vi == 4 || ae == 34 || ae == 37)) {
              errors += "\n  *Credit Card Type is invalid";
              errors += "\n     Not a Visa, MasterCard, or American Express";
              continue;
            }
            continue;
          }
        }
        continue;
      }
    }
  }
  var msg = 'Please correct the following errors and then click the ' + f.elements[f.length-1].value + ' button:';
  if (!empty_fields && !errors) return true;
  if (empty_fields) { msg += empty_fields; }
  if (errors) { msg += errors; }
  /* msg += "\n-------------------------------------------------------"
      +  "\n- Debug: Variable Information -"
      +  "\n-------------------------------------------------------"
      +  debug
      +  "\n-------------------------------------------------------"; */
  if (msg.match(/\*/)) {
    alert(msg);
    return false;
  } else {
    return true;
  }
}
function isValidCC(ccnum) {
  var c; var sum = 0; var nul = 1;
  for(var i = 0; i < ccnum.length; i++) {
    var num = ccnum.substring(ccnum.length-i-1,ccnum.length-i);
    var tnum = parseInt(num,10)*nul;
    if (tnum >= 10) { sum += (tnum % 10) + 1; } else { sum += tnum; }
    if (nul == 1) { nul++; } else { nul--; }
  }
  if((sum % 10) != 0) { return false; } else { return true; }
}
function populate() {
  if (document.cs.sameInfo.value !=null && document.cs.sameInfo.checked) {
    document.cs.addressb.value=document.cs.address.value;
    document.cs.bcity.value=document.cs.city.value;
    document.cs.bstate.value=document.cs.state.value;
    document.cs.bzip_code.value=document.cs.zip_code.value;
    document.cs.bcountry.value=document.cs.country.value;
  } else {
    document.cs.addressb.value='';
    document.cs.bcity.value='';
    document.cs.bstate.value='Select';
    document.cs.bzip_code.value='';
    document.cs.bcountry.value='';
  }
  return true;
}