/*	Nyl Brian Ong - UPLB Administrative Services
	File: forms.js
	Description: handles form functions.
*/

function changeView() {
	document.forms['change_view'].v.value = document.getElementById("viewby").selectedIndex;
	document.forms['change_view'].submit();
}

function checkAll() {
	if (document.getElementById('check_index').innerHTML == "Check All") {
		document.getElementById('check_index').innerHTML = "Uncheck All";
		checked = true;
	} else {
		document.getElementById('check_index').innerHTML = "Check All";
		checked = false;
	}
	
	for (a = 1; a < (document.table_form.elements.length); a++) {
		if(document.table_form.elements[a].type == 'checkbox')
			document.table_form.elements[a].checked = checked;
	}
}

function checkThis(value) {
	if (value == false) {
		document.getElementById('check_index').innerHTML = "Check All";
		document.getElementById('index_ck').checked = false;
	}
}

function formDelete(optype,getval) {
	checked = false;
	for (a = 1; a < (document.table_form.elements.length); a++) {
		if(document.table_form.elements[a].type == 'checkbox')
			if (document.table_form.elements[a].checked == true) {
				checked = true;
				break;
			}
	}
	if (checked) {
		switch (getval) {
			case '0': msg = confirm("Are you sure you want to delete?"); act = "del_"; break;
			case '1': msg = confirm("Deleting archived data would forever be gone, proceed with delete?"); act = "per_del_"; break;
		}
		
		if (msg) {
			document.table_form.action.value = act + optype;
			document.table_form.submit();
		}
	} else
		alert("Please select at least one data to delete.");
}

function formRestore(optype) {
	checked = false;
	for (a = 1; a < (document.table_form.elements.length); a++) {
		if(document.table_form.elements[a].type == 'checkbox')
			if (document.table_form.elements[a].checked == true) {
				checked = true;
				break;
			}
	}
	if (checked) {
		if (confirm("Are you sure you want to restore archived file")) {
			document.table_form.action.value = "restore_" + optype;
			document.table_form.submit();
		}
	} else
		alert("Please select at least one data to restore.");
}

function formReset(optype) {
	checked = false;
	for (a = 1; a < (document.table_form.elements.length); a++) {
		if(document.table_form.elements[a].type == 'checkbox')
			if (document.table_form.elements[a].checked == true) {
				checked = true;
				break;
			}
	}
	if (checked) {
		if (confirm("Are you sure you want to reset password?")) {
			document.table_form.action.value = "reset_" + optype;
			document.table_form.submit();
		}
	} else
		alert("Please select at least one user to reset.");
}

function checkUser() {
	if (document.table_form.username.value == "" && document.table_form.password.disabled == false) {
		alert("Please input a username.");
		document.table_form.username.focus();
	} else if (document.table_form.firstname.value == "") {
		alert("Please input a firstname.");
		document.table_form.firstname.focus();
	} else if (document.table_form.lastname.value == "") {
		alert("Please input a lastname.");
		document.table_form.lastname.focus();
	} else if (document.table_form.password.value == "" && document.table_form.password.disabled == false) {
		alert("Please input a password.");
		document.table_form.password.focus();
	} else if (document.table_form.re_pass.value == "" && document.table_form.re_pass.disabled == false) {
		alert("Please re-type your password.");
		document.table_form.re_pass.focus();
	} else if (document.table_form.password.value != document.table_form.re_pass.value && document.table_form.password.disabled == false) {
		alert("Login passwords don't match. Please re-type your password.");
		document.table_form.password.value = "";
		document.table_form.re_pass.value = "";
		document.table_form.password.focus();
	} else if (document.table_form.def_password.value == "" && document.table_form.def_password.disabled == false) {
		alert("Please input a default password.");
		document.table_form.def_password.focus();
	} else if (document.table_form.re_def.value == "" && document.table_form.re_def.disabled == false) {
		alert("Please re-type your default password.");
		document.table_form.re_def.focus();
	} else if (document.table_form.def_password.value != document.table_form.re_def.value && document.table_form.password.disabled == false) {
		alert("Default passwords don't match. Please re-type your default password.");
		document.table_form.def_password.value = "";
		document.table_form.re_def.value = "";
		document.table_form.def_password.focus();
	} else {
		if (confirm("Are you sure you want to submit this form?"))
			document.table_form.submit();	
	} 
}

