/*
 * Modified by FusionNet Group
 * Blake Johnson 
 *
 */

function VerifyForm() {

  this.msgSeparator = '\n';
  this.blankValue   = '';
  this._gripes      = new Array();

  this._StateUSA = /^(A[AEKLPRSZ]|C[AOT]|DC|DE|FL|FM|GA|GU|HI|I[ADLN]|KS|KY|LA|M[ADEHINOPST]|N[CDEHJMVY]|O[HKR]|P[ARW]|RI|SC|SD|T[NTX]|UT|V[AIT]|W[AIVY])$/i;
  this._StateCan = /^(AB|BC|MB|N[BFST]|ON|PE|PQ|QC|SK|YT)$/i;
  this._ZipUSA   = /^\d{5}([- ]?\d{4})?$/;
  this._ZipCan   = /^[A-Z]\d[A-Z] ?\d[A-Z]\d$/i;
  this._PhoneNA  = /^\(?\d{3}\)?\D?\d{3}\D?\d{4}$/;
  this._Email    = /^([0-9a-z_&.+-]+!)*[0-9a-z_&.+-]+@(([0-9a-z]([0-9a-z-]*[0-9a-z])?\.)+[a-z]{2,3}|([0-9]{1,3}\.){3}[0-9]{1,3})$/i;
  this._EmailBad = /^(((postmaster|root|hostmaster|mailer-daemon|webmaster)@(adobe|frame)\.com)|.*@(.*\.(adobe|frame)\.com|localhost\.com|127\.0\.0\.1))$/i;
  this._Country = {
    us: /^(u\.?s\.?(a\.?)?|united states)$/i,
    ca: /^(ca\.?|can\.?|canada)$/i,
    jp: /^(jp\.?|japan|nippon)$/i,
    at: /^(at\.?|austria|osterreich)$/i,
    be: /^(be\.?|belgium|belgie)$/i,
    dk: /^(dk\.?|denmark|danmark)$/i,
    fi: /^(fi\.?|finland|suomi)$/i,
    fr: /^(fr\.?|france)$/i,
    de: /^(de\.?|germany|deutschland)$/i,
    gr: /^(gr\.?|greece|hellas)$/i,
    ie: /^(ie\.?|(republic of )?ireland|eire)$/i,
    it: /^(it\.?|italy|italia)$/i,
    lu: /^(lu\.?|luxembourg)$/i,
    nl: /^(nl\.?|(the )?netherlands|holland)$/i,
    pt: /^(pt\.?|portugal)$/i,
    es: /^(es\.?|spain|espana)$/i,
    se: /^(se\.?|sweden|sverige)$/i,
    uk: /^(u\.?k\.?|united kingdom|(great )?britain|england)$/i,
    no: /^(no\.?|norway|norge)$/i,
    ch: /^(ch\.?|switzerland.*)$/i
  };
  this._CC = {
    Visa: /^4\d{12}(\d{3})?$/,
    MasterCard: /^5[1-5]\d{14}$/,
    'American Express': /^3[47]\d{13}$/,
    Discover: /^6011\d{12}$/
  };

  this.addError      = _VerifyForm_addError;
  this.showErrors    = _VerifyForm_showErrors;
  this.hasValue      = _VerifyForm_hasValue;
  this.getValue      = _VerifyForm_getValue;
  this.getAllValues  = _VerifyForm_getAllValues;
  this.clearFirst    = _VerifyForm_clearFirst;
  this.clearLast     = _VerifyForm_clearLast;
  this.clearAll      = _VerifyForm_clearAll;
  this.setFirst      = _VerifyForm_setFirst;
  this.setLast       = _VerifyForm_setLast;
  this.setAll        = _VerifyForm_setAll;
  this.checkFirst    = _VerifyForm_checkFirst;
  this.checkLast     = _VerifyForm_checkLast;
  this.checkAll      = _VerifyForm_checkAll;
  this.uncheckFirst  = _VerifyForm_uncheckFirst;
  this.uncheckLast   = _VerifyForm_uncheckLast;
  this.uncheckAll    = _VerifyForm_uncheckAll;
  this._setField     = _VerifyForm__setField;
  this.validState    = _VerifyForm_validState;
  this.validZip      = _VerifyForm_validZip;
  this.validPhone    = _VerifyForm_validPhone;
  this.validEmail    = _VerifyForm_validEmail;
  this.getCountry    = _VerifyForm_getCountry;
  this.isNA          = _VerifyForm_isNA;
  this.isEU          = _VerifyForm_isEU;
  this.isUSA         = _VerifyForm_isUSA; // deprecated
  this.isCanada      = _VerifyForm_isCanada; // deprecated
  this.validCardType = _VerifyForm_validCardType;
  this.validCard     = _VerifyForm_validCard;
  this.getCardType   = _VerifyForm_getCardType;
  this._mod10        = _VerifyForm__mod10;
  this.checkDate     = _VerifyForm_checkDate;
  this.checkCardDate = _VerifyForm_checkCardDate;
  this.checkPw       = _VerifyForm_checkPw;
  this.validUser     = _VerifyForm_validUser;
  this.isNumber      = _VerifyForm_isNumber;
  return this;
}

