var host='http://'+document.location.hostname+'/';
function checkUserName(userName)
{

if(userName!='')
	{
     
	  new Ajax.Updater('', host+'login/checkuser?userName='+userName,{asynchronous:true, evalScripts:true,onComplete: 
		               function(request){
		                 
		                   if(!request.responseText)
		                     {
						      alert('The user name already exists in our database. Please enter a different user name');
							  $('userFlag').value='false';
						     }
							 else
								 $('userFlag').value='true';

							}});
    }


}

function checkUserEmail(userEmail)
{
if(userEmail!='')
	{
    
	  new Ajax.Updater('', host+'login/checkuser?userEmail='+userEmail,{asynchronous:true, evalScripts:true,onComplete: 
		               function(request){
		                 
		                   if(!request.responseText)
		                     { 
						      alert('The email address already exists in our database. Please enter a different email address');
							   $('emailFlag').value='false';
						     }
							 else
							   $('emailFlag').value='true';


							}});
    }

}

function checkUserFlag()
{

var pass = document.getElementById('regpassword').value;
var cpass = document.getElementById('confirmpassword').value;
	if(pass!=cpass)
	{
		alert('Password and Confirm Password not matched.');
		document.getElementById('confirmpassword').select();
		document.getElementById('confirmpassword').focus();
		return false;
	}


var date = document.getElementById('date').value;
var month = document.getElementById('month').value;

 if((parseInt(month)==2 && parseInt(date)>29) || (parseInt(month)==4 && parseInt(date)>30) || (parseInt(month)==6 && parseInt(date)>30) || (parseInt(month)==9 && parseInt(date)>30) || (parseInt(month)==11 && parseInt(date)>30))
 {
	  alert('Please select a valid date of birth');
	  return false;
 }

	
if($("userFlag").value=='true' && $("userFlag").value=='emailFlag') 
	return true;
else
	{
	if($("userFlag").value=='false')
		{
			alert('The user name already exists in our database. Please enter a different user name');
			return false;
		}
   else{

	  if($("emailFlag").value=='false')
		{
			alert('The email address already exists in our database. Please enter a different email address');
			return false;
		}
          else
	     return true;
   
}
	}


}

function updatePopularDiv(type)
{     
	  if(type=='popular')
	   {
         $('popular_link').className='top_row_recipeindex01_active';
	     $('user_link').className='top_row_recipeindex02';
	   }
	  else
	   {
         $('popular_link').className='top_row_recipeindex01';
	     $('user_link').className='top_row_recipeindex02_active';
	   }

	  new Ajax.Updater('popular_div', host+'index/getpopulardata?type='+type,{asynchronous:true, evalScripts:true});
}

function checkImageData()
{
var valid_extensions = /(.jpg|.jpeg|.gif|.bmp)$/i;
var error=0;
var fld
for(var count=1;count<8;count++)
	{   fld=document.getElementById('recipe_image_'+count);
		
		if (!(valid_extensions.test(fld.value)) && (fld.value!=''))
			error=1;
	}

if(error==0)
	return true;
else
	{
    alert("Please upload only images:  \nPlease select a new file to upload.");
	return false;
	}
}

function checkImageFormat(img)
{
var valid_extensions = /(.jpg|.jpeg|.gif|.bmp)$/i;
var error=0;
var fld=document.getElementById(img);
  		


if(img=='profile_image')
	{
	var pass = document.getElementById('password').value;
    var cpass = document.getElementById('confirmpassword').value;
	if(pass!=cpass && pass!='')
	{
		alert('Password and Confirm Password not matched.');
		document.getElementById('confirmpassword').select();
		document.getElementById('confirmpassword').focus();
		return false;
	}
	var date = document.getElementById('date').value;
	var month = document.getElementById('month').value;

	 if((parseInt(month)==2 && parseInt(date)>29) || (parseInt(month)==4 && parseInt(date)>30) || (parseInt(month)==6 && parseInt(date)>30) || (parseInt(month)==9 && parseInt(date)>30) || (parseInt(month)==11 && parseInt(date)>30))
	 {
		  alert('Please select a valid date of birth');
		  return false;
	 }
	}
	else
	{
	  if(fld.value=='')
		  error=1;
	
	}

if (!(valid_extensions.test(fld.value)) && (fld.value!='') && error==0)
	error=1;
	

if(error==0)
	return true;
else
	{
    alert("Please upload only images:  \nPlease select a new file to upload.");
	return false;
	}
}
function checkForgotEmail()
{
	var forgotEmail = document.getElementById('email_address').value;
	if(forgotEmail=='')
	{
		alert('Email address should not be blank.');
		document.getElementById('email_address').select();
		document.getElementById('email_address').focus();
		return false;
	}

}

function checkImageFormatRecipe()
{
var valid_extensions = /(.jpg|.jpeg|.gif)$/i;
var fld=document.getElementById('recipe_image');
if(fld.value!='' && !(valid_extensions.test(fld.value)))
{
  alert("Please upload only images:  \nPlease select a new file to upload.");
  return false;
}
else
  return true; 

}