
var _BadField, _BadFieldClr, _F, _BadFieldColor='#FFFFE0', 
	_BadFieldMsg='Please fill in the field before submitting the form',
	_SubmDisTime=3000, UseParent4CB=false;

function Empty(s){return (s.search(/[^\s]/)==-1);}

function GetParent(Field, N){
var i, Parent;

for(i=0; i<N; i++){
	Parent=Field.parentNode;
	if (Parent==null) return Field;
	Field=Parent;
	}
return Field;
}

function BadField(Field, Msg, NoFocus, bConfirm){
//Give a warning on an incorrect field value, followed by focusing and hilighting the field
var Response, i;

if (Msg==null) Msg=_BadFieldMsg;
if (_BadField) _SetBadFieldColor(true);

if ((Field.type=='checkbox' || Field.type=='radio') && W.UseParent4CB)
	Field=GetParent(Field, UseParent4CB);

_BadField=Field; _SetBadFieldColor();

if (!bConfirm) alert(Msg); else Response=confirm(Msg);
if (!NoFocus) Field.focus(); //Field.scrollIntoView();
setTimeout('_SetBadFieldColor(true)', 250);
return (!bConfirm ? false : Response);
}

function _SetBadFieldColor(bRestore){

if (!_BadField) return;

if (_BadField.tagName=='TR'){
	var aCells=_BadField.cells, aCell;
	for(i=0; i<aCells.length; i++) _SetTagFieldColor(aCells[i], bRestore);
	}
else _SetTagFieldColor(_BadField, bRestore);
	
if (bRestore) _BadField=null;
}

function _SetTagFieldColor(Tag, bRestore){

if (!bRestore){
	Tag.OrigBG=Tag.style.backgroundColor;
	Tag.style.backgroundColor=_BadFieldColor;
	}
else Tag.style.backgroundColor=Tag.OrigBG;
}

function EmptyField(Field, NoMsg, Msg){
//Checks whether the user typed anything into the fiels, if not gives a warning

if (!Field) return false;

var Type=Field.type, Result=false, ind;

if (!Type || Type=='hidden' || Field.disabled) return false;
if (Type=='text' || Type=='textarea' || Type=='password' || Type=='file')	Result=Empty(Field.value);
else if (Type.substring(0, 6)=='select') Result=(Field.selectedIndex<=0);

if (NoMsg==null) NoMsg=false;
if (Result && !NoMsg) BadField(Field, Msg); 
return Result;
}

function CheckInputData(form, Fields, NoMsg, bAny){
//Check that all form fields have some values
//Fields - a list of mandatory or (begins with "-") skipped fields
var Form, Field, Skip, Found;

Form=(form ? form : F);
if (Fields && Fields.charAt(0)=='-'){Skip=true; Fields=Fields.substr(1);}

for(var No=0; No<Form.length; No++){
	Field=Form[No];
	if (!Field.type) continue;
	if (Fields){
		Found=Fields.match(new RegExp('(^|,)'+Field.name+'($|,)'));
		if ((!Skip && !Found) || (Skip && Found)) continue;
		}
	if (!bAny){
		if (EmptyField(Field, NoMsg)) return false;
		}
	else if (!EmptyField(Field, true) && Field.type!='submit' && Field.type!='hidden'){
		return true;
		}
	}

return !bAny;
}

function RadioGet(name, Default, F){
//Returns the checked options from a list of radios
//Default - the value returned when no option is selected, 
//  true means the first option, an object (field) specifies the field directly
var rb, def, fst, opt, i;

if (!F) F=D.F; rb=F[name];

for(i=0; i<rb.length; i++){
	opt=rb[i];
	if (!opt.disabled){
		if (opt.checked) return opt.value;
		if (!fst) fst=opt;
		}
	}

if (Default==null) Default=true;
if (Default){
	def=(typeof(Default)=='object' ? Default : fst);
	def.checked=true; return def.value;
	}
else return null;
}

function RadioSet(name, Value, F){
//Select the option in a list of radios
var rb, i;

if (!F) F=D.F; rb=F[name];
for(var i=0; i<rb.length; i++){	
	if (rb[i].value==Value){rb[i].checked=true; return;}
	}
}

function CBSet(Name, Values){
var aValues, i, Len, Fld;

aValues=Values.split(","); Len=aValues.length;
for(i=0; i<Len; i++){
	Fld=F[Name+'_'+aValues[i]];
	if (Fld) Fld.checked=true;
	}
}

function CBRead(F, Prefix, Format){
//Return checked checkboxes
//Format: {'list', 'arr', 'get', 'any'}
var name, res='', fno, f, FormatNo;

FirstCBField=null;
if (Format=='get') FormatNo=0;
else if (Format=='arr'){
	FormatNo=1; res=[];
	}
else if (Format=='list' || !Format) FormatNo=2;
else FormatNo=3;

for(fno=0; fno<F.length; fno++){
	f=F[fno]; if (f.type!='checkbox') continue;
	name=f.name; 
	if (Prefix){
		if (!StartsWith(name, Prefix)) continue;
		}

	if (!FirstCBField) FirstCBField=f;

	if (f.checked 
		&& !f.disabled){
		if (FormatNo==2) res+=(res!='' ? ',' : '')+f.value;
		else if (FormatNo==1) res[name]=f.value;
		else if (FormatNo==0) res+=(res!='' ? '&' : '')+name+'=1';
		else if (FormatNo==3) return true;
		}
	}
return res;
}

function CommonSubmit(F, Button){

_F=(!F ? W.F : F);
if (!Button) Button='Submit';
setTimeout("_F."+Button+".disabled=true; W.focus();", 1);
setTimeout("_F."+Button+".disabled=false;", _SubmDisTime);
return true;
}

function CheckEmail(Fld, bAllowEmpty){

if (bAllowEmpty && Empty(Fld.value)) return true;
return (
	Fld.value.search(/^[^.]+(\.[^.]+)*@[^.]+(\.[^.]+)+$/)==-1 ? 
	BadField(Fld, 'Enter a correct e-mail address') : true);
}