function _VerifyForm_addError (field, msg) {
  if ( ! this._gripes.length) {
    if (field.all || field.focus)  field.focus();
    //if (field.value && (field.all || field.select)) field.select();
  }
  this._gripes[this._gripes.length] = msg;
}
function _VerifyForm_showErrors (head, foot) {
  if (this._gripes.length) {
    alert((head ? head + this.msgSeparator : '') + this._gripes.join(this.msgSeparator) + (foot ? this.msgSeparator + foot : ''));
    return false;
  }
  return true;
}

function _VerifyForm_hasValue (field) {
  if ( ! field.type && field.length ) {
    for (var i = 0; i < field.length; i++)
      if (field[i].type && this.hasValue(field[i]))
        return true;
    return false;
  }
  if (/select/.test(field.type))
    return (field.selectedIndex != -1 && (field.options[field.selectedIndex].value != this.blankValue));
  if (/(checkbox|radio)/.test(field.type))
    return ( field.checked && (field.value != this.blankValue) );
  if (/(button|reset|submit)/.test(field.type))
    return false;
  return (field.value != this.blankValue)
}
function _VerifyForm_getValue (field) {
  if ( ! field.type && field.length ) {
    for (var i = 0; i < field.length; i++) {
      if (field[i].type) {
        var value = this.getValue(field[i]);
        if (value) return value;
      }
    }
    return null;
  }
  if (/select/.test(field.type))
    return ( (field.selectedIndex != -1 && (field.options[field.selectedIndex].value != this.blankValue))
      ? field.options[field.selectedIndex].value : null );
  if (/(checkbox|radio)/.test(field.type))
    return ( (field.checked && (field.value != this.blankValue)) ? field.value : null );
  return ( ( ! /(button|reset|submit)/.test(field.type) && (field.value != this.blankValue)) ? field.value : null )
}
function _VerifyForm_getAllValues (field) {
  var arr = new Array();
  if ( ! field.type && field.length ) {
    var temparr;
    for (var i = 0; i < field.length; i++) {
      if (field[i].type) {
        temparr = this.getAllValues(field[i]);
        for (var j = 0; j < temparr.length; j++)
          arr[arr.length] = temparr[j];
      }
    }
  }
  else if (/select/.test(field.type)) {
    for (var i = 0; i < field.length; i++)
      if (field.options[i].selected && (field.options[i].value != this.blankValue))
        arr[arr.length] = field.options[i].value;
  }
  else if (/(checkbox|radio)/.test(field.type) && field.checked && (field.value != this.blankValue))
    arr[arr.length] = field.value;
  else if ( ! /(button|reset|submit)/.test(field.type) && (field.value != this.blankValue))
    arr[arr.length] = field.value;
  return arr;
}

function _VerifyForm_clearFirst (field, val) { return this._setField(field, 1, val, this.blankValue, true) }
function _VerifyForm_clearLast (field, val) { return this._setField(field, -1, val, this.blankValue, true) }
function _VerifyForm_clearAll (field, val) { return this._setField(field, 0, val, this.blankValue, true) }
function _VerifyForm_setFirst (field, toval, val) { return this._setField(field, 1, val, toval, true) }
function _VerifyForm_setLast (field, toval, val) { return this._setField(field, -1, val, toval, true) }
function _VerifyForm_setAll (field, toval, val) { return this._setField(field, 0, val, toval, true) }
function _VerifyForm_checkFirst (field, val) { return this._setField(field, 1, val, true, false) }
function _VerifyForm_checkLast (field, val) { return this._setField(field, -1, val, true, false) }
function _VerifyForm_checkAll (field, val) { return this._setField(field, 0, val, true, false) }
function _VerifyForm_uncheckFirst (field, val) { return this._setField(field, 1, val, false, false) }
function _VerifyForm_uncheckLast (field, val) { return this._setField(field, -1, val, false, false) }
function _VerifyForm_uncheckAll (field, val) { return this._setField(field, 0, val, false, false) }
function _VerifyForm__setField (field, whichway, val, toval, valueorcheck) {
  var retval = false;
  if ( ! field.type && field.length ) {
    for (var i = (whichway == 1 ? 0 : field.length - 1); i < field.length && i >= 0; i += (whichway || -1))
      if (field[i].type && this._setField(field[i], whichway, val, toval, valueorcheck))
        if (whichway) return true;
        else retval = true;
    return retval;
  }
  if (valueorcheck) {
    if (/(file|password|text)/.test(field.type) && (typeof val != 'string' || field.value == val)) {
      field.value = toval;
      return true;
    }
  }
  else if (/select/.test(field.type)) {
    for (var i = (whichway == 1 ? 0 : field.length - 1); i < field.length && i >= 0; i += (whichway || -1)) {
      if ( typeof val != 'string' || field.options[i].value == val) {
        field.options[i].selected = toval;
        if (whichway) return true;
        else retval = true;
      }
    }
    return retval;
  }
  else if (/(checkbox|radio)/.test(field.type) && (typeof val != 'string' || field.value == val)) {
    field.checked = toval;
    return true;
  }
  return false
}

