String.prototype.trim = function(){
	return( this.replace(new RegExp("^([\\s]+)|([\\s]+)$", "gm"), "") );
}

function loader() {
    var form = document.forms[0];
    var myXsl = document.getElementsByName("myxsl").value;
    
	// zet de focus op eerste veld van het formulier
	var bFound = false;
    for (i=0; i < form.length; i++) {
   		if (form[i].type != 'hidden') {
        	if (form[i].disabled != true) {
				form[i].focus();
				var bFound = true;
			}
		}
		if (bFound == true) break;
	}
}

function formCheckboxChecked(form) {
	var chk = 0;
	for (var i=0; form.elements[i]; i++)
		if (form.elements[i].type == 'checkbox') {
			chk += 1;
			if (form.elements[i].checked) return true;
		}
	if (chk==0) return true; else return false;		
}

function formTextboxFilled(form) {
	for (var i=0; form.elements[i]; i++) {
		var elem = form.elements[i];
		if ((elem.type == 'text' || elem.tagName.toLowerCase() == 'select') && (elem.value.trim() != '')) {
			return true;
		}
	}
	return false;
}

function formYearCorrect(form) {
	var from = 1000;
	var minFrom = 1000;
	var to = 10000;
	var maxTo = 10000;
	for (var i=0; form.elements[i]; i++) {
		if (form.elements[i].name.match("qc_rangefrom_")) {
			if (form.elements[i].value == '') {
				from = minFrom;
			} else {
				from = parseInt(form.elements[i].value);
				if (isNaN(from)) return false; // geen goed jaar
			}
		} else if (form.elements[i].name.match("qc_rangeto_")) {
			if (form.elements[i].value == '') {
				to = maxTo;
			} else {
				to = parseInt(form.elements[i].value);
				if (isNaN(to)) return false; // geen goed jaar
			} 
		}		
	}
	if (from == minFrom && to == maxTo) return true; // geen from/to velden
	if ((from < 1000) || (to < 1000)) return false;
	else if (from > to) return false;
	else return true;
}

function formSortfieldSet(form) {
	var radios = document.getElementsByName("sortfield");
	for (var i=0; i < radios.length; i++) 
		if (radios[i].checked) {
			switch (radios[i].value) {
				case "year_sort":
					form.action = setParam(form.action,'sortdir','desc');
					break;
				case "date":
					form.action = setParam(form.action,'sortdir','desc');
					break;
				case "datum":
					form.action = setParam(form.action,'sortdir','desc');
					break;
				case "oprichting":
					form.action = setParam(form.action,'sortdir','desc');
					break;
				case null:
					form.action = setParam(form.action,'sortdir','desc'); // score
					break;
				default:
					form.action = setParam(form.action,'sortdir','asc');
					break;  
			}
		}
}

// Check of voldoende velden zijn ingevuld voor submit 
function formCheck(form) {
	var myXsl = document.getElementById("myxsl").getAttribute('value');
	
	if (!(formTextboxFilled(form) && formCheckboxChecked(form))) {
		form.action = setParam(form.action,'exception-class','Form'); 
		form.action = setParam(form.action,'exception-message','empty');
		form.action = setParam(form.action,'xsl',myXsl);
	} else if (!formYearCorrect(form)) {
		form.action = setParam(form.action,'exception-class','Form'); 
		form.action = setParam(form.action,'exception-message','year');
		form.action = setParam(form.action,'xsl',myXsl);
	} else {
		formSortfieldSet(form);
	}
}
function formCheckByClick() {
	var form = document.forms[0];
	formCheck(form);
	form.submit();
	return true;
}
function formCheckBySubmit() {
	var form = document.forms[0];
	formCheck(form);
	return true;
}
    
// aangeroepen voor zoekboxje boven in de resultatenlijst
function doSearch(xsl,lang) {
	var form = document.forms[0];
	form.action = setParam(form.action,'lang',lang);
	form.action = setParam(form.action,'xsl',xsl);
	form.submit();
}

// set a parameter in an action, add if not available.
function setParam(action,pname,value) {
	var pat = new RegExp('\&' + pname + '=' + '[^\&]*' + '\&');
	action += '&';
	if (pat.exec(action)) 
		action = action.replace(pat,'&' + pname + '=' + value + '&');
	else
		action += pname + '=' + value + '&';
	return action.substring(0,action.length - 1);
}

function goToResults(col,xsl,lang) {
	var form = document.getElementById('searchForm_' + col);
	form.action = setParam(form.action,'lang',lang);
	form.action = setParam(form.action,'xsl',xsl);
	form.action = setParam(form.action,'page',1);
	form.submit();
}

function goToPage(page, xsl, lang) {
	var form = document.forms[0];
	form.action = setParam(form.action,'lang',lang);
	form.action = setParam(form.action,'xsl',xsl);
	form.action = setParam(form.action,'page',page);
	form.submit();
}

function goToImgPage(xsl,pagenum,imgpagelen) {
	var form = document.forms[0];
	form.action = setParam(form.action,'xsl',xsl);
	form.action = setParam(form.action,'pagenum',pagenum);
	form.action = setParam(form.action,'imgpagelen',imgpagelen);
	form.submit();
}

