// JavaScript Document

//---------------------------------------------------------------------------------------------------------
//   SHOWHIDE SCRIPT
//---------------------------------------------------------------------------------------------------------
function clearDefault(el) {
  if (el.defaultValue==el.value) el.value = ""
}

//---------------------------------------------------------------------------------------------------------
//   SHOWHIDE SCRIPT
//---------------------------------------------------------------------------------------------------------
function showhide(targetID) {
	//change target element mode
	var elementmode = document.getElementById(targetID).style;
	elementmode.display = (!elementmode.display) ? 'none' : '';
	}
function changetext(changee,oldText,newText) {
	//changes text in source element
	var elementToChange = document.getElementById(changee);
	elementToChange.innerHTML = (elementToChange.innerHTML == oldText) ? newText : oldText;
	}
function workforchange(targetID,sourceID,oldContent,newContent) {
	showhide(targetID);
	changetext(sourceID,oldContent,newContent);
	}
function openall() {
	workforchange('p1','changer1','-','+')
	workforchange('p2','changer2','-','+')
	workforchange('p3','changer3','-','+')
	}

//---------------------------------------------------------------------------------------------------------
//  DATE AND TIME SCRIPT - Courtesy of SimplytheBest.net - http://simplythebest.net/scripts/
//---------------------------------------------------------------------------------------------------------
function makeArray(n) {
	this.length = n
	return this
	}
monthNames = new makeArray(12)
monthNames[1] = "January"
monthNames[2] = "February"
monthNames[3] = "March"
monthNames[4] = "April"
monthNames[5] = "May"
monthNames[6] = "June"
monthNames[7] = "July"
monthNames[8] = "August"
monthNames[9] = "September"
monthNames[10] = "October"
monthNames[11] = "November"
monthNames[12] = "December"

function dateString(oneDate) {
	var theMonth = monthNames[oneDate.getMonth() + 1]
	var theYear = oneDate.getFullYear()
	return theMonth + " " + oneDate.getDate() + ", " + theYear
}

//---------------------------------------------------------------------------------------------------------
//   TABS SCRIPT
//---------------------------------------------------------------------------------------------------------
function switchid(id,section){
	//document.write(section);
	switch (section){
		case (section="avp"):
			var ids=new Array('avp1','avp2','avp3');
  			break
		case (section="w2w"):
			var ids=new Array('w2w1','w2w2','w2w3');
  			break
		case (section="mrf"):
			var ids=new Array('mrf1','mrf2','mrf3');
  			break
		case (section="mf"):
			var ids=new Array('mf1','mf2','mf3');
  			break
		case (section="ub"):
			var ids=new Array('ub1','ub2','ub3');
  			break
		case (section="rh"):
			var ids=new Array('rh1','rh2','rh3');
  			break
		case (section="yn"):
			var ids=new Array('yn1','yn2','yn3');
  			break
		case (section="hww"):
			var ids=new Array('hww1','hww2','hww3','hww4','hww5','hww6','hww7');
  			break
	}
	for (var i=0;i<ids.length;i++){
		//document.write(ids);
		hidediv(ids[i]);
	}
	showdiv(id);
}
function hidediv(id) {	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}
function showdiv(id) {	//safe function to show an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
		}
		else { // IE 4
			document.all.id.style.display = 'block';
		}
	}
}


//---------------------------------------------------------------------------------------------------------
//   VALIDATION SCRIPT
//---------------------------------------------------------------------------------------------------------
// CHECK LOGIN FORM SCRIPT
function checkClientLogin(theForm) {
    var why = "";
    why += checkPassword(theForm.password.value);
    why += checkUsername(theForm.username.value);
    if (why != "") {
       alert(why);
       return false;
    }
	return true;
}
// CHECK CONTACT FORM SCRIPT
function checkContactForm(theForm) {

    var why = "";
	why += visitorIsEmpty(theForm.visitor.value);
	//document.write (why);
    why += checkEmail(theForm.mail.value);
	//document.write (why);
    why += checkPhone(theForm.phone.value);
	//document.write (why);
    why += messageIsEmpty(theForm.message.value);
	//document.write (why);
    //why += isDifferent(theForm.different.value);
	//document.write (why);
    if (why != "") {
       alert(why);
       return false;
    }
	return true;
}
// non-empty textbox  - NAME FIELD
function visitorIsEmpty(strng) {
	var error = "";
	if (strng.length == 0) {
     	error = "You have not entered your name.\n"
	}
	return error;	  
}

