var DLG = {};

var DLGSUCC = {};

var bAlerting = false;
var nextActionFunc = null;
DLG.MyAlert = {
	start: function(msg, nWidth){
		var w = 413;
		
		if(nWidth != null)
			w = nWidth;
			
		var options = {
			url: "/dlg/template.aspx?type=alert&msg=" + encodeURIComponent(msg),
			params:"",
			title: "Openbay",
			width: w,
			onDestroy : this.onDestroy
		}		
		if( this.modal != undefined && this.modal != null) this.modal = null; this.modal = FMW.MyModal(options);		
	},
	show : function(msg,w)
	{
		if(bAlerting)
			return;
			
		bAlerting = true;
		this.start(msg,w);
	},	
	hide : function()
	{
		this.modal.destroy();delete this.modal;this.modal=null;
	},
	onDestroy : function()
	{
		bAlerting = false;
	}
};

function myAlert(msg,nextAction)
{
	if(nextAction == "undefined" || nextAction == null || nextAction == undefined)
		nextActionFunc = null;
	else	
		nextActionFunc = nextAction;

	DLG.MyAlert.show(msg);
}
function GetProfitableWidth()
{
	var w;
	w = Screen.getDocumentWidth() - 50;
	if(w > 415)
		w = 415;

	return w;
}

function myAlert2(msg,nextAction,w)
{
	if(nextAction == "undefined" || nextAction == null || nextAction == undefined)
		nextActionFunc = null;
	else	
		nextActionFunc = nextAction;

	DLG.MyAlert.show(msg,GetProfitableWidth());
}

function myAlertHide()
{
	DLG.MyAlert.hide();
	
	if(nextActionFunc)
		nextActionFunc();
}

DLG.MyConfirm = {
	start: function(msg, event){
		var w = 413;

		var options = {
			url: "/dlg/template.aspx?type=confirm&msg=" + encodeURIComponent(msg),
			params:"",
			title: "확인",
			width: w
		}			
		if( this.modal != undefined && this.modal != null) this.modal = null; this.modal = FMW.MyModal(options);
	},
	destroy : function(){
		this.modal.destroy();delete this.modal;this.modal=null;
	},
	show : function(msg,event)
	{
		this.start(msg,event);
	},	
	hide : function()
	{	
		this.modal.destroy();delete this.modal;this.modal=null;
	}
};

function myConfirm(msg,nextAction)
{
	nextActionFunc = nextAction;
	DLG.MyConfirm.show(msg);
}
function myConfirmAction()
{
	myConfirmHide();
	nextActionFunc();
}
function myConfirmHide()
{
	DLG.MyConfirm.destroy();
}

var VD = {};

var nextFunctionValue = null;

VD.utils = {
	
	chkSel : function(sel,msg)
	{		
	    if(sel.length == null || sel.length == "undefined")
	    {
	        if(sel.checked)
	        	return true;
	        
	        myAlert(msg);	        
	        return false;
	    }
	    else
	    {        
	        var i=0;
	        for(i=0;i<sel.length;i++)
	        {
	            if(sel[i].checked)
	                return true;
	        }
	        
	        myAlert(msg);
	        return false;
	    }
	},
		
	nextFunc : function()
	{
		if(!nextFunctionValue)
			return;
			
		try
		{
		nextFunctionValue();
		nextFunctionValue = null;
		}
		catch(ex)
		{
			// none;
		}
	},	
	
	chkBlank : function(obj,msg)
	{
		if(obj.value == "")
		{
			nextFunctionValue = obj.focus;			
			myAlert(msg,VD.utils.nextFunc);
			return false;
		}
		
		return true;
	},
	
	chkBlankNoFocus : function(obj,msg)
	{
		if(obj.value == "")
		{
			myAlert(msg,VD.utils.nextFunc);
			return false;
		}
		
		return true;
	},
	
	chkSel2 : function(sel,msg)
	{		
	    if(sel.length == null || sel.length == "undefined")
	    {
	        if(sel.checked)
	        	return true;
	        
	        alert(msg);	        
	        return false;
	    }
	    else
	    {        
	        var i=0;
	        for(i=0;i<sel.length;i++)
	        {
	            if(sel[i].checked)
	                return true;
	        }
	        
	        alert(msg);
	        return false;
	    }
	},

	chkBlank2 : function(obj,msg)
	{
		if(obj.value == "")
		{
			alert(msg);
			obj.focus();
			return false;
		}
		
		return true;
	},
	
	chkSame: function(obj1,obj2)
	{
		if(obj1.value != obj2.value)
		{
			nextFunctionValue = obj1.focus;
			var msg = "항목이 일치하지 않습니다.";
			myAlert(msg,VD.utils.nextFunc);
			return false;
		}
		
		return true;
	}
}

