
// (c) copyright 2002 by Joost Bentvelsen, ABROGO B.V.
// ab_formcheck.js versie 1.3 door Joost Bentvelsen (jbentvelsen@abrogo.nl)


// Versiehistorie:
// ---------------
// 1.7: 25-04-2005 - RT - Password controle toegevoegd en type ab_length t.b.v. lengtecontrole pasword
// 1.6: 31-12-2004 - JB - Meertaligheid o.b.v. constante variabele (kLanguage) in paginaheader
// 1.5: 30-12-2002 - JB - function InitForms uitgebreid met vooraangsmodus tussen value en ab_value
// 1.4: 04-12-2002 - JB - function InitForms toegevoegd voor meta generieke value instelling mbv JS
// 1.3: 26-07-2002 - RT - loops in checkphone en checknumber obv 'j' ivm infinite loop (i)
// 1.2: 10-07-2002 - JB - ab_required waarde 'yes' geven i.v.m. Mac
// 1.1: 07-07-2002 - JB - Functies URL en NUMBER toegevoegd + diverse aanpassingen bestaande functies

var kLanguage = "";
var nPasswordLength = 6;
var gRadioArray = new Array(0);

function InitForms(theModus)
{
 if(!theModus) theModus = 1;  // ab_value heeft standaard voorrang op de html-value, in modus 2 wordt de html-value als standaard aangehouden.

	for(i=document.forms.length-1; i>=0; i--)
		 for(j=document.forms[i].length-1; j>=0; j--)
		  	if(document.forms[i][j].ab_value)
			   	if(document.forms[i][j].type)
				   {
				   	 if(document.forms[i][j].type == "radio" || document.forms[i][j].type == "checkbox")
					    {
					     	if(theModus != 2 || document.forms[i][j].selectedIndex < 0)
					     	  if(document.forms[i][j].value == eval(document.forms[i][j].ab_value))
					   	   	  document.forms[i][j].checked = true;
					    }
					    else if(document.forms[i][j].type.indexOf('select') >= 0)
					    {
					     	if(theModus != 2 || document.forms[i][j].selectedIndex < 0)
						     	for(k=0 ; k<document.forms[i][j].options.length ; k++)
								     	if(document.forms[i][j].options[k].value == eval(document.forms[i][j].ab_value))
									     		document.forms[i][j].options[k].selected = true;
					    }
					    else
         {
           if(theModus == 2)
           {
             if(document.forms[i][j].value == "")
               document.forms[i][j].value = eval(document.forms[i][j].ab_value);
           }
           else
             document.forms[i][j].value = eval(document.forms[i][j].ab_value);
         }
				   }
}



function CheckForm(theForm) // Check ALL FIELDS (elements) in the given Form
{
  var success = true;

  for(i=0 ; i<theForm.length; i++)
  {
    if(theForm[i].ab_required == "yes") success = CheckField(theForm[i]);
    if(success == false) return false;
  }


  for(i=0 ; i<gRadioArray.length ; i++)
  {
    if(gRadioArray[i][1] != true)
    {
    	AlertMessage(1001);
      return false;
    }
  }
  return true;
}



function CheckField(theElement) // Check the GIVEN INPUT ELEMENT to have the right format
{
  if(theElement.type.toLowerCase() == "radio" && theElement.ab_required == "yes") return RegisterRadioButton(theElement);
  else if(theElement.ab_format.toLowerCase() == "email")  return CheckEmailFormat(theElement);
  else if(theElement.ab_format.toLowerCase() == "zip")    return CheckZipFormat(theElement);
  else if(theElement.ab_format.toLowerCase() == "date")   return CheckDateFormat(theElement);
  else if(theElement.ab_format.toLowerCase() == "time")   return CheckTimeFormat(theElement);
  else if(theElement.ab_format.toLowerCase() == "phone")  return CheckPhoneFormat(theElement);
  else if(theElement.ab_format.toLowerCase() == "url")    return CheckUrlFormat(theElement);
  else if(theElement.ab_format.toLowerCase() == "number") return CheckNumberFormat(theElement);
  else if(theElement.ab_format.toLowerCase() == "password") return CheckPasswordFormat(theElement);  
  else return CheckTextFormat(theElement);
}



