/**
 * 공통 자바스트립트 파일
 * 2009.02.06
 * 		checkJuminNo() 추가 
 */

function Message(arr) {
	this.errorMsg = new Array();
	this.errorIds = new Array();
	this.warningMsg = new Array();
	this.isSuccess = arr['isSuccess'];
	this.data = arr['data'];
	
	var errorMsg = arr['errorMsg'];
	for(var i = 0; i < errorMsg.length ; i++){
		this.errorMsg.push(errorMsg[i]);
	}
	
	var errorIds = arr['errorIds'];
	for(var i = 0; i < errorIds.length ; i++){
		this.errorIds.push(errorIds[i]);
	}
	
	var warningMsg = arr['warningMsg'];
	for(var i = 0; i < warningMsg.length ; i++){
		this.warningMsg.push(warningMsg[i]);
	}
	
	this.print = function(opt){
		if(this.errorMsg.length) {
			for(var i = 0 ; i < this.errorMsg.length ; i++){
				this.alert(this.errorMsg[i], opt);
			}
		}
		if(this.warningMsg.length) {
			for(var i = 0 ; i < this.warningMsg.length ; i++){
				this.alert(this.warningMsg[i], opt);
			}
		}
	}
	this.alert = function(str, option) {
		jfAlert(str, option );
	}
}

function jfAlert(msg, opt) {
	switch( opt ) {
		case 'dialog' :
			var str = "<div class='jfAlert' title=\"안내\">"+ msg +"</div>";
			$("body").append(str);
			$(".jfAlert").dialog({ close: function(event,ui) { $(".jfAlert").remove(); } });
			break;
			
		case 'modal' :
			var str = "<div class='jfAlert' title=\"안내\">"+ msg +"</div>";
			$("body").append(str);
			$(".jfAlert").dialog({
				modal: true,
				buttons: {
					ok: function() {
						$(this).dialog('close');
					}
				},				
				close: function(event,ui) { $(".jfAlert").remove(); }
			});
			break;
			
		case 'growl' :
			var str = "<div class='jfAlert' title=\"안내\">"+ msg +"</div>";
			$("body").append(str);			
			$(".jfAlert").fadeIn();
			window.setTimeout("$('.jfAlert').fadeOut( function() { $('.jfAlert').remove(); } );",3000);
			break;

		default :
			alert(msg);
			break;
	}
}

function number_format(str) {
	if(str == '') return '0';
	str = ""+str+"";
	var retValue = "";
	for(iii=0; iii<str.length; iii++) {
		if(iii > 0 && (iii%3)==0) {
			retValue = str.charAt(str.length - iii -1) + "," + retValue;
		} else {
			retValue = str.charAt(str.length - iii -1) + retValue;
		}
	}
	return retValue;
}

function getInt(num1) {
	var myre=/,/gi;
	var num2=num1.replace(myre,"");
	if (isNaN(parseInt(num2,10))==false) {
		return parseInt(num2,10);
	} else {
		return parseInt(0);
	}
}

function isInt(str){
	if(/[^0-9]/g.test(str)) return false;
	else return true;
}

function fixEvent(e) {
	var e = document.documentElement, b = document.body;
	e.pageX = e.clientX + (e && e.scrollLeft || b.scrollLeft || 0);
	e.pageY = e.clientY + (e && e.scrollTop || b.scrollTop || 0);
	return e;
}

function checkJuminNo(resno) {
	// 주민번호의 형태와 7번째 자리(성별) 유효성 검사
	fmt = /^\d{6}-[1234]\d{6}$/;
	if (!fmt.test(resno)) {
		return false;
	}

	// 날짜 유효성 검사
	birthYear = (resno.charAt(7) <= "2") ? "19" : "20";
	birthYear += resno.substr(0, 2);
	birthMonth = resno.substr(2, 2) - 1;
	birthDate = resno.substr(4, 2);
	birth = new Date(birthYear, birthMonth, birthDate);

	if ( birth.getYear() % 100 != resno.substr(0, 2) ||
	     birth.getMonth() != birthMonth ||
	     birth.getDate() != birthDate) {
	     return false;
	}

	// Check Sum 코드의 유효성 검사
	buf = new Array(13);
	for (i = 0; i < 6; i++) buf[i] = parseInt(resno.charAt(i));
	for (i = 6; i < 13; i++) buf[i] = parseInt(resno.charAt(i + 1));
	
  	multipliers = [2,3,4,5,6,7,8,9,2,3,4,5];
	for (i = 0, sum = 0; i < 12; i++) sum += (buf[i] *= multipliers[i]);

	if ((11 - (sum % 11)) % 10 != buf[12]) {
	  return false;
	}

	return true;
}

function setCookie(name,value,expireDays){
	var str = name + "=" + escape(value) + "; path=/; ";
	if(expireDays) {
		var todayDate = new Date();
		todayDate.setDate( todayDate.getDate() + expireDays);
		str += "expires=" + todayDate.toGMTString() + ";"
	}
	document.cookie = str;
}

function getCookie (name) {
	var dcookie = document.cookie;
	var cname = name + "=";
	var clen = dcookie.length;
	var cbegin = 0;
	while (cbegin < clen) {
		var vbegin = cbegin + cname.length;
			if (dcookie.substring(cbegin, vbegin) == cname) {
				var vend = dcookie.indexOf (";", vbegin);
				if (vend == -1) vend = clen;
			return unescape(dcookie.substring(vbegin, vend));
		}
		cbegin = dcookie.indexOf(" ", cbegin) + 1;
		if (cbegin == 0) break;
	}
	return "";
}
