/********************************************************************
   Validate 3.13                                           2002-10-10

   Collection of validate functions.

   // Markus Gemstad
   gemstad@hotmail.com
   http://www.gemstad.com (references, samples etc)

********************************************************************/


/** Errormessages ***************************************************

  Here's all the error messages used in the validate functions. If 
  you want them in a different language all you have to do is change 
  the strings below. You should not remove the ".\n" or the extra 
  spaces (" ") in front or after the text. To get an english version 
  you comment these swedish ones and decomment the english strings 
  below and vice versa.                                            */


// Messages in english
var sErrIsEmpty                 = " is required.\n";
var sErrFormat                  = "Err!";
var sErrNotChoosen              = " is not chosen.\n";

var sErrValidateTextMinLength1  = " must be at least ";
var sErrValidateTextMinLength2  = " characters.\n";
var sErrValidateTextMaxLength1  = " can be at most ";
var sErrValidateTextMaxLength2  = " characters.\n";

var sErrValidateText2           = " must only contain numbers and the letters A-Z.\n";

var sErrValidateNumber          = " must only contain a number.\n";
var sErrValidateNumberMin       = " Minimum value is ";
var sErrValidateNumberMax       = " Maximum value is ";

var sErrValidateFileExt         = " allows only the fileextensions "
var sErrValidateTime            = " must contain a time in the format HH:MM.\n";
var sErrValidateRegNr           = " must contain a car registration number in the formats \"ABC123\" or \"ABC 123\".\n";
var sErrValidateZipcode         = " must contain a zipcode in the formats \"123 45\" or \"12345\".\n";
var sErrValidateEmail           = " is not a valid e-mail address.\n";
var sErrValidateDate            = " only allow the formats ";

var sErrValidatePersNr          = " is not a valid swedish personalnumber.\n";

var sErrCompareDatesIsNot       = " is not ";
var sErrCompareDatesSameAs      = "same as";
var sErrCompareDatesLessOrEqual = "less or equal to";
var sErrCompareDatesMoreOrEqual = "more or equal to";
var sErrCompareDatesLessThan    = "less than";
var sErrCompareDatesMoreThan    = "more than";
var sErrCompareDatesDifferent   = "different";

/*******************************************************************/

function validateText(sText, sName, bAllowEmpty, iMinLength, iMaxLength)
{
   var sErrorMsg = "";
   sText = trim(sText);
   
   if(!bAllowEmpty && sText == "") // If empty
   {
      sErrorMsg = "- " + sName + sErrIsEmpty;
   }
   else if(sText != "") // else if to short or to long string
   {
      if(iMinLength != null && sText.length < iMinLength)
         sErrorMsg += "- " + sName + sErrValidateTextMinLength1 + iMinLength + sErrValidateTextMinLength2;
      if(iMaxLength != null && sText.length > iMaxLength)
         sErrorMsg += "- " + sName + sErrValidateTextMaxLength1 + iMaxLength + sErrValidateTextMaxLength2;
   }

   return sErrorMsg;
}

function validateText2(sText, sName, bAllowEmpty, iMinLength, iMaxLength)
{
   var sErrorMsg = "";

   if(!bAllowEmpty && sText == "") // If empty
   {
      sErrorMsg = "- " + sName + sErrIsEmpty;
   }
   else if(sText != "") // else if to short or to long string or nonvalid characters
   {
      sErrorMsg = validateText(sText, sName, bAllowEmpty, iMinLength, iMaxLength);
      
      if(sErrorMsg == "")
      {
		   for(var i=0; i<sText.length; i++)
		   {
		      if(!(sText.charAt(i) <= "9" && sText.charAt(i) >= "0" || 
		           sText.charAt(i) <= "z" && sText.charAt(i) >= "a" || 
		           sText.charAt(i) <= "Z" && sText.charAt(i) >= "A"))
		      {
		         sErrorMsg += "- " + sName + sErrValidateText2;
		         break;
		      }
		   }
      }
   }
   
   return sErrorMsg;
}

