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

File Name:         validate_form.js
Description:       contains functions that are used in form validations
Created:           12May2001 (GV)
Modified:           
Called From:      most files
History:           
*/
//*************************
// functions: 
// checkString()
// checkEmail()
// checkEmpty()
// checkZIP()
// checkPhone()
// checkMatch()
// checkYear()
// checkSpecial()
// checkPostingPeriod()
// checkInquiryRange()
// checkInteger()
// checkInRange()
//*************************
var postingPeriodMin = 7;
var postingPeriodMax = 90;

var inquiryNumberMin = 1;
var inquiryNumberMax = 100;

var digits = "0123456789";
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var whitespace = " \t\n\r";
var decimalPointDelimiter = ".";
var phoneNumberDelimiters = "()- ";

//expert name
var invalidNameChars = digits+"<>;:~!@#$%^&*()_+{}[]<>?,/\\\"\t\n\r";  // special chars that are allowed: '. (space)
//company name
var invalidCompanyChars = "/\\\t\n\r";   // special chars that are allowed: ';:!?",. (space)<>~@#$%^*()_+{}[]<>
//address
var invalidStreetAddressChars = "<>~!@#$%^&*_+{}[]?\t\n\r"; // special chars that are allowed: ';:()/\",. (space)
//subsidiaries
var invalidSubsChars = "<>:~!@#$%^&*()_+{}[]<>?/\\\"\t\n\r"; // special chars that are allowed: ';,. (space)
//username
var invalidUsernameChars = " <>;:~!#$%^&*()_+{}[]<>?,/\\\"\t\n\r"; // special chars that are allowed: @.'
//other
var invalidSpecialChars = "<@>";

//ZIP code
var validZipChars = digits+lowercaseLetters+uppercaseLetters+" -";
var validPhoneChars = digits+phoneNumberDelimiters+"+";

function isWhitespace (s)
{   var i;
    if ((s == null) || (s.length == 0)) return true;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) return false;
    }
    return true;
}

function stripCharsInBag (s, bag)
{   var i;
    var returnString = "";
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function stripCharsNotInBag (s, bag)
{   var i;
    var returnString = "";
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (bag.indexOf(c) != -1) returnString += c;
    }
    return returnString;
}

function stripWhitespace (s)
{   return stripCharsInBag (s, whitespace)
}

function charInString (c, s)
{   for (i = 0; i < s.length; i++)
    {   if (s.charAt(i) == c) return true;
    }
    return false
}

function stripInitialWhitespace (s)
{   var i = 0;
    while ((i < s.length) && charInString (s.charAt(i), whitespace))
       i++;
    return s.substring (i, s.length);
}

function stripClosingWhitespace (s)
{ var i = s.length
  while ((i > 0) && charInString (s.charAt(i), whitespace))
    i--;
  return s.substring (0, i);
}

function isEmpty(s)
{   
	s = stripInitialWhitespace(s);
	s = stripClosingWhitespace(s);
	return ((s == null) || (s.length == 0))
}

function isLetter (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

function isLetterOrDigit (c)
{   return (isLetter(c) || isDigit(c))
}

function isInteger (s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (!isDigit(c)) return false;
    }
    return true;
}

function isDecimal (s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (!isDigit(c) && c!="." && c!=",") return false;
    }
    return true;
}

function isAlpha (s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (!(isLetter(c)))
        return false;
    }
    return true;
}

function isAlphanumeric (s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (! (isLetter(c) || isDigit(c) ) )
        return false;
    }
    return true;
}

function isPC1 (s)
{   var i;
	 if (s.length!=6) return false;
    for (i = 0; i < 5; i++)
    {   
        var c = s.charAt(i);
        if (!isDigit(c))
        return false;
    }
    return true;
}

function isPC2 (s)
{   var i;
	 if (s.length!=5) return false;
    for (i = 0; i < 4; i++)
    {   
        var c = s.charAt(i);
        if (!isDigit(c))
        return false;
    }
    return true;
}

