window.name = "opener"

function writeEm() {
	document.write('<a href="mailto:sales@pmeweb.co.uk">sales@pmeweb.co.uk</a>')
}

function openChildWindow(theURL,winName,features) { 
  window.open(theURL,winName,features)
}

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 TitliseAdd(Sentence) {	// converts 1st character of the words in an address 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("\n",spPos)==-1) {							// only one word in address
			Sentence=Sentence.charAt(0).toUpperCase()+Sentence.slice(1,size)
		}
		else {
			while ( spPos!=-1) {
				spPos = Sentence.indexOf("\n",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 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 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 checkForm() {
	var name	= document.mailForm.Name
	var address = document.mailForm.Address
	var address = document.mailForm.Address
	var code	= document.mailForm.Code
	var tel		= document.mailForm.Telephone
	var email	= document.mailForm.emailAddress
	var design	= document.mailForm.Design
	var url		= document.mailForm.clientURL
	var mess	= document.mailForm.Other

	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)


	if (address.value == "") {
		alert("Please enter your address")
		address.focus()
		return false
	}
	if (findHTML(address.value)) {
		alert("Markup not Permitted")
		address.value = ""
		address.focus()
		return false
	}
	address.value=TitliseAdd(address.value)


	if (code.value == "") {
		alert("Please enter your post code")
		code.focus()
		return false
	}
	if (findHTML(code.value)) {
		alert("Markup not Permitted")
		code.value = ""
		code.focus()
		return false
	}
	code.value=code.value.toUpperCase()

	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
	}

	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 (design[1].checked) {
		if (url.value == "") {
			alert("Please enter your current web address")
			url.focus()
			return false
		}
	}

	if (findHTML(url.value)) {
		alert("Markup not Permitted")
		url.value = ""
		url.focus()
		return false
	}

	var msg=Trim(mess.value)
	var 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
	}
	return true	// Form OK
}