function validateLogin() {
	if (document.getElementById('user').value=="") {
		alert("You must enter a User ID.")
		document.getElementById('user').focus();
		return false;
	}
	if (document.getElementById('auth').value=="") {
		alert("You must enter a Password.")
		document.getElementById('auth').focus();
		return false;
	}
	return true;
}

function validateDetails() {
	if (document.getElementById('u_name').value=="") {
		alert("You must enter your Name.")
		document.getElementById('u_name').focus();
		return false;
	}
	if (document.getElementById('tel_area_code').value=="" || document.getElementById('telephone').value=="") {
		alert("You must enter a Telephone Number including Area Code.")
		if (document.getElementById('tel_area_code').value=="") {
			document.getElementById('tel_area_code').focus();
		} else {
			document.getElementById('telephone').focus();
		}
		return false;
	}
	if (document.getElementById("uid").value=="") {
		if (document.getElementById('u_email').value=="") {
			alert("You must enter a valid Email.")
			document.getElementById('u_email').focus();
			return false;
		}
		//for proper format of email 
		var uemail=document.getElementById('u_email');
		if( !(validate_email(uemail.value)) ) { 
	        alert('Please enter a valid email address (eg: yourname@domain.com).'); 
			document.getElementById('u_email').focus();
		    return false; 
		} 
		if (document.getElementById('u_pword').value=="") {
			alert("You must enter a Password.")
			document.getElementById('u_pword').focus();
			return false;
		}
	}
	if (document.getElementById('u_pword').value!="") {
		if (document.getElementById('u_cpword').value=="") {
			alert("You must enter a Confirm Password.")
			document.getElementById('u_cpword').focus();
			return false;
		}
		if (document.getElementById('u_pword').value!=document.getElementById('u_cpword').value) {
			alert("The Confirm Password does not match the Password.")
			document.getElementById('u_cpword').focus();
			return false;
		}
	}
	return true;
}

function validate_email(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}

function confirmDeleteListing(root,id) {
	if (confirm("Are you sure you wish to delete this listing?")) {
		document.location.href=root+'/listings/listing.php?mode=delete&id='+id;
	}
}

function confirmDeleteUser(root,id) {
	if (confirm("Are you sure you wish to delete this user?")) {
		document.location.href=root+'/admin/users.php?mode=delete&id='+id;
	}
}

function validateUser() {
	if (document.getElementById('u_name').value=="") {
		alert("You must enter your Name.")
		document.getElementById('u_name').focus();
		return false;
	}
	if (document.getElementById('tel_area_code').value=="" || document.getElementById('telephone').value=="") {
		alert("You must enter a Telephone Number including Area Code.")
		if (document.getElementById('tel_area_code').value=="") {
			document.getElementById('tel_area_code').focus();
		} else {
			document.getElementById('telephone').focus();
		}
		return false;
	}
	if (document.getElementById('u_email').value=="") {
		alert("You must enter a valid Email.")
		document.getElementById('u_email').focus();
		return false;
	}
	//for proper format of email 
	var uemail=document.getElementById('u_email');
	if( !(validate_email(uemail.value)) ) { 
        alert('Please enter a valid email address (eg: yourname@domain.com).'); 
		document.getElementById('u_email').focus();
	    return false; 
	} 
	if (document.getElementById('u_pword').value=="") {
		alert("You must enter a Password.")
		document.getElementById('u_pword').focus();
		return false;
	}
	if (document.getElementById('u_pword').value!="") {
		if (document.getElementById('u_cpword').value=="") {
			alert("You must enter a Confirm Password.")
			document.getElementById('u_cpword').focus();
			return false;
		}
		if (document.getElementById('u_pword').value!=document.getElementById('u_cpword').value) {
			alert("The Confirm Password does not match the Password.")
			document.getElementById('u_cpword').focus();
			return false;
		}
	}
	return true;
}

