// ---------------------------
//
// Check Hostname, switch between big5 and gb version
//
// ---------------------------

function j2big5(url){
        if(location.hostname=="202.181.201.237"){
        location.href='ht'+'tp://'+url;
        return false;
        }
        else
        {
       location.href='http://202.181.201.237:10089/gate/gb/big5.hrichina.org:8152/big5';
        return false;
        }
}


// ----------------------------
//
// Alphanumeric checking related
//
// ----------------------------

function check_integer(num) {
  for (var i=0; i<num.length; i++) {
    if (num.charAt(i) < '0' || num.charAt(i) > '9') {
      return false;
    }
  }
  return true;
}

function check_float(num) {
  re=/^(\d+).(\d+)$/;
  x = num.search(re);
  if ( x == -1 ) {
    if ( !check_integer(num) ) {
      return false;
    }
  }
  return true;
}

function check_string(str) {
  str = str.toUpperCase();
  for (var i=0; i<str.length; i++) {
    if (!(str.charAt(i) >= '0' && str.charAt(i) <= '9') &&
	  !(str.charAt(i) >= 'A' && str.charAt(i) <= "Z") &&
	  !(str.charAt(i) == '_')) {
      return false;
    }
  }
  return true;
}



// ----------------------------
//
// Date checking related
//
// ----------------------------


function check_date(year,month,day) 
{
	var err = 0
/*
	month = form.month.options[form.month.selectedIndex].value
	year = form.year.options[form.year.selectedIndex].value
	day = form.day.options[form.day.selectedIndex].value
*/	
	if (month==2){
		var g=parseInt(year/4)
		if (isNaN(g))
		{
			err=1
		}
		if (day > 29) 
			err=1
		if (day==29 && ((year/4)!= parseInt(year/4))) 
			err=1
	}
	if ((month == 4 ||
	     month == 6 ||
	     month == 9 ||
	     month == 11) && day == 31)
	     err = 1

	if (err==1) 
	{
		return false;
	}
	else 
	{
		return true;
	}
}

function build_days_array(year) {
  var leapyear;
  if (((year % 4) == 0) && ((year % 100) != 0) || ((year % 4) == 0))
    leapyear = 1;
  else
    leapyear = 0;
  
  var days = new Array(12);

  days[0] = 31;
  days[1] = (leapyear == 1) ? 29 : 28; 
  days[2] = 31;
  days[3] = 30;
  days[4] = 31;
  days[5] = 30;
  days[6] = 31;
  days[7] = 31;
  days[8] = 30;
  days[9] = 31;
  days[10] = 30;
  days[11] = 31;
  return days;
}

function check_year(year) {
  var now = new Date();
  var now_year = now.getYear();
  now_year = (now_year >= 2000) ? now_year : 1900 + now_year; 
  var min_year = now_year - 50;
  var max_year = now_year + 50;

  if ( year >= min_year && year <= max_year ) {
    return true;
  } else {
    return false;
  }
}

function check_month(month) {
  if ( month >= 1 && month <= 12 ) {
    return true;
  } else {
    return false;
  }
}

function check_day(year, month, day) {
  var days = build_days_array(year);

  if (day > days[month-1]) 
    return false;
  return true;
}

 
function check_date_format(date) {
  re= /^(\d\d\d\d)\/(\d+)\/(\d+)$/;
  x = date.search(re);

  if ( x == -1 )
    return false; 

  year = RegExp.$1;
  month = RegExp.$2;
  day = RegExp.$3;

  if ( !check_year(year) ) {
    return false;
  }
  if ( !check_month(month) ) {
    return false;
  }
  if ( !check_day(year, month, day) ) {
    return false;
  }
  return true;
}

function add_date(date, num_days) {
  if (check_date_format(date) == false) {
    return false;
  }

  re = /^(\d\d\d\d)\/(\d+)\/(\d+)$/;
  x = date.search(re);
  if ( x == -1 ) {
    return false; 
  }

  var year = parseInt(RegExp.$1);
  var month = parseInt(RegExp.$2);
  var day = parseInt(RegExp.$3);
  var days = build_days_array(year);

  new_day = day + parseInt(num_days);
  new_month = (new_day > days[month-1]) ? month + 1 : month;
  new_year = (new_month > 12) ? year + 1 : year; 

  new_day = (new_day > days[month-1]) ? new_day - days[month-1] : new_day;
  new_month = (new_month > 12) ? new_month - 12 : new_month;
  new_date = new_year + "/" + new_month + "/" + new_day;
  return new_date;
}

function check_time_format(time) {
  re=/^(\d+):(\d+)$/;
  x = time.search(re);
  if ( x == -1 )
    return false;
  hour = RegExp.$1;
  minute = RegExp.$2; 
  if ( hour > 23 || hour < 0 ) 
    return false;
  if ( minute > 59 || minute < 0 )
    return false; 
  return true;
}


// ----------------------------
//
//  Email format checking
//
// ----------------------------

function check_email (emailStr) {
/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	//  alert("Email address seems incorrect (check @ and .'s)")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    //  alert("The username doesn't seem to be valid.")
    return false
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        //  alert("Destination IP address is invalid!")
		return false
	    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	//  alert("The domain name doesn't seem to be valid.")
    return false
}

/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding 
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
   //  alert("The address must end in a three-letter domain, or two letter country.")
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="This address is missing a hostname!"
   //  alert(errStr)
   return false
}

// If we've gotten this far, everything's valid!
return true;
}


// ----------------------------
//
//  HK ID card checking
//
// ----------------------------

function check_hkid(hkid) {
  hkid = hkid.toUpperCase();	

  if (hkid.length != 10) {
    return false;
  }	
  
  if (hkid.charAt(0) < 'A' || hkid.charAt(0) > 'Z') {
    return false;
  }
  
  for (i=1; i<=6; i++) {
    if (hkid.charAt(i) < '0' || hkid.charAt(i) > '9') {
      return false;
    }
  }
  
  if (hkid.charAt(7) != '(' || hkid.charAt(9) != ')') {
    return false;
  }
  
  if (!(hkid.charAt(8) >= '0' && hkid.charAt(8) <= '9') &&
	  !(hkid.charAt(8) >= 'A' && hkid.charAt(8) <= "Z")) {
      return false;
  }
  
  sum = eval((hkid.charCodeAt(0)-64)*8) + eval(hkid.charAt(1)*7 + hkid.charAt(2)*6 + hkid.charAt(3)*5 + hkid.charAt(4)*4 + hkid.charAt(5)*3 + hkid.charAt(6)*2); 
  check_digit = 11 - sum%11;
  
  if (check_digit == 10) {
    check_digit = 'A';
  }

  if (hkid.charAt(8) != check_digit) {
    return false;
  }    
 
  return true;  
}

// A 123456
// 11 - mod(A*8 + 1*7 + 2*6 + 3*5 + 4*4 +5*3 + 6*2, 11)