function RegisterRadioButton(theElement)
{
    var aGroupDefined = 0;

    for(j=0 ; j < gRadioArray.length ; j++)
    {
      if(gRadioArray[j][0] == theElement.name.toLowerCase() && aGroupDefined == 0)
      {
        aGroupDefined = 1;

        if(theElement.checked == true )
          gRadioArray[j][1] = true;
      }
    }

    if(aGroupDefined == 0)
    {
      gRadioArray.length += 1;
      gRadioArray[gRadioArray.length - 1] = new Array(theElement.name.toLowerCase(), theElement.checked, theElement.ab_label);
    }

    return true;
}



function CheckEmailFormat(theField)
{
  if(theField.value.length == 0 && theField.ab_required != "yes") return true;
  else
  {
    var aPos1 = theField.value.indexOf('@')
    var aPos2 = theField.value.indexOf('.', aPos1+1)

    if( aPos1 <= 0                                           ||
        aPos2 <= 0                                           ||
        aPos2 - aPos1 == 1                                   ||
        theField.value.indexOf(" ") >= 0                     ||
        theField.value.charAt(0) == "."                      ||
        theField.value.charAt(theField.value.length - 1) == "."  
      )
    {
     	AlertMessage(1002, theField);
      theField.select();
      return false;
    } else return true;
  }
}



function CheckZipFormat(theField)
{
  var PermittedNumbers = "0123456789";
  var PermittedLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

  if(theField.value.length == 0 && theField.ab_required != "yes") return true;
  else
  {
    // Restyling the value to consistent format.
    theField.value = theField.value.toUpperCase();
    if(theField.value.length == 6) theField.value = theField.value.substring(0,4) + " " + theField.value.substring(4,6);

    if( theField.value.length != 7                              ||
        PermittedNumbers.indexOf(theField.value.charAt(0)) < 0  || 
        PermittedNumbers.indexOf(theField.value.charAt(1)) < 0  || 
        PermittedNumbers.indexOf(theField.value.charAt(2)) < 0  || 
        PermittedNumbers.indexOf(theField.value.charAt(3)) < 0  ||
        theField.value.charAt(4) != " "                         ||
        PermittedLetters.indexOf(theField.value.charAt(5)) < 0 ||
        PermittedLetters.indexOf(theField.value.charAt(6)) < 0 
      )
    {
    	AlertMessage(1004, theField);
      theField.select();
      return false;
    } else return true;
  }
}



function CheckDateFormat(theField)
{
  var aFormat = true;
  var PermittedNumbers = "0123456789";

  if(theField.value.length == 0 && theField.ab_required != "yes") return true;
  else
  {
    // Restyling the value to consistent format.
    theField.value = theField.value.replace('/','-');
    theField.value = theField.value.replace('/','-');

    aPos1 = theField.value.indexOf('-');
    aPos2 = theField.value.indexOf('-', aPos1+1);

    if(aPos1 < 0 || aPos2 < 0) aFormat = false;

    aDate  = theField.value.substring(0, aPos1);
    aMonth = theField.value.substring(aPos1+1, aPos2);
    aYear  = theField.value.substring(aPos2+1, theField.value.length);

    if(aDate.length == 1) aDate = "0" + aDate;
    if(aMonth.length == 1) aMonth = "0" + aMonth;
    if(aYear.length == 2)
    {
       if(Number(aYear) >= 80) aYear = "19" + aYear;
       else aYear = "20" + aYear;
    }
    if(aFormat) theField.value = aDate + "-" + aMonth + "-" + aYear;

    if( !aFormat                                                || 
        PermittedNumbers.indexOf(theField.value.charAt(0)) < 0  || 
        PermittedNumbers.indexOf(theField.value.charAt(1)) < 0  || 
        PermittedNumbers.indexOf(theField.value.charAt(3)) < 0  || 
        PermittedNumbers.indexOf(theField.value.charAt(4)) < 0  || 
        PermittedNumbers.indexOf(theField.value.charAt(6)) < 0  || 
        PermittedNumbers.indexOf(theField.value.charAt(7)) < 0  || 
        PermittedNumbers.indexOf(theField.value.charAt(8)) < 0  || 
        PermittedNumbers.indexOf(theField.value.charAt(9)) < 0  || 
        Number(aDate)  > 31 || Number(aDate) < 1                || 
        Number(aMonth) > 12 || Number(aMonth) < 1               || 
        Number(aYear)  > 9999 || Number(aYear) < 0               
        )
    {
    	AlertMessage(1005, theField);
      theField.select();
      return false;
    } else return true;
  }
}



