var client;

function EditorSave(obj) {
	var f = document.getElementById(obj);

	if (EditorSubmit(obj)) {
		f.submit();
	}

	return false;
}

function EditorPrev(obj) {
	var f = document.getElementById(obj);

	//if (EditorSubmit(obj)) {
		f.editorstatus.value='prev';
		f.submit();
	//}
	return false;
}

function EditorNext(obj) {
	var f = document.getElementById(obj);
	
	if (EditorSubmit(obj)) {
		f.editorstatus.value='next';
		f.submit();
	}
	return false;
}

function EditorStatusSubmit(obj,astatus) {
	var f = document.getElementById(obj);
	f.editorstatus.value=astatus;
	f.submit();
	return false;
}

function EditorFormInit(aForm) {
	return;
	/*var f = document.getElementById(aForm);
	for (var i=0;i<f.elements.length;i++) {
		//f.elements[i].addEventListener('blur',_Blur,false);
		//f.elements[i].onblur=new Function("EditorValidate(this.form,this,false);");
		//f.elements[i].onkeypress=new Function("event","EditorKeyPress(this.form.id,event);");
	}*/
}

function EditorSubmit(obj) {
	var f = document.getElementById(obj);
	var good = true;

	var lines = '';

	var inputs = f.getElementsByTagName("input");
	for (var i=0;i<inputs.length;i++) {
		var tmp = document.getElementById('force'+inputs[i].name);
		if (inputs[i].type!='hidden' || tmp!=null)
			good = good & EditorValidate(f,inputs[i],good);
		//if (!good) break;
	}

	//if (good) {
		inputs = f.getElementsByTagName("select");
		for (var i=0;i<inputs.length;i++) {
			good = good & EditorValidate(f,inputs[i],good);
			//if (!good) break;
		}
	//}

//	if (good)
//		f.editorstatus.value='ok';

	return good;
}

function EditorValidate(aForm,aEl,aFocusOnError) {
	var good = true;
	var validatorwith = document.getElementById('val'+aEl.name+'with');
	var validator = document.getElementById('val'+aEl.name);
	if (validator!=null) {
		var tmp = null;
		var name=validator.value;
		var value=aEl.value;
		if (aEl.type=='checkbox' && !aEl.checked)
			value='';
		if (validatorwith!=null) {
			var elwith=document.getElementById(validatorwith.value);
			tmp = eval('EditorVal'+name+'(value,elwith.value,validatorwith);');;
		} else {
			tmp = eval('EditorVal'+name+'(value,null,null);');;
		}
		if (!tmp) {
			EditorErrorDiv(aEl,true);
			if (aFocusOnError)
				aEl.focus();
		} else
			EditorErrorDiv(aEl,false);
		good = (good && tmp);
	}
	return good;
}

function EditorErrorDiv(aEl,vis) {
	var d = document.getElementById('error'+aEl.name);
	if (d!=null)
		if (vis) {
			var par = aEl.parentNode;
			d.style.display='inline';
			//d.style.position='absolute';
			aEl.style.background='#ff2020';
			par.insertBefore(d,null);
		} else {
			aEl.style.background='white';
			d.style.display='none';
		}
}

function EditorCancel(obj) {
	obj = document.getElementById(obj);
	obj.editorstatus.value='cancel';
	obj.submit();
	return false;
}

function EditorDelete(obj) {
	obj = document.getElementById(obj);
	if (confirm('Kaart verwijderen?')) {
		obj.editorstatus.value='delete';
		obj.submit();
	}
	return false;
}

function EditorValReg(aValue,aValueWith,aValidator) {
	return true;
}

function EditorValTextonly(aValue,aValueWith,aValidator) {
	return aValue.match(/^[a-z0-9 -\/]+$/i)!=null;
}

function EditorValNumeric(aValue,aValueWith,aValidator) {
	return aValue.match(/^[0-9]+$/i)!=null;
}

