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;
}

// This is the function that performs form verification. It will be invoked
// from the onSubmit() event handler. The handler should return whatever
// value this function returns.
function verify(f)
{
    var msg;
    var empty_fields = "";
    var errors = "";
    var email_addrs = new Array();
    var email_count = 0;

    // Loop through the elements of the form, looking for all 
    // text and textarea elements that don't have an "optional" property
    // defined. Then, check for fields that are empty and make a list of them.
    // Also, if any of these elements have a "min" or a "max" property defined,
    // then verify that they are numbers and that they are in the right range.
    // Put together error messages for fields that are wrong.
    
   for(var i = 0; i < f.length; i++) 
   {
     var e = f.elements[i];
             	
     if (((e.type == "text") || (e.type == "textarea") ||  (e.type =="select-one")) && !e.optional) 
     {
       // first check if the field is empty
       if ((e.value == null) || (e.value == "") || isblank(e.value)) 
       {
         empty_fields += "\n          " + e.realname;                		
         continue;
       }
            	
       if (e.email)
       {
         if(!validate_email(e.value)) errors += e.realname + " Must be a valid email address\n";            		
     	 else
     	 {
     	   email_addrs[email_count] = e.value;
      	   email_count++;
      	 }
	 continue;
       }
          
       if (e.numb)
       {
	  var result = checknumber(e.value);	
//	  document.write(result);
	
//          if(!checknumber(e.value))
          if(!result)
	  {
//	  document.write("errors");
//	    errors += e.realname + " Must be a number\n";
errors += e.realname + " Must be a number\n";
//	  document.write("errors");
	  }
       }
          
       if (e.ccnumb)
       {
	   var cc_result = checkcreditcard(e.value);
//	   document.write(cc_result);
	   if(!cc_result)
	   {
		errors += "Please provide a valid credit card number\n";
	   }
//         if(!checkcreditcard(e.value)) errors += e.realname + " Must be a valid credit card number\n";
       }
            	
       if (e.type =="select-one")
       {			
         if (e.selectedIndex == 0)
       	 {				
       	   empty_fields += "\n          " + e.realname;						
       	   continue;
       	 }
       }
     }	
   }



    // Now, if there were any errors, display the messages, and
    // return false to prevent the form from being submitted. 
    // Otherwise return true.

    //compare the emails
    var no_match = "";
    if(email_addrs[0] != email_addrs[1])
    {
	no_match = "\nThe email confirm does not match your email address";
    }

    if (!empty_fields && !errors && !no_match) return true;

    msg  = "______________________________________________________\n\n"
    msg += "The form was not submitted because of the following error(s).\n";
    msg += "Please correct these error(s) and re-submit.\n";
    msg += "______________________________________________________\n\n"

    if (empty_fields) 
    {
        msg += "- The following required field(s) are empty:" + empty_fields + "\n\n\n";

        if (errors) msg += "\n";
    }
    msg += errors;

    msg += no_match;	

    alert(msg);
    return false;
}

function validate_email(addr) {
	if(-1 == addr.indexOf("@")) { 
	       return false; 
	}
	var at_array = addr.split('@');
	if(at_array.length > 2) {
	       return false; 
	}
	if(-1 != addr.indexOf(",")) { 
	       return false; 
	}
	if(-1 != addr.indexOf("#")) { 
	       return false; 
	}
	if(-1 != addr.indexOf("!")) { 
		return false; 
	}
	if(-1 != addr.indexOf(" ")) { 
	       return false; 
	}
	if(-1 != addr.indexOf("(")) { 
	       return false; 
	}   
	if(-1 != addr.indexOf(")")) { 
	       return false; 
	}  
	if(-1 != addr.indexOf("[")) { 
	       return false; 
	}  
	if(-1 != addr.indexOf("]")) { 
	       return false; 
	}  
	if(-1 != addr.indexOf("{")) { 
	       return false; 
	}  
	if(-1 != addr.indexOf("}")) { 
	       return false; 
	}    
	if(addr.length == (addr.indexOf("@")+1) ) {
	       return false;
	}
	return true;
}


function checknumber(object_value)
{
    //Returns true if value is a number or is NULL
    //otherwise returns false	

    if (object_value.length == 0)
        return true;

    //Returns true if value is a number defined as
    //   having an optional leading + or -.
    //   having at most 1 decimal point.
    //   otherwise containing only the characters 0-9.
	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;

    //The first character can be + - .  blank or a digit.
	check_char = start_format.indexOf(object_value.charAt(0));
    //Was it a decimal?
	if (check_char == 1)
	    decimal = true;
	else if (check_char < 1)
		return false;
        
	//Remaining characters can be only . or a digit, but only one decimal.
	for (var i = 1; i < object_value.length; i++)
	{
//	  document.write("\n character"+i);

		check_char = number_format.indexOf(object_value.charAt(i));
		if (check_char < 0)
			return false;
		else if (check_char == 1)
		{
			if (decimal)		// Second decimal.
				return false;
			else
				decimal = true;
		}
		else if (check_char == 0)
		{
			if (decimal || digits)	
				trailing_blank = true;
        // ignore leading blanks

		}
	        else if (trailing_blank)
			return false;
		else
			digits = true;
	}	
    //All tests passed, so...
    return true;
}



function checkcreditcard(object_value)
{
	var white_space = " -";
	var creditcard_string="";
	var check_char;


    if (object_value.length == 0)
        return true;

	// squish out the white space
	for (var i = 0; i < object_value.length; i++)
	{
		check_char = white_space.indexOf(object_value.charAt(i));
		if (check_char < 0)
			creditcard_string += object_value.substring(i, (i + 1));
	}	

	// if all white space return error
    if (creditcard_string.length == 0)
        return false;
	 
	 	
	// make sure number is a valid integer
	if (creditcard_string.charAt(0) == "+")
        return false;

	if (!checknumber(creditcard_string))
		return false;

    // now check mod10

	var doubledigit = creditcard_string.length % 2 == 1 ? false : true;
	var checkdigit = 0;
	var tempdigit;

	for (var i = 0; i < creditcard_string.length; i++)
	{
		tempdigit = eval(creditcard_string.charAt(i));

		if (doubledigit)
		{
			tempdigit *= 2;
			checkdigit += (tempdigit % 10);

			if ((tempdigit / 10) >= 1.0)
			{
				checkdigit++;
			}

			doubledigit = false;
		}
		else
		{
			checkdigit += tempdigit;
			doubledigit = true;
		}
	}	
	return (checkdigit % 10) == 0 ? true : false;

}
