	/* 용어 정리 													*/
	/* source :  드래그 앤 드랍을 할 수 있는 대상 요소 					*/
	/* taget  :  대상요소(source)를 드래그 앤 드랍할 수 있는 지정된 범위 	*/
	var expdate = new Date(); 
	expdate.setTime(expdate.getTime() + 1000 * 3600 * 24 * 1 * 7); //일주일간 쿠키저장
	var md = "fre";
	var tp_session_key = "";
	var target_day_height = 0;

	/* 쿠키 저장 */
	function setCookie2 (name, value, expires) {
		document.cookie = name + "=" + escape(value) + "; path=/; expires=" + expires.toGMTString();
	}

	/* 쿠키 가져오기 */
	function getCookie2(Name) {
		var search = Name + "="
	  	if (document.cookie.length > 0) { // 쿠키가 설정되어 있다면
	    	offset = document.cookie.indexOf(search)
	    	if (offset != -1) { // 쿠키가 존재하면
	      		offset += search.length
			    // set index of beginning of value

	      		end1 = document.cookie.indexOf(";", offset)
	      		// 쿠키 값의 마지막 위치 인덱스 번호 설정

	     		if (end1 == -1)
	        		end1 = document.cookie.length
	      	return unescape(document.cookie.substring(offset, end1))
	    	}
	  	}
	  	return "";
	}

	/* 여행정보에서 쿠키 저장하기 */
	/*function setTiCookie(contentid, content_nm, type) {
		var source_key = "";

		if(contentid == ""){
			alert("Erreur du compte de contenu");
			return false;
		}

		if(content_nm == ""){
			alert("Erreur du nom de contenu");
			return false;
		}

		if(type == "76"){
			source_key = "myplace";
		}else if(type == "78"){
			source_key = "myplace";
		}else if(type == "82"){
			source_key = "myrestaurant";
		}else if(type == "80"){
			source_key = "mystaying";
		}else if(type == "75"){
			source_key = "myleisure";
		}else if(type == "85"){
			source_key = "myevent";
		}else if(type == "79"){
			source_key = "myshopping";
		}else{
			source_key = "fail";
		}

		if(source_key == "fail"){
			alert("Ce n'est pas le genre de sites touristiques disponibles chez [Travel Planner]");
			return false;
		}else{
			source_key = md + "_" + source_key;
			var temp1 = 0;
			var tempcnt = eval(getCookie2(source_key + "cnt"));
			
			if(tempcnt>0){
				temp1 = tempcnt;
			}
			
			setCookie2(source_key + "cnt", temp1 +1, expdate);
			setCookie2(source_key + "|" + temp1, contentid + "|" + content_nm.replace("'","") + "|" +type, expdate);
			
			return true;
		}
	}*/

	/* 드래그앤드랍 타켓에 들어 있는 source들을 쿠키에 저장 */
	function setTargetSourceCookie(target, source_key) {
		source_key = md + "_" + source_key;
		var i=-1;
		var ii=-1;
		var mm = target.childNodes;
		if (target.hasChildNodes()) {
			for (i=0; i<mm.length; i++) {
				if(mm[i].nodeType==1){
					ii++;
					source_temp = mm[i].id.split("^");
					source_id = source_temp[0];
					source_value = source_temp[1];
					temp_str = source_value.split("|");
					setCookie2(source_key + "|" + ii, temp_str[0] + "|" + temp_str[1] + "|" + temp_str[2],expdate);
				}
			}
		}
		setCookie2(source_key+"cnt", eval(ii)+1, expdate);
		return mm.length;
	}
	
	/* 일정 타겟div의 height를 설정하기 위해 가장 많은 source가 들어 있는 타겟의 source 갯수를 구한다.*/
	function getTargetSourceCnt() {
		temp = 0;
		cnt = 0;
		for(var j=start_day; j< start_day+4; j++){
			temp = getIndividualTargetSourceCnt(document.getElementById('target_day'+ j));
			if(temp > cnt){
				cnt = temp;
			}
		}
		return cnt;
	}
	
	/* 타겟div의 height를 설정하기 위해 가장 많은 source가 들어 있는 타겟의 source 갯수를 구한다.*/
	function getIndividualTargetSourceCnt(target) {
		temp2 = 0;
		cnt2 = 0;
		var mm = target.childNodes;
		temp2 = mm.length;
		temp3 = 0;
		for(var i=0; i<temp2; i++){
			if(mm[i].nodeType==1){
				temp3++;
			}
		}
		
		if(temp3 > cnt2){
			cnt2 = temp3;
		}
		return cnt2;
	}
	
	/* 타겟 div의 height 설정 */
	function setTargetHeight(target, cnt) {
		if(cnt == 0)
			cnt = 1;
		var div_height=50;
		div_height = cnt * 50;
		target.style.height = div_height + "px";
	}
	
	/* 타겟 div의 height에서 -50을 한다. */
	function setTargetHeightMinus(target) {
		if(target.id.indexOf("target_center")==0){
			return;
		}
		var div_height=50;
		if(eval(target.style.height.split("p")[0]) > 50){
			target.style.height = eval(target.style.height.split("p")[0]) - div_height + "px";	
		}
	}
	
	/* 쿠키에 저장 되어 있는 컨텐츠를 target에 넣어 보여주고 드래그앤드랍이 가능하도록 source로 생성 */
	function getTargetSourceCookie(target,source_key) {
		source_key = md + "_" + source_key;
		var code = '';
		var code_nm = '';
		var type = '';
		for(var i=0; i<getCookie2(source_key + "cnt"); i++){
			var str = getCookie2(source_key + "|" + i).split("|");
			code = str[0];
			code_nm = str[1];
			type = str[2];
			
			var newitem = document.createElement("div");
			newitem.setAttribute("id", source_key + "_data_source_" + i + "^" + code + "|" +code_nm + "|" + type);
			newitem.className ="top_wrap";
			var html = '<div class="bottom"><span class="ulbl"><p><a href="#" onclick=javascript:goContent("'+ code + '","w_left")>' + code_nm + '</a></p></span></div>';
			newitem.innerHTML = html;
			
			var itemListNode = target;
			itemListNode.appendChild(newitem);
			dndMgr.addDragSource(new ajax.dnd.DragSource(source_key + "_data_source_" + i + "^" + code + "|" + code_nm + "|" + type, code, code_nm, type));
		}
	}

	/* 나의 여행 만들기 선택한 컨텐츠들의 contentid, content_nm, tour_day, tour_planng_no를  */
	/* DB에 저장 할 수 있도록  input hidden 객체에 저장 한다.                                */
	function setSourceHidden(e_contentid, e_content_nm, e_tour_day, e_tour_plang_no) {
		var source_key = md + "_day";
		var temp = '';
		var str = '';
		var contentid = '';
		var content_nm = '';
		var day = '';
		var day_seq = '';
		var temp_seq = 0;
		for(var i=1; i<33; i++){
			temp = getCookie2(source_key + i + "cnt");
			if(temp > 0 ){
				for(var t=0; t<temp; t++){
					temp_seq++;
					var str = getCookie2(source_key + i + "|" + t).split("|");
				
					contentid += '|' + str[0];
					content_nm += '|' +  str[1];
					day += '|' + i;
					day_seq += '|' + temp_seq;
				}
				temp_seq = 0;
			}
		}
		e_contentid.value     = contentid;
		e_content_nm.value    = content_nm;
		e_tour_day.value      = day;
		e_tour_plang_no.value = day_seq;
	}
	
	//##############################################################3
	/* 기존에 저장된 session key를 리턴한다. */
	function getExistSessionKey(){
		tp_session_key = getCookie2(md + "_tp_session_user_key");
		return tp_session_key;
	}
	
	/* session key를 새로 생성하여 쿠키에 저장한다. */
	function setSessionKey(tp_session_key){
		setCookie2(md + "_tp_session_user_key", tp_session_key, expdate);
	}
	
	/* DB에 저장 되어 있는 컨텐츠를 target에 넣어 보여주고 드래그앤드랍이 가능하도록 source로 생성 */
	function loadContentList(tp_session_key, target_key, source_key){
		source_key = md + "_" + source_key;
		new ajax.xhr.Request("/tp.kto?func_name=sourceList&tpDTO.session_key=" + tp_session_key + "&tpDTO.md=" + md + "&tpDTO.target_key=" + target_key + "&tpDTO.source_key=" + source_key ,"",loadContentResult, 'GET');
	}

	function loadContentResult(req){
		var source_key = "";
		var target_key = "";
		if (req.readyState == 4) {
			if (req.status == 200) {
				var xmlDoc = req.responseXML;
				var code = xmlDoc.getElementsByTagName('code').item(0).firstChild.nodeValue;
				source_key = xmlDoc.getElementsByTagName('source_key').item(0).firstChild.nodeValue + "_data_source_";
				target_key = xmlDoc.getElementsByTagName('target_key').item(0).firstChild.nodeValue;
				if (code == 'success') {
					var locationList = eval("(" +
					    xmlDoc.getElementsByTagName('data').item(0).firstChild.nodeValue + ")");
					    
					var i = 0;
					if(locationList.length > 0){
						var listDiv = "";
						
						for (i = 0 ; i < locationList.length ; i++) {
							target_key = locationList[i].target_key;
							listDiv = document.getElementById(target_key);
							var locationDiv = makeContentView(locationList[i], i, source_key);
							listDiv.appendChild(locationDiv);
							dndMgr.addDragSource(new ajax.dnd.DragSource(source_key + i + "^" + locationList[i].contentid + "|" + locationList[i].title + "|" + locationList[i].contenttypeid, locationList[i].contentid, locationList[i].title, locationList[i].contenttypeid, locationList[i].sn, locationList[i].target_key));
						}
						
					}
					if(target_key.indexOf("target_day")==0){
						if(target_day_height <= i){
							target_day_height = i;
							// 나의 여행만들기 타겟의 height를 설정
							for(var j=start_day; j< start_day+4; j++){
								setTargetHeight(document.getElementById("target_day" + j), target_day_height);
							}
						}
					}else{
						setTargetHeight(document.getElementById(target_key), i);
					}
				} else if (code == 'error') {
					var message = xmlDoc.getElementsByTagName('message').item(0).firstChild.nodeValue;
					alert("에러 발생:"+message);
				}
			} else {
				alert("컨텐츠 목록 로딩 실패:"+req.status);
			}
		}
	}

	function makeContentView(location, i, source_key) {
		var locationDiv = document.createElement('div');
		locationDiv.setAttribute('id', source_key + i + "^" + location.contentid + "|" + location.title + "|" + location.contenttypeid);
		locationDiv.className ="top_wrap";
		var div_id = source_key + i + "^" + location.contentid + "|" + location.title + "|" + location.contenttypeid;
		var html = "<div class=\"bottom\"><span class=\"ulbl\"><p><a href=\"#\" onclick=\"javascript:goContent('"+ location.contentid + "', '" + div_id + "')\">" + location.title + "</a></p></span></div>";
		locationDiv.innerHTML = html;
		
		locationDiv.location = location;
		return locationDiv;
	}
	
	/* 여행정보에서 쿠키 저장하기 */
	function setTiDB(contentid, content_nm, type) {
		var source_key = "";
		var target_key = "";
		
		if(contentid == ""){
			alert("Erreur du compte de contenu");
			return false;
		}

		if(content_nm == ""){
			alert("Erreur du nom de contenu");
			return false;
		}
		
		if(type == "76"){
			source_key = "myplace";
			target_key = "target_tour_place";
		}else if(type == "78"){
			source_key = "myplace";
			target_key = "target_tour_place";
		}else if(type == "82"){
			source_key = "myrestaurant";
			target_key = "target_restaurant";
		}else if(type == "80"){
			source_key = "mystaying";
			target_key = "target_staying";
		}else if(type == "75"){
			source_key = "myleisure";
			target_key = "target_leisure";
		}else if(type == "85"){
			source_key = "myevent";
			target_key = "target_event";
		}else if(type == "79"){
			source_key = "myshopping";
			target_key = "target_shopping";
		}else{
			source_key = "fail";
		}
		
		if(source_key == "fail"){
			alert("Ce n'est pas le genre de sites touristiques disponibles chez [Travel Planner]");
			return false;
		}else{
			source_key = md + "_" + source_key;
			new ajax.xhr.Request("/tp.kto?func_name=sourceAdd&tpDTO.session_key=" + getExistSessionKey() + "&tpDTO.md=" + md + "&tpDTO.target_key=" + target_key + "&tpDTO.contentid=" + contentid + "&tpDTO.content_nm=" + encodeURIComponent(content_nm) + "&tpDTO.contenttypeid=" + type ,"",addContentResult, 'GET');
			return true;
		}
	}
	
	// source를 이동했을 경우 처리
	function moveSource(dataSource, dropTarget){
		//위치 및 주변 정보에서 이동 했을 경우
		if(dataSource.target_key.indexOf("target_center")==0){
			new ajax.xhr.Request("/tp.kto?func_name=sourceAdd&tpDTO.session_key=" + getExistSessionKey() + "&tpDTO.md=" + md + "&tpDTO.target_key_str=" + dropTarget.element.id + "&tpDTO.target_key_old=" + dataSource.target_key + "&tpDTO.contentid_str=" + dataSource.code + "&tpDTO.content_nm_str=" + encodeURIComponent(dataSource.code_nm) + "&tpDTO.contenttypeid_str=" + dataSource.contenttype ,"",moveContentResult, 'GET', dataSource);
		}else if(dropTarget.element.id=="target_wastebasket"){ //휴지통으로 이동했을경우 처리
			new ajax.xhr.Request("/tp.kto?func_name=sourceDelete&tpDTO.session_key=" + getExistSessionKey() + "&tpDTO.md=" + md + "&tpDTO.target_key=" + dropTarget.element.id + "&tpDTO.target_key_old=" + dataSource.target_key + "&tpDTO.sn=" + dataSource.sn + "&tpDTO.contentid=" + dataSource.code + "&tpDTO.content_nm=" + encodeURIComponent(dataSource.code_nm) + "&tpDTO.contenttypeid=" + dataSource.contenttype ,"",moveContentResult, 'GET', dataSource);
		}else{
			new ajax.xhr.Request("/tp.kto?func_name=sourceMove&tpDTO.session_key=" + getExistSessionKey() + "&tpDTO.md=" + md + "&tpDTO.target_key=" + dropTarget.element.id + "&tpDTO.target_key_old=" + dataSource.target_key + "&tpDTO.sn=" + dataSource.sn + "&tpDTO.contentid=" + dataSource.code + "&tpDTO.content_nm=" + encodeURIComponent(dataSource.code_nm) + "&tpDTO.contenttypeid=" + dataSource.contenttype ,"",moveContentResult, 'GET', dataSource);
		}
	}
	
	/* 관광정보를 내가 수집한 정보에 DB 저장하기 */
	function setTiCookie(a_contentid, a_content_nm, a_type) {
		//이미 저장된 session_key 쿠키가 있는지 확인
		if(!getExistSessionKey()){
			//session key가 존재 하지 않는다면 sesstion key를 생성한다.
			var params = "tpDTO.contentid=" + a_contentid + "&" +
						 "tpDTO.content_nm=" + encodeURIComponent(a_content_nm) + "&" +
						 "tpDTO.contenttypeid=" + a_type;
			new ajax.xhr.Request("/tp.kto?func_name=getSessionID&tpDTO.md=" + md , params, sessionResult, 'POST');
		}else{
			var source_key = "";
			var target_key = "";
			var contentid = "";
			var content_nm = "";
			var type = "";
			var temp_id = "";
			var temp_nm = "";
			var temp_type = "";
			var len = 0;
			if(a_contentid[0]==undefined){
				len = 1;
			}else{
				/* 2011-05-31 ie 8.0, ff에서 오류 발생하므로 아래 if문 추가*/
				if(a_contentid[0].value==undefined){
					len = 1;
				} else {
					len = a_contentid.length;
				}
			}
			
			for(var i=0; i<len; i++){
				if(len == 1){
					temp_id = a_contentid;
					temp_nm = a_content_nm;
					temp_type = a_type;		
				}else if(len > 1){
					temp_id = a_contentid[i];
					temp_nm = a_content_nm[i];
					temp_type = a_type[i];
				}
				
				if(temp_id == ""){
					alert("Erreur du compte de contenu");
					return false;
				}
		
				if(temp_nm == ""){
					alert("Erreur du nom de contenu");
					return false;
				}
				
				if(temp_type == "76"){
					target_key += "target_tour_place^";
				}else if(temp_type == "78"){
					target_key += "target_tour_place^";
				}else if(temp_type == "82"){
					target_key += "target_restaurant^";
				}else if(temp_type == "80"){
					target_key += "target_staying^";
				}else if(temp_type == "75"){
					target_key += "target_leisure^";
				}else if(temp_type == "85"){
					target_key += "target_event^";
				}else if(temp_type == "79"){
					target_key += "target_shopping^";
				}else{
					source_key = "fail";
				}
				
				if(source_key == "fail"){
					alert("Ce n'est pas le genre de sites touristiques disponibles chez [Travel Planner]");
					return false;
				}
				contentid  += temp_id + "^";
				content_nm += temp_nm + "^";
				type       += temp_type + "^";
			}
			source_key = md + "_" + source_key;
			new ajax.xhr.Request("/tp.kto?func_name=sourceAdd&tpDTO.session_key=" + getExistSessionKey() + "&tpDTO.md=" + md + "&tpDTO.target_key_str=" + target_key + "&tpDTO.contentid_str=" + contentid + "&tpDTO.content_nm_str=" + encodeURIComponent(content_nm) + "&tpDTO.contenttypeid_str=" + type ,"",addContentResultPop, 'GET');
			if(a_contentid[0]!=undefined){
				opener.goRefresh();
			}else{
				return true;
			}
		}
	}
	
	function addContentResult(req){
		var source_key = "";
		if (req.readyState == 4) {
			if (req.status == 200) {
				var xmlDoc = req.responseXML;
				var code = xmlDoc.getElementsByTagName('code').item(0).firstChild.nodeValue;
				if (code == 'success') {
					return true;
				} else if (code == 'error') {
					var message = xmlDoc.getElementsByTagName('message').item(0).firstChild.nodeValue;
					return false;
					//alert("에러 발생:"+message);
				}
			} else {
				return false;
				//alert("댓글 목록 로딩 실패:"+req.status);
			}
		}
	}
	
	function addContentResultPop(req){
		if (req.readyState == 4) {
			if (req.status == 200) {
				var xmlDoc = req.responseXML;
				var code = xmlDoc.getElementsByTagName('code').item(0).firstChild.nodeValue;
				if (code == 'success') {
					alert("Information enregistrée");
				} else if (code == 'error') {
					var message = xmlDoc.getElementsByTagName('message').item(0).firstChild.nodeValue;
					return false;
				}
			} else {
				return false;
				//alert("댓글 목록 로딩 실패:"+req.status);
			}
		}
	}
	
	function sessionResult(req){
		var source_key = "";
		var target_key = "";
		var contentid = "";
		var content_nm = "";
		var type = "";
		var temp_id = "";
		var temp_nm = "";
		var temp_type = "";
		
		if (req.readyState == 4) {
			if (req.status == 200) {
				var xmlDoc = req.responseXML;
				var code = xmlDoc.getElementsByTagName('code').item(0).firstChild.nodeValue;
				if (code == 'success') {
					var locationList = eval("(" +
					    xmlDoc.getElementsByTagName('data').item(0).firstChild.nodeValue + ")");
					    
					var i = 0;
					if(locationList.length > 0){
						for (i = 0 ; i < locationList.length ; i++) {
							session_key = locationList[i].session_key;
							temp_id = locationList[i].contentid;
							temp_nm = locationList[i].content_nm;
							temp_type = locationList[i].contenttypeid;
							setSessionKey(session_key);
						}
						
													
						if(temp_id == ""){
							alert("Erreur du compte de contenu");
							return false;
						}
				
						if(temp_nm == ""){
							alert("Erreur du nom de contenu");
							return false;
						}
						
						if(temp_type == "76"){
							target_key += "target_tour_place^";
						}else if(temp_type == "78"){
							target_key += "target_tour_place^";
						}else if(temp_type == "82"){
							target_key += "target_restaurant^";
						}else if(temp_type == "80"){
							target_key += "target_staying^";
						}else if(temp_type == "75"){
							target_key += "target_leisure^";
						}else if(temp_type == "85"){
							target_key += "target_event^";
						}else if(temp_type == "79"){
							target_key += "target_shopping^";
						}else{
							source_key = "fail";
						}
						
						if(source_key == "fail"){
							alert("Ce n'est pas le genre de sites touristiques disponibles chez [Travel Planner]");
							return false;
						}
						contentid  += temp_id + "^";
						content_nm += temp_nm + "^";
						type       += temp_type + "^";
						
						source_key = md + "_" + source_key;
						new ajax.xhr.Request("/tp.kto?func_name=sourceAdd&tpDTO.session_key=" + getExistSessionKey() + "&tpDTO.md=" + md + "&tpDTO.target_key_str=" + target_key + "&tpDTO.contentid_str=" + contentid + "&tpDTO.content_nm_str=" + encodeURIComponent(content_nm) + "&tpDTO.contenttypeid_str=" + type ,"",addContentResultPop, 'GET');
					}
				} else if (code == 'error') {
					var message = xmlDoc.getElementsByTagName('message').item(0).firstChild.nodeValue;
					alert("에러 발생:"+message);
				}
			} else {
				alert("컨텐츠 목록 로딩 실패:"+req.status);
			}
		}
	}
	
	function moveContentResult(req, dataSource){
		var sn = "";
		var target_key = "";
		var target_key_old = "";
		var source_cnt = 0;
		if (req.readyState == 4) {
			if (req.status == 200) {
				var xmlDoc = req.responseXML;
				var code = xmlDoc.getElementsByTagName('code').item(0).firstChild.nodeValue;
				target_key = xmlDoc.getElementsByTagName('target_key').item(0).firstChild.nodeValue;
				target_key_old = xmlDoc.getElementsByTagName('target_key_old').item(0).firstChild.nodeValue;
				sn = xmlDoc.getElementsByTagName('maxsn').item(0).firstChild.nodeValue;
				source_cnt = xmlDoc.getElementsByTagName('source_cnt').item(0).firstChild.nodeValue;
				if (code == 'success') {
					//휴지통에 버렸을 경우 처리
					if(target_key=="target_wastebasket"){
						document.getElementById(target_key).innerHTML = "";
						setTargetHeightMinus(document.getElementById(target_key_old));
					}else{
						//source에 새로 옮겨진 target과 새로운 sn을 set한다.
						dataSource.target_key = target_key;
						dataSource.sn = sn;
						setTargetHeight(document.getElementById(target_key), source_cnt);
						//source를 자기 자리에서 옮긴경우는 height를 minus하지 않는다.
						if(target_key != target_key_old)
							setTargetHeightMinus(document.getElementById(target_key_old));
					}
					if(target_key.indexOf("target_day")==0 || target_key_old.indexOf("target_day")==0){
						//나의 여행 만들기는 가장많은 source가 들어 있는 타겟의 겟수로 height를 잡는다.
						//타겟div의 height를 설정하기 위해 가장 많은 source가 들어 있는 타겟의 source 갯수를 구한다.
						var target_source_cnt = getTargetSourceCnt();
				
						// 나의 여행만들기 타겟의 height를 설정
						for(var j=start_day; j< start_day+4; j++){
							setTargetHeight(document.getElementById("target_day" + j), target_source_cnt);
						}
					}
					
				} else if (code == 'error') {
					var message = xmlDoc.getElementsByTagName('message').item(0).firstChild.nodeValue;
					alert("에러 발생:"+message);
				}
			} else {
				alert("컨텐츠 목록 로딩 실패:"+req.status);
			}
		}
	}
