function trim( str ) {
	while( str.length > 0 && str.indexOf( " " ) == 0 )
		str = str.substr( 1 );
	while( str.length > 0 && str.lastIndexOf( " " ) == str.length - 1 )
		str = str.substr( 0 , str.length - 1 );
	return str;
}

function isHour( str ) {
	if( str.match( /^[0-2][0-9]:00$/ ) )
		return true;
	return false;
}

function isPhoneNumber( str ) {
	str = str.replace( /[^0-9]/ , "" );
	if( str.length < 7 )
		return false;
	return true;
}

function isEmailAddress( str ) {
	if( str.match( /^[a-zA-Z0-9\-_\.]+@[a-zA-Z0-9\-]+(\.[a-zA-Z0-9\-]+)+$/ ) )
		return true;
	return false;
}

function confirmSubmission() {
	if(
		!okName() ||
		!okGender() ||
		!okStatus() ||
		!okOccupation() ||
		!okJobDescription() ||
		!okWorkHours() ||
		!okCity() ||
		!okPhone() ||
		!okEmail() ||
		!okPhoto()
	)
		return false;
	return true;
}

function okName() {
	var row_name = document.getElementById( "RowName" );
	var input_name = row_name.getElementsByTagName( "input" )[0];
	if( trim( input_name.value ) == "" ) {
		alert( "Please input your name." );
		input_name.focus();
		return false;
	}
	return true;
}

function okGender() {
	var row_gender = document.getElementById( "RowGender" );
	var inputs = row_gender.getElementsByTagName( "input" );
	for( var x = 0 ; x < inputs.length ; x++ )
		if( inputs[x].checked == true && inputs[x].value != "" )
			return true;
	alert( "Please select your gender." );
	return false;
}

function okStatus() {
	var row_status = document.getElementById( "RowStatus" );
	var inputs = row_status.getElementsByTagName( "input" );
	for( var x = 0 ; x < inputs.length ; x++ )
		if( inputs[x].checked == true && inputs[x].value != "" )
			return true;
	alert( "Please select a status." );
	return false;
}

function okOccupation() {
	var row_occupation = document.getElementById( "RowOccupation" );
	var input_occupation = row_occupation.getElementsByTagName( "input" )[0];
	if( trim( input_occupation.value ) == "" ) {
		alert( "Please input your occupation." );
		input_occupation.focus();
		return false;
	}
	return true;
}

function okJobDescription() {
	var row_job_description = document.getElementById( "RowJobDescription" );
	var textarea_job_description = row_job_description.getElementsByTagName( "textarea" )[0];
	if( trim( textarea_job_description.value ) == "" ) {
		alert( "Please input your job description." );
		textarea_job_description.focus();
		return false;
	}
	return true;
}

function okWorkHours() {
	var row_work_hours = document.getElementById( "RowWorkHours" );
	var selects = row_work_hours.getElementsByTagName( "select" );
	for( var x = 0 ; x < selects.length ; x++ ) {
		if( !isHour( selects[x].options[selects[x].selectedIndex].value ) ) {
			alert( "Please input your work hours." );
			selects[x].focus();
			return false;
		}
	}
	return true;
}

function okCity() {
	var row_city = document.getElementById( "RowCity" );
	var select_city = row_city.getElementsByTagName( "select" )[0];
	if( select_city.options[select_city.selectedIndex].value == "" ) {
		alert( "Please select your city of residence." );
		select_city.focus();
		return false;
	}
	return true;
}

function okPhone() {
	var row_cell_phone = document.getElementById( "RowCellPhone" );
	var row_home_phone = document.getElementById( "RowHomePhone" );
	var row_work_phone = document.getElementById( "RowWorkPhone" );
	var input_cell_phone = row_cell_phone.getElementsByTagName( "input" )[0];
	var input_home_phone = row_home_phone.getElementsByTagName( "input" )[0];
	var input_work_phone = row_work_phone.getElementsByTagName( "input" )[0];
	if(
		!isPhoneNumber( input_cell_phone.value ) &&
		!isPhoneNumber( input_home_phone.value ) &&
		!isPhoneNumber( input_work_phone.value )
	) {
		alert( "Please input at least one phone number." );
		input_cell_phone.focus();
		return false;
	}
	return true;
}

function okEmail() {
	var row_cell_email = document.getElementById( "RowCellEmail" );
	var row_home_email = document.getElementById( "RowHomeEmail" );
	var row_work_email = document.getElementById( "RowWorkEmail" );
	var input_cell_email = row_cell_email.getElementsByTagName( "input" )[0];
	var input_home_email = row_home_email.getElementsByTagName( "input" )[0];
	var input_work_email = row_work_email.getElementsByTagName( "input" )[0];
	if(
		!isEmailAddress( input_cell_email.value ) &&
		!isEmailAddress( input_home_email.value ) &&
		!isEmailAddress( input_work_email.value )
	) {
		alert( "Please input at least one e-mail address." );
		input_cell_email.focus();
		return false;
	}
	return true;
}

function okPhoto() {
	var row_photo = document.getElementById( "RowUploadPhoto" );
	var input_photo = row_photo.getElementsByTagName( "input" )[0];
	if( input_photo.value == "" ) {
		alert( "Please select a photo to upload." );
		input_photo.focus();
		return false;
	}
	return true;
}