function NeedToLogin()
{
	myAlert("먼저 로그인해주세요.");
}

var onsuccRealURL = "";
DLG.RealName = {
	start: function(val){
		var w = 500;
		var options = {
			title: "실명 확인",
			url: "/dlg/dlg.aspx?type=realname",
			width: w
		}
		
		onsuccRealURL = val;
		if( this.modal != undefined && this.modal != null) this.modal = null; this.modal = FMW.MyModal(options);
	},
	show : function(msg)
	{
		this.start(msg);
	},
	hide : function()
	{	
		this.modal.destroy();delete this.modal;this.modal=null;
	},
	doit : function()
	{	
		if( !this.validCheck() )
			return;
			
		var ajaxURL = "/a/real.aspx";
		var data = "";
		data += FormSerialize("realname");
		PostData(ajaxURL,data,DLG.RealName.onsucc);		
	},
	onsucc : function(res)
	{
		// 결과..
		var r = setJSON(res);		
		if(!confirmRes(r))
		{
			// 실패했다..
			if(r["ret"] == "2" || r["ret"] == "3" )
			{
	 			var URL ="http://www.creditbank.co.kr/its/its.cb?m=namecheckMismatch"; 
        var status = "toolbar=no,directories=no,scrollbars=no,resizable=no,status=no,menubar=no, width= 640, height= 480, top=0,left=20"; 
        window.open(URL,"",status); 
        return;
			}			
			else if(r["ret"] == "50")
			{
         var URL ="http://www.creditbank.co.kr/its/itsProtect.cb?m=namecheckProtected"; 
         var status = "toolbar=no,directories=no,scrollbars=no,resizable=no,status=no,menubar=no, width= 640, height= 480, top=0,left=20"; 
         window.open(URL,"",status); 
         return;
			}
			else if(r["ret"] == "999")
			{
         myAlert("동일한 주민번호로 기존에 이미 가입된 회원이 존재합니다.");
         return;
			}
			else
			{
				myAlert("실명인증에 실패하였습니다.");
			}
			return;
		}
		
		// 성공..
		document.location.href=onsuccRealURL;
	},
	validCheck : function()
	{
		var oform = document.realname;
		if(!VD.utils.chkBlank(oform.name,"이름을 입력해주세요."))
			return false;		
		if(!VD.utils.chkBlank(oform.id1,"주민번호를 입력해주세요."))
			return false;		
		if(!VD.utils.chkBlank(oform.id2,"주민번호를 입력해주세요."))
			return false;		

		return true;
	}	
};

DLG.InviteFriend = {
	start: function(){
		var w = 530;
		var options = {
			title: "친구 초대하기",
			url: "/dlg/dlg.aspx?type=invite",
			width: w,
			onLoadFunc: this.onloaddlg
		}
		
		if( this.modal != undefined && this.modal != null) this.modal = null; this.modal = FMW.MyModal(options);
	},
	setfocus : function()
	{
		document.invite.email.focus();
	},	
	onloaddlg : function()
	{
		setTimeout(DLG.InviteFriend.setfocus, 1000);
	},		
	show : function()
	{
		this.start();
	},
	hide : function()
	{	
		this.modal.destroy();delete this.modal;this.modal=null;
	},
	onsucc : function(res)
	{
		myAlert("초대 메일이 전송되었습니다.");

		DLG.InviteFriend.hide();
	},
	send : function()
	{
		var oform = document.invite;
		if(!VD.utils.chkBlank(oform.email,"이메일을 입력해주세요."))
			return;
		if(!VD.utils.chkBlank(oform.msg,"내용을 입력해주세요."))
			return;

		var ajaxURL = "/a/sendEmail.aspx";
		var data = "type=invite&";
		data += FormSerialize("invite");
		PostData(ajaxURL,data,DLG.InviteFriend.onsucc);		
	}	
};

