/* -- character count for blog comment form -- */

function textCounter(field, countfield, maxlimit) {
			if (field.value.length > maxlimit)
			field.value = field.value.substring(0, maxlimit);
			else
			countfield.value = maxlimit - field.value.length;
			}

/* ------- Validation for Form ---- */
function ffErrors(args) {
	alert(args);
}

function validate(f) {
	var errors		= "" ;

	var theRadio 	= 0 ;
	var theInput	= null ;
	for(i=0;i<f.length;i++) {
			//set referer url
		if ( f.referrerURL ) f.referrerURL.value=document.location;

		theInput	= f[i] ;

		if (theInput.type != "hidden") {

			//check terms and conditions

			if (theInput.type == "checkbox" && theInput.name=="terms") {
				if(theInput.checked!=true) errors += "You must read the terms and conditions\n\n";
			}
			//check comp Question
			if (theInput.type == "radio" && theInput.name=="CompetitionAnswer") {
				error1="You must answer the question to enter the competition\n\n";
				(theInput.checked)?(theRadio=-1):((theRadio==-1)?theRadio==-1:theRadio+=1);
				if(theRadio!=-1 && theRadio>=3) {
					errors += error1;
				}
			}


			if (theInput.type=="text") {
				str=theInput.value.length;
				str2=0;
				 while(str!=0) {
					//test(theInput.value.charCodeAt(str-1));
				 	if(theInput.value.charCodeAt(str-1)<=32) {
						str2+=1;
					}
					str-=1;
				}
				if(str2>=theInput.value.length-str2) {
					errors+=theInput.name+" ?\n";
				} else {
					//specific matches only now
					if(theInput.name=="email") {
						chk2 = 0;
						str=theInput.value.length;
						while(str!=0) {
							if(theInput.value.charAt(str)==('@') || theInput.value.charAt(str)==('.')) {
							chk2+=1;
							}
						str-=1;

						}
						if(chk2<2) {
						errors+="email address is invalid\n";
						}
					}

					if(theInput.name=="telephone") {
						chk2 = 0;
						str=theInput.value.length;
						while(str!=0) {
							if(theInput.value.charCodeAt(str)<49 || theInput.value.charCodeAt(str)>58) {
							chk2+=1;
							}
						str-=1;

						}
						if(chk2>3) {
						errors+="Telephone No. is invalid\n";
						}
					}

				}

			}


		}

	}
	if(errors!="") {
		ffErrors("Sorry there appears to be a problem with details you supplied.\n\nPlease correct the following questions and try again. \n\n" +errors)
		return false;
	}
	return true ;

}