function cc_check(ccNumb) {
    var valid = "0123456789"  // Valid digits in a credit card number
    var len = ccNumb.length;  // The length of the submitted cc number
    var iCCN = parseInt(ccNumb);  // integer of ccNumb
    var sCCN = ccNumb.toString();  // string of ccNumb
    sCCN = sCCN.replace (/^\s+|\s+$/g,'');  // strip spaces
    var iTotal = 0;  // integer total set at zero
    var bNum = true;  // by default assume it is a number
    var bResult = false;  // by default assume it is NOT a valid cc
    var temp;  // temp variable for parsing string
    var calc;  // used for calculation of each digit
    
    // Determine if the ccNumb is in fact all numbers
    for (var j=0; j<len; j++) {
      temp = "" + sCCN.substring(j, j+1);
      if (valid.indexOf(temp) == "-1"){bNum = false;}
    }
    
    // if it is NOT a number, you can either alert to the fact, or just pass a failure
    if(!bNum){
      /*alert("Not a Number");*/bResult = false;
    }
    
    // Determine if it is the proper length 
    if((len == 0)&&(bResult)){  // nothing, field is blank AND passed above # check
      bResult = false;
    } else{  // ccNumb is a number and the proper length - let's see if it is a valid card number
      if(len >= 15){  // 15 or 16 for Amex or V/MC
        for(var i=len;i>0;i--){  // LOOP throught the digits of the card
          calc = parseInt(iCCN) % 10;  // right most digit
          calc = parseInt(calc);  // assure it is an integer
          iTotal += calc;  // running total of the card number as we loop - Do Nothing to first digit
          i--;  // decrement the count - move to the next digit in the card
          iCCN = iCCN / 10;                               // subtracts right most digit from ccNumb
          calc = parseInt(iCCN) % 10 ;    // NEXT right most digit
          calc = calc *2;                                 // multiply the digit by two
          // Instead of some screwy method of converting 16 to a string and then parsing 1 and 6 and then adding them to make 7,
          // I use a simple switch statement to change the value of calc2 to 7 if 16 is the multiple.
          switch(calc){
            case 10: calc = 1; break;       //5*2=10 & 1+0 = 1
            case 12: calc = 3; break;       //6*2=12 & 1+2 = 3
            case 14: calc = 5; break;       //7*2=14 & 1+4 = 5
            case 16: calc = 7; break;       //8*2=16 & 1+6 = 7
            case 18: calc = 9; break;       //9*2=18 & 1+8 = 9
            default: calc = calc;           //4*2= 8 &   8 = 8  -same for all lower numbers
          }                                               
        iCCN = iCCN / 10;  // subtracts right most digit from ccNum
        iTotal += calc;  // running total of the card number as we loop
      }  // END OF LOOP
      if ((iTotal%10)==0){  // check to see if the sum Mod 10 is zero
        bResult = true;  // This IS (or could be) a valid credit card number.
      } else {
        bResult = false;  // This could NOT be a valid credit card number
        }
      }
    }
    // change alert to on-page display or other indication as needed.
    if(bResult) {
      //alert("This IS a valid Credit Card Number!");
      //return('good_card');
    }
    if(!bResult){
      //alert("This is NOT a valid Credit Card Number!");
      return('bad_card');
    }
      //return bResult; // Return the results
}


function loadSystemDetails() {
    
    var node = document.getElementById('system_details');
    if (node) {
        
        var systemDetailsHTML = getURL('/system_details_list.php');
        node.innerHTML = systemDetailsHTML;
        
    }
    
}

function loadOptions(p) {
    
    deleteOptions({select: p.select});
    for (var i = 0; i < p.options.length; i++) {
        
        p.select.options[i] = new Option(p.options[i].text, p.options[i].value, null, (p.options[i].selected) ? true : false);
        
    }
    
}

function deleteOptions(p) {
    
    p.select.options.length = 0;
    return;
    
}

function xmlHttpRequestLoader() {
    
    if (window.XMLHttpRequest) {
        
        return new XMLHttpRequest();
        
    } else {
        
        return new ActiveXObject('Microsoft.XMLHTTP');
        
    }
    
}

function getURL(url) {
    
    var xhr = xmlHttpRequestLoader();
    xhr.open("GET", url, false);
    xhr.send(null);
    return xhr.responseText;
    
}

function setCookie(name, value, expires, path, domain, secure) {
    var curCookie = name + "=" + escape(value) +
                    ((expires) ? "; expires=" + expires.toGMTString() : "") +
                    ((path) ? "; path=" + path : "") +
                    ((domain) ? "; domain=" + domain : "") +
                    ((secure) ? "; secure" : "");
    document.cookie = curCookie;
}


function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else
        begin += 2;
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
        end = dc.length;
    return unescape(dc.substring(begin + prefix.length, end));
}


function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
                          ((path) ? "; path=" + path : "") +
                          ((domain) ? "; domain=" + domain : "") +
                          "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function toggleDisplay(id) {
    
    var e = document.getElementById(id);
    if (e.style.display == 'none') {
        
        e.style.display = 'block';
        
    } else {
        
        e.style.display = 'none';
        
    }
    
}

function isValidCreditCard(ccnum) {
   var matched = 0;
   if (ccnum == '4111111111111111') { return true; } // Number to run tests
   // Visa: length 16, prefix 4, dashes optional.
   var visa = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
   // Mastercard: length 16, prefix 51-55, dashes optional.
   var mc = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
   // Discover: length 16, prefix 6011, dashes optional.
   var dis = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
   // American Express: length 15, prefix 34 or 37.
   var amex = /^3[4,7]\d{13}$/;
   // Diners: length 14, prefix 30, 36, or 38.
   var din = /^3[0,6,8]\d{12}$/;
   if (visa.test(ccnum)) {
      matched = 1;
   } else if (mc.test(ccnum)) {
      matched = 1;
   } else if (dis.test(ccnum)) {
      matched = 1;
   } else if (amex.test(ccnum)) {
      matched = 1;
   }/* else if (din.test(ccnum)) {
      matched = 1;
   }*/
   if (!matched) return false;
   // Checksum ("Mod 10")
   // Add even digits in even length strings or odd digits in odd length strings.
   var checksum = 0;
   for (var i=(2-(ccnum.length % 2)); i<=ccnum.length; i+=2) {
      checksum += parseInt(ccnum.charAt(i-1));
   }
   // Analyze odd digits in even length strings or even digits in odd length strings.
   for (var i=(ccnum.length % 2) + 1; i<ccnum.length; i+=2) {
      var digit = parseInt(ccnum.charAt(i-1)) * 2;
      if (digit < 10) { checksum += digit; } else { checksum += (digit-9); }
   }
   if ((checksum % 10) == 0) return true; else return false;
}

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 MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function NewWindow(mypage, myname, w, h, scroll) {
var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
win = window.open(mypage, myname, winprops)
if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function openWin(url,windowName,options){
  var WindowHandle=window.open(url,windowName,options);
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function showlayer(whichLayer) {
    document.getElementById(whichLayer).style.display = 'block';
}

function hidelayer(whichLayer) {
    document.getElementById(whichLayer).style.display = 'none';
}

