

////////////////////////////

// example of how to use this: <input type="button" name="Submit"  value="Submit"  onclick="runSubmit(this.form)">

var form_errors = "";

function isEmail(str) {
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

function validateCreditCard(s) {
    
    // remove non-numerics
    var v = "0123456789";
    var w = "";
    for (i=0; i < s.length; i++) {
    x = s.charAt(i);
    if (v.indexOf(x,0) != -1)
    w += x;
    }
    // validate number
    j = w.length / 2;
    if (j < 6.5 || j > 8 || j == 7) return false;
    k = Math.floor(j);
    m = Math.ceil(j) - k;
    c = 0;
    for (i=0; i<k; i++) {
    a = w.charAt(i*2+m) * 2;
    c += a > 9 ? Math.floor(a/10 + a%10) : a;
    }
    for (i=0; i<k+m; i++) c += w.charAt(i*2+1-m) * 1;
    return (c%10 == 0);
}

// check exp date
function validateCardExpDate(month,year) {
    year = parseInt(year,10);
    month = parseInt(month,10);
    var today=new Date();
    month_now = today.getMonth()+1;
    year_now = today.getFullYear();
    
    if (year < 1) return false;
    if (year<50) year = year+2000;
    if (year<100) year = year+1900;
    //alert ('testing: month='+month+',year='+year+', against:'+ month_now +'/'+ year_now);
    if (year < year_now) return false;
    if ((month<1) || (month>12)) return false;
    if ((month < month_now) && (year == year_now)) return false;
    return true;
}

// simple test for address format
function testMail (form, Ctrl, mesg) {
	if (!isEmail(Ctrl.value)) form_errors = form_errors + mesg + "\r\n";
}

// simple test for a value
function testValue (form, Ctrl, value, mesg) {
	if (!(Ctrl.value == value)) form_errors = form_errors + mesg + "\r\n";
}

// simple test for checkbox
function testChecked (form, Ctrl, mesg) {
	if (!Ctrl.checked) form_errors = form_errors + mesg + "\r\n";
}

// simple test for cc card format
function testCCNum (form, Ctrl, mesg) {
	if (!validateCreditCard(Ctrl.value)) form_errors = form_errors + mesg + "\r\n";
}

// simple test for exp date format
function testCCExpDate (form, Ctrl_month, Ctrl_Year, mesg) {
	if (!validateCardExpDate(Ctrl_month.value,Ctrl_Year.value)) form_errors = form_errors + mesg + "\r\n";
}


// simple test for empty value
function testEmpty(form, Ctrl, mesg) {
	if (Ctrl.value == "") form_errors = form_errors + mesg + "\r\n";
}

// Credit Card Validation Javascript
// copyright 12th May 2003, by Stephen Chapman, Felgall Pty Ltd

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.


function testRadioChecked(form, Ctrl, mesg) {
   var is_checked = false;
   var val = "";
   for (var i=0; i < Ctrl.length; i++) {
       if (Ctrl[i].checked) {
          is_checked = true;
          val = Ctrl[i].value;
       }
   }
       
   if (!is_checked) form_errors = form_errors + mesg + "\r\n"; 
   return  val;
}


function formOK() {
   if (form_errors == "") return true;
   return false;
}


	







