// varifies length
function verify_length(form,element,lungime)
{
	if (document.forms[form].elements[element].value.length > lungime) 
	{
		document.forms[form].elements[element].value = document.forms[form].elements[element].value.substr(0,lungime)
		alert("Max lenght is "+lungime+" characters")
	}
}
////////////////////////////





// global variables
acF = new Array()
acF[1] = new Array() // form name
acF[2] = new Array() // field name in form

acF[3] = new Array() // notnull - value not null
										 // equals - value must match the specified value
										 // notequals - value must differ from the specified value
										 // format - value must be in the specified format
										 // maxsize - maximum length
										 // minsize - minimum length
										 // maxsizenull - maximum number of characters or null
										 // minsizenull - minimum number of characters or null
										 // password - 
                     // checked - value must be checked
                     // allchars - value must contain only chars
										 
acF[4] = new Array() // specified value
acF[5] = new Array() // name of field in error message

sizeOf_acF = 0

// add condition
// formName - form name
// fieldName - field name
// fieldCondition - condition that the field name must meet
// fieldValue - specified value for some conditions (e.g. maxsize, equals)
// fieldDescription - name of the field in the error message
function addCond(formName, fieldName, fieldCondition, fieldValue, fieldDescription)
{
	isOK = true;
	if (acF[1].length > 0)
	{
		for(i=1;i<acF[1].length;i++)
		{
			if (formName == acF[1][i] && fieldName == acF[2][i] && fieldCondition == acF[3][i] && fieldValue == acF[4][i] && fieldDescription == acF[5][i])
				isOK = false;
		}
	}
	if (isOK)
	{
		sizeOf_acF = acF[1].length + 1
		acF[1][sizeOf_acF] = formName
		acF[2][sizeOf_acF] = fieldName
		acF[3][sizeOf_acF] = fieldCondition
		acF[4][sizeOf_acF] = fieldValue
		acF[5][sizeOf_acF] = fieldDescription
	}
}

function delCond(formName, fieldName, fieldCondition, fieldValue, fieldDescription)
{
		tempSize = 0;
		acFtmp = new Array();
		acFtmp[1] = new Array()
		acFtmp[2] = new Array()
		acFtmp[3] = new Array()
		acFtmp[4] = new Array()
		acFtmp[5] = new Array()

for (i=1;i<=acF[1].length;i++)
		{
				if (formName != acF[1][i] || fieldName != acF[2][i] || fieldCondition != acF[3][i] || fieldValue != acF[4][i] || fieldDescription != acF[5][i])
				{
					tempSize++;
					acFtmp[1][tempSize] = acF[1][i];
				}
		}
		acF = acFtmp;
		acFtmp = null;
}

// verifies if a char is a number
function isNumeric(cValue)
{
  isN = true
	nums = "0123456789"
	if (nums.indexOf(cValue) == -1) isN = false
	return isN
}

// verifies if a string contains only numbers
function isOnlyNumeric(sValue)
{
  isGood = true
  for (i=0; i < sValue.length && isGood == true; i++)
  {
    c = sValue.charAt(i)
    if (! isNumeric(c))
    {
      isGood = false
    }
  }
  return isGood
}

function isEmail(string) 
{
	if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
		return true;
	else
	{
		return false;
	}
}

// verifies if a char is a letter
function isLiteral(cValue)
{
  isL = true
	lit = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
	if (lit.indexOf(cValue) == -1) isL = false
	return isL
}

// Verify if a char is an allowed character
function isValidLiteral(cValue)
{
  isL = true
	lit = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 .,"
	if (lit.indexOf(cValue) == -1) isL = false
	return isL
}

// verifies if a string contains only letters
function isOnlyLiteral(sValue)
{
  isGood = true
  for (i=0; i < sValue.length && isGood == true; i++)
  {
    c = sValue.charAt(i)
    if (! isValidLiteral(c))
    {
      isGood = false
    }
  }
  return isGood
}

// verifies if a string has a certain format
// values: % - letters (a..z, A..Z)
//         # - numbers (0..9)
//         * - any character
// any other character is replaced by itself
function cFFormat(sValue, sFormat)
{
  isCorect = true
	if (sValue.length != sFormat.length) isCorect = false
	for (i=0; i < sValue.length && isCorect == true; i++)
	{
	  c = sValue.charAt(i)
		f = sFormat.charAt(i)
		switch(f)
		{
		  case "#":
			{
			  if (! isNumeric(c))
				{
				  isCorect = false
				}
				break
			}
		  case "%":
			{
			  if (! isLiteral(c))
				{
				  isCorect = false
				}
				break
			}
		  case "*":	break
			default:
			{
			  if (c != f) isCorect = false
			}
		}
	}
	return isCorect
}

