/*1234567890123456789012345678901234567890123456789012345678901234567890*
 * jscript.inc                                                          *
 *                                                                      *
 * Copyright (c) 2006 Albert Au,                                        *
 * Wenbert Technology Development & Trading Co.                         *
 * Mobile : 852-63538930                                                *
 * Email  : albert@wenbert.com                                          *
 * Web    : http://wenbert.com                                          *
 * Created on : December 26, 2006.                                      *
 * Version    : 1.0                                                     *
 *                                                                      *
 * It provides all the javascript functions.                            *
 *                                                                      *
 ************************************************************************/

<!--
var monthname = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

/*
Get academic year
*/
var d=new Date();
if (d.getMonth()+1 < 9) {
  var acadyr1=d.getFullYear()-1;
  var acadyr2=d.getFullYear();
  var acadyr3=""+acadyr1+"-"+acadyr2;
}
else {
  var acadyr1=d.getFullYear();
  var acadyr2=d.getFullYear()+1;
  var acadyr3=""+acadyr1+"-"+acadyr2;
}

function getAcadyr() {
  var d=new Date();
  
  if (d.getMonth()+1 < 9) {
    var acadyr1=d.getFullYear()-1;
    var acadyr2=d.getFullYear();
    var acadyr3=""+acadyr1+"-"+acadyr2;
  }
  else {
    var acadyr1=d.getFullYear();
    var acadyr2=d.getFullYear()+1;
    var acadyr3=""+acadyr1+"-"+acadyr2;
  }
  
  return acadyr3;
}

/*
Get Character randomly
*/
function getChar() {
  var c;
  var test = Math.floor(Math.random()*3);

  if (test == 0) {
    c=Math.floor(Math.random() * 10);
  }
  else if (test == 1) {
    c=String.fromCharCode(Math.floor(Math.random()*26) + 65);
  }
  else if (test == 2) {
    c=String.fromCharCode(Math.floor(Math.random()*26) + 97);
  }

  if (((c >= '0') && (c <= '9')) || ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'))) {
    return c;
  }

  return getchar();
}

/*
Get Password
*/
function getPasswd(n) {
  for (var i=0, pwd=""; i<n; i++) {
    pwd=pwd+getchar();
  }  
  return pwd;
}

/*
Get English Month
*/
function getEngMonth(n) {
  if (n == 1) {
    return ("January");
  }
  else if (n == 2) {
    return ("February");
  }
  else if (n == 3) {
    return ("March");
  }
  else if (n == 4) {
    return ("April");
  }
  else if (n == 5) {
    return ("May");
  }
  else if (n == 6) {
    return ("June");
  }
  else if (n == 7) {
    return ("July");
  }
  else if (n == 8) {
    return ("August");
  }
  else if (n == 9) {
    return ("September");
  }
  else if (n == 10) {
    return ("October");
  }
  else if (n == 11) {
    return ("November");
  }
  else if (n == 12) {
    return ("December");
  }
  else {
    return ("");
  }
}

/*
Disable right mouse click Script
*/

var message="";

function clickIE() {
  if (document.all) {(message); return false;}
}

function clickNS(e) {
  if (document.layers || (document.getElementById && !document.all)) {
    if (e.which == 2 || e.which == 3) {(message); return false;}
  }
}

if (document.layers) {
  document.captureEvents(Event.MOUSEDOWN);
  document.onmousedown=clickNS;
}
else {
  document.onmouseup=clickNS;
  document.oncontextmenu=clickIE;
}

document.oncontextmenu=new Function("return false");

/*
Disable ESC key in IE
*/
function escIE() {
  return event.keyCode!=27;
}

if (document.all) {
  document.onkeydown=escIE;
}

/*
Open a new window
*/
function showList(filename) {
  winList=eval("window.open(filename, 'winList', 'width=500,height=300')");
  winList.focus();
}

function openNewWin(filename, winname, para) {
  newWin=eval("window.open(filename, winname, para)");
  newWin.focus();
}

