
$(document).ready(function() {
	checkLogin();
	addEventHander();
});

function addEventHander()
{
	// IDÀúÀå, TLD Ã¼Å©¹Ú½º value ÃÊ±âÈ­ ¹× click event handler µî·Ï
	$("input:checkbox[name!='check_cctld[]']").each(function() {
		$(this).val($(this).is(":checked") ? 'Y' : 'N');
		$(this).click(function() {
			$(this).val($(this).is(":checked") ? 'Y' : 'N');
		});
	});

	// ·Î±×ÀÎ
	$("input[name='user_pwd']").keydown(function(e) { if (e.which == '13') doLogin(); });
	$('#btn_login').keydown(function(e) { if (e.which == '13') doLogin(); });
	$('#btn_login').click(function() {
		doLogin();
	});

	// ±¹Á¦, KR µµ¸ÞÀÎ °Ë»ö
	$("input[name='check_domain_gtld_input']").keydown(function(e) { if (e.which == '13') $("#btn_check_domain_gtld").click(); });
	$("#btn_check_domain_gtld").click(function() {
		checkDomainGTLD();
	});
	
	// ¼¼°è±¹°¡ µµ¸ÞÀÎ °Ë»ö
	$("input[name='checking_domains_cctld']").keydown(function(e) { if (e.which == '13') $("#btn_check_domain_cctld").click(); });
	$("#btn_check_domain_cctld").click(function() {
		checkDomainccTLD();
	});

	// ¼¼°è±¹°¡ µµ¸ÞÀÎ ´ë·ú ¼±ÅÃ
	$("input:radio[name='cctld_continent']").click(function() {
		$("#continent_asia").hide();
		$("#continent_america").hide();
		$("#continent_europe").hide();
		$("#continent_africa").hide();
		$("#continent_oceania").hide();
		switch ($(this).val()) {
		case 'asia':
			$("#continent_asia").show();
			break;
		case 'america':
			$("#continent_america").show();
			break;
		case 'europe':
			$("#continent_europe").show();
			break;
		case 'africa':
			$("#continent_africa").show();
			break;
		case 'oceania':
			$("#continent_oceania").show();
			break;
		}
	});

	// ¾Æ½Ã¾Æ ÀüÃ¼¼±ÅÃ
	$("#check_all_continent_asia").click(function() {
		if ($(this).is(':checked')) {
			$("#continent_asia input:checkbox[name='check_cctld[]']").attr('checked', true);
		} else {
			$("#continent_asia input:checkbox[name='check_cctld[]']").attr('checked', false);
		}
	});
	
	// ¾Æ¸Þ¸®Ä« ÀüÃ¼¼±ÅÃ
	$("#check_all_continent_america").click(function() {
		if ($(this).is(':checked')) {
			$("#continent_america input:checkbox[name='check_cctld[]']").attr('checked', true);
		} else {
			$("#continent_america input:checkbox[name='check_cctld[]']").attr('checked', false);
		}
	});
	
	// À¯·´ ÀüÃ¼¼±ÅÃ
	$("#check_all_continent_europe").click(function() {
		if ($(this).is(':checked')) {
			$("#continent_europe input:checkbox[name='check_cctld[]']").attr('checked', true);
		} else {
			$("#continent_europe input:checkbox[name='check_cctld[]']").attr('checked', false);
		}
	});
	
	// ¾ÆÇÁ¸®Ä« ÀüÃ¼¼±ÅÃ
	$("#check_all_continent_africa").click(function() {
		if ($(this).is(':checked')) {
			$("#continent_africa input:checkbox[name='check_cctld[]']").attr('checked', true);
		} else {
			$("#continent_africa input:checkbox[name='check_cctld[]']").attr('checked', false);
		}
	});
	
	// ¿À¼¼¾Æ´Ï¾Æ ÀüÃ¼¼±ÅÃ
	$("#check_all_continent_oceania").click(function() {
		if ($(this).is(':checked')) {
			$("#continent_oceania input:checkbox[name='check_cctld[]']").attr('checked', true);
		} else {
			$("#continent_oceania input:checkbox[name='check_cctld[]']").attr('checked', false);
		}
	});
}