DLG.AfterNote = {
	start: function(uid){
		var w = 530;
		var options = {
			title: "낙찰소감보기",
			url: "/dlg/dlg.aspx?type=afternode&uid="+uid,
			width: w
		}
		
		if( this.modal != undefined && this.modal != null) this.modal = null; this.modal = FMW.MyModal(options);
	},
	show : function(uid)
	{
		this.start(uid);
	},
	hide : function()
	{	
		this.modal.destroy();delete this.modal;this.modal=null;
	}
};

DLG.sAfterNote = {
	start: function(uid){
		var w = 530;
		var options = {
			title: "낙찰소감보기",
			url: "/dlg/dlg.aspx?type=safternode&uid="+uid,
			width: w
		}
		
		if( this.modal != undefined && this.modal != null) this.modal = null; this.modal = FMW.MyModal(options);
	},
	show : function(uid)
	{
		this.start(uid);
	},
	hide : function()
	{	
		this.modal.destroy();delete this.modal;this.modal=null;
	}
};

DLG.OrderNotice = {
	start: function(){
		var w = 530;
		var options = {
			title: "공지사항",
			url: "/dlg/dlg.aspx?type=ordern",
			width: w
		}
		
		if( this.modal != undefined && this.modal != null) this.modal = null; this.modal = FMW.MyModal(options);
	},
	show : function()
	{
		this.start();
	},
	hide : function()
	{	
		this.modal.destroy();delete this.modal;this.modal=null;
	}
};

DLG.NoticeBid = {
	start: function(){
		var w = 530;
		var options = {
			title: "공지사항",
			url: "/dlg/dlg.aspx?type=notice",
			width: w
		}
		
		if( this.modal != undefined && this.modal != null) this.modal = null; this.modal = FMW.MyModal(options);
	},
	show : function()
	{
		this.start();
	},
	hide : function()
	{	
		this.modal.destroy();delete this.modal;this.modal=null;
	}
};

DLG.PopcornProd = {
	start: function(uid,point){
		var w = 530;
		var options = {
			title: "상품 추천하기",
			url: "/dlg/popcornProd.aspx?uid="+uid+"&point="+point,
			width: w,
			onLoadFunc: this.onloaddlg
		}
		
		if( this.modal != undefined && this.modal != null) this.modal = null; this.modal = FMW.MyModal(options);
	},
	setfocus : function()
	{
		document.popcornprod.comment.focus();
	},	
	onloaddlg : function()
	{
		setTimeout(DLG.PopcornProd.setfocus, 1000);
	},			
	show : function(uid,point)
	{
		this.start(uid,point);
	},
	hide : function()
	{	
		this.modal.destroy();delete this.modal;this.modal=null;
	},
	succ_confirm : function(res)
	{
		document.location.reload();
	},
	confirm : function()
	{
		var oform = document.popcornprod;
		
		if(!VD.utils.chkBlank(oform.comment,"추천글을 입력해주세요."))
			return;
		
		var ajaxURL = "/a/popprod.aspx";
		var data;
		data = FormSerialize("popcornprod");
	
		PostData(ajaxURL,data,DLG.PopcornProd.succ_confirm);
	}
};

DLG.SBidConfirm = {
	start: function(uid){
		var w = 530;
		var options = {
			title: "팝콘세일 참여하기",
			url: "/dlg/sbidConfirm.aspx?uid="+uid,
			width: w,
			onLoadFunc: this.onloaddlg
		}
		if( this.modal != undefined && this.modal != null) this.modal = null; this.modal = FMW.MyModal(options);
	},
	show : function(uid)
	{
		this.start(uid,"N");
	},
	hide : function()
	{	
		this.modal.destroy();delete this.modal;this.modal=null;
	},
	setfocus : function()
	{
		if(document.formverify != undefined)
			document.formverify.verify.focus();
	},	
	onloaddlg : function()
	{
		setTimeout(DLG.SBidConfirm.setfocus, 1000);
	},
	action : function(uid)
	{
		if(bidBlind)
		{
			DLG.SBidBlindConfirm.show(uid);
		}
		else
		{
			if(document.formverify == undefined)
			{
				sBID.module.bid(uid,'');
			}		
			else
			{
				sBID.module.bid(uid,document.formverify.verify.value);
			}
		}
		
		this.hide();
	}
};