function validateNumber(sNumber, sName, bAllowEmpty, iMinValue, iMaxValue, bAllowNegative)
{
   var sErrorMsg = "";
   sNumber = trim(sNumber);

   if(!bAllowEmpty && sNumber == "") // If empty
   {
      sErrorMsg = "- " + sName + sErrIsEmpty;
   }
   else if(sNumber != "") // else if number - to low or to high
   {
      if(bAllowNegative && sNumber.charAt(0) == "-")
         sNumber = sNumber.slice(1);
      var objRegExp   = new RegExp("[^0-9]", "g"); // Search for everything except 0-9
      var iInvalidPos = sNumber.search(objRegExp);

      if(iInvalidPos != -1) 
      {
         sErrorMsg += "- " + sName + sErrValidateNumber;
      }
		else // Only number characters
		{
		   if(iMinValue != null && eval(sNumber) < iMinValue)
		      sErrorMsg += "- " + sName + sErrValidateNumberMin + iMinValue + ".\n";
		   if(iMaxValue != null && eval(sNumber) > iMaxValue)
		      sErrorMsg += "- " + sName + sErrValidateNumberMax + iMaxValue + ".\n";
		}
   }
   return sErrorMsg;
}

function formatZipcode(sZipcode, iReturnFormat)
{
   // Format     Length  iReturnFormat
   // ==================================
   // 12345      5       1
   // 123 45     6       2

   var sFormated = "";
   sZipcode = trim(sZipcode);
   
   if(validateZipcode(sZipcode, "Zipcode", true) == "" && sZipcode.length > 0)
   {
      // Format to 123 45
      if(sZipcode.length == 5 && iReturnFormat == 2)
         sFormated = sZipcode.slice(0,3) + " " + sZipcode.slice(3,5);
      else
         sFormated = sZipcode;
   }
   else if(sZipcode.length > 0)
      sFormated = sErrFormat;
   
   return sFormated;
}

function validateZipcode(sZipcode, sName, bAllowEmpty)
{
   // Valid format    Length
   // ======================
   // 12345           5
   // 123 45          6

   var sErrorMsg = "";
   var objRegExp;

   sZipcode = trim(sZipcode);

   if(!bAllowEmpty && sZipcode.length == 0) // If empty
   {
      sErrorMsg = "- " + sName + sErrIsEmpty;
   }
   else if(sZipcode.length > 0) // else check zipcode
   {
      if(sZipcode.length == 5) // 12345
      {
         objRegExp = new RegExp("[0-9]{5}");
         if(sZipcode.search(objRegExp) == -1) // If invalid
            sErrorMsg = "- " + sName + sErrValidateZipcode;
      }
      else if(sZipcode.length == 6) // 123 45
      {
         objRegExp = new RegExp("[0-9]{3} [0-9]{2}");
         if(sZipcode.search(objRegExp) == -1) // If invalid
            sErrorMsg = "- " + sName + sErrValidateZipcode;
      }
      else // If another length
         sErrorMsg = "- " + sName + sErrValidateZipcode;
   }

   return sErrorMsg;
}

