﻿function getStrLen(str) {
	var nbytes=0;
	for (i=0; i<str.length; i++) {
		var ch=str.charAt(i);
		if(escape(ch).length > 4) {nbytes += 2;}
		else if (ch=='\n')
			{if (str.charAt(i-1)!='\r') {nbytes += 1;}}
		else if (ch=='<' || ch=='>') {nbytes += 4;}
		else {nbytes += 1;}
	}
	return nbytes;
}

function isPassword(str)
{
	var reg=/^[a-z0-9]{5,16}$/;
	if(str.match(reg)==null) {return false;}
}

function isEmail(str)
{	
	var reg=/^[_0-9a-zA-Z-]+(\.[_0-9a-zA-Z]+)*@[0-9a-zA-Z-]+(\.[0-9a-zA-Z]+)+$/g;	
	if(str.match(reg)==null) {return false;}
}

function isUrl(str)
{	
	var reg=/(http|https)\:\/\/[a-zA-z]+(\.[a-zA-Z0-9]+)*(\.[a-zA-Z0-9]+)/;
	if(str.match(reg)==null) {return false;}
}

function isPhone(str)
{	
	var reg=/^\d{2,3}-\d{3,4}-\d{4}$/g;	
	if(str.match(reg)==null) {return false;}
}

function isDate(str)
{
	var reg=/^\d{4}-\d{2}-\d{2}$/
	if(str.match(reg)==null) {return false;}
}

function isTime(str)
{
	var reg=/^\d{2}:\d{2}$/
	if(str.match(reg)==null) {return false;}
}

function isKorean(str){
	var reg = /[가-�R]+$/;
	if(!str.match(reg)){
		return false;
	}
		return true;
}

function isBlank(str)
{
	var arrStr=str.split(" ")
	if (str.length+1==arrStr.length)
		return true;
	return false;
}

function isOverLen(str,len)
{
	if (getStrLen(str)>len)
		return true;
	else
		return false;
}

function isSpace(str) 
{
  var chkstr = str + "";
  var is_space = false ;
  for ( j = 0 ; j < chkstr.length  ; j++)
  {
    if( chkstr.substring( j , j+1 ) == " " )
      is_space = true ;
  }
  return ( is_space );
}

function chkReg(obj,reg,str)
{
	if(!obj){return false;}
	if(obj.value.match(reg)==null)
	{
		if(str) {alert(str);}
		try{obj.focus();}catch(e){}
		return false;
	}
	return true;
}

function chkBlank(obj,str)
{
	if(!obj){return false;}
	if(isBlank(obj.value))
	{
		if(str) {alert(str);}
		try{obj.focus();}catch(e){}		
		return false;
	}
	return true;
}

function chkName(obj){
	if(!obj){return false;}
	if(isBlank(obj.value) || obj.value.length < 2) { 
		alert("input name");
		try{obj.focus();}catch(e){}		
		return false;
	}
	if(!isKorean(obj.value) || isSpace(obj.value)) {
		alert("input currently name");
		try{obj.focus();}catch(e){}		
		return false;
	}
	return true;
}

function chkNumber(obj,str)
{
	if(!obj){return false;}
	if(isBlank(obj.value) || isNaN(obj.value))
	{
		if(str) {alert(str);}
		obj.value="";
		try{obj.focus();}catch(e){}		
		return false;
	}
	return true;
}

function chkPassword(obj1,obj2)
{
	if(isBlank(obj1.value))
	{
		alert("input passord");
		try{obj1.focus();}catch(e){}		
		return false;
	}
	
	if(isPassword(obj1.value)==false)
	{
		alert("input currently password");
		try{obj1.focus();}catch(e){}		
		return false;
	}
	
	if(isBlank(obj2.value))
	{
		alert("input password2");
		try{obj2.focus();}catch(e){}		
		return false;
	}
	
	if(isPassword(obj2.value)==false)
	{
		alert("input currently password2");
		try{obj2.focus();}catch(e){}		
		return false;
	}
	
	if(obj1.value!=obj2.value)
	{
		alert('not compare password1 to password2');
		return false;
	}
	return true;
}

function chkEmail(obj,str,objFocus)
{
	if(!obj){return false;}
	if(!objFocus){objFocus=obj;}
	if(isEmail(obj.value)==false)
	{
		if(str) {alert(str);} else {alert("Please insert correct Email address");}
		try{objFocus.focus();}catch(e){}
		return false;
	}
	return true;
}

