/*
* this script class depends on the prototype.js.
* 2009. 03. 24
* version 1.0
*/
var mailDomains =
	[
		'naver.com', 'chol.com', 'dreamwiz.com', 'empal.com', 'freechal.com', 'gmail.com',
		'hanafos.com', 'hanmail.net', 'hanmir.com', 'hitel.net', 'hotmail.com', 'korea.com', 'lycos.co.kr', 'nate.com',
		'netian.com', 'paran.com', 'yahoo.com', 'yahoo.co.kr'
	];

var PHONE_REGEXP = /^\(?(\d{2,4})\)?\-?(\d{3,4})\-?(\d{4})$/;
var MOBILE_PHONE_REGEXP = /^\(?(\d{3})\)?\-?(\d{3,4})\-?(\d{4})$/;
var ZIPCODE_REGEXP = /^\(?(\d{3})\)?\-?(\d{3})$/;
var MAIL_REGEXP = /^[\w_-]+(\.[\w_-]+)*@[\w_-]+(\.[\w_-]+)*\.\w{2,3}$/;
var IS_IE = Prototype.Browser.IE;
var IS_SUBMITTED = false;
var UtilsNew = Class.create({
	initialize : function(name){
		this.name = name;
		this.version = "1.0";
	},
	getName : function(){
		return this.name;
	},
	getVersion : function(){
		return this.version;
	},
	/*
	*@param event prototype's event
	*/
	numericOnly : function(event){
		/*
		* event code 8 = backspace
		* event code 9 = tab
		*/
		if(event.keyCode != 8 && event.keyCode != 9) {
			if((event.keyCode < 48)||(event.keyCode > 57)
					&& (event.keyCode < 96 || event.keyCode > 105 ) ) {
			   event.returnValue=false;
			}
		}
		return true;
	},
	isEmpty : function(string){
		return string.strip() == '';
	},
	isKorean : function(str) {
		var i;
		var ch;
		for (i=0;i<str.length;i++) {
			ch = escape(str.charAt(i));

			if (strCharByte(ch) != 2) {
				return false;
			}
		}
		return true;
	},
	setEmptyValue : function(fieldId, isFocus){
		$(fieldId).setValue('');
		if(isFocus){
			$(fieldId).focus();
		}
	},
	checkSocialNumber : function( firstNumber, secondNumber ){
		var i, sum = 0;
		var str = firstNumber + secondNumber;
		var first = firstNumber;
		var second=secondNumber;

		if( parseInt( second ) < 1000000 && parseInt( second ) > 2999999 ){
			return false;
		}

		if( str.length < 13 ){
			return false;
		}

		for (i=0,sum=0; i<12; i++){
			sum += ( ( ( i % 8 ) + 2 ) * ( str.charAt( i ) - "0" ) );
		}
		sum = 11 - ( sum % 11 );
		sum = sum % 10;

		if( sum == str.charAt( 12 ) ){
			return true;
		}
		return false;
	},
	/*
	*returns the input text length
	*/
	getTextLength : function(str){
		var retCode = 0;
		var strLength = 0;
		for (i = 0; i < str.length; i++) {
			var code = str.charCodeAt(i)
			var ch = str.substr(i,1).toUpperCase()
			code = parseInt(code)

			if ((ch < "0" || ch > "9") && (ch < "A" || ch > "Z") && ((code > 255) || (code < 0)))
				strLength = strLength + 2;
			else
				strLength = strLength + 1;
		}
		return strLength;
	},
	isValidPhone : function( text ) {
		return this.isValidFormat(text, PHONE_REGEXP);
	},
	isValidMobilePhone : function( text ) {
		return this.isValidFormat(text, MOBILE_PHONE_REGEXP);
	},
	isValidEmail : function( text ) {
		return this.isValidFormat(text, MAIL_REGEXP);
	},
	isValidFormat : function (text, format) {
		var str = text

		if( str == null){
			return false;
		}
	    if (str.search(format) != -1) {
	        return true;
	    }
	    return false;
	},
	isDigit : function(value){
		return !isNaN(value);
	},
	getSequence : function(id){
		return id.substring(((id.lastIndexOf("_")+1)));
	},
	//Reject 2byte character
	rejectKoChar : function(inputElement){
		if(this.codeMask(inputElement) == 1 ){
			element.value = '';
			element.focus();
			return false;
		}
	},
	codeMask : function( objects ) {
		var Svalue = objects.value;
		var	isNumber = 0;
		var	isChar = 0;
		if ( Svalue == "" ) {
			return -1;
		}

		for ( var Ncnt = 0 ; Ncnt < Svalue.length ; Ncnt ++ ) {
			if (( '0' <= Svalue.charAt( Ncnt )) && ( Svalue.charAt( Ncnt ) <= '9' )) {
				isNumber ++;
				continue;
			} else if (( 'A' <= Svalue.charAt( Ncnt )) && ( Svalue.charAt( Ncnt ) <= 'Z' )) {
				isChar ++;
				continue;
			} else if (( 'a' <= Svalue.charAt( Ncnt )) && ( Svalue.charAt( Ncnt ) <= 'z' )) {
				isChar ++;
				continue;
			} else {
				return 1;
			}
		}

		if ( isNumber == Svalue.length )
			return 2;

		if ( isChar == Svalue.length )
			return 3;

		return 0;
	},
	textMask : function(value){
		var Svalue = value;
		var	isNumber = 0;
		var	isChar = 0;
		if ( Svalue == "" ) {
			return -1;
		}

		for ( var Ncnt = 0 ; Ncnt < Svalue.length ; Ncnt ++ ) {
			if (( '0' <= Svalue.charAt( Ncnt )) && ( Svalue.charAt( Ncnt ) <= '9' )) {
				isNumber ++;
				continue;
			} else if (( 'A' <= Svalue.charAt( Ncnt )) && ( Svalue.charAt( Ncnt ) <= 'Z' )) {
				isChar ++;
				continue;
			} else if (( 'a' <= Svalue.charAt( Ncnt )) && ( Svalue.charAt( Ncnt ) <= 'z' )) {
				isChar ++;
				continue;
			} else {
				return 1;
			}
		}

		if ( isNumber == Svalue.length )
			return 2;

		if ( isChar == Svalue.length )
			return 3;

		return 0;
	},
	getEventTarget : function(event){
		if( event == null ){
			return null;
		}
		if( event.target ){
			return target;
		}else if( event.srcElement){
			return event.srcElement;
		}
		return null;
	},
	limitBytes : function(length, event){
		var element = this.getEventTarget(event);
		if(element == null){
			return true;
		}
		if(this.getTextLength(element.value.trim()) > length){
			element.value=element.value.substring(0, element.value.length -1);
			return false;
		}
		return true;
	},
	/**
	 * create select options. all elements removes before create options.
	 * if OPTIONS.length==0, add "new Option("Select", "0", false, false)" option.
	 * cause) must vals.length == opts.length.
	 *
	 * @param {Object} selObj SELECT ELEMENT
	 * @param {Object} jsonAry OPTIONm  Values - JSON Array
	 * @param {Object} valKey OPTIONm  Value key
	 * @param {Object} textKey OPTION Text key
	 * @param {Object} defSelVal Default Select Values, it null, not default select.
	 * @param {Object} isDefSelectionStr a value for default selection.
	 */
	createSelect : function( selObj, jsonAry, valKey, textKey, defSelVal, isDefSelectionStr){
		var def = 0;
		this.removeAllOptions(selObj);
		if( jsonAry == null || jsonAry.length == 0 ){
			if(isDefSelectionStr){
				if( IS_IE ){
					selObj.add(new Option("Select", '', false, false));
				}else{
					selObj.appendChild(new Option("Select", '', false, false));
				}
			}
			return;
		}else{
			if(isDefSelectionStr){
				if( IS_IE){
					selObj.add(new Option("Select", '', false, false));
				}else{
					selObj.appendChild(new Option("Select", '', false, false));
				}
			}
		}
		if( IS_IE ){
			if( jsonAry != null && jsonAry.length > 0){
				for( i = 0; i < jsonAry.length; i++ ){
					selObj.add(new Option(jsonAry[i][valKey], jsonAry[i][textKey], false, false));
				}
			}else{
				for( i = 0; i < jsonAry.length; i++ ){
					selObj.add(new Option(jsonAry[i][valKey], '', false, false));
				}
			}
		}else{
			if( jsonAry != null && jsonAry.length > 0){
				for( i = 0; i < jsonAry.length; i++ ){
					selObj.appendChild(new Option(jsonAry[i][valKey], jsonAry[i][textKey], false, false));
				}
			}else{
				for( i = 0; i < jsonAry.length; i++ ){
					selObj.appendChild(new Option(jsonAry[i][valKey], '', false, false));
				}
			}
		}
		if( defSelVal ){
			selObj.value = defSelVal;
		}
	},
	/**
	 * removes all options of Select element.
	 * @param {Object} sourceSelect source SELECT Element
	 */
	removeAllOptions : function (sourceSelect){
		if( !sourceSelect)
			return;
		var elm = sourceSelect.options;
		for((i = (elm.length-1)), k = 0; i >= k; i--){
			sourceSelect.removeChild(elm[i]);
		}
	},
	/**
	*@param id
	*@param type
	*/
	getCheckedCount : function(id, type){
		var elm = null;
		if( type == 0 ){
			elm = $$('input[id='+id+']');
		}else{
			elm = $$('input[id^='+id+']');
		}
		if( elm == null || elm.length == 0 ){
			return 0;
		}
		var count = 0;
		for(i = 0; i < elm.length; i++ ){
			if( elm[i].checked){
				count++;
			}
		}
		return count;
	},
	/**
	*@param id
	*@param type
	*@return
	*/
	getCheckedValue : function(id, type, attr){
		var elm = null;
		var propName = attr==null?"id":attr;
		if( type == 0 ){
			elm = $$('input['+propName+'='+id+']');
		}else{
			elm = $$('input['+propName+'^='+id+']');
		}
		if( elm == null || elm.length == 0 ){
			return null;
		}
		for(i = 0; i < elm.length; i++ ){
			if( elm[i].checked){
				return elm[i].value;
			}
		}
		return null;
	},
	/**
	*@param id
	*@param type
	*@param delim
	*@return
	*/
	getCheckedValues : function(id, type, delim){
		var elm = null;
		if( type == 0 ){
			elm = $$('input[id='+id+']');
		}else{
			elm = $$('input[id^='+id+']');
		}
		if( elm == null || elm.length == 0 ){
			return "";
		}
		var str = "";
		for(i = 0; i < elm.length; i++ ){
			if( elm[i].checked){
				str += elm[i].value + delim;
			}
		}
		if( str.substring(str.length-1) == delim ){
			str = str.substring(0, str.length -1);
		}
		return str;
	},
	/**
	*@param id
	*@param type
	*/
	getUnCheckedCount : function(id, type){
		var elm = null;
		if( type == 0 ){
			elm = $$('input[id='+id+']');
		}else{
			elm = $$('input[id^='+id+']');
		}
		if( elm == null || elm.length == 0 ){
			return 0;
		}
		var count = 0;
		for(i = 0; i < elm.length; i++ ){
			if(!elm[i].checked){
				count++;
			}
		}
		return count;
	},
	/**
	*@param id
	*@param type
	*@param delim
	*@return
	*/
	getUnCheckedValues : function(id, type, delim){
		var elm = null;
		if( type == 0 ){
			elm = $$('input[id='+id+']');
		}else{
			elm = $$('input[id^='+id+']');
		}
		if( elm == null || elm.length == 0 ){
			return "";
		}
		var str = "";
		for(i = 0; i < elm.length; i++ ){
			if(!elm[i].checked){
				str += elm[i].value + delim;
			}
		}
		if( str.substring(str.length-1) == delim ){
			str = str.substring(0, str.length -1);
		}
		return str;
	}
});