function validateEmail(sEmail, sName, bAllowEmpty)
{
   /* Written by Paolo Wales (paolo@taize.fr) starting on a basis by Samrat Sen.

   Notes:
   
   'exclude' checks 5 conditions:
   
   a) characters that should not be in the address
   b) characters that should not be at the start
   c) & d) characters that shouldn't be together
   e) there's not more than one '@'
   
   'check' checks there's at least one '@', later followed by at least one '.'
   'checkend' checks the address ends with a period followed by 2 or 3 alpha characters.
   N.B. Javascript 1.2 only works with version 4 browsers and higher. */
   
   var exclude   =/[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
   var check     =/@[\w\-]+\./;
   var checkend  =/\.[a-zA-Z]{2,3}$/;   
   var sErrorMsg = "";
   sEmail = trim(sEmail);
 
   if(!bAllowEmpty && sEmail == "")
   {
      sErrorMsg = "- " + sName + sErrIsEmpty;
   }
   else if(sEmail != "")
   {
      if(((sEmail.search(exclude) != -1) || 
          (sEmail.search(check)) == -1) || 
          (sEmail.search(checkend) == -1))
      {
         sErrorMsg = "- " + sName + sErrValidateEmail;
      }
   }

   return sErrorMsg;
}

function validateEmail2(sEmail, sEmail2, sName)
{
  var sErrorMsg = "";
  
  if(sEmail == sEmail2)
  {
  sErrorMsg = "";
  }
  else if(sEmail != sEmail2)
  {
  sErrorMsg = "- " + sName + " is different in two fields, please re-enter. \n";
 
  }
  return sErrorMsg;
}


function validateSelect(oFormObj, sName)
{
   var sErrorMsg = "";

   if(oFormObj.multiple)
   {
      sErrorMsg = "- " + sName + sErrNotChoosen;
      for(var i=0; i<oFormObj.options.length; i++)
      {
         if(oFormObj.options[i].selected)
         {
            sErrorMsg = "";
            break;
         }
      }
   }
   else
   {
      if(oFormObj.options[oFormObj.selectedIndex].value == "0" ||
         oFormObj.options[oFormObj.selectedIndex].value == "")
      {
         sErrorMsg = "- " + sName + sErrNotChoosen;
      }
   }
   return sErrorMsg;
}

function validateSelect2(oFormObj, oFormObj2, sName, sName2)
{
   var sErrorMsg = "";

   if(oFormObj.multiple)
   {
      sErrorMsg = "- " + sName + sErrNotChoosen;
      for(var i=0; i<oFormObj.options.length; i++)
      {
         if(oFormObj.options[i].selected)
         {
            sErrorMsg = "";
            break;
         }
      }
   }
   else if
   
      (oFormObj.options[oFormObj.selectedIndex].value == "0" ||
         oFormObj.options[oFormObj.selectedIndex].value == "")
      {
         sErrorMsg = "- " + sName + sErrNotChoosen;
      }
   
   else
   
       if(oFormObj.options[oFormObj.selectedIndex].value == oFormObj2.options[oFormObj2.selectedIndex].value)
	   {
	   	 sErrorMsg = "- " + sName2 + " can not be the same. \n";
		 }
	
   return sErrorMsg;
}

function validateRadio(oFormObj, sName)
{
   var sErrorMsg = "- " + sName + sErrNotChoosen;
   if(oFormObj.length == null && oFormObj.checked)
      sErrorMsg = "";
   else
   {
  	   for(i=0;i<oFormObj.length;i++)
	   {
         if(oFormObj[i].checked)
         {
            sErrorMsg = "";
            break;
         }
	   }
   }
   return sErrorMsg;
}

function validateCheckbox(oFormObj, sName)
{
   var sErrorMsg = "";
   var checkbox_choices = 0; 
  
	for (i = 0; i < oFormObj.length; i++) {
if (oFormObj[i].checked){
checkbox_choices = checkbox_choices + 1;
}
}
if(checkbox_choices != 1 ){

		sErrorMsg = "- " + sName + sErrNotChoosen;
}
if (oFormObj[0].checked && oFormObj[2].checked){
	sErrorMsg = "- Not a valid choice for Check Boxes. \n"
	}

if (oFormObj[1].checked && oFormObj[2].checked){
	sErrorMsg = "- Not a valid choice for Check Boxes. \n"
	}
	
if (oFormObj[0].checked && oFormObj[1].checked && oFormObj[2].checked){
	sErrorMsg = "- All three boxes can't be chosen. \n"
	}    

   return sErrorMsg;
}

function validateCheckbox2(oFormObj, sName)
{
   var sErrorMsg = "";
   var checkbox_choices = 0; 
  
	for (i = 0; i < oFormObj.length; i++) {
el=oFormObj[i];
   if (el.type=="checkbox") {
     if (el.checked) { return sErrorMsg; } 
    }
 }
 sErrorMsg = "- " + sName + sErrNotChoosen;
 return sErrorMsg;
 return false;
}


function ltrim(sValue)
{
   while(1)
   {
      if(sValue.substring(0, 1) != " ")
         break;
      sValue = sValue.substring(1, sValue.length);
   }
   return sValue;
}

function rtrim(sValue)
{
   while(1)
   {
      if(sValue.substring(sValue.length - 1, sValue.length) != " ")
         break;
      sValue = sValue.substring(0, sValue.length - 1);
   }
   return sValue;
}

function trim(sValue)
{
   var sTemp = ltrim(sValue);
   return rtrim(sTemp);
}

function SSNValidation(oFormObj, sName, bAllowEmpty) {

var matchArr = oFormObj.match(/^(\d{3})-?\d{2}-?\d{4}$/);
var numDashes = oFormObj.split('-').length - 1;
var sErrorMsg = "";
oFormObj = trim(oFormObj);
   
if(!bAllowEmpty && oFormObj == "") // If empty
   {
      sErrorMsg = "";
}
else if (matchArr == null || numDashes == 1) {
sErrorMsg = "- " + sName + " must be 9 digits or in the form NNN-NN-NNNN. \n";
}
else 
if (parseInt(matchArr[1],10)==0) {
sErrorMsg = "- " + sName + " can't start with 000. \n";
}
else {
return sErrorMsg;
   }
return sErrorMsg;
}

