
function checkSugEdit() {
	var test = document.getElementById("subName");
	if (stringEmpty(test.value)) {
		alert ("We need your real name and email to run a membership check before accepting your suggestion");
		test.style.backgroundColor = "red";		
		return false;
	} else {
		test.style.backgroundColor = "white";	
	}
	if (checkEmail()==false) {	
		return false;
	}
	return true;
}


function numCheck(field,allow) {
// used onkeyup and check if field is number and makes it red if not
	var check = true;
	allow += "1234567890";
	var value = field.value; //get characters
 	//check that all characters are digits, ., -, or ""
	for(var i=0;i < field.value.length; ++i) {
		var new_key = value.charAt(i); //cycle through characters
		if((allow.indexOf(new_key) == -1) && !(new_key == "")) {
			check = false;
			break;
		}
	}
	//apply appropriate colour based on value
	if(!check) {
		field.style.backgroundColor = "red";
	} else {
		field.style.backgroundColor = "white";
	}
}


function phoneCheck(x) {
// checks that passed string x looks like a telephone number
	//strip out acceptable non-numeric characters spaces brackets dots and hyphens
	var stripped = x.replace(/[\(\)\.\-\ ]/g, '');
	//allow leading +
	if (stripped.indexOf('+') == 0) {
		stripped = stripped.substr(1,stripped.length-1);
	}
	if (isNaN(parseInt(stripped))) {
		return false;
	}
	if (stripped.length < 11 || stripped.length > 12) {

		return false;
	}
	return true;
}


function checkEmail() {
 	formInput = document.getElementById("email");	
   if (typeof(formInput) != "object") {
        alert("Validation not supported on this browser.");
        return(false);
    }
    var message;
//alert("in check email");
//return false;
    if (stringEmpty(formInput.value)) {
        message = "Please enter an email address; this is required. \nWe will only use it to contact you about this site and will not reveal it to anyone else.";
        alert(message);
	    return (false);
    } else if (noAtSign( formInput.value )) {
        message = "Error! The address \"" + formInput.value + "\" does not contain an '@' character.";
        alert(message);
	    return (false);
    } else if (nothingBeforeAt(formInput.value)) {
        message = "Error! The address \"" + formInput.value;
        message += "\" has nothing before the '@' character";
        alert(message);
	    return (false);
    } else if (noLeftBracket(formInput.value)) {
        message = "Error! The address \"" + formInput.value;
        message += "\" contains a right square bracket ']',\nbut no corresponding left square bracket '['.";
        alert(message);
	    return (false);
    } else if (noRightBracket(formInput.value)) {
        message = "Error! The address \"" + formInput.value;
        message += "\" contains a left square bracket '[',\nbut no corresponding right square bracket ']'.";
        alert( message);
	    return (false);
    } else if (noValidPeriod(formInput.value)) {
        message = "Error! The address \"" + formInput.value + "\" has no period ('.') character.";
        alert(message);
	    return (false);
    } else if (noValidSuffix(formInput.value)) {
        message = "Error! The address \"" + formInput.value;
        message += "\" has an invalid suffix (the last bit after the dot).";
        alert(message);
	    return (false);
    }
    return (true);
}


function checkValid (formField) {
    if ( checkValidation ( formField ) == true ) {
        return true;
    }
    return ( false );
}



function stringEmpty (formField) {
    // Return TRUE if STRING IS EMPTY
    if ( formField.length < 1 ) {
       return ( true );
    } else {
//		formField.style.backgroundColor = "white";
        return ( false );
    }
}



function noAtSign (formField) {

    // CHECK THAT THERE IS AN '@' CHARACTER IN THE STRING

    if (formField.indexOf ('@', 0) == -1) {

        return ( true )

    } else {

        return ( false );

    }

}



function nothingBeforeAt (formField) {

    // CHECK THERE IS AT LEAST ONE CHARACTER BEFORE THE '@' CHARACTER

    if ( formField.indexOf ( '@', 0 ) < 1 ) {

        return ( true )

    } else {

        return ( false );

    }

}



function noLeftBracket (formField) {

    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR LEFT BRACKET

    if ( formField.indexOf ( '[', 0 ) == -1 && formField.charAt (formField.length - 1) == ']') {

        return ( true )

    } else {

        return ( false );

    }

}



function noRightBracket (formField) {

    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR RIGHT BRACKET

    if (formField.indexOf ( '[', 0 ) > -1 && formField.charAt (formField.length - 1) != ']') {

        return ( true );

    } else {

        return ( false );

    }

}



function noValidPeriod (formField) {

    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED

    if (formField.indexOf ( '@', 0 ) > 1 && formField.charAt (formField.length - 1 ) == ']')

        return ( false );



    // CHECK THAT THERE IS AT LEAST ONE PERIOD IN THE STRING

    if (formField.indexOf ( '.', 0 ) == -1)

        return ( true );



    return ( false );

}



function noValidSuffix(formField) {

    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED

    if (formField.indexOf('@', 0) > 1 && formField.charAt(formField.length - 1) == ']') {

        return ( false );

    }



    // CHECK THAT THERE IS A TWO OR THREE CHARACTER SUFFIX AFTER THE LAST PERIOD

    var len = formField.length;

    var pos = formField.lastIndexOf ( '.', len - 1 ) + 1;

    if ( ( len - pos ) < 2 || ( len - pos ) > 4 ) {

        return ( true );

    } else {

        return ( false );

    }

}



function stripLeadSpace(x) {

	while (x.slice(0,1) == " "){

		x = x.substr(1,x.length-1);

	}

	return x;

}



function checkPC(){ //check postcode format is valid

	e=document.getElementById("postcode");

	test = e.value;

 	test = test.toUpperCase(); //Change to uppercase

 	test = stripLeadSpace(test);

 	size = test.length; 	

 	while(test.slice(size-1,size)== " ") { //Strip trailing spaces

  		test = test.substr(0,size-1);

  		size = test.length

  	}

 	e.value = test; //write back to form field

//	return false;

 	if ((size < 6) || (size > 8)){ //Code length rule

     alert(test + " is not a valid postcode - wrong length");

    //  e.focus();

  		return false;

  	}

 	if (!(isNaN(test.charAt(0)))) { //leftmost character must be alpha character rule

   alert(test + " is not a valid postcode - cannot start with a number");

//   e.focus();

   		return false;

  	}

 	if (isNaN(test.charAt(size-3))) { //first character of inward code must be numeric rule

   alert(test + " is not a valid postcode - alpha character in wrong position");

//   e.focus();

   		return false;

  	}

 	if (!(isNaN(test.charAt(size-2)))){ //second character of inward code must be alpha rule

   alert(test + " is not a valid postcode - number in wrong position");

//   e.focus();

   		return false;

  	}

 	if (!(isNaN(test.charAt(size-1)))) { //third character of inward code must be alpha rule

   alert(test + " is not a valid postcode - number in wrong position");

//   e.focus();

   		return false;

  	}

 	if (!(test.charAt(size-4) == " ")) {//space in position length-3 rule

   alert(test + " is not a valid postcode - no space or space in wrong position");

//   e.focus();

   		return false;

   	}

 	count1 = test.indexOf(" ");

	count2 = test.lastIndexOf(" ");

 	if (count1 != count2) {//only one space rule

   alert(test + " is not a valid postcode - only one space allowed");

//   e.focus();

   		return false;

  	}

//alert("Postcode Format OK");

	return true;

}





