/* JS Functions */
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() {
	clearDefaults();
	
	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 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 checkFocus( objField, sValueToCheck ) {
	if ( objField.value == sValueToCheck ) {
		objField.value = "";
	}
}

function checkValue( objField, sValueToCheck ) {
	if ( objField.value == "" ) {
		objField.value = sValueToCheck;
	}
}

function checkSendCV() {
	with( document.job_apply ) {
		/* Check the values of the fields */
		if ( name.value == "Your Name" || email.value == "Your E-mail" || phone.value == "Your Phone" ) {
			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;
		}
		
		/* If submitting a CV */
		if ( cv_type[0].checked ) {
			/* 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 handleCVType( sValue ) {
	var bAttach = ( sValue == "1" );
	document.getElementById( "attach_cv" ).style.display = ( bAttach ) ? '' : 'none';
}

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 updateCaptcha() {
	var img = document.getElementById( "captchaImg" );
	img.src = "/libs/captcha/captcha.php";
}