function _VerifyForm_validState (s, c) { return ( this.isUSA(c) ? this._StateUSA.test(s) : (this.isCanada(c) ? this._StateCan.test(s) : true) ) }
function _VerifyForm_validZip (z, c) { return ( this.isUSA(c) ? this._ZipUSA.test(z)   : (this.isCanada(c) ? this._ZipCan.test(z)   : true) ) }
function _VerifyForm_validPhone (p, c) { return ( this.isNA(c)  ? this._PhoneNA.test(p)  : true ) }
function _VerifyForm_validEmail (e) { return ( this._Email.test(e) && ! this._EmailBad.test(e) ) }
function _VerifyForm_getCountry (c) {
  for (var i = 1; i < arguments.length; i++)
    if (this._Country[arguments[i]] && this._Country[arguments[i]].test(c))
      return arguments[i];
  return null;
}
function _VerifyForm_isNA (c) { return ( this.getCountry(c, 'us', 'ca') ) }
function _VerifyForm_isEU (c) { return ( this.getCountry(c, 'at', 'be', 'dk', 'fi', 'fr', 'de', 'gr', 'ie', 'it', 'lu', 'nl', 'pt', 'es', 'se', 'uk') ) }
function _VerifyForm_isUSA (c) { return ( this._Country.us.test(c) ) } // deprecated
function _VerifyForm_isCanada (c) { return ( this._Country.ca.test(c) ) } // deprecated

function _VerifyForm_validCardType (n, t) {
  return ( this._CC[t] && this._CC[t].test(n) && this._mod10(n) )
}
function _VerifyForm_validCard (n) {
  for (var t in this._CC) if (this._CC[t].test(n))
    return (this._mod10(n) ? t : null); return null
}
function _VerifyForm_getCardType (n) {
  for (var t in this._CC) if (this._CC[t].test(n)) return t; return null
}
function _VerifyForm__mod10 (num) {
  var sum = 0;
  for (var i = num.length - 1; i >= 0; i -= 2)
    sum += num.charAt(i) * 1 + num.charAt(i - 1) * 2 - (num.charAt(i - 1) >= 5 ? 9 : 0);
  return ( sum % 10 == 0 );
}

function _VerifyForm_checkDate(day, month, year) {
 
  var myDayStr = day.value;
var myMonthStr = month.value;
var myYearStr = year.value;
var myDateStr = myDayStr + ' ' + myMonthStr + ' ' + myYearStr;

/* Using form values, create a new date object
which looks like "Wed Jan 1 00:00:00 EST 1975". */
var myDate = new Date( myDateStr );

// Convert the date to a string so we can parse it.
var myDate_string = myDate.toGMTString();

/* Split the string at every space and put the values into an array so,
using the previous example, the first element in the array is "Wed", the
second element is "Jan", the third element is "1", etc. */
var myDate_array = myDate_string.split( ' ' );

/* If we entered "Feb 31, 1975" in the form, the "new Date()" function
converts the value to "Mar 3, 1975". Therefore, we compare the month
in the array with the month we entered into the form. If they match,
then the date is valid, otherwise, the date is NOT valid. */
if ( myDate_array[2] != myMonthStr ) {
  //alert( 'I\'m sorry, but "' + myDateStr + '" is NOT a valid date.' );
  return false;
} else {
  //alert( 'Congratulations! "' + myDateStr + '" IS a valid date.' );
return true;
 
}
}

function _VerifyForm_checkCardDate(month, year, now) {
 
var mynow = new Date(now);
var thismonth = new Date(mynow.getFullYear(), mynow.getMonth(), '01');
var PassedDateStr = '01 ' + month.value + ' ' + year.value;
var PassedDate = new Date( PassedDateStr );
if (PassedDate < thismonth) {
    return false;
} else {
    return true;
}
}

