var url_home = "http://www.myfreeonlinerecipe.com/";
//var url_home = "http://localhost/myfreeonlinerecipe.com/";
function trim(val) {
	var nstr = "";
	for(var i=0;i<val.length;i++) {
		if(val[i]==" ") {}
		else {nstr += val[i];}
	}
	return nstr;
}

function NameValid(urname,fieldname) {	
	if(!valid_required(urname))	{		
		alert(fieldname+ " is required field !");return false;
	}
	var iChars = "!@#$%^&*()+=-[]\\\';,/{}|\":<>?1234567890";	
	for (var i = 0; i < urname.length; i++) {
		if (iChars.indexOf(urname.charAt(i)) != -1) {
			alert ("Invalid characters Found");return false;
		}
	}return true;	
}

function ValidPhone(aphone) {
	var valid = "0123456789";
	if(aphone=="")	{	
		alert("Phone number is required field!");return false;
	}	
	if(aphone.length < 10)	{	
		alert("Invalid phone number! Please try again.");return false;
	}	
	var iChars = "0123456789";	
	for (var i=0; i < aphone.length; i++)	{	
		temp = "" + aphone.substring(i, i+1);		
		if (valid.indexOf(temp) == "-1")	{ 		
			alert("Invalid characters in your phone.  Please try again.");return false;
		}
	}return true;	
}

function valid_required(field) {
	if(trim(field)=="") 	{
		return false;
	}return true;	
}
var email_error="";
function EmailValid(emailfield)	{
	var email = emailfield;	
	if(email=="")	{
		email_error = "Email is required field!";
		return false;
	}
	len = email.length;
	if((email.charAt(1)=='@')||(email.charAt(1)=='.'))		{
		email_error = "Invalid Email Please try again!";
		return false;
	}
	if((email.charAt(len-2)=='@')||(email.charAt(len-1)=='.'))	{
		email_error = "Invalid Email Please try again!";
		return false;
	}
	count=0;
	dotcount=0;
	for (i=0; i< email.length; i++)	{
		if(email.charAt(i)=='@')
		count++;
		if(email.charAt(i)=='.')
		dotcount++;
	}		
	if((count !=1)||(dotcount <1))	{
		email_error = "Invalid Email Please try again!";
		return false;
	}			  
	return true
}
$(document).ready(function(){
	$("#start_emb").hide();
	$("#cls,.cls").click(function(){
		$("#start_emb").hide();
		$('#mask').hide();
	}); 
});	

function showSignUp(logtype) {	
	$("#start_emb").slideToggle("slow");
	$("#post").show();
	$("#loginsignupform").show();
	$("#askqns2011").hide();
	$("#messages").hide();
	//$("#signupform").hide();
	$("#fgpwdform").hide();	
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();
	$('#mask').css({'width':maskWidth,'height':maskHeight,backgroundColor:"black"});
	$('#mask').fadeTo("fast",0.8);
	$('#post_title').val('') ;
	$('#summary_description').val('') ;
	if(logtype==1) {
		$("#pup_top_heading1").text("Ask a Question");
		$("#pup_top_heading2").text("Please login or register so we can let you know that your question has been answered by one of our experts.");
	}
	else {
		$("#pup_top_heading1").text("Login/SignUp");
		$("#pup_top_heading2").text("");
	}	
	$("#fgpwdform").hide();
	$("input[name=ltype]").val(logtype);
}
function showLoginBox(logtype) {
	$("#start_emb").slideToggle("slow");
	$("#post").show();
	$("#loginsignupform").show();
	$("#askqns2011").hide();
	$("#messages").hide();
	//$("#signupform").hide();
	$("#fgpwdform").hide();		
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();
	$('#mask').css({'width':maskWidth,'height':maskHeight,backgroundColor:"#black"});
	$('#mask').fadeTo("fast",0.8);
	$('#post_title').val('') ;
	$('#summary_description').val('');
	if(logtype==1) {
		$("#pup_top_heading1").text("Ask a Question");
		$("#pup_top_heading2").text("Please login or register so we can let you know that your question has been answered by one of our experts.");
	}
	else {
		$("#pup_top_heading1").text("Login/SignUp");
		$("#pup_top_heading2").text("");
	}	
	$("#fgpwdform").hide();	
	$("input[name=ltype]").val(logtype);
}

