// JavaScript Document
function checkemailavail() {
if (!document.getElementById("email").value) { return [[document.getElementById("email"), "Please enter your email address."]]; }
		$.post('regfunctions.php', {ac:"chkemail",emailadd:$('#email').val()}, function(data) {
			var email_exists = data;
			$('#rese').val(data);
   		});
		
		if(email_exists!="valid") {
			 var field = document.getElementById("email");
    		return [[field, "The email you provided is already registered."]];
     	 } else {
			$('#rese').val('valid');
		 }
		 return true;
		}
		
function checkuidavail() {
if (!document.getElementById("uid").value) { return [[document.getElementById("uid"), "Please enter your desired username."]]; }
		$.post('regfunctions.php', {ac:"chkuser",uidname:$('#uid').val()}, function(data) {
			var user_exists = data;
			$('#resp').val(data);
   		});
		
		if(user_exists!="valid") {
			 var field = document.getElementById("uid");
    		return [[field, "The username you provided is already taken."]];
     	 }
		 return true;
		}		

$(document).ready(function() {	
		
        $("#registrationmain").RSV({
                rules: [
                        "required,first_name,Please enter your first name.",
                        "required,last_name,Please enter your last name.",
                        "valid_email,email,Please enter a valid email address.",
						"function,checkemailavail",
						"same_as,email,retype_email,Please enter same email as above.",
						"function,checkuidavail",
                        "required,pwd,Please enter your desired password.",
                        "same_as,pwd,retype_pwd,Please enter same password as above."
                ]
				
        });
});

