function trim( str )
{
   return str.replace( /^\s*|\s*$/g, "" );
}

function focusEmail( e ) {
	if ( e.value == "E-mail Address" ) {
		e.value = "";
	}
}

function blurEmail( e ) {
	if ( e.value == "" ) {
		e.value = "E-mail Address";
	}
}

function isValidEmail( s ) {
	var bValid = true;
	
	// Check for @ symbol
	bValid = ( s.indexOf("@") != -1 );
	// Check for . after the @
	bValid = ( bValid && ( s.lastIndexOf(".") > s.indexOf("@") ) );
	// Check . is not last character
	bValid = ( bValid && ( s.lastIndexOf(".") < s.length - 2 ) );
	// Check @ is not first character
	bValid = ( bValid && ( s.indexOf(".") > 1 && s.indexOf("@") > 1 ) );
	
	return bValid;
}

function copySelectToCheckbox() {
	with( document.news_sign ) {
		
		// Validate the user's e-mail before going any further
		if ( ! isValidEmail( email.value ) ) {
			alert("Please enter a valid e-mail address.");
			email.value = "";
			email.focus();
			return false;
		}
	
		// Reset all the checkboxes
		var vInputs = getElementsByTagName("INPUT");
		for( i = 0; i < vInputs.length; i ++ ) {
			if ( vInputs[i].type == "checkbox" )
				vInputs[i].checked = false;
		}

		// Go through the drop-down and select all the checkboxes matching the user's selected options
		for( j = 1; j < drop_91.options.length; j ++ ) {
			if ( drop_91.options[j].selected ) {
				// Ensure the "Please Select" is turned off
				drop_91.options[0].selected = false;
				elements["check_" + drop_91.options[j].value + "_91"].checked = true;
			}
		}
	
		// If "Please Select" still selected, return false
		var bSuccessful = ! drop_91.options[0].selected
		if ( bSuccessful ) {
			doSignup.disabled = true;
		} else {
			alert("You must select at least one job sector");
		}
		return ( bSuccessful );
	}
}

function checkContactForm() {
	with( document.contact ) {
		// Ensure all mandatory fields are provided
		if ( trim( first_name.value ) == "" || trim( surname.value ) == "" || trim( phone.value ) == "" || trim( post_code.value ) == "" ) {
			alert("Please enter some information in ALL of the required fields in the form.");
			return false;
		}
		
		// Validate the user's e-mail
		if ( ! isValidEmail( email.value ) ) {
			alert("Please enter a valid reply e-mail address.");
			email.value = "";
			email.focus();
			return false;
		}		
	}
	
	return true
}

function updateTip( iTipId ) {
	location.href = "/tips/" + iTipId;
}

function checkSendCV() {
	clearDefaults();
	
	with( document.job_apply ) {
		/* Check the values of the fields */
		if ( name.value == "" || email.value == "" || phone.value == "" ) {
			alert("Please ensure you have completed all required fields before submitting your application.");
			return false;
		}
		
		if ( ! isValidEmail( email.value ) ) {
			alert("Please enter a valid e-mail address before sending your application." );
			email.focus();
			return false;
		}
		
		/* Check the name of the file that has been chosen as the person's CV */
		var sFilePath = elements["cv-file"].value;
		if ( sFilePath.indexOf( ".doc" ) == -1 ) {
			alert("You have not chosen a valid Microsoft Word document as your CV.\n\nPlease try again...");
			return false;
		}
	}
	return true;
}

function checkFocus( objField, sValueToCheck ) {
	if ( objField.value == sValueToCheck ) {
		objField.value = "";
	}
}

function checkValue( objField, sValueToCheck ) {
	if ( objField.value == "" ) {
		objField.value = sValueToCheck;
	}
}

var aPageDefaults = Array();

function buildDefaults() {
	with( document.forms[0] ) {
		for( i = 0; i < elements.length; i ++ ) {
			aPageDefaults[i] = Array( elements[i].id, elements[i].value );
		}
	}
}

function clearDefaults() {
	var obj;
	with( document.forms[0] ) {
		for( i = 0; i < aPageDefaults.length - 1; i ++ ) {
			obj = document.getElementById( aPageDefaults[i][0] );
			if ( obj && obj.value == aPageDefaults[i][1] ) {
				obj.value = "";
			}
		}
	}	
	return false;
}

function compPasswords() {
	with( document.forms[0] ) {
		if ( password.value == "" ) {
			alert("Please enter a password");
			return false;
		}
		if ( password.value != conf_password.value ) {
			alert("Please enter matching passwords");
			return false;
		}
	}
	
	return true;
}

function checkSMS() {
	var bSectorSelected = true;
	
	with( document.sms_sign ) {
		/*var ar = document.getElementsByName( "sms_sectors[]" );
		for( i = 0; i < ar.length; i ++ ) {
			if ( ar[i].checked ) {
				bSectorSelected = true;
				break;
			}
		}*/
		
		if ( mobile_no.value == "" || password.value == "" || ! bSectorSelected ) {
			alert("Please enter your mobile number, password and select your areas of interest");
			return false;
		}
		
		if ( ! isValidMobile( mobile_no.value ) ) {
			alert("Please enter a valid UK mobile number (no prefix)");
			return false;
		}
	}
	
	return compPasswords();
}

function isValidMobile( sNumber ) {
	if ( sNumber.length != 11 ) {
		return false;
	}
	
	for( i = 0; i < sNumber.length; i ++ ) {
		if ( isNaN( parseInt( sNumber.charAt( i ) ) ) ) {
			return false;
		}
	}
	
	return true;
}

function checkAdminSMS() {
	var bSectorSelected = false;
	
	if( document.send_sms.message.value.length == 0 ) {
		alert("Please enter a message to be sent");
		document.send_sms.message.focus();
		return false;
	}

	/*var ar = document.getElementsByName( "sms_sectors[]" );
	for( i = 0; i < ar.length; i ++ ) {
		if ( ar[i].checked ) {
			bSectorSelected = true;
			break;
		}
	}
	
	if ( ! bSectorSelected ) {
		alert("Please select at least one sector to receive this message");
		return false;
	} else*/
		return true;
}

function drawBannerFlash() {
	document.write( '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="800" height="81">' );
	document.write( '<param name="movie" value="/images/banner.swf" />' );
	document.write( '<param name="quality" value="high" />' );
	document.write( '<embed src="/images/banner.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="800" height="81"></embed>' );
	document.write( '</object>' );
}

function drawHomeFlash() {
	document.write( '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="450" height="301">' );
	document.write( '<param name="movie" value="/images/home_feature.swf" />' );
	document.write( '<param name="quality" value="high" />' );
	document.write( '<embed src="/images/home_feature.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="450" height="301"></embed>' );
	document.write( '</object>' );
}