// ¶óÀÌºêÆû Å¬·¡½º
var  domain = !!domain == true ? domain : document.domain;

var LIVEFORM = function() {
	var is_opener = false;
	var is_member = false;
	var is_form = new String();
	var is_locale = new String();
	var sb_selects = new Object();
	var sb_embeds = new Object();
	var sb_objects = new Object();
}
// ºí¶óÀÎµå
LIVEFORM.prototype.LiveScreenBlind = function(mode) {
	if(mode===true) {
		var _height = parseInt(document.body.clientHeight,10);
		if(parseInt(document.body.scrollHeight,10)>_height) _height = parseInt(document.body.scrollHeight,10);
		$('LiveScreenBlind').style.height = _height + "px";
		$('LiveScreenBlind').style.marginTop = "-" + parseInt(document.body.scrollTop,10) + "px";
		this.changeDisplay("LiveScreenBlind", true);
		// SELECT/EMBED/OBJECT °´Ã¼ ¼û±â±â
		this.sb_selects = document.getElementsByTagName("select");
		this.sb_embeds = document.getElementsByTagName("embed");
		this.sb_objects = document.getElementsByTagName("object");
		for(var i=0; i<this.sb_selects.length; i++) {
				if(this.sb_selects[i].name == "email_server") continue;
				this.sb_selects[i].style.visibility = "hidden";
		}
		for(var i=0; i<this.sb_embeds.length; i++) this.sb_embeds[i].style.visibility = "hidden";
		for(var i=0; i<this.sb_objects.length; i++) this.sb_objects[i].style.visibility = "hidden";
	}
	else {
		this.changeDisplay("LiveScreenBlind", false);
		for(var i=0; i<this.sb_selects.length; i++) this.sb_selects[i].style.visibility = "visible";
		for(var i=0; i<this.sb_embeds.length; i++) this.sb_embeds[i].style.visibility = "visible";
		for(var i=0; i<this.sb_objects.length; i++) this.sb_objects[i].style.visibility = "visible";
	}
}
// ¾ÆÀÌµðÁßº¹ Ã¼Å©
LIVEFORM.prototype.useVerifyID = function() {
	LiveFormMembershipFrm.verifyID.value=LiveFormMembershipFrm.id.value=LiveFormVerifyInfoFrm.liveID.value;
	this.changeDisplay('LiveFormVerifyInfoDiv', false);
	LiveFormMembershipFrm.passwd.focus();
}
// ·Î±×ÀÎ Ã¼Å©
LIVEFORM.prototype.checkLogin = function() {
	return this.is_member;
}