function checkLogin()
{
	if ($("input[name='login_error']").val() == '1') {
		$("#login_r1").show();
		$("#login_r2").show();
	} else {
		displayLoginInfo();
	}
}

function doLogin()
{
	var user_id  = $.trim($("input[name='user_id']").val());
	var user_pwd = $.trim($("input[name='user_pwd']").val());

	if (user_id.length == 0) {
		alert("¾ÆÀÌµð¸¦ ÀÔ·ÂÇØÁÖ¼¼¿ä.");
		$("input[name='user_id']").focus();
		return false;
	}

	if (user_pwd.length == 0) {
		alert("ºñ¹Ð¹øÈ£¸¦ ÀÔ·ÂÇØÁÖ¼¼¿ä.");
		$("input[name='user_pwd']").focus();
		return false;
	}

	var login_url = (location.hostname == "domain.r.co.kr") ? "https://domain.r.co.kr/login/helm.php" : "/login/helm.php";
	
	var postdata = {'user_id': user_id, 'user_pwd': user_pwd,
					'origin_site': location.hostname, 'main_login_flag': 'Y'}

	$.post('/login/helm.php', postdata, function(retdata) {
		if (retdata.login_error == '0') {
			displayLoginInfo();
		} else {
			alert(retdata.err_msg);
			if (retdata.login_error == '1') {
				$("input[name='user_id']").select();
			} else if (retdata.login_error == '2') {
				$("input[name='user_pwd']").select();
			}
			return false;
		}
	}, 'json');
}

function displayLoginInfo()
{
	$("#top_btn_loginout").html("<a href='/login/logout.php'>X</a>");

	$("#login_r1").hide();
	$("#login_r2").hide();
	$("#login_user_info").show();

	$("#userinfo_id").html("");
	// get_login_user_info();/......................·Î Ã¤¿ö³Ö±â
}

function checkDomainGTLD()
{
	var input = $.trim($("input[name='check_domain_gtld_input']").val());
	
	if (input.length == 0) {
		alert("°Ë»öÇÒ µµ¸ÞÀÎÀ» ÀÔ·ÂÇØÁÖ¼¼¿ä.");
		$("input[name='check_domain_gtld_input']").focus();
		return false;
	}

	if ($("input:checkbox[name='check_gtld']").val() == 'N') {
		alert("°Ë»öÇÒ µµ¸ÞÀÎ TLD¸¦ ¼±ÅÃÇØÁÖ¼¼¿ä.");
		$("input:checkbox[name='check_gtld']").focus();
		return false;
	}


	// http://r.co.kr/index.php¿¡¼­ Ã¼Å© °¡´ÉÇÑ TLDµé
	//var check_gtld_list = ['com', 'net', 'co.kr', 'kr', 'pe.kr', 'org', 'biz', 'info', 'name', 'tel', 'me', 'asia', 'co', 'Å°¿öµå'];
	var check_gtld_list = ['com', 'net', 'co.kr', 'kr', 'pe.kr', 'org', 'biz', 'info', 'name', 'tel', 'me', 'asia', 'co'];
	var avail_domains = [];
	var with_tld = false;
	
	if (input.split('.').pop() == input) { // TLD ¾øÀÌ ÀÔ·Â
		$.each(check_gtld_list, function(idx, tld) {
			var domain = Validator.check(input, tld);
			if (domain)
				avail_domains.push(domain);
		});
	} else { // TLD±îÁö ÀÔ·Â
		with_tld = true;
		var domain = Validator.check(input);
		if (domain)
			avail_domains.push(domain);
	}

	if (avail_domains.length < 1) {
		if (with_tld) alert(Validator.errmsg);
		else alert("°Ë»ö °¡´ÉÇÑ µµ¸ÞÀÎÀÌ ¾ø½À´Ï´Ù.");

		$("input[name='check_domain_gtld_input']").select();
		return false;
	}

	$("#frm_submit input").remove();
	$.each(avail_domains, function(idx, domain) {
		$("#frm_submit").append("<input type='hidden' name='check_domains[]' value='" + domain + "'>");
	});

	$("#frm_submit").submit();
}

