
////////////////////////////////////////////////////// FORM VALIDATION

function chnewslet(){
	if(isEmail(document.getElementById('newslet'), "Please enter your email!")){
		return true;
	}
	return false;
}
function chnewsletbtn(){if(chnewslet()){document.newsletfrm.submit();}}


function chlogin(){
	if(isEmpty(document.getElementById('loginnick'), "Please enter your nickname!")){
		if(isEmpty(document.getElementById('loginpw'), "Please enter your password!")){
			return true;
		}
	}
	return false;
}
function chloginbtn(){if(chlogin()){document.loginfrm.submit();}}


function chsrch(){
	if(isEmpty(document.getElementById('srch'), "Please enter what to search for!")){
		return true;
	}
	return false;
}
function chsrchbtn(){if(chsrch()){document.srchfrm.submit();}}


function chcom(){
	if(isEmpty(document.getElementById('addcomm'), "Please enter your comment!")){
		return true;
	}
	return false;
}
function chcombtn(){if(chcom()){document.comfrm.submit();}}


function chcomp(){
	if(isGroupChecked("radio", "answer", "Please select the answer!")){
		return true;
	}
	return false;
}
function chcompbtn(){if(chcomp()){document.compfrm.submit();}}


function chreg(){
	if(isEmpty(document.getElementById('name'), "Please enter your name!")){
		if(isEmpty(document.getElementById('sname'), "Please enter your surname!")){
			if(isEmpty(document.getElementById('nname'), "Please enter your nickname!")){
				if(isValidNick(document.getElementById('nname'), "Nickname may only consist of letters, numbers, hyphens and underscores!")){
					if(isEmail(document.getElementById('email'), "Please enter your email!")){
						if(isEmpty(document.getElementById("pwd"), "Please enter password!")){
							if(isEmpty(document.getElementById("cpwd"), "Please enter password again!")){
								if(isEqual(document.getElementById('pwd'), document.getElementById('cpwd'), "Your both passwords do not match, please enter again!")){
									return true;
								}
							}
						}
					}
				}
			}
		}
	}
	return false;
}
function chregbtn(){if(chreg()){document.regfrm.submit();}}


function cheditprof(){
	if(isEmpty(document.getElementById('name'), "Please enter your name!")){
		if(isEmpty(document.getElementById('sname'), "Please enter your surname!")){
			if(isEmail(document.getElementById('email'), "Please enter your email!")){
				return true;
			}
		}
	}
	return false;
}
function cheditprofbtn(){if(cheditprof()){document.editproffrm.submit();}}

function cheditpass(){
	if(isEmpty(document.getElementById("pwd"), "Please enter password!")){
		if(isEmpty(document.getElementById("cpwd"), "Please enter password again!")){
			if(isEqual(document.getElementById('pwd'), document.getElementById('cpwd'), "Your both passwords do not match, please enter again!")){
				return true;
			}
		}
	}
	return false;
}
function cheditpassbtn(){if(cheditpass()){document.editpassfrm.submit();}}


function chforgot(){
	if(isEmail(document.getElementById('email'), "Please enter your email!")){
		return true;
	}
	return false;
}
function chforgotbtn(){if(chforgot()){document.forgotfrm.submit();}}


////////////////////////////////////////////////////// FORM VALIDATION GLOBAL

function isEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		alert(helperMsg);
		elem.focus();
		return false;
	}else{
		return true;
	}
}

function isGroupChecked(type, name, helperMsg){
    var arrInput = document.getElementsByTagName("input");
	for (i = 0; i < arrInput.length; i++) {
		if ((arrInput[i].type == type) && (arrInput[i].name == name) && (arrInput[i].checked)) {
			return true;
		}
	}
    
	alert(helperMsg);
    return false;
}

function isEmail(elem, helperMsg){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isValidNick(elem, helperMsg){
	var emailExp = /^([a-zA-Z0-9-_@])+$/;
	if(elem.value.match(emailExp)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isEqual(elem, elem2, helperMsg){
	if(elem.value != elem2.value){
		alert(helperMsg);
		elem.value = '';
		elem2.value = '';
		elem.focus();
		return false;
	}else{
		return true;
	}
}



