//----------------------------------------------------------------
// <form> related functions
//----------------------------------------------------------------
function changeStyle(x, fail)
{
	// Highlights incomplete required fields
	if (document.getElementById)
	{
		var ctrl = document.getElementById(x);
		if(fail)
		{
			ctrl.style.background='#cf9';
		}
		else
		{
			ctrl.style.background='#fff';
		}
	}
}

function resetStyle(frm)
{
	// Resets background of required fields
	if (document.getElementById)
	{
		for(var i=0; i < frm.length; i++)
		{
			var e = frm.elements[i];
			if(e.type == "text" || e.type == "textarea" || e.type == "select-one" || e.type == "password")
			{
				changeStyle(e.name, false);
			}
		}
	}
}

function addHelp()
{
	// Add Form Help (Author: Gez Lemon - www.juicystudio.com)
	var strID, objHelp;

	// Check we're working with a DOM compliant browser
	if (document.getElementById && document.appendChild && document.removeChild)
	{
		var objHelpform = document.getElementById('f_search');
		var objAnchors = objHelpform.getElementsByTagName('a');
		
		// Iterate through all anchors in the form
		for (var iCounter=0; iCounter<objAnchors.length; iCounter++)
		{
			if (objAnchors[iCounter].className != 'leave')
			{
				// Locate the associated help text's container and hide it
				strID = getIDFromHref(objAnchors[iCounter].href);
				objHelp = document.getElementById(strID);
				objHelp.style.display = 'none';
				
				// Add events to reveal/hide
				objAnchors[iCounter].onclick = function(event){return expandHelp(this, event);}
				objAnchors[iCounter].onkeypress = function(event){return expandHelp(this, event);}
				
				// Move beneath current form field
				objAnchors[iCounter].parentNode.appendChild(objHelp);
			}
		}
		
		// Remove the remainder of the old help section
		var objOldnode = document.getElementById('helpcontainer');
    	objOldnode.parentNode.removeChild(objOldnode);
		
		// Release memory to prevent IE memory leak
		// Thanks to Mark Wubben <http://novemberborn.net/>
		// for highlighting the issue
		objHelpform = null;
		objHelp = null;
		objAnchors = null;
	}
}

function getIDFromHref(strHref)
{
	// Return the ID of the element from the "href" attribute
	var iOffset = strHref.indexOf('#') + 1;
	var iEnd = strHref.length;
	return strHref.substring(iOffset, iEnd);
}

function expandHelp(objAnchor, objEvent)
{
	var iKeyCode;

	// If from the keyboard, check the user is
	// activating it rather than tabbing through
	if (objEvent && objEvent.type == 'keypress')
	{
		if (objEvent.keyCode)
			iKeyCode = objEvent.keyCode;
		else if (objEvent.which)
			iKeyCode = objEvent.which;
		
		if (iKeyCode != 13 && iKeyCode != 32)
			return true;
	}

	strID = getIDFromHref(objAnchor.href);
	objHelp = document.getElementById(strID);

	// Toggle on and off
	if (objHelp.style.display == 'none')
		objHelp.style.display = 'block';
	else
		objHelp.style.display = 'none';
	return false;
}

function setAutoSuggest()
{
	// Autosuggests Therapies - main functions in autocomplete.js
	var options = {script:'therapies_xml.asp?',varname:'therapy',json:false,shownoresults:false,minchars:2,timeout:3000};
	var as = new bsn.AutoSuggest('sTherapy', options);
}

function setForm()
{
	// Sets form(s) by calling relevant functions
	addHelp();
	setAutoSuggest();
}

function ShowProgress()
{
	// Show upload progress for profile image
	var barRef = document.imgFrm.barRef.value;
	var strAppVersion = navigator.appVersion;

	if (document.imgFrm.Image.value != "")
	{
		if (strAppVersion.indexOf('MSIE') != -1 && strAppVersion.substr(strAppVersion.indexOf('MSIE')+5,1) > 4)
		{
			winstyle = "dialogWidth=385px; dialogHeight:140px; center:yes";
			window.showModelessDialog(barRef + '&b=IE',null,winstyle);
		}
		else
		{
			window.open(barRef + '&b=NN','','width=370,height=115', true);
		}
 	}
	return true;
}

//----------------------------------------------------------------
// <form> validation functions
//----------------------------------------------------------------
function textCounter(field,maxlimit)
{
	if (field.value.length > maxlimit)
	{
		// Reached Character Limit!
		field.value = field.value.substring(0, maxlimit);
		alert("You have reached the " + maxlimit + " character limit");
	}
}

function vSearchSwapPP(frm)
{
	if(frm.sName.value == "" && frm.sTherapy.value == "")
	{
		alert("You must enter a name or therapy to search this directory.");
		return false;
	}
	else
	{
		return true;
	}
}

function vSearchCSF(frm)
{
	if(frm.sTherapy.value == "")
	{
		alert("You must enter a therapy to search this directory.");
		return false;
	}
	else
	{
		return true;
	}
}