// Å¸°Ù Ã¼Å©
LIVEFORM.prototype.checkOpener = function() {
	return this.is_opener;
}
//Áßº¹¾ÆÀÌµð Ã¼Å©
LIVEFORM.prototype.verifyID = function(el) {
	//ÆÐÅÏ°ª
	var pattern = /^[a-z]{1,}[a-z0-9]{3,14}$/;
	//°ªÀÌ ¾øÀ»°æ¿ì
	if(el.value =="") {
		$('alert_msg').innerHTML = "";return;
	}
	//4ÀÚÀÌ»óÀÌ°í 15ÀÚÀÌÇÏÀÎ°æ¿ì 
	if(el.value.length >= 4 && el.value.length <= 15) {
		var url = domain+"LiveForm/LiveFormMultiProcess.ajax.html?mode=verifyID&uid="+el.value;
		if((!pattern.test(el.value))) {
			$('alert_msg').innerHTML = "¿µ¹® ¼Ò¹®ÀÚ·Î ½ÃÀÛÇÏ´Â 4~15ÀÚÀÇ ¿µ¹® ¼Ò¹®ÀÚ¿Í ¼ýÀÚÀÇ Á¶ÇÕ¸¸ »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù.";
			$('alert_msg').className = "alert_msg";
			return;
		}
		new Ajax.Request(url, {
			onSuccess: function(trans) {
				var resultData = trans.responseXML.getElementsByTagName('resultData')[0];
				if(resultData.getAttribute("result") == "success") {
					$('alert_msg').innerHTML = resultData.firstChild.nodeValue;
					$('alert_msg').className = "valid_msg";
					$('verifyID').value = el.value;
				} else {
					$('alert_msg').innerHTML = resultData.firstChild.nodeValue;
					$('alert_msg').className = "alert_msg";
					$('verifyID').value = "";
				}
			}
		});
	} else {
		$('alert_msg').innerHTML = "4~15ÀÚÀÇ ¿µ¹®,¼ýÀÚ¸¸ °¡´ÉÇÕ´Ï´Ù.";
		$('alert_msg').className = "alert_msg";
	}
}
// ÆûÃ¼Å©
LIVEFORM.prototype.validateForm = function(el) {
	var classObj = this;
	if(typeof(el.name)=="object") { // el.name ÀÌ object ÀÎ °æ¿ì´Â È¸¿ø°¡ÀÔÆûÀ» ÀÇ¹ÌÇÔ
		// ¸Þ½ÅÀú Á¾·ù ¼±ÅÃ À¯µµ - 2009.09.02 added
		$('messenger').value.blank() ? $('messenger_kind').removeAttribute('required') : $('messenger_kind').setAttribute('required', 'required');
	}
	if(!validate(el)) return false;
	if(el.name=="LiveFormVerifyInfoFrm") {
		var url = domain+"LiveForm/LiveFormMultiProcess.ajax.html?mode=verifyID&id="+el.id.value;
		var myRequest = new Ajax.Request(url, {
			method: 'get',
			onSuccess: function(transport) {
				if(!transport.responseText.match(null)) {
					var resultData = transport.responseXML.getElementsByTagName('resultData')[0];
					var resultText = resultData.firstChild!=null ? resultData.firstChild.nodeValue : null;
					$('LiveFormVerifyResult').innerHTML = resultText; // °á°ú ¸Þ½ÃÁö
					classObj.changeDisplay('LiveFormVerifyResultDiv', true);
				}
				myRequest = null;
			}
		});
	}
	else {
		this.is_opener = this.checkOpener()==undefined ? false : this.is_opener;
		this.is_form = (typeof(el.name)=='object') ? "LiveFormMembershipFrm" : el.name;
		this.multiProcess();
	}
	return true;
}
// ÇÁ·Î¼¼½º - ·Î±×ÀÎ/È¸¿ø°¡ÀÔ Ã³¸®
LIVEFORM.prototype.multiProcess = function() {
	if(this.is_form=='') return false;

	//º¸¾È¼­¹ö¿ë
	//var https_domain = 'https://'+document.domain+':450/';

	//ÀÏ¹Ý¿ë
	var https_domain = 'http://'+document.domain+'/';
	document.forms[this.is_form].action = https_domain+"LiveForm/LiveFormMultiProcess.html?mode="+this.is_form+"&uri="+encodeURIComponent(this.is_locale)+"&openMth="+this.is_opener;
}
// ÀÌ¸ÞÀÏ ¼­¹ö Ã¼Å© - È¸¿ø°¡ÀÔ ½Ã
LIVEFORM.prototype.check_email_server = function(el) {
	var email_account = document.getElementsByName('email_account')[0];
	if(el.value!="") {
		email_account.setAttribute("SPAN", '2');
		email_account.setAttribute("GLUE", '@');
		email_account.setAttribute("MAXBYTE", 39 - parseInt(el.value.length));
		email_account.setAttribute("UNIT", "ÀÚ");
		$('email_account').value = $('email_account').value.replace(/@(.*)?/gi,'');
		$('email_at').update("&nbsp;£À&nbsp;");
	}
	else {
		email_account.removeAttribute("SPAN");
		email_account.removeAttribute("GLUE");
		email_account.removeAttribute("MAXBYTE");
		email_account.removeAttribute("UNIT");
		$('email_at').update('&nbsp;');
	}
}