function isPC (s)
{   var i;
	 if (s.length!=12) return false;
	 var c = s.charAt(6);
	 if(c!="-") return false;
    for (i = 0; i < 5; i++)
    {   
        var c = s.charAt(i);
        if (!isDigit(c))
        return false;
    }
    for (i = 7; i < 11; i++)
    {   
        var c = s.charAt(i);
        if (!isDigit(c))
        return false;
    }
    return true;
}

function isAlphanumericDot (s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (! (isLetter(c) || isDigit(c) || (c == ".") || (c == "-") || (c == "_")) )
        return false;
    }
    return true;
}

function isZip (s)
{
      sLengthBefore = s.length;
      s = stripCharsNotInBag(s, validZipChars);
      sLengthAfter = s.length;
      if (sLengthBefore != sLengthAfter) return false;
        else return true;
}

function isPhone (s)
{
      sLengthBefore = s.length;
      s = stripCharsNotInBag(s, validPhoneChars);
      sLengthAfter = s.length;
      if (sLengthBefore != sLengthAfter) return false;
        else return true;
}

//***************************
function isString (s, bag)
{ 
      sLengthBefore = s.length;
      s = stripCharsInBag(s, bag);
      sLengthAfter = s.length;
      if (sLengthBefore != sLengthAfter) return false;
        else return true;
}

function isEmail (s)
{  
  if (isWhitespace(s)) return false;
    var i = 1;
    var sLength = s.length;
    if (!isAlphanumeric(s.charAt(0))) return false; 
    while ((i < sLength) && (s.charAt(i) != "@"))
    {
      if (!isAlphanumericDot(s.charAt(i))) return false; 
      i++
    }
    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;
    if (!isAlphanumeric(s.charAt(i-1))) return false; 
    while ((i < sLength) && (s.charAt(i) != "."))
    { 
      if (!isAlphanumericDot(s.charAt(i))) return false; 
      i++
    }
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else i += 2;
    if (!isAlphanumeric(s.charAt(i-1))) return false;
    while (i < sLength - 1)
    {
      if (!isAlphanumericDot(s.charAt(i))) return false;
      i++
    }
  return true;
}

function isYear (s)
{  
    if (!isInteger(s)) return false;
    var num = parseInt (s);
    if (num < 1000) return false;
    return (s.length == 4);
}

function isInRange (s, a, b)
{   
    var num = parseInt (s);
    return ((num >= a) && (num <= b));
}

function isDate (s, style)
{
	delimiter = "." // default
	var daysInMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
	switch (style) {
		case 1: // dd.mm.gggg
			{
				delimiter = "."
				if (s.length < 10) return false // pārbaudam garumu
				regexp = /\d?\d\.\d?\d\.\d{4}/
				if (s.search(regexp) != 0) return false // meklējamai izteiksmei jāsākas 0-tajā pozīcijā
				break;
			}
		case 2: // dd-mm-gggg
			{
				delimiter = "-"
				if (s.length < 10) return false // pārbaudam garumu
				regexp = /\d?\d\-\d?\d\-\d{4}/
				if (s.search(regexp) != 0) return false // meklējamai izteiksmei jāsākas 0-tajā pozīcijā
				break;
			}
		case 3: // dd/mm/yyyy
			{
				delimiter = "/"
				if (s.length < 10) return false // check length
				regexp = /\d?\d\/\d?\d\/\d{4}/
				if (s.search(regexp) != 0) return false // should start at position 0
				break;
			}
	}
	sArray = s.split(delimiter, 3)
	var day = sArray[0]
	var month = sArray[1]
	var year = sArray[2]
	if (!isYear(year)) return false
	if ((month < 1) || (month > 12)) return false
	if ((day < 1) || (day > daysInMonth[month-1])) return false
	return true
}

function warnEmpty (theField, s)
{   theField.focus()
    alert(s)
    return false
}

function warnInvalid (theField, s)
{   theField.focus()
    theField.select()
    alert(s)
    return false
}

