﻿var submitCount=0; //global var to store the humber of times the form has been submitted

function validateFormOnSubmit(form) {

submitCount++;
//alert(submitCount)

var reason = "";
	reason += validateEmpty2(form.ScreenTitle,"ScreenTitle");
	reason += validateEmpty2(form.Title,"Title");
	reason += validateEmpty2(form.salutation,"title");
	reason += validateEmpty2(form.realname,"name");
	reason += validateEmpty2(form.jobtitle,"job title");
	reason += validateEmpty2(form.company,"organisation");
  	reason += validateEmail(form.email);
  	reason += validateNumber(form.phone);
  	reason += validateCheckBox(form.dataAgreement);
    reason += validateCheckBox2(form.agree);
   	reason += validateRadio(form.permit);
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

function validateCheckBox2(fld) {
	var error = "";
 if (!fld) return error; 	
	if (submitCount>0) {
		if (fld.checked == 0) {
			fld.style.background = 'Red'; 
			error = "  - Please agree to make this presentation available\n"
		} else {
			fld.style.background = '';
		}
	}
	return error; 
}



function validateCheckBox(fld) {
	var error = "";
 if (!fld) return error; 	
	if (submitCount>0) {
		if (fld.checked == 0) {
			fld.style.background = 'Red'; 
			error = "  - Please agree to the data protection statement\n"
		} else {
			fld.style.background = '';
		}
	}
	return error; 
}


function validateEmpty(fld) {
	validateEmpty2(fld,"");
}



function validateEmpty2(fld,description) {
    var error = "";
   if (!fld) return error; 
	if (submitCount>0) {
	    if (fld.value.length == 0) {
	        fld.style.background = 'Red'; 
	        error = "  - The " + description + " field is required\n"
	    } else {
	        fld.style.background = '';
	    }
    }
    return error;  
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function trimSpaces(s)
{
  return s.replace(/\s+/g, '');
}

function validateEmail(fld) {
    var error="";
 if (!fld) return error;    
   if (submitCount>0) {
	    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
	    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
	    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
	   
	    if (fld.value == "") {
	        fld.style.background = 'Red';
	        error = "  - You didn't enter an email address\n";
	    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
	        fld.style.background = 'Red';
	     
	    } else if (fld.value.match(illegalChars)) {

	        fld.style.background = 'Red';
	        error = "  - The email address contains illegal characters\n";
	    } else {
	        fld.style.background = '';
	    }
    }
    return error;
}



function validateNumber(fld) {
    var error = "";
  if (!fld) return error;   
   if (submitCount>0) {
	    var tfld = trimSpaces(fld.value)
	    
	    var nonDigitChars = /[^0-9]/;    
	
		if (fld.value== "") {
			//do nothing if empty because number is not required
			fld.style.background = '';
		}else if (tfld.match(nonDigitChars)) {
	        error = "  - The phone number contains illegal characters\n";
	        fld.style.background = 'Red';
	    }else {
	    	fld.style.background = '';
	    }
    }
    return error;
}

function valButton(fld) {
    var cnt = -1;
    for (var i=fld.length-1; i > -1; i--) {
        if (fld[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1) return fld[cnt].value;
    else return null;
}
                  
function SetRadioColor(fld,color)
{
    	for (var i=0; i < fld.length; i++)
	{
		fld[i].style.background = color; 
    	}
}

function validateRadio(fld) {
	var error = "";
	if (!fld) return error;	//check if fld exists before doing anything else
	if (fld.length<2) return error; //check that it has at least two entries as required for useful radio button

	if (submitCount>0)
	{
		var selectedBtn = valButton(fld) //Get the currently selected button
		if (selectedBtn == null) 	//valButton returns null if no button selected
		{
			SetRadioColor(fld,'Red')
			error = "  - Please select an agree option\n"
		} else {
			SetRadioColor(fld,'')
		}
	}
	return error; 
}