// ÆäÀÌÁö ÀüÈ¯
LIVEFORM.prototype.changePage = function(uri, join) {	
	if(!this.checkLogin()) {
		this.is_locale = uri;
		if(join===true) this.changeForm("LiveFormMembership"); // È¸¿ø°¡ÀÔ Æû ·Îµå
		else this.changeForm("LiveFormLogin"); // ·Î±×ÀÎ Æû ·Îµå
	}
	else {
		if(uri.indexOf("javascript")==-1) document.location.href=uri; // ¸µÅ© ÀÌµ¿
		else eval(uri.replace(/javascript:/g, '')); // ÀÚ½º ½ÇÇà
	}
	return void(0);
}
// Æû º¯°æ
LIVEFORM.prototype.changeForm = function(el) {

	this.LiveScreenBlind(true);
	switch(el) {
		case "LiveFormLogin": // ·Î±×ÀÎÆû È°¼ºÈ­
			this.changeDisplay("LiveFormMembershipDiv", false);
			this.changeDisplay("LiveFormLoginDiv", true);
			break;
		case "LiveFormMembership": // È¸¿ø°¡ÀÔÆû È°¼ºÈ­
			this.changeDisplay("LiveFormLoginDiv", false);
			this.changeDisplay("LiveFormMembershipDiv", true);			
			break;
	}
}
// ·¹ÀÌ¾î º¯°æ
LIVEFORM.prototype.changeDisplay = function(el, val) {
	if(val===true||val===false) {
		var obj = $(el);
		obj.style.display = val ? "block" : "none";
		if(val===false) {
			if(el=="LiveFormVerifyInfoDiv") {
				$('email_server').style.visibility = "visible";
				$('messenger_kind').style.visibility = "visible";
			}
			return false;
		}
		// ÆäÀÌÁö Áß¾Ó¿¡ ¶ç¿ì±â
		obj.style.top = (obj.offsetHeight>document.body.clientHeight) ? document.body.scrollTop : document.body.scrollTop+(document.body.clientHeight-obj.offsetHeight)/2 + "px";
		obj.style.left = (obj.offsetWidth>document.body.clientWidth) ? document.body.scrollLeft : document.body.scrollLeft+(document.body.clientWidth-obj.offsetWidth)/2 + "px";
		// Æû ÃÊ±âÈ­
		if(in_array(el, new Array("LiveFormMembershipDiv", "LiveFormLoginDiv", "LiveFormVerifyInfoDiv"))) {
			var frm = document.forms[el.replace(/Div/g, "Frm")];
			frm.reset(); 
			if(el=="LiveFormVerifyInfoDiv") {// Áßº¹Ã¼Å© Æû ÃÊ±âÈ­
				$('email_server').style.visibility = "hidden"; // ÀÌ¸ÞÀÏ¼­¹ö¼±ÅÃ
				$('messenger_kind').style.visibility = "hidden"; // ¸Þ½ÅÀú Á¾·ù¼±ÅÃ
				LiveFormVerifyInfoFrm.id.value=LiveFormMembershipFrm.id.value;
				this.changeDisplay("LiveFormVerifyResultDiv", false);
			}
		}
	}
	else {
		var _val = $(el).style.display;
		$(el).style.display = (_val=="none") ? "block" : "none";
		this.LiveScreenBlind(false);
	}
}

// ÀÌº¥Æ® ÀüÈ¯
LIVEFORM.prototype.openerLoc = function(uri) {
	window.opener.top.location.href = uri;
	self.close();
}

// ÀÌº¥Æ® ÀüÈ¯
LIVEFORM.prototype.initializeForm = function() {
	var Atags = document.getElementsByTagName("A");
	for(var i=0; i<Atags.length; i++) {
		if(!(this.is_member==true && this.is_opener!=true)){
			if(this.is_member!=true) { // ¡ê undefined			
				if(Atags[i].getAttribute("CHECKLOGIN")!=null) Atags[i].href = "javascript:LiveForm.changePage(\""+Atags[i].href+"\");";
				else if(Atags[i].getAttribute("JOINMEMBERSHIP")!=null) Atags[i].href = "javascript:LiveForm.changePage(\""+Atags[i].href+"\", true);";
			}else if(this.is_opener==true){
				if(Atags[i].getAttribute("CHECKLOGIN")!=null) Atags[i].href = "javascript:LiveForm.openerLoc(\""+Atags[i].href+"\");";
			}
		}
	}
}