function _VerifyForm_checkPw(parm_password, parm_password2) {
password = parm_password.value;
password2 = parm_password2.value;

if (password != password2) {
//alert ("\nYou did not enter the same new password twice. Please re-enter your password.")
return false;
}
else return true;
}

function _VerifyForm_isNumber(num) {
var valid = "0123456789"
var ok = "yes";
var temp;
for (var i=0; i<num.length; i++) {
	temp = "" + num.substring(i, i+1);
	if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") { 
   return false;

}
else
  return true;
}


function _VerifyForm_validUser(field) {
var valid = "abcdefghijklmnopqrstuvwxyz_0123456789-"
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
	temp = "" + field.value.substring(i, i+1);
	if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") { 
   return false;
//alert("Invalid entry!  Only lower-case characters and numbers are accepted!");
//field.focus();
//field.select();
}
else
  return true;
}


function checkform (form) {
//setvar.value='10';
//if (form.remchanged == '1'){
  var v = new VerifyForm();

  if ( ! v.hasValue(form.first_name) )
	v.addError(form.first_name,   "First name is required (or we could just call you 'Jane' or 'John')");
  if ( ! v.hasValue(form.last_name) )
	v.addError(form.last_name,   "Last name is required (or we'll assume 'Doe')");
  if ( ! v.hasValue(form.address1) )
	v.addError(form.address1,   "Street address is required");
  if ( ! v.hasValue(form.city) )
	v.addError(form.city,     "City is required");
  if ( ! v.hasValue(form.state) )
	v.addError(form.state,     "State is required");	
  if ( ! v.hasValue(form.zip) )
	v.addError(form.zip,     "Zip code is required");
  if ( ! v.hasValue(form.day_phone) )
	v.addError(form.day_phone,     "Day Phone is required");	
  if ( ! v.hasValue(form.email) )
	v.addError(form.email,     "E-mail address is required");
  if ( ! v.hasValue(form.email) )
	v.addError(form.email_confirm,     "E-mail address confirmation is required");
  if ( ! v.hasValue(form.email_confirm) )
	v.addError(form.email_confirm,     "Please re-enter your e-mail");

if ( v.hasValue(form.email) && ! v.validEmail(v.getValue(form.email)) )
	v.addError(form.email,     "E-mail: Your e-mail address doesn't seem to be correct--can you check it again?");

if( form.email.value != form.email_confirm.value ) {
	v.addError(form.email_confirm,     "Please ensure your e-mail addresses match");
}
if( form.password.value != form.password_confirm.value ) {
	v.addError(form.password_confirm,     "Please ensure your passwords match");
}
  return v.showErrors('Oops! Your inquiry information has some problems:\n','\nPlease complete all required information in the form.');
//}
//return true;
}

function checkform_checkout( form ) {
  var v = new VerifyForm();

//  alert(form);
//  alert("got here");
  if ( ! v.hasValue(form.firstName) )
	v.addError(form.name,   "Please include your first name so we can address you properly");

  if ( ! v.hasValue(form.lastName) )
	v.addError(form.name,   "Please include your last name so we can address you properly");

  if ( ! v.hasValue(form.phone1) )
	v.addError(form.phone1,     "We can't get in touch with you if we don't have your phone number");

  if ( ! v.hasValue(form.email) )
	v.addError(form.email,     "We can't get in touch with you if we don't have your e-mail address");

  if ( ! v.hasValue(form.schoolName) )
	v.addError(form.schoolName,     "Don't forget your school or business name");

  if ( ! v.hasValue(form.address1) )
	v.addError(form.address1,     "Don't forget your address");

  if ( ! v.hasValue(form.city) )
	v.addError(form.city,     "Don't forget to include which city you're in");

  if ( ! v.hasValue(form.state) )
	v.addError(form.state,     "Don't forget to include which state you're in");

  if ( ! v.hasValue(form.zip) )
	v.addError(form.zip,     "Don't forget to include your zip/postal code");


  if ( v.hasValue(form.email) && ! v.validEmail(v.getValue(form.email)) )
	v.addError(form.email,     "E-mail: Your e-mail address doesn't seem to be correct--can you check it again?");

/*  if ( v.hasValue(form.phone1) && ! v.validPhone( form.phone1, "USA" ) )
	v.addError(form.phone1,     "Phone: Your phone number doesn't seem to be correct--can you check it again?");
*/
return v.showErrors('Oops! Your registration information has some problems:\n','');
}