function EditorValdate(aValue,aValueWith,aValidator) {
	return aValue.match(/^([0-9]{2}-[0-9]{2}-[0-9]{4})$|^$/)!=null;
}

function EditorValNotempty(aValue,aValueWith,aValidator) {
	return aValue.length>=1;
}

function EditorValMustbepositive(aValue,aValueWith,aValidator) {
	return aValue>0;
}

function EditorValPasswordeq(aValue,aValueWith,aValidator) {
	return true;
}

function EditorValCompare(aValue,aValueWith,aValidator) {
	return aValue==aValueWith;
}

function EditorValEmail(aValue,aValueWith,aValidator) {
	return aValue.match(/\b(^(\S+@).+(\..{2,4})$)\b/gi)!=null;
}

function EditorValZipcode(aValue,aValueWith,aValidator) {
	var res=aValue.match(/^[0-9]{4} [a-z]{2}$/gi)!=null;
	if (res) EditorValZipcodeWith(aValue,aValueWith,aValidator);
	return res;
}

function EditorValZipcodeWith(aValue,aValueWith,aValidator) {
	if (aValidator!=null) {
		var res=aValue.match(/^[0-9]{4}/gi);
		client = new XMLHttpRequest();
		client.onreadystatechange = EditorValZipcodeWithHandler;
		client.open("GET","zipcodelookup.php?l="+aValidator.value+"&z="+res,true);
		client.send('');
	}
}

	function EditorValZipcodeWithHandler() {
		if (client.readyState==4 && client.status == 200) {
			var res = client.responseText.split(';');
			var o = document.getElementById(res[0]);
			if (o!=null)
				o.value=res[1];
		}
	}

function EditorValRte(aValue) {
	updateRTEs();
	return true;
}

function EditorKeyPress(aForm,e) {
	var key = window.event ? e.keyCode : e.which;
	if (key==13) {
		if (document.getElementById(aForm+'disablereturn')!=null)
			return false;
		if (document.getElementById(aForm+'next')!=null) {
			return EditorNext(aForm);
		}	else
			return EditorSave(aForm);
	}
	return true;
}

function EditorCurrencyFormat(aValue) {
	var tmp = '';var chars=0;var i=0;
	aValue=parseFloat(aValue).toFixed(2).toString();
	for (i=aValue.length-1;i>=0;i--) {
		if (chars++==2) tmp=','+tmp;
		if (chars>4 && (chars-2)%3==2) tmp='.'+tmp;
		if (aValue.charAt(i)>='0' && aValue.charAt(i)<='9')
			tmp=aValue.charAt(i)+tmp;
	}
	return tmp;
}

function EditorValRekening(aValue) {
	var i=0;
	var total=0;
	var tmp = '';

	if (!aValue.match(/^(P[0-9]{6,12})|([0-9\.]{6,12})$/gi)) return false;

	if (aValue.match(/^[0-9\.]{6,12}$/gi)) {
		for (i=0;i<aValue.length;i++)
			if (aValue.charAt(i)>='0' && aValue.charAt(i)<='9')
				tmp+=aValue.charAt(i);
		while (tmp.length<10) tmp='0'+tmp;
		if (tmp.length>10) return false;
		for (i=10;i>1;i--)
			total+=(parseInt(tmp.charAt(i-1))*i);
		if ((total%11)!=0) return false;
	} else
	if (aValue.length<6)
		return false;
	return true;
}

function EditorHideTabs(obj) {
	var f = document.getElementById(obj);
	var inputs = f.getElementsByTagName('div');
	var txt = '';
	for (var i=0;i<inputs.length;i++) {
		txt += inputs[i].id.substr(0,3);
		if (inputs[i].id.substr(0,3)=='tab')
			inputs[i].style.display='none';
	}
	return false;
}

function EditorShowTab(obj,aTab) {
	EditorHideTabs(obj);
	var obj = document.getElementById(aTab);
	obj.style.display='block';
	return false;
}
