var enterratingtext = 'Enter your comments about this recipe.';
var ie5=document.all&&document.getElementById;
var ns6=document.getElementById&&!document.all;

function clearSearchTabs(i)
{
	document.getElementById('searchtype').value=i;
	if (i!='recipes')
	{
		document.getElementById('st_r').className='inactive';
	}
	if (i!='articles')
	{
		document.getElementById('st_a').className='inactive';
	}
	if (i!='members')
	{
		document.getElementById('st_m').className='inactive';
	}
	if (i!='restaurants')
	{
		document.getElementById('st_t').className='inactive';
	}
	document.getElementById('query_header').focus();
}

function toggleText(id,a,b)
{
	did = document.getElementById(id);
	if (did.innerHTML==a)
	{
		did.innerHTML=b;
	}
	else
	{
		did.innerHTML=a;
	}
}

function bookmarkAction(id,action,type,subid,siteURL)
{
	//alert(siteURL+'if_bookmark.php?id='+id+'&do='+action+'&type='+type+'&subid='+subid);
  
	window.frames['bookmarkFrame'].location.replace(siteURL+'if_bookmark.php?id='+id+'&do='+action+'&type='+type+'&subid='+subid);
  //var ajax_url = siteURL+'if_bookmark.php?id='+id+'&do='+action+'&type='+type+'&subid='+subid;
  //alert(ajax_url);
  //jQuery.ajax({ type: "POST", url: ajax_url});
}

function shoppinglistAction(id,action,type,servings,siteURL)
{
	//alert(siteURL+'if_bookmark.php?id='+id+'&do='+action+'&type='+type+'&subid='+subid);
	//window.frames['shoppinglistFrame'].location.replace(siteURL+'if_shoppinglist.php?id='+id+'&do='+action+'&type='+type+'&servings='+servings);
	var shoppinglist_url = siteURL+'if_shoppinglist.php?id='+id+'&do='+action+'&type='+type+'&servings='+servings;
	
	jQuery.ajax({ type: "POST", url: shoppinglist_url });
}

function shoppinglistUpdate(siteURL)
{
	var randupdate = Math.floor(Math.random()*100001);
	window.frames['shoppingupdateFrame'].location.replace(siteURL+'if_shoppingupdate.php?do=update&t='+randupdate);
}

function select_all(CheckValue){
	var objCheckBoxes = document.forms['formlist'].elements['selcontacts[]'];
	if(!objCheckBoxes)
	return;

	var countCheckBoxes = objCheckBoxes.length;
	if(!countCheckBoxes)
		objCheckBoxes.checked = CheckValue;
	else
		for(var i = 0; i < countCheckBoxes; i++)
			objCheckBoxes[i].checked = CheckValue;
}

function onoff(id)
{
	if (document.getElementById(id).style.display=='none')
		changeto = 'block';
	
	if (document.getElementById(id).style.display=='block')
		changeto = 'none';

	document.getElementById(id).style.display=changeto;		
}

function on(id)
{
	document.getElementById(id).style.display='block';		
}

function clearRatingTA()
{
	var dfr = document.forms.ratingform;
	if (dfr.content.value==enterratingtext)
	{
		dfr.content.value='';
	}	
	dfr.content.style.color='#000';
}

function popUp(loc,w,h)
{
    winLeft = 50;
    winTop = 50; 
    newwindow=window.open(loc,'FoodConnect','toolbar=no,border=no,menubar=no,resizable=no,width='+w+',height='+h+', scrollbars=yes,left='+winLeft+',top='+winTop);
	if (window.focus) {newwindow.focus()}
}


function iecompattest(){
return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}


function echeck(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   alert("That is not a valid email address.")
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert("That is not a valid email address.")
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		alert("That is not a valid email address.")
		return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		alert("That is not a valid email address.")
		return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		alert("That is not a valid email address.")
		return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		alert("That is not a valid email address.")
		return false
	 }
	
	 if (str.indexOf(" ")!=-1){
		alert("That is not a valid email address.")
		return false
	 }

	 return true					
}

function validateMenuForm(f)
{	
	if (f.title.value.length < 3)
	{
		alert("Please enter the menu title.")
		f.title.focus();
		return false;
	}
	return true;
}

function validateAlbumForm(f)
{	
	if (f.title.value.length < 3)
	{
		alert("Please enter the album title.")
		f.title.focus();
		return false;
	}
	return true;
}

function validateNewMessageForm(f)
{	
	if (f.title.value.length < 1)
	{
		alert("Please enter the message subject.")
		f.title.focus();
		return false;
	}

	if (f.content.value.length < 3)
	{
		alert("Please enter a message.")
		f.content.focus();
		return false;
	}

	return true;
}

