//<![CDATA[
var keyData = {
	id		 : [ "error_userid", "error_userid_icon", "enable_id", "allow_id"],
	email	 : [ "error_email", "error_email_icon", "enable_email", "allow_email"], 
	nick	 : [ "error_nick", "error_nick_icon", "enable_nick", "allow_nick"],
	parentid : [ "error_parentid", "error_parentid_icon", "enable_parentid", "allow_parentid"]
};
var addMsg = {
	id		 : "\nEl usuario está siendo usado. Por favor elija otro usuario.      ", 
	email	 : "\nEl email está siendo usado. Por favor elija otro email.      ", 
	nick	 : "\nEl nick está siendo usado. Por favor elija otro nick.      ", 
	parentid : "\nEs un usuario NO REGISTRADO. Por favor elija otro usuario.      " 
};
var isContine = true;
var xObj = {
	/* object string , html object, ajax return value(varified data) */
	id		 : { htmlStr:null, htmlObj:null, xhrData:null },
	email	 : { htmlStr:null, htmlObj:null, xhrData:null },
	nick	 : { htmlStr:null, htmlObj:null, xhrData:null },
	parentid : { htmlStr:null, htmlObj:null, xhrData:null }
};

function setValidedMark(obj, clear)
{
	try {
		var f = document.member_join_form;
		var icoObj = null;
		
		if (clear != undefined) {
			icoObj = document.getElementById(keyData[obj][1]);
			//alert("clear : " + clear);
			icoObj.className = "";
		} else {
			//original routin
			if (f[keyData[obj][3]] != undefined  && f[keyData[obj][2]].value == "Y") {
				icoObj = document.getElementById(keyData[obj][1]);
				
				if (f[obj].value == f[keyData[obj][3]].value) {
					icoObj.className = "valided";
				} else {
					icoObj.className = "notvalided";
					f[obj].focus();
				}
			}
		}
	} catch(e) {
		alert("setValidedMark fnc : " + e);
	}
}

