function checkForm(theForm) {
 
 	var why = "";  
    
    if ( document.getElementById('individual-reg') ) {
    
		why += checkText(theForm.firstname.value, "your first name.");
		why += checkText(theForm.lastname.value, "your last name.");
		why += checkText(theForm.company.value, "a company.");
		why += checkText(theForm.title.value, "a title.");
		why += checkText(theForm.address.value, "an address.");
		why += checkText(theForm.city.value, 'a city');
		why += checkPhone(theForm.phone.value, 'a valid phone number');
		why += checkSelect(theForm.state.value, 'state'); 
		why += checkEmail(theForm.email.value);
	
		var chkarray = [	[ 'affiliation[]', 'affiliation' ] 	];
  		why += checkcheckArray(theForm, chkarray, "Please pick an ");
   
   		why += checkcheckBoxTerms(theForm.surcharge.checked, "You must accept the hotel surcharge terms before continuing to the next step!");
		
   
	}
	
	if ( document.getElementById('sponsor-reg') ) {
    
		why += checkText(theForm.name.value, "a main contact name.");
		why += checkText(theForm.company.value, "a company.");
		why += checkText(theForm.url.value, "a company url.");
		why += checkText(theForm.address.value, "an address.");
		why += checkText(theForm.city.value, 'a city');
		why += checkPhone(theForm.phone.value, 'a valid phone number');
		why += checkSelect(theForm.state.value, 'state'); 
		why += checkEmail(theForm.email.value);
	
		var chkarray = [ [ 'reg_type[]', 'sponsorship level' ] ];
   		why += checkcheckArray(theForm, chkarray, "Please pick a ");
   		
   		var chkarray = [ [ 'act_type[]', 'activity type' ] ];
   		why += checkcheckArray(theForm, chkarray, "Please pick your preferred ");
   	
	}
	
	if ( document.getElementById('techcenter-reg') ) {
    
		why += checkText(theForm.name.value, "a main contact name.");
		why += checkText(theForm.company.value, "a company.");
		why += checkText(theForm.address.value, "an address.");
		why += checkText(theForm.city.value, 'a city');
		why += checkPhone(theForm.phone.value, 'a valid phone number');
		why += checkSelect(theForm.state.value, 'state'); 
		why += checkEmail(theForm.email.value);
	
	}
	
	if ( document.getElementById('buzun-reg') ) {
    
		why += checkText(theForm.name.value, "a main contact name.");
		why += checkText(theForm.company.value, "a company.");
		why += checkText(theForm.address.value, "an address.");
		why += checkText(theForm.city.value, 'a city');
		why += checkPhone(theForm.phone.value, 'a valid phone number');
		why += checkSelect(theForm.state.value, 'state'); 
		why += checkEmail(theForm.email.value);
		
		var chkarray = [ 
		[ 'affiliation[]', 'company type (1)' ]
		];
		why += checkcheckArray(theForm, chkarray, "Please choose a ");
		
		var companyType = (checkcheckArrayVal(theForm, chkarray));
		
		why += checkRadios(theForm, 'yn_representative',  "Please choose whether you have a representative (2)");
		
		why += checkRadios(theForm, 'yn_db',  "Please choose whether your company utilizes an enterprise data management system (3)");
		
		why += checkRadios(theForm, 'yn_review',  "Please choose whether your company participates in a data synchronization and/or data quality review program (4)");
		
		var chkarray = [ 
		[ 'pricing_data[]', 'way you obtain product and pricing data (5)' ]
		];
   		why += checkcheckArray(theForm, chkarray, "Please choose a ");
   		
   		var otherType = (checkcheckArrayVal(theForm, chkarray));
   		
   		if (otherType == "Other") {
			why += checkText(theForm.other_pricing_data.value, 'the other way you obtain product and pricing data (5)');
		}
   	
		why += checkSelect(theForm.frequency.value, 'frequency of uploads/downloads (5)'); 
	
		var chkarray = [ 
		[ 'information_type[]', 'the type of information you submit (6)' ],
		[ 'business_transactions[]', 'method for business transactions (7)' ]
		];
		
		var chkarray1 = [ 
		[ 'information_type[]', 'the type of information you submit (6)' ] ];
		
		var chkarray2 = [ 
		[ 'business_transactions[]', 'method for business transactions (7)' ]
		];
		
		var otherType = (checkcheckArrayVal(theForm, chkarray1));
		
		if (otherType == "Other") {
			why += checkText(theForm.other_information_type.value, 'the other type of information you submit/obtain from IDW (6)');
		}
		
		var otherType = (checkcheckArrayVal(theForm, chkarray2));
		
		if (otherType == "Other") {
			why += checkText(theForm.other_business_transactions.value, 'the other method for business transactions (7)');
		}
		
   		why += checkcheckArray(theForm, chkarray, "Please choose a ");
   	
		
   		why += checkRadios(theForm, 'yn_edi_pro',  "Please choose whether your company supports EDI Pro standards? (8)");
		
		why += checkSelect(theForm.accept.value, 'percentage of your business documents (9)'); 
	
		why += checkRadios(theForm, 'yn_implement',  "Please choose whether your your company offers a program(s) to encourage your trading partners' participation  (10)");
		
		var chkarray = [ 
		[ 'services_data[]', 'service(s) do you offer through your company\'s web site (11)' ] ]
   		why += checkcheckArray(theForm, chkarray, "Please choose a ");
   		
   		var otherType = (checkcheckArrayVal(theForm, chkarray));
   		
   		if (otherType == "Other") {
			why += checkText(theForm.other_services_data.value, 'the other service(s) do you offer (11)');
		}
   		
   		//why += checkText(theForm.website.value, 'Company web site URL (11)');
   		
   		
   		if ( companyType == "Manufacturer/Supplier" ) {
   		
   			var chkarray = [ 
			[ 'manufacturer_store[]', 'whether you store UNSPSC codes in your business system. (12)' ]
			];
			
			why += checkcheckArray(theForm, chkarray, "Please choose ");
   			
   		} else if ( companyType == "Distributor" ) {
   		
   			why += checkRadios(theForm, 'distributors_codes_yn',  "Please choose whether you use UNSPSC codes in your business system (12)");
   			
   		} 
   		
   		why += checkRadios(theForm, 'yn_ecommerce_activity',  "Please choose whether your company complies with GS1 Global Data Synchronization Network (GDSN) standards. (13)");
   		
   		why += checkRadios(theForm, 'yn_ecommerce_activity',  "Please choose whether your company has been recognized for eCommerce activity. (15)");
	
		//why += checkText(theForm.top_trading_one.value, 'a top trading partner (16)');
	}
	
    if (why != "") {
       alert(why);
       return false;
    }
 
	return true;
}

