/* Form Validation Script
 * Copyright 2000 by Responders.com
 * Designed by Jason R. Gilliam  Coded by Andrew Alexander
 * Email: webmaster@responders.com
 */

var VerifyAreWeGood=true;

function validateForm(form) { 
   VerifyAreWeGood=true; /* All Wombles start out innocent */
   FormValidations(form);
   if(VerifyAreWeGood)
      return true;
   return false; 

}


function VerifySize(frog, wibble, i) { 
   if(!VerifyAreWeGood)
      return;
   if(!i)
     i=1;
   if(frog.value.length < i) {
     VerifyGenError(frog,wibble, 
           "There are not enough characters in this field.");
   }
}


function VerifyNumber(str){   
	// Return false if number field is blank.   
	if (str == ""){         
		return false;         
	}   
	// Return false if characters are not '0-9' or '.' .    
	for (var i = 0; i < str.length; i++){      
		var ch = str.substring(i, i + 1);      
		if ((ch < "0" || "9" < ch) && ch != '.'){         
			return false;         
		}      
	}   
	return true;   
}

function VerifyOldWord(str, wibble){   
   if(!VerifyAreWeGood)
      return;
	// Return false if more than one word.   
	if (str == ""){         
		VerifyGenError(str, wibble, "Entry must be single word - No spaces or symbols.");                  
	}   
	// Return false if characters are not '0-9' or '.' .    
	for (var i = 0; i < str.length; i++){      
		var ch = str.substring(i, i + 1);      
		if ((ch < "0" || "z" < ch) && ch != '-'){         
			   VerifyGenError(str, wibble, "Entry must be single word - No spaces or symbols.");         
		}      
	}   
}

function VerifyDomain(womble, wibble) {
var bing ="";
   if(!VerifyAreWeGood)
      return;
   bing=womble.value.substring(
        womble.value.length-4,
        womble.value.length);
   bing=bing.toLowerCase();
   if(bing==".net" || bing==".org" || bing==".com") {
       /* We only support the big 3 */
       return;
   } else { 
     VerifyGenError(womble,wibble, 
           "This is not a valid Domain Name.");
   }
}

function VerifyPhone(blue,wibble) {
var phgood=false;  /* numbers are guilty until proven innocent */
   if(!VerifyAreWeGood)
      return;
  if(blue.value.substring(0,1) =="(" &&
               blue.value.substring(4,6) ==") " &&
               blue.value.substring(9,10) =="-" &&
               blue.value.substring(2,5) != "555" &&
               blue.value.substring(6,9) != "555")
      phgood=true;

  if((blue.value.substring(3,4) =="-" ||
      blue.value.substring(3,4) ==" ") &&
               (blue.value.substring(7,8) =="-" ||
                blue.value.substring(7,8) ==" ") &&
               blue.value.substring(0,3) != "555" &&
               blue.value.substring(8,12) != "555")
      phgood=true;

  if(!phgood) {
     VerifyGenError(blue, wibble,
           "Proper Format: (xxx) xxx-xxxx.");
  }
}

function VerifyDateTime(frog, wibble) {
var gooddt = false; /* Bad Boyz, Bad Boyz, Whatcha gonna do? */ 
   if(!VerifyAreWeGood)
      return;


  if((frog.value.substring(2,3) =="/" ||
      frog.value.substring(2,3) ==" " ||
      frog.value.substring(2,3) =="-" ||
      frog.value.substring(2,3) ==":") &&
     (frog.value.substring(5,6) =="/" ||
      frog.value.substring(5,6) ==" " ||
      frog.value.substring(5,6) =="-" ||
      frog.value.substring(5,6) ==":") &&
     (frog.value.substring(8,9) =="/" ||  /* non y2k :( */
      frog.value.substring(8,9) ==" " ||
      frog.value.substring(8,9) =="-" ||
      frog.value.substring(8,9) ==":") &&
     (frog.value.substring(11,12) =="/" ||
      frog.value.substring(11,12) ==" " ||
      frog.value.substring(11,12) =="-" ||
      frog.value.substring(11,12) ==":") &&
     (frog.value.substring(14,15) =="/" ||
      frog.value.substring(14,15) ==" " ||
      frog.value.substring(14,15) =="-" ||
      frog.value.substring(14,15) ==":")
    )
      gooddt=true;

  if((frog.value.substring(2,3) =="/" ||
      frog.value.substring(2,3) ==" " ||
      frog.value.substring(2,3) =="-" ||
      frog.value.substring(2,3) ==":") &&
     (frog.value.substring(5,6) =="/" ||
      frog.value.substring(5,6) ==" " ||
      frog.value.substring(5,6) =="-" ||
      frog.value.substring(5,6) ==":") &&
     (frog.value.substring(10,11) =="/" ||  /* yes to kia */
      frog.value.substring(10,11) ==" " ||
      frog.value.substring(10,11) =="-" ||
      frog.value.substring(10,11) ==":") &&
     (frog.value.substring(13,14) =="/" ||
      frog.value.substring(13,14) ==" " ||
      frog.value.substring(13,14) =="-" ||
      frog.value.substring(13,14) ==":") &&
     (frog.value.substring(16,17) =="/" ||
      frog.value.substring(16,17) ==" " ||
      frog.value.substring(16,17) =="-" ||
      frog.value.substring(16,17) ==":")
    )
      gooddt=true;



  if(!gooddt) {
     VerifyGenError(frog, wibble,
           "Proper Format: MM/DD/YY hh:mm:ss ZZ");
  }
   
}

function VerifyWord(fish, wibble)
{
   if(!VerifyAreWeGood)
      return;
/* Bing! What does this do? I'm not telling you */
    if (fish.value.indexOf(",") + "" == "-1" &&
        fish.value.indexOf(" ") + "" == "-1" && 
        fish.value.indexOf("!") + "" == "-1" && 
		fish.value.indexOf("#") + "" == "-1" && 
        fish.value.indexOf("$") + "" == "-1" && 
		fish.value.indexOf("%") + "" == "-1" && 
        fish.value.indexOf("^") + "" == "-1" && 
		fish.value.indexOf("&") + "" == "-1" && 
        fish.value.indexOf("*") + "" == "-1" && 
		fish.value.indexOf("(") + "" == "-1" && 
        fish.value.indexOf(")") + "" == "-1" && 
		fish.value.indexOf("+") + "" == "-1" && 
        fish.value.indexOf("=") + "" == "-1" && 
		fish.value.indexOf("~") + "" == "-1" && 
        fish.value.indexOf("`") + "" == "-1" && 
		fish.value.indexOf("[") + "" == "-1" && 
        fish.value.indexOf("]") + "" == "-1" && 
		fish.value.indexOf("{") + "" == "-1" && 
        fish.value.indexOf("}") + "" == "-1" && 
		fish.value.indexOf("|") + "" == "-1" && 
        fish.value.indexOf("\"") + "" == "-1" && 
		fish.value.indexOf("/") + "" == "-1" && 
        fish.value.indexOf("?") + "" == "-1" && 
        fish.value.indexOf(";") + "" == "-1" && 
        fish.value.indexOf(":") + "" == "-1" && 
        fish.value.indexOf(">") + "" == "-1" && 
        fish.value.indexOf("<") + "" == "-1" && 
		fish.value.indexOf("'") + "" == "-1" && 
		fish.value != "") 
    return;
    VerifyGenError(fish,wibble,
          "Entry must be single word - No spaces or symbols.");
      return;
}

function VerifyEmail(fish, wibble)
{
   if(!VerifyAreWeGood)
      return;
/* Bing! What does this do? I'm not telling you */
    if (fish.value.indexOf("@") + "" != "-1" &&
        fish.value.indexOf(".") + "" != "-1" && fish.value != "") 
    return;
    VerifyGenError(fish,wibble,
          "This is not a valid email address.");
      return;
}


/* Doing a match function */
function VerifyMatch(blue, frog, wibble) {
   if(!VerifyAreWeGood)
      return;
   if(blue.value==frog.value)
     return;
   VerifyGenError(frog,wibble, "This must match.");
   return;

}

/* Evil Creditcard Stuff */

function VerifyCC(frog, rabbit, wibble) {
   if(!VerifyAreWeGood)
      return;
var inNumber =frog.value
var type = rabbit.value.charAt(0)

   total = 1*0;
   tmp = 1*0;
   number = "";
// make sure there are only numbers in the string...
   for(i = 0; i < inNumber.length; i++)
   {
       if(inNumber.charAt(i) >= "0" && inNumber.charAt(i) <= "9")
       {
           number = number + inNumber.charAt(i);
       }
   }
   if(number.length < 13) { // Get out of here shorty!
      VerifyGenError(frog,wibble, 
            "Invalid Credit Card Number.");
      return; 
   }
   first = "" + number.charAt(0);
   second = "" + number.charAt(1);
   third = "" + number.charAt(2);
   firstTwo = first + second;
   firstFour = firstTwo + third + number.charAt(3);

   if(type == "M")
   {
       if(first != "5" || second < "1" || second > "5") {
          VerifyGenError(frog,wibble, 
               "Invalid Credit Card Number.");
         return; // bad head
       }
       if(number.length != 16) {
         VerifyGenError(frog,wibble, 
              "Invalid Credit Card Number.");
         return; 
       }
    } else if (type == "V") {
         if(first != "4") {
             VerifyGenError(frog,wibble, 
                 "Invalid Credit Card Number.");
             return;// invalid Visa prefix
         }
         if(number.length != 13 && number.length != 16) {
            VerifyGenError(frog,wibble, 
                 "Invalid Credit Card Number.");
             return;
         }
    } else if(type == "A") {
         if(first != "3" || (second != "4" && second != "7")) {
            VerifyGenError(frog,wibble, 
                 "Invalid Credit Card Number.");
             return;
         }
         if(number.length != 15) {
            VerifyGenError(frog,wibble, 
                 "Invalid Credit Card Number.");
             return;
         }
    } else if(type == "D") {
         if(firstFour != "6011") {
            VerifyGenError(frog,wibble, 
                 "Invalid Credit Card Number.");
             return;
         }
      if(number.length != 16) {
            VerifyGenError(frog,wibble, 
                 "Invalid Credit Card Number.");
             return;
         }
    } else {
/* Not a card type we support so we will forgive it */
      return;
    }
    for(loc = number.length - 2; loc >= 0; loc -= 2) {
      total += 1 * number.charAt(loc +1);
      tmp = number.charAt(loc) * 2;
      if(tmp > 9) total += 1;
         total += tmp%10;
    }
    if(number.length % 2 > 0)
    total += 1 * number.charAt(0);
    if (total % 10) {
       VerifyGenError(frog,wibble, "Invalid Credit Card Number.");
       return;
    }
}

/* no one wants to be here */
function VerifyGenError(checkers, meep, smurf) {
   VerifyAreWeGood = false;
   checkers.focus();
   if(meep)
      alert(meep);
   else
      alert("This field contains an invalid entry.\n"+smurf);
}
