/*
	Name: guest.js
	Author: Abhishek Dilliwal
	Date created: Jul 1 2008
	last modification: sept 19 2008
	description: javascript for register.php page
*/
//register form validation

//displays a message for a field
function show_message(obj_id,message){
	var obj = document.getElementById(obj_id);
	obj.style.color = "#FFF";
	obj.innerHTML = message;
}

//displays warning messgae for fields
function show_warning(obj_id,message){
	var obj = document.getElementById(obj_id);
	obj.style.color = "yellow";
	obj.innerHTML="";
	obj.innerHTML = message;	
}


//validates email
function validate_email(){
	obj = document.getElementById("email");
	if(obj.value==""){
		show_warning('m_email','You must specify your email');
		return false;
	}
	var log = chkEmail(obj.value);
	if(log!="true"){
		show_warning('m_email',log);
		return false;
	}
	else{
		return true;
	}
}


//chks if email is correct
function chkEmail(email){
	var i = 0;
	var l = email.length;
	var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.@";
	if(l<5) return "email minimum size could be 5 like a@b.c"; //email minimum size could be 5 like a@b.c
	var flag=false;
	var flag_dot =false;
	for(i=0;i<email.length;i++){
		var c = email.charAt(i);
		if(valid.indexOf(c) == -1){
			return "use of invalid character like $%^";//use of invalid character like $%^
		}
		if(c=="@"){
			if(i==0 || i>l-2) return "@ cannot be at first or last position";//@ cannot be at first position
			if(flag==true)	return "more than 1 @";//more than 1 @
			flag = true;
		}
		if(flag==true && c=="." && i<l-1){
			flag_dot = true;
			//alert("m here");
		}
	}
	//alert(flag_dot);
	if(!flag_dot) return "incomplete"; //no . after @
	return "true"; //everything alright
}

//monitors email as the user types
function monitor_email(e){
	var email = e.value;
	var result=chkEmail(email);
	if(result=="true"){
		show_message('m_email','');
		return true;
	}
	else{
		show_warning('m_email',result);
		return false;
	}
}

//checks if the username characters are valid
function monitor_username(obj){
			var i = 0;
			var valid="0123456789abcdefghijklmnopqrstuvwxyz._";
			var str="";
			for(i=0;i<obj.value.length;i++){
				c = obj.value.charAt(i);//alert(c);
				if(valid.indexOf(c) != -1){
					str+=c;
				}
				else{
					show_warning('m_username','only a-z, 0-9, . and _ could be used');
				}
			}
			//alert(str);
			obj.value = str;
}


//validates fname
function validate_fname(){
	obj = document.getElementById("name");
	if(obj.value==""){
		show_warning('m_fname','Name is required');
		return false;
	}
	else
	show_warning('m_fname','');
	return true;
}

//validates fname
function validate_comment(){
	obj = document.getElementById("comment");
	if(obj.value==""){
		show_warning('m_comment','No Comment???');
		return false;
	}
	else
	show_warning('m_comment','');
	return true;
}



//form registration vaidation
function validate_guestform(){
	if(	validate_fname() &&
	validate_email() && validate_comment()){
		return true;
	}
	else 
		return false;
}

	
