﻿
var vn_date_today = new Date();
var vn_year = vn_date_today.getYear();
var inputYear, inputMonth, inputDay;

function getLastDay( year, month )
{
	var date = new Date( year, month, 0 );

	return date.getDate();
}

if (isLogin)
{
	location.href="/";
}


function goto_byselect(sel)
{
	var index = sel.selectedIndex;
	if (sel.options[index].value != null) {
		window.open(sel.options[index].value,'_parent');
	}
}

function emailCheck (emailStr) {
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null) {
		alert("Putting the correct e-mail address, please. ex: mail@tt4you.com");
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			alert("Invalid user name.");
			return false;
		}
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			alert("Invalid domain name.");
			return false;
		}
	}
	if (user.match(userPat)==null) {
		alert("Invalid user name.");
		return false;
	}
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert("E-mail IP may not be accepted.");
				return false;
			}
		}
		return true;
	}

	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			alert("Domain names are invalid email.");
			return false;
		}
	}
	if (checkTLD && domArr[domArr.length-1].length!=2 && 
	domArr[domArr.length-1].search(knownDomsPat)==-1) {
		alert("Domain names are invalid email.");
		return false;
	}
	if (len<2) {
		alert("Domain names are invalid email.");
		return false;
	}
	return true;
}

function join() {
	var form = document.form2;

	if (!idcheck())
	{
		form.uid.focus();
		return false;
	}

	if (form.pwd.value=='') {
		alert('Please enter Password.');
		form.pwd.focus();
		return false;
	}

	if (form.pwd.value.length < 4 ) {
		alert('The four-character password, enter at least.');
		form.pwd.focus();
		return  false;
	}

	if (form.pwd.value.length > 12 ) {
		alert('Please enter your password 12 characters or less');
		form.pwd.focus();
		return  false;
	}

	var str = form.pwd.value;

	for(var i=0; i<str.length; i++) {
		var chr = str.substr(i,1);
		if(!((chr >= '0' && chr <= '9') || (chr >= 'a' && chr <= 'z') )) {
			alert("Passwords are English lowercase or English, numeric combination.");
			form.pwd.focus();
			return false;
		}
	}

	if (form.pwd1.value=='') {
		alert('Please enter a password verification.');
		form.pwd1.focus();
		return false;
	}


	if (form.pwd.value != form.pwd1.value) {
		alert('Password and Confirm password is not the same. Re-enter details.');
		form.pwd1.focus();
		return false;
	}

	if (form.unm.value=='') {
		alert('Please enter your name.');
		form.unm.focus();
		return false;
	}

	if (!emailCheck (form.email.value)){
		form.email.focus();
		return false;
	}
/*
	if (form.emailanum.value=='') {
		alert('E-mail received authorization number to enter.');
		form.emailanum.focus();
		return false;
	}
*/
	if (form.CaptchaStr.value=='') {
		alert('Please enter the number shown');
		form.CaptchaStr.focus();
		return false;
	}

	return true;
}


function hanCheck(val) 
{ 
	var a = 0 ;
	for(i=0;i< val.length;i++) 
	{ 
		a = val.charCodeAt(i); 
		if (a > 128) return true; 
		 
	} 
	return false;
} 

function idcheck() {
	var form = document.form2;
	var mikExp = /[$\\@\\\#%\^\&\"\'\*\(\)\[\]\+\_\{\}\`\~\=\|/><?`-]/;
	
	if (form.uid.value=='') {
		alert('Please enter ID.');
		return false;
	}
	if (form.uid.value.length < 4 ) {
		alert('The four-character ID, enter at least.');
		return  false;
	}
	
	if (form.uid.value.length > 12 ) {
		alert('Please enter your ID is 12 characters or less.');
		return false ;
	}
	
	var id = form.uid.value;

	if (hanCheck(id)) 
	{
		alert("Enter English only.");
		return false;	
	}	
	
	if(form.uid.value.search(mikExp) != -1) {
		alert("You should not enter special characters for ID");
		return false;	
	}

	var str = form.uid.value;

	for(var i=0; i<str.length; i++) {
		var chr = str.substr(i,1);
		if(!((chr >= '0' && chr <= '9') || (chr >= 'a' && chr <= 'z') )) {
			alert("ID are English lowercase or English, numeric combination.");
			return false;
		}
	}
 
	var str_chk = str.substring(0,1);
	if(str_chk) {
		if(str_chk >= 0 || str_chk <= 9) {
			alert("ID must be the first letter in English. Please re-enter");
			form.uid.value="";
			return false;
		}
	}
	return true;
}

function GetInfo(val1, val2)
{
	if (val2=="email")
	{
		if (!emailCheck (val1)){
			document.form2.email.focus();
			return;
		}
	}
	else if (val2=="uid")
	{
		if (!idcheck()){
			document.form2.uid.focus();
			return;
		}
	}
	else if (val2=="recommender")
	{
		if (document.form2.recommender.value==""){
			alert("Please enter recommended ID.");
			document.form2.recommender.focus();
			return;
		}
	}

	chkkind = val2;

	var pageUrl = "/member/checkdup.asp";
	var sendmsg = "chkword="+escape(val1)+"&chktyp="+escape(val2);
	new Ajax.Request(pageUrl, 
		{ method: 'post', parameters : sendmsg, onComplete: chkExisting }
	);
}

function chkExisting(oriRequest)
{
	var vals = oriRequest.responseText.strip();
	if (vals.length == 0) return;

	if (chkkind=="email")
	{
		if (vals=="T")
			alert("Your authentication code entered by mail has been sent.\n Enter authentication code");
//			alert("Available email address.");
		else if (vals=="F")
			alert("Email addresses are already being used.\n Enter another email address");
		else
			alert("Unspecified error!");
	}
	else if (chkkind=="uid")
	{
		if (vals=="T")
		{ 
			alert("Available ID");
			bchkID = true;
		}
		else if (vals=="F")
		{
			alert("ID already being used. Please enter another ID.");
			bchkID = false;
		}
		else
		{
			alert("Unspecified error!");
			bchkID = false;
		}
	}
	else if (chkkind=="recommender")
	{
		if (vals=="T")
		{
			alert("This recommendation Nick is available.");
			bchkRecID = true;
		}
		else if (vals=="F")
		{
			alert("ID is unregistered.\n Please enter another recommended ID");
			bchkRecID = false;
		}
		else
		{
			alert("Unspecified error!");
			bchkRecID = false;
		}
	}
}