function checkAccess() {
	if (document.table_form.access_desc.value == "") {
		alert("Please input an access description.");
		document.table_form.access_desc.focus();
	} else {
		if (confirm("Are you sure you want to submit this form?"))
			document.table_form.submit();	
	}
}

function checkUnit() {
	if (document.table_form.unit_name.value == "") {
		alert("Please input a unit name.");
		document.table_form.unit_name.focus();
	} else {
		if (confirm("Are you sure you want to submit this form?"))
			document.table_form.submit();	
	}
}

function checkServices(value) {
	checked = false;
	valid = true;
	a = 5 + value;
	for (a;a < ((document.table_form.elements.length - 8 - value)/2)+ 5 + value; a++) {
		if(document.table_form.elements[a].type == 'checkbox')
			if (document.table_form.elements[a].checked == true) {
				checked = true;
				break;
			}
	}
	
	if (checked) {
		a = 5 + value;
		for (a; a < document.table_form.elements.length-8 - value; a++) {
			if(document.table_form.elements[a].type == 'checkbox') {
				i = Math.ceil(((document.table_form.elements.length - 8 - value)/2) + a);
				if (document.table_form.elements[a].checked == true && document.table_form.elements[i].checked == true) {
					valid = false;
					break;
				}
			}
		}
		
		if (valid) {
			if (document.table_form.service_name.value == "") {
				alert("Please input a service name.");
				document.table_form.service_name.focus();
			} else if (document.table_form.service_desc.value == "") {
				alert("Please input a service description.");
				document.table_form.service_desc.focus();
			} else if (document.table_form.admin_url.value == "") {
				alert("Please input an admin url.");
				document.table_form.admin_url.focus();
			} else {
				if (confirm("Are you sure you want to submit this form?")) {
					document.table_form.submit();
				}
			}
		} else {
			alert("An assigned admin cannot be blocked at the same time.");
		}
	} else
		alert("Please assign at least one unit to be admin for this service.");
}

function checkLinks() {
	if (document.table_form.link_name.value == "") {
		alert("Please input a link name.");
		document.table_form.link_name.focus();
	} else if (document.table_form.link_desc.value == "") {
		alert("Please input a link description.");
		document.table_form.link_desc.focus();
	} else if (document.table_form.link_url.value == "") {
		alert("Please input a url link.");
		document.table_form.link_url.focus();
	} else {
		if (confirm("Are you sure you want to submit this form?"))
			document.table_form.submit();	
	}
}

function checkBanner() {
	var str = (document.table_form.pic_path.value).split(".");
	var temp = str[str.length-1].toLowerCase();
	var check = 0;
	
	switch (temp) {
		case "jpg":
		case "jpeg":
		case "gif":
		case "bmp":			
		case "png": check = 1; break;
	}

	if (document.table_form.pic_desc.value == "") {
		alert("Please input a banner name.");
		document.table_form.pic_desc.focus();
	} else if (document.table_form.pic_path.value == "" && document.table_form.pic_path.disabled == false) {
		alert("Please input a picture.");
		document.table_form.pic_path.focus();
	} else if (check == 0 && document.table_form.pic_path.disabled == false) {
		alert("The file you selected is not an image file.");
		document.table_form.pic_path.focus();
	} else {
		if (confirm("Are you sure you want to submit this form?"))
			document.table_form.submit();	
	}
}

function checkDownload() {
	if (document.table_form.file_title.value == "") {
		alert("Please input a file title.");
		document.table_form.file_title.focus();
	} else if (document.table_form.file_path.value == "" && document.table_form.file_path.disabled == false) {
		alert("Please input a file to upload.");
		document.table_form.file_path.focus();
	} else {
		if (confirm("Are you sure you want to submit this form?"))
			document.table_form.submit();	
	}
}

function checkNews() {
	var str = (document.table_form.news_pic.value).split(".");
	var temp = str[str.length-1].toLowerCase();
	var check = 0;
	
	switch (temp) {
		case "":
		case "jpg":
		case "jpeg":
		case "gif":
		case "bmp":			
		case "png": check = 1; break;
	}
	
	if (document.table_form.news_title.value == "") {
		alert("Please input a news title.");
		document.table_form.news_title.focus();
	} else if (check == 0 && document.table_form.news_pic.disabled == false) {
		alert("The file you selected is not an image file.");
		document.table_form.news_pic.focus();
	} else {
		if (confirm("Are you sure you want to submit this form?"))
			document.table_form.submit();	
	}
}