DLG.SBidBlindConfirm = {
	start: function(uid){
		var w = 530;
		var options = {
			title: "팝콘세일 블라인드 참여하기",
			url: "/dlg/sbidBlindConfirm.aspx?uid="+uid,
			width: w,
			onLoadFunc: this.onloaddlg
		}
		if( this.modal != undefined && this.modal != null) this.modal = null; this.modal = FMW.MyModal(options);
	},
	show : function(uid)
	{
		this.start(uid);
	},
	hide : function()
	{	
		this.modal.destroy();delete this.modal;this.modal=null;
	},
	action : function(uid)
	{
		sBID.module.blindBid(uid,'');
		this.hide();
	}
};

DLG.agreement = {
	start: function(){
		var w = 530;
		var options = {
			title: "팝콘경매 참여동의",
			url: "/dlg/agreement.aspx",
			width: w,
			onLoadFunc: this.onloaddlg
		}
		if( this.modal != undefined && this.modal != null) this.modal = null; this.modal = FMW.MyModal(options);
	},
	show : function()
	{
		this.start();
	},
	hide : function()
	{	
		this.modal.destroy();delete this.modal;this.modal=null;
	},
	action : function()
	{
		if( !document.oform.agree[0].checked )
		{
			document.location.href = "/";
			return;
		}		

		var ajaxURL = "/a/agreement.aspx";
		PostData(ajaxURL,"",DLG.agreement.onsucc);		
	},
	onsucc : function(res)
	{
		document.location.href = "home.aspx";		
	}	
};


DLG.agentItem = {
	start: function(uid,mode){
		var w = 530;
		var options = {
			title: "팝콘경매 아이템",
			url: "/a/agentItem.aspx?uid="+uid+"&mode="+mode,
			width: w,
			onLoadFunc: this.onloaddlg
		}
		if( this.modal != undefined && this.modal != null) this.modal = null; this.modal = FMW.MyModal(options);
	},
	show : function(uid,mode)
	{
		this.start(uid,mode);
	},
	hide : function()
	{	
		this.modal.destroy();delete this.modal;this.modal=null;
	},
	buy : function(o,uid,mode)
	{		
		DLG.agentItem.turn(o,uid,mode);
	},
	turn : function(o,uid,mode)
	{
		var ajaxURL = "/a/turnagentitem.aspx?uid="+uid+"&mode="+mode+"&o="+o;
		PostData(ajaxURL,"",DLG.agentItem.onsucc);		
	},
	onsucc : function(res)
	{
		var r = setJSON(res);
		
		if(!r["res"])
		{
			myAlert(r["msg"]);
			return;
		}
		else
		{
			DLG.agentItem.chk(r["uid"]);
		}
		
		DLG.agentItem.hide();
	},
	chk : function(uid)
	{
		var ajaxURL = "/a/getagentitem.aspx?uid="+uid;
		PostData(ajaxURL,"",DLG.agentItem.onchk);		
	},
	onchk : function(res)
	{
		var r = setJSON(res);		
		
		if( $('item01') )
		{		
			if(r["item01"])
				$('item01').src="/images/item01_on.gif";
			else
				$('item01').src="/images/item01_off.gif";
		}
		
		if( $('item02') )
		{			
			if(r["item02"])
				$('item02').src="/images/item02_on.gif";
			else
				$('item02').src="/images/item02_off.gif";
		}
		
		if( $('item03') )
		{			
			if(r["item03"])
				$('item03').src="/images/item03_on.gif";
			else
				$('item03').src="/images/item03_off.gif";
		}		
	}
};

