// given a form field, see whether it is filled in
function validatePresent(thefield) {

  if ( typeof(thefield) == "undefined" ) {
    //alert("Testing for unknown field.");
    //return true;
  }
 
  if (thefield.value  == "") {
    thefield.focus();
    thefield.select();
    thefield.style.backgroundColor = "#ffcccc";
    thefield.style.outline = "2px pink solid";
    return false;
 
  }
thefield.style.backgroundColor = "#ffffff";
return true;
}

function validatePresent2(thefield) {

  if ( typeof(thefield) == "undefined" ) {
    //alert("Testing for unknown field.");
    //return true;
  }
 
  if (thefield.value  == "") {
    thefield.focus();
    thefield.select();
    thefield.style.backgroundColor = "#ffcccc";
    thefield.style.outline = "2px pink solid";
    return false;
 
  }
/*thefield.style.backgroundColor = "#ffffff";*/

/*var oldclass = thefield.class;*/
alert ("dammit");
thefield.setAttribute('class', 'okay');
alert ("dammit");
return true;

}

//simple a@b.c mail validation
function validateEmail(email) {


  var re_mail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z])+$/;
  
  if (!re_mail.test(email.value) || email.value == "" ) {
    email.focus();
    email.select();
    email.style.backgroundColor = "#ffcccc";
    return false;
  } else {
    //return true;
    email.style.backgroundColor = "#ffffff";
    return true;
  }

}

// If field matches predetermined "null" value "--", return false
// Assume single value allowed at a time
function validateSelect(thefield) {

var dropdownIndex = thefield.options.selectedIndex;
var dropdownValue = thefield.options[dropdownIndex].text;

  if (dropdownValue == "--") { 
    thefield.focus();
    /*thefield.select();*/ /*not a fn in IE?*/
    thefield.style.backgroundColor = "#ffcccc";
    return false;
  } else {
    thefield.style.backgroundColor = "#ffffff";
    return true;
  }
  

}

//true if two form fields have equal values
function validateIdentical(field1, field2) {

  if (field1.value == field2.value) {
    //field1.style.backgroundColor = "#ffffff";
    //field2.style.backgroundColor = "#ffffff";
    return true;
  } else {


    field2.style.backgroundColor = "#ffcccc";

    with (field1) {
      focus();
      select();
      style.backgroundColor = "#ffcccc";
    }

    return false;
  }

}


function ccLuhn(cc_number)
{

    var success = 0;
    var cc = cc_number.value
    // digits 0-9 doubled with nines cast out
    var doubled = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9];

    // remove non-digit characters
    cc = cc.replace(/[^\d]/g, '');

    if (cc.length == 0) {
      return false;
    }

    var digits = cc.split('');

    // alternate between summing the digits
    // or the result of doubling the digits and
    // casting out nines (see Luhn description)
    var alt = false;
    var total = 0;
    while (digits.length)
    {
        var d = Number(digits.pop());
        total += (alt ? doubled[d] : d);
        alt = !alt;
    }
    //return total % 10 == 0;
    success = total % 10 == 0;

    if (success) {
      cc_number.style.backgroundColor = "#ffffff"; 
    } else {
      cc_number.style.backgroundColor = "#ffcccc";
    }

    return success;
    
}



