/**
 * Copyright 2007 SK Communications. All rights reserved
 * @since 07.11.05
 * @author okjungsoo
 * 
 * 상품 후원 다이얼로그를 정의합니다. 
 */
if(typeof(Modal) == 'undefined') {
//	alert("You need Modal.RecommendDlg.js !!");
}

Object.extend(Modal, {
	setFocus: function(element) {
		if(Modal.current && Modal.current.setFocus) {
			Modal.current.setFocus(element);
		}
	}, 
	
	execute: function(){
		if(Modal.current && Modal.current.execute) {
			Modal.current.execute();
		}
	}
});


Modal.SupportDlg = Class.create();
Object.extend(Modal.SupportDlg.prototype, {
	isLoaded: false,
	isFocused: false, 
	
	MSG_NOTICE: '후원 상품에 대해 간단히 알려주세요. 빠른 시일 내에 연락 드리도록 하겠습니다. 감사합니다.',
		
	MSG_NO_NAME: '이름을 입력해 주세요.',
	MSG_NO_EMAIL: '이메일을 입력해 주세요.',
	MSG_NO_PHONE: '전화번호를 입력해 주세요.',
	MSG_NO_GOODS: '후원상품을 입력해 주세요.',
	MSG_NO_REASON: '후원사유를 입력해 주세요.',
	MSG_OVERSIZED: '후원사유는 200자 이하로 써 주세요.',
	
	initialize: function(_dialogID) {
		this.dialogID = _dialogID;
		this.dialog = $(this.dialogID);
		this.isLoaded = Modal.addDialog(this);
	}, 
	
	open: function(params) {
		Modal.center(this.dialog);

		var form = document.SupportForm;
		form.supporter_name.value = ""; 
		form.supporter_email.value = "";
		form.supporter_phone1.value = "";
		form.supporter_phone2.value = "";
		form.supporter_phone3.value = "";
		form.supporter_item.value = "";
		
		var textarea = form.supporter_message;
		textarea.value = this.MSG_NOTICE;
		textarea.style.color = "#888";
		
		this.dialog.show();
		form.supporter_name.focus();
	},
	
	close: function() {
		this.dialog.hide();
	},
	
	visible: function() {
		return this.dialog.visible();
	},
	
	setFocus: function(element) {
		if (this.isFocused == false)  {
			this.isFocused = true;

			var textarea = document.SupportForm.supporter_message;
			textarea.value = '';
			textarea.style.color = '#333333';
		}
	}, 
	
	hasValue: function(formElm, msg) {
		if(formElm.value.blank()) {
			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 > 200) {
			return this._handleFailure(textarea, this.MSG_OVERSIZED);
		} else {
			return true;			
		}
	},
	
	_handleFailure: function(formElm, msg) {
		alert(msg);
		formElm.focus();
		return false;
	},
	
	execute: function() {
		var form = document.SupportForm;
		if(this.hasValue(form.supporter_name, this.MSG_NO_NAME) 
				&& this.hasValue(form.supporter_email, this.MSG_NO_EMAIL)
				&& this.hasValue(form.supporter_phone1, this.MSG_NO_PHONE)
				&& this.hasValue(form.supporter_phone2, this.MSG_NO_PHONE)
				&& this.hasValue(form.supporter_phone3, this.MSG_NO_PHONE)
				&& this.hasValue(form.supporter_item, this.MSG_NO_GOODS)
				&& this.hasValue(form.supporter_message, this.MSG_NO_REASON)
				&& this.isValidContents(form.supporter_message)) {
			
			new Ajax.Request('/exec/review/support_exec.php', {
				parameters: $H({
					supporter_name : form.supporter_name.value,
					supporter_email : form.supporter_email.value,
					supporter_phone : form.supporter_phone1.value+'-'+form.supporter_phone2.value+'-'+form.supporter_phone3.value,
					supporter_item	 : form.supporter_item.value,
					supporter_message : form.supporter_message.value
				}),
				onSuccess: function(response) {
					var xml = response.responseXML;
					try {
						var result, message, item;
						var channel = xml.getElementsByTagName('channel')[0];
						
						for (var i=0; i<channel.childNodes.length; i++) {
							item = channel.childNodes[i];
							switch (item.tagName) {
								case 'result':
									result = this.getValue(item);
									break;
								case 'message':
									message = this.getValue(item);
									break;
							}
						}
						alert(message);
						if (result == '1') {
							Modal.close();
						}
					} catch (e) { /* alert(e); */}					
				}.bind(this)
			});			
		}
	}, 
	
	getValue: function(item) {
		return (item.childNodes.length > 0 ? item.childNodes[0].nodeValue : '');
	}
});