function showLogin() {
	$("#loginsignupform").show();
	$("#fgpwdform").hide();	
}
function showFgPwd() {
	$("#loginsignupform").hide();
	$("#fgpwdform").show();
}
function showAskTheExpert(uid) {
	$("#start_emb").slideToggle("slow");
	$("#post").hide();
	$("#messages").hide();
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();
	$('#mask').css({'width':maskWidth,'height':maskHeight,backgroundColor:"black"});
	$('#mask').fadeTo("fast",0.8);
	$("#askqns2011").show();
	$('#askqns2011').html('<p align="center"><img src="'+url_home+'images/loading.gif" /></p>');
	$.ajax({
		url : url_home+"askqns.php?uid="+uid,
		success : function (data) {	
			document.getElementById("askqns2011").innerHTML = data;
		}
	});	
}
function signUpEmailvalidate(elm) {
	var emailvalue = elm.value;	
	if(EmailValid(emailvalue)) { //validate email id from server (duplicay check)
		$('#loader005').html('Verifying...');//<img src="'+url_home+'images/loading.gif" />
		$.post(url_home+"userAjax.php", { act:"validate_email", email:emailvalue },
		function(data) {
			if(data=="present") {
				elm.style.backgroundColor = "#ffaaaa";
				$("#signup_email_res").html("Email alredy exists.");
			}
			if(data=="ok") {
				elm.style.backgroundColor = "#ffffff";
				$("#signup_email_res").html("");
			}
		});
	}
}

//LOGIN VALIDATION
function validateLogin(formid) {	
	if(!valid_required(formid.email.value))	{	
		$("#login_email_res").html("Please enter the email!");
		formid.email.style.backgroundColor = "#ffaaaa";
		formid.email.focus();
		return false;
	}
	if(!valid_required(formid.password.value))	{		
		$("#login_pwd_res").html("Please enter the password !");
		formid.password.style.backgroundColor = "#ffaaaa";	
		formid.password.focus();
		return false;
	}
	if(formid.ltype.value!=1) {
		//return true;
		var em = formid.email.value;
		var pw = formid.password.value;
		$.post(url_home+"userAjax.php", { act:"login", email: em, password: pw },
		function(data) {
			if(data==0) {				
				$("#login_res").html("Email ID or Password is Invalid");
				formid.password.style.backgroundColor = "#ffaaaa";
				formid.email.style.backgroundColor = "#ffaaaa";
				formid.email.focus();
			}
			else if(data>0) {				
				document.location.href=url_home+"user.php";
			}
			else {;}
		});
		return false;
	}
	
	else {
		var em = formid.email.value;
		var pw = formid.password.value;
		$.post(url_home+"userAjax.php", { act:"login", email: em, password: pw },
		function(data) {
			if(data==0) {				
				$("#login_res").html("Email ID or Password is Invalid");
				formid.password.style.backgroundColor = "#ffaaaa";
				formid.email.style.backgroundColor = "#ffaaaa";
				formid.email.focus();
			}
			else if(data>0) {				
				showAskTheExpert(data);
			}
			else {;}
		});
		return false;
	}
}

//SIGNUP VALIDATION
function validateSignUp(formid) {	
	if(!EmailValid(formid.email.value)) {
		formid.email.style.backgroundColor = "#ffaaaa";
		$("#signup_email_res").html("Invalid email id !");
		formid.email.focus();
		return false;
	}
	else {
		formid.email.style.backgroundColor = "#FFFFFF";
		$("#signup_email_res").html("");
	}
	signUpEmailvalidate(formid.email);	
	if(!valid_required(formid.password.value))	{	
		formid.password.style.backgroundColor = "#ffaaaa";
		$("#signup_pwd_res").html("Please enter the password !");		
		formid.password.focus();
		return false;
	}
	else {
		formid.password.style.backgroundColor = "#FFFFFF";
		$("#signup_pwd_res").html("");
	}
	if(formid.ltype.value!=1) {
		//return true;
		var emailid = formid.email.value;
		var pw = formid.password.value;
		var act_value = formid.act.value;
		$.post(url_home+"userAjax.php", { act:act_value, password: pw, email:emailid },
		function(data) {
			if(data!="" && data>0) {				
				document.location.href=url_home+"signup-result.php";
			}
		});
		return false;
	}
	if(formid.ltype.value==1) {
		var emailid = formid.email.value;
		var pw = formid.password.value;
		var act_value = formid.act.value;
		$.post(url_home+"userAjax.php", { act:act_value, password: pw, email:emailid },
		function(data) {			
			if(data!="" && data>0) {
				newsignup = true;
				showAskTheExpert(data);				
			}
		});
		return false;
	}return false;
}

//FORGOT PASSWORD VALIDATION
function validateFgPwd(formid) {	
	if(!EmailValid(formid.email.value)) {
		formid.email.focus();
		return false;
	}
	return true;
}

function showMessage(texts) {
	$("#start_emb").slideToggle("slow");
	$("#post").hide();
	$("#askqns2011").hide();
	$("#messages").hide();
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();
	$('#mask').css({'width':maskWidth,'height':maskHeight,backgroundColor:"#black"});
	$('#mask').fadeTo("fast",0.8);
	$('#post_title').val('') ;
	$('#summary_description').val('') ;
	$("#messages").show();
	$("#messages_conets").show();
	$("#pup_heading15").html("Message");
	$("#messages_conets").html(texts);	
}