function validateEventForm(f)
{	
	if (f.title.value.length < 3)
	{
		alert("Please enter the event title.")
		f.title.focus();
		return false;
	}

	if (f.eventlocation.value.length < 3)
	{
		alert("Please enter the location of the event.")
		f.eventlocation.focus();
		return false;
	}

	if (f.description.value.length < 3)
	{
		alert("Please enter a brief description of your event.")
		f.description.focus();
		return false;
	}

	return true;
}

function validateNewSubmissionForm(f)
{	
	if (f.title.value.length < 3)
	{
		alert("Please enter the recipe title.")
		f.title.focus();
		return false;
	}

	if (f.description.value.length < 3)
	{
		alert("Please enter a brief description of your recipe.")
		f.description.focus();
		return false;
	}

	if (parseInt(f.time_prep.value) < 1)
	{
		alert("Please estimate the time necessary to prepare the ingredients before cooking.");
		f.time_prep.focus();
		return false;
	}

	if (parseInt(f.time_cook.value) < 1)
	{
		alert("Please estimate the total amount of cooking time.");
		f.time_cook.focus();
		return false;
	}

	if (f.servings.value.length < 1)
	{
		alert("Please estimate the total number of servings you'll get from this recipe.");
		f.servings.focus();
		return false;
	}

	if (f.ingredients.value.length < 3)
	{
		alert("Please enter the recipe ingredients, one per line.");
		f.ingredients.focus();
		return false;
	}

	if (f.method.value.length < 3)
	{
		alert("Please enter the recipe ingredients, one per line.");
		f.ingredients.focus();
		return false;
	}
	return true;
}


function validateNewSubmissionFormRestaurant(f)
{	
	if (f.title.value.length < 3)
	{
		alert("Please enter the restaurant title.")
		f.title.focus();
		return false;
	}

	if (f.city.value.length < 3)
	{
		alert("Please enter the city.")
		f.city.focus();
		return false;
	}

	if (f.state.value.length < 2)
	{
		alert("Please enter the state.")
		f.state.focus();
		return false;
	}

	if (f.description.value.length < 3)
	{
		alert("Please enter a brief description of the restaurant.")
		f.description.focus();
		return false;
	}

	return true;
}


function validateFeedbackForm(f)
{	
	if (f.name.value.length < 3)
	{
		alert("Please enter your name.");
		f.name.focus();
		return false;
	}

	var emailID=f.email;
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please enter your email address.")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.focus()
		return false
	}

	

	if (f.content.value.length < 3)
	{
		alert("Please enter a message.");
		f.password.focus();
		return false;
	}

	return true;
}


function validateContactsForm(f)
{	
	if (f.email.value.length < 1)
	{
		alert("Please enter your email address.")
		f.email.focus();
		return false;
	}

	if (f.password.value.length < 3)
	{
		alert("Please enter your password.");
		f.password.focus();
		return false;
	}
	return true;
}

function validateProfileDetailsForm(f)
{	
	if (f.name_given.value.length < 1)
	{
		alert("Please enter your first name.")
		f.name_given.focus();
		return false;
	}

	if (f.name_surname.value.length < 2)
	{
		alert("Please enter your last name.")
		f.name_surname.focus();
		return false;
	}

	if (f.name_display.value.length < 4)
	{
		alert("Please enter the name to display as your Food Connect ID.")
		f.name_display.focus();
		return false;
	}

	return true;
}

function validateProfilePictureForm(f)
{	
  // if (f.elements[2].value.length < 3)
  if($('#fileupload').val() == '')
	{
		alert("Please choose a picture to upload, or select a default image.");
		f.elements[2].focus();
		return false;
	}
	
	if($("input[@name='default_image']:checked").length == 0)
	{
	  alert("Please choose a picture to upload, or select a default image.");
		f.elements[2].focus();
		return false;
	}
	
	return true;
}

function validateProfileProfileForm(f)
{	
	return true;
}

function validateProfileSettingsForm(f)
{	
	return true;
}

function validateProfilePasswordForm(f)
{	
	if (f.ptest_pl.value.length < 4)
	{
		alert("Please enter your current password.");
		f.ptest_pl.focus();
		return false;
	}

	var emailID=f.email;
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please enter your email address.")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.focus()
		return false
	}

	if (emailvalid==false)
	{
		alert("Please check your email address.");
		return false;
	}

	if (f.newpassword.value.length > 0 && f.newpassword.value.length < 4)
	{
		alert("Please enter a new password.");
		f.newpassword.focus();
		return false;
	}

	if (f.newpassword.value.length > 0 && (f.newpassword.value != f.newpassword_confirm.value) )
	{
		alert("Make sure the two passwords match.");
		f.newpassword.value=''; f.newpassword_confirm.value='';
		f.newpassword.focus();
		return false;
	}

  if (passvalid==false)
	{
		alert("Please check your password strength.");
		return false;
	}
	//f.ptest.value=strmd5(f.ptest_pl.value);
  return true;
}

