// reg.js : script for the current students registration
//

function checkEmailAddress( s ) //returns 0 if OK
{

  if ( s.length == 0
    || s.indexOf(" ") > -1
    || s.indexOf("@") < 1
    || s.indexOf("@") == s.length - 1 )
  {
    return 1
  }
  return 0
}

function checkStudentInfo( theForm )
{
  err = ''
  if ( theForm.student_last_name.value.length  == 0 ) err += 'student\'s last name, '
  if ( theForm.student_first_name.value.length == 0 ) err += 'student\'s first name, '
  if ( err.length > 0 )
  {
	alert( 'Missing ' + err.substring(0, err.length-2) + '.' )
	return false
  }
  return true
}


function update_id_list(id)
{
	alert("erer" + document.regForm.checkboxes.length)
	var max = document.regForm.student_count.value;

        document.regForm.update_id_list.value = ""
	for (var idx = 0; idx < max; idx++)
	{
		if (document.regForm.checkboxes[idx].checked == true)
		{
			if (document.regForm.update_id_list.value == "" )
			{
				document.regForm.update_id_list.value = document.regForm.checkboxes[idx].value
			}
			else
			{
			   document.regForm.update_id_list.value =  document.regForm.update_id_list.value + ',' + document.regForm.checkboxes[idx].value
			}

		}
	}
	return true;

}
function checkRegInfo( theForm )
{
	err = 'ERR'
	if( theForm.remove_class.value == 'Y' )
	{ 
	    var max = document.regForm.class_count.value;
            if ( max <=1 )
            {
		var max1 = theForm.checkboxes.value;
                //alert ( "TEST tetet" + max + max1 )
               theForm.update_id_list.value = max1
            }
            else
            {
		var max = theForm.checkboxes.length;
		theForm.update_id_list.value = ""
		for (var idx = 0; idx < max; idx++)
		{
			if (theForm.checkboxes[idx].checked == true)
			{
				if (theForm.update_id_list.value == "" )
				{
					theForm.update_id_list.value = theForm.checkboxes[idx].value
				}
				else
				{
				   theForm.update_id_list.value =  theForm.update_id_list.value + ',' + theForm.checkboxes[idx].value
				}

			}
		}
		if ( theForm.update_id_list.value.length == 0 )
		{
			alert( 'If you need to remove a class, please click at least a class in removal column.' )
			return false
		}
		
		theForm.action="reg_family.php";
		theForm.submit();
	        return true;
	    }

	}
	if( theForm.add_class.value == 'Y' )
	{
		theForm.action="reg_admin_class.php";
		theForm.submit();
	}
	if( theForm.reg_finish.value == 'Y' )
	{
		theForm.action="reg_admin_final.php";
		theForm.submit();
	}
        return true
}

function checkInfo( theForm )
{
  err = ''
  if ( theForm.address.value.length    == 0 ) err += 'address, '
  if ( theForm.city.value.length       == 0 ) err += 'city, '
  if ( theForm.state.value.length  == 0 ) err += 'state, '
  if ( theForm.zip_code.value.length  == 0 ) err += 'zip code, '
  if ( theForm.phone.value.length      == 0 ) err += 'home phone, '
  if ( theForm.email.value.length      == 0 ) err += 'email, '
  if ( theForm.email1.value.length      == 0 ) err += 'retype email, '
  if ( err.length > 0 )
  {
    alert( 'Missing ' + err.substring(0, err.length-2) + '.' )
    return false
  }
  if ( checkEmailAddress(theForm.email.value) )
  {
    alert( 'Invalid email address.' )
    return false
  }
    
  if ( theForm.email.value != theForm.email1.value )
  {
    alert( 'Email addresses did not match!')
    return false
  
  }
  err = ''
  if ( theForm.f_fname.value.length    == 0 ) err += 'Father First Name, '
  if ( theForm.f_lname.value.length     == 0 ) err += 'Father Last Name, '
  if ( theForm.f_office_phone.value.length  == 0 ) err += 'Father Office Phone, '
  err1 = ''
  if ( theForm.m_fname.value.length    == 0 ) err1 += 'Mother First Name, '
  if ( theForm.m_lname.value.length     == 0 ) err1 += 'Mother Last Name, '
  if ( theForm.m_office_phone.value.length  == 0 ) err1 += 'Mother Office Phone, '
  if ( err.length > 0 && err1.length > 0)
  {
    alert( 'Missing ' + err.substring(0, err.length-2) + ', or missing ' + err1.substring(0, err1.length-2) )
    return false
  }
  if ( theForm.password1.value.length  > 0 )
  {
	  if(theForm.password1.value.length < 8)
	  {
	    alert( 'Password is less than 8 characters! Please retype password again.')
	    return false
	  }
	  if ( theForm.password1.value != theForm.password2.value )
	  {
	    alert( 'Password do not match, please retype password again.')
	    return false
	  }
  }
  err1 = ''
  if ( theForm.s_fname.value.length    == 0 ) err1 += 'Student First Name, '
  if ( theForm.s_lname.value.length     == 0 ) err1 += 'Student Last Name, '
  if ( err1.length > 0)
  {
    alert( 'Missing ' + err1.substring(0, err1.length-2) )
    return false
  }
  return true
}

function desc_pop(t_desc, t_image, t_name)
{
  var generator=window.open('','name','height=350,width=450, scrollbars=yes,resizable=yes');

  generator.document.write('<html><head><title>Popup</title>');
  generator.document.write('<link rel="stylesheet" href="style.css">');
  generator.document.write('</head><body>');
  generator.document.write('<table><tr><td><img width=90 height=120 src=' + t_image + '></td></tr>');
  generator.document.write('<tr><td>' + t_name + '</td></tr></table>');
  generator.document.write('<tr><td>' + t_desc + '</td></tr></table>');
  generator.document.write('<p><a href="javascript:self.close()">Close the window</a></p>');
  generator.document.write('</body></html>');
  generator.document.close();
}