
function set_checked(e){
document.forms[e].checked.value="yes";
}

function validate(e){
message = "";
focus_on = "";
	switch (e){
	case "emailsignup":
	if(IsEmail(document.emailsignup.email.value)) {
	openWin('../../popup/email_news308d.html?email='+document.emailsignup.email.value,'389','455');
	} else {
	message="Please enter a valid e-mail address."
	focus_on = "Email_Address";
	}
	break;
	case "comp_1q":
	if(IsBlank(document.forms["comp_1q"].answer1.value)){message="Please complete all the answers";focus_on = "answer1";}
	if(!IsEmail(document.forms["comp_1q"].email.value)){message="Please enter a valid e-mail address, " + message;focus_on = "email";}
	if(IsBlank(document.forms["comp_1q"].name.value)){message="Please enter your name, " + message;focus_on = "name";}
	
	break;
	case "comp_3q":
	blank = 0;
	if(IsBlank(document.forms["comp_3q"].answer3.value)){blank = blank + 1;focus_on = "answer3";}
	if(IsBlank(document.forms["comp_3q"].answer2.value)){blank = blank + 1;focus_on = "answer2";}
	if(IsBlank(document.forms["comp_3q"].answer1.value)){blank = blank + 1;focus_on = "answer1";}
	if (blank > 0){message="all questions must be answered";}
	if(!IsEmail(document.forms["comp_3q"].email.value)){message="your email address, " + message;focus_on = "email";}
	if(IsBlank(document.forms["comp_3q"].name.value)){message="your name, " + message;focus_on = "name";}
	
	break;
	case "quote":
	
	if(IsBlank(document.forms["quote"].quote.value)){message="quote";focus_on = "quote";}
	if(IsBlank(document.forms["quote"].First_name.value)){message="name, " + message;focus_on = "First_name";}
	
	break;
	case "suggest_poll":
	if(IsBlank(document.forms["suggest_poll"].answer3.value)){message="Answer 3, " + message;focus_on = "answer3";}
	if(IsBlank(document.forms["suggest_poll"].answer2.value)){message="Answer 2, " + message;focus_on = "answer2";}
	if(IsBlank(document.forms["suggest_poll"].answer1.value)){message="Answer 1, " + message;focus_on = "answer1";}
	if(IsBlank(document.forms["suggest_poll"].question.value)){message="Question, " + message;focus_on = "question";}
	if(IsBlank(document.forms["suggest_poll"].reason.value)){message="Reasons for asking, " + message;focus_on = "reason";}
	if(IsBlank(document.forms["suggest_poll"].First_name.value)){message="your name, " + message;focus_on = "First_name";}
	
	break;
	case "lookalikes":
	if(IsBlank(document.forms["lookalikes"].userFile.value)){message="Your file, " + message;focus_on = "userFile";}
	if(IsBlank(document.forms["lookalikes"].name.value)){message="your name, " + message;focus_on = "name";}
	
	break;
	case "vote_poll":
	if(document.forms["vote_poll"].checked.value == "no")message="Please select an option first.";
	
	break;
	case "competition":
	//can not validate at present
	
	break;
	case "talking_point":
	//not validating location and email
	
	if(!IsSentence(document.forms["talking_point"].comment.value)){message="comment must contain at least 3 words";focus_on = "comment";}
	if(IsBlank(document.forms["talking_point"].First_name.value)){message="name, " + message;focus_on = "First_name";}
	TPName=document.forms["talking_point"].comment.value;
	TPName=TPName.replace(/,/g, " ");
	document.forms["talking_point"].comment.value=TPName;
	TPVal=document.forms["talking_point"].comment.value;
	TPVal=TPVal.replace(/,/g, " ");
	document.forms["talking_point"].comment.value=TPVal;
	TPLoc=document.forms["talking_point"].comment.value;
	TPLoc=TPLoc.replace(/,/g, " ");
	document.forms["talking_point"].comment.value=TPLoc;
	TPMail=document.forms["talking_point"].comment.value;
	TPMail=TPMail.replace(/,/g, " ");
	document.forms["talking_point"].comment.value=TPMail;
	
	break;
	case "live_chat":
	//not validating location and email
	
	if(!IsSentence(document.forms["live_chat"].question.value)){message="question must contain at least 3 words";focus_on = "question";}
	if(IsBlank(document.forms["live_chat"].First_name.value)){message="name, " + message;focus_on = "First_name";}
	
	break;
	case "suggest_question":
	//not validating location and email
	if(!IsSentence(document.forms["suggest_question"].question.value)){message="question must contain at least 3 words";focus_on = "question";}
	if(IsBlank(document.forms["suggest_question"].name.value)){message="name, " + message;focus_on = "name";}
	
	break;
	case "email_news":
	checked_val = 0;
	if(document.forms["email_news"].evening.checked!=true){checked_val++;focus_on = "evening"};
	if(document.forms["email_news"].breakfast.checked!=true){checked_val++;focus_on = "breakfast"};
	if(checked_val == 3){message="select at least one type";}	if(!IsEmail(document.forms["email_news"].Email_Address.value)){message="your email address, " + message;focus_on = "Email_Address";}
	if(IsBlank(document.forms["email_news"].First_name.value)){message="your name, " + message;focus_on = "First_name";}
	break;
	case "unsubscribe":
	checked_val = 0;
	if(document.forms["unsubscribe"].evening.checked!=true){checked_val++;focus_on = "evening"};
	if(document.forms["unsubscribe"].breakfast.checked!=true){checked_val++;focus_on = "breakfast"};
	if(checked_val == 3){message="select at least one type";}	if(!IsEmail(document.forms["unsubscribe"].Email_Address.value)){message="your email address, " + message;focus_on = "Email_Address";}
	if(IsBlank(document.forms["unsubscribe"].First_name.value)){message="your name, " + message;focus_on = "First_name";}
	break;
	case "email_friend":
	if(IsBlank(document.forms["email_friend"].message.value)){message="your message";focus_on = "message";}
	if(!IsEmail(document.forms["email_friend"].friendsemail.value)){message="your friends email, " + message;focus_on = "friendsemail";}
	if(IsBlank(document.forms["email_friend"].friendsname.value)){message="your friends name, " + message;focus_on = "friendsname";}
	if(!IsEmail(document.forms["email_friend"].email.value)){message="your email address, " + message;focus_on = "email";}
	if(IsBlank(document.forms["email_friend"].name.value)){message="your name, " + message;focus_on = "name";}
	
	break;
	case "fan_site":
	
	if(!IsSentence(document.forms["fan_site"].reason.value)){message="the reason must contain at least 3 words";focus_on = "reason";}
	if(IsBlank(document.forms["fan_site"].url.value)){message="site url, " + message;focus_on = "url";}
	if(IsBlank(document.forms["fan_site"].name.value)){message="your name, " + message;focus_on = "name";}
	
	break;
	default:
	     message="Sorry we could not send off your form please contact web-master quoting: "+e+".\n\nThank-you.";
	}
	
	if (message != ""){
		//make the message make sense
		
		if (message.substring(0,5) != "Pleas"){if (message.substring(message.length-2,message.length) == ", ")message = message.substring(0,message.length-2);message = "There was an error with your submission form in:\n" + message + "."}
		
		
		
		alert(message);
		//this focuses on the first form field left out
		//this works because we validate the page from bottom up
		
		if (focus_on!="")eval("document.forms[e]."+focus_on+".focus()");
		return false;
	}else{
		//submit the form
		// document.forms[e].submit();
		return true;
	}
}

