﻿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.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") {
			if(!is_valid_student(form)) return;
			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 = "adminmain.php";
			hidden_act.value = 54;
			form.submit();
			return;
		}
		
		// save new password
		if(action.toLowerCase() == "cancel change password") {
			form.action = "adminmain.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() == '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;
}

/**
is valid new student details
*/
function is_valid_student(form) {
	if(form == null) return 0==1;
	
	if(!is_valid_string(form.user_name.value)) {
		form.user_name.focus();
		alert("请输入用户名");
		return false;
	}
	
	if(form.password1.value != form.password2.value) {
		form.password1.focus();
		alert("密码不匹配");
		return false;
	}
		
	if(!is_valid_string(form.email.value)) {
		form.email.focus();
		alert("请输入email");
		return false;
	}
		
	if(!is_valid_date(form.birth_date.value)) {
		form.birth_date.focus();
		alert("不是有效的生日日期! 请输入有效的生日日期");
		return false;
	}
	
	if(!is_valid_string(form.visa_type.value)) {
		form.visa_type.focus();
		alert("请输入签证类型");
		return false;
	}
		
	if(!is_valid_string(form.passport_no.value)) {
		form.passport_no.focus();
		alert("请输入护照号码");
		return false;
	}
		
	if(!is_valid_date(form.passport_expire_date.value)) {
		form.passport_expire_date.focus();
		alert("不是有效的护照过期时间! 请重新输入护照过期时间");
		return false;
	}
	
	return true;
}

/**
is empty
*/
function is_valid_string(str) {
	if(str == null) return false;
	if(str.length < 1) return false;
	
	return true;
}


/**
check is valide date
**/
function is_valid_date(str_) {
	//alert('test str is:' + str_);
	var str = str_.replace(/[\-|\.|_]/g, "/");  
	var result = false;
	
	if(str == null || str.length < 8 || str.length > 10) {
		return result;
	}
	
	var parts = new Array();
	
	if(str.indexOf('-') != -1) {
		parts = str.split('-');
	} else if(str.indexOf('/') != -1) {
		parts = str.split('/');
	}
	
	if(parts.length == 3) {
		try {
			var year = 0;
			var month = 0;
			var day = 0;
			year = parts[0].valueOf();
			month = parts[1].valueOf();
			day = parts[2].valueOf();
			
			//alert('year-'+parts[0]+';month-'+parts[1]+';day-'+parts[2]);
			//alert('year-'+year+';month-'+month+';day-'+day);
			if(year>1900 && year<2100 && day >= 1) {
				if( (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && day <= 31 )
					result = true;
				else if( (month == 4 || month == 6 || month == 9 || month == 11) && day <= 30 ) 
					result = true;
				else if(month == 2) {
					if( (year%400 == 0 || (year%100 != 0 && year%4==0)) && day<=29)
						result = true;
					else if(day <= 28)
						result = true;
				}
			}
		} catch(ex) {
		}
	}
	
	return result;
}
