function setRequired(toggle,form,list)
{	
	var container = "initialvalue";
	var formObj = eval("document." + form);
	var elems = list.split(",");	
	var rr = eval("document." + form + ".radiorequired"); 
	//alert(rr.value);
	for (var i=0; i < elems.length; i++) {
		var field = eval("document." + form + "." + elems[i]);
		if (field.name != "intialvalue") {
		//loop through form fields and find radio button fields
		for	(var x = 0; x < formObj.length; x++) {
				var e = formObj.elements[x];		
				if (e.type == "radio"){
					// if the current requiredradio value (list item) is found in the form - perform the following...
					if (e.name == elems[i])
					{
						// by default, we assume that the value is the first instance (2 instances for each button)
						var isNew = "yes";
						// check to see if this radio field has already been evaluated
						// 1st instances are added to container variable
						if (isInString(e.name,container)) {
							isNew = "no";
							// we don't process the item if so
						}
						// since this is the first instance, we need to to add or remove it from the
						// radiorequired list/value					
						if (isNew == "yes") {
							// if we find it in the list, we need to remove it
							if (isInString(elems[i],rr.value) && (toggle == "off")) {
								//remove from requiredradio	
								var rrvalues = rr.value.split(",");
								rr.value = "initialvalue";
								for	(var z = 0; z < rrvalues.length; z++) {
									if ((rrvalues[z] != elems[i]) && (rrvalues[z] != "intialvalue")) {
									rr.value = rr.value + "," + rrvalues[z];
									}
								}
							}
							// if it is not in the list, we need to add it
							else if (!isInString(elems[i],rr.value) && (toggle == "on")) {
								//add to requiredradio
								rr.value = rr.value + "," + elems[i];
							}
							container = container + "," + elems[i];			
							//alert(rr.value);
							//alert(container);				
						}					
					}
				}
			}			
			//alert(field.name);
			strClassObject = eval("document.getElementById('required_" + field.name + "')");
			if (toggle == "checkbox") {
				if (field.required == true) {
					strClassObject.className = "requiredoff";
					field.required = false; }
				else {
					strClassObject.className = "requiredon";
					field.required = true; }
			}
			else {
				if (toggle == "off") {
					strClassObject.className = "requiredoff";
					field.required = false; }
				else {
					strClassObject.className = "requiredon";
					field.required = true; }
			}
			//alert(strClassObject.className);
		}
	}
	return true;	
}	
// Remove all spaces from a string
function removeSpaces(string) {
	var newString = '';
	for (var i = 0; i < string.length; i++) {
		if (string.charAt(i) != ' ') newString += string.charAt(i);
	}
	return newString;
} 
// Check for null and for empty
function isFilled(elm) {
    if (elm.value == "" ||
        elm.value == null) 
    return false;
    else return true;
}
// A utility function that returns true if a string contains only whitespace characters.
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 allDigits(str)
{
	return inValidCharSet(str,"0123456789");
}
function isDigit(c) {
	return ((c >= '0') && (c <= '9'))
}	
function isLetter(c) {
	if (((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')))
		return true;
	return false;
}
//A utility function to test for numeric values
function isInt(elm) {
	if ((elm == "") || (elm == null)) return false;	
	if (!isNumeric(elm)) return false;		
	return true;
}
function isNameChar(elm)
{ 
  var valChar=" '-"
  for (var i=0; i < elm.length; i++) {
    var c=elm.charAt(i);
	if( valChar.indexOf(c) == -1 ) return false;
  }
  return true;
}
function isName(elm) 
	{
	if ((elm == "") || (elm == null)) return false;	
	for (var i = 0; i < elm.length; i++) 
		{
		if ((!isLetter(elm.charAt(i)) && !isNameChar(elm.charAt(i)))) return false;
		}
	return true;
}
//A utility function to test for alpha values
function isAlpha(elm) {
	if ((elm == "") || (elm == null)) return false;	
	for (var i = 0; i < elm.length; i++) {
		if (!isLetter(elm.charAt(i))) {return false;}
		}
	return true;
}
function isAlphaNum(elm) 
	{
	if ((elm == "") || (elm == null)) return false;	
	for (var i = 0; i < elm.length; i++) 
		{
		if ((!isLetter(elm.charAt(i)) && !isDigit(elm.charAt(i)))) return false;
		}
	return true;
}
//A utility function to test for graphic extensions (jpg, gif)
function isGraphic(elm) { 
	if ((elm == "") || (elm == null)) return false;	
	var extstr3 = elm.substring((elm.length - 3),elm.length); //get last 3 digits of field value
	var extstr4 = elm.substring((elm.length - 4),elm.length); //get last 3 digits of field value
	if ((extstr3 != "jpg") && (extstr4 != "jpeg") && (extstr3 != "gif") && (extstr3 != "png") && (extstr3 != "JPG") && (extstr4 != "JPEG") && (extstr3 != "GIF") && (extstr3 != "PNG")) return false;
	return true;
}
function LuhnCheck(str) 
{
  var result = true;
  var sum = 0; 
  var mul = 1; 
  var strLen = str.length;
  for (i = 0; i < strLen; i++) 
  {
    var digit = str.substring(strLen-i-1,strLen-i);
    var tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
  if ((sum % 10) != 0)
    result = false;
  return result;
}
function isValidExpiration(elm)
{
	if ((elm == "") || (elm == null)) return false;
	if (elm.substr(2,1) != "/") return false;
	var elems = elm.split("/");
	var result = (elems.length == 2); // should be two components
	if (!isInt(elems[0]) || !isInt(elems[1])) result = false;
	if (elems[0].length != 2) result = false;
	if ((elems[1].length != 2) && (elems[1].length != 4)) result = false;
	//check for valid month
	var m = elems[0];
	if (elems[0].substr(0,1) == 0) m = elems[0].substr(1,1);
	m = parseInt(m);
	if ((m > 12) || (m < 1)) result = false;
	if (result) 
		{
			// set up initial variables
			var month = elems[0];
 			var year = elems[1];
			if (elems[1].length == 2)
			{
				// check the first digit of year - if zero, make year into second digit only
				var z = year.substr(0,1);
				if (z == 0) year = year.substr(1,1);
				// make year into integer and tack onto 2000 for date check
				year = parseInt(year);
				year = year + 2000;
			}
			var now = new Date();
 			var nowMonth = now.getMonth() + 1;
 			var nowYear = now.getFullYear();
			if (year > nowYear + 25) result = false;
			expired = (nowYear > year) || ((nowYear == year ) && (nowMonth > month));
			if (expired) result = false;
		}
	return result;
}
function isEmail(elm) {
     var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
     var regex = new RegExp(emailReg);
     return regex.test(elm);
  }
function isPhone(elm)
{ 
  var valChar='0123456789 -()+'
  for (var i=0; i < elm.length; i++) {
    var c=elm.charAt(i);
	if( valChar.indexOf(c) == -1 ) return false;
  }
  return true;
}
function isZip(zipcode)
	{
	zipcode = removeSpaces(zipcode);
	if (!(zipcode.length == 5 || zipcode.length == 9 || zipcode.length == 10 || zipcode.length == 6)) return false;
	if ((zipcode.length == 5 || zipcode.length == 9) && !isInt(zipcode)) return false;
	if (zipcode.length == 10 && zipcode.search && zipcode.search(/^\d{5}-\d{4}$/) == -1) return false;
	if (zipcode.length == 6 && !isAlphaNum(zipcode)) return false;
	return true;
	}
function isAnyAll(elm,form)
{
	var elems = elm.split(",");
	var start = 0;
	var end = elems.length;
	var counter = 0;
	for (var i=0; i < elems.length; i++) 
	{
		for	(var x = 0; x < form.length; x++) 
		{
			var name = form.elements[x].name;
			var value = form.elements[x].value;
			if (name == elems[i]) 
			{
				if ((value !=  "") && (value != null)) counter++;
			}
		}
	}
	if ((counter != start) && (counter != end))
	{
		return false;
	}
	return true;
}
function getRadioValue(radio) 
{
	for (index=0; index<radio.length; index++)
	{ 
		if (radio[index].checked) return radio[index].value; 
	}
	return null;
	}
function isInString(a,b)
{
	var elems = b.split(",");
	for (var i=0; i < elems.length; i++) {
		if (a == elems[i]) {
			return true;
		}
	}
	return false;
}	
function isMoney(elm) {
	//regex=/^[a,b,c,x,y,z]{3}\d{3}$/;
     var myexp = /^\d+\.\d{0,2}$/;
     var regex = new RegExp(myexp);
     return regex.test(elm);
}

// main function - validates the field names using id attribute on form inputs, id = message
// other forms of validation can be called by the validate="float" attribute 
// and the 'message="Message Text" 	
function isReady(form,submitalert) {
	var errors = "";
	var empty_fields = "";
	var num_fields = "";
	var alpha_fields = "";
	var namechar_fields = "";
	var graphic_fields = "";
	var creditcard_fields = "";
	var creditcardexp_fields = "";
	var email_fields = "";
	var money_fields = "";
	var phone_fields = "";
	var forceSelect_fields = ""; 
	var zip_fields = "";
	var anyall_fields = "";
	var msg;
	var checked = "initialvalue";
	var unchecked = "initialvalue";
	//alert("stop");
	// loop through form	
	for	(var i = 0; i < form.length; i++) {
		var e = form.elements[i];
		//can we use the name of the existing field to get the array of values for all fields with same name
		
		if (e.customError) {
			return false;
		}
		
		
		if (e.name.substr(0,11) == 'forceSelect') {
		
			var forceFields = e.value.split(',');
			var cartFields = document.getElementsByName('addtocart');
			var isSelection = false;
			
			//loop through cart fields and check the values against the forceSelect value list
			for	(var f = 0; f < forceFields.length; f++) {
				for	(var c = 0; c < cartFields.length; c++) {
					if ( ( forceFields[f] == cartFields[c].value ) && (cartFields[c].checked) ) {
						isSelection = true;
					}
				}
			}
			
			if ( !isSelection ) {
				forceSelect_fields += "\n" + e.id;
				errors = 1;
			}
			
		}
		
		if (e.type == "radio"){
			if (form.radiorequired){
				if	(isInString(e.name,form.radiorequired.value)) {
					if (e.checked == true){
						// put in checked container
						checked = checked + "," + e.id;
					}
					else {
						unchecked = unchecked + "," + e.id;
					}					
				}
			}			
		} else if (e.type == "checkbox"){		
			if (form.checkboxrequired){
				if	(isInString(e.name,form.checkboxrequired.value)) {
					if (e.checked != true){
						//alert(e.name);
						empty_fields += "\n" + e.id; 
						errors = 1;					
					}					
				}
			}
		} else if (isblank(e.value)) {
			// check for required fields
			if (e.required) { 
				empty_fields += "\n" + e.id; 
				errors = 1;
			}
		}
		if (!isblank(e.value)) {
			// check numeric values
			if	(e.numeric) {
				if	(!isInt(e.value)) {
					num_fields += "\n" + e.id;
					errors = 1;
				}
			}
		}
		if (!isblank(e.value)) {
			// check alpha values
			if	(e.alpha) {
				if	(!isAlpha(e.value)) {
					alpha_fields += "\n" + e.id;
					errors = 1;
				}
			}
		}
		if (!isblank(e.value)) {
			// check name values
			if	(e.namechar) {
				if	(!isName(e.value)) {
					namechar_fields += "\n" + e.id;
					errors = 1;
				}
			}
		}	
		if (!isblank(e.value)) {
			// check file extension values for graphics
			if	(e.graphic) {
				if	(!isGraphic(e.value)) {
					graphic_fields += "\n" + e.id;
					errors = 1;
				}
			}
		}		
		if (!isblank(e.value)) {
			// check for valid credit card number
			if	(e.creditcard) {
				if	(!LuhnCheck(e.value)) {
					creditcard_fields += "\n" + e.id;
					errors = 1;
				}
			}
		}		
		if (!isblank(e.value)) {
			// check for valid credit card expiration date
			if	(e.creditcardexpiration) {
				if	(!isValidExpiration(e.value)) {
					creditcardexp_fields += "\n" + e.id;
					errors = 1;
				}
			}
		}	
		if (!isblank(e.value)) {
			// check for valid email address
			if	(e.email) {
				if	(!isEmail(e.value)) {
					email_fields += "\n" + e.id;
					errors = 1;
				}
			}
		}
		if (!isblank(e.value)) {
			// check for valid dollar value
			if	(e.money) {
				if	(!isMoney(e.value)) {
					money_fields += "\n" + e.id;
					errors = 1;
				}
			}
		}
		if (!isblank(e.value)) {
			// check for valid phone number
			if	(e.phone) {
				if	(!isPhone(e.value)) {
					phone_fields += "\n" + e.id;
					errors = 1;
				}
			}
		}
		if (!isblank(e.value)) {
			// check for valid zip code
			if	(e.zip) {
				if	(!isZip(e.value)) {
					zip_fields += "\n" + e.id;
					errors = 1;
				}
			}
		}
		if (!isblank(e.value)) {
			// check to make sure a group of fields are filled out or left blank
			if	(e.anyall) {
				if	(!isAnyAll(e.value,form)) {
					anyall_fields += "\n" + e.id;
					errors = 1;
				}
			}
		}
	} //end for
	//make this smarter later - it should come in its place in the loop
	//split radio containers and add onto required strings
	var radioStr = unchecked.split(",");
	for (var i=0; i < radioStr.length; i++) {
		if (!isInString(radioStr[i],checked)) {
			empty_fields += "\n" + radioStr[i];
			errors = 1;
			checked = checked + "," + radioStr[i];//add error value into checked string so we don't get dups
		}
	}
    // If no errors, ask user not to submit form again then return true submitting form, 
	// otherwise, build and display error message
    if (!errors)
		{
		if (submitalert == "alerton") 
			alert("The information you have entered appears to be valid.\n\n Please wait for the information to be posted and do not click the submit button again. Thank you.");
		return true;
		}
	else
		{
		msg  = "The form was not submitted because of the following error(s).\n";
		msg += "Please correct these error(s) and re-submit.\n";
	    if (empty_fields) {
	        msg += "\n";
			msg += "* The following are required field(s) to submit this form:" 
				+ empty_fields + "\n";
		    }
		if (num_fields) {
			msg += "\n";
			msg += "* The following field(s) must be numeric:" 
			+ num_fields + "\n";	 
			}
		if (alpha_fields) {
			msg += "\n";
			msg += "* The following field(s) must be letters only:" 
			+ alpha_fields + "\n";	 
			}	
		if (namechar_fields) {
			msg += "\n";
			msg += "* The following field(s) must be composed of letters, dash, or apostrophe:" 
			+ namechar_fields + "\n";	 
			}					
		if (graphic_fields) {
			msg += "\n";
			msg += "* The following field(s) must be graphic files (.jpg .jpeg .gif .png):" 
			+ graphic_fields + "\n";	 
			}	
		if (creditcard_fields) {
			msg += "\n";
			msg += "* The following field(s) must be a valid credit card number:" 
			+ creditcard_fields + "\n";	 
			}
		if (creditcardexp_fields) {
			msg += "\n";
			msg += "* The following expiration date does not appear to be valid. Please "
			msg += "\n make sure that it has not expired and is formatted as MM/YY:" 
			+ creditcardexp_fields + "\n";
			}
		if (email_fields) {
			msg += "\n";
			msg += "* The following fields(s) must contain a valid email address:" 
			+ email_fields + "\n";	 
			}		
		if (money_fields) {
			msg += "\n";
			msg += "* The following fields(s) must contain a valid dollar amount (no dollar sign):"
			+ money_fields + "\n";	 
			}		
		if (phone_fields) {
			msg += "\n";
			msg += "* The following fields(s) must contain a valid phone number:" 
			+ phone_fields + "\n";	 
			}
		if (forceSelect_fields) {
			msg += "\n";
			msg += "* You are required to select a product:" 
			+ forceSelect_fields + "\n";	 
			}
		if (zip_fields) {
			msg += "\n";
			msg += "* The following fields(s) must contain a valid zip code (12345 or 12345-6789):" 
			+ zip_fields + "\n";	 
			}				
		if (anyall_fields) {
			msg += "\n";
			msg += "* The following sections(s) must filled out completely or left blank:" 
			+ anyall_fields + "\n";	 
			}
	    alert(msg);
	    return false;
		}
}

// Validate URL syntax for URL input fields
// This function should ensure that user enters map (and other) URL's in the format of
// "www.something.com" but it's not working - Can you incorporate it, or is this taken care of
// somewhere else in the above validation routines?
/*
	function checkURL() {
		if (document.forms[0].mapurl.value.indexOf("http") >= 0) {
			alert ("The URL you provided should NOT contain the 'http' prefix.");
			return false;
		}
		
		if ((document.forms[0].Website.value.length > 0) &&	(document.forms[0].mapurl.value.indexOf("www") != 0)) {
			alert ("The URL you provided should begin with the 'www' prefix.");
			return false;
		}
		return true;
	}
*/