function goToHit(col, hitNumber, docid, xsl, totalhits, page, lang) {
	var form = document.getElementById('searchForm_' + col);
	if (!form) form = document.getElementById('mailForm');
	form.action = setParam(form.action,'lang',lang);
	if (docid != null) {
		form.hit.value = hitNumber;
		form.totalhits.value = totalhits;
		form.page.value = page;
		form.action = setParam(form.action,'xsl',xsl);
		form.action = setParam(form.action,'docid',docid);
	} else {
		form.action = setParam(form.action,'xsl',xsl);
		form.action = setParam(form.action,'hit',hitNumber);
	}
	form.submit();
}

// ga naar plaatje imgnr uit totalimgs plaatjes binnen dit record.
function goToImg(imgnr, totalimgs, docid) {
	var form = document.forms[0];
	form.action = setParam(form.action,'docid',docid);
	form.action = setParam(form.action,'totalimgs',totalimgs);
	form.action = setParam(form.action,'imgnr',imgnr);
	form.action = setParam(form.action,'imgpagelen',1);
	form.action = setParam(form.action,'xsl','marc_images-detail.xsl');
	form.submit();
}

// aangeroepen voor zoekboxje boven in de resultatenlijst
function reSearch(xsl,lang) {
	var form = document.forms[0];
	form.action = setParam(form.action,'lang',lang);
	form.action = setParam(form.action,'xsl',xsl);
	if (form.page) form.page.value = 1;
	form.submit();
}

function markeerVoorEmail(formid,col,docid,myxsl,preview,mark) {
	var form = document.getElementById(formid);
	form.action = setParam(form.action,'myxsl',myxsl);
	form.action = setParam(form.action,'docid',docid);
	if (preview) {
		form.action = setParam(form.action,'xsl','email-mark.xsl');
		if (mark) 
			form.action = setParam(form.action,'emailmark','1');
		else
			form.action = setParam(form.action,'emailmark','');
	} else {
		form.action = setParam(form.action,'xsl',col + '-detail.xsl');
		form.action = setParam(form.action,'emailmark','1')
	}	
	form.submit();
}

function sendEmail(myxsl) {
	var form = document.getElementById('mailForm');
	var emailPattern = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
	var form = document.getElementById('mailForm');
	if (emailPattern.exec(form.emailadres.value)) {
		form.action = document.location.pathname + "?action=transform";
		form.action = setParam(form.action,'xsl',myxsl);
		form.action = setParam(form.action,'col',form.col.value);
		form.action = setParam(form.action,'docid',form.docid.value);
		form.action = setParam(form.action,'send','1');
	} else {
		form.action = document.location.pathname + "?action=transform&xsl=email-mark.xsl&myxsl=" + myxsl + "&send=0";
	}
	//showFormFields(form);
	form.submit();	
}

// debug info:
function showFormFields(form) {
	var e = form.elements;
	var s = "form action is: " + form.action + "\nFields:\n";
	for (var i = 0; i < e.length; ++i) {
		s = s + e[i].name + ' = ' + e[i].value + '\n';
	}	
	alert(s);
}

function selectUnion() {				
	if (document.getElementById("qc_term_type").value == "j") {
		resetplaatsen();
		document.getElementById("qc_user_plaats").disabled = true;
		document.getElementById("qc_term_provincie").disabled = true;
		document.getElementById("btnshowcities").disabled = true;
	} else {
		resetplaatsen();		
		document.getElementById("qc_user_plaats").disabled = false;
		document.getElementById("qc_term_provincie").disabled = false;
		document.getElementById("btnshowcities").disabled = false;			
	}
}
			
function fillplaatsen(foutmelding) {
	var prov = (document.getElementById("qc_term_provincie").value);
	if (prov != "") {
		document.getElementById("qc_user_plaats").style.display = 'none';
		document.getElementById("divloader").style.display = 'block';
		fill_div("...loading...");
		document.getElementById("loader").src = (document.location.pathname + "?action=transform&xsl=hisvak-cities.xsl&col=hisvak&prov=" + prov);
	} else {
		alert(foutmelding);
	}
}	
function fillberoepen(lang,foutmelding) {
	var bgroep = (document.getElementById("qc_term_beroepsgroep" + lang).value);
	if (bgroep != "") {
		document.getElementById("qc_user_beroep" + lang).style.display = 'none';
		document.getElementById("divloader").style.display = 'block';
		fill_div("...loading...");
		document.getElementById("loader").src = (document.location.pathname + "?action=transform&xsl=strikes-beroepen.xsl&col=strikes&bgroep=" + bgroep + '&lang=' + lang);
	} else {
		alert(foutmelding);
	}
}	
			
function resetplaatsen() {
	fill_div("");
	document.getElementById("divloader").style.display = 'none';
	document.getElementById("qc_user_plaats").value = '';
	document.getElementById("qc_user_plaats").style.display = 'block';
}
function resetberoepen(lang) {
	fill_div("");
	document.getElementById("divloader").style.display = 'none';
	document.getElementById("qc_user_beroep" + lang).value = '';
	document.getElementById("qc_user_beroep" + lang).style.display = 'block';
}
			
function fill_div(content) {
	document.getElementById("divloader").innerHTML = content;			
}
			
function init() {
	document.getElementById("divloader").style.display = 'none';
}

/* Experimental: */
function useSuggestion(page, xsl, lang, suggestion) {
	var form = document.forms[0];
	document.getElementById("qc_user").value = suggestion;
	goToPage(page, xsl, lang);
}

