﻿function Titlise(Sentence) {	// converts 1st character of the words in a sentence to upper case

	var spPos=0
	var size = Sentence.length
	Sentence=Sentence.toLowerCase()

	if (size>0) {
		while (Sentence.slice(0,1)==" ") {			//Strip leading spaces
			Sentence = Sentence.substr(1,size-1)
			size = Sentence.length
		}  
		while(Sentence.slice(size-1,size)==" ") {	// Strip trailing spaces
			Sentence = Sentence.substr(0,size-1)
			size = Sentence.length
		}

		if (Sentence.indexOf(" ",spPos)==-1) {							// only one word in sentence
			Sentence=Sentence.charAt(0).toUpperCase()+Sentence.slice(1,size)
		}
		else {
			while ( spPos!=-1) {
				spPos = Sentence.indexOf(" ",spPos+1)
				Sentence=Sentence.slice(0,spPos+1)+Sentence.charAt(spPos+1).toUpperCase()+Sentence.slice(spPos+2,size)
			} // end while
		} // end else
	} // end if (size>0)
	return Sentence
} // end function

function isTelephone(v) {
	var nGoodChars = 0
	var nPoints = 0
	var len = v.length
	var goodChar = new Array(" ","0","1","2","3","4","5","6","7","8","9","+")


	for (var i = 0;i<len;i++) {						// loop through each character in input string
		if (i>0) {									// seach string for "+" characters
			if( v.charAt(i) == goodChar[11]) {
			  	return false						// plus  only in 1st position
			}
		}
	}

	for (var i = 0;i<len;i++) {						// loop through each character in input string
		for (var j = 0;j<12;j++) {					// loop though good character
			if( v.charAt(i) == goodChar[j]) {		// found numeral or space
				nGoodChars = nGoodChars + 1			// count good characters
			}
		}
	}

	if (nGoodChars<len) {
		return false
	}

	return true
}

function findHTML(phrase) {
	if (phrase.indexOf("<",0) > -1) { 
		return true
	}
	if (phrase.indexOf(">",0) > -1) {
		 return true
	}
	if (phrase.indexOf("&#60;",0) > -1) {
		return true
	}	
	if (phrase.indexOf("&#62;",0) > -1) {
		return true
	}	
	if (phrase.indexOf("&lt;",0) > -1) {
		return true
	}
	if (phrase.indexOf("&gt;",0) > -1) {
		return true
	}
	return false
}

function Trim(phrase) {	// Strips leading and trailing spaces
	var Sp=" "
	var size = phrase.length
	while (phrase.slice(0,1)==Sp) {					// Strip leading spaces
		phrase = phrase.substr(1,size-1)
		size = phrase.length
	}  
	while(phrase.slice(size-1,size)==Sp) {			// Strip trailing spaces
		phrase = phrase.substr(0,size-1)
		size = phrase.length
	}
	return phrase
}


function checkForm() {
	var name	= document.mailForm.N
	var email	= document.mailForm.E
	var tel		= document.mailForm.T
	var sub		= document.mailForm.S
	var mess	= document.mailForm.M

	var size
		
	if (name.value == "") {
		alert("Please enter your name")
		name.focus()
		return false
	}
	if (findHTML(name.value)) {
		alert("Markup not Permitted")
		name.value = ""
		name.focus()
		return false
	}
	name.value=Titlise(name.value)

	var em=email.value.toLowerCase()
	em=Trim(em)
	if (em == "") {
		alert("Please enter your email address")
		email.focus()
		return  false
	}
	var apos = "'"
	var badChars = new Array("/",";",":",",","<",">","&")

	for (var i = 0;i<badChars.length;i++) {
		if( em.indexOf(badChars[i],0) > -1) {
			alert("Invalid email address:\rBad character: "+badChars[i])
			email.focus()
			email.value=""
			return  false
		}
	}

	var atPos = em.indexOf("@",1)
	if (atPos == -1) {
		alert("Invalid email address:\rMissing "+apos+"at"+apos+" character: "+apos+"@"+apos)
		email.focus()
		email.value=""
		return  false
	}

	if (em.indexOf("@",atPos+1) > -1) {
		alert("Invalid email address:\rMore than one @ character")
			email.focus()
			email.value=""
			return  false
	}

	dotPos = em.indexOf(".",atPos)
	if (dotPos == -1) {
		alert("Invalid email address:\rMissing period: "+apos+"."+apos)
			email.focus()
			email.value=""
			return  false
	}

	if (dotPos+3 > em.length ) {
		alert("Invalid email address:\rThere should be at least 2 characters after the period: "+apos+"."+apos)
			email.focus()
			email.value=""
			return  false
	}

	if (tel.value == "") {
		alert("Please enter your contact number")
		tel.focus()
		return false
	}

	if (!isTelephone(tel.value)) {
		alert("Not a telephone number")
		tel.value = ""
		tel.focus()
		return false
	}

	if (findHTML(tel.value)) {
		alert("Markup not Permitted")
		tel.value = ""
		tel.focus()
		return false
	}

	size = sub.length
	if (size>0) {
		sub.value=Titlise(sub)
	}
	else {
		sub.value="Wern Watkin Bunkhouse Enquiry"
	}

	var msg=Trim(mess.value)
	size = msg.length
	if (size>0) {
		if (findHTML(msg)) {
			alert("Markup not Permitted")
			mess.value = ""
			return false
		}
		msg=msg.charAt(0).toUpperCase()+msg.slice(1,size)		// Capitalise 1st word
		if (msg.charAt(size) != ".") { msg=msg+"."}				// Add full stop
		mess.value=msg
	}
	else {
		alert("Please enter your message")
		mess.focus()
		return  false
	}
		
	document.mailForm.action = TEAdecrypt("âÇhAèh¨Å°")			// Decryption function in tea_de.js

	return true	// Form OK
}