<!--
//Version: $Id: validators.js,v 1.2 2005/11/30 15:16:12 ropson Exp $

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

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

/** Funkcja sprawdza, czy text jest poprawną datą zapisaną według podanego
formatu dateFormat
*/
function isDate(s,dateFormat){
    if (isEmpty(s))
        return true;
    if (!dateFormat)
        dateFormat='Y-m-d';
    var i,j=0,x1,x2,x3,x4,error=false,c;
    var value=s;
    for (i=0;i<dateFormat.length;i++)
    {
        if (j>=value.length) return false;
        c=dateFormat.charAt(i);
        switch (c)
        {
            case 'Y':
                if (j+3>=value.length) return false;

                x1=value.charAt(j++);x2=value.charAt(j++);x3=value.charAt(j++);x4=value.charAt(j++);
                if (!(isDigit(x1) && isDigit(x2) && isDigit(x3) && isDigit(x4)))
                    return false;
                break;
            case 'm':
                if (j+1>=value.length) return false;
                x1=value.charAt(j++);x2=value.charAt(j++);
                if (!(isDigit(x1) && isDigit(x2)) || new Number(x1+x2)>12 || new Number(x1+x2)<1) return false;
                break;
            case 'd':
                if (j+1>=value.length) return false;
                x1=value.charAt(j++);x2=value.charAt(j++);
                if (!(isDigit(x1) && isDigit(x2)) || new Number(x1+x2)>31  || new Number(x1+x2)<1) return false;
                break;
            case 'H':
                if (j+1>=value.length) return false;
                x1=value.charAt(j++);x2=value.charAt(j++);
                if (!(isDigit(x1) && isDigit(x2)) || new Number(x1+x2)>23) return false;
                break;
            case 'i':
            case 's':
                if (j+1>=value.length) return false;
                x1=value.charAt(j++);x2=value.charAt(j++);
                if (!(isDigit(x1) && isDigit(x2)) || new Number(x1+x2)>59) return false;
                break;
            default:
                if (c!=value.charAt(j++)) return false;
        }
    }
    if (j!=value.length)
        return false;
    return true;
}

function isDateTime(s) {
    return isDate(s, 'Y-m-d H:i:s');
};

function isPostCode(s) {
    if (isEmpty(s)) return true; 
    if (s.length!=6) return false; 
    if (!(isDigit(s.charAt(0)) && isDigit(s.charAt(1)) && s.charAt(2)=='-' && isDigit(s.charAt(3)) && isDigit(s.charAt(4)) && isDigit(s.charAt(5)))) 
        return false;	
    return true;
}

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

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

function isEmail(s) {
    if (isEmpty(s)) return true;
    if (s.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)==-1) 
        return false; 
    return true;
}

function isValidLength(length, obj) {
    if (obj.value.length <= length)
        return true;
    return false;
}

function isRegon(s) {
	if (isEmpty(s)) return true;

	if (s.search(/^[0-9]{7}|[0-9]{9}$/) == -1)
		return false;
	checkSumParts = ((s.length == 9) ? '89234567' : '234567');
	checkSum = 0;
	for (nr = 0; nr < s.length-1; nr++)
		checkSum += parseInt(checkSumParts.charAt(nr))*parseInt(s.charAt(nr));
	return (parseInt(s.charAt(s.length-1)) == (checkSum % 11 % 10));	
}

function isNip(s) {
	if (isEmpty(s)) return true;

	rg  = /\-/g;
	s = s.replace(rg,'');

	if (s.length != 10)
		return false;
	
	steps = new Array(6, 5, 7, 2, 3, 4, 5, 6, 7);
	
	// tworzenie sumy iloczynów
	sum_nb = 0;
	for (x = 0; x < 9; x++)
{	
sum_nb += steps[x] * parseInt(s.charAt(x));
}	
	sum_m = sum_nb % 11;
	
	if (sum_m == parseInt(s.charAt(9))) 
		return true;
	
	return false;
}

//-->
