// JavaScript Document
function createEditor(id,value,height)
{
	if (document.all){
		var idGenerator = new IDGenerator(id);
		if (isNaN(height)) height=200;
		var editor = new Editor(idGenerator,value,height);
		editor.Instantiate();
	}else{
		document.write("Sorry! your browser does not support this action. Please use Internet Explorer.")
	}
	return true;
}
function popWin(url,width,height,windowName,scrollbar,content){
	var x=(screen.width)?(screen.width-width)/2:100;
	var y=(screen.height)?(screen.height-height)/2:100;

	var features="width="+width+"px,height="+height+"px, top="+y+"px, left="+x+"px";
		features+=(scrollbar)?",scrollbars=yes":"";
	if (!windowName){
		windowName="untitled";
	}
	var popWindow=window.open(url, windowName, features);

	if (popWin.arguments.length==6&&content!=""){
		popWindow.document.write(content);
	}
	if (document.layers){
		window.moveTo(x, y);
	}
	popWindow.focus();
}
function checkForm(frm, arr_ctrl) {

	var count= arr_ctrl.length ;
	var i=0  ;
	for(i=0; i< count; i++) {
		func= trim( arr_ctrl[i][0] ) ;
		if( eval( func ) ) { //== 
			alert( arr_ctrl[i][1] ) ;
			if( arr_ctrl[i][2] != "" )
				eval( "frm."  + arr_ctrl[i][2] ).focus() ;
			return false ; //== Error
		}
	}
	return true ; //== OK
}

function is_empty(str)
{
	results = str.replace(/\<[^<>]*\>/g, "").replace("&nbsp;",""); 
	if(results=="") return true;
	return false;
}

function isTel(s){
	if (s.search(/^(\(\d{2,}\)\s)?\d{4,}$/)!=-1)	return true ;
	return false ;
}

function isEmail(s){
	if (s.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]{2,4}$/) != -1)
		return true ;
	return false ;
}

function isUrl(s)
{
	//alert(s.search(/^(http\:\/\/)?www\.[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)+[a-z]$/));
	//if (s.search(/^(http\:\/\/)?www\.[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)+[a-z]$/i) != -1) return true;
	if (s.search(/\.((com)|(vn)|(au)|(net)|(biz)|(ws)|(sg))/) != -1) return true
	return false;
}

function isString(s){ 
	if (s.search(/^[A-Za-z\s]+$/) != -1)
		return true ;
	return false ;
}

function checkImage(img)
{
	if (img.search(/\.((gif)|(GIF)|(jpg)|(JPG)|(bmp)|(BMP)|(pnp)|(PNP))\b$/) != -1)
		return true ;
	return false ;
}

function trim(str) {
	return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function isNegative(str) {
	if( isNaN(str) || str <= 0 )
		return true ;
	return false ;
}

function isBlank(str) {
	if( str == "" ) 
		return true ;
	return false ;
}
function open_calendar(frm_name,frm_field,func)
{
	if(!func)
		func=""
	var url="style_js/calendar/calendar.htm#"+frm_name+"="+frm_field +"="+func
	popWin(url,300,250,"calendar","no","")
		//popWin(url,width,height,windowName,scrollbar,content)

}
function isDate(date,type){
	if (!(type==0||type==1||type==2)) {alert("Please specify date type in check form function!"); return false;}
	var day, month, year;
	var dateText=new Array("null","january","february","march","april","may","june","july","august","september","october","november","december");
	var thisdate=trim(date);
	
	//validate basic format
	var dateType=new Array();
	dateType[0]=/^\d{1,2}(\-|\/|\.|\s)\d{1,2}(\-|\/|\.|\s)\d+$/;	//date format dd mm yyyy
	dateType[1]=/^\d{1,2}(\-|\/|\.|\s)\d{1,2}(\-|\/|\.|\s)\d+$/;	//date format mm dd yyyy
	dateType[2]=/^\d{1,2}(\-|\/|\.|\s)\w{3,9}(\-|\/|\.|\s)\d+$/;	//date format dd mmmm yyyy
	if (thisdate.search(dateType[type])==-1) return false;
	
	var seperator=(thisdate.indexOf("-")!=-1)?"-":(thisdate.indexOf("/")!=-1)?"/":(thisdate.indexOf(".")!=-1)?".":(thisdate.indexOf(" ")!=-1)?" ":"";
	if (seperator=="") return false;	//no seperator

	var dateParam=thisdate.split(seperator);
	if (dateParam.length!=3) return false;	//use difference seperator
	
	//day month year from date string
	day=dateParam[0]; 
	year=dateParam[2];
	switch (type){
		case 0:
			month=dateParam[1];
		break;
		case 1:
			day=dateParam[1];
			month=dateParam[0];
		break;
		case 2:
			month=inArray(dateParam[1].toLowerCase(),dateText);
		break;
	}
	if (!month) return false;
	if (!senseDate(day,month,year)) return false;	
	return true;
}


function inArray(itemToCheck,targetArray){
	var i=-1; var result=false;
	if (!isArray(targetArray)) return false;
	while ((i<targetArray.length-1)&&(!result)){
		i++;
		result=(targetArray[i].indexOf(itemToCheck)!=-1)?true:false;
	}
	if (result) return i;
	return false;
}

function isArray(obj){
	if (obj.constructor.toString().indexOf("Array")==-1)
		return false;
	return true;
}

function senseDate(day,month,year){ 
	if ((day<1)||(day>31)) return false;
	if ((month<1)||(month>12)) return false;
	if ((year<1000)||(year>3000)) return false;
	if ((month==2)&&(day>29)) return false;
	if (((month==4)||(month==6)||(month==9)||(month==11))&&(day>30)) return false;
	if ((month==2)&&(day==29)){
		var div4=year%4;
        var div100=year%100;
        var div400=year%400;
		if (div4!=0) return false;
		if ((div100==0)&&(div400!=0)) return false;
	}
	return true;
}
function resizePopWin(nest_object){
	var body_margin = 32;
	var scroll_bar = 20;
	var title_bar = 25;
	var form_space = 12;
	var scale = 1.3;	//	scale = height/width;
	var additional_range = 100;
	
	var window_width = nest_object.clientWidth + body_margin + scroll_bar;
	var content_height = nest_object.clientHeight + body_margin + form_space + title_bar;
	var window_height = Math.min(Math.round(window_width * scale), Math.min(content_height, screen.height));
	
	if (window_height == Math.round(window_width * scale) && Math.abs(window_height - content_height) < additional_range && content_height < screen.height - additional_range){
		window_height = content_height;
	}
	
	if (window_height < content_height){
		document.body.scroll = "auto";
	}else{
		document.body.scroll = "no";
	}

	window.resizeTo(window_width, window_height);
	
	var x = (screen.width) ? (screen.width - window_width) / 2 : 100;
	var y = (screen.height) ? (screen.height - window_height) / 2 : 100;
	
	window.moveTo(x, y);
}