function checkForm(formName)
{
  isOK = true
	sMsg = ""
	CrLf = "\r\n"
  for (cFCount=1;cFCount<=acF[1].length;cFCount++)
	{
	  if (acF[1][cFCount] == formName)
		{
		  switch(acF[3][cFCount])
			{
			  case "notnull":
				{
				  if (document.forms[formName].elements[acF[2][cFCount]].value == "")
					{
					  isOK = false
						sMsg = sMsg + "'" + acF[5][cFCount] + "' is a mandatory field." + CrLf;
					}
					break
				}
			  case "checked":
				{
				  if (!document.forms[formName].elements[acF[2][cFCount]].checked)
					{
					  isOK = false
						sMsg = sMsg + "'" + acF[5][cFCount] + "' must be selected." + CrLf;
					}
					break
				}
			  case "selected_radio":
				{
				  if (!document.forms[formName].elements[acF[2][cFCount]][acF[4][cFCount]].checked)
					{
					  isOK = false
						sMsg = sMsg + "'" + acF[5][cFCount] + "' must be set to " + document.forms[formName].elements[acF[2][cFCount]][acF[4][cFCount]].value + "." + CrLf;
					}
					break
				}
			  case "equals":
				{
				  if (document.forms[formName].elements[acF[2][cFCount]].value != acF[4][cFCount])
					{
					  isOK = false
						sMsg = sMsg + "'" + acF[5][cFCount] + "' must be set to " + acF[4][cFCount] + "." + CrLf;
					}
					break
				}
			  case "notequals":
				{
				  if (document.forms[formName].elements[acF[2][cFCount]].value == acF[4][cFCount])
					{
					  isOK = false
						sMsg = sMsg + "'" + acF[5][cFCount] + "' must differ from '" + acF[4][cFCount] + "'." + CrLf;
					}
					break
				}
			  case "password":
				{
				  if (document.forms[formName].elements[acF[2][cFCount]].value != document.forms[formName].elements[acF[4][cFCount]].value)
					{
					  isOK = false
						sMsg = sMsg + "Password and Confirm Password do not match." + CrLf;
					}
					break
				}
			  case "email":
				{
				  if (isEmail(document.forms[formName].elements[acF[2][cFCount]].value) != true)
					{
					  isOK = false
						sMsg = sMsg + "'" + acF[5][cFCount] + "' is not a valid email address." + CrLf;
					}
					break
				}
			  case "equalsize":
				{
  		    if (document.forms[formName].elements[acF[2][cFCount]].value.length != parseInt(acF[4][cFCount]))
  				{
  				  isOK = false
  					sMsg = sMsg + "'" + acF[5][cFCount] + "' must be of this size: " + parseInt(acF[4][cFCount]) + "." + CrLf;
  				}
				break
        }
			  case "maxsize":
				{
  		    if (document.forms[formName].elements[acF[2][cFCount]].value.length > parseInt(acF[4][cFCount]))
  				{
  				  isOK = false
  					sMsg = sMsg + "'" + acF[5][cFCount] + "' is too long. (Max length = " + parseInt(acF[4][cFCount]) + ")" + CrLf;
  				}
				break
        }
			  case "maxvalue":
				{
  		    if (parseInt(document.forms[formName].elements[acF[2][cFCount]].value) > parseInt(acF[4][cFCount]))
  				{
  				  isOK = false
  					sMsg = sMsg + "'" + acF[5][cFCount] + "' is too large. (Max value = " + parseInt(acF[4][cFCount]) + ")" + CrLf;
  				}
				break
        }
			  case "maxsizenull":
				{
				  if (document.forms[formName].elements[acF[2][cFCount]].value != "")
          {
  				  if (document.forms[formName].elements[acF[2][cFCount]].value.length > parseInt(acF[4][cFCount]))
  					{
  					  isOK = false
  						sMsg = sMsg + "'" + acF[5][cFCount] + "' is too long. (Max length = " + parseInt(acF[4][cFCount]) + ")" + CrLf;
  					}
          }
					break
				}
			  case "minsize":
				{
				  if (document.forms[formName].elements[acF[2][cFCount]].value.length < parseInt(acF[4][cFCount]))
					{
					  isOK = false
						sMsg = sMsg + "'" + acF[5][cFCount] + "' is too short. (Min length = " + parseInt(acF[4][cFCount]) + ")" + CrLf;
					}
					break
				}
			  case "minvalue":
				{
				  if (parseInt(document.forms[formName].elements[acF[2][cFCount]].value) < parseInt(acF[4][cFCount]))
					{
					  isOK = false
						sMsg = sMsg + "'" + acF[5][cFCount] + "' is too small. (Min value = " + parseInt(acF[4][cFCount]) + ")" + CrLf;
					}
					break
				}
			  case "minsizenull":
				{
				  if (document.forms[formName].elements[acF[2][cFCount]].value != "")
          {
  				  if (document.forms[formName].elements[acF[2][cFCount]].value.length < parseInt(acF[4][cFCount]))
  					{
  					  isOK = false
  						sMsg = sMsg + "'" + acF[5][cFCount] + "' is too short. (Min length = " + parseInt(acF[4][cFCount]) + ")" + CrLf;
  					}
          }
					break
				}
			  case "format":
				{
				  if (! cFFormat(document.forms[formName].elements[acF[2][cFCount]].value, acF[4][cFCount]))
					{
					  isOK = false
						sMsg = sMsg + "'" + acF[5][cFCount] + "' must be: '" + acF[4][cFCount] + "'." + CrLf;
					}
					break
				}
			  case "phone_format":
				{
				  if ((! cFFormat(document.forms[formName].elements[acF[2][cFCount]].value, '###-###-####'))&&
						(! cFFormat(document.forms[formName].elements[acF[2][cFCount]].value, '(###) ###-####')))
					{
					  isOK = false
						sMsg = sMsg + "'" + acF[5][cFCount] + "' must be in one of these formats: '###-###-####' or '(###) ###-####'." + CrLf;
					}
					break
				}
			  case "allchars":
				{
				  if (! isOnlyLiteral(document.forms[formName].elements[acF[2][cFCount]].value))
					{
					  isOK = false
						sMsg = sMsg + "'" + acF[5][cFCount] + "' must contain only chars a-z, A-Z, 1-9, comma, period or space." + CrLf;
					}
					break
				}
			  case "allnums":
				{
				  if (! isOnlyNumeric(document.forms[formName].elements[acF[2][cFCount]].value))
					{
					  isOK = false
						sMsg = sMsg + "'" + acF[5][cFCount] + "' must be a number." + CrLf;
					}
					break
				}


			case "list_empty":
				{
					//!!!!!!!!!!!!!! DOESN'T WORK WITH SELECT NAMES THAT CONTAIN [] e.g.: "lists[]".  !!!!!!!!!!!!!!!!
					// if you must send a select by post (with []) then create check function locally
					if (document.forms[formName].elements[acF[2][cFCount]].length > 0)
					{
						isOK = false;
						for (i=0;i<document.forms[formName].elements[acF[2][cFCount]].length;i++)
						{
							if (document.forms[formName].elements[acF[2][cFCount]].options[i].selected == true)
								isOK = true;
						}
						if (!isOK)
						{
							isOK = false;
							sMsg = sMsg + "Must select at least one  " + acF[5][cFCount] + "." + CrLf;
						}
					}
					else
					{
						isOK = false;
						sMsg = sMsg + "Must select at least one  " + acF[5][cFCount] + "." + CrLf;
					}
					break;
				}


			  case "dateformat":
				{
				if (document.forms[acF[1][cFCount]].elements[acF[4][cFCount]].checked)
					{
					isOK = true
					}
				else
					{
					ceva = document.forms[acF[1][cFCount]].elements[acF[2][cFCount]].value.split('-')
					an=ceva[0]
					luna=ceva[1]
					zi=ceva[2]
					if (!((an>0000) && (an<4000)))
						{
						isOK = false
						sMsg = sMsg + "'" + acF[5][cFCount] + "' must be in this format: yyyy-mm-dd " + CrLf;
						break
						}
					if (!((luna>00) && (luna<13)))
						{
						isOK = false
						sMsg = sMsg + "'" + acF[5][cFCount] + "' must be in this format: yyyy-mm-dd " + CrLf;
						break
						}
					if (!((zi>00) && (zi<32)))
						{
						isOK = false
						sMsg = sMsg + "'" + acF[5][cFCount] + "' must be in this format: yyyy-mm-dd " + CrLf;
						break
						}
					}
				}
			case "dateformat_simple":
				{
					if (document.forms[acF[1][cFCount]].elements[acF[2][cFCount]].value == "" || document.forms[acF[1][cFCount]].elements[acF[2][cFCount]].value == "yyyy-mm-dd")
					{
						break
					}
					ceva = document.forms[acF[1][cFCount]].elements[acF[2][cFCount]].value.split('-')
					an=ceva[0]
					luna=ceva[1]
					zi=ceva[2]
					if (!((an>0000) && (an<4000)))
					{
						isOK = false
						sMsg = sMsg + "'" + acF[5][cFCount] + "' must be in this format: yyyy-mm-dd " + CrLf;
						break
					}
					if (!((luna>00) && (luna<13)))
					{
						isOK = false
						sMsg = sMsg + "'" + acF[5][cFCount] + "' must be in this format: yyyy-mm-dd " + CrLf;
						break
					}
					if (!((zi>00) && (zi<32)))
					{
						isOK = false
						sMsg = sMsg + "'" + acF[5][cFCount] + "' must be in this format: yyyy-mm-dd " + CrLf;
						break
					}
				}
			}
		}
	}
  	if (sMsg != "") 
		alert("Error! In order to procede please correct the following:" + CrLf + CrLf + sMsg);
	return isOK;
}

// invert value
function OnOff(formName, fieldName)
{
  document.forms[formName].elements[fieldName].checked = !document.forms[formName].elements[fieldName].checked
}

function checkDate(formularName,fieldDate,fieldActive)
{
	if (document.forms[formularName].elements[fieldActive].checked) return true
	an = document.forms[formularName].elements[fieldDate].value.substring(0,3)
	alert(an);
}
