// JavaScript Document

function checkForm(bOpenWindow) {
	if (bOpenWindow == null ) {
		bOpenWindow = false;
	}
	
	var errstr = "";
	var myform = document.fmZoeken;
	var mandatory = false;
	var today = new Date();
	
	errstr = errstr + checkDate(myform.tfBegindatum, "de begindatum", true);
	errstr = errstr + checkDate(myform.tfEinddatum, "de einddatum", true);
	
	// Compare begindatum with today
	if (myform.tfBegindatum.value != "" && myform.tfEinddatum.value != "") {
		month = 1 + today.getMonth();
		compdate = compareDates(myform.tfBegindatum.value, ""+today.getDate()+"-"+month+"-"+today.getFullYear());
		if (compdate < 0) {
			errstr = errstr + "De begindatum is in het verleden. Geef a.u.b. een latere datum.\n";
		}else{
			if (compdate == 0) {
				errstr = errstr + "U zoekt op een beschikbaarheid van te verhuren motoren vanaf vandaag.\n"
					  + "Echter, u kunt geen motoren reserveren via deze site vanaf vandaag,\n"
					  + "maar op z'n vroegst na 24 uur.\n";
			}
		}
			
		// Compare dates
		if (compareDates(myform.tfBegindatum.value, myform.tfEinddatum.value) > 0) {
			errstr = errstr + "De begindatum is later dan de einddatum.\nGeef a.u.b. een geldige periode.\n";
		}
	}
	
	if (myform.tfPostcode) {
		myform.tfPostcode.value = myform.tfPostcode.value.toUpperCase();
		errstr = errstr + checkRegexp(myform.tfPostcode, "uw volledige postcode, bijv. 1234 AA", /^\d{4}\s?[A-Z][A-Z]$/);
	}
	
	if (errstr == "") {
		if (bOpenWindow) {
			window.open('', 'winResults', 'titlebar=0,resizable=1,width=820,height=600,scrollbars=1');
			myform.target = 'winResults';
			myform.submit();
			return false;
		}else{
			myform.submit();
			return false;
		}
	}

	alert(errstr);
	if (focusfield && focusfield != null) {
		focusToFocusfield();
	}
	return false;
}

function submitForm(bWindowOpen) {
	if (checkForm(bWindowOpen)) {
		document.fmZoeken.btnSubmit.disabled = true;
		document.fmZoeken.offset.value=1;
		document.fmZoeken.submit();
		//startSearch();
	}
	return false;
}

function invalidateForm() {
	document.fmZoeken.formchanged.value = 1;	
}

function changeSortOrder(sortname) {
	var myform = document.fmZoeken;
	var currsort = myform.sort_on;
	var currascend = myform.ascend;
	
	if (checkForm()) {
		if (currsort.value != sortname) {
			currsort.value = sortname;
			myform.offset.value = "1";
			currascend.value = "ASC";
			myform.submit();
		}else{
			myform.offset.value = "1";
			if (currascend.value == "ASC") {
				currascend.value = "DESC";
			}else{
				currascend.value = "ASC";
			}
			myform.submit();
		}
	}
}

function showMotor(id_motor) {
	var myform = document.fmZoeken;
	
	if (myform) {
		document.fmZoeken.id_motor.value = id_motor;
		tmpform = document.fmZoeken.action;
		document.fmZoeken.action = "motordetails.cfm";
		document.fmZoeken.submit();
		document.fmZoeken.action = tmpform;
	}
}

function showPrev(nofrows) {
	var offset = document.fmZoeken.offset.value;
	
	//if (checkForm()) {
		if (parseInt(offset, 10) > 1) {
			offset = parseInt(offset, 10) - parseInt(nofrows, 10);
			if (offset < 1) offset = 1;
			document.fmZoeken.offset.value = offset;
			document.fmZoeken.submit();
		}
	//}
}

function showNext(nofrows, maxrows) {
	var offset = document.fmZoeken.offset.value;
	
	//if (checkForm()) {
		offset = parseInt(offset, 10) + parseInt(nofrows, 10);
		if (offset <= maxrows) {
			document.fmZoeken.offset.value = offset;
			document.fmZoeken.submit();
		}
	//}
}

function markthiscell(cellobj, markon) {
	if (markon) {
		cellobj.style.borderWidth = 1;
		cellobj.style.borderColor = "#0099ff";
		cellobj.style.borderStyle = "solid";
	}else{
		cellobj.style.borderWidth = 0;
	}
}

function mark(tableobj, markon) {
	if (markon) {
		tableobj.className = "motorlist mark";
	}else{
		tableobj.className = "motorlist unmark";
	}
}

function updateSelectBoxes(datefield, dayfield, monthfield, yearfield) {
	var myform = document.fmZoeken;
	var strdate = myform[datefield].value;
	var arrdate = strdate.split(/-/);
	
	if (arrdate.length == 3) {
		var today = new Date();
		
		myform[dayfield].selectedIndex = parseInt(arrdate[0], 10) - 1;
		myform[monthfield].selectedIndex = parseInt(arrdate[1], 10) - 1;
		
		idx = parseInt(arrdate[2], 10) - today.getFullYear();
		if (idx >= 0 && idx < myform[yearfield].options.length) {
			myform[yearfield].selectedIndex = idx;
		}else{
			alert("Gekozen jaartal is te ver in de toekomst");
		}
	}
}

function updateDateField(datefield, dayfield, monthfield, yearfield) {
	var myform = document.fmZoeken;
	
	myform[datefield].value = myform[dayfield].value + '-' + myform[monthfield].value + '-' + myform[yearfield].value;
}

var searchmsg_obj = null;
function searchForMotors(id_zoeken, id_blokjes, id_resultaten, maxseconds, symbol) {
	if (searchmsg_obj == null) {
		searchmsg_obj = getObject(id_blokjes);

		robj = getObject(id_zoeken);
		robj.style.display = "block";
	}
	
	if (searchmsg_obj) {
		if (maxseconds == 0) {
			robj = getObject(id_zoeken);
			robj.style.display = "none";
			
			searchmsg_obj.innerHTML = "";
			searchmsg_obj = null;
			
			//document.fmZoeken.btnSubmit.disabled = false;
			//if (document.fmZoeken) document.fmZoeken.submit();
			return;
		}
		
		imgobj = document.createElement('img');
		imgobj.setAttribute("alt", "");
		imgobj.setAttribute("src", symbol);
		imgobj.style.verticalAlign = "bottom";
		
		if (imgobj) {
			searchmsg_obj.appendChild(imgobj);
		}

		maxseconds = maxseconds - 1;
		setTimeout("searchForMotors('"+id_zoeken+"','"+id_blokjes+"','"+id_resultaten+"',"+maxseconds+",'"+symbol+"');", 750);
		setTimeout('if (document.fmZoeken) document.fmZoeken.submit();', 3500);
	}
}

function startSearch() {
	searchForMotors('divid_zoeken', 'spanid_zoeken', 'divid_resultaten', 9, '/images/blokje.gif');
}

function scrollToTop(){
	window.scrollTo(0,0)
}
