function tooltip(){
	// alert('tooltip init');
	$("*[jtip]").each(function(i){
		if(!this.id)
			this.id = this.name;
		if(!this.id){
			this.id = 'X'+Math.ceil(Math.random()*10000000000000);
		}
		
		$("#"+this.id).tooltip({
			bodyHandler: function() {
				return $("#"+this.id).attr("jtip");
			},
			track: true,
			delay: 0,
			showURL: false,
			fade: 250
		});
	});
}

function inputCtypeDate(){
	$("input[cType='date']").each(function(i){
		if(!this.id)
			this.id = this.name;
		
		var myId = "#"+this.id;
		 $(myId).DatePicker({
		 	calendars:1,
			format:'Y-m-d',
			date:  $(myId).val(),
			current:  $(myId).val(),
			starts: 1,
			position: 'bottom',
			onBeforeShow: function(){
				 $(myId).DatePickerSetDate( $(myId).val(), true);
			},
			onChange: function(formated, dates){
				 $(myId).val(formated);
			}
		});
		$(myId).attr({ 
			readonly:true
		});
		$(myId).addClass("datepickerInput");
	});
}

function formInput(){
	$("#petForm input").each(function(i){
		if(this.type == 'text' || this.type == 'password'){
			if(!this.id)
				this.id = this.name;
			var myId = "#"+this.id;
			$(myId).bind('keypress', function(evt) {
			    var charCode = (evt.which) ? evt.which : window.event.keyCode;
			    if(charCode == '13'){
			    	try{
			    		doAction();
			    	}catch(e){}
			    }
			});
		}
	});
	
	$("#petForm select").each(function(i){
		if(this.bind != 'no'){
			if(!this.id)
				this.id = this.name;
			var myId = "#"+this.id;
			$(myId).bind('change', function(evt) {
			    		doAction();
			});
		}
	});
}

function selectBoxOption(){
	$("select[ajax]").each(function(i){
		if(!this.id)
			this.id = this.name;
		var myId = "#"+this.id;
		var ajax = $(myId).attr("ajax");
		var url;
		if(ajax == 'comm' || ajax == 'CommCode'){
			url = 'commService.getCommCode';
		}else{
			url = 'commService.get'+ ajax;
		}
		var prev = $(myId).attr("prev");
		var param = $(myId).attr("param");
		if(!param)
			param = '';
		var initValue = $(myId).attr("initValue") == ""?"":$(myId).attr("initValue");
		
		if(prev){
			$("#"+prev).change(function(e){
				if($("#"+prev).val() != '')
					makeSelectBox(url,$("#"+prev).val(),myId,initValue);
				else
					$(myId).removeOption(/./);
			});
			
			if(param != ''){
				makeSelectBox(url,param,myId,initValue);
			}else{
				// $(myId).addOption('',':: ¼±ÅÃ ::');
			}
		}else{
			makeSelectBox(url,param,myId,initValue);
		}
	});
}

// make select box
// by shue177
function makeSelectBox(url,param,myId,initValue){
	var exeStr =url+"("+((param != '')?"'"+param+"',":"")+"function(data){$(myId).removeOption(/./);if(data && data.length > 0){if(!$(myId).attr('required')){$(myId).addOption('',':: ¼±ÅÃ ::');}	$.each(data, function(i,data){$(myId).addOption(data.code,data.code_name);});	if(initValue){$(myId).selectOptions(initValue);	}else{$(myId).selectOptions('');}}})"; 
	eval(exeStr);
}



function inputCtypeInt(){
	$("input[cType='int']").each(function(i){
		if(!this.id)
			this.id = this.name;
		var myId = "#"+this.id;
		// $(myId).addClass("input-right");
		$(myId).addClass("ime-disable");
		numberComma(myId);
		$(myId).bind('keypress', function(e) {
		    if(!intOnly(e)){
		    	e.preventDefault();
		    }
		});
		$(myId).bind('keyup', function(e) {
		    numberComma(myId);
		});
	});
}

function inputCtypeNum(){
	$("input[cType='num']").each(function(i){
		if(!this.id)
			this.id = this.name;
		var myId = "#"+this.id;
		$(myId).addClass("ime-disable");
		$(myId).bind('keypress', function(e) {
		    if(!intOnly(e)){
		    	e.preventDefault();
		    }
		});
	});
}

function inputCtypeEng(){
	$("input[cType='eng']").each(function(i){
		if(!this.id)
			this.id = this.name;
		var myId = "#"+this.id;
		$(myId).addClass("ime-disable");
		$(myId).bind('keypress', function(e) {
		    if(!engOnly(e)){
		    	e.preventDefault();
		    }
		});
	});
}

