$(document).ready(function(){
	function slideout(){
		setTimeout(function(){
		$("#error").slideUp("slow", function () {});}, 5000);
	}
	
	$("#error").hide();
	$("#signupsubmit").click(function(){validsignup();});
	$('input[type="text"]').each(function(){
		$(this).keypress(function(e){
			if (e.which == 13) { validsignup(); }
		});
	});
	$('input[type="password"]').each(function(){
		$(this).keypress(function(e){
			if (e.which == 13) { validsignup(); }
		});
	});
	

	function validsignup(){
		$('#loading_img').html('<img src="../images/loading.gif" style="position:relative;top:-15px;"/>');
		var user_login = $('#user_login').val();
		var user_email = $('#user_email').val();
		var user_password = $('#user_password').val();
		var user_password_confirmation = $('#user_password_confirmation').val();
		var user_city = $('#user_city').val();
		var user_state = $('#user_state').val();
		var user_country = $('#user_country').val();
		var birthdate = $('#birthdate').val();
		
		var err = '';
		if(user_login==''){ err+='<li>Username is required!.</li>'; }
		if(user_email==''){ 
			err+='<li>Email is required!.</li>'; 
		} else { 
			if(validEmail(user_email) == false ){ err+='<li>Invalid E-mail ID!.</li>'; }
		}
		if(user_password ==''){ err+='<li>Password is required!.</li>'; }
		if(user_password_confirmation==''){ err+='<li>Confirm Password is required!.</li>'; }
		if(user_password != user_password_confirmation ){ err+='<li>Password doesn\'t match confirmation!.</li>'; }
		if(birthdate){
			var bdate = birthdate.split("/");
			var d= new Date();
			var curr_date = d.getDate();
			var curr_month = d.getMonth() + 1;
			var curr_year = d.getFullYear();
			//var curr_date = curr_date+'/'+curr_month+'/'+curr_year;
			//if(birthdate > curr_date ){ err+='<li>Birth date should be less than current date!.</li>'; }
			if(parseInt(bdate[2])>=parseInt(curr_year)){
				if(parseInt(bdate[1])>=parseInt(curr_month)){
					if(parseInt(bdate[0])>=parseInt(curr_date)){
						err+='<li>Birth date should be less than current date!.</li>';
					}else {
						if(parseInt(bdate[1])>parseInt(curr_month)){
							err+='<li>Birth date should be less than current date!.</li>';
						}
					}
				}else{
					if(parseInt(bdate[2])>parseInt(curr_year)){
						err+='<li>Birth date should be less than current date!.</li>';
					}
				}
			}
		}
		if(err){
			var msg ='<div class="error flash"><ul>'+err+'</ul></div>';
			$("#error").html(msg);
			$("#error").slideDown('slow');
			$('#loading_img').html('');
			//slideout();
		} else {
			//document.loginform.submit();
			var signupval='name='+user_login+'&email='+user_email+'&auth='+user_password+'&city='+user_city+'&state='+user_state+'&country='+user_country+'&bdate='+birthdate; 
			$.post("/user/signupsubmit.php", signupval, function(theResponse){
			$("#error").html(theResponse);
			$("#error").slideDown('slow');
			$('#loading_img').html('');
			//slideout();
			}); 	
		}
		
	};

});	


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 mystates(countrycode)
{
	var mycountry='countrycode='+countrycode; 
	$.post("/user/fetchstates.php", mycountry, function(theResponse){
		document.getElementById('mystates').innerHTML=theResponse;
		//alert(theResponse);
	}); 	
	
}