$(document).ready(function(){
	var galat = Array();
	$('#u_fname').bind('blur',function(e){
		if($(this).val()==''){
			$(this).addClass("formfalse");
			alert('ERROR!! Have you filled First name field?');
			galat['u_fname'] = true;	
		} else {
			$(this).removeClass("formfalse");
			$(this).addClass("formtrue");
		}	
	});
	$('#u_bday').bind('blur',function(e){
		if($(this).val()==''){
			$(this).addClass("formfalse");
			alert('ERROR!! Have you filled Birth Day field?');	
		} else {
			$(this).removeClass("formfalse");
			$(this).addClass("formtrue");
		}	
	});
	$('#u_address').bind('blur',function(e){
		if($(this).val()==''){
			$(this).addClass("formfalse");
			alert('ERROR!! Have you filled Address field?');	
		} else {
			$(this).removeClass("formfalse");
			$(this).addClass("formtrue");
		}	
	});
	$('#u_phone').bind('blur',function(e){
		if($(this).val()==''){
			$(this).addClass("formfalse");
			alert('ERROR!! Have you filled Phone field?');	
		} else {
			$(this).removeClass("formfalse");
			$(this).addClass("formtrue");
		}	
	});
	$('#u_cityzip').bind('blur',function(e){
		if($(this).val()==''){
			$(this).addClass("formfalse");
			alert('ERROR!! Have you filled City/Zip Code field?');	
		} 
		else {
			$(this).removeClass("formfalse");
			$(this).addClass("formtrue");
		}	
	});
	$('#u_mphone').bind('blur',function(e){
		if($(this).val()==''){
			$(this).addClass("formfalse");
			alert('ERROR!! Have you filled Mobile Phone field?');	
		} 
		else {
			$(this).removeClass("formfalse");
			$(this).addClass("formtrue");
		}	
	});
	$('#u_email').bind('blur',function(e){
		if($(this).val()==''){
			$(this).addClass("formfalse");
			alert('ERROR!! Have you filled Email field?');	
		} 
		else if (!validemail($(this).val())) {
			$(this).addClass("formfalse");
			alert('ERROR!! Please enter valid email address');	
		}
		else {
			$.get("/get/email.valid", { e: $(this).val()},
				function(data){
					var isthere = trim(data,'');
					if(isthere=='true') {
						//$('#u_email').addClass("formfalse");
						//alert('ERROR!! Your email address has been registered before');	
					} else {
						$('#u_email').removeClass("formfalse");
						$('#u_email').addClass("formtrue");
					}
				}
			);
		}	
	});
});

function validemail(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}
		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }
		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }
		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		 if (str.indexOf(" ")!=-1){
		    return false
		 }
 		 return true					
	}
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"), "");
}