function inputCtypeFloat(){
	$("input[cType='float']").each(function(i){
		if(!this.id)
			this.id = this.name;
		var myId = "#"+this.id;
		//$(myId).addClass("input-right");
		$(myId).addClass("ime-disable");
		numberComma(myId);
		$(myId).bind('keypress', function(e) {
		    if(!floatOnly(e,myId)){
		    	e.preventDefault();
		    }
		});
		
		$(myId).bind('keyup', function(e) {
		    numberComma(myId);
		});
	});
}


function inputCtypeTelNum()
{
	$("input[cType='telNum']").each(function(i)
	{
		if (!this.id)
			this.id = this.name;
		var myId = "#"+this.id;
		$(myId).addClass("ime-disable");
		$(myId).bind('keypress', function(e) {
		    if(!intOnly(e)){
		    	e.preventDefault();
		    }
		});
		
		$(myId).bind('focus', function(e)
		{
			$(myId).val($(myId).val().replace(/[-]+/g, ''));
		});
		
		$(myId).bind('blur', function(e)
		{
			var found = false;
			var myValue = $(myId).val();
			var twoDigitPrefix = new Array("02");
			var threeDigitPrefix = new Array("010", "011", "016", "019", "032", "042", "062", "051", "053", "052", "031", "033", "041", "043", "061", "063","055", "054", "064");
			
			if (myValue.length > 8 && myValue.length < 11)
			{
				for (i = 0; i < twoDigitPrefix.length; ++i)
				{
					if (myValue.substr(0, 2) == twoDigitPrefix[i])
					{
						switch (myValue.length)
						{
							case 9: $(myId).val(divTelNum(myValue, 2, 3)); found = true; break;
							case 10: $(myId).val(divTelNum(myValue, 2, 4)); found = true; break;
						};
						break;
					}
				}
			}
			if (!found && myValue.length > 9 && myValue.length < 12)
			{
				for (i = 0; i < threeDigitPrefix.length; ++i)
				{
					if (myValue.substr(0, 3) == threeDigitPrefix[i])
					{
						switch (myValue.length)
						{
							case 10: $(myId).val(divTelNum(myValue, 3, 3)); found = true; break;
							case 11: $(myId).val(divTelNum(myValue, 3, 4)); found = true; break;
						};
						break;
					}
				}
			}
			if (!found && myValue.length > 10 && myValue.length < 13)
			{
				switch (myValue.length)
				{
					case 11: $(myId).val(divTelNum(myValue, 4, 3)); found = true; break;
					case 12: $(myId).val(divTelNum(myValue, 4, 4)); found = true; break;
				};
			}
		});
	});
}

function divTelNum(value, length1, length2)
{
	var result = value.substr(0, length1) + "-" + value.substr(length1, length2) + "-" + value.substr(length1+length2, value.length);
	return result;
}


//validateion
function validate(){
	$("input").each(function(i){
		// ÀúÀå½Ã ÄÞ¸¶Á¦°Å
		try{
			if(this.cType == 'int' || this.cType == 'float'){
				this.value = this.value.replace(/,/g,"");
			}
			
		}catch(e){}
		// ÀúÀå½Ã - Á¦°Å
		try{
			if(this.cType == 'date' || this.cType == 'telNum'){
				this.value = this.value.replace(/-/g,"");
			}
		}catch(e){}
		if(!this.id)
			this.id = this.name;
		var myId = this.id;
		
		// $("#"+myId).val($.trim(this.value));
	});
	
	$("textarea").each(function(i){
		if(!this.id)
			this.id = this.name;
		var myId = this.id;
		$("#"+myId).val($.trim(this.value));
	});
	
	var success = true;
	$("*[required]").each(function(i){
		if(!this.id)
			this.id = this.name;
		var myId = this.id;
		if($.trim(this.value) == ''){
			alert("'"+$("#"+myId).attr('required')+"' ÇÊ¼ö ÀÔ·Â Ç×¸ñÀÔ´Ï´Ù.");
			try{
				$("#"+myId).focus();
			}catch(e){}
			success= false;
			return success;
		}
	});
	
	if(success){
		$("input[cType='email']").each(function(i){
			if(!this.id)
				this.id = this.name;
			var myId = this.id;
			if(!isValidEmail($.trim(this.value))){
				alert("¿Ã¹Ù¸¥ ÀÌ¸ÞÀÏ Çü½ÄÀÌ ¾Æ´Õ´Ï´Ù.");
				try{
					$("#"+myId).focus();
				}catch(e){}
				success=false;
				return success;
			}
		});
	}
	return success;
}

