function trim(inputString) {
	if (typeof inputString != "string") { return inputString; }
		var retValue = inputString;
		var ch = retValue.substring(0, 1);
		while (ch == " ") { 
			retValue = retValue.substring(1, retValue.length);
			ch = retValue.substring(0, 1);
		}
		ch = retValue.substring(retValue.length-1, retValue.length);
		while (ch == " ") { 
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}
	while (retValue.indexOf("  ") != -1) {
		retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); 
	}
	return retValue; 
} 				

function validate_submit()
{
	try
	{
		var eml = document.getElementById( 'fld_email');
		var confeml = document.getElementById( 'fld_confirmemail');
		
		if ( ( eml.value != null && eml.value == 'Email address' ) || 
		     ( confeml.value != null && confeml.value == 'Confirm email address' ) )
		{
//				document.getElementById( 'errors' ).style.display = "block";
			document.getElementById( 'errormsg' ).innerHTML = "* The email field and the confirmation email field are required";
			return false;
		}

		if ( eml.value == null || trim( eml.value ) == '' || confeml.value == null || trim( confeml.value ) == '' )
		{
			document.getElementById( 'errormsg' ).innerHTML = "* The email field and the confirmation email field are required";
			return false;
		}
		
		if ( eml.value != confeml.value  )
		{
			document.getElementById( 'errormsg' ).innerHTML = "* The email and the confirmation email must match";
			return false;
		}

		if ( (v = validate_email( eml.value ))!= true )
		{
			document.getElementById( 'errormsg' ).innerHTML = v;
			return false;
		}
//		if ( eml.value.indexOf( '@' ) < 0  )
//		{
//			document.getElementById( 'errormsg' ).innerHTML = "* The email field is not a valid email";
//			return false;
//		}
			
	} catch ( ex ) {
		
	}
	return true;
}


function validate_changeemail()
{
	try
	{
		var old_eml = document.getElementById( 'fld_old_email').value;
		var new_eml = document.getElementById( 'fld_new_email').value;
		var new_confirmemail = document.getElementById( 'fld_new_confirmemail').value;
		
		if ( old_eml == null || new_eml == null || new_confirmemail == null ||
			 old_eml == '' || new_eml == '' || new_confirmemail == '' || 
			 old_eml == 'Old email address' || new_eml == 'New email address' || new_confirmemail == 'Confirm email address' )
		{
			document.getElementById( 'changeemailerror' ).innerHTML = "* All fields are required";
			return false;
		}
		
		if ( new_eml != new_confirmemail  )
		{
			document.getElementById( 'changeemailerror' ).innerHTML = "* The new email and the confirmation email must match";
			return false;
		}

		if ( old_eml.indexOf( '@' ) < 0 )
		{
			document.getElementById( 'changeemailerror' ).innerHTML = "* The email field is not a valid email";
			return false;
		}
		
		if ( (v = validate_email( new_eml ))!= true )
		{
			document.getElementById( 'changeemailerror' ).innerHTML = v;
			return false;
		}
	} catch ( ex ) {
		
	}
	return true;
}
function validate_manageemail()
{
	try
	{
		var eml = document.getElementById( 'fld_stop_email');
		
		if ( eml.value == null || trim( eml.value ) == '' || ( eml.value != null && eml.value == 'Email address' ) )
		{
			document.getElementById( 'manageemailerror' ).innerHTML = "* The email field is required";
			return false;
		}

		if ( (v = validate_email( eml.valu ))!= true )
		{
			document.getElementById( 'manageemailerror' ).innerHTML = v;
			return false;
		}
	} catch ( ex ) {
		
	}
	return true;
}

function validate_managechangeemail()
{
	try
	{
		var eml = document.getElementById( 'fld_changeemail');
		
		if ( eml.value == null || trim( eml.value ) == '' || ( eml.value != null && eml.value == 'Email address' ) )
		{
			document.getElementById( 'manageemailerror' ).innerHTML = "* The email field is required";
			return false;
		}

		if ( (v = validate_email( eml.value ))!= true )
		{
			document.getElementById( 'manageemailerror' ).innerHTML = v;
			return false;
		}
	} catch ( ex ) {
		alert( ex );
	}
	return true;
}

function fldfocus( fld )
{
	if ( fld.value == "Email address" || 
	     fld.value == "Your Name" ||
	     fld.value == "Old email address" ||
	     fld.value == "New email address" ||
	     fld.value == " Search..."
	   )
	{
		fld.style.color = "#000"
		fld.value = ""
	}
}

function fldblur( fld, defaulttext )
{
	if ( fld.value == '' )
	{
		fld.style.color = "#666";
		fld.value = defaulttext;
	}
}

function showchangeemail( )
{
	document.getElementById( 'box-changeemail' ).style.display = "block";
	document.getElementById( 'box-manageemail' ).style.display = "none";
}

function showmanageemail( )
{
	document.getElementById( 'box-manageemail' ).style.display = "block";
	document.getElementById( 'box-changeemail' ).style.display = "none";
}

function hidechangeemail( )
{
	document.getElementById( 'box-changeemail' ).style.display = "none";
}

function hidemanageemail( )
{
	document.getElementById( 'box-manageemail' ).style.display = "none";
}

function validate_email(addr,man) {
	if (addr == '' && man) {
	   return 'Email address is required';
	}
	if (addr == '') return true;
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	for (i=0; i<invalidChars.length; i++) {
	   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
	      return 'Email address contains invalid characters';
	   }
	}
	for (i=0; i<addr.length; i++) {
	   if (addr.charCodeAt(i)>127) {
	      return "Email address contains non ascii characters.";
	   }
	   if ( i == (addr.length-1) && addr.charAt(i)==',') {
	      return 'Email cannot end in a comma';	   	
	   }
	   if ( i == (addr.length-1) && addr.charAt(i)=='.') {
	      return 'Email cannot end in a period';	   	
	   }
	}
	
	var atPos = addr.indexOf('@',0);
	if (atPos == -1) {
	   return 'Email address must contain an @';
	}
	if (atPos == 0) {
	   return 'Email address must not start with @';
	}
	if (addr.indexOf('@', atPos + 1) > - 1) {
	   return 'Email address must contain only one @';
	}
	if (addr.indexOf('.', atPos) == -1) {
	   return 'Email address must contain a period in the domain name';
	}
	if (addr.indexOf('@.',0) != -1) {
	   return 'A period must not immediately follow @ in email address';
	}
	if (addr.indexOf('.@',0) != -1){
	   return 'A period must not immediately precede @ in email address';
	}
	if (addr.indexOf('..',0) != -1) {
	   return 'Two periods must not be adjacent in email address';
	}
	return true;
}