﻿// ## Alias

var $N = Number;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



// ## Image OVER / OUT

var _on = "on.gif";
var _off = ".gif";

function overImg(oThis){
	if( oThis.src.search(_on) != -1 ) { 
		oThis.onmouseout = function(){};
		return;
	}
	
	oThis.src = oThis.src.replace(_off, _on);
}

function outImg(oThis){
	oThis.src = oThis.src.replace(_on, _off);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



// ## Common Function

function getMovie( id ) {
	
	var obj = document.getElementById( id );
	if( typeof obj != 'object' &&navigator.userAgent.indexOf("Safari") == -1 ) obj = document.getElementsByTagName( 'object' )[0];
			
	return obj;
	
}

function $id( obj ) {
	return document.getElementById( obj );
}

function getKeyCode( event ){
	return event.which || event.keyCode;
}

function getText( obj ){
	return obj.innerText || obj.textContent || obj.text || '';
}

function onlyNumber(){
	
	var event = arguments[0];
	var extend_permission = arguments[1];
	
	var keyCode = getKeyCode(event);
	var permission = "[48] [49] [50] [51] [52] [53] [54] [55] [56] [57] [96] [97] [98] [99] [100] [101] [102] [103] [104] [105] [35] [36] [46] [37] [39] [8] [9] ";
	permission = permission + extend_permission;
	//alert(keyCode);
	if(permission.indexOf("["+keyCode+"]") == -1  ) {
		if (event.preventDefault) event.preventDefault();
		else event.returnValue = false;
	}
}

function onlyNumber_comma(oThis, event, arg){
		
	onlyNumber(event, arg);
		
	var key_code = getKeyCode(event);
	if( (key_code >= 48 && key_code <= 57) || (key_code >= 96 && key_code <= 105) || key_code == 46 || key_code == 8 ){
		
		oThis.value = oThis.value.replaceAll(",", "" ).comma();
		
	}
	
}

function preview( oThis ){
	
	if( !oThis.value.match(/\.(jpg|jpeg|png|gif)$/i) || window.navigator.userAgent.indexOf("MSIE") == -1 ) return;
	
	$id("view_img").innerHTML = '';
	$id("view_img").style.width = "110px";
	$id("view_img").style.height = "145px";
	$id("view_img").style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + oThis.value + "' , sizingMethod='scale')";	
	
}

function addCommas( str ) {
	str = String(str);
 	var objRegExp = new RegExp('(-?[0-9]+)([0-9]{3})'); 
 	while( objRegExp.test(str) )  {
  	str = str.replace(objRegExp, '$1,$2');
 	}
 	return str;
}

function setSubmitMsg(){
	
	var msg = ( arguments[0] ) ? arguments[0] : "등록 중입니다. 다른작업을 하지마세요.";
	
	if( $id("submit_btn_wrap") ){
		
		var obj = getChildNodes( $id("submit_btn_wrap") );
		for( var i=0, j=obj.length; i<j; i++ ){
			
			if( obj[i].nodeType == 1 ){
				obj[i].style.visibility = 'hidden';
			}
			
		}
		
		var div = document.createElement( "div" );
		div.innerHTML = msg;
		$id("submit_btn_wrap").appendChild( div );
		
	}
	
}

//프린트
function PrintPop(bseq){

  var form=document.send_form;
  var oNewWin = window.open("","readPrintWin","width=670,height=600,left=300,top=100,scrollbars=1");
  form.bseq.value = bseq;
  form.action = "read_print.aspx";
  form.target = "readPrintWin";
  form.submit();
  
}

function send_sns( type, msg, url, thumbnail ){
			
	var s_url = encodeURIComponent( url );
	var s_thumbnail = encodeURIComponent( thumbnail );
	
	var tempText = msg;
	var text = encodeURIComponent(tempText);
	
	if( type == "facebook" ){
		window.open("http://www.facebook.com/share.php?u=" + s_url + "&t=[" + text + "]", "open_facebook", "width=1008, height=650");
	}else if( type == "twitter" ){
		window.open("http://twitter.com/share?url=" + s_url + "&text=[" + text + "]", "open_twitter", "width=1008, height=650");
	}else if( type == "me2day" ){
		text = '"' + text + '":' + s_url;
		window.open("http://www.me2day.net/posts/new?new_post[body]=" + text + "&new_post[url]=" + s_url, "open_me2day", "");
	}else if( type == "yo" ){
		window.open( "http://yozm.daum.net/api/popup/prePost?link=" + s_url + "&prefix=" + text, "yo", "width=1008, height=650" );
	}else if( type == "cLog" ){
		window.open( "http://csp.cyworld.com/bi/bi_recommend_pop.php?url=" + s_url + "&thumbnail=" + s_thumbnail + "&summary=" + text, "cLog", "width=380, height=380" );
	}
	
}

function getDocHeight() {
	var D = document;
	return Math.max(
			Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
			Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
			Math.max(D.body.clientHeight, D.documentElement.clientHeight)
	);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



// ## Prototype

//공백 함수
String.prototype.replaceAll = function(regex, replacement) {   
    return this.split(regex).join(replacement);   
}  

String.prototype.trim = function(){
	return this.replace(/(^\s*)|(\s*$)/g,"");
}

String.prototype.getNumber = function(){
	var pattern = /[^0-9]/g;	
	return this.replace(pattern,'');
}

String.prototype.pad_left = function( c, n ){
	var repeat = "";	
	if( this.length >= n ) return this;	
	for( var i= this.length; i<n; i++ ) repeat += c;
	
	return (repeat + this);
}

String.prototype.comma = function() { 

	var l_text=this; 
	var l_pattern=/^(-?\d+)(\d{3})($|\..*$)/; 

	if(l_pattern.test(l_text)){ 
		l_text=l_text.replace(l_pattern,function(str,p1,p2,p3) { 
			return p1.comma() + ("," + p2 + p3); 
		}); 
	}

	return l_text; 

}

Array.prototype.indexOf = function(item, from){
	var len = this.length;
	for (var i = (from < 0) ? Math.max(0, len + from) : from || 0; i < len; i++){
		if (this[i] === item) return i;
	}
	return -1;
}

Number.prototype.round = function(precision){
	precision = Math.pow(10, precision || 0);
	return Math.round(this * precision) / precision;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



// ## DOM

function addOption( obj, txt, value ){
	
	try{			 //IE는 두번째 인자값을 넣어주면 예외가 발생한다. (비표준)
			
		obj.add( new Option(txt, value) );			
		
	}catch(e){ //IE 외 모든 브라우저 (표준)
		
		obj.add( new Option(txt, value), null );
		
	}
	
}

function getChildNodes( obj ){
	
	var c = obj.childNodes;
	var arr = [];
	
	for( var i=0, j=c.length; i<j; i++ ) {
		if( c[i].nodeType == 1 ) {
			arr.push( c[i] );
		}
	}
	
	return arr;
	
}

function removeElement( node ){

	node.parentNode.removeChild( node );
	
}

function inputToArr( obj ){
	
	var arr = [];
	for( var i = 0, j = obj.length; i<j; i++ ){
		
		arr.unshift( obj[i].value );
		
	}
	
	return arr;
	
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



// ## Table

function getTable( obj ){
	
	var t_obj = obj;
	while( t_obj.nodeName != "TABLE" ){
		
		t_obj = t_obj.parentNode;
		
	}
	
	return t_obj;
	
}

function getTr( obj ){
	
	var t_obj = obj;
	while( t_obj.nodeName != "TR" ){
		
		t_obj = t_obj.parentNode;
		
	}
	
	return t_obj;
	
}

function getTd( obj ){
	
	var t_obj = obj;
	while( t_obj.nodeName != "TD" ){
		
		t_obj = t_obj.parentNode;
		
	}
	
	return t_obj;
	
}

function insertTr( table ){
	
	return table.insertRow( table.rows.length );
	
}

function moveTr( tr, check ){
	
	var rowIndex = tr.rowIndex-1;
	var table = getTable( tr );
	
	var tableBody = getChildNodes( table )[2];
	var tableChild = getChildNodes( tableBody );
	
	if( check == "up" && rowIndex != 0 ){
			
		tableBody.insertBefore( tableChild[rowIndex], tableChild[rowIndex-1] );
			
	}else if( check == "down" && rowIndex != (tableChild.length-1) ){
			
		tableBody.insertBefore( tableChild[rowIndex+1], tableChild[rowIndex] );
		
	}
	
}

function copyTr( table, copy_tr ){
	
	var new_tr = table.insertRow( table.rows.length );
	
	for( var i=0, j=copy_tr.cells.length; i<j; i++ ){
		
		var td = new_tr.insertCell(i);
		td.innerHTML = copy_tr.cells[i].innerHTML;
		
	}
	
	return new_tr;
	
}

//not_arr, 복사되지 않을 TD의 순번을 배열로 전달
function replaceTr( old_tr, new_tr, not_arr ){
	
	for( var i=0, j=old_tr.cells.length; i<j; i++ ){
		
		if( not_arr.indexOf( i ) == -1 ){
			new_tr.cells[i].innerHTML = old_tr.cells[i].innerHTML;
		}
		
	}
	
	return new_tr;
	
}

function displayEmptyTR( table_id, empty_td_id, colSpan, txt, parent_yn, callBack ){
	
	var table = null;
	if( parent_yn == "Y" ) table = parent.$id( table_id );
	else table = $id( table_id );
	
	var cnt = table.rows.length;
	
	var empty_td = null;
	if( parent_yn == "Y" ) empty_td = parent.$id( empty_td_id );
	else empty_td = $id( empty_td_id );
	
	if( empty_td ){
		
		if( cnt > 2 ){
			
			var rowIndex = getTr( empty_td ).rowIndex;
			table.deleteRow( rowIndex );
			
		}
		
	} else {
		
		if( cnt == 1 ){
			
			var tr = insertTr( table );
			var td1 = tr.insertCell(0);
			td1.id = empty_td_id;
			td1.colSpan = colSpan;
			td1.innerHTML = txt;
			
		}
		
	}
	
	if( callBack ) callBack();
	
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



// ## Goods Common

function copy_lotte( source ){
	
	clipboardData.setData('Text', source);
	alert( "롯데닷컴 주소창에 붙여넣기 해주세요." );
	
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



// ## Client Goods Common

function img_src_change( oThis, src ){
	
	if( oThis.getAttribute( "chk" ) == "Y" ) return;
	oThis.src = src;
	
}

function visible_on( id ){
	
	$id( id ).style.visibility = 'visible';
	
}

function visible_out( id ){
	
	$id( id ).style.visibility = 'hidden';
	
}

function list_pic_over( oThis ){
	
	var obj = getChildNodes( oThis.parentNode.parentNode );
	obj[1].style.display = '';
	
}

function list_pic_out( oThis ){
	
	var obj = getChildNodes( oThis.parentNode.parentNode );
	obj[1].style.display = 'none';
	
}

function fast_frame( oThis, gds_code ){
	
	var width = 710;
	var height = 400;
	var scroll = "no";
	var src = "../shop/shop_frame.aspx?gds_code=" + gds_code;
	
	//레이어가 이미 존재하면 삭제
	remove_fast_layer();
	
	var doc = ( parent ) ? parent.document : document;
	var win = ( parent ) ? parent.window : window;
	
	var top = $( doc ).scrollTop() + ( ( $( win ).height() / 2 ) - ( height/2 ) );
	var left = ( ( $( win ).width() / 2 ) - (width/2) );

	//Wrapper
	var wrapper = doc.createElement("div");
	wrapper.style.cssText = "width:" + width + "px; height:" + height + "px; position:absolute; top:" + top + "px; left:" + left + "px;";
	wrapper.id = "fast_layer";
	
	//Iframe
	var wrapper_frmae = doc.createElement("iframe");
	wrapper_frmae.style.cssText = "border:0; background-color:transparent;";
	wrapper_frmae.src = src;
	wrapper_frmae.frameBorder = 0;
	wrapper_frmae.width = width;
	wrapper_frmae.height = height;
	wrapper_frmae.marginWidth = 0;
	wrapper_frmae.marginHeight = 0;
	wrapper_frmae.scrolling = scroll;
	wrapper_frmae.allowTransparency = true;
	
	wrapper.appendChild( wrapper_frmae );

	doc.body.appendChild(wrapper);
			
}

function remove_fast_layer(){
	
	var layer = ( parent ) ? parent.$id("fast_layer") : $id("fast_layer");
	
	if( layer ){
		
		removeElement( layer );
		
	}
		
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