/*
Cookies
*/
function createCookie(name, value, days) {
  if (days) {
    var date=new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires="; expires="+date.toGMTString();
  }
  else {
    var expires="";
  }
  document.cookie=name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ=name+"=";
  var ca=document.cookie.split(';');
  for(var i=0; i<ca.length; i++) {
    var c=ca[i];
    while (c.charAt(0) == ' ') {
	  c=c.substring(1, c.length);
    }
    if (c.indexOf(nameEQ) == 0) {
      return c.substring(nameEQ.length, c.length);
    }
  }
  return null;
}

function eraseCookie(name) {
  createCookie(name, "", -1);
}

/*
Clear all checkboxes
*/
function clrAllChkBox(thischkbx) {
  for (var i=0; i<thischkbx.length; i++) {
    thischkbx[i].checked=false;
  }
}

/*
Select all checkboxes
*/
function selAllChkBox(thischkbx) {
  for (var i=0; i<thischkbx.length; i++) {
    thischkbx[i].checked=true;
  }
}

/*
Count the number of checkboxes which have been checked!
*/
function cntAllChkBox(thischkbx) {
  for (var i=0, cnt=0; i<thischkbx.length; i++) {
    if (thischkbx[i].checked == true) {
      cnt=cnt+1;
    }
  }
  // Prevent only one checkbox is present
  if (thischkbx.checked == true) {
    cnt=cnt+1;
  }
  return cnt;
}

/*
Get the values for the checkboxes which have been checked!
*/
function getAllChkBox(thischkbx) {
  for (var i=0, tmpstr=""; i<thischkbx.length; i++) {
    if (thischkbx[i].checked == true) {
      if (tmpstr == "") {
        tmpstr=thischkbx[i].value;
      }
      else {
        tmpstr=tmpstr+","+thischkbx[i].value;
      }
    }
  }
  return tmpstr;
}

function checkChkBox(thisform) {
  if (cntAllChkBox(thisform.sel) <= 0) {
    alert("You must select a record which you want to modify before clicking the submit button!");
    return false;
  }
  return true;
}

/*
Check functions
*/
var USAGE0="The number of characters exists the limit!";

function chkNumOfWords(str, n) {
  if (show_word_count == null) {
    show_word_count=true;
  }
  if (show_char_count == null) {
    show_char_count=false;
  }
  var char_count=str.length;
  var fullStr=str+" ";
  var initial_whitespace_rExp=/^[^A-Za-z0-9]+/gi;
  var left_trimmedStr=fullStr.replace(initial_whitespace_rExp, "");
  var non_alphanumerics_rExp=rExp=/[^A-Za-z0-9]+/gi;
  var cleanedStr=left_trimmedStr.replace(non_alphanumerics_rExp, " ");
  var splitString=cleanedStr.split(" ");
  var word_count=splitString.length-1;

  if (fullStr.length < 2) {
    word_count=0;
  }
  if (word_count == 1) {
    wordOrWords=" word";
  }
  else {
    wordOrWords=" words";
  }
  if (char_count == 1) {
    charOrChars=" character";
  } 
  else {
    charOrChars=" characters";
  }
  if (show_word_count & show_char_count) {
    alert ("Word Count:\n"+"    "+word_count+wordOrWords+"\n"+"    "+char_count+charOrChars);
  }
  else {
    if (show_word_count) {
      alert ("Word Count:  "+word_count+wordOrWords);
    }
    else {
      if (show_char_count) {
        alert ("Character Count:  "+char_count+charOrChars);
      }
    }
  }
  return word_count;
}

function chkNumOfChrs(str, n) {
  if (str.length > n) {
    alert(USAGE0);
    return false;
  }
  return true;
}

function isNumber(str) {
  for (var i=0, test=0; i<str.length; i++) {
    if (((str.substr(i, 1) < "0") || (str.substr(i, 1) > "9"))
       && (str.substr(i, 1) != ".")) {
      return false;
    }
    else if (str.substr(i, 1) == ".") {
      test=test+1;
    }
    if (test > 1) {
      return false;
    }
  }
  return true;
}

