/*
This series of functions provide common form field validation.  You customize the "validateForm" function to make calls to the following functions with your form fields.  If the validation fails, the functions will alert the user with a message and put the text input focus on the field with the error.

validRequired(formField, fieldLabel) -- checks for a field being empty
validEmail(formField, fieldLabel, required) -- checks for a correctly formatted email address
validNum(formField, fieldLabel, required) -- checks for a valid number (a string where all the characters are digits)
validInt(formField, fieldLabel, required) -- checks for a number (integer) that can be correctly parsed with the function parseInt
The parameters to these functions are defined as:

formField -- the document reference to your form field (e.g., myform.fieldName)
fieldLabel -- the user friendly string that you want the error message to refer to your field by
required -- whether the field is required to be filled in by the user (true/false) 

*/

var dvs_script_visible = false;

function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function validRequired(formField,fieldLabel)
{
	var result = true;
	
	if (formField.value == "")
	{
		alert('Proszę wprowadzić warto¶ć dla pola "' + fieldLabel +'".');
		formField.focus();
		result = false;
	}
	
	return result;
}

function allDigits(str)
{
	return inValidCharSet(str,"0123456789");
}

function inValidCharSet(str,charset)
{
	var result = true;

	// Note: doesn't use regular expressions to avoid early Mac browser bugs	
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}
	
	return result;
}

function validEmail(formField,fieldLabel,required)
{
	var result = true;
	
	if (required && !validRequired(formField,fieldLabel))
		result = false;

	if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
	{
		alert("Proszę wprowadzić prawidłową wartość adresu email w następującej postaci: nazwa@domena.com.pl");
		formField.focus();
		result = false;
	}
   
  return result;

}

function validNum(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		if (!inValidCharSet(formField.value,"0123456789., "))
 		{
 			alert('Proszę wprowadzić liczbę dla pola "' + fieldLabel +'".');
			formField.focus();		
			result = false;
		}
	} 
	
	return result;
}


function validInt(formField,fieldLabel,required)
{
	var result = true;

	if (required && !validRequired(formField,fieldLabel))
		result = false;
  
 	if (result)
 	{
 		var num = parseInt(formField.value,10);
 		if (isNaN(num))
 		{
 			alert('Proszę wprowadzić liczbę dla pola "' + fieldLabel +'".');
			formField.focus();		
			result = false;
		}
	} 
	
	return result;
}

/*
The TextArea Max script provides handlers that you can attach to a TextArea form element. 
The handler functions limit the number of characters a user can type and display the number of 
characters remaining in a counter field. 
*/

function TrackCount(fieldObj,countFieldName,maxChars)
{
  if (fieldObj.value.length > maxChars)
  {
    fieldObj.value = fieldObj.value.substring(0,maxChars);
  }
}

function LimitText(fieldObj,maxChars)
{
  var result = true;
  if (fieldObj.value.length >= maxChars)
    result = false;
  
  if (window.event)
    window.event.returnValue = result;
  return result;
}
  
function checkNumeric(objName,minval, maxval,comma,period,hyphen)
{
	var numberfield = objName;
	if (chkNumeric(objName,minval,maxval,comma,period,hyphen) == false)
	{
		numberfield.select();
		numberfield.focus();
		return false;
	}
	else
	{
		return true;
	}
}

function chkNumeric(objName,minval,maxval,comma,period,hyphen)
{
    // only allow 0-9 be entered, plus any values passed
    // (can be in any order, and don't have to be comma, period, or hyphen)
    // if all numbers allow commas, periods, hyphens or whatever,
    // just hard code it here and take out the passed parameters

    var checkOK = "0123456789" + comma + period + hyphen;
    var checkStr = objName;
    var allValid = true;
    var decPoints = 0;
    var allNum = "";

    for (i = 0;  i < checkStr.value.length;  i++)
    {
        ch = checkStr.value.charAt(i);
        for (j = 0;  j < checkOK.length;  j++)
        if (ch == checkOK.charAt(j))
        break;
        if (j == checkOK.length)
        {
            allValid = false;
            break;
        }
        if (ch != ",")
        allNum += ch;
    }
    if (!allValid)
        {	
        alertsay = "Please enter only these values \""
        alertsay = alertsay + checkOK + "\" in the \"" + checkStr.name + "\" field."
        alert(alertsay);
        return (false);
    }

    // set the minimum and maximum
    var chkVal = allNum;
    var prsVal = parseInt(allNum);
    if (chkVal != "" && prsVal!=0)
    {
        alertsay2 = '';
        if ((minval!=0 && prsVal < minval) || (maxval!=0 && prsVal > maxval))
        {
            if (minval!=0)
                alertsay2 += "większa od wartości "+(minval-1);

            if (maxval!=0)
            {
                if (alertsay2 != '') alertsay2 += ' i ';
                alertsay2 += "mniejsza od wartości "+(maxval+1);
            }
            
            if (alertsay2 != '')
            {
                alert("Wprowadzona wartość powinna być " + alertsay2);
                return (false);
            }
        }
    }

}//chkNumeric(objName,minval,maxval,comma,period,hyphen)

function valButton(btn) {
    var cnt = -1;
    for (var i=btn.length-1; i > -1; i--) {
        if (btn[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1) return btn[cnt].value;
    else return null;
}
                  
function IsValidTimeE(timeElement, allowEmpty)
{

  var timeStr = timeElement.value;
  if (timeStr == '') 
  {
    if (allowEmpty==false)
          alert("Nieprawidłowy format czasu. Prawidłowo wprowadzony czas powinien mieć formę np: 23:59");

    return allowEmpty;
  }

  // Checks if time is in HH:MM:SS AM/PM format.
  // The seconds and AM/PM are optional.

  var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

  var matchArray = timeStr.match(timePat);
  if (matchArray == null) {
    alert("Nieprawidłowy format czasu. Prawidłowo wprowadzony czas powinien mieć formę np: 23:59");
    timeElement.focus();
    return false;
  }
  hour = matchArray[1];
  minute = matchArray[2];
  second = matchArray[4];
  ampm = matchArray[6];

  if (second=="") { second = null; }
  if (ampm=="") { ampm = null }

  if (hour < 0  || hour > 23) {
    alert("Godziny musz± być pomiędzy 0 a 23");
    timeElement.focus();
    return false;
  }
  if  (hour > 12 && ampm != null) {
    alert("You can't specify AM or PM for military time.");
    timeElement.focus();
    return false;
  }
  if (minute<0 || minute > 59) {
    alert ("Minuty musz± być pomiędzy 0 a 59.");
    timeElement.focus();
    return false;
  }
  if (second != null && (second < 0 || second > 59)) {
    alert ("Sekundy musz± być pomiędzy 0 a 59.");
    timeElement.focus();
    return false;
  }

  return true;
}

function IsValidTime(timeElement)
{
  return IsValidTimeE(timeElement, true);
}