function CheckTimeFormat(theField)
{
  var aFormat = true;
  var PermittedNumbers = "0123456789";

  if(theField.value.length == 0 && theField.ab_required != "yes") return true;
  else
  {
    // Restyling the value to consistent format.
    theField.value = theField.value.replace('.',':');

    aPos = theField.value.indexOf(':');

    if(aPos < 0) aFormat = false;

    aHour   = theField.value.substring(0, aPos);
    aMinute = theField.value.substring(aPos+1, theField.value.length);

    if(aHour.length == 1)   aHour = "0" + aHour;
    if(aMinute.length == 1) aMinute = "0" + aMinute ;

    if(aFormat) theField.value = aHour+ ":" + aMinute;

    if( !aFormat                                                || 
        PermittedNumbers.indexOf(theField.value.charAt(0)) < 0  || 
        PermittedNumbers.indexOf(theField.value.charAt(1)) < 0  || 
        PermittedNumbers.indexOf(theField.value.charAt(3)) < 0  || 
        PermittedNumbers.indexOf(theField.value.charAt(4)) < 0  || 
        Number(aHour)  >= 24 || Number(aHour) < 0               || 
        Number(aMinute) >= 60 || Number(aMinute) < 0               
        )
    {
	AlertMessage(1006, theField);
      theField.select();
      return false;
    } else return true;
  }
}



function CheckPhoneFormat(theField)
{
  var PermittedNumbers = "0123456789";
  var aFormat = true;
  var aPhoneString = "";

  if(theField.value.length == 0 && theField.ab_required != "yes") return true;
  else
  {
    // Restyling the value to consistent format.
    for(j=0 ; j<theField.value.length ; j++)
      if( PermittedNumbers.indexOf(theField.value.charAt(j)) >= 0)
        aPhoneString = aPhoneString + theField.value.charAt(j);

    if( aPhoneString.length != 10 || aPhoneString.charAt(0) != '0') aFormat = false;

    if(aFormat)
    {
      if(aPhoneString.substring(0,2) == "06")
        theField.value = aPhoneString.substring(0,2) + " - " + aPhoneString.substring(2,4) + " " +  aPhoneString.substring(4,6) + " " +  aPhoneString.substring(6,8) + " " +  aPhoneString.substring(8,10);

      return true;
    }
    else
    {
	AlertMessage(1007, theField);
      theField.select();
      return false;
    }
  }
}



function CheckUrlFormat(theField)
{
  if(theField.value.length == 0 && theField.ab_required != "yes") return true;
  else
  {
    // Restyling value
    if(theField.value.substring(0,3).toLowerCase() == "www") theField.value = "http://" + theField.value;
    if(theField.value.substring(0,3).toLowerCase() == "ftp") theField.value = "ftp://" + theField.value;

    if( theField.value.indexOf("://") < 0                    ||
        theField.value.indexOf(".") < 0                    ||
        theField.value.indexOf(" ") >= 0                     ||
        theField.value.charAt(0) == "."                      ||
        theField.value.charAt(theField.value.length - 1) == "."  
      )
    {
    	AlertMessage(1008, theField);
      theField.select();
      return false;
    } else return true;
  }
}



function CheckNumberFormat(theField)
{
  var PermittedNumbers = "0123456789,";
  var aNumber = true;

  if(theField.value.length == 0 && theField.ab_required != "yes") return true;
  else
  {
  	if(theField.value.length == 0) aNumber = false;
    // Restyling value
    if(theField.type.indexOf("select") < 0) theField.value = theField.value.replace(".", ",")

    for(j=0 ; j<theField.value.length ; j++)
      if( PermittedNumbers.indexOf(theField.value.charAt(j)) < 0)
        aNumber = false;

    if( !aNumber )
    {
    	AlertMessage(1009, theField);
      if(theField.type.indexOf("select") < 0) theField.select();
      else theField.focus();
      return false;
    } else return true;
  }
}



