/****************************************************************************/
//  DDX - dynamic data exchange
//
//  Description:
//  Validation wrappers for forms
//  
//  Author: Ralph Lorenc 
//  E-Mail: r.lorenc@it-system.pl	 
//  (c) 2005-2006, ITSS Rafał Lorenc 
//  http://www.itss.com.pl (.com) (.de)
//	   
//  All right reserved. Any portions of this code cannot be 
//  used in any form  without licence by ITSS company.  
/****************************************************************************/

function DDXRadio(control,errmsg) {
	if (!control)
		return false;
	
	for (var option = 0; option < control.length; option++) {
		if (control[option].checked) return true;
	}
	self.status=errmsg;
	alert(errmsg);
	//control.focus();
	return false;
}

function DDXValidDate(control_day,control_month,control_year,errmsg) {
    
	var day = control_day.value;
    //var month = control_month.options[control_month.selectedIndex].value;
	var month = control_month.value;
    var year = control_year.value;

	var maxDay = new Array("", 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	if (year % 4 == 0) maxDay[2] = 29;
	
	if (day > maxDay[month]) {
		self.status=errmsg;
		alert(errmsg);
		control_day.focus();
		return false;
	}

	return true;

}

function DDXLen(control,minLen,maxLen,errmsg)
{
	if (!control)
		return false;
	
	var s = control.value;
	if (s.length < minLen || s.length > maxLen )
	{
		self.status=errmsg;
		alert(errmsg);
		control.focus();
		control.select();
		return false;
	}
	return true;
}

function removeNL(s){
  return s.replace(/[\n\r\t]/g,'');
}
//validate string length without newlines chars
function DDXLenRNL(control,minLen,maxLen,errmsg){
    if (!control)
		return false;
	
	var s = removeNL(control.value);
	if (s.length < minLen || s.length > maxLen )
	{
		self.status=errmsg;
		alert(errmsg);
		control.focus();
		control.select();
		return false;
	}
	return true;
}

function DDXLenNoLimit(control,minLen,errmsg)
{
	if (!control)
		return false;
	
	var s = control.value;
	if (s.length < minLen)
	{
		self.status=errmsg;
		alert(errmsg);
		control.focus();
		control.select();
		return false;
	}
	return true;
}
	
function DDXInt(control,min,max,errmsg)
{
	if (!control)
		return false;
	
	var s = control.value;
	
	if ( isNaN (s,10)  )
	{
		self.status=errmsg;
		alert(errmsg);
		control.focus();
		control.select();
		return false;
	}
	
	if ( s.valueOf() < min || s.valueOf() > max )
	{
		self.status=errmsg;
		alert(errmsg);
		control.focus();
		control.select();
		return false;
	}
	return true;
}


function DDXSelect2(control,errmsg)
{
	if (!control)
		return false;
	
	var s = control.selectedIndex; // index > 1; modyfikowane przez JN na potrzeby Feeding For Life
	if ( s < 1)
	{
		self.status=errmsg;
		control.focus();
		alert(errmsg);
		return false;
	}
	return true;
}

function DDXSelect(control,errmsg)
{
	if (!control)
		return false;
	
	var s = control[control.selectedIndex].value;
	if ( s.length < 1)
	{
		self.status=errmsg;
		alert(errmsg);
		control.focus();
		return false;
	}
	return true;
}


function DDXChecked(control,nochecked,errmsg)
{
	if (!control)
		return false;
	
	var l = control.length;
	var bRet = false;
	
	if ((!control.length || control.length == 0) &&  nochecked==1) {
		bRet = control.checked;
	}
	if (!bRet ) {
		for ( i=0;i<l;i++ ) {
			if ( control[i].checked ) {
				nochecked--;
			}
		}
		bRet = (nochecked == 0);
	}
	if ( !bRet )
		alert(errmsg);
		
	return bRet;
}


function DDXEMail(control, errmsg)
{
	if (!control)
		return false;
	
	var s = control.value;
	var rE = /^[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
	var mArr = s.match(rE);
	if (mArr == null) 
	{
		self.status=errmsg;
		alert(errmsg);
		control.focus();
		control.select();
		return false;
	}	

	return true;		
}

function DDXRExp(control, rE, errmsg)
{
	if (!control)
		return false;
	
	var s = control.value;
	var ms
	var mArr = s.match(rE)
	if (mArr == null) 
	{
		self.status=errmsg;
		alert(errmsg);
		control.focus();
		control.select();
		return false;
	}	
	return true;		
}



