// JavaScript Document


function isNumeric(pccharinput) {  // 1) this allows the numbers 0-9
	return(pccharinput>="0" && pccharinput<="9")
}

function isQty(pccharinput) {
	return (pccharinput=="-" || pccharinput=="-1" || pccharinput=="-2" || pccharinput=="-3" || pccharinput=="-4" || pccharinput=="-5" || pccharinput=="-6" || pccharinput=="-7" || pccharinput=="-8" || pccharinput=="-9" )
}

function isAlphabetic(pccharinput) { // 2) this allows the letters A-Z & a-z
	return((pccharinput>="A" && pccharinput<="Z") || 
		(pccharinput>="a" && pccharinput<="z"))
}

function isAlphaNumeric(pccharinput) { //  3) this allows 1) & 2) from above to be put together
	return (isAlphabetic(pccharinput) || isNumeric(pccharinput))
}

function isEmpty(pccharinput) { // checks for any empty fields in the HTML form
	if (pccharinput=="" || pccharinput==" ")
		return true;
	else 
		return false;
	}
	
function isAddressChar(pccharInput) {  // allows numbers, alphabets and the special name characters from line 34.
	if (isAlphaNumeric(pccharInput) || isNameSpecialChar(pccharInput))
	return true
	else
	return false
}

function isNameChar(pccharinput) {  // this is all the alphabet and the special characters for the name
	return (isAlphabetic(pccharinput) || isNameSpecialChar(pccharinput))
}

//alert(isNameChar("michelle'hgf"))
function isNameSpecialChar(pccharinput) {  // these are the special characters allowed for the name
	return (pccharinput=="'" || pccharinput=="-" || pccharinput==" ")
}




function isNameString(pcstrInput) {  

	var w, lvintapostrophepos, lvintdashpos, lvintspacepos
	
	lvintapostrophepos=pcstrInput.indexOf("'")
	lvintdashpos=pcstrInput.indexOf("-")
	lvintspacepos=pcstrInput.indexOf(" ")
	
	for (w=0; w<pcstrInput.length; w++) {
	
		if (!isNameChar (pcstrInput.charAt(w)))
		{ 
			return false
		}
	}
	
	if (lvintapostrophepos==0)  // if ' is at postion 0 it's false
		return false
	if (lvintdashpos==0) // if - is at postion 0 it's false
		return false
	if (lvintspacepos==0) // if there is a space at 0 it's wrong
		return false
	if (isEmpty(pcstrInput))  // if the text field is empty it will return false
		return false
	return true
} 

//alert(isNameString("'kjgf"))
//alert(isNameString(" "))
//alert(isNameString("-hgd"))
//alert(isNameString("hgfd's"))

///////////////////////////////////////////////////////////

function isEmailSpecialChar(pccharinput) {
	return (pccharinput=="@" || pccharinput=="-" || pccharinput=="_" || pccharinput==".")
}

function isEmailChar(pccharinput) {  // allows numbers, alphabets and the special email characters from above.
	return (isAlphaNumeric(pccharinput) || isEmailSpecialChar(pccharinput))
}

//alert(isEmailChar("michelle@.com"))
//alert(isEmailChar("mich_-elle@.com"))
//alert(isEmailChar("mich_-elle@.com653"))
//alert(isEmailChar("mi#$%$%ch_-elle@.com"))




function isEmailString (pcstrInput) { 

	var w, lvintlength, lvintlastpos, lvintatpos, lvintatlastpos, lvintfirstpos

	lvintlength=pcstrInput.length
	lvintlastpos=pcstrInput.lastIndexOf(".")
	lvintatpos=pcstrInput.indexOf("@")
	lvintfirstpos=pcstrInput.indexOf(".")
	lvintatlastpos=pcstrInput.lastIndexOf("@")
	
	
	for (w=0; w<pcstrInput.length; w++) 
	{ 
		if (!isEmailChar (pcstrInput.charAt(w)))
		{ 
			return false
		} 
	} 	

	if(lvintatpos==0) // if @ is = 0 it's false.
		return false

	if (lvintlastpos==0) // if last . is = 0 it's wrong
		return false

	if (lvintfirstpos==0) // if . is = to 0 it's wrong
		return false

	if (lvintatlastpos==-1) // there is no @ in the email address
		return false

	if (lvintfirstpos==-1) // there is no . in the email address
		return false

	if (pcstrInput.indexOf("@.")!=-1) // this means the @. are not = to -1 (basically if there is @ and. together it returns false.)
		return false
		
	if (pcstrInput.indexOf(".@")!=-1) //there are .@ together
		return false
		
	if (pcstrInput.indexOf("@@")!=-1) // two @@ together
		return false
		
	if (pcstrInput.indexOf("..")!=-1) // this means the two .. are not = to -1 (basically if there are 2 .. it returns false.)
		return false

	if(lvintlength-lvintlastpos<3) // there are not two letters after the last .
		return false
	 
	return true
} 

 



function isSubmit() {  // this validates the add user form.
	
	if (window.document.enquiry.Firstname.value=="") {
		alert("Please enter your name.")
		window.document.enquiry.Firstname.focus();
		return false;
		}
	
	if (window.document.enquiry.Phone.value=="") {
		alert("Please enter your phone number.")
		window.document.enquiry.Phone.focus();
		return false;
		}
		
	if (window.document.enquiry.WorkRequired.value=="") {
		alert("Please write a breif description of the work you require.")
		window.document.enquiry.WorkRequired.focus();
		return false;
		}
		
		
}