var AutoSelector = Class.create({
	initialize : function(inputFieldId, domainDisplayDivId, topClass, childClass, selectSources){
		this.topClass = topClass;
		this.childClass = childClass;
		this.field = $(inputFieldId);
		this.div = $(domainDisplayDivId);
		this.domains = selectSources;
		this.childElements = null;
		this.start = 0;
		this.backColor = '#ccc';
		this.orgBackColor = '#fff';
		this.orgValue = '';
		this.URL = null;
		this.params = null;
	},
	setBackColor : function(backColor){
		this.backColor = backColor;
	},
	setOrgBackColor : function(orgColor){
		this.orgBackColor = orgColor;
	},
	init : function(){
		Event.observe(this.field, 'keyup', function(event){
			this.fireSelectTarget.apply(this, arguments);
		}.bind(this));
	},
	setURL : function(url, params){
		this.URL = url;
		this.params = params;
	},
	fireSelectTarget : function(event){
		var element = Event.element(event);

		if(event.keyCode == Event.KEY_TAB || event.keyCode == Event.KEY_ESC){
			//element.value = this.orgValue;
			this.div.hide();
			this.removeSelectorNodeTop();
			return;
		}

		if(this.URL == null ){	//call local json element
			if( event.keyCode != Event.KEY_UP && event.keyCode != Event.KEY_DOWN
				&& event.keyCode != 27 && event.keyCode != Event.KEY_RETURN){
				var domains = this.findoutTargets($F(element));
				this.orgValue = element.value;
				if(domains.length > 0){
					this.createTargetSelector(domains);
					this.div.show();
				}else{
					this.div.hide();
				}
			}else{
				if(event.keyCode == Event.KEY_DOWN){
					this.setBackGroundAndPickupTextDown();
				}else if(event.keyCode == Event.KEY_UP){
					this.setBackGroundAndPickupTextUp();
				}else if(event.keyCode == 27){
					element.value = this.orgValue;
					this.div.hide();
					this.removeSelectorNodeTop();
				}else if(event.keyCode == Event.KEY_RETURN){
					this.div.hide();
					this.removeSelectorNodeTop();
					this.resetStart();
				}
			}
		}else{	//use xhttp remote call

		}
	},
	setBackGroundAndPickupTextDown : function(){
		if(!$('selector_node_top')){
			return;
		}
		this.clearBackground();
		$('selector_child_' + this.start).setStyle({backgroundColor : this.backColor})
		this.field.value = $('selector_child_' + this.start).innerHTML.unescapeHTML();
		if(this.start < this.childElements.length-1){
			this.start++;
		}
	},
	setBackGroundAndPickupTextUp : function(){
		if(!$('selector_node_top')){
			return;
		}
		if(this.start > 0){
			this.start--;
			this.clearBackground();
			$('selector_child_' + this.start).setStyle({backgroundColor : this.backColor});
			this.field.value = $('selector_child_' + this.start).innerHTML.unescapeHTML();
		}else{
			//this.field.value = this.orgValue;
			this.div.hide();
			this.removeSelectorNodeTop();
		}

	},
	clearBackground : function(){
		this.childElements.each(function(obj){
			obj.setStyle({backgroundColor : this.orgBackColor})
		}.bind(this));
	},
	findoutTargets : function(inputValue){
		var ary = new Array();
		var count = 0;
		for( i = 0; i < this.domains.length; i++){
			if(!inputValue.blank() && this.domains[i].startsWith(inputValue)){
				ary[count++] = this.domains[i];
				continue;
			}
		}
		return ary;
	},
	createTargetSelector : function(elements){
		this.removeSelectorNodeTop();
		var html = "<ul id='selector_node_top' class='"+this.topClass+"'>";
		for( i = 0; i < elements.length; i++){
			html += "<li id='selector_child_"+i+"' class='"+this.childClass+"'>" + elements[i] + "</li>";
		}
		html += "</ul">
		this.div.insert(html);
		this.childElements = $$('li[id^=selector_child]');
		this.childElements.each(function(obj){
			obj.observe('click', function(event){
				this.fireSelect.apply(this, arguments);
			}.bind(this));
			obj.observe('mouseover', function(event){
				Event.element(event).setStyle({'cursor':'pointer'});
			}.bind(this));
		}.bind(this));
	},
	fireSelect : function(event){
		var element = Event.element(event);
		this.field.value = element.innerHTML.unescapeHTML();
		this.field.focus();
		this.resetStart();
		this.div.hide();
		this.removeSelectorNodeTop();
	},
	resetStart : function(){
		this.start = 0;
	},
	removeSelectorNodeTop : function(){
		if($('selector_node_top')){
			$('selector_node_top').remove();
		}
	}
});

var ShowMessage = Class.create({
	initialize : function(id, msg, isDelay){
		this.id = id;
		this.msgDiv = $(id);
		this.msg = msg;
		this.name = "message creator";
		this.isDelay = isDelay;
	},
	show : function(){
		this.msgDiv.update(this.msg);
		if(this.isDelay){
			window.setTimeout(function(){
				this.msgDiv.update('');
			}.bind(this), 3000);
		}
	}
});

function rejectKoChar(element){
	if(codeMask(element) == 1 ){
		element.value = '';
		element.focus();
		return false;
	}
}