//Global variables
var MaxSub = 180;
var MaxPrice = 180;
var MaxComs = 250;
var MaxLocation = 50;


function extLinks() { 
 if (!document.getElementsByTagName) return; 
 var anchors = document.getElementsByTagName("a"); 
 for (var i=0; i<anchors.length; i++) { 
   var anchor = anchors[i]; 
   if (anchor.getAttribute("href") && 
       anchor.getAttribute("rel") == "nofollow") 
     anchor.target = "_blank"; 
	 if (anchor.getAttribute("href") && 
       anchor.getAttribute("rel") == "blank") 
     anchor.target = "_blank";
if (anchor.getAttribute("href") && 
       anchor.getAttribute("rel") == "top") 
     anchor.target = "_top"; 
 } 
} 


function EnableSubmit() {
	document.tutorform.SEND.disabled=false;
}

function DisableSubmit() {
	document.tutorform.SEND.disabled=true;
	document.tutorform.terms[0].checked=false;
	document.tutorform.terms[1].checked=true;
	document.tutorform.DISTANCE.disabled=false;
}

function ChangeColour() {
	i = document.tutorform.color.selectedIndex;
	tabcolor = document.tutorform.color.options[i].value;
	document.getElementById("ctable").style.background = tabcolor;
}


function ChangeTitle() {
	title = document.tutorform.title2.selectedIndex;
	val = document.tutorform.title2.options[title].value;
	var name2 = document.getElementById("TUTORNAME");
	name2.firstChild.nodeValue=val;
	document.tutorform.title.selectedIndex = title;
}
	
function AddFirstname() {
	var firstname = document.tutorform.FIRSTNAME2.value;
	var name1 = document.getElementById("TUTORNAME1");
	name1.firstChild.nodeValue=firstname;
	document.tutorform.FIRSTNAME.value = firstname;	
}

function AddSurname() {
	var surname = document.tutorform.SURNAME2.value;
	var name3 = document.getElementById("TUTORNAME2");
	name3.firstChild.nodeValue=surname;
	document.tutorform.SURNAME.value = surname;
}
function ChangePhone() {
	var phone = document.tutorform.TELEPHONE2.value;
	var PhoneLength = document.tutorform.TELEPHONE2.value.length;
		if (PhoneLength != 0) {
	var phone2 = document.getElementById("PHONE1");
	phone2.firstChild.nodeValue=phone;
	document.tutorform.TELEPHONE.value = phone;}
		else {
	var phone2 = document.getElementById("PHONE1").innerHTML="&nbsp;";
	}


}
function ChangeMobile() {
	var mobile= document.tutorform.MOBILE2.value;
	var MobileLength= document.tutorform.MOBILE2.value.length;
		if (MobileLength != 0) {
	var mobile2 = document.getElementById("MOBILE");
	mobile2.firstChild.nodeValue=mobile;
	document.tutorform.MOBILE.value = mobile;}
		else {
	var mobile2 = document.getElementById("MOBILE").innerHTML="&nbsp;";
	}
}


function ChangeEmail() {
	var email= document.tutorform.EMAIL2.value;
	var EmailLength= document.tutorform.EMAIL2.value.length;
		if (EmailLength != 0) {
	var email2 = document.getElementById("EMAIL");
	email2.firstChild.nodeValue=email;
	document.tutorform.EMAIL.value = email;}
		else {
	var email2 = document.getElementById("EMAIL").innerHTML="&nbsp;";
	}
}


function ConfirmEmail() {
	var FirstEmail= document.tutorform.EMAIL.value;
	var FirstEmailLength= document.tutorform.EMAIL.value.length;
	var SecondEmail= document.tutorform.EMAIL3.value;
	var SecondEmailLength= document.tutorform.EMAIL3.value.length;
	if (FirstEmailLength != 0  && SecondEmailLength !=0) {
		if (FirstEmail != SecondEmail) {
	alert("Confirm email  not the same as Email, Please check");
	document.tutorform.EMAIL.focus();
	return false;}
	}
}

function ChangeWebsite() {
	var web = document.tutorform.WEBSITE2.value;
	var WebLength = document.tutorform.WEBSITE2.value.length;
		if (WebLength != 0) {
	var web2 = document.getElementById("WEBURL");
	web2.firstChild.nodeValue=web;}
		else {
	var web2 = document.getElementById("WEBURL").innerHTML="&nbsp;";
	}
}


