/**
 * Copyright 2007 SK Communications. All rights reserved
 * @since 07.11.06
 * @author umkki
 * @author jingikim 2008-11-30
 * 
 * 상품 신청 다이얼로그를 정의합니다. 
 */
if(typeof(Modal) == 'undefined') {
//	alert("You must first include Modal.js");
}

Object.extend(Modal, {
	setFocus: function(element) {
		if(Modal.current && Modal.current.setFocus) {
			Modal.current.setFocus(element);
		}
	}, 
	
	modify: function(element) {
		if (Modal.current && Modal.current.modify) {
			Modal.current.modify(element);
		}
	},
	
	blurElement: function(element) {
		if (Modal.current && Modal.current.blurElement) {
			Modal.current.blurElement(element);
		}
	},
	
	execute: function(){
		if(Modal.current && Modal.current.execute) {
			Modal.current.execute();
		}
	},
	
	openZip: function() {
		Popup.openWindow('/review/zipsearch.php', 'ZipSearch', 200, 200, true, true, true);
	}
});

Modal.ApplicationDlg = Class.create();
Object.extend(Modal.ApplicationDlg.prototype, {
	isLoaded: false,
	isFocused: false,
	
	itemid: null,
	nowModifing: "username",
	
	MSG_NOTICE: '신청자 한마디를 잘 적어주시면 당첨 확률이 높아집니다. 그리고 작성하신 신청자 한마디는 각 상품 페이지 하단에 보여집니다. (최대 400자)',
	MSG_NO_NAME: '이름을 입력해 주세요.',
	MSG_NO_EMAIL: '이메일을 입력해 주세요.',
	MSG_NO_PHONE: '전화번호를 입력해 주세요.',
	MSG_NO_ZIPCODE: '배송지 우편번호를 입력해 주세요.',
	MSG_NO_ADDRESS: '배송지 주소를 입력해 주세요.',
	MSG_NO_REASON: '신청자 한마디를 입력해 주세요.',
	MSG_OVERSIZED: '신청자 한마디는 400자 이하로 써 주세요.',
	MSG_NO_CHECKED: '개인정보 이용에 동의해 주세요.',
	
	initialize: function(_dialogID, _params) {
		this.dialogID = _dialogID;
		this.dialog = $(this.dialogID);
		if (_params) {
			this.itemid = _params.itemid;
		}
		this.isLoaded = Modal.addDialog(this);
	}, 
	
	open: function(params) {
		var form = document.ApplicationForm;
		if (typeof form != "undefined") {
			this.itemid = form.itemid.value;
		}
		
		new Ajax.Request('/exec/review/get_user_info.php', {
			parameters: {
				'itemid': this.itemid
			},
			onSuccess: function(transport) {
				var text = transport.responseText;
				if (text.isJSON()) {
					var result = text.evalJSON();
					if (result.code == 1) {
						Modal.center(this.dialog);
				
						if (form) {
							var textarea = form.message;
							textarea.value = this.MSG_NOTICE;
							textarea.style.color = "#888";
						}
						this.dialog.show();
					}
					else if (result.code == 2){
						var href = window.location.href;
						window.location.href = 'http://www.egloos.com/login.php?returnurl='+encodeURIComponent(href);
					}
					else {
						alert(result.message);
					}
				}
			}.bind(this),
			onFailure: function() {
				
			}
		});
	},
	
	close: function() {
		this.isFocused = false;
		this.dialog.hide();
	},
	
	visible: function() {
		return this.dialog.visible();
	},
	
	setFocus: function(element) {
		if (this.nowModifing) {
			this.finish(this.nowModifing);
		}
		if (this.isFocused == false)  {
			this.isFocused = true;
			var textarea = document.ApplicationForm.message;
			textarea.value = '';
			textarea.style.color = '#333333';
		}
	}, 
	
	modify: function(element) {
		var btnName = element.name;
		var postfix = btnName.replace('btn_', '');
		
		if (this.nowModifing) {
			this.finish(this.nowModifing);
		}
		
		this.nowModifing = postfix;
		
		if (postfix == 'address') {
			this.modifyAddress(postfix);
		}
		else {
			var txtElm = $('txt_'+postfix);
			var modifyElm = $('modify_'+postfix);
			
			txtElm.style.display = 'none';
			modifyElm.style.display = '';
			modifyElm.getElementsByTagName('INPUT')[0].focus();
		}
	},
	
	modifyAddress: function(postfix) {
		$('txt_zipcode').style.display = 'none';
		$('txt_address').style.display = 'none';
		var modifyZipcodeElm = $('modify_zipcode');
		var modifyAddressElm = $('modify_address');
		modifyZipcodeElm.style.display = '';
		modifyAddressElm.style.display = '';
		modifyAddressElm.getElementsByTagName('INPUT')[0].focus();
	},
	
	finishAddress: function(postfix) {
		$('modify_zipcode').style.display = 'none';
		$('modify_address').style.display = 'none';
		
		var txtZipcodeElm = $('txt_zipcode');
		var txtAddressElm = $('txt_address');
		
		var form = document.ApplicationForm;
		var span = txtZipcodeElm.getElementsByTagName('SPAN')[0];
		span.innerHTML = form.zipcode.value;
		var span = txtAddressElm.getElementsByTagName('SPAN')[0];
		span.innerHTML = form.address.value;
		txtZipcodeElm.style.display = '';
		txtAddressElm.style.display = '';
	},
	
	finish: function(postfix) {
		if (postfix == 'address' || postfix == 'zipcode') {
			this.finishAddress(postfix);
		}
		else {
			var txtElm = $('txt_'+postfix);
			var element = document.ApplicationForm[postfix];
			var span = txtElm.getElementsByTagName('SPAN')[0];
			span.innerHTML = element.value;
			var modifyElm = $('modify_'+postfix);
			
			modifyElm.style.display = 'none';
			txtElm.style.display = '';
		}
		this.nowModifing = null;
	},
	
	blurElement: function(element){
		var postfix = element.name;
		this.finish(postfix);
	},
	
	hasValue: function(formElm, msg) {
		if(formElm.value.blank()) {
			return this._handleFailure(formElm, msg);
		} else {
			return true;			
		}
	},

    isChecked: function(formElm, msg) {
		if(!formElm.checked) {
            formElm.focus();
			return this._handleFailure(formElm, msg);
		} else {
			return true;			
		}
    },
	
	isValidContents: function(textarea) {
		var str = textarea.value;
		if(str == this.MSG_NOTICE) {
			return this._handleFailure(textarea, this.MSG_NO_REASON);
		} else if(str.length > 400) {
			return this._handleFailure(textarea, this.MSG_OVERSIZED);
		} else {
			return true;			
		}
	},
	
	_handleFailure: function(formElm, msg) {
		alert(msg);
		return false;
	},
	
	execute: function() {
		var form = document.ApplicationForm;
		if(this.hasValue(form.email, this.MSG_NO_EMAIL)
				&& this.hasValue(form.username, this.MSG_NO_NAME)
				&& this.hasValue(form.phone, this.MSG_NO_PHONE)
				&& this.hasValue(form.zipcode, this.MSG_NO_ZIPCODE)
				&& this.hasValue(form.address, this.MSG_NO_ADDRESS)
				&& this.hasValue(form.message, this.MSG_NO_REASON)
				&& this.isChecked(form.agree, this.MSG_NO_CHECKED)
				&& this.isValidContents(form.message)) {
			
			new Ajax.Request('/exec/review/application_exec.php', {
				parameters: {
					itemid		: form.itemid.value,
					username	: form.username.value,
					email		: form.email.value,
					phone		: form.phone.value,
					zipcode		: form.zipcode.value,
					address		: form.address.value,
					message		: form.message.value
				},
				onSuccess: function(response) {
					var text = response.responseText;
					if (text.isJSON()) {
						var channel = text.evalJSON();
						if (channel.result == '1') {
                            Modal.close();
						}
						else {
							alert(channel.message);
						}
					}

				}.bind(this)
			});			
		}
	}
});

Modal.EventDlg = Class.create();
Object.extend(Modal.EventDlg.prototype, {
	initialize: function(_dialogID) {
		this.dialogID = _dialogID;
		this.dialog = $(this.dialogID);
		this.isLoaded = Modal.addDialog(this);
	}, 
	
	open: function(params) {
		Modal.center(this.dialog);
		this.dialog.show();
	},
	
	close: function() {
		this.dialog.hide();
	},
	
	visible: function() {
		return this.dialog.visible();
	}
});
