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 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() ||
		!okBirthdate() ||
		!okCitizenship() ||
		!okHeight() ||
		!okWeight() ||
		!okStatus() ||
		!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 okBirthdate() {
	var row_birthdate = document.getElementById( "RowBirthdate" );
	var selects = row_birthdate.getElementsByTagName( "select" );
	var select_birthyear = selects[0];
	if( select_birthyear.options[select_birthyear.selectedIndex].value == "" ) {
		alert( "Please input your birthdate." );
		select_birthyear.focus();
		return false;
	}
	var select_birthmonth = selects[1];
	if( select_birthmonth.options[select_birthmonth.selectedIndex].value == "" ) {
		alert( "Please input your birthdate." );
		select_birthmonth.focus();
		return false;
	}
	var select_birthday = selects[2];
	if( select_birthday.options[select_birthday.selectedIndex].value == "" ) {
		alert( "Please input your birthdate." );
		select_birthday.focus();
		return false;
	}
	return true;
}

function okCitizenship() {
	var row_citizenship = document.getElementById( "RowCitizenship" );
	var input_citizenship = row_citizenship.getElementsByTagName( "input" )[0];
	if( trim( input_citizenship.value ) == "" ) {
		alert( "Please input your citizenship." );
		input_citizenship.focus();
		return false;
	}
	return true;
}

function okHeight() {
	var row_height = document.getElementById( "RowHeight" );
	var input_height = row_height.getElementsByTagName( "input" )[0];
	if( trim( input_height.value ) == "" ) {
		alert( "Please input your height." );
		input_height.focus();
		return false;
	}
	return true;
}

function okWeight() {
	var row_weight = document.getElementById( "RowWeight" );
	var input_weight = row_weight.getElementsByTagName( "input" )[0];
	if( trim( input_weight.value ) == "" ) {
		alert( "Please input your weight." );
		input_weight.focus();
		return false;
	}
	return true;
}

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 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;
}
