﻿// web.js: javascript public functions

/* to judg a email is valid or not */
function IsEmail(temp){
	return(new RegExp(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/).test(temp));
}

/* 	password is composed of alphabet, number or under line.
	the password length is minimum of 6 */
function IsPassword(temp){
	return(new RegExp(/^\w{6,20}$/).test(temp));
}

/*	confirm code is a string of number
	and the length of number is 4*/
function IsConfirmCode(temp){
	return(new RegExp(/^\d{4,4}$/).test(temp));
}

/* 	required num cannot be left blank */
function IsRequiredNum(temp){
	return(new RegExp(/^\d+$/).test(temp));
}

/* 	elective num can be empty */
function IsElectiveNum(temp){
	return(new RegExp(/^\d*$/).test(temp));
}

/*	phone number is a string of number
	the phone number length is minimum of 6*/
function IsPhoneNum(temp){
	return(new RegExp(/^\d{7,20}$/).test(temp));
}

/*	phone number is elective
	the phone number length is minimum of 6*/
function IsFaxNum(temp){
	return(new RegExp(/^(\d{7,20})?$/).test(temp));
}

/*	link url should be start with http:// or https:// */
function IsLinkUrl(temp){
	return(new RegExp(/^https?:\/\/.+$/).test(temp));
}

/* 	nickname can composed of any characters. 
	the nickname length is minimum of 6. */
function IsNickname(temp){
	return(new RegExp(/^[^\']{3,20}$/).test(temp));
}

/* 	date string should be looked like yyyy-mm-dd hh:mm:ss */
function IsDate(temp){
	return(new RegExp(/^\d{4}\-\d{1,2}\-\d{1,2} \d{1,2}\:\d{1,2}\:\d{1,2}$/).test(temp)); 
}

/* 	all rss indexes are numbers
	each of first three indexes cannot be zero.*/
function IsValidRssIndex(temp){
	return(new RegExp(/^[1-9],[1-9],[1-9],\d{1},\d{1},\d{1}$/).test(temp));
}

/* 	cannot be left blank */
function IsEmpty(obj,strCon){
	var conObj = eval("obj." + strCon.toLowerCase());
	var msg = eval("EMP_" + strCon.toUpperCase());
	if(!conObj.value){alert(msg); conObj.focus(); return true;}
}

/* form data checking */
function DataCheck(obj, temp){
	var strItem = temp.split("|");
	for(var i=0;i<strItem.length;i++){
		switch(strItem[i]){
			/* email checking */
			case "VAL_EMAIL":
			if(!IsEmail(obj.email.value)){
				alert(ERR_EMAIL); obj.email.select(); return false;}
			break;
			/* password checking */
			case "VAL_PASSWORD":
			if(!IsPassword(obj.password.value)){
				alert(ERR_PASSWORD); obj.password.focus(); return false;}
			break;
			/* password equal checking */
			case "VAL_PASSWORD_EQUAL":
			if(obj.password.value != obj.password2.value){
				alert(ERR_PASSWORD_EQUAL); obj.password.select(); return false;}
			break;
			/* confirm code checking */
			case "VAL_CONFIRMCODE":
			if(!IsConfirmCode(obj.confirmcode.value)){
				alert(ERR_CONFIRMCODE);	obj.confirmcode.select(); return false; }
			break;
			/* file size checking */
			case "VAL_FILESIZE":
			if(!IsRequiredNum(obj.filesize.value)){
				alert(ERR_FILESIZE);obj.filesize.select();return false;}
			break;
			/* link checking */
			case "VAL_LINKURL":
			if(!IsLinkUrl(obj.linkurl.value)){
				alert(ERR_LINKURL);obj.linkurl.select();return false;}
			break;
			/* nickname checking */
			case "VAL_NICKNAME":
			if(!IsNickname(obj.nickname.value)){
				alert(ERR_NICKNAME); obj.nickname.focus(); return false;
			}
			break;
			/* phone number checking */
			case "VAL_PHONE":
			if(!IsPhoneNum(obj.phone.value)){
				alert(ERR_PHONE); obj.phone.select(); return false; }
			break;
			/* fax number */
			case "VAL_FAX":
			if(!IsFaxNum(obj.fax.value)){
				alert(ERR_FAX); obj.fax.select(); return false; }
			break;
			/* content checking */
			case "EDT_CONTENT":
			if(!obj.content.value){ alert(EMP_CONTENT); return false; }
			break;
			/* time checking */
			case "VAL_ADDTIME":
			if(!IsDate(obj.addtime.value)){
				alert(ERR_TIME); obj.addtime.select(); return false; }
			break;
			case "VAL_STARTTIME":
			if(!IsDate(obj.starttime.value)){
				alert(ERR_TIME); obj.starttime.select(); return false; }
			break;
			case "VAL_ENDTIME":
			if(!IsDate(obj.endtime.value)){
				alert(ERR_TIME); obj.endtime.select(); return false; }
			break;
			case "VAL_RSSINDEX":
			var strIndex = "";
			strIndex += obj.indextitle.value + ",";
			strIndex += obj.indexlinkurl.value + ",";
			strIndex += obj.indexnote.value + ",";
			strIndex += obj.indextime.value + ",";
			strIndex += obj.indexsource.value + ",";
			strIndex += obj.indexauthor.value;
			if(!IsValidRssIndex(strIndex)){
				alert(ERR_RSSINDEX); return false; }
			break;
			/* title checking */
			default:
			if(IsEmpty(obj, strItem[i])) return false; break;
		}
	}
	return true;
}

/*	to enable text colored to instruct that input box is on active */
function FocusMark(id){document.getElementById(id).className = "focus_mark";}
function FocusRelease(id){document.getElementById(id).className = "focus_release";}

/******************* show messages when cursor on *******************/
function CloseInfo(obj){divInfo.style.visibility = "hidden"; obj.src = "../images/question_inactive.gif";}
function ShowInfo(obj, info){
			var tableHead = "<table border='0' cellpadding='0' class='box_info' cellspacing='0'><tr><td width='200'>";
			var tableTail = "</td></tr></table>";
			divInfo.innerHTML = tableHead + info + tableTail;
			divInfo.style.left = event.x + 20;						//get pos x
			divInfo.style.top = event.y + document.body.scrollTop;	//get pos y
			divInfo.style.visibility = "visible";
			obj.src = "../images/question_active.gif";
}
/******************************** end ********************************/

/* Spread or close content */
function SwitchContent(id){
	var className = document.getElementById(id).className;
	document.getElementById(id).className = (className == "close") ? "spread" : "close";	
}

/* set form's action */
function SetAction(obj,action){
	obj.action = "newsletter?action=" + action;
}

/* delete confirm */
function DeleteConfirm(){
	return confirm("Are you sure?");
}