DLG.GiftRedBean = {
	start: function(val){
		var w = 530;
		var options = {
			title: "빨간콩 선물하기",
			url: "/dlg/pop_gift_bean.aspx",
			width: w
		}
		if( this.modal != undefined && this.modal != null) this.modal = null; this.modal = FMW.MyModal(options);
	},
	show : function(msg)
	{
		this.start(msg);
	},
	hide : function()
	{	
		this.modal.destroy();delete this.modal;this.modal=null;
	},
	res_doit : function(res)
	{
		// 처리한다
		var r = setJSON(res);		
		if(!confirmRes(r))
		{
			myAlert(r["msg"]);
		}
		else
		{
			myAlert("빨간콩을 선물하였습니다.");
			DLG.GiftRedBean.hide();
		}		
	},	
	doit : function()
	{
		var ajaxURL = "/a/gift.aspx";
		var data = "";
		data += FormSerialize("formgift");
		PostData(ajaxURL,data,DLG.GiftRedBean.res_doit);		
	}
};

DLG.ShowSaleWinner = {
	start: function(uid){
		var w = 530;
		var options = {
			title: "팝콘세일 아이템",
			url: "/a/ShowSaleWinner.aspx?uid="+uid,
			width: w,
			onLoadFunc: this.onloaddlg
		}
		if( this.modal != undefined && this.modal != null) this.modal = null; this.modal = FMW.MyModal(options);
	},
	show : function(uid)
	{
		this.start(uid);
	},
	hide : function()
	{	
		this.modal.destroy();delete this.modal;this.modal=null;
	},
	buy : function(uid)
	{
		var ajaxURL = "/a/getSaleCurrentWinner.aspx?uid="+uid;
		PostData(ajaxURL,"",DLG.ShowSaleWinner.onsucc);		
	},
	onsucc : function(res)
	{
		var r = setJSON(res);
		
		if(!r["res"])
		{
			myAlert(r["msg"]);
			return;
		}
		else
		{
			var msg = '<img src="/images/list_bullet.gif" align="absmiddle"> 현재 예상낙찰자 : ';

			var oval = "<a target='_blank' href='/credit.aspx?seller=";
			oval += r["winner"];
			oval += "'><font color='336699'><b>";
			oval += r["winner"];
			oval += "</b></font></a>";
			
			msg += oval;
			
			$('currentWinner').innerHTML = msg;
		}		
	}
};

DLG.HideMyId = {
	start: function(uid){
		var w = 530;
		var options = {
			title: "팝콘세일 아이템",
			url: "/a/HideMyId.aspx?uid="+uid,
			width: w,
			onLoadFunc: this.onloaddlg
		}
		if( this.modal != undefined && this.modal != null) this.modal = null; this.modal = FMW.MyModal(options);
	},
	show : function(uid)
	{
		this.start(uid);
	},
	hide : function()
	{	
		this.modal.destroy();delete this.modal;this.modal=null;
	},
	buy : function(uid)
	{
		var ajaxURL = "/a/setSaleHideMyID.aspx?uid="+uid;
		PostData(ajaxURL,"",DLG.HideMyId.onsucc);		
	},
	onsucc : function(res)
	{
		var r = setJSON(res);
		
		if(!r["res"])
		{
			myAlert(r["msg"]);
			return;
		}
		
		DLG.HideMyId.hide();
	}
};

DLG.AlertUser = {
	start: function(){
		var w = 530;
		var options = {
			title: "",
			url: "/dlg/alert.aspx",
			width: w,
			onLoadFunc: this.onloaddlg
		}

		if( this.modal != undefined && this.modal != null) this.modal = null; this.modal = FMW.MyModal(options);
	},
	show : function()
	{
		this.start();
	},
	hide : function()
	{	
		this.modal.destroy();delete this.modal;this.modal=null;
	}
};

DLG.BuyItNowHelp = {
	start: function(){
		var w = 600;
		var options = {
			title: "",
			url: "/dlg/dlg.aspx?type=buyitnow",
			width: w,
			onLoadFunc: this.onloaddlg
		}

		if( this.modal != undefined && this.modal != null) this.modal = null; this.modal = FMW.MyModal(options);
	},
	show : function()
	{
		this.start();
	},
	hide : function()
	{	
		this.modal.destroy();delete this.modal;this.modal=null;
	}
};