function CountChars(field, countfield, maxlimit) {
OverChar = (field.value.length - maxlimit);
if (field.value.length > maxlimit) // if the current length is more than allowed
{
field.value =field.value.substring(0, maxlimit);
//alert ("Only " + maxlimit + " characters allowed in this section. You have reached the maximum"); }// don't allow further input
alert("Total number of characters in this section must be no more than " + maxlimit + " characters. \nYou have entered " + (field.value.length+1) +  " characters\nPlease delete " + OverChar + " or more characters.");
}
else{
countfield.value = maxlimit - field.value.length;} // set the display field to remaining number
}



function ChangeSubjects() {
var Subs = document.tutorform.subjects.value;
var SubsLength = Subs.length;
	if (SubsLength <= MaxSub){
	var subjects2 = document.getElementById("SUBS");
	subjects2.firstChild.nodeValue=Subs;}
else { alert("Total number of characters in the Subjects and levels box must be no more than " + MaxSub + " characters. \nYou have entered " + SubsLength +  " characters\nPlease delete " + OverSub + " or more characters.");}
}



function ChangePrice() {
Prices = document.tutorform.PriceRange.value;
PricesLength = Prices.length;
OverPrices = (PricesLength - MaxPrice);	


if (PricesLength <= MaxPrice){
	var rang2 = document.getElementById("CHARGES");
	rang2.firstChild.nodeValue=Prices;}
else { alert("Total number of characters in the price information box must be no more than " + MaxPrice + " characters. \nYou have entered " + PricesLength +  " characters\nPlease delete " + OverPrices + " or more characters.");}
}



function ChangeComments() {
Coms = document.tutorform.Comments.value;
ComsLength = Coms.length;
OverComs = (ComsLength - MaxComs);


if (ComsLength <= MaxComs){
	var comments2 = document.getElementById("ADINFO");
	comments2.firstChild.nodeValue=Coms;}
else { alert( "Total number of characters in the additional information box  must be no more than " + MaxComs + " characters. \nYou have entered " + ComsLength +  " characters\nPlease delete " + OverComs + " or more characters.");}
}



function ChangeLocation() {
	var location = document.tutorform.LOCATION.value;
	LocationLength = location.length;
	OverLocation = (LocationLength - MaxLocation);

if (LocationLength <= MaxLocation) {
	var location2 = document.getElementById("LOCALITY");
	location2.firstChild.nodeValue=location;}
else { alert ( "Total number of characters in the location box  must be no more than " + MaxLocation + " characters. \nYou have entered " + LocationLength +  " characters\nPlease delete " + OverLocation + " or more characters." );
}
}



function MaxField() {
	if  (document.tutorform.travel[0].checked) {
	document.tutorform.DISTANCE.value = "Not Applicable";
	document.tutorform.DISTANCE.disabled=true;
	}
	else {
	document.tutorform.DISTANCE.disabled=false;
	if (document.tutorform.travel[1].checked) {
	removeChild.tutorform.DISTANCE.value = "Not Applicable";
	alert("remove the item");
}
}
}

function ChangeDistance() {
	dist = document.tutorform.DISTANCE.selectedIndex;
	val = document.tutorform.DISTANCE.options[dist].value;
	var dist2 = document.getElementById("DISTA");
	app = " ";
	if (dist ==0) dist2.firstChild.nodeValue=(app);
	if (dist !=0) dist2.firstChild.nodeValue=("Distance travelled: " + val);
	
}

function ImageChange() {
	if  (document.tutorform.travel[0].checked) {
	document.images['TravImage'].src= "house.gif";}
	if  (document.tutorform.travel[1].checked) {
	document.images['TravImage'].src= "car.gif";}
	if  (document.tutorform.travel[2].checked) {
	document.images['TravImage'].src= "both.gif";}
	}

