﻿var DEBUG = 0;

            function login() {
                var loginForm = document.getElementById("loginForm");
                if (loginForm.username.value == "") {
                    alert("请输入用户名.");
                    return false;
                }
                if (loginForm.password.value == "") {
                    alert("请输入密码.");
                    return false;
                }
                var submitForm = document.getElementById("submitForm");
                submitForm.username.value = loginForm.username.value;
                submitForm.response.value = loginForm.password.value;
                //submitForm.response.value =
                    //hex_md5(loginForm.challenge.value+loginForm.password.value);
                submitForm.submit();
            }
            
function test(str) {
	if(DEBUG != 1) return;
	alert(str);
}

/**
submit action with pre-check
*/
function submitButton(form, action) {
	try {
		test('call submit button: '+action);
		
		var hidden_act = form.act;
		
		// select page
		var pii = form["page_index"];
		// just move to certain page  index of current page style
		if(action.substr(0, 5).toLowerCase() == "page ") {
			var a = action.split(' ');
			var index = parseInt(a[1]);
			pii.selectedIndex = index;
			
			form.submit();
			return;
		}
		
		// always shows first page
		if(pii != null) {
			pii.selectedIndex = 0;
		}
		
		// just move to edit customer page
		if(action == "edit student page") {
			result = checkTick(form, "sid[]");
			
			if(result == -1) return;
			
			if(result == 0) {
				alert("No item is selected. Please select at least one item.");
				return;
			}
			
			if(result == 2) {
				alert("Multiple items are selected. Please select one item only.");
				return;
			}
			
			form.action = "adminedituser.php";
			hidden_act.value = 31;
			form.submit();
			return;
		}
		
		// just move to edit customer page
		if(action == "view student page") {
			result = checkTick(form, "sid[]");
			
			if(result == -1) return;
			
			if(result == 0) {
				alert("No item is selected. Please select at least one item.");
				return;
			}
			
			if(result == 2) {
				alert("Multiple items are selected. Please select one item only.");
				return;
			}
			
			form.action = "adminviewuser.php";
			hidden_act.value = 21;
			form.submit();
			return;
		}
		
		// just move to delete customer page
		if(action.toLowerCase() == "delete student page") {
			result = checkTick(form, "sid[]");
			
			if(result == -1) return;
			
			if(result == 0) {
				alert("No item is selected. Please select at least one item.");
				return;
			}
			
			var isOK = confirm("Do you really want to delete those customer(s)?");
			if(isOK == false) return;
			
			form.action = "adminmain.php";
			hidden_act.value = 41;
			form.submit();
			return;
		}
		
		// add new student record
		else if(action == "add student") {
			form.action = "adminmain.php";
			hidden_act.value = 11;
			form.submit();
			return;
		}
		
		// save student details
		if(action.toLowerCase() == "save student" || action.toLowerCase() == "update student") {
			form.action = "adminedituser.php";
			hidden_act.value = 51;
			form.submit();
			return;
		}
		
		// save new password
		if(action.toLowerCase() == "save new password") {
			form.action = "main.php";
			hidden_act.value = 54;
			form.submit();
			return;
		}
		
		// save new password
		if(action.toLowerCase() == "cancel change password") {
			form.action = "main.php";
			hidden_act.value = -1;
			form.submit();
			return;
		}
		
		if(action.toLowerCase() == 'select chinese_name') {
			form.submit();
			return;
		}
		
		if(action.toLowerCase() == 'select english_name') {
			form.submit();
			return;
		}
		
		if(action.toLowerCase() == 'select visa_type') {
			form.submit();
			return;
		}
		
		if(action.toLowerCase() == 'select accomedation_phone') {
			form.submit();
			return;
		}
		
		if(action.toLowerCase() == 'select mobile') {
			form.submit();
			return;
		}
		
		if(action.toLowerCase() == 'select visa_expire_date') {
			form.submit();
			return;
		}
		
		if(action.toLowerCase() == 'select email') {
			form.submit();
			return;
		}
		
		if(action.toLowerCase() == 'select birth_date') {
			form.submit();
			return;
		}
		
		if(action.toLowerCase() == 'select passport_no') {
			form.submit();
			return;
		}
		
		if(action.toLowerCase() == 'save diary') {
			diary_content = form.diary_content;
			//alert('content: '+diary_content.value);
			hidden_act.value = 33;
			form.submit();
			return;
		}
		
		if(action.toLowerCase() == 'upload file') {
			hidden_act.value = 15;
			form.encoding = "multipart/form-data";
			form.submit();
			return;
		}
		
		if(action.toLowerCase() == 'delete file') {
			hidden_act.value = 45;
			form.submit();
			return;
		}
		
		
	} catch (ex) {
		var msg = "Error Description: " + err.message;
		alert(msg);
	}
}

/**
check box tick check
-1 - no such item exsit
0 - no tick
1 - one item ticked
2 - multiple item ticked
*/
function checkTick(form, itemName) {
	result = -1;
	
	if(form[itemName]==null) return result;
	
	var cb = form[itemName];
	
	// only one item is available, not array
	if(cb.length == null) {
		if(cb.checked == false) result = 0;
		else result =1;
		return result;
	}
	
	// handle array of checkboxes
	var selectedIndex = -1;
	// multiple items seletected check
	for(i=0; i<cb.length; ++i) {
		var cbi = cb[i];
		if(cbi.checked == true) {
			if(selectedIndex != -1) {
				result = 2;
				break;
			}
			selectedIndex = i;
		}
	}
	// no item selected check
	if(selectedIndex == -1) {
		result = 0;
	} else if(result == -1) {
		result = 1;
	}
	
	return result;
}