
var errorField = "";

function checkInvalidChar(str, message)	{
		var word=/;#<>"!\[\]\{\}/;
		var invalidchar=new RegExp(word);
		var flag2= str.match("[" + invalidchar + "]");
		if(flag2!=null) {
			return message + "(" + flag2 + ")\n";
		} else {
			return "";
		}
	}

	function validateCustomQuestions(form) {
		var ttmsg = '';

	
	
		  if (typeof form.custom_19 != "undefined") {
			if (form.custom_19.type != "hidden") {	
	
		
				if (form.custom_19.value == '') {
					ttmsg += "Please answer Job Function\n";
					errorField = form.custom_19;
					//form.custom_19.focus();
				}
	 }
		}		

	
	
		  if (typeof form.custom_8 != "undefined") {
			if (form.custom_8.type != "hidden") {	
	
		
				if (form.custom_8.value == '') {
					ttmsg += "Please answer Which of the following describe your primary role?\n";
					errorField = form.custom_8;
					//form.custom_8.focus();
				}
	 }
		}		

	
	
		  if (typeof form.custom_9 != "undefined") {
			if (form.custom_9.type != "hidden") {	
	
		
				if (form.custom_9.value == '') {
					ttmsg += "Please answer Which of the following best describes your organization's business?\n";
					errorField = form.custom_9;
					//form.custom_9.focus();
				}
	 }
		}		

	
	
		  if (typeof form.custom_10 != "undefined") {
			if (form.custom_10.type != "hidden") {	
	
		
				if (form.custom_10.value == '') {
					ttmsg += "Please answer What is your organization's annual revenue?\n";
					errorField = form.custom_10;
					//form.custom_10.focus();
				}
	 }
		}		

	
	
		  if (typeof form.custom_12 != "undefined") {
			if (form.custom_12.type != "hidden") {	
	
		
				if (form.custom_12.value == '') {
					ttmsg += "Please answer In what size organizations does your company target its sales efforts? (To make multiple selections: PC Users: Use Ctrl+click, MAC Users: Use Apple+click)\n";
					errorField = form.custom_12;
					//form.custom_12.focus();
				}
	 }
		}		

	
	
		  if (typeof form.custom_13 != "undefined") {
			if (form.custom_13.type != "hidden") {	
	
		
				if (form.custom_13.value == '') {
					ttmsg += "Please answer Which region(s) does your company target its sales efforts? (To make multiple selections: PC Users: Use Ctrl+click, MAC Users: Use Apple+click)\n";
					errorField = form.custom_13;
					//form.custom_13.focus();
				}
	 }
		}		

	
	
		  if (typeof form.custom_14 != "undefined") {
			if (form.custom_14.type != "hidden") {	
	
		
				if (form.custom_14.value == '') {
					ttmsg += "Please answer Which technologies does your company resell, integrate and/or consult on? (To make multiple selections: PC Users: Use Ctrl+click, MAC Users: Use Apple+click)\n";
					errorField = form.custom_14;
					//form.custom_14.focus();
				}
	 }
		}		

	
	
		  if (typeof form.custom_16 != "undefined") {
			if (form.custom_16.type != "hidden") {	
	
		
				if (form.custom_16.value == '') {
					ttmsg += "Please answer How many IT projects are you personally involved in annually (evaluation, recommendation, sales, implementation, support)?\n";
					errorField = form.custom_16;
					//form.custom_16.focus();
				}
	 }
		}		

	
	
		  if (typeof form.custom_23 != "undefined") {
			if (form.custom_23.type != "hidden") {	
	
		
				if (form.custom_23.value == '') {
					ttmsg += "Please answer Which vendors' products do you sell/implement? (To make multiple selections: PC Users: Use Ctrl+click, MAC Users: Use Apple+click)\n";
					errorField = form.custom_23;
					//form.custom_23.focus();
				}
	 }
		}		

	
	
		  if (typeof form.custom_18 != "undefined") {
			if (form.custom_18.type != "hidden") {	
	
		
				if (form.custom_18.value == '') {
					ttmsg += "Please answer Which distributors do you buy from? (To make multiple selections: PC Users: Use Ctrl+click, MAC Users: Use Apple+click)\n";
					errorField = form.custom_18;
					//form.custom_18.focus();
				}
	 }
		}		
 return ttmsg;
	}

	function validateRegistration(form, valOnly) {
		// valOnly is optional to return just error messages but not submit form
		var ttalert = '';
		var emailStatus = '';
		var errorField = '';
		var fieldCount = 0;
		var jsPhoneExprUS = '^[\(]?[0-9]{3}[\)]?[- ]?[ ]?[0-9]{3}[- ]?[0-9]{4}$';
		var jsPhoneExprCAN = '^[\(]?[0-9]{3}[\)]?[- ]?[ ]?[0-9]{3}[- ]?[0-9]{4}$';	
		var jsPhoneExprINTL = '^[\+]?[- 0-9()\/.]+$';
		var jsZipExprUS = '^[0-9]{5}([-]?[0-9]{4})?$'
		var jsZipExprCAN = '^[A-Za-z][0-9][A-Za-z] ?[0-9][A-Za-z][0-9]$'
		var jsZipExprINTL = '^[- 0-9A-Za-z]+$'	
		// var 'countryVal' is used by other javascript 
		var countryVal= ''
		if (typeof form.country != "undefined") {
			if (typeof form.country.options != "undefined") {
				countryVal = form.country.options[form.country.selectedIndex].value
			} else {
				if (typeof form.country.value != "undefined") {
					countryVal = form.country.value
				}
			}
		}
		// EMAIL ADDRESS
		if (typeof form.email != "undefined") {
			if (form.email.type != "hidden") {
				if (form.email.value == '') {
					ttalert += 'Please enter an Email Address.\n'
					errorField = (errorField == '') ? form.email : errorField;
				} else {
					emailStatus = checkEmail(form.email.value)
					if (emailStatus != 'good') {
						ttalert += emailStatus
						ttalert += '\n'
						errorField = (errorField == '') ? form.email : errorField;
					}
				}
			}
		}
	
		// PASSWORD
		if (typeof form.password != "undefined") {	
			if (form.password.type != "hidden") {	
				if (form.password.value == '') {
					ttalert += 'Please choose a Password.\n'
					errorField = (errorField == '') ? form.password : errorField;
				}
				if (typeof form.passwordConfirm != "undefined") {
					if (form.passwordConfirm.value == '') {
						ttalert += 'Please confirm Password.\n'
						errorField = (errorField == '') ? form.passwordConfirm : errorField;
					}
					if (form.password.value != '' && form.passwordConfirm.value != '' && form.password.value != form.passwordConfirm.value) {
						ttalert += 'Create Password and Confirm Password do not match.\n'
						errorField = (errorField == '') ? form.password : errorField;
					}
				}
			}
		}		
		// FIRST NAME
		if (typeof form.first_name != "undefined") {	
			if (form.first_name.type != "hidden") {	
				if (form.first_name.value == '') {
					ttalert += 'Please enter First Name.\n'
					errorField = (errorField == '') ? form.first_name : errorField;					
				} else {
					ttalert +=checkInvalidChar(form.first_name.value, "Invalid character in First Name.");
				}
			}
		}
		// LAST NAME
		if (typeof form.last_name != "undefined") {	
			if ((form.last_name.value == '') && (form.last_name.type != "hidden")) {
				ttalert += 'Please enter Last Name.\n'
				errorField = (errorField == '') ? form.last_name : errorField;						
			} else {
				ttalert +=checkInvalidChar(form.last_name.value, "Invalid character in Last Name.");
			}
		}
		// COMPANY
		if (typeof form.company != "undefined") {	
			if ((form.company.value == '') && (form.company.type != "hidden")) {
				ttalert += 'Please enter Company Name. \n'
				errorField = (errorField == '') ? form.company : errorField;	
			}
		}	
		// JOB TITLE (FREE-TEXT SPECIFIC TITLE)
		if (typeof form.specific_title != "undefined") {	
			if ((form.specific_title.value == '') && (form.specific_title.type != "hidden")) {
				ttalert += 'Please enter Job Title. \n'
				form.specific_title.focus()
			}
		}			
		// PHONE
		if (typeof form.phone != "undefined") {
			if (form.phone.type != "hidden") {		
				if (form.phone.value == '') {
					ttalert += 'Please enter Phone. \n'
					errorField = (errorField == '') ? form.phone : errorField;	
				} else if (countryVal == 'US') {
					if (form.phone.value.search(jsPhoneExprUS) == -1) {
						ttalert += 'Please enter a valid U.S. Phone Number.\n';
						errorField = (errorField == '') ? form.phone : errorField;	
					}
				} else if (countryVal == 'CA') {
					if (form.phone.value.search(jsPhoneExprCAN) == -1) {
						ttalert += 'Please enter a valid Canadian Phone Number.\n';
						errorField = (errorField == '') ? form.phone : errorField;	
					}
				} else {
					if (form.phone.value.search(jsPhoneExprINTL) == -1) {
						ttalert += 'Please enter a valid International Phone Number.\n';
						errorField = (errorField == '') ? form.phone : errorField;	
					}
				}	
			}
		}		
		// ADDRESS 1
		if (typeof form.address1 != "undefined") {		
			if ((form.address1.value == '') && (form.address1.type != "hidden")) {
				ttalert += 'Please enter Address Line 1.\n'
				errorField = (errorField == '') ? form.address1 : errorField;	
			}
		}
		// CITY
		if (typeof form.city != "undefined") {			
			if (form.city.type != "hidden") {
				if (form.city.value == '') {
					ttalert += 'Please enter a Town or City.\n'
					errorField = (errorField == '') ? form.city : errorField;	
				}
			}
		}
		// COUNTRY & STATE/PROV
		if (typeof form.country != "undefined") {
			if (typeof form.country.options != "undefined") {
				if (form.country.type != "hidden") {
					if (form.country.value == '' || form.country.options[form.country.selectedIndex].value == '') {
						ttalert += 'Please enter Country.\n';
						errorField = (errorField == '') ? form.country : errorField;	
					}
				}
			} 
		}
		if (typeof form.province_cd != "undefined") {
			if (typeof form.province_cd.options != "undefined") {
				if (form.province_cd.type != "hidden") {
					if (form.province_cd.length > 1 && (form.province_cd.value == '' || form.province_cd.options[form.province_cd.selectedIndex].value == '') && form.province.value == '') {
						ttalert += 'Please enter State or Province.\n'
						errorField = (errorField == '') ? form.province_cd : errorField;	
					}
				}
			}
		}
		// ZIP
		if (typeof form.zip != "undefined") {
			if (form.zip.type != "hidden") {
				if (form.zip.value.length > 10) {
					ttalert += 'Zip/Postal Code maximum length is 10 characters.\n';
				}
				if (countryVal == 'US') {
					if (form.zip.value.search(jsZipExprUS) == -1) {
						ttalert += 'Please enter a valid U.S. Zip Code.\n';
						errorField = (errorField == '') ? form.zip : errorField;	
					}
				} else if (countryVal == 'CA') {
					if (form.zip.value.search(jsZipExprCAN) == -1) {
						ttalert += 'Please enter a valid Canadian Postal Code.\n';
						errorField = (errorField == '') ? form.zip : errorField;	
					}
				} else { 
					if (form.zip.value.search(jsZipExprINTL) == -1) {
						ttalert += 'Please enter a valid Zip or Postal Code.\n';
						errorField = (errorField == '') ? form.zip : errorField;	
					}
				}
			}
		}
		// JOB TITLE & FUNCTION
		if (typeof form.title != "undefined") {
			if (form.title.type != "hidden") {			
				if (form.title.value == '') {
					ttalert += 'Please enter a Job Function.\n'
					errorField = (errorField == '') ? form.title : errorField;	
				}
			}
		}
		// JOB FUNCTION - NO LONGER USED
		if (typeof form.co_job_function != "undefined") {
			if (form.co_job_function.type != "hidden") {	
				if (form.co_job_function.value == '') {
					ttalert += 'Please enter a Job Function.\n'
					errorField = (errorField == '') ? form.co_job_function : errorField;						
				}
			}
		}
		// FAX
		if (typeof form.fax != "undefined") {	
			if (form.fax.type != "hidden") {			
				if (form.fax.value != '') {
					if (countryVal == 'US') {
						if (form.fax.value.search(jsPhoneExprUS) == -1) {
							ttalert += 'Please enter a valid U.S. Fax Number.\n';
							errorField = (errorField == '') ? form.fax : errorField;	
						}
					} else if (countryVal == 'CA') {
						if (form.fax.value.search(jsPhoneExprCAN) == -1) {
							ttalert += 'Please enter a valid Canadian Fax Number.\n';
							errorField = (errorField == '') ? form.fax : errorField;	
						}
					} else {
						if (form.fax.value.search(jsPhoneExprINTL) == -1) {
							ttalert += 'Please enter a valid International Phone Number.\n';
							errorField = (errorField == '') ? form.fax : errorField;	
						}
					}		
				}
			}
		}
		// BUDGET
		if (typeof form.co_budget != "undefined") {
			if (form.co_budget.type != "hidden") {			
				if (form.co_budget.value == '') {
					ttalert += 'Please answer the IT Budget question.\n'
					errorField = (errorField == '') ? form.co_budget : errorField;						
				}
			}
		}
		// EMPLOYEE COUNT
		if (typeof form.co_emp_count != "undefined") {
			if (form.co_emp_count.type != "hidden") {			
				if (form.co_emp_count.value == '') {
					ttalert += 'Please answer the Employees at Company question.\n'
					errorField = (errorField == '') ? form.co_emp_count : errorField;	
				}
			}
		} 
		// PURCHASING SCOPE
		if (typeof form.co_scope != "undefined") {
			if (form.co_scope.type != "hidden") {	
				if (form.co_scope.value == '') {
					// multi select list
					ttalert += 'Please answer the Role in Purchasing question.\n'
					errorField = (errorField == '') ? form.co_scope : errorField;					
				} else if (typeof form.co_scope.value == "undefined") {
					// must be array of checkboxes 
					var scopeCount = 0;
					for (i=0;i < form.co_scope.length; i++) {
						if(form.co_scope[i].checked) {
							scopeCount++;
						}
					}
					if (scopeCount == 0) {			
						ttalert += 'Please answer the Role in Purchasing question.\n'
						errorField = (errorField == '') ? form.co_scope : errorField;					
					}
				}
			}
		} 
		// INDUSTRY
		if (typeof form.co_industry != "undefined") {
			if (form.co_industry.type != "hidden") {			
				if (form.co_industry.value == '') {
					ttalert += 'Please answer the Industry question.\n'
					errorField = (errorField == '') ? form.co_industry_type : errorField;						
				}
			}
		}
		// CUSTOM QUESTIONS 
		if (typeof validateCustomQuestions == "function") {
			ttalert += validateCustomQuestions(form);
		}
		
		// MAGAZINE QUESTIONS
		if (typeof validateMagazines == "function") {
			ttalert += validateMagazines(form);
		}
		
		// IT MANANAGE
		if (typeof form.IT_MANAGE != "undefined") {				
			if (form.IT_MANAGE[0].checked == form.IT_MANAGE[1].checked) {
				ttalert += 'Please answer "Do you manage, evaluate, recommend, implement, support or purchase security products, software or services?"\n'
				errorField = (errorField == '') ? form.IT_MANAGE : errorField;	
			}
		}				
		
		// SECURITY-SPECIFIC QUESTIONS
		// BUDGET
		if (typeof form.BUDGET != "undefined") {					
			if (form.BUDGET.value == '' || form.BUDGET.options[form.BUDGET.selectedIndex].value == '') {
				ttalert += 'Please answer "Estimate the yearly security budget."\n';
				errorField = (errorField == '') ? form.BUDGET : errorField;					}
		}
		// PURCHASING ROLE
		if (typeof form.PURCHASING != "undefined") {				
			var roleCount = 0;
			for (i=0;i < form.PURCHASING.length; i++) {
				if(form.PURCHASING[i].checked) {
					roleCount++;
				}
			}		
			if (roleCount == 0) {
				ttalert += 'Please answer "What is your role in purchasing?".\n';
				errorField = (errorField == '') ? form.PURCHASING : errorField;	
			}			
		}
		// STORAGE-SPECIFIC QUESTIONS			

		// IT EXPENDITURE
		if (typeof form.IT_ANNUAL_EXPENDITURE != "undefined") {			
			if (form.IT_ANNUAL_EXPENDITURE.value == '' || form.IT_ANNUAL_EXPENDITURE.options[form.IT_ANNUAL_EXPENDITURE.selectedIndex].value == '') {
				ttalert += 'Please answer "What is your average annual expenditure on storage?"\n';
				errorField = (errorField == '') ? form.IT_ANNUAL_EXPENDITURE : errorField;	
			}
		}
		
		// PURCHASING ROLE
		if (typeof form.IT_SPECIFY != "undefined") {		
			var roleCount = 0;
			for (i=0;i < form.IT_SPECIFY.length; i++) {
				if(form.IT_SPECIFY[i].checked) {
					roleCount++;
				}
			}		
			if (roleCount == 0) {
				ttalert += 'Please answer "What is your role in purchasing?."\n';
				errorField = (errorField == '') ? form.IT_SPECIFY : errorField;					
			}			
		}
			
		// FINAL CHECK
		if (typeof valOnly != "undefined") {
			return ttalert;
		} else if (ttalert != "") {
			alert (ttalert);
			if (errorField != '') {
				errorField.focus();
			}
		} else {
			if (typeof form.contentURL != "undefined") {
				if (form.contentURL.value == '') {
					form.contentURL.value = window.location.href;		
				}
			}
			if (typeof form.fieldCount != "undefined") {
				// count form fields
				for (x=0;x < form.elements.length; x++) {
					if (form.elements[x].type != "hidden") {
						fieldCount++;
					}
				}
				
				 form.fieldCount.value = fieldCount;
			}
			form.submit();
		}
	}