// ÀÌ¸ÞÀÏ Çü½Ä validation
function isValidEmail(email){
	var emailEx1 = /^([A-Za-z0-9_]{1,15})(@{1})([A-Za-z0-9_]{1,15})(.{1})([A-Za-z0-9_]{2,10})(.{1}[A-Za-z]{2,10})?(.{1}[A-Za-z]{2,10})?$/; 
	return emailEx1.test(email);
}

// menu popup
function popWin(url,name,width,height){
	window.open(url,name,"width="+width+",height="+height+",toolbar=no,menubar=no,location=no,scrollbars=yes,status=yes,resizable=yes,fullscreen=no,channelmode=no,left=0,top=0");
}

// int only
function intOnly(evt){
	var charCode = (evt.which) ? evt.which : window.event.keyCode;
	
	if (charCode <= 13){
		return true;
	}else{
		var keyChar = String.fromCharCode(charCode);
		var re = /[0-9]/
		return re.test(keyChar);
	}
}

// eng only
function engOnly(evt){
	var charCode = (evt.which) ? evt.which : window.event.keyCode;
	
	if (charCode <= 13){
		return true;
	}else{
		var keyChar = String.fromCharCode(charCode);
		var re = /[a-zA-Z0-9]/
		return re.test(keyChar);
	}
}

// float only
function floatOnly(evt,myId){
	var charCode = (evt.which) ? evt.which : window.event.keyCode;
	
	if (String.fromCharCode(charCode)=="."){
		if ($(myId).val() == "") return false;  
		if ($(myId).val().indexOf('.') > 0) return false;
	}
	
	if (charCode <= 13){
		return true;
	}else{
		var keyChar = String.fromCharCode(charCode);
		var re = /[0-9.]/
		return re.test(keyChar);
	}
}

var tmpNC="";
function numberComma(myId){
	// if(tmpNC==$(myId).val()) return;
	tmpNC=$(myId).val();
	$(myId).val(getNumberComma(tmpNC));
}

function getNumberComma(s){
	// toString
	s = s+'';
	// dot indexing
	var dot = s.indexOf('.');
	var extra = '';
	
	if(dot > 0){
		extra = s.substr(dot);
		s = s.substr(0,dot);
	}
	
	s=s.replace(/\D/g,"");
	l=s.length-3;
	while(l>0) {
		s=s.substr(0,l)+","+s.substr(l);
		l-=3;
	}
	return s+extra;
}

function strToDate(str){
	return new Date(str.substr(0,4), str.substr(4,2)-1, str.substr(6,2)); 
}

function isNumVal(NUM) {
	for(var i=0;i<NUM.length;i++){
		achar = NUM.substring(i,i+1);
		if( achar < "0" || achar > "9" ){
			return false;
		}
	}
	return true;
}

// $ document ready
$(document).ready(function() {
	try{
		springValidate();
	}catch(e){}
	
	try{
		selectBoxOption();
	}catch(e){}

	inputCtypeInt();
	inputCtypeEng();
	//inputCtypeFloat();
	inputCtypeNum();
	inputCtypeTelNum();
	inputCtypeDate();
	
	try{
		tooltip();
	}catch(e){}
	
	try{
		formInput();
	}catch(e){}
});

function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}


function URLDecode (encodedString) {
  var output = encodedString;
  var binVal, thisString;
  var myregexp = /(%[^%]{2})/;
  while ((match = myregexp.exec(output)) != null
             && match.length > 1
             && match[1] != '') {
    binVal = parseInt(match[1].substr(1),16);
    thisString = String.fromCharCode(binVal);
    output = output.replace(match[1], thisString);
  }
  return output;
}

function getBytes(str){
  var tcount = 0;

  var tmpStr = new String(str);
  var temp = tmpStr.length;

  var onechar;
  for ( k=0; k<temp; k++ )
  {
    onechar = tmpStr.charAt(k);
    if (escape(onechar).length > 4)
    {
      tcount += 2;
    }
    else
    {
      tcount += 1;
    }
  }

  return tcount;
}

function toObjectMap( queryString ){    
	return '{\'' + queryString.replace(/=/g, '\':\'').replace(/&/g, '\',\'') + '\'}';
}

function reload(){
	top.location.replace(top.location.href);
}

function parentReload(){
	parent.location.replace(parent.location.href);
}