function checkDomainccTLD()
{
	var avail_domains = [];
	var with_tld = false;

	var input = $.trim($("input[name='checking_domains_cctld']").val());
	
	if (input.length == 0) {
		alert("°Ë»öÇÒ µµ¸ÞÀÎÀ» ÀÔ·ÂÇØÁÖ¼¼¿ä.");
		$("input[name='checking_domains_cctld']").focus();
		return false;
	}

	if (($("#cctld_continent_asia").is(":checked")    && $("#continent_asia input:checkbox[name='check_cctld[]']:checked").length == 0)    ||
		($("#cctld_continent_america").is(":checked") && $("#continent_america input:checkbox[name='check_cctld[]']:checked").length == 0) ||
		($("#cctld_continent_europe").is(":checked")  && $("#continent_europe input:checkbox[name='check_cctld[]']:checked").length == 0)  ||
		($("#cctld_continent_africa").is(":checked")  && $("#continent_africa input:checkbox[name='check_cctld[]']:checked").length == 0)  ||
		($("#cctld_continent_oceania").is(":checked") && $("#continent_oceania input:checkbox[name='check_cctld[]']:checked").length == 0)) {
		alert("°Ë»öÇÒ ccTLD¸¦ ¼±ÅÃÇØÁÖ¼¼¿ä.");
		return false;
	}

	if (input.split('.').pop() == input) { // TLD ¾øÀÌ ÀÔ·Â
		var check_cctld_list = get_selected_cctld_list();
		$.each(check_cctld_list, function(idx, tld) {
			var domain = Validator.check(input, tld);
			if (domain)
				avail_domains.push(domain);
		});
	} else { // TLD±îÁö ÀÔ·Â
		with_tld = true;
		var domain = Validator.check(input);
		if (domain)
			avail_domains.push(domain);
	}
	
	if (avail_domains.length < 1) {
		if (with_tld) alert(Validator.errmsg);
		else alert("°Ë»ö °¡´ÉÇÑ µµ¸ÞÀÎÀÌ ¾ø½À´Ï´Ù.");

		$("input[name='checking_domains_cctld']").select();
		return false;
	}
//	alert('fsdfsfsf');
	$("#frm_submit input").remove();
	$.each(avail_domains, function(idx, domain) {
		$("#frm_submit").append("<input type='hidden' name='check_domains[]' value='" + domain + "'>");
	});
	$("#frm_submit").attr("action", "/regist/regist_step1.php");
	$("#frm_submit").submit();
}

