/*
This is the SFTH Data Management JavaScript functions file.  Please consult
Daniel Krook before adding or editing functions.
*/

// Jumps to the selected item in the dropdown list
function jumpURL(dropdown) {
	location.href = dropdown.options[dropdown.selectedIndex].value;
}

// Popup for dog photo
function openPhotoUpload () {
	open("/inc/upload.php",
		 "SFTH",
		 "width=280,height=235,resizable=no,toolbar=no,status=no,menubar=no,scrollbars=no");
}

// Popup for bug reports
function openBugSubmit () {
	open("/bugs.php",
		 "SFTHbugs",
		 "width=550,height=500,resizable=no,toolbar=no,status=no,menubar=no,scrollbars=no");
}

// Popup for secondary dog tables
function openDogTables (strURL) {
	open(strURL,
		 "SFTHbugs",
		 "width=400,height=400,resizable=yes,toolbar=no,status=no,menubar=no,scrollbars=yes");
}

// The delete confirmation message
function confirmDelete () {
	if (!confirm('Are you sure you really want to delete this dog?')) {
		return false;
	}
}

// The delete confirmation message for the task manager
function confirmDeleteTask () {
	if (!confirm('Are you sure you really want to delete this task?')) {
		return false;
	}
}

// Populate the dog nick name field with what the user entered in the full name field
function repeatName () {
	if (document.frmInsertDog.dog_name_nick.value == "") {
		document.frmInsertDog.dog_name_nick.value = document.frmInsertDog.dog_name_full.value;
	}
}

// Populate the dog markings field with what the user entered in the color field
function repeatMarkings () {
	if (document.frmInsertDog.dog_appearance_markings.value == "") {
		document.frmInsertDog.dog_appearance_markings.value = document.frmInsertDog.dog_appearance_color.value;
	}
}

// Set the dogs literal weight to his relative weight and vice versa.
function syncSelect (objForm, strThisName, intThisIndex, strTargetName) {
	 objForm.elements[strTargetName].options.selectedIndex =
	 objForm.elements[strThisName].options.selectedIndex;
}

// Function to validate the Insert/Update forms
// call:  onsubmit="return validateInsert(this);"
function validateInsert (objForm) {
	var strErrorMsg = "";

	if (objForm.dog_sex.value == "") {
		strErrorMsg += "* Please select the dog's gender.\n";
	}

	if (objForm.dog_category.value == "") {
		strErrorMsg += "* Please select the dog's category.\n";
	}

	if (objForm.dog_name_full.value == "") {
		strErrorMsg += "* Please enter the dog's full name.\n";
	}

	if (objForm.dog_housetrained.value == "") {
		strErrorMsg += "* Please select whether the dog is housetrained.\n";
	}

	if (objForm.dog_appearance_color.value == "") {
		strErrorMsg += "* Please enter the dog's appearance/color.\n";
	}

	if (objForm.dog_size_desc.value == "") {
		strErrorMsg += "* Please select the dog's relative weight.\n";
	}

	if (objForm.dog_size_pounds.value == "") {
		strErrorMsg += "* Please select the dog's weight in pounds.\n";
	}

	// Print the error message if it exists. Otherwise lets the form submit.
	if (strErrorMsg != ""){
		alert(strErrorMsg);
		return false;
	}
}

// Function to validate the Add a Dog form
function validateAddADog (objForm) {
	var strErrorMsg = "";

	if (objForm.dog_name_full.value == "") {
		strErrorMsg += "* Please enter the dog's full name.\n";
	}

	if (objForm.dog_sex.value == "") {
		strErrorMsg += "* Please select the dog's gender.\n";
	}

	if (objForm.dog_housetrained.value == "") {
		strErrorMsg += "* Please select whether the dog is housetrained.\n";
	}

	if (objForm.dog_neutered.value == "") {
		strErrorMsg += "* Please select whether the dog is neutered.\n";
	}

	if (objForm.dog_appearance_color.value == "") {
		strErrorMsg += "* Please enter the dog's appearance/color.\n";
	}

	if (objForm.userfile.value == "") {
		strErrorMsg += "* Please include a picture of the dog.\n";
	}

	if (objForm.dog_size_desc.value == "") {
		strErrorMsg += "* Please select the dog's relative weight.\n";
	}

	if (objForm.dog_size_pounds.value == "") {
		strErrorMsg += "* Please select the dog's weight in pounds.\n";
	}

	if (objForm.submit_email.value == "") {
		strErrorMsg += "* Please enter your e-mail address.\n";
	}

	if (objForm.submit_name.value == "") {
		strErrorMsg += "* Please enter your name.\n";
	}

	if (objForm.submit_phone.value == "") {
		strErrorMsg += "* Please enter your phone number.\n";
	}


	// Print the error message if it exists. Otherwise lets the form submit.
	if (strErrorMsg != ""){
		alert(strErrorMsg);
		return false;
	}
}

// Function to validate the bug report form
// call:  onsubmit="return validateBugReport(this);"
function validateBugReport (objForm) {
	var strErrorMsg = "";

	if (objForm.strSenderName.value == "") {
		strErrorMsg += "* Please enter your name\n";
	}

	if (objForm.strSenderEmail.value == "") {
		strErrorMsg += "* Please enter your email.\n";
	}

	if (strErrorMsg != ""){
		alert(strErrorMsg);
		return false;
	}
}

// Set the value of a hidden form field with that selected in a dropdown
function updateHidden (objForm, strThisValue, strTargetName) {
	 objForm.elements[strTargetName].value = strThisValue;
}