DLG.ShowSaleID = {
	start: function(uid,myid,rank){
		var w = 400;
		var options = {
			title: "현재 점유자 확인",
			url: "/dlg/showsaleid.aspx?uid="+uid+"&myid="+myid+"&rank="+rank,
			width: w,
			onLoadFunc: this.onloaddlg
		}

		if( this.modal != undefined && this.modal != null) this.modal = null; this.modal = FMW.MyModal(options);
	},
	setfocus : function()
	{
		if(document.formverify.verify)
			document.formverify.verify.focus();
	},	
	onloaddlg : function()
	{
		setTimeout(DLG.ShowSaleID.setfocus, 1000);
	},
	show : function(uid,myid,rank)
	{
		this.start(uid,myid,rank);
	},
	onsucc : function(res)
	{
		DLG.ShowSaleID.hide();
		
		// 결과..
		var r = setJSON(res);		
		if(confirmRes(r))
		{
			// 성공
			var msg = "<b>" + r["msg"] + "</b>";
			
			if( !r["myid"] )
			{
				$('sbidder'+r["uid"]).innerHTML = "<b><font color=#666666>" + r["msg"] + "</font></b>";			
			}
			else
				$(r["myid"]).innerHTML = "<b><font color=#666666>" + r["msg"] + "</font></b>";			
		}
	},
	action : function(uid,myid,rank,val)
	{
		var ajaxURL = "/a/getSaleID.aspx";
			
		var data = "uid="+uid+"&myid="+myid+"&vcode="+val+"&rank="+rank;
		PostData(ajaxURL,data,DLG.ShowSaleID.onsucc);		
	},
	hide : function()
	{	
		this.modal.destroy();delete this.modal;this.modal=null;
	}
};

DLG.BuyItNow = {
	start: function(uid){
		var w = 600;
		var options = {
			title: "",
			url: "/dlg/buyitnow.aspx?uid="+uid,
			width: w
		}

		if( this.modal != undefined && this.modal != null) this.modal = null; this.modal = FMW.MyModal(options);
	},
	show : function(uid)
	{
		this.start(uid);
	},
	hide : function()
	{	
		this.modal.destroy();delete this.modal;this.modal=null;
	}
};

DLG.BuyItNow2 = {
	start: function(uid){
		var w = 600;
		var options = {
			title: "",
			url: "/dlg/buyitnow2.aspx?uid="+uid,
			width: w
		}

		if( this.modal != undefined && this.modal != null) this.modal = null; this.modal = FMW.MyModal(options);
	},
	show : function(uid)
	{
		this.start(uid);
	},
	hide : function()
	{	
		this.modal.destroy();delete this.modal;this.modal=null;
	}
};

DLG.AuctionAgreement = {
	start: function(page){
		var w = 600;
		var options = {
			title: "팝콘경매 안내 및 동의",
			url: "/dlg/aagreement.aspx?page="+page,
			width: w
		}

		if( this.modal != undefined && this.modal != null) this.modal = null; this.modal = FMW.MyModal(options);		
		
		$("closeBtn"+this.modal.modalid).style.display = "none";
	},
	show : function(page)
	{
		this.start(page);
	},
	hide : function()
	{	
		this.modal.destroy();delete this.modal;this.modal=null;
	}
};

DLG.SaleAgreement = {
	start: function(page){
		var w = 600;
		var options = {
			title: "팝콘세일 안내 및 동의",
			url: "/dlg/sagreement.aspx?page="+page,
			width: w
		}

		if( this.modal != undefined && this.modal != null) this.modal = null; this.modal = FMW.MyModal(options);		
		
		$("closeBtn"+this.modal.modalid).style.display = "none";
	},
	show : function(page)
	{
		this.start(page);
	},
	hide : function()
	{	
		this.modal.destroy();delete this.modal;this.modal=null;
	}
};

DLG.Contact = {
	start: function(page){
		var w = 600;
		var options = {
			title: "연락처 확인",
			url: "/dlg/contact.aspx",
			width: w
		}

		if( this.modal != undefined && this.modal != null) this.modal = null; this.modal = FMW.MyModal(options);		
	},
	show : function(page)
	{
		this.start(page);
	},
	hide : function()
	{	
		this.modal.destroy();delete this.modal;this.modal=null;
	}
};