function validateLoginForm(f)
{
	var emailID=f.email;
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please enter your email address.")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.focus()
		return false
	}
	if (f.pp.value.length < 4)
	{
		alert("Please enter your password.");
		f.pp.focus();
		return false;
	}
	f.password.value=strmd5(f.pp.value);
	f.pp.value='';
	return true;
}

function validateRegisterForm(f)
{
  if (f.name_given.value.length < 1)
	{
		alert("Please enter your first name.");
		f.name_given.focus();
		return false;
	}

	if (f.name_surname.value.length < 2)
	{
		alert("Please enter your last name.");
		f.name_surname.focus();
		return false;
	}

	if (f.country.value == '' || f.country.value == 'nocountry')
	{
		alert("Please select your country of residence.");
		f.country.focus();
		return false;
	}
	
  if(f.region.value == '' || f.region.value == 'noregion')
  {
  	alert("Please select your region of residence.");
		f.region.focus();
		return false;
  }
  
	// added the check for the city field [abhijit]
	if (f.city.value.length<1)
	{
		alert("Please enter your city.");
		f.city.focus();
		return false;
	}
	
	var emailID=f.email;
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please enter your email address.")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.focus()
		return false
	}
	
	if (emailvalid==false)
	{
		alert("Please check your email address.");
		return false;
	}

  if (f.email.value != f.email_confirm.value)
  {
    alert("Make sure the two new emails match.");
    f.email.focus();
    return false;
  }
  
	if (f.password.value.length < 4)
	{
		alert("Please enter a password.");
		f.password.focus();
		return false;
	}

	if (f.password.value != f.password_confirm.value)
	{
		alert("Make sure the two passwords match.");
		f.password.value=''; f.password_confirm.value='';
		f.password.focus();
		return false;
	}

	if (passvalid==false)
	{
		alert("Please check your password strength.");
		return false;
	}

	//if (f.captcha.value.length<5)
	//{
	//	alert("Please enter the security code to verify you aren't a robot.");
	//	f.captcha.focus();
	//	return false;
	//}

  if (!f.agree.checked)
	{
		alert("Do you agree with the Terms of Service?");
		f.agree.focus();
		return false;
	}	
	
	return true;
}




function validateAccountForm(f)
{

	if (f.firstName.value.length < 1)
	{
		alert("Please enter your first name.");
		f.firstName.focus();
		return false;
	}

	if (f.lastName.value.length < 2)
	{
		alert("Please enter your last name.");
		f.lastName.focus();
		return false;
	}

	if (f.address1.value.length < 8)
	{
		alert("Please enter your street address.");
		f.address1.focus();
		return false;
	}

	if (f.city.value.length < 3)
	{
		alert("Please enter the name of your city.");
		f.city.focus();
		return false;
	}

	if (f.state.options[f.state.selectedIndex].value=='')
	{
		alert("Please select your state or province.");
		f.state.focus();
		return false;
	}

	if (f.zip.value.length < 3)
	{
		alert("Please enter your zip / zip code.");
		f.zip.focus();
		return false;
	}

	if (f.country.options[f.country.options.selectedIndex].text=='')
	{
		alert("Please select your country of residence.");
		f.country.focus();
		return false;
	}

	if (f.city.value.length < 3)
	{
		alert("Please enter the name of your city.");
		f.city.focus();
		return false;
	}

	if (f.phone.value.length < 10)
	{
		alert("Please enter your telephone number.");
		f.phone.focus();
		return false;
	}
	
	var emailID=f.email;
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please enter your email address.")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.focus()
		return false
	}

	if (emailvalid==false)
	{
		alert("Please check your email address.");
		return false;
	}

	if (f.password_current.value.length < 4)
	{
		alert("Please enter your current password.");
		f.password_current.focus();
		return false;
	}

	if (f.password.value.length > 0)
	{
		if (f.password.value.length < 4)
		{
			alert("Please enter a new password at least 5 characters long.");
			f.password.focus();
			return false;
		}

		if (f.password.value != f.password_confirm.value)
		{
			alert("Make sure the two new passwords match.");
			f.password.value=''; f.password_confirm.value='';
			f.password.focus();
			return false;
		}
	}
	
	if (passvalid==false)
	{
		alert("Please check your password strength.");
		return false;
	}
	return true;
}
