/**
 * Copyright 2008 SK Communications. All rights reserved
 * @since 08.07.11
 * @author umkki
 * 
 * 외부 블로거 상품 신청 다이얼로그를 정의합니다. 
 */
if(typeof(Modal) == 'undefined') {
//	alert("You must first include Modal.js");
}

Object.extend(Modal, {
	executeExt: function() {
		if(Modal.current && Modal.current.executeExt) {
			Modal.current.executeExt();
		}
	},
	
	end: function() {
		if(Modal.current && Modal.current.end) {
			Modal.current.end();
		}
	}
	
});

Modal.ApplicationExtDlg = Class.create();
Object.extend(Modal.ApplicationExtDlg.prototype, {
	isLoaded: false,
	isFocused: false,
	isFinished: false,
	
	itemid: null,
	
	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.ApplicationForm2;
		if (form) {
			var textarea = form.contents;
			textarea.value = "";
			if (this.itemid == null) {
				this.itemid = form.item_id.value;
			}
		}
		
		Modal.center(this.dialog);
		this.dialog.show();
	},
	
	close: function() {
		this.isFinished = false;
		if (this.innerHTML) {
			this.dialog.innerHTML = this.innerHTML;
		}
		this.isFocused = false;
		this.dialog.hide();
	},
	
	visible: function() {
		return this.dialog.visible();
	},
	
	setFocus: function(element) {
		if (this.isFocused == false)  {
			this.isFocused = true;
			var textarea = document.ApplicationForm2.contents;
			textarea.value = '';
			textarea.style.color = '#333333';
		}
	}, 

	executeExt: function() {
		var form = document.ApplicationForm2;
		
        var email	= document.getElementById('o_email');
        var blogurl	= document.getElementById('o_blog_url');
        var blog	= document.getElementById('select_blog');        
        var o_contents	= document.getElementById('o_contents');
        var contents = o_contents.value;
        var itemid	= document.getElementById('o_item_id');
        var username	= document.getElementById('o_name');
        var phone1	= document.getElementById('o_phone1');
        var phone2	= document.getElementById('o_phone2');
        var phone3	= document.getElementById('o_phone3');
        var phone	= phone1.value+"-"+phone2.value+"-"+phone3.value;
        var address	= document.getElementById('o_address');
        var zipcode	= document.getElementById('o_zipcode');
        var agree1	= document.getElementById('o_agree1');
        var agree2	= document.getElementById('o_agree2');

        
        if(contents == "") { 
            alert('신청자 한마디를 입력해 주세요.');
            o_contents.focus();            
            return;
        }

        if(contents.length > 400) { 
            alert('신청자 한마디는 400자 이하로 써 주세요.');
            o_contents.focus();            
            return;
        }
        
        if(email.value == "") { 
            alert('이메일 주소를 입력해 주세요.');
            email.focus();
            return;
        }        
        
        if(blogurl.value == "") { 
            alert('블로그 주소를 입력해 주세요.');
            blogurl.focus();            
            return;
        }
        
        if(escape(encodeURIComponent(username.value)) == "") { 
            alert('신청자의 이름을 입력해 주세요.');
            username.focus();
            return;
        }
        
        if(phone == "--") { 
            alert('전화번호를 입력해 주세요.');
            phone1.focus();        
            return;
        }                     
        
        if(escape(encodeURIComponent(address.value)) == "") { 
            alert('배송주소를 입력해 주세요.');
            document.getElementById('o_address').focus();            
            return;
        }       

        if(agree1.checked == false) { 
            alert('개인정보 수집/이용에 동의해 주세요.');
            agree1.focus();            
            return;
        }

        if(agree2.checked == false) { 
            alert('개인정보 제3자 제공에 동의해 주세요.');
            agree2.focus();            
            return;
        }
        					
		new Ajax.Request('/exec/review/application_ext_exec.php', {
			parameters: {
				itemid		: itemid.value,
				username	: username.value,
				email		: email.value,
				phone		: phone,
				zipcode		: zipcode.value,
				address		: address.value,
				contents	: contents,
				blog		: blog.value,
				blogurl		: blogurl.value
			},
			onSuccess: function(response) {
				var text = response.responseText;
				if (text.isJSON()) {
					var channel = text.evalJSON();
					if(channel.message == "RSS ERR") {
						alert("RSS 정보를 읽어올 수 없습니다.");
						blogurl.focus();
						return false;
					}
					else if(channel.message == "DUP ERR") {
						alert("이미 신청 하셨습니다.");
						window.location.reload();
						return false;
					}
					this.isFinished = true;
					this.saveInnerHTML();
					this.dialog.innerHTML = channel.innerHTML;
					
					this.eventHTML = channel.eventHTML;
				}
			}.bind(this)
		});			
	}, 
	
	getValue: function(item) {
		return (item.childNodes.length > 0 ? item.childNodes[0].nodeValue : '');
	},
	
	end: function() {
		Modal.close();
	},
	
	saveInnerHTML: function() {
		this.innerHTML = this.dialog.innerHTML;
	}
});
