function fCollectAndValidate(bInit)
{
	try{return fCollectAndValidate1(bInit);}
	catch(e){alert("javascript exception occurred:name=["+e.name+"] message=["+e.message+"]");return false;}
}

function fCollectAndValidate1(bInit)
{
	aCError=new Array;
	str=document.getElementById("$.form.validate$").innerHTML;
	xmlLoad(str);
	vElems=xmlGetElemByName(gvXmlDoc,"elem");
	for (i=0;i<vElems.length;i++){
		vElem=vElems[i];
		strId=xmlGetAttrVal(vElem,"_id");
		if(bInit){
			document.getElementById(strId).value=xmlGetAttrVal(vElem,"value");
			continue;
		}
		vFormElem=document.getElementById(strId);
		strVal=vFormElem.value;
		strType=xmlGetAttrVal(vElem,"type");
		strInterRel=xmlGetAttrVal(vElem,"interRel");
		strL=xmlGetAttrVal(vElem,"lMin");
		if(strL&&strVal.length<strL){
			if(vFormElem.type=="password")strVal="***PWD***";
			aCError.push(new CError(strId,strVal+" must have at least a length of "+strL));
			continue;
		}
		strL=xmlGetAttrVal(vElem,"lMax");
		if(strL&&strVal.length>strL){
			if(vFormElem.type=="password")strVal="***PWD***";
			aCError.push(new CError(strId,strVal+" must have at most a length of "+strL));
			continue;
		}
		if(strType.charAt(0)=='_')strType=strType.substr(1);
		switch(strType){
			case "email":str=".+@.+\\..+";break;
			case "i":str="\\d+";break;
			case "s":str=".*";break;
			default:str=strType;
		}
		i0=(strVal?strVal.search("^"+str+"$"):0);
		if(i0<0){
			if(vFormElem.type=="password")strVal="***PWD***";
			aCError.push(new CError(strId,strVal+" must be of type "+strType+" "+str));
			continue;
		}
		if(strInterRel&&strInterRel.charAt(0)=='='){
			strVal0=document.getElementById(strInterRel.substr(1)).value;
			if(strVal!=strVal0){
				if(vFormElem.type=="password")
					aCError.push(new CError(strId,"password mismatch"));
				else
					aCError.push(new CError(strId,strVal+" is different from "+strVal0));
				continue;
			}
		}
	}
	if(bInit){fErrDisplay(bInit);return false;}
	vElemErr=document.getElementById("$.form.err.summary$");
	if(!aCError.length){
		vElemErr.style.display="none";
		return true;
	}
	vElem=document.getElementById("$.form.err.summary.errors$");
	vElem.innerHTML="";
	for(i=0;i<aCError.length;i++) {
			vElem.innerHTML+="<div _id="+aCError[i].m_strId+">"+aCError[i].m_strMsg+"</div>";
	}
	fErrDisplay(bInit);
	return false;
}
function fErrDisplay(bInit)
{
	vElemInfo=document.getElementById("$.form.err.summary.info$");
	vElemErrors=document.getElementById("$.form.err.summary.errors$");
	if(!vElemErrors.childNodes.length)return;
	vElemInfo.parentNode.style.display="inline";
	//vElemInfo.innerHTML="there is "+vElemErrors.childNodes.length+" errors ("+(bInit?"SERVER":"CLIENT")+" validation)";
	for(i=0;i<vElemErrors.childNodes.length;i++){
		//vElemInfo.innerHTML+="<br>["+vElemErrors.childNodes[i].attributes["_id"].value+"]"+vElemErrors.childNodes[i].innerHTML;
		//alert(vElemErrors.childNodes[i].innerHTML);
	}
}

function find(e,strAttr,strVal,aElts,strNames)
{
var i,bSkip;

if(strNames&&strNames.indexOf(";"+e.nodeName.toLowerCase()+";")<0)
	bSkip=true;
aAttrs=e.attributes;
for(i=0;!bSkip&&i<aAttrs.length;i++){
	if(aAttrs[i].name.search("^"+strAttr+"$")<0)continue;
	if(aAttrs[i].value.search("^"+strVal+"$")<0)continue;
	aElts[aElts.length]=e;
	break;
}
for(i=0;i<e.childNodes.length;i++){
	if(e.childNodes[i].nodeType!=1)continue;
	find(e.childNodes[i],strAttr,strVal,aElts,strNames);
}
}

function xmlLoad(str)
{
	try{
		gvXmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		gvXmlDoc.async="false";
		gvXmlDoc.loadXML(str);
		gbIe=true;
	}
	catch(e){
		vParser=new DOMParser();
		gvXmlDoc=vParser.parseFromString(str,"text/xml");
	}
}
function xmlGetElemByName(vElem,str)
{
	if(gbIe)str=str.toUpperCase();
	return vElem.getElementsByTagName(str);
}

function xmlGetAttrVal(vElem,str)
{
	if(!gbIe)str=str.toLowerCase();
	vElem0=vElem.attributes.getNamedItem(str);
	return (vElem0?vElem0.nodeValue:null);
}

function CError(strId,strMsg)
{
	this.m_strId=strId;
	this.m_strMsg=strMsg;
}

function drawCircle(radius, color, id){
			var circle = document.getElementById(id);
			if (!circle) return;
			circle.style.width  = (2 * radius) + 'px';
			circle.style.height = (2 * radius) + 'px';
			var index = 0;
			for (index = 0; index < (2 * radius); index ++){
				var line = document.createElement('p');
				line.appendChild(document.createTextNode(' ')); 
				line.style.border = 'none';
				line.style.borderSpacing = '0px';
				line.style.padding = '0px';
				line.style.height = '1px';
				line.style.fontSize = '0px';
				line.style.backgroundColor = color;
				var r = Math.abs(radius - index);
				var width = 2 * Math.round(Math.sqrt(radius * radius - r * r));
				line.style.margin = '0px ' +  (radius - width / 2) +
									'px 0px ' + (radius - width / 2) + 'px';
				line.style.width = width + 'px';
				circle.appendChild(line);
			}
}