function vJoin(frm)
{
	// Validates Registration form
	resetStyle(frm);

	var err_msg = "Please complete the following:\n\n";
	var err_flag = false;
	var i = 1;

	if(frm.Forename.value == "")
	{
		err_msg += i + "> Your Forename.\n";
		err_flag = true;
		i++;
		changeStyle('Forename', true);
	}
	if(frm.Surname.value == "")
	{
		err_msg += i + "> Your Surname.\n";
		err_flag = true;
		i++;
		changeStyle('Surname', true);
	}
	if(frm.CityTownVillage.value == "")
	{
		err_msg += i + "> Your City/Town/Village.\n";
		err_flag = true;
		i++;
		changeStyle('CityTownVillage', true);
	}
	if(frm.County.value == "")
	{
		err_msg += i + "> Your County.\n";
		err_flag = true;
		i++;
		changeStyle('County', true);
	}
	if(frm.PostCode.value == "")
	{
		err_msg += i + "> Your Postcode.\n";
		err_flag = true;
		i++;
		changeStyle('PostCode', true);
	}
	if(frm.PrimaryPhone.value == "")
	{
		err_msg += i + "> Your PrimaryPhone.\n";
		err_flag = true;
		i++;
		changeStyle('PrimaryPhone', true);
	}
	if(frm.Email.value == "")
	{
		err_msg += i + "> Your Email Address.\n";
		err_flag = true;
		i++;
		changeStyle('Email', true);
	}	
	else
	{
		// Check email address supplied
		if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(frm.Email.value))
		{
			// Valid Email Address - check email matches 'confirm email' field...
			if(frm.Email.value != frm.ConfirmEmail.value)
			{
				// Email Mismatch - flag error
				err_msg += i + "> The email addresses you entered do not match.\n";
				err_flag = true;
				i++;
				changeStyle('Email', true);
				changeStyle('ConfirmEmail', true);
			}
		}
		else
		{
			// Invalid Email Address
			err_msg += i + "> Your email address (" + frm.Email.value + ") appears to be invalid.\n";
			err_flag = true;
			i++;
			changeStyle('Email', true);
		}
	}
	if(frm.ConfirmEmail.value == "")
	{
		err_msg += i + "> Confirm your email address.\n";
		err_flag = true;
		i++;
		changeStyle('ConfirmEmail', true);
	}
	var therapy_flag = false;
	for(var x = 0; x < frm.Therapies.length; x++)
	{
		if(frm.Therapies[x].checked == true)
		{
			therapy_flag = true;
		}
	}
	if(therapy_flag == false)
	{
		if(frm.OtherTherapies.value == "")
		{
			// havent selected a skill or provided more info
			err_msg += i + "> You have not selected any Therapies.\n";
			err_flag = true;
			i++;
		}
	}
	if(frm.Qualifications.value == "")
	{
		err_msg += i + "> Your Qualifications.\n";
		err_flag = true;
		i++;
		changeStyle('Qualifications', true);
	}
	if(frm.Associations.value == "")
	{
		err_msg += i + "> Professional Association Memberships.\n";
		err_flag = true;
		i++;
		changeStyle('Associations', true);
	}
	// Where there any errors?
	if(err_flag)
	{
		alert(err_msg);
		return false;
	}
	else
	{
		return true;
	}
}

function vLogin(frm)
{
	// Validates Login Form
	resetStyle(frm);

	var err_msg = "Please complete the following:\n\n";
	var err_flag = false;
	var i = 1;

	if(frm.mEmail.value == "")
	{
		err_msg += i + "> Your Email Address.\n";
		err_flag = true;
		i++;
		changeStyle('mEmail', true);
	}
	else
	{
		// Check email address supplied
		if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(frm.mEmail.value))
		{
			// Valid Email Address - do nothing
		}
		else
		{
			// Invalid Email Address
			err_msg += i + "> Your email address (" + frm.mEmail.value + ") appears to be invalid.\n";
			err_flag = true;
			i++;
			changeStyle('mEmail', true);
		}
	}
	if(frm.mPass.value == "")
	{
		err_msg += i + "> Your Password.\n";
		err_flag = true;
		i++
		changeStyle('mPass', true);
	}
	// Where there any errors?
	if(err_flag)
	{
		alert(err_msg);
		return false;
	}
	else
	{
		return true;
	}
}

function vReminder(frm)
{
	// Validate Password Reminder form
	resetStyle(frm);

	var err_msg = "Please complete the following:\n\n";
	var err_flag = false;
	var i = 1;

	if(frm.rEmail.value == "")
	{
		err_msg += i + "> Your Email Address.\n";
		err_flag = true;
		i++;
		changeStyle('rEmail', true);
	}
	else
	{
		// Check email address supplied
		if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(frm.rEmail.value))
		{
			// Valid Email Address - do nothing
		}
		else
		{
			// Invalid Email Address
			err_msg += i + "> Your email address (" + frm.rEmail.value + ") appears to be invalid.\n";
			err_flag = true;
			i++;
			changeStyle('rEmail', true);
		}
	}
	// Where there any errors?
	if(err_flag)
	{
		alert(err_msg);
		return false;
	}
	else
	{
		return true;
	}
}

function checkPass(frm)
{
	// Validates password spelling
	if(frm.Pass1.value == "" | frm.Pass2.value == "")
	{
		alert("You must enter your password into both fields.");
		return false;
	}
	else
	{
		if(frm.Pass1.value != frm.Pass2.value)
		{
			alert("Your passwords do not match!");
			return false;
		}
	}
}

function vSwap(frm)
{
	// Validates acceptance of T&C
	if(frm.Terms.checked == false)
	{
		alert("You must agree to the Terms & Conditions to activate your SWAP entry.");
		return false;
	}
	else
	{
		return true;
	}
}

//----------------------------------------------------------------
// Miscellaneous functions
//----------------------------------------------------------------
function toggleDiv(id)
{
	// Toggle visibility of specified <div>
	if (document.getElementById)
	{
		var row = document.getElementById('hiddenAddresses');
		if(row.style.display == 'block')
		{
			row.style.display = 'none';
		}
		else
		{
			row.style.display = 'block';
		}
	}
}