function isValid(obj, types)
{
	var isVal = false;
	var f = document.member_join_form;
	var obj = f[obj];
	try {
		if (types == 1) {//is empty
			if (obj.value != "") {
				isVal = true;
			}
		} else if (types == 2) {//4~20, char + num
			if (/(^[0-9a-zA-Z]{4,20}$)/.test(obj.value)) {
				isVal = true;
			}
		} else if (types == 3) {//password type
			if (/(^[0-9a-zA-Z\!\@\#\$\%\^\&*\(\)\_\-\+\=<\>\.\/\?\:\;\[\]\{\}]{4,20}$)/.test(obj.value)) {
				isVal = true;
			}
		} else if (types == 4) {//email type - name
		//if (/^([0-9a-zA-Z_-]+)@([0-9a-zA-Z_-]+)(\.[0-9a-zA-Z_-]+){1,2}$/.test(obj.value)) {
			if (/^([0-9a-zA-Z_-]+){1,2}$/.test(obj.value)) {
				isVal = true;
			}
		} else if (types == 5) {//check type
			if (obj.checked) isVal = true;
		} else if (types == 6) {//radio
			var o = obj;
			var oLen = o.length;
			for(var j=0; j<oLen; j++){
				if(o[j].checked){
					isVal = true;
					break;
				}
			}					
		}
		//recommand id
		if (types == 7) {
			if (obj.value != "") {
				//alert("type 7 is run");
				isVal = true;
			}
		}
		
		return isVal;
	} catch(e) {
		//alert("e: " + e.description + " , obj: " + obj);
		return false;
	}
}

function check_inquiry_form() {

	var f = document.inquiry_form;
	
	// title empty
	if (f.title.value == '') {
		alert("Por favor proporciona tu Titulo.      ");
		f.title.focus();
		return false;
	}

	// contents empty
	if (f.contents.value == '') {
		alert("Por favor proporciona tu Contenido.      ");
		f.contents.focus();
		return false;
	}
	
	f.sbmtBtn.style.display = "none";

}

function check_member_join_form() {
	try {
		var f = document.member_join_form;
		for (var i=0; i<chkData.length; ++i) {
			var htmlObj = f[chkData[i]['htmlStr']];
			if (chkData[i]['isValid']) {
				if ( !isValid(chkData[i]['htmlStr'], chkData[i]['validType']) ) {
					
					if (xObj[chkData[i]['htmlStr']] != undefined) {
						if (xObj[chkData[i]['htmlStr']]['xhrData']) setValidedMark(chkData[i]['htmlStr']);
					}
					
					if (chkData[i]['validType'] != 7) { //excepted routin at 20110407
						alert(chkData[i]['msg']);	
						htmlObj.focus();
						return false;
						break;
					} else {
						var tmpObj = document.getElementById(keyData[htmlObj.id][1]);
						//-- if (tmpObj) document.getElementById(keyData[htmlObj.id][1]).className = "";
						if (tmpObj) setValidedMark(htmlObj.id, 1);
					}
					
				} else {
					//id
					if (htmlObj.id == "id") {					
						if ( xObj['id']['xhrData'] != f.id.value ) {
							callAjax(htmlObj, "id");
							return false;
						}
						setValidedMark(htmlObj.id);
					}
					//email
					if (htmlObj.id == "email") {
						var tmpMail = f['ori_email'];
						var isCallAjax = false;
						if (tmpMail != undefined) {
							if (tmpMail.value != f.email.value) { //if email data is be
								if ( xObj['email']['xhrData'] != f.email.value ) {
									isCallAjax = true;
								}
							} else {
								if (xObj['email']['htmlStr'] && !xObj['email']['xhrData']) {
									isCallAjax = true;
								}
							}
						} else {
							if ( xObj['email']['xhrData'] != f.email.value ) {
								isCallAjax = true;
							}
						}

						// email domain	
						if (f.email_domain.value == "") {
							alert("Introduce una direccion de correo electronico valida.");
							f.email_domain.focus();
							return false;
						}											
						
						if (isCallAjax) {
							callAjax(htmlObj, "email");
							return false;
						}
						setValidedMark(htmlObj.id);
					}
					//nick
					if (htmlObj.id == "nick") {					
						if ( xObj['nick']['xhrData'] != f.nick.value ) {
							callAjax(htmlObj, "nick");
							return false;
						}
						setValidedMark(htmlObj.id);
					}
					//parentid
					if (htmlObj.id == "parentid") {
						if (htmlObj.value == "") alert(11);
						if (xObj['parentid']['xhrData'] != f.parentid.value) {
							callAjax(htmlObj, "parentid");
							return false;
						}
						setValidedMark(htmlObj.id);
					}
					
					// pw1 is not equal pw2
					if (htmlObj.id == "pw2") {
						if (f.pw2.value != f.pw1.value) {
							//패스워드를 동일하게 넣어주세요.
							alert("Las contraseñas que has introducido no coinciden. \nPor favor ingrese la misma contraseña que puso anteriormente.      ");
							htmlObj.focus();
							return false;
						}
					}
				}
			}
			
		}
		return true;
	} catch(e) {
		alert("check_member_join_form fnc : " + e);
		return false;
	}
}

function contineValidation() {
	try {
		if(isContine) document.getElementById('sbmtBtn').click();
	} catch(e) {
		alert("contineValidation fnc : " + e);
		return false;
	}
}

function getDataIdx(objStr) {
	var n = 0;
	try {		
		for (var i=0; i<chkData.length; ++i) {		
			if (objStr == chkData[i]['htmlStr']) {
				n = i;
				break;
			}
		}
		return n;
	} catch(e) {
		alert("getDataIdx fnc : " + e);
		return n;
	}
}

function callAjax(obj, objStr, once) {
	try {
		var msg = "Por favor introduce el correcto.     ";//올바르게 입력해 주세요.
		if (once) { //for standalone call
			if (obj.value == "") {
				alert(msg);
				obj.focus();
				return false;
			} else {
				
				var n = getDataIdx(objStr);
				if ( !isValid(objStr, chkData[n]['validType']) ) {
					alert(chkData[n]['msg']);					
					obj.focus();
					return false;
				} else {
					if (xObj[objStr]['xhrData']) {
						if ( xObj[objStr]['xhrData'] != obj.value ) {
							isContine = false;
						} else {
							alert(obj.value + " está disponible.     ");
							setValidedMark(objStr);
							return false;
						}
					}
				}
			}
		}
				
		xObj[objStr]['htmlStr'] = objStr; //asign to string
		xObj[objStr]['htmlObj'] = obj;    //asign to object (html object)
		
		var prePath = "";
		var params = objStr + "=" + obj.value;
		
		// 메일주소일 경우 값 추가
		if (objStr == "email") {
			params = params + "&email_domain=@" + document.member_join_form.email_domain.value;
		}
		
		setXHR('POST',prePath + 'chk_data.html?'+params, true, '', '', '', true, '', onResponse);
	} catch(e) {
		alert("callAjax fnc : " + e);
		return false;
	}
}

var rstVal = null; //for game

function onResponse(rsData)
{
	try {
		if (rsData) {
			//alert("rsData : " + rsData);			
			if (!rsData.err) {
				if (rsData.success == "Y") {
					xObj[rsData.name]['xhrData'] = rsData.nameValue;
					
					document.getElementById(rsData.objId).innerHTML = rsData.cnt;
					
					contineValidation();
					if (!isContine) {
						alert(rsData.nameValue + " está disponible.     ");
						isContine = true;
					}
				} else {
					alert(rsData.nameValue + " no está disponible.      "  + addMsg[rsData.name]);
					if (document.getElementById(keyData[rsData.name][1]) != undefined) {
						setValidedMark(rsData.name);
					} else {
						document.getElementById(rsData.objId).innerHTML = rsData.cnt;
					}
					isContine = true;
					xObj[rsData.name]['htmlObj'].focus();
				}				
			} else {
				return false;
			}
			
		}
		else
		{
			//no data is return
			alert("Lo sentimos. por favor, inténtelo de nuevo.      ");
			return false;
			
		}
	} catch(e) {
		alert("onResponse fnc : " + e);
		return false;		
	}
}
//]]>
