olympiamanagement/app/assets/javascripts/olympiamanagement.js

186 lines
6.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function checkid(it){
var ALP_STR = "ABCDEFGHJKLMNPQRSTUVXYWZIO";
var NUM_STR = "0123456789";
function CheckPID(it) {
var sMsg = "";
var sPID = it.value;
if (sPID == '') {
sMsg = "請輸入身分證字號";
} else if (sPID.length != 10) {
sMsg = "身分證字號長度應為 10 ";
} else {
sPID = sPID.toUpperCase().trim();
if (!chkPID_CHAR(sPID)) return;
var iChkNum = getPID_SUM(sPID);
if (iChkNum % 10 != 0) {
var iLastNum = sPID.substr(9, 1) * 1;
for (i = 0; i < 10; i++) {
var xRightAlpNum = iChkNum - iLastNum + i;
if ((xRightAlpNum % 10) == 0) {
sMsg = "身分證字號最後一個數應為:" + i;
break;
}
}
}
}
if (sMsg.length != 0) {
//alert(sMsg);
alert((I18n.locale != "en") ? "身分證字號錯誤" : "ID Number error");
it.select();
return false;
}else{
return true;
}
}
// 身分證字號檢查器 - 檢查合法字元
function chkPID_CHAR(sPID) {
var sMsg = "";
//sPID = trim(sPID.toUpperCase());
var iPIDLen = String(sPID).length;
var sChk = ALP_STR + NUM_STR;
for (i = 0; i < iPIDLen; i++) {
if (sChk.indexOf(sPID.substr(i, 1)) < 0) {
sMsg = "這個身分證字號含有不正確的字元!";
break;
}
}
if (sMsg.length == 0) {
if (ALP_STR.indexOf(sPID.substr(0, 1)) < 0) {
sMsg = "身分證字號第 1 碼應為英文字母(A~Z)。";
} else if ((sPID.substr(1, 1) != "1") && (sPID.substr(1, 1) != "2")) {
sMsg = "身分證字號第 2 碼應為數字(1~2)。";
} else {
for (var i = 2; i < iPIDLen; i++) {
if (NUM_STR.indexOf(sPID.substr(i, 1)) < 0) {
sMsg = "第 " + (i + 1) + " 碼應為數字(0~9)。";
break;
}
}
}
}
if (sMsg.length != 0) {
//alert(sMsg);
alert((I18n.locale != "en") ? "身分證字號錯誤" : "ID Number error");
return false;
} else {
return true;
}
}
//身份證字號檢查器 - 累加檢查碼
function getPID_SUM(sPID) {
var iChkNum = 0;
// 第 1 碼
iChkNum = ALP_STR.indexOf(sPID.substr(0, 1)) + 10;
iChkNum = Math.floor(iChkNum / 10) + (iChkNum % 10 * 9);
// 第 2 - 9 碼
for (var i = 1; i < sPID.length - 1; i++) {
iChkNum += sPID.substr(i, 1) * (9 - i);
}
// 第 10 碼
iChkNum += sPID.substr(9, 1) * 1;
return iChkNum;
}
return CheckPID(it);
};
$(document).ready(function(){
$('.class_setting_block input[type="reset"]').off('click').on('click',function(){
$('.col_input').val('');
});
$('.class_setting_block input[type="submit"]').off('click').on('click',function(){
var class_number_range = [];
class_number_range.push(Number($('.class_setting_block .col_input[name="MinClass"]').val()));
class_number_range.push(Number($('.class_setting_block .col_input[name="MaxClass"]').val()));
var enrollment_available = $('.class_setting_block .col_input#Person').val();
class_number_range.sort();
var params_array = (window.location.search.substring(1)=="") ? [] : window.location.search.substring(1).split('&').map(function(el){return el.split('=')});
var params = {};
params_array.forEach(function(el){params[el[0]] = el[1]});
var params_to_send = {'type':'class_setting','class_number_range':class_number_range,'enrollment_available':enrollment_available};
params_to_send = Object.assign(params,params_to_send);
var post_data = $.post('/admin/olympiamanagements/update_school_data',params_to_send);
post_data.done(function(){
window.location.search="";
});
});
$('.fieldset_block input[type="submit"]').click(function(){
$el = $('.fieldset_block li.required');
for(var i = 0; i< $el.length ; i++){
if($el.eq(i).attr('for') == "StudentFile"){
if($el.eq(i).siblings('.col_r').find('img').length != 0){
continue;
}};
if($el.eq(i).siblings('.col_r').find('.required').val() == ""){
$el.eq(i).addClass('errFld');
if($el.eq(i).siblings('.col_r').find('.errMsg').length == 0)
$el.eq(i).siblings('.col_r').append('<div id="SchoolName-E" class="errMsg"> '+((I18n.locale == "en") ? 'Required field': '必填欄位')+'</div>');
}else{
$el.eq(i).removeClass('errFld');
$el.eq(i).siblings('.col_r').find('.errMsg').remove();
};
};
if($('.fieldset_block .required.errFld').length != 0)
return false;
else
if($('#StudentIDNO').length != 0)
try{return checkid(document.getElementById('StudentIDNO'));
}catch(e){console.log(e);return false;};
})
var $el = $('.fieldset_block select');
for(var i = 0 ; i < $el.length ; i++)
if($el.eq(i).attr('value') != "" && $el.eq(i).attr('value') != undefined)
$el.eq(i).find('option[value='+$el.eq(i).attr('value')+']').attr('selected','selected');
try{CKEDITOR.replaceAll()}catch(e){console.log(e)};
if($('#StudentIdentity').val() != '1' && $('#StudentIdentity').val() != '2')
$('#StudentFile').removeClass('required');
$('#StudentIdentity').click(function(){
if(this.value != '1' && this.value != '2'){
$('#StudentFile').removeClass('required');
}else{
$('#StudentFile').addClass('required');
};
$el = $('.fieldset_block li[for="StudentFile"]');
if($el.siblings('.col_r').find('.required').val() == ""){
$el.addClass('errFld');
if($el.siblings('.col_r').find('.errMsg').length == 0)
$el.siblings('.col_r').append('<div id="SchoolName-E" class="errMsg"> '+((I18n.locale == "en") ? 'Required field': '必填欄位')+'</div>');
}else{
$el.removeClass('errFld');
$el.siblings('.col_r').find('.errMsg').remove();
};
});
$('.fieldset_block input').on('input',function(){
if($(this).val() != ''){
$(this).parent().siblings('.col').removeClass('errFld');
$(this).siblings('.errMsg').remove();
};
});
$('.fieldset_block select').on('input',function(){
if($(this).val() != ''){
$(this).parent().siblings('.col').removeClass('errFld');
$(this).siblings('.errMsg').remove();
};
});
$('#StudentFile').on('input',function(){
if($(this).val() != ''){
$('[for="StudentFile"]').removeClass('errFld');
$('[for="StudentFile"]').siblings('.col_r').find('.errMsg').remove();
};
});
$('[data-dismiss="fileupload"]').click(function(){
if($('#StudentFile').hasClass('required')){
$el = $('.fieldset_block li[for="StudentFile"]');
$el.addClass('errFld');
if($el.siblings('.col_r').find('.errMsg').length == 0)
$el.siblings('.col_r').append('<div id="SchoolName-E" class="errMsg"> '+((I18n.locale == "en") ? 'Required field': '必填欄位')+'</div>');
};
});
});