//----------------------------------------------------------
// DIGITAL ACID - POWER VALIDATOR
//----------------------------------------------------------


//----------------------------------------------------------
// COPYRIGHT NOTICE
//----------------------------------------------------------
// Copyright (c) 1999 Digital Acid (Timothy Reid Dumas)
// This is a licensed copy for BlindsWholesale, Inc.
// License #131E1047160D050F071A130C060B0C070000044D0A0B09

// This license is for use only on www.blindswholesale.com.
// To alter this license or purchase another please contact
// webmaster@digitalacid.com
//----------------------------------------------------------

var DA_POWER_VALIDATOR_SCRIPT_INCLUDED = true;

if ( typeof DA_POWER_VALIDATOR_CONFIG_INCLUDED == "undefined" )
{
	alert('                                      Fatal Error\n______________________________________________________\n\n        You need to include "daPowerValidator-Config.js"\n        before you can use "daPowerValidator-Script.js".\n\n______________________________________________________');
}

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 msg;
   var empty_fields = "";
   var no_selections = "";
   var unchecked_fields = "";
   var errors = "";
   var result = "";
   var foundAnError = 0;
   var o_p = null;
   var o_r = null;
   var isChecked = false;

   for(var i = 0; i < f.length; i++) {
      var e = f.elements[i];
      if(empty_fields || no_selections || errors) foundAnError = 1;
      if((foundAnError == 0) && (errorStop == 0)) foundAnError = 1;
      result = foundAnError & errorStop;
      if(!(foundAnError && errorStop)) {
         if(((e.type == "text") || (e.type == "textarea")) && !e.optional) {
            if((e.value == null) || (e.value == "") || isblank(e.value)) {
               if(e.dependant) {
                  var v = e.dependant;
                  var elObj = eval("document.forms[0].elements[\"" + v + "\"]");
                  var dependantVal = elObj.value
                  if(e.value == "") {
                     if(dependantVal != "") {
                        e.friendly?errors += "- Field " + e.friendly + " can not be left blank.\n\t":errors += "- Field " + e.name + " can not be left blank.\n\t";
                     }
                  }
                  continue;
               }
               e.friendly?empty_fields += "\n          " + e.friendly:empty_fields += "\n          " + e.name;
               continue;
            }
            if(e.numeric) {
               var v = parseFloat(e.value);
               if(isNaN(v) || ((e.min != null) && (v < e.min)) || ((e.max != null) && (v > e.max))) {
                  errors += "- The field " + e.name + " must be a number";
                  if(e.min != null)
                     errors += " that is greater than " + e.min;
                  if(e.max != null && e.min != null)
                     errors += " and less than " + e.max;
                  else if(e.max != null)
                     errors += " that is less than " + e.max;
                  errors += ".\n\n";
               }
            }
            if(e.zipcode) {
               var v = e.value;
               var l = v.length;
               var sa = v.slice(0,5);

               if(l>5) {
                  var sb = v.slice(6);
               }
               else {
                  var sb = 1;
               }
               if(isNaN(sa) || isNaN(sb) || (sa.length < 5) || (sb.length < 4)){
                  errors += "- The field " + e.name + " is not a valid zipcode\n\n";
               }
               continue;
            }
            if(e.areacode) {
               var v = e.value;
               var l = v.length;

               if(isNaN(v) || (l != 3)){
                  errors += "- The field " + e.name + " is not a valid area code.\n\n";
               }
               continue;
            }
            if(e.phone) {
               var v = e.value;
               var l = v.length;

               if(isNaN(v) || (l != 7)){
                  errors += "- The field " + e.name + " is not a valid phone number.\n  Please follow this format 5551212.\n\n";
               }
               continue;
            }
            if(e.state) {
               var v = e.value;
               var states = "AKAZARALCACOCTDEDCFLGAHIIDILINIAKSKYLAMEMDMAMIMSMOMTMNNENMNVNHNJNYNCNDOHOKORPARISCSDTNTXUTVTVAWAWVWIWY";

               v = v.toUpperCase();
               if(0 != (states.indexOf(v) % 2)) {
                  errors += "- The field " + e.name + " is not a valid state abbreviation.\n\n";
               }
               continue;
            }
            if(e.email) {
               var v = e.value;
               var pattern_a = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|(@.*[`~!#\$\%^\&\*\(\)+=_{}:;,<>\?]{1,})/;
               var pattern_b = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.((ARPA)|(COM)|(EDU)|(GOV)|(INT)|(MIL)|(NATO)|(NET)|(ORG)|(AG)|(AL)|(AQ)|(AR)|(AT)|(AU)|(BB)|(BE)|(BG)|(BO)|(BR)|(BS)|(BZ)|(CA)|(CH)|(CL)|(CR)|(CS)|(CN)|(CO)|(DE)|(DK)|(DM)|(DO)|(EC)|(EG)|(ES)|(FI)|(FR)|(GB)|(GR)|(HK)|(HU)|(IE)|(IL)|(IN)|(IS)|(IT)|(JM)|(JP)|(KM)|(KN)|(KR)|(LC)|(LK)|(LU)|(MX)|(MY)|(NA)|(NI)|(NL)|(NO)|(NZ)|(PE)|(PG)|(PH)|(PL)|(PR)|(PT)|(PY)|(RO)|(RU)|(SE)|(SG)|(SR)|(SU)|(TH)|(TN)|(TR)|(TT)|(TW)|(UK)|(US)|(UY)|(VC)|(VE)|(YU)|(ZA)|(ZW)|(arpa)|(com)|(edu)|(gov)|(int)|(mil)|(nato)|(net)|(org)|(ag)|(al)|(aq)|(ar)|(at)|(au)|(bb)|(be)|(bg)|(bo)|(br)|(bs)|(bz)|(ca)|(ch)|(cl)|(cr)|(cs)|(cn)|(co)|(de)|(dk)|(dm)|(do)|(ec)|(eg)|(es)|(fi)|(fr)|(gb)|(gr)|(hk)|(hu)|(ie)|(il)|(in)|(is)|(it)|(jm)|(jp)|(km)|(kn)|(kr)|(lc)|(lk)|(lu)|(mx)|(my)|(na)|(ni)|(nl)|(no)|(nz)|(pe)|(pg)|(ph)|(pl)|(pr)|(pt)|(py)|(ro)|(ru)|(se)|(sg)|(sr)|(su)|(th)|(tn)|(tr)|(tt)|(tw)|(uk)|(us)|(uy)|(vc)|(ve)|(yu)|(za)|(zw)|[0-9]{1,3})(\]?)$/;
               if(!document.layers) {
                  if(pattern_a.test(v)) {
                     errors += "- The field " + e.name + " is not a valid email address.\n\n";
                  }
                  if(!pattern_b.test(v)) {
                     errors += "- The field " + e.name + " is not a valid email address.\n\n";
                  }
               }
               continue;
            }
            if(e.card) {
               var v = e.value;
               var sum = 0;
               var weight = 0;
               var oddeven = v.length & 1;
               var cardlength = v.length;

               if( (cardlength < 12) || (cardlength > 16) ) {
                  errors += " that is between 13 and 16 digits in length.\n";
               }
               for (var count = 0; count < cardlength; count++) {
                  var digit = parseInt(v.charAt(count));
                  if(!((count & 1) ^ oddeven)) {
                     digit *= 2;
                     if(digit > 9) {
                        digit -= 9;
                     }
                  }
                  sum += digit;
               }
               if(sum % 10 != 0) {
                  errors += "- This card does not appear to be a valid card number.\n";
                  errors += "  Please reenter the number if you are still having\n";
                  errors += "  problems then you may " + contact_method + " us at " + contact_how + ".\n\n";
               }
               continue;
            }
         }
         if(((e.type == "select-one") || (e.type == "select")) && !e.optional)
         {
            //alert('selectedIndex'+e.selectedIndex);
            var eNum = e.selectedIndex;
            if (document.layers)
            {
               var eName = e.options[eNum].value;
            }
            else
            {
               eName = e.value;
            }
            //Select dependant on radio
            if(e.dependant)
            {
               var v = e.dependant;
               var elObj = eval(e.dependant);
               var dependantVal = elObj.checked;
               //alert(elObj.checked);
               if((eName == 0) && (dependantVal))
               {
                   no_selections += "\n          " + e.name;
               }
            }
            else
            {
                if (eName == 0) {
                   no_selections += "\n          " + e.name;
                }
            }
         }
         if(e.type == "checkbox") {
            if (e.checked == false) {
               unchecked_fields += "\n          " + e.name;
               continue;
            }
         }
         if(e.type == "radio") {
            //if (o_r != null) alert('type: '+e.type+'\nname: '+e.name+'\nvalue: '+e.value+'\nchecked: '+e.checked+'\n\nisChecked: '+isChecked+'\n\nold name: '+o_r.name+'\nold value: '+o_r.value+'\nold checked: '+o_r.checked);
            if((o_r != null)&&(o_r.name == e.name))
            {
                if(!isChecked)
                {
                    if(e.checked)
                    {
                        isChecked = true;
                    }
                }
            }
            else
            {
                if((o_r != null)&&(!isChecked))
                {
                    unchecked_fields += "\n          " + o_r.name;
                }
                o_r = e;
                isChecked = false;
                if(e.checked)
                {
                    isChecked = true;
                }
                continue;
            }
         }
         if(e.type == "password") {
            if(e.name == "password1") {
                o_p = e;
            }
            if(e.name == "password2") {
               if(o_p.value != e.value) {
                   errors += "- The passwords you have entered do not match. Please reenter them.\n";
               }
               else if(o_p.value == "") {
                   empty_fields += "\n          " + o_p.name;
               }
            }
         }
      }
   }
   // Radio clean up
   if((o_r != null)&&(!isChecked))
   {
       unchecked_fields += "\n          " + o_r.name;
   }
   if(!empty_fields && !no_selections && !unchecked_fields && !errors) {
      if(win_alrt == 1) { alert("validation passed"); }
      return true;
   }
   msg = "The form was not submitted because of the following error(s).\n";
   msg += "______________________________________________________\n\n";

   if (empty_fields) {
      msg += "- The following required field(s) are empty:" + empty_fields + "\n";
      if(errors) msg += "\n";
   }
   if (no_selections) {
      msg += "- You have not made a selection from a required list box(es):" + no_selections + "\n";
      if(errors) msg += "\n";
   }
   if (unchecked_fields) {
      msg += "- You have not checked the following required item(s):" + unchecked_fields + "\n";
      if(errors) msg += "\n";
   }
   msg += errors;
   alert(msg);
   return false;
}