function checkCheckboxes(stringToCheckFor, maximum) {
	var error = "";
	var elem = document.getElementById('frmMain').elements;
	var boxcounter = 0;
	for (var i = 0; i < elem.length; i++) {
		if ((elem[i].name.substring(0, 17) == stringToCheckFor) && (elem[i].checked)) {
			boxcounter++;
		}
	}
	if (boxcounter == 0) {
		error = "\n - between 1-" + maximum + " business categories.";
	}
	if (boxcounter > 3) {
		error = "\n - between 1-" + maximum + " business categories.";
	}	
	return error;
}

function checkOtherField (strng, errormsg) {
	 var error = "";
	 if (strng == "") {
		error = errormsg;
	 }
	 return error;
}

function checkTextField (strng, errormsg) {
 var error = "";
 if (strng == "") {
    error = errormsg;
 }
 return error;
}

function checkDropdown(choice, errormsg) {
    var error = "";
    if (choice == "NULL") {
       error = errormsg;
    }    
return error;
} 

function checkEmail (strng, errormsg) {
	var error = "";
	var emailFilter=/^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/;
	if (!(emailFilter.test(strng))) {
	       error = errormsg;
	}
	return error;
}
function checkPhone (strng, errormsg) {
	var error = "";
	var stripped = strng.replace(/[\(\)\.\-\ ]/g, '');
	//strip out acceptable non-numeric characters
	if (isNaN(parseInt(stripped))) {
	       error = errormsg;
	}
	return error;
}
function checkRadio (checkvalue, errormsg) {
	var error = "";
	if (!(checkvalue)) {
	   error = errormsg;
	}
	return error;
}

function CheckLikes() {
	boxes = document.f1.Liked.length;
	txt = "";
	for (i = 0; i < boxes; i++) {
		if (document.f1.Liked[i].checked) {
			txt = txt + document.f1.Liked[i].value + " ";
		}
	}

	if (txt == "") {
		Message = "No Boxes ticked";
	}
	else {
		Message = "";
	}
	return Message;
}

function checkManageProfileForm(theForm)  {
	var why = "";
	var why_errors = "";
	
	why_errors += checkTextField(theForm.business_name.value, "\n- business name");	
	why_errors += checkDropdown(theForm.business_type_ref.value, "\n- business type");
	why_errors += checkTextField(theForm.fname.value, "\n- your full name");
	why_errors += checkTextField(theForm.surname.value, "\n- your surname");
	why_errors += checkEmail(theForm.username.value, "\n- your email address (will be used as username)");
	why_errors += checkTextField(theForm.designation.value, "\n- your designation in the company");
	why_errors += checkTextField(theForm.physical.value, "\n- physical address");		
	why_errors += checkTextField(theForm.code_physical.value, "\n- physical postal code");	
	why_errors += checkTextField(theForm.postal.value, "\n- postal address");	
	why_errors += checkTextField(theForm.code_postal.value, "\n- physical postal code");	
	why_errors += checkPhone(theForm.telephone.value, "\n- telephone number");
	why_errors += checkTextField(theForm.products_services.value, "\n- main products / services");
	//why_errors += checkDropdown(theForm.payment_group_ref.value, "\n- payment type");
	
	//the following javascript checks that at maximum 3 and at least 1 checkbox has been selected
	why_errors += checkCheckboxes('business_category', '3');
	
	why_errors += checkTextField(theForm.contactperson1.value, "\n- at least one key contact person (name)");
	why_errors += checkEmail(theForm.contactemail1.value, "\n- at least one key contact person (email)");
	why_errors += checkPhone(theForm.contactcell1.value, "\n- at least one key contact person (telephone)");
	why_errors += checkTextField(theForm.num_employees.value, "\n- the number of employees");	
	
    if (why_errors != "") {
       why = "Please fill in:"+why_errors;
       alert(why);
       return false;
    }
    
}

function checkRegistrationForm(theForm)  {
	var why = "";
	var why_errors = "";
	
	why_errors += checkTextField(theForm.fname.value, "\n- your full name");	
	why_errors += checkTextField(theForm.surname.value, "\n- your surname");
	why_errors += checkEmail(theForm.username.value, "\n- your email address (will be used as username)");	
	why_errors += checkTextField(theForm.designation.value, "\n- your designation in the company");
	why_errors += checkTextField(theForm.business_name.value, "\n- business name");	
	for (i=0, n=theForm.business_type_ref.length; i<n; i++) {
	   if (theForm.business_type_ref[i].checked) {
	      var checkvalue = theForm.business_type_ref[i].value;
	      break;
	   }
	}
	why_errors += checkRadio(checkvalue, "\n- business type");
	why_errors += checkTextField(theForm.products_services.value, "\n- main products / services");
	why_errors += checkTextField(theForm.contactperson1.value, "\n- at least one key contact person (name)");
	why_errors += checkEmail(theForm.contactemail1.value, "\n- at least one key contact person (email)");
	why_errors += checkPhone(theForm.contactcell1.value, "\n- at least one key contact person (telephone)");
	why_errors += checkTextField(theForm.num_employees.value, "\n- the number of employees");	
	/*
	for (i=0, n=theForm.payment_group_ref.length; i<n; i++) {
	   if (theForm.payment_group_ref[i].checked) {
	      var checkvalue = theForm.payment_group_ref[i].value;
	      break;
	   }
	}
	why_errors += checkRadio(checkvalue, "\n- select your payment group");	
	*/
	why_errors += checkDropdown(theForm.howdidyouhear.value, "\n- how did you hear about the NSBC?");
    if (why_errors != "") {
       why = "Please fill in:"+why_errors;
       alert(why);
       return false;
    }
}

