// Declaring required variables
	var digits = "0123456789";
	// non-digit characters which are allowed in phone numbers
	var phoneNumberDelimiters = "()- ";
	// characters which are allowed in international phone numbers
	// (a leading + is OK)
	var validWorldPhoneChars = phoneNumberDelimiters + "+";
	// Minimum no of digits in an international phone no.
	var minDigitsInIPhoneNumber = 10;


function checkMainForm(form) {
	
	var incomplete = 0;
	var alertMsg = "The following fields are required:\n";
	
	if(allTrim(form.name.value) == "") {
		incomplete++;
		alertMsg = alertMsg +"Name\n";
	}
	
	if(allTrim(form.title.value) == "") {
		incomplete++;
		alertMsg = alertMsg +"Title\n";
	}
	
	if(allTrim(form.company.value) == "") {
		incomplete++;
		alertMsg = alertMsg +"Company\n";
	}
	if(allTrim(form.address1.value) == "") {
		incomplete++;
		alertMsg = alertMsg +"Address\n";
	}
	if(allTrim(form.city.value) == "") {
		incomplete++;
		alertMsg = alertMsg +"City\n";
	}
	if(allTrim(form.state.value) == "") {
		incomplete++;
		alertMsg = alertMsg +"State\n";
	}
	if(allTrim(form.email.value) == "") {
		incomplete++;
		alertMsg = alertMsg +"Email\n";
	}
	
	if(allTrim(form.phone.value) == "") {
		incomplete++;
		alertMsg = alertMsg +"Phone\n";
	}
	
	
	
	if (incomplete > 0){
		alert(alertMsg);
		return false;
	}

	
	//Check Email for @, period after @ and text in between
	if (checkEmail(form.email.value) == false) {
		form.email.focus();
		return false;
	}
	
	
	//Phone Number Validation	
	if (checkInternationalPhone(form.phone.value)==false){
		alert("Please Enter a Valid Phone Number")
		form.phone.focus()
		return false
	}
	
		
	
	return true;
}

function checkEmail(addr) {
//  Will check for @, period after @ and text in between
	var in_space = addr.indexOf(" ");
	if (in_space != -1)
	{ alert ("E-mail address should contain no spaces and be of the form jdoe\@aol.com");
			return false;  }

	var len = addr.length;
	var alpha = addr.indexOf("@");
	var last_alpha = addr.lastIndexOf("@");

	if (alpha != last_alpha)
	 {  alert ("Invalid e-mail address. Should contain only one @ and be of the form jdoe\@aol.com");
			 return false; }

	// No @, in first position, or name too short
	if (alpha == -1 || alpha == 0 || len<6 )
	 {  alert ("Invalid e-mail address. Should contain an @ and be of the form jdoe\@aol.com");
			 return false; }

	var last_p = addr.lastIndexOf(".");
			// Be sure period at least two spaces after @, but not last char.
			
	if (last_p - alpha < 2 || last_p == (len - 1) )
		{  alert ("Invalid e-mail address. Should contain a period after the @ and be of the form jdoe\@aol.com");
				return false; }
}

function allTrim(cValue){
 var lDone=false;
 while (lDone==false){
  if (cValue.length==0) {return cValue;}
  if (cValue.indexOf(' ')==0){cValue=cValue.substring(1);lDone=false; continue;}
  else {lDone=true;}
  if (cValue.lastIndexOf(' ')==cValue.length-1){cValue=cValue.substring(0, cValue.length-1);lDone=false;continue;}
  else {lDone=true;}
 }
 return cValue;
}



function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

//Function to determine how many checkboxes where checked
function howManyChecked(whichForm,whichCheckBoxArray,myMin)

{
	var _countChecked = 0;
	var err = 0;
	/* iterate through all the elements in the checkbox array */
	for(i=0;i<document[whichForm][whichCheckBoxArray].length;i++)
	{
		/* and check to see if each is checked */
		if(document[whichForm][whichCheckBoxArray][i].checked==true)
			/* if it is, increment a counter */
			{ _countChecked++; }
	}
	if(_countChecked < myMin)
		{ 
			err = 1;
		}
	if (err == 1) { return false; }
}
 //function to Validate 1 radio button selected
function valButton(btn) {
var cnt = -1;
for (var i=0; i < btn.length; i++) {
   if (btn[i].checked) {cnt = i; i = btn.length;}
   }
if (cnt > -1) return btn[cnt].value;
else return null;
}

function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }
