


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

function clearsearch(){
	if(document.getElementById('search').value == "meklēt"){
		document.getElementById('search').value = "";
	}
}

function setsearch(){
	if(document.getElementById('search').value == ""){
		document.getElementById('search').value = "meklēt";
	}
}

function chsearch(){
	if(isEmpty(document.getElementById('search'), "Ievadi ko meklēt!")){
		if(document.getElementById('search').value == "meklēt"){
			alert("Ievadi ko meklēt!");
			return false;
		} else if(document.getElementById('search').value.length < 3){
			alert("Ievadi min. 3 burtus!");
			return false;
		}else{
			return true;
		}
	}
	return false;
}
function chsearchbtn(){if(chsearch()){document.searchfrm.submit();}}


function chlogin(){
	if(isEmail(document.getElementById('regemail'), "Ievadi e-pastu!")){
		if(isEmpty(document.getElementById('regpw'), "Ievadi paroli!")){
			return true;
		}
	}
	return false;
}
function chloginbtn(){if(chlogin()){document.loginfrm.submit();}}


function chforgot(){
	if(isEmail(document.getElementById('email'), "Ievadi e-pastu!")){
		return true;
	}
	return false;
}
function chforgotbtn(){if(chforgot()){document.forgotfrm.submit();}}

function chadd(){
	if(document.getElementById('txt')){ //if add form
		if(isEmpty(document.getElementById('name'), "Ievadi slikto!")){
			if(isEmpty(document.getElementById('txt'), "Ievadi - kāpēc ir sliktais!")){
				return true;
			}
		}
		return false;
	}else{
		if(document.getElementById('utunreg').checked == true){
			if(isEmail(document.getElementById('unregemail'), "Ievadi e-pastu!")){
				if(isEmpty(document.getElementById('unregpw'), "Ievadi paroli!")){
					if(isEmpty(document.getElementById('unregpw2'), "Ievadi paroli atkārtoti!")){
						if(document.getElementById('unregpw').value == document.getElementById('unregpw2').value){
							return true;
						}
					}
				}
			}
			return false;
		}else{
			if(isEmail(document.getElementById('regemail'), "Ievadi e-pastu!")){
				if(isEmpty(document.getElementById('regpw'), "Ievadi paroli!")){
					return true;
				}
			}
			return false;
		}
	}
	//return false;
}
function chaddbtn(){if(chadd()){document.addfrm.submit();}}



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

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

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;
	}
}