function get_selected_cctld_list()
{
	var check_cctld_list = [];

	if ($("#cctld_continent_asia").is(":checked")) {
		if ($("#continent_asia input[name='check_cctld[]'][value='asia']").is(":checked")) {
			check_cctld_list.push('asia');
		}
		if ($("#continent_asia input[name='check_cctld[]'][value='in']").is(":checked")) {
			check_cctld_list.push('in');
			check_cctld_list.push('co.in');
			check_cctld_list.push('net.in');
			check_cctld_list.push('org.in');
			check_cctld_list.push('firm.in');
			check_cctld_list.push('gen.in');
			check_cctld_list.push('ind.in');
		}
		if ($("#continent_asia input[name='check_cctld[]'][value='cn']").is(":checked")) {
			check_cctld_list.push('cn');
			check_cctld_list.push('com.cn');
			check_cctld_list.push('net.cn');
			check_cctld_list.push('org.cn');
		}
		if ($("#continent_asia input[name='check_cctld[]'][value='jp']").is(":checked")) {
			check_cctld_list.push('jp');
		}
		if ($("#continent_asia input[name='check_cctld[]'][value='hk']").is(":checked")) {
			check_cctld_list.push('hk');
		}
		if ($("#continent_asia input[name='check_cctld[]'][value='tm']").is(":checked")) {
			check_cctld_list.push('tm');
		}
		if ($("#continent_asia input[name='check_cctld[]'][value='tw']").is(":checked")) {
			check_cctld_list.push('tw');
			check_cctld_list.push('com.tw');
			check_cctld_list.push('org.tw');
			check_cctld_list.push('club.tw');
			check_cctld_list.push('ebiz.tw');
			check_cctld_list.push('game.tw');
			check_cctld_list.push('idv.tw');
		}
		if ($("#continent_asia input[name='check_cctld[]'][value='ph']").is(":checked")) {
			check_cctld_list.push('ph');
			check_cctld_list.push('com.ph');
			check_cctld_list.push('net.ph');
			check_cctld_list.push('org.ph');
		}
		if ($("#continent_asia input[name='check_cctld[]'][value='pk']").is(":checked")) {
			check_cctld_list.push('pk');
			check_cctld_list.push('com.pk');
			check_cctld_list.push('net.pk');
			check_cctld_list.push('org.pk');
		}
		if ($("#continent_asia input[name='check_cctld[]'][value='uz']").is(":checked")) {
			check_cctld_list.push('uz');
			check_cctld_list.push('co.uz');
			check_cctld_list.push('com.uz');
			check_cctld_list.push('org.uz');
		}
		if ($("#continent_asia input[name='check_cctld[]'][value='la']").is(":checked")) {
			check_cctld_list.push('la');
		}
		if ($("#continent_asia input[name='check_cctld[]'][value='sg']").is(":checked")) {
			check_cctld_list.push('sg');
			check_cctld_list.push('com.sg');
		}
		if ($("#continent_asia input[name='check_cctld[]'][value='mn']").is(":checked")) {
			check_cctld_list.push('mn');
		}
		if ($("#continent_asia input[name='check_cctld[]'][value='tl']").is(":checked")) {
			check_cctld_list.push('tl');
		}
		if ($("#continent_asia input[name='check_cctld[]'][value='af']").is(":checked")) {
			check_cctld_list.push('af');
			check_cctld_list.push('com.af');
			check_cctld_list.push('net.af');
			check_cctld_list.push('org.af');
		}
		if ($("#continent_asia input[name='check_cctld[]'][value='ae']").is(":checked")) {
			check_cctld_list.push('ae');
		}
		if ($("#continent_asia input[name='check_cctld[]'][value='am']").is(":checked")) {
			check_cctld_list.push('am');
		}
		if ($("#continent_asia input[name='check_cctld[]'][value='ws']").is(":checked")) {
			check_cctld_list.push('ws');
		}
	} else if ($("#cctld_continent_america").is(":checked")) {
		if ($("#continent_america input[name='check_cctld[]'][value='us']").is(":checked")) {
			check_cctld_list.push('us');
		}
		if ($("#continent_america input[name='check_cctld[]'][value='com.mx']").is(":checked")) {
			check_cctld_list.push('com.mx');
		}
		if ($("#continent_america input[name='check_cctld[]'][value='ag']").is(":checked")) {
			check_cctld_list.push('ag');
			check_cctld_list.push('com.ag');
			check_cctld_list.push('net.ag');
			check_cctld_list.push('org.ag');
		}
		if ($("#continent_america input[name='check_cctld[]'][value='bz']").is(":checked")) {
			check_cctld_list.push('bz');
		}
		if ($("#continent_america input[name='check_cctld[]'][value='gd']").is(":checked")) {
			check_cctld_list.push('gd');
		}
		if ($("#continent_america input[name='check_cctld[]'][value='gl']").is(":checked")) {
			check_cctld_list.push('gl');
			check_cctld_list.push('co.gl');
			check_cctld_list.push('com.gl');
			check_cctld_list.push('edu.gl');
			check_cctld_list.push('net.gl');
			check_cctld_list.push('org.gl');
		}
		if ($("#continent_america input[name='check_cctld[]'][value='gs']").is(":checked")) {
			check_cctld_list.push('gs');
		}
		if ($("#continent_america input[name='check_cctld[]'][value='ms']").is(":checked")) {
			check_cctld_list.push('ms');
		}
		if ($("#continent_america input[name='check_cctld[]'][value='hn']").is(":checked")) {
			check_cctld_list.push('hn');
			check_cctld_list.push('com.hn');
			check_cctld_list.push('net.hn');
			check_cctld_list.push('org.hn');
		}
		if ($("#continent_america input[name='check_cctld[]'][value='ht']").is(":checked")) {
			check_cctld_list.push('ht');
			check_cctld_list.push('com.ht');
			check_cctld_list.push('net.ht');
			check_cctld_list.push('org.ht');
		}
		if ($("#continent_america input[name='check_cctld[]'][value='lc']").is(":checked")) {
			check_cctld_list.push('lc');
			check_cctld_list.push('com.lc');
			check_cctld_list.push('net.lc');
			check_cctld_list.push('org.lc');
		}
		if ($("#continent_america input[name='check_cctld[]'][value='pe']").is(":checked")) {
			check_cctld_list.push('pe');
		}
		if ($("#continent_america input[name='check_cctld[]'][value='tc']").is(":checked")) {
			check_cctld_list.push('tc');
		}
		if ($("#continent_america input[name='check_cctld[]'][value='vc']").is(":checked")) {
			check_cctld_list.push('vc');
			check_cctld_list.push('com.vc');
			check_cctld_list.push('net.vc');
			check_cctld_list.push('org.vc');
		}
		if ($("#continent_america input[name='check_cctld[]'][value='vg']").is(":checked")) {
			check_cctld_list.push('vg');
		}
	} else if ($("#cctld_continent_europe").is(":checked")) {
		if ($("#continent_europe input[name='check_cctld[]'][value='eu']").is(":checked")) {
			check_cctld_list.push('eu');
		}
		if ($("#continent_europe input[name='check_cctld[]'][value='de']").is(":checked")) {
			check_cctld_list.push('de');
		}
		if ($("#continent_europe input[name='check_cctld[]'][value='uk']").is(":checked")) {
			check_cctld_list.push('co.uk');
			check_cctld_list.push('me.uk');
			check_cctld_list.push('org.uk');
		}
		if ($("#continent_europe input[name='check_cctld[]'][value='fr']").is(":checked")) {
			check_cctld_list.push('fr');
		}
		if ($("#continent_europe input[name='check_cctld[]'][value='ch']").is(":checked")) {
			check_cctld_list.push('ch');
		}
		if ($("#continent_europe input[name='check_cctld[]'][value='cz']").is(":checked")) {
			check_cctld_list.push('cz');
		}
		if ($("#continent_europe input[name='check_cctld[]'][value='es']").is(":checked")) {
			check_cctld_list.push('es');
			check_cctld_list.push('com.es');
			check_cctld_list.push('nom.es');
			check_cctld_list.push('org.es');
		}
		if ($("#continent_europe input[name='check_cctld[]'][value='at']").is(":checked")) {
			check_cctld_list.push('at');
		}
		if ($("#continent_europe input[name='check_cctld[]'][value='be']").is(":checked")) {
			check_cctld_list.push('be');
		}
		if ($("#continent_europe input[name='check_cctld[]'][value='pl']").is(":checked")) {
			check_cctld_list.push('pl');
			check_cctld_list.push('com.pl');
			check_cctld_list.push('net.pl');
			check_cctld_list.push('org.pl');
			check_cctld_list.push('biz.pl');
			check_cctld_list.push('info.pl');
		}
		if ($("#continent_europe input[name='check_cctld[]'][value='lu']").is(":checked")) {
			check_cctld_list.push('lu');
		}
		if ($("#continent_europe input[name='check_cctld[]'][value='si']").is(":checked")) {
			check_cctld_list.push('si');
		}
		if ($("#continent_europe input[name='check_cctld[]'][value='sk']").is(":checked")) {
			check_cctld_list.push('sk');
		}
		if ($("#continent_europe input[name='check_cctld[]'][value='md']").is(":checked")) {
			check_cctld_list.push('md');
		}
		if ($("#continent_europe input[name='check_cctld[]'][value='li']").is(":checked")) {
			check_cctld_list.push('li');
		}
		if ($("#continent_europe input[name='check_cctld[]'][value='lt']").is(":checked")) {
			check_cctld_list.push('lt');
		}
		if ($("#continent_europe input[name='check_cctld[]'][value='im']").is(":checked")) {
			check_cctld_list.push('im');
		}
	} else if ($("#cctld_continent_africa").is(":checked")) {
		if ($("#continent_africa input[name='check_cctld[]'][value='ac']").is(":checked")) {
			check_cctld_list.push('ac');
		}
		if ($("#continent_africa input[name='check_cctld[]'][value='cm']").is(":checked")) {
			check_cctld_list.push('co.cm');
			check_cctld_list.push('com.cm');
			check_cctld_list.push('net.cm');
		}
		if ($("#continent_africa input[name='check_cctld[]'][value='mu']").is(":checked")) {
			check_cctld_list.push('mu');
			check_cctld_list.push('com.mu');
			check_cctld_list.push('net.mu');
			check_cctld_list.push('org.mu');
		}
		if ($("#continent_africa input[name='check_cctld[]'][value='mg']").is(":checked")) {
			check_cctld_list.push('mg');
		}
		if ($("#continent_africa input[name='check_cctld[]'][value='sh']").is(":checked")) {
			check_cctld_list.push('sh');
		}
		if ($("#continent_africa input[name='check_cctld[]'][value='sc']").is(":checked")) {
			check_cctld_list.push('sc');
			check_cctld_list.push('com.sc');
			check_cctld_list.push('net.sc');
			check_cctld_list.push('org.sc');
		}
		if ($("#continent_africa input[name='check_cctld[]'][value='tk']").is(":checked")) {
			check_cctld_list.push('tk');
		}
		if ($("#continent_africa input[name='check_cctld[]'][value='co.za']").is(":checked")) {
			check_cctld_list.push('co.za');
		}
	} else if ($("#cctld_continent_oceania").is(":checked")) {
		if ($("#continent_oceania input[name='check_cctld[]'][value='nz']").is(":checked")) {
			check_cctld_list.push('co.nz');
			check_cctld_list.push('net.nz');
			check_cctld_list.push('org.nz');
		}
		if ($("#continent_oceania input[name='check_cctld[]'][value='fm']").is(":checked")) {
			check_cctld_list.push('fm');
		}
		if ($("#continent_oceania input[name='check_cctld[]'][value='cx']").is(":checked")) {
			check_cctld_list.push('cx');
		}
		if ($("#continent_oceania input[name='check_cctld[]'][value='ki']").is(":checked")) {
			check_cctld_list.push('ki');
			check_cctld_list.push('com.ki');
			check_cctld_list.push('net.ki');
			check_cctld_list.push('org.ki');
		}
		if ($("#continent_oceania input[name='check_cctld[]'][value='nf']").is(":checked")) {
			check_cctld_list.push('nf');
			check_cctld_list.push('com.nf');
			check_cctld_list.push('net.nf');
		}
		if ($("#continent_oceania input[name='check_cctld[]'][value='io']").is(":checked")) {
			check_cctld_list.push('io');
		}
		if ($("#continent_oceania input[name='check_cctld[]'][value='nu']").is(":checked")) {
			check_cctld_list.push('nu');
		}
		if ($("#continent_oceania input[name='check_cctld[]'][value='sb']").is(":checked")) {
			check_cctld_list.push('com.sb');
			check_cctld_list.push('net.sb');
			check_cctld_list.push('org.sb');
		}
	}

	return check_cctld_list;
}

