Initial commit.
This commit is contained in:
139
htdocs/wov/scripts/common.js
Normal file
139
htdocs/wov/scripts/common.js
Normal file
@@ -0,0 +1,139 @@
|
||||
/* Woman's Voice
|
||||
* common.js: The common JavaScript subroutines.
|
||||
*/
|
||||
|
||||
/* Copyright (c) 2000-2018 imacat.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/* Author: imacat <imacat@mail.imacat.idv.tw>
|
||||
* First written: 2000-05-17
|
||||
*/
|
||||
|
||||
// _: Gettext
|
||||
function _(msg) {
|
||||
var i;
|
||||
for (i = 0; i < lc_messages.length; i++) {
|
||||
if (lc_messages[i][0] == msg) {
|
||||
return lc_messages[i][1];
|
||||
}
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
// The messages
|
||||
lc_messages = [];
|
||||
|
||||
// isEmail: Check if an email address is legal
|
||||
function isEmail(a) {
|
||||
var i, c, re;
|
||||
if (typeof(RegExp) == "undefined") return true;
|
||||
re = new RegExp("^[\\w\\-]+(\\.[\\w\\-]+)*\\@([\\w\\-]+\\.)+[\\w\\-]+$");
|
||||
if (typeof(a) != "string") return false;
|
||||
a = a.toLowerCase();
|
||||
if (re.exec(a) == null) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
// isDate: Check if a date is legal
|
||||
function isDate(dateText) {
|
||||
var i, year, month, day, maxDay;
|
||||
if (dateText.length != 10) {
|
||||
return false;
|
||||
}
|
||||
// Check each character
|
||||
for (i = 0; i < dateText.length; i++) {
|
||||
// The dash sign
|
||||
if (i == 4 || i == 7) {
|
||||
if (dateText.charAt(i) != "-")
|
||||
return false;
|
||||
// The digits
|
||||
} else {
|
||||
if (dateText.charCodeAt(i) < 48 || dateText.charCodeAt(i) > 57)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// Check if the date is valid
|
||||
year = dateText.substr(0, 4) * 1;
|
||||
month = dateText.substr(5, 2) * 1;
|
||||
day = dateText.substr(8, 2) * 1;
|
||||
// A reasonable month
|
||||
if (month < 1 || month > 12)
|
||||
return false;
|
||||
// Find the maximum day in this month
|
||||
switch (month) {
|
||||
case 1:
|
||||
case 3:
|
||||
case 5:
|
||||
case 7:
|
||||
case 8:
|
||||
case 10:
|
||||
case 12:
|
||||
maxDay = 31;
|
||||
break;
|
||||
case 4:
|
||||
case 6:
|
||||
case 9:
|
||||
case 11:
|
||||
maxDay = 30;
|
||||
break;
|
||||
case 2:
|
||||
maxDay = 28;
|
||||
if (year % 4 == 0)
|
||||
maxDay = 29;
|
||||
if (year % 100 == 0)
|
||||
maxDay = 28;
|
||||
if (year % 400 == 0)
|
||||
maxDay = 29;
|
||||
break;
|
||||
}
|
||||
// A reasonable day
|
||||
if (day < 1 || day > maxDay)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
// trim: Trim the leading and tailing spaces
|
||||
function trim(a) {
|
||||
var pos, start, len, spaces;
|
||||
start = 0;
|
||||
len = a.length;
|
||||
spaces = " \t\f\n\r";
|
||||
for (start = 0; start < a.length && spaces.indexOf(a.charAt(start)) >= 0; start++, len--);
|
||||
for (pos = a.length - 1; pos >= 0 && spaces.indexOf(a.charAt(pos)) >= 0; pos--, len--);
|
||||
return a.substr(start, len);
|
||||
}
|
||||
function trimText(a) {
|
||||
var pos, start, len, spaces, newlines;
|
||||
start = 0;
|
||||
len = a.length;
|
||||
spaces = " \t\f\n\r";
|
||||
newlines = "\n\r";
|
||||
for (start = 0; start < a.length && spaces.indexOf(a.charAt(start)) >= 0; start++, len--);
|
||||
for ( ; start-1 >= 0 && newlines.indexOf(a.charAt(start-1)) < 0; start--, len++);
|
||||
for (pos = a.length - 1; pos >= 0 && spaces.indexOf(a.charAt(pos)) >= 0; pos--, len--);
|
||||
return a.substr(start, len);
|
||||
}
|
||||
|
||||
// Replace one phace with another in a string
|
||||
function replace(source, oldStr, newStr) {
|
||||
var pos;
|
||||
pos = 0;
|
||||
while (true) {
|
||||
pos = source.indexOf(oldStr, pos);
|
||||
if (pos == -1) break;
|
||||
source = source.substr(0, pos) + newStr + source.substr(pos + oldStr.length)
|
||||
pos += newStr.length;
|
||||
}
|
||||
return source;
|
||||
}
|
||||
Reference in New Issue
Block a user