function checkEventsForm(theForm)  {
	var why = "";
	var why_errors = "";

	why_errors += checkTextField(theForm.event_title.value, "\n- event title");	
	why_errors += checkTextField(theForm.event_desc.value, "\n- event description");	
	why_errors += checkDropdown(theForm.event_status_ref.value, "\n- event status");
	
	if (why_errors != "") {
	why = "Please fill in:"+why_errors;
		alert(why);
		return false;
	}
    
}

function checkEnquiriesForm(theForm)  {
	var why = "";
	var why_errors = "";

	why_errors += checkTextField(theForm.name.value, "\n- your name");	
	why_errors += checkTextField(theForm.surname.value, "\n- your surname");	
	why_errors += checkEmail(theForm.epos.value, "\n- a valid email address");	
	why_errors += checkTextField(theForm.comment.value, "\n- your enqiry/comment");	
	
	if (why_errors != "") {
	why = "Please fill in:"+why_errors;
		alert(why);
		return false;
	}    
}

function checkBusinessAdvisorForm(theForm)  {
	var why = "";
	var why_errors = "";

	why_errors += checkTextField(theForm.name.value, "\n- your name");	
	why_errors += checkTextField(theForm.surname.value, "\n- your surname");	
	why_errors += checkEmail(theForm.epos.value, "\n- a valid email address");	
	why_errors += checkPhone(theForm.tel.value, "\n- a contact number (digits only)");
	why_errors += checkTextField(theForm.dob.value, "\n- your date of birth");	
	why_errors += checkDropdown(theForm.employed.value, "\n- whether or not you are currently employed");	
	why_errors += checkTextField(theForm.wherefrom.value, "\n- where you're from");	
	var checkvalue = "";
	for (i=0, n=theForm.geld.length; i<n; i++) {
	   if (theForm.geld[i].checked) {
		  checkvalue = theForm.geld[i].value;
		  break;
	   }
	}	
	why_errors += checkRadio(checkvalue, "\n- do you have R28 500.00 (excl VAT) to invest in your new business?");
	why_errors += checkTextField(theForm.number.value, "\n- the security string");

	
	if (why_errors != "") {
	why = "Please fill in:"+why_errors;
		alert(why);
		return false;
	}    
}

function checkDelegateBookingForm(theForm)  {
	var why = "";
	var why_errors = "";

	why_errors += checkTextField(theForm.name.value, "\n- your name");		
	why_errors += checkTextField(theForm.surname.value, "\n- your surname");	
	why_errors += checkPhone(theForm.cell.value, "\n- your cell number (digits only)");	
	why_errors += checkEmail(theForm.epos.value, "\n- a valid email address");	
	
	for (i=0, n=theForm.business_owner.length; i<n; i++) {
	   if (theForm.business_owner[i].checked) {
	      var checkvalue = theForm.business_owner[i].value;
	      break;
	   }
	}
	
	why_errors += checkRadio(checkvalue, "\n- are you a business owner?");
	for (i=0, n=theForm.starting_business.length; i<n; i++) {
	   if (theForm.starting_business[i].checked) {
	      var checkvalue = theForm.starting_business[i].value;
	      break;
	   }
	}

	why_errors += checkRadio(checkvalue, "\n- are you interested in starting a business?");	
	why_errors += checkDropdown(theForm.whichconference.value, "\n- the conference you'll be attending");
	why_errors += checkTextField(theForm.attendingbcos.value, "\n- your main purpose for attending?");	

		
	if (why_errors != "") {
	why = "Please fill in:"+why_errors;
		alert(why);
		return false;
	}    
}

function checkSubscriptionForm(theForm)  {
	var why = "";
	var why_errors = "";

	why_errors += checkTextField(theForm.name.value, "\n- your name");	
	why_errors += checkEmail(theForm.epos.value, "\n- a valid email address");	
	why_errors += checkTextField(theForm.location.value, "\n- the town/city you live in");	
	
	if (why_errors != "") {
	why = "Please fill in:"+why_errors;
		alert(why);
		return false;
	}    
}

function checkBuzzPartyForm(theForm)  {
	var why = "";
	var why_errors = "";

	why_errors += checkTextField(theForm.name.value, "\n- your name");		
	why_errors += checkTextField(theForm.surname.value, "\n- your surname");	
	why_errors += checkPhone(theForm.cell.value, "\n- your cell number (digits only)");	
	why_errors += checkEmail(theForm.epos.value, "\n- a valid email address");	
	why_errors += checkDropdown(theForm.whichparty.value, "\n- the BuzzParty you'll be attending");
	why_errors += checkDropdown(theForm.member.value, "\n- whether or not you're a member of the NSBC");
		
	if (why_errors != "") {
	why = "Please fill in:"+why_errors;
		alert(why);
		return false;
	}    
}