function checkEmpty(theField, s)
{
    theField.value = stripInitialWhitespace(theField.value);
    theField.value = stripClosingWhitespace(theField.value);
    if (isEmpty(theField.value)) {
        warnEmpty(theField, s);
        return false;}
    else return true;
}

//****************************************
//***************************************
function checkMatch (field1, field2, s) {
    field1.value = stripInitialWhitespace(field1.value);
    field1.value = stripClosingWhitespace(field1.value);
    field2.value = stripInitialWhitespace(field2.value);
    field2.value = stripClosingWhitespace(field2.value);
  if (field1.value != field2.value) {
    alert(s);
    return(false);
  } else {return(true);}
}

function checkZIP (theField, s)
{ 
    theField.value = stripInitialWhitespace(theField.value);
    theField.value = stripClosingWhitespace(theField.value);
    if (!isZip(theField.value))
       return warnInvalid (theField, s);
    return true;
}

function checkPhone (theField, s)
{  
    theField.value = stripInitialWhitespace(theField.value);
    theField.value = stripClosingWhitespace(theField.value);
    if (!isPhone(theField.value))
       return warnInvalid (theField, s);
    return true;
}

function checkDate (theField, s)
{
	var checkstr = "0123456789";
	var DateField = theField;
	var Datevalue = "";
	var DateTemp = "";
	var seperator = ".";
	var day;
	var month;
	var year;
	var leap = 0;
	var err = 0;
	var i;
	
	if (!checkEmpty(theField, s)) return false;
	if (theField.value.length!=10) return warnInvalid (theField, s);
	
   err = 0;
   DateValue = DateField.value;
   /* Delete all chars except 0..9 */
   for (i = 0; i < DateValue.length; i++) {
	  if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
	     DateTemp = DateTemp + DateValue.substr(i,1);
	  }
   }
   DateValue = DateTemp;
   /* Always change date to 8 digits - string*/
   /* if year is entered as 2-digit / always assume 20xx */
   if (DateValue.length == 6) {
      DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }
   if (DateValue.length != 8) {
      err = 19;}
   /* year is wrong if year = 0000 */
   year = DateValue.substr(4,4);
   if (year == 0) {
      err = 20;
   }
   /* Validation of month*/
   month = DateValue.substr(2,2);
   if ((month < 1) || (month > 12)) {
      err = 21;
   }
   /* Validation of day*/
   day = DateValue.substr(0,2);
   if (day < 1) {
     err = 22;
   }
   /* Validation leap-year / february / day */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }
   /* Validation of other months */
   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
      err = 26;
   }
   /* if 00 ist entered, no error, deleting the entry */
   if ((day == 0) && (month == 0) && (year == 00)) {
      err = 0; day = ""; month = ""; year = ""; seperator = "";
   }
   /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
   if (err == 0) {
      DateField.value = day + seperator + month + seperator + year;
      return true;
   }
   /* Error-message if err != 0 */
   else {
      return warnInvalid (theField, s);
   }
}

function compareDates(startDate, endDate, s)
{
	var dStart=new Date();
	var dEnd=new Date();
	
	var dsY = (startDate.value.substr(6,4));
	var dsM = (startDate.value.substr(3,2));
	var dsD = (startDate.value.substr(0,2));
	
	var deY = (endDate.value.substr(6,4));
	var deM = (endDate.value.substr(3,2));
	var deD = (endDate.value.substr(0,2));
	
	if (dsY<deY)
		return true;
	else if (dsY>deY)
		return warnInvalid(endDate, s);
	
	if (dsM<deM)
		return true;
	else if (dsM>deM)
		return warnInvalid(endDate, s);
	
	if (dsD<deD)
		return true;
	
	return warnInvalid(endDate, s);
}

function checkString (theField, bag, s)
{  
    theField.value = stripInitialWhitespace(theField.value);
    theField.value = stripClosingWhitespace(theField.value);
    switch (bag) {
      case 'name': bag = invalidNameChars;break;
      case 'company': bag = invalidCompanyChars;break;
      case 'street': bag = invalidStreetAddressChars;break;
      case 'subsidiary': bag = invalidSubsChars;break;
      case 'username': bag = invalidUsernameChars;break;
      case 'special': bag = invalidSpecialChars;break;
      default:
    } 
    if (!isString(theField.value, bag))
       return warnInvalid (theField, s);
    return true;
}

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

