function checkLength(theform, field, message)
{
  if( field.value.length < 3 ) {
 	  alert(message);
  	field.focus();
	 	return (false);
  }
	return (true);
}

function checkPasswords(f)
{
  if (f.f_pass1.value != f.f_pass2.value) {
 	  alert("Password fields doesn't match, please reenter your desired password");
 	  f.f_pass1.value = "";
 	  f.f_pass2.value = "";
 	  f.f_pass1.focus();
	 	return (false);
  }
  if (f.f_pass1.value.length > 0) {
    if ((f.f_pass1.value.length < 6 ) || (f.f_pass2.value.length < 6)) {
   	  alert("The password must have 6 characters or more");
   	  f.f_pass1.value = "";
   	  f.f_pass2.value = "";
   	  f.f_pass1.focus();
  	 	return (false);
    }
  }
	return (true);
}


function checkEmail(theform, field, message) 
{
  if (field.value == "" || field.value.indexOf ('@', 0) == -1 || field.value.length < 8) { 
     alert(message); 
     field.focus(); 
     return (false);
  } 
  return (true);
}

// /control/index.php
function checkLoginForm(f)
{
  if (!checkLength(f, f.f_login, "You must provide your school name. If you don't yet, please, contact to webmaster@inforteach.com.")) return;
  if (!checkLength(f, f.f_password, "You must provide a valid password")) return;
	f.submitbutton.disabled = true;
	f.submit();
	return (true);
}

// /control/school_info.php
function checkGeneralSchoolInfoForm(f)
{
  if (!checkLength(f, f.f_schooltitle, "Please specify a valid school name.")) return;
  if (!checkLength(f, f.f_address, "Please specify a valid address.")) return;
  if (!checkLength(f, f.f_location, "Please specify a valid location (City or Town).")) return;
  if (!checkLength(f, f.f_phone1, "Please specify a valid phone.")) return;
  if (!checkEmail(f, f.f_email, "Please specify a valid e-mail address. We need your e-mail address in order to mail your important changes or you password if you've forgot it.")) return;
  if (f.f_country.selectedIndex == 0)
  {
    alert("Please select a country");
    f.f_country.focus();
    return;  
  }
	f.submitbutton.disabled = true;
	f.submit();
	return (true);
}

function checkCharacsForm(f)
{
  if (f.f_closestcity.value == '')
  {
    alert("Please select the closest capital city to your school");
    f.f_closestcity.focus();
    return;  
  }
	f.submitbutton.disabled = true;
	f.submit();
	return (true);
}


function checkSettingsForm(f)
{
  if (!checkPasswords(f)) return;
  if ((f.f_emailnewsinforteach.checked) && (!checkEmail(f, f.f_email, "Please specify a valid e-mail address."))) return;
	f.submitbutton.disabled = true;
	f.submit();
	return (true);
}

function intelligentSubmit(f)
{
	f.submitbutton.disabled = true;
	f.submit();
	return (true);
}

function goLocation(location)
{
  document.location=location;
  return (true);
}

function doActionCoursesForm(f, string_action, nCourseId, bSaveButton, nCourseIndex)
{
  var i;
  f.f_actionid.value = string_action;
  f.f_courseid.value = nCourseId;
  if ( (string_action == 'delete_row') && (confirm('Are you sure to delete the course number ' + nCourseIndex + '?') == false) ) return (false);
	if (bSaveButton) f.save_button.disabled = true;
  f.submit();
  return (true);
}

function replaceTitle(f1, f2)
{
  if (f2.selectedIndex != 0) f1.value = f2.value;
  f2.selectedIndex = 0;
}

function goToCourse(f)
{
  document.location = "#C" + f.f_selectcourse.value;
  f.f_selectcourse.selectedIndex = 0;
  return (true);
}

function confirmLink(message, url)
{
  if (confirm(message) == false) return (false);
  document.location = url;
  return (true);
}