function CheckTextFormat(theField)
{
  if(theField.value.length == 0 && theField.ab_required != "yes") return true;
  else
  {
    // Restyling value
    if(theField.type.indexOf("select") < 0) theField.value = theField.value.charAt(0).toUpperCase() + theField.value.substring(1, theField.value.length);

    if( theField.value.length == 0 ||
        (
          theField.value.indexOf("a") < 0 &&
          theField.value.indexOf("A") < 0 &&
          theField.value.indexOf("e") < 0 &&
          theField.value.indexOf("E") < 0 &&
          theField.value.indexOf("i") < 0 &&
          theField.value.indexOf("I") < 0 &&
          theField.value.indexOf("o") < 0 &&
          theField.value.indexOf("O") < 0 &&
          theField.value.indexOf("u") < 0 &&
          theField.value.indexOf("U") < 0 &&
          theField.value.indexOf("y") < 0 &&
          theField.value.indexOf("Y") < 0
        )
      )
    {
     	AlertMessage(1003, theField);
      if(theField.type.indexOf("select") < 0) theField.select();
      else theField.focus();
      return false;
    } else return true;
  }
}


function CheckPasswordFormat(theField)
{
if(theField.ab_length) nPasswordLength = theField.ab_length;
  if(theField.value.length == 0 && theField.ab_required != "yes") return true;
  else
  {
    if( theField.value.length < nPasswordLength)
    {
     	AlertMessage(1010, theField, nPasswordLength);
      if(theField.type.indexOf("select") < 0) theField.select();
      else theField.focus();
      return false;
    } else return true;
  }
}

function AlertMessage(theID, theField)
{
	var aLength;
	eval("aLanguage = kLanguage");
	eval("aLength = nPasswordLength");
	if(aLanguage == "") aLanguage = "NL";
	
	if(aLanguage == "NL")
	{
		if(theID == 1001) alert("De keuze '" + gRadioArray[i][2] + "' is niet ingevuld.");
		if(theID == 1002) alert("Het veld '" + theField.ab_label + "' is niet correct ingevuld. Mogelijk bent u het '@'-teken vergeten of vergeten af te sluiten met .nl of .com");
		if(theID == 1003) alert("Het veld '" + theField.ab_label + "' is niet correct ingevuld.");
		if(theID == 1004) alert("Het veld '" + theField.ab_label + "' is niet correct ingevuld. Het juiste formaat is '1234 AB'.");
		if(theID == 1005) alert("Het veld '" + theField.ab_label + "' is niet correct ingevuld. Het juiste formaat is '01-01-2002'.");
		if(theID == 1006) alert("Het veld '" + theField.ab_label + "' is niet correct ingevuld. Het juiste formaat is '09:30'.");
		if(theID == 1007) alert("Het veld '" + theField.ab_label + "' is niet correct ingevuld. Een telefoonnummer behoort uit 10 cijfers te bestaan.");
		if(theID == 1008) alert("Het veld '" + theField.ab_label + "' is niet correct ingevuld. Een internet-adres dient het volgende formaat te hebben: http://www.domain.com");
		if(theID == 1009) alert("Het veld '" + theField.ab_label + "' is niet correct ingevuld. Slechts cijfers kunnen in dit veld worden ingevoerd.");
		if(theID == 1010) alert("Het veld '" + theField.ab_label + "' is niet correct ingevuld. Uw toegangscode behoort uit minimaal " + aLength + " cijfers of letters te bestaan.");		
	}
	else
	{
		if(theID == 1001) alert("You have not selected '" + gRadioArray[i][2] + "'.");
		if(theID == 1002) alert("The field '" + theField.ab_label + "' is not filled in correctly. You might have forgotten the '@'-sign or the .com at the end");
		if(theID == 1003) alert("The field '" + theField.ab_label + "' is not filled in correctly.");
		if(theID == 1004) alert("The field '" + theField.ab_label + "' is not filled in correctly. The right format is '1234 AB'.");
		if(theID == 1005) alert("The field '" + theField.ab_label + "' is not filled in correctly. The right format is '01-01-2002'.");
		if(theID == 1006) alert("The field '" + theField.ab_label + "' is not filled in correctly. The right format is '09:30'.");
		if(theID == 1007) alert("The field '" + theField.ab_label + "' is not filled in correctly. A phone number needs to exist of 10 numbers.");
		if(theID == 1008) alert("The field '" + theField.ab_label + "' is not filled in correctly. The right format is 'http://www.domain.com'.");
		if(theID == 1009) alert("The field '" + theField.ab_label + "' is not filled in correctly. Only numbers are permitted in this field.");
		if(theID == 1010) alert("The field '" + theField.ab_label + "' is not filled in correctly. A password needs a minimum of " + aLength + " numbers or characters.");				
	}
}