function doSearch() {
	var searchString = '';
	var contractType = document.getElementById( 'search_contract_type' );
	var location = document.getElementById( 'search_location' );
	var sector = document.getElementById( 'search_sector' );
	var keywords = document.getElementById( 'search_keywords' );
	if ( sector.value != '0' ) {
		searchString += sector.value;
	}
	if ( ( keywords.value != '' ) && ( keywords.value != ' - Keywords' ) ) {
		searchString += '/%20search::' + keywords.value;
	}
	if ( contractType.value != '0' ) {
		searchString += '/%20Placement::' + contractType.value;
	}
	if ( location.value != '0' ) {
		searchString += '/%20Location::' + location.value;
	}
	// if we have something to search on, do the search
	if ( searchString != '' ) {
		if ( sector.value == '0' ) {
			searchString = '/jobs' + searchString;
		}
		document.location = searchString;
	} else {
		alert('Please enter some search criteria');
	}
	return false;
}

function refreshPage() {
	document.location = document.location;
}

function createJobAlert() {
	var searchString = '';
	var alertName = document.getElementById( 'alert_name' ).value;
	if ( alertName != '' ) {
		var contractType = document.getElementById( 'alert_contract_type' );
		var location = document.getElementById( 'alert_location' );
		var sector = document.getElementById( 'alert_sector' );
		var keywords = document.getElementById( 'alert_keywords' );
		if ( sector.value != '0' ) {
			searchString += sector.value;
		}
		if ( ( keywords.value != '' ) && ( keywords.value != ' - Keywords' ) ) {
			searchString += '/%20search::' + keywords.value;
		}
		if ( contractType.value != '0' ) {
			searchString += '/%20Placement::' + contractType.value;
		}
		if ( location.value != '0' ) {
			searchString += '/%20Location::' + location.value;
		}
		// if we have something to search on, do the search
		if ( searchString != '' ) {
			if ( sector.value == '0' ) {
				searchString = '/jobs' + searchString;
			}
			setCookie( 'jobSearchPath', searchString );
			saveJobSearch( alertName, refreshPage );
		} else {
			alert('Please enter some search criteria');
		}
	} else {
		alert( 'Please enter a name for your alert' );
	}
	return false;
}

function inputBoxFocus( input, defaultText, passwordField ) {
	if( typeof( passwordField ) != 'undefined' ) {
		document.getElementById( passwordField ).style.display = '';
		document.getElementById( passwordField+'_text' ).style.display = 'none';
		document.getElementById( passwordField ).focus();
	} else {
		if( input.value == defaultText ) {
			input.value = '';
		}
	}
}

function inputBoxBlur( input, defaultText, passwordField ) {
	if( input.value == '' ) {
		if( typeof( passwordField ) != 'undefined' ) {
			document.getElementById( passwordField ).style.display = 'none';
			document.getElementById( passwordField+'_text' ).style.display = '';
		} else {
			input.value = defaultText;
		}
	}
}

function saveJobSearch( title, callbackFunc ) {
	
	var sUrl = getCookie( 'jobSearchPath' );
	sUrl += '/%20save::' + title;
	
	// add our flag so the template knows to return a status message rather than the whole page
	sUrl += '?savingSearch=true';
	
	var handleSuccess = function( result ){
		if ( result.responseText !== undefined ) {
			if ( result.responseText == 'success' ) {
				if ( document.getElementById('jobSearchNameArea') ) {
					document.getElementById('jobSearchNameArea').style.display = 'none';
				}
				alert( 'Your job alert has been successfully created!' );
				if ( typeof callbackFunc != 'undefined' ) {
					callbackFunc();
				}
			} else {
				alert( 'Sorry, an error occured saving your search, please try again.' );
				return false;
			}
		}
	}
	
	var handleFailure = function(o){
		if(o.responseText !== undefined){
			alert( 'Sorry, an error occured saving your search, please try again.' );
		}
	}
	
	var callback =
	{
	  success:handleSuccess,
	  failure: handleFailure
	};
	
	var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback); 
}


function setCookie( name, value ) {
	document.cookie = name + '=' + value + ';path=/;';
}

function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) {
		return null;
	}
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) {
		end = document.cookie.length;
	}
	return unescape( document.cookie.substring( len, end ) );
}