function checkEmail (theField, s)
{  
    theField.value = stripInitialWhitespace(theField.value);
    theField.value = stripClosingWhitespace(theField.value);
    if (!isEmpty(theField.value)) 
    {
      if (!isEmail(theField.value)) 
         return warnInvalid (theField, s);
    }
    return true;
}

function checkYear (theField, s)
{  
    theField.value = stripInitialWhitespace(theField.value);
    theField.value = stripClosingWhitespace(theField.value);
    if (!isYear(theField.value))
      return warnInvalid(theField, s);
    return true;
}

function checkPostingPeriod (theField, s_acc, s_len)
{
    theField.value = stripInitialWhitespace(theField.value);
    theField.value = stripClosingWhitespace(theField.value);
    if (!isInteger(theField.value)) 
        return warnInvalid(theField, s_acc)
    if (!isInRange(theField.value, postingPeriodMin, postingPeriodMax))
        return warnInvalid(theField, s_len)
    return true;
}

function checkInquiryNumber (theField, s_acc, s_len)
{
    theField.value = stripInitialWhitespace(theField.value);
    theField.value = stripClosingWhitespace(theField.value);
    if (!isInteger(theField.value)) 
        return warnInvalid(theField, s_acc)
    if (!isInRange(theField.value, inquiryNumberMin, inquiryNumberMax))
        return warnInvalid(theField, s_len)
    return true;
}

function checkSpecial (theField, s)
{
  var areaText = theField.innerText;
  if (!isString(areaText, invalidSpecialChars)) 
    return warnInvalid(theField, s)
  return true;
}

function checkInteger (theField, s)
{
    theField.value = stripInitialWhitespace(theField.value);
    theField.value = stripClosingWhitespace(theField.value);
    if (!isInteger(theField.value)) 
        return warnInvalid(theField, s)
    return true;
}

function checkInRange (theField, r_min, r_max, s)
{
    theField.value = stripInitialWhitespace(theField.value);
    theField.value = stripClosingWhitespace(theField.value);
    if (!isInRange(theField.value, r_min, r_max))
        return warnInvalid(theField, s)
    return true;
}

function checkAlpha (theField, s)
{
    theField.value = stripInitialWhitespace(theField.value);
    theField.value = stripClosingWhitespace(theField.value);
    if (!isAlpha(theField.value))
        return warnInvalid(theField, s)
    return true;
}

function checkPC  (theField, s)
{
    theField.value = stripInitialWhitespace(theField.value);
    theField.value = stripClosingWhitespace(theField.value);
    if (!isPC(theField.value))
        return warnInvalid(theField, s)
    return true;
}

function checkPC1  (theField, s)
{
    theField.value = stripInitialWhitespace(theField.value);
    theField.value = stripClosingWhitespace(theField.value);
    if (!isPC1(theField.value))
        return warnInvalid(theField, s)
    return true;
}

function checkPC2  (theField, s)
{
    theField.value = stripInitialWhitespace(theField.value);
    theField.value = stripClosingWhitespace(theField.value);
    if (!isPC2(theField.value))
        return warnInvalid(theField, s)
    return true;
}

function checkAlphanumeric (theField, s)
{
    theField.value = stripInitialWhitespace(theField.value);
    theField.value = stripClosingWhitespace(theField.value);
    if (!isAlphanumeric(theField.value))
        return warnInvalid(theField, s)
    return true;
}

function checkDecimal (theField, s)
{
    theField.value = stripInitialWhitespace(theField.value);
    theField.value = stripClosingWhitespace(theField.value);
    if (!isDecimal(theField.value))
        return warnInvalid(theField, s)
    return true;
}

function checkLen(Object, MaxLen)
{
	if(Object.value.length > MaxLen)
	{     
		Object.value = Object.value.substring(0, MaxLen);
	}
}