function isHexNumber(str) {
  for (var i=0; i<str.length; i++) {
    if (((str.substr(i, 1) < "0") || (str.substr(i, 1) > "9"))
       && ((str.substr(i, 1).toUpperCase < "A") || (str.substr(i, 1).toUpperCase > "F"))) {
      return false;
    }
  }
  return true;
}

function isYear(str) {
  var t=parseInt(str, 10);
  if (t < 1900) {
    return false;
  }
  return true;
}

function isMonth(str) {
  var t=parseInt(str, 10);
  if ((t < 1 ) || (t > 12)) {
    return false;
  }
  return true;
}

function isDay(str) {
  var t=parseInt(str, 10);
  if ((t < 1 ) || (t > 31)) {
    return false;
  }
  return true;
}

function isHour(str) {
  var t=parseInt(str, 10);
  if ((t < 0 ) || (t > 60)) {
    return false;
  }
  return true;
}

function isMinute(str) {
  var t=parseInt(str, 10);
  if ((t < 0 ) || (t > 60)) {
    return false;
  }
  return true;
}

function isSecond(str) {
  var t=parseInt(str, 10);
  if ((t < 0 ) || (t > 60)) {
    return false;
  }
  return true;
}

function isLetter(str) {
  for (var i=0, test=0; i<str.length; i++) {
    if (((str.substr(i, 1) < "a") || (str.substr(i, 1) > "z"))
       && ((str.substr(i, 1) < "A") || (str.substr(i, 1) > "Z"))) {
      return false;
    }
  }
  return true;
}

function isEmail(str) {
  if ((str.length < 5) || (str.substr(str.length-1, 1) == "@") || (str.substr(str.length-1, 1) == ".")) {
    return false;
  }
  for (var i=0, cnt0=0; i<str.length; i++) {
    if ((str.substr(i, 2) == "@.") || (str.substr(i, 2) == ".@") || (str.substr(i, 2) == "@@") || (str.substr(i, 2) == "..")) {
      cnt0=cnt0+1;
    }
  }
  if (cnt0 > 0) {
    return false;
  }
  for (var i=0, cnt1=0; i<str.length; i++) {
    if (str.substr(i, 1) == "@") {
      cnt1=cnt1+1;
    }
  }
  if (cnt1 != 1) {
    return false;
  }
  for (var i=0, cnt2=0; i<str.length; i++) {
    if (str.substr(i, 1) == ".") {
      cnt2=cnt2+1;
    }
  }
  if (cnt2 < 1) {
    return false;
  }
  return true;
}

function isDate(dateStr) {
  var datePat=/^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
  var matchArray=dateStr.match(datePat); // is the format ok?
  if (matchArray == null) {
    alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
    return false;
  }
  month=matchArray[1]; // parse date into variables
  day=matchArray[3];
  year=matchArray[5];
  if (month < 1 || month > 12) { // check month range
    alert("Month must be between 1 and 12.");
    return false;
  }
  if (day < 1 || day > 31) {
    alert("Day must be between 1 and 31.");
    return false;
  }
  if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) {
    alert("Month " + month + " doesn't have 31 days!")
    return false;
  }
  if (month == 2) { // check for february 29th
    var isleap=(year%4 == 0 && (year%100 != 0 || year%400 == 0));
    if (day > 29 || (day == 29 && !isleap)) {
      alert("February " + year + " doesn't have " + day + " days!");
      return false;
    }
  }
  return true; // date is valid
}

/*
String to a value function
*/
function str2Val(dateStr) {
  var p1=dateStr.indexOf('-');
  var p2=dateStr.lastIndexOf('-');
  var p3=dateStr.length;
//var d=new Date(parseInt(dateStr.substring(0, 4)), parseInt(dateStr.substring(p1+1, p2))-1, parseInt(dateStr.substring(p2+1, p3)));
  return parseInt(dateStr.substring(0, 4))*366+(parseInt(dateStr.substring(p1+1, p2))-1)*31+parseInt(dateStr.substring(p2+1, p3));
}
//-->