function chkUrl(obj,str,objFocus)
{
	if(!obj){return false;}
	if(!objFocus){objFocus=obj;}
	if(isUrl(obj.value)==false)
	{
		if(str) {alert(str);} else {alert("input currently URL");}
		try{objFocus.focus();}catch(e){}
		return false;
	}
	return true;
}

function chkPhone(obj,str)
{
	if(!obj){return false;}
	if(isPhone(obj.value)==false)
	{
		if(str) {alert(str);}
		obj.value="";
		try{obj.focus();}catch(e){}		
		return false;
	}
	return true;
}

function chkDate(obj,str)
{
	if(!obj){return false;}
	if(isDate(obj.value)==false)
	{
		if(str) {alert(str);} else {alert("input datetype\n\n예)2000-01-01");}
		obj.value="";
		try{obj.focus();}catch(e){}		
		return false;
	}
	return true;
}

function chkTime(obj,str)
{
	if(!obj){return false;}
	if(isTime(obj.value)==false)
	{
		if(str) {alert(str);} else {alert("datetimetype\n\n예)13:24");}
		obj.value="";
		try{obj.focus();}catch(e){}
		return false;
	}
	return true;
}

function chkOption(obj,str)
{
	if(!obj){return false;}
	var chkValue;
	for(var i=0;i<obj.length;i++)
	{
		if(!obj[i]){continue;}
		if(obj[i].checked) {chkValue=obj[i].value;break;}
	}
	if(!chkValue)
	{
		if(str) {alert(str);}
		return false;
	}
	return true;
}

function chgImg(obj)
{
	var tmp=obj.src;
	var ext=tmp.substr(tmp.lastIndexOf(".")+1,3)
	if(tmp.indexOf("_over."+ext)==-1)
		obj.src=tmp.replace("."+ext,"_over."+ext)
	else
		obj.src=tmp.replace("_over."+ext,"."+ext)
}

function chkRemain(maxLen,obj,rObj) {
	var remainbyte;
	if(!obj) {return false;}
	var length=getStrLen(obj.value);
	if(rObj) {remainbyte=rObj;}
	if(!remainbyte){return false;}
	remainbyte.innerHTML=length;

	if (length>maxLen) {
		alert("You can only insert less Than " + maxLen + " characters.");
		obj.value=obj.value.replace(/\r\n$/, "");
		obj.value=cutStr(obj.value,maxLen-1);
		chkRemain(maxLen,obj,rObj);
	}
}


function cutStr(value,maxLen) {
	var nbytes=0;
	var result="";
	var length=value.length;

	for (i=0;i<length;i++) {
		var ch=value.charAt(i);
		if(escape(ch).length>4) {nbytes+=2;}
		else if (ch=='\n') {if (value.charAt(i-1) != '\r') {nbytes+=1;}}
		else if (ch=='<' || ch=='>') {nbytes+=4;}
		else {nbytes+=1;}
		if (nbytes>maxLen+1) {break;}
		result+=ch;
	}
	return result;
}

function del_options(obj,opt) {
	var len = obj.options.length;
	
	if(opt=="all") {
		for(var i=0;i<len;i++)
			obj.options[0] = null;
	} else {
		for(var i=1;i<len;i++)
			obj.options[1] = null;
	}
}
function set_options(obj,array) {
	var opt_cnt = obj.options.length;
	for(var i=0;i<array.length;i++) {
		obj.options[opt_cnt] = new Option(array[i].name,array[i].value);
		opt_cnt++;		
	}
}

function win_open_center(url,name,width,height,scrollbars) {
	var posLt = (screen.availWidth-width)/2;
	var posTp = (screen.availHeight-height)/2;
	if (width==null && height==null) {
		var pop = window.open(url,name,"top="+posTp+",left="+posLt+",scrollbars="+scrollbars);
	} else {
		var pop = window.open(url,name,"top="+posTp+",left="+posLt+",width="+width+",height="+height+",scrollbars="+scrollbars);
	}
	try {pop.focus();} catch(e) {}
}

function viewImgPop(fileURL,width,height) {
	if(!fileURL) {return false;}
	var fileType = fileURL.substring(fileURL.lastIndexOf(".")+1).toUpperCase();
	if(fileType!="GIF" && fileType!="JPG" && fileType!="BMP") {
		return false;
	}
	if(!width || !height) {
		width=100;height=100;
		var posLt = 50;
		var posTp = 50;
	} else {
		var posLt = (sw-width)/2;
		var posTp = (sh-height)/2;
	}
	var popImg = window.open("/lib/pop_img.asp?FileURL="+escape(fileURL),"popImg","top="+posTp+",left="+posLt+",width="+width+",height="+height+",scrollbars=1");
	if(popImg) {popImg.focus();}
}