//checks to see if field is blank by looking for " " \n and \t and nothing else if is returns true
function IsBlank(e) {
	for(var i = 0; i < e.length; i++) {
		var c = e.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
	}
	return true;
}
//simple looks for an @ if is returns true
function IsEmail(e) {
	if ((e.indexOf(".")!=-1)&&(e.indexOf("@")!=-1))return true;
	return false;
}
//looks for at least 3 spaces ie " " in the paragraph 
//to change spaces looked for change space == 3 to number wanted in marked line
function IsSentence(e) {
	var space = 0;
	if (((e.charCodeAt(0) > 64) && (e.charCodeAt(0) < 91)) || ((e.charCodeAt(0) > 96) && (e.charCodeAt(0) < 123)) || ((e.charCodeAt(0) > 47) && (e.charCodeAt(0) < 58))) {
		for(var i = 1; i < (e.length - 1); i++) {
			if ((e.charCodeAt(i) == 32) &&  ((e.charCodeAt(i+1) > 64) && ((e.charCodeAt(i+1) < 91)) || ((e.charCodeAt(i+1) > 96) && (e.charCodeAt(i+1) < 123)) || ((e.charCodeAt(i+1) > 47) && (e.charCodeAt(i+1) < 58)))) space++;
// this line below is the one to change
			if (space == 2) return true;
		}
	}	
	return false;
} 