function echeck(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 validate_login_form(){
	
	var log_form = document.login_form;
	
	if(log_form.log_username.value == ""){
		alert("Please provide username to log in.");
		log_form.log_username.focus();
		return false;
	}

	if(log_form.log_password.value == ""){
		alert("Please provide password to log in.");
		log_form.log_password.focus();
		return false;
	}

}

function validate_add(){
	var add_form = document.feed_form;
	
	if(add_form.vod_title.value == ""){
		alert("Please provide feed title.");
		add_form.vod_title.focus();
		return false;
	}

	if(add_form.vod_desc.value == ""){
		alert("Please provide feed description.");
		add_form.vod_desc.focus();
		return false;
	}

	if(add_form.vod_file.value == ""){
		alert("Please provide feed file or URL.");
		add_form.vod_file.focus();
		return false;
	}

}

function validate_epart() {
	var epart_form = document.epart;
	
	if(epart_form.fullname.value == ""){
		alert("Please enter your full name.");
		epart_form.fullname.focus();
		return false;
	}
	
	if(epart_form.email.value == ""){
		alert("Please enter your email address.");
		epart_form.email.focus();
		return false;
	}
	
	if (echeck(epart_form.email.value)==false){
		alert("Please enter a valid email address.");
		epart_form.email.focus();
		return false
	}
	
	
}

function validate_notice() {
	var add_form = document.notice_form;
	if(add_form.ntc_title.value == ""){
		alert("Please notice title.");
		add_form.ntc_title.focus();
		return false;
	}

	if(add_form.ntc_body.value == ""){
		alert("Please provide notice message.");
		add_form.ntc_body.focus();
		return false;
	}
}


/*
function validate_registration() {
	var fname = trim(document.getElementById("first_name").value);
	var lname = trim(document.getElementById("last_name").value);
	var email = trim(document.getElementById("email").value);
	var uid = trim(document.getElementById("uid").value);
	var pwd = trim(document.getElementById("pwd").value);
	var confirm = trim(document.getElementById("retype_pwd").value);

	if ((fname.length>0) && (lname.length>0) && (email.length>0) && (uid.length>0) && (pwd.length>0) && (confirm.length>0)) {
		if ((validate_email(email) == true) && (pwd == confirm)) {
			return true;
		}else{
			alert("Email is not in a correct format or password is not equal.");
		}
	}else{
		alert("All fields are required!");
	}
	return false;
}


function validate_edit() {
	var fname = trim(document.getElementById("first_name").value);
	var lname = trim(document.getElementById("last_name").value);
	var email = trim(document.getElementById("email").value);
	var uid = trim(document.getElementById("uid").value);
	var pwd = trim(document.getElementById("pwd").value);
	var confirm = trim(document.getElementById("retype_pwd").value);

	if ((fname.length>0) && (lname.length>0) && (email.length>0) && (uid.length>0)) {
		if ((validate_email(email) == true)) {
			if ((pwd.length>0) && (confirm.length>0))
			{
				if ((pwd == confirm))
				{
					return true;
				}else{
					alert("Password is not equal!");
				}
			}else if ((pwd.length>0) && (confirm.length<=0)) {
				alert("Re-type Password must not be blank!");
			}else if ((pwd.length<=0) && (confirm.length>0)) {
				alert("New Password must not be blank!");
			}else{
				return true;
			}
		}else{
			alert("Email is not in a correct format or password is not equal.");
		}
	}else{
		alert("All fields are required!");
	}
	return false;
}
*/

/*
function validate_registration() {
	var fname = trim(document.getElementById("first_name").value);
	var lname = trim(document.getElementById("last_name").value);
	var email = trim(document.getElementById("email").value);
	var confirm_email = trim(document.getElementById("retype_email").value);
	var uid = trim(document.getElementById("uid").value);
	var pwd = trim(document.getElementById("pwd").value);
	var confirm = trim(document.getElementById("retype_pwd").value);
	var emailconf;

	if ((fname.length>0) && (lname.length>0) && (email.length>0) && (uid.length>0) && (pwd.length>0) && (confirm.length>0) && (confirm_email.length>0)) {
		if ((validate_email(email) == true) && (validate_email(confirm_email) == true)) {
			
			//if (emailconf != "emailexist") {
			if (pwd == confirm) {
				if (email == confirm_email) {
					return true;
				}else{
					alert("Email Address is not equal.");
				}
			}else{
				alert("Password is not equal.");
			}
			//}else{
			//alert("The email you provide is already registered to this site!");
		//}
		}else{
			alert("Email is not in a correct format");
		}
		
	}else{
		alert("All fields are required!");
	}
	return false;
}
*/
function validate_edit() {
	var fname = trim(document.getElementById("first_name").value);
	var lname = trim(document.getElementById("last_name").value);
	var email = trim(document.getElementById("email").value);
	var confirm_email = trim(document.getElementById("retype_email").value);
	var uid = trim(document.getElementById("uid").value);
	var pwd = trim(document.getElementById("pwd").value);
	var confirm = trim(document.getElementById("retype_pwd").value);

	if ((fname.length>0) && (lname.length>0) && (email.length>0) && (uid.length>0) && (confirm_email.length>0)) {
		if ((validate_email(email) == true) && (validate_email(confirm_email) == true)) {
			if ((pwd.length>0) && (confirm.length>0))
			{
				if ((pwd == confirm))
				{
					if (email == confirm_email) {
						return true;
					}else{
						alert("Email Address is not equal.");
					}
				}else{
					alert("Password is not equal!");
				}
			}else if ((pwd.length>0) && (confirm.length<=0)) {
				alert("Re-type Password must not be blank!");
			}else if ((pwd.length<=0) && (confirm.length>0)) {
				alert("New Password must not be blank!");
			}else{
				if (email == confirm_email) {
					return true;
				}else{
					alert("Email Address is not equal.");
				}
			}
		}else{
			alert("Email is not in a correct format");
		}
	}else{
		alert("All fields are required!");
	}
	return false;
}


function validate_login() {
	var uid = trim(document.getElementById("login_uid").value);
	var pwd = trim(document.getElementById("login_pwd").value);
	
	if ((uid.length>0) && (pwd.length>0)) {
		return true;
	}else{
		alert("You will need to provide your username and password before you can login!");
	}
	return false;
}

	
function validate_email(email) {
	return (email.indexOf(".") > 2) && (email.indexOf("@") > 0);
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}


function recover_submit(lnk) {
	var email = trim(document.getElementById("email_recover").value);

	if (email.length>0) {
		if (validate_email(email) == true) {
			window.location = lnk + '&em=' + email;
		}else{
			alert("Email is not in a correct format or password is not equal.");
		}
	}else{
		alert("You need to fill in the email address box before submitting.");
	}
	return false;
}

function show_recover_box() {
	document.getElementById("recoverbox").style.display = "";
}