// CHECK DIRECTIONS FORM SCRIPT
function checkDirectionsForm(theForm) {
    var why = "";
	why += postcodeIsEmpty(theForm.saddr.value);
    if (why != "") {
       alert(why);
       return false;
    }
	return true;
}
// email
function checkEmail (strng) {
	var error="";
	if (strng == "") {
  		error = "You didn't enter an email address.\n";
	}
	var emailFilter=/^.+@.+\..{2,3}$/;
	if (!(emailFilter.test(strng))) { 
       error = "Please enter a valid email address.\n";
	}
	else {
	//test email for illegal characters
		var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
    	if (strng.match(illegalChars)) {
			error = "The email address contains illegal characters.\n";
       }
    }
	return error;    
}

// phone number - strip out delimiters and check for 10 digits
function checkPhone (strng) {
	var error = "";
	if (strng == "") {
		error = "You didn't enter a phone number.\n";
	}
	else {
		var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    	if (isNaN(parseInt(stripped))) {
       		error = "The phone number contains illegal characters.\n";
    	}
	}
    //if (!(stripped.length == 10)) {
	//	error = "The phone number is the wrong length. Make sure you included an area code.\n";
    //} 
	return error;
}

// password - between 6-8 chars, uppercase, lowercase, and numeral
function checkPassword (strng) {
	var error = "";
	if (strng == "") {
   		error = "You didn't enter a password.\n";
	}
    var illegalChars = /[\W_]/; // allow only letters and numbers
	if ((strng.length < 6) || (strng.length > 10)) {
       	error = "The password is the wrong length.\n";
   	}
   	else if (illegalChars.test(strng)) {
   		error = "The password contains illegal characters.\n";
   	} 
    else if (!((strng.search(/(a-z)+/)) && (strng.search(/(A-Z)+/)) && (strng.search(/(0-9)+/)))) {
   		error = "The password must contain at least one uppercase letter, one lowercase letter, and one numeral.\n";
    }  
	return error;    
}    
// username - 4-10 chars, uc, lc, and underscore only.
function checkUsername (strng) {
	var error = "";
	if (strng == "") {
    	error = "You didn't enter a username.\n";
	}
    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if ((strng.length < 6) || (strng.length > 10)) {
        error = "The username is the wrong length.\n";
    }
    else if (illegalChars.test(strng)) {
    	error = "The username contains illegal characters.\n";
    } 
return error;
}
// non-empty textbox - POSTCODE
function postcodeIsEmpty(strng) {
	var error = "";
	if (strng.length == 0) {
     	error = "You must enter your postcode.\n"
	}
	return error;	  
}

// non-empty textbox
function messageIsEmpty(strng) {
	var error = "";
	if (strng.length == 0) {
     	error = "You have not entered a message.\n"
	}
	return error;	  
}
// was textbox altered
function isDifferent(strng) {
	var error = ""; 
	if (strng != "Can\'t touch this!") {
     error = "You altered the inviolate text area.\n";
	}
	return error;
}

// exactly one radio button is chosen
function checkRadio(checkvalue) {
	var error = "";
	if (!(checkvalue)) {
		error = "Please check a radio button.\n";
    }
	return error;
}

// valid selector from dropdown list
function checkDropdown(choice) {
	var error = "";
    if (choice == 0) {
    	error = "You didn't choose an option from the drop-down list.\n";
    }    
	return error;
}    