function ChBoxCount(f){
var t=0;
var C12=25; 			// The cost of advertising for twelve months
var E = 10; 				//The cost per extra avert
var c=f['COUNTY[]'];		//f=this.form
for(i=0;i<c.length;i++){
c[i].checked?t++:null;
}
if (t > 0) {
	Z = C12-E;  		// Cost for twelve months  minus £extra advert
	if (t > 1) {
	p = " pages";}		// If more than one advert selected use pages otherwise use page
	else {
	p= " page"; }
			
x = (t*E + Z); // Total cost = no of counties selected multiplied by cost per extra advert ( currently £5) plus cost of one advert minus 5
y = 'As you have selected ' + t + p + ' the cost of your advertising will be  £'+ x +'.00.';				// Text to output on page
cost = document.getElementById("output");
cost.firstChild.nodeValue = y;
CosB = document.getElementById("CostButton");
CosB.firstChild.nodeValue = "Re-Calculate Cost";  // Updates text within button from calculate cost to RE-CALCULATE cost after first use 
} 
else 
{alert ('No Counties/Pages selected');}  		// Alert box if no counties selected
}	
	

	
	
function validate(){
var Subs = document.tutorform.subjects.value;
var SubsLength = Subs.length;
OverSub = (SubsLength - MaxSub);

Prices = document.tutorform.PriceRange.value;
PricesLength = Prices.length;
OverPrices = (PricesLength - MaxPrice);	

Coms = document.tutorform.Comments.value;
ComsLength = Coms.length;
OverComs = (ComsLength - MaxComs);


	titlex = document.tutorform.title.selectedIndex;
	val = document.tutorform.title.options[titlex].value;
	if (val=="_") {
	alert("please select your title");
	document.tutorform.title.focus();
	return false;}

	if (document.tutorform.SURNAME.value.length < 1) {
	alert("Please check that you have entered your surname");
	document.tutorform.SURNAME.focus();
	return false;}
	
	if (document.tutorform.ADDRESS1.value.length < 2) {
	alert("Have you entered your house name/number and Street?");
	document.tutorform.ADDRESS1.focus();
	return false;}

	

	if (document.tutorform.ADDRESS3.value.length < 2) {
	alert("Please enter the Town/City...");
	document.tutorform.ADDRESS3.focus();
	return false;}

 
	
	if (document.tutorform.POSTCODE.value.length < 6){
	alert("Have you have entered a valid postcode?");
	document.tutorform.POSTCODE.focus();
	return false;}

	if (document.tutorform.TELEPHONE.value.length < 11){
	alert("Please check that you have entered the full telephone number and included your area code");
	document.tutorform.TELEPHONE.focus();
	return false;}
	
	if (document.tutorform.EMAIL.value.length < 6){
	alert("Please check that you have entered an E-Mail Address");
	document.tutorform.EMAIL.focus();
	return false;}
	
	if (PricesLength == 0){
	alert("Please check that you have entered some text in the Price and Range section.");
	document.tutorform.PriceRange.focus();
	return false;}

	if (SubsLength == 0){
	alert("Please check that you have entered some text in the  subjects and levels section.");
	document.tutorform.subjects.focus();
	return false;}

	if (ComsLength == 0){
	alert("Please check that you have entered some text in the additional information section.");
	document.tutorform.Comments.focus();
	return false;}

	if (PricesLength > MaxPrice){
	alert("Maximum number of characters in the price information box is " + MaxPrice + "\nYou have entered " + PricesLength +  " characters\nPlease delete " + OverPrices +  " or more characters.");
	document.tutorform.PriceRange.focus();
	return false;}

	


	if (SubsLength > MaxSub){
	alert("Maximum number of characters in the Subjects and levels box  is " + MaxSub + "\nYou have entered " + SubsLength +  " characters\nPlease delete " + OverSub + " or more characters.");
	document.tutorform.subjects.focus();
	return false;}

	if (ComsLength > MaxComs){
	alert("Maximum number of characters in the additional information box is " + MaxComs + "\nYou have entered " + ComsLength +  " characters\nPlease delete " + OverComs + " or more characters.");
	document.tutorform.Comments.focus();
	return false;}

	

	if (document.tutorform.LOCATION.value.length < 2){
	alert("Please check that you have entered your location");
	document.tutorform.LOCATION.focus();
	}


	

	Conf=confirm ("You are about to submit your details to HomeTutorsDirectory.co.uk. Press OK to submit details OR Cancel to return to the form");
	if (Conf==true) {return true;}
	if (Conf==false) {return false;}
	

return true;
}





