/****************************************************************************/
//  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 DDXLen(control,minLen,maxLen,errmsg)
{
	if (!control)
		return false;
	
	var s = control.value;
	if (s.length < minLen || s.length > maxLen )
	{
		self.status=errmsg;
		if (control.style.visibility) {
			control.focus();
			control.select();
		}
		alert(errmsg);
		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;
		control.focus();
		control.select();
		alert(errmsg);
		return false;
	}
	
	if ( s.valueOf() < min || s.valueOf() > max )
	{
		self.status=errmsg;
		control.focus();
		control.select();
		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;
		control.focus();
		alert(errmsg);
		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;
		control.focus();
		control.select();
		alert(errmsg);
		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;
		control.focus();
		control.select();
		alert(errmsg);
		return false;
	}	
	return true;		
}

function getRadioValue(control) {
	var answer_id;
	
	if(control.length == undefined)
	{
		if(control.checked) {
			answer_id = answer_id.value;
		} else {
			answer_id = false;
		}
	}
	else
	{
		for(var i=0;i<control.length;i++)
		{
			if(control[i].checked)
			{
				answer_id = control[i].value;
				break;
			}
		}
	}
	
	return answer_id;
}

function DDXRadio(control, errmsg) {
	if(getRadioValue(control) == undefined) {
		alert(errmsg);
		return false;
	}
	
	return true;
}
/*
* Fuction to compare two values, exxample email or password confirmation
* Added by L. Albrzykowski
*/ 
function DDXCompare(firstValue,secondValue,errmsg) {
	if(firstValue.value==secondValue.value){
		return true;
	}else{
		alert(errmsg);
	}
}