function checkcheckArray(theForm, chkarray, errorMsg) {
	
	var checkvalue = "";
   	var error = "";
   	
   	for(x=0; x < chkarray.length; x++)
			{
			
				for (i=0, n=theForm[chkarray[x][0]].length; i<n; i++) {
				 if (theForm[chkarray[x][0]][i].checked) {
					  var checkvalue = theForm[chkarray[x][0]][i].value;
					  break;
				   }
				}
				error += checkcheckBox(checkvalue, chkarray[x][1], errorMsg);
			}
			
	return error;
}

function checkcheckArrayVal(theForm, chkarray) {
	
	var checkvalue = "";
   	var error = "";
   	
   	for(x=0; x < chkarray.length; x++)
			{
			
				for (i=0, n=theForm[chkarray[x][0]].length; i<n; i++) {
				 if (theForm[chkarray[x][0]][i].checked) {
					  var checkvalue = theForm[chkarray[x][0]][i].value;
					  break;
				   }
				}
				
			}
			
	return checkvalue;
}


function checkcheckBox(checkvalue, fields, errorMsg) {
	var error = "";
	if (!errorMsg) errorMsg = "Pick at least one "
	   if (!(checkvalue)) {
		   error = errorMsg + fields + "\n";
		}
	return error;    
}

function checkcheckBoxTerms(checkvalue, fields, errorMsg) {
	var error = "";
	if (!errorMsg) errorMsg = ""
	   if (!(checkvalue)) {
		   error = errorMsg + fields + "\n";
		}
	return error;    
}

function checkEmail (strng) {
var error="";
if (strng == "") {
   error = "Please enter a valid email address.\n";
}

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       error = "Please enter a valid email address.\n";
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          error = "The email address contains illegal characters.\n";
       }
    }
return error;    
}

function checkText (strng, formfield) {
 var error = "";
 if (strng == "") {
    error = "Please enter " + formfield + "\n";
 }
 return error;
 }

function checkSelect (strng, formfield) {
 var error = "";
 if (strng == "" || strng == 'choose' ) {
    error = "Please select a " + formfield + "\n";
 }
 return error;
 }



function checkPhone (strng) {
 	
 	var error = "";
 
	if (strng == "") {
	   error = "Please enter a daytime phone number.\n";
	   return error;
	}

 
	var stripped = strng.replace(/[\(\)\.\-\ ]/g, '');
	//strip out acceptable non-numeric characters
	
	if (isNaN(parseInt(stripped))) {
	 	error = "The phone number contains illegal characters.";
	 
	}
	
	if (!(stripped.length == 10)) {
		error = "The phone number is not a complete phone number with area code.\n";
	}
 
 	return error;
 }


function checkDropdown(choice, strng) {
    var error = "";
    if (choice == "") {
       error = strng + "\n";
    }    
return error;
}   

function checkRadios(theForm, radioSet, errorMsg) {
	
	var radios = theForm[radioSet];
    var error = "";
	
	for(var y = 0; y < radios.length; y++ ) {
		var isChecked = false;
		if (radios[y].checked) {
		 	return error;
		}
	}
	
	if (!isChecked) {
		error = errorMsg + "\n";
	}
	
	return error;
	
}


if( document.getElementById('individual-reg') ) {
	document.getElementById('individual-reg').onsubmit = function() { return checkForm(document.getElementById('individual-reg')) }
}

if( document.getElementById('sponsor-reg') ) {
	document.getElementById('sponsor-reg').onsubmit = function() { return checkForm(document.getElementById('sponsor-reg')) }
}

if( document.getElementById('techcenter-reg') ) {
	document.getElementById('techcenter-reg').onsubmit = function() { return checkForm(document.getElementById('techcenter-reg')) }
}

if( document.getElementById('buzun-reg') ) {
	document.getElementById('buzun-reg').onsubmit = function() { return checkForm(document.getElementById('buzun-reg')) }
}