function enablecpic(value) {
	if (value) {
		document.table_form.pic_path.disabled = false;
	} else {
		document.table_form.pic_path.disabled = true;
	}
}

function enablecnews(value) {
	if (value) {
		document.table_form.news_pic.disabled = false;
	} else {
		document.table_form.news_pic.disabled = true;
	}
}

function enablecfile(value) {
	if (value) {
		document.table_form.file_path.disabled = false;
	} else {
		document.table_form.file_path.disabled = true;
	}
}

function enablecpass(value) {
	if (value) {
		document.table_form.password.disabled = false;
		document.table_form.re_pass.disabled = false;		
	} else {
		document.table_form.password.disabled = true;
		document.table_form.re_pass.disabled = true;
	}
}

function enablecdef(value) {
	if (value) {
		document.table_form.def_password.disabled = false;
		document.table_form.re_def.disabled = false;		
	} else {
		document.table_form.def_password.disabled = true;
		document.table_form.re_def.disabled = true;
	}
}

function checkSource() {
	if (document.table_form.source_name.value == "") {
		alert("Please input a source name.");
		document.table_form.source_name.focus();
	} else {
		if (confirm("Are you sure you want to submit this form?"))
			document.table_form.submit();	
	}
}

function checkUpload() {
	if (document.table_form.up_file.value == "") {
		alert("Please upload a file.");
		document.table_form.up_file.focus();
	} else {
		if (confirm("Are you sure you want to submit this form?"))
			document.table_form.submit();	
	}
}

function checkCategory() {
	if (document.table_form.cat_name.value == "") {
		alert("Please input a category name.");
		document.table_form.cat_name.focus();
	} else {
		if (confirm("Are you sure you want to submit this form?"))
			document.table_form.submit();	
	}
}

function setActive(value) {
	document.table_form.action.value = "set_active";
	document.table_form.i.value = value;
	document.table_form.submit();
}

function checkProfile() {
	if (document.table_form.firstname.value == "") {
		alert("Please input a first name.");
		document.table_form.firstname.focus();
	} else if (document.table_form.lastname.value == "") {
		alert("Please input a last name.");
		document.table_form.lastname.focus();
	} else {
		if (confirm("Are you sure you want to update your profile?"))
			document.table_form.submit();	
	}
}

function checkQuestion() {
	if (document.table_form.answer.value == "") {
		alert("Please input an answer.");
		document.table_form.answer.focus();
	} else {
		if (confirm("Are you sure you want to update your security question?"))
			document.table_form.submit();	
	}
}

function checkPassword() {
	if (document.table_form.current.value == "") {
		alert("Please input your current password.");
		document.table_form.current.focus();
	} else if (document.table_form.password.value == "") {
		alert("Please input a new password.");
		document.table_form.password.focus();
	} else if (document.table_form.re_password.value == "") {
		alert("Please re-type your new password.");
		document.table_form.re_password.focus();
	} else if (document.table_form.re_password.value != document.table_form.password.value) {
		alert("Your new passwords doesn't match. Please re-type them again");
		document.table_form.re_password.value = "";
		document.table_form.password.value = "";
		document.table_form.password.focus();
	} else {
		if (confirm("Are you sure you want to update your login password?"))
			document.table_form.submit();	
	}
}

function checkDefPassword() {
	if (document.table_form.current.value == "") {
		alert("Please input your current default password.");
		document.table_form.current.focus();
	} else if (document.table_form.password.value == "") {
		alert("Please input a new default password.");
		document.table_form.password.focus();
	} else if (document.table_form.re_password.value == "") {
		alert("Please re-type your new default password.");
		document.table_form.re_password.focus();
	} else if (document.table_form.re_password.value != document.table_form.password.value) {
		alert("Your new passwords doesn't match. Please re-type them again");
		document.table_form.re_password.value = "";
		document.table_form.password.value = "";
		document.table_form.password.focus();
	} else {
		if (confirm("Are you sure you want to update your default password?"))
			document.table_form.submit();	
	}
}

function checkFeedback() {
	if (document.table_form.com_sender.value == "") {
		document.table_form.com_sender.value = "Unknown";
	} 
	if (document.table_form.com_email.value == "") {
		document.table_form.com_email.value = "None";
	} 

	if (document.table_form.com_subject.value == "") {
		alert("Please type a subject.");
		document.table_form.com_subject.focus();
	} else if (document.table_form.com_content.value == "") {
		alert("Please type a comment.");
		document.table_form.com_content.focus();
	} else {
		if (confirm("Are you sure you want to submit comment?"))
			document.table_form.submit();	
	}
}

function delCom() {
	if (confirm("Are you sure you want to delete this comment?"))
		document.table_form.submit();	
}

function postAnnounce() {
	if (confirm("Are you sure you want to post this announcement?"))
		document.table_form.submit();	
}

function checkFinished() {
	if (confirm("Are you finished editing your account?")) {
			document.table_form.action.value = "account_finished";
			document.table_form.submit();
	}
}

function clearAnnounce() {
	document.table_form.announce_content.value = "";
}

function stype() {
	if (document.getElementById("typeid").selectedIndex == 3) {
		document.table_form.app_url.disabled = false;
		document.table_form.file_path.disabled = true;
		document.table_form.cfile.checked = false;
		document.table_form.cfile.disabled = true;
	} else {
		document.table_form.app_url.disabled = true;
		document.table_form.file_path.disabled = false;
		document.table_form.cfile.disabled = false;
	}
}


function enablecfiles(value) {
	if (value) {
		document.table_form.file_path.disabled = false;
		document.table_form.up_type.disabled = false;
	} else {
		document.table_form.file_path.disabled = true;
		document.table_form.up_type.disabled = true;
	}
}	

function checkPay(val) {
	var str = "i"+val;
	if (document.getElementById(str).value == "") {
		alert("Please input file to be uploaded.");
		document.getElementById(str).focus();
	} else {
		document.table_form.d.value = val;
		document.table_form.submit();
	}
}

function uploadMore() {
	var row = document.createElement("tr");
	var cell1 = document.createElement("td");
	var cell2 = document.createElement("td");
	var upfile = document.createElement("input");
	var caption = document.createElement("input");
	
	upfile.type = "file";
	upfile.size = "23";
	upfile.name = "upload_path[]";
	caption.type = "text";
	caption.name = "caption[]";
	caption.className = "txtbar";
	
	cell1.appendChild(upfile);
	cell2.appendChild(caption);
	row.appendChild(cell1);
	row.appendChild(cell2);
	document.getElementById('table_data').appendChild(row);
}

function checkImage(value) {
	temp = value.split('.');
	switch ((temp[temp.length - 1]).toLowerCase()) {
		case "txt":
		case "odt":
		case "gif":
		case "jpeg":
		case "jpg":
		case "bmp":
		case "png": return true; break;
		default: return false;		
	}
}

function checkPhotos() {
	found = false;
	for (i = 0; i < document.table_form.elements.length; i++) {
		switch (document.table_form.elements[i].name) {
			case "title":
				if (document.table_form.elements[i].value == "") {
					alert("Please type a photo news title.");
					document.table_form.elements[i].focus();
					return false;
				}
			break;
			case "upload_path[]":
				if (document.table_form.elements[i].value != "" && !checkImage(document.table_form.elements[i].value)) {
					alert("Please upload an image format.");
					document.table_form.elements[i].select();
					return false;
				}
				if (checkImage(document.table_form.elements[i].value))
					found = true;
			break;
			case "caption[]":
				if (document.table_form.elements[i].value != "") {
					if (document.table_form.elements[i-1].name == "upload_path[]" ) {
						if (document.table_form.elements[i-1].value == "" ) {
							alert("Please upload an image for this caption");
							document.table_form.elements[i].select();
							return false;
						} else if (!checkImage(document.table_form.elements[i-1].value)) {
							alert("Please upload an image format.");
							document.table_form.elements[i-1].focus();
							return false;
						}
						
					}
				}
			break;
		}
	}
	if (!found) {
		alert("Please upload at least one photo.");
		return false;
	} else {
		if (confirm("Are you sure you want to submit this form?"))
			document.table_form.submit();
	}
	
}