 
var tgt;
var curstartpos = 0;
var currendpos = 0;
var thdir = 'th170';


//========================================================= Functions to submit a search form =================================================================

function setSearch(f, retainCurrent, doSubmit){
	tgt = document.mainsearchform;
	
    // check of advanced formulier wel werkt bestaat om js filter te kunnen uitvoeren
	if (typeof(document.advancedsearchform) != 'undefined')
	{
		
	}
	
	
	//get criteria
	if(!setCriteria(f, retainCurrent))
		return false;
	
	//get imagetype and budget
	if(!setImagetype(f, retainCurrent))
		return false;
		
	//get orientations
	if(!setOrienations(f, retainCurrent))
		return false;
		
	//get colortypes
	if(!setColortype(f, retainCurrent))
		return false;
		
	//get brandfilter
	if(!setBrandfilter(f, retainCurrent))
		return false;
		
	//get vehiclefilter if used
	if(!setVehicles(f, retainCurrent))
		return false;
		
	//get startpos, maxrows
	if(!setPositions(f, retainCurrent))
		return false;
	
	//get advancedsearch
	if(!setAdvancedSearch(f, retainCurrent))
		return false;
	
		
	//submit the search
	if(doSubmit!=0){
		tgt.submit();	
	}
	
	return false;
}

function addtocustomhidden(value,targetf){
	if (value != undefined && value !=""){
		document.getElementById(targetf).value = value
	}
}

	
function addtocustom(fieldvalue){
	
	if (fieldvalue != undefined && fieldvalue !=""){
		var targetfield = document.getElementById("advancedsearchform_F_searchinputAll").value;
		if (targetfield.indexOf(fieldvalue,0)==-1){
			document.getElementById("advancedsearchform_F_searchinputAll").value = targetfield + ' ' + fieldvalue
			// targetfield = targetfield + ' ' + fieldvalue;
		}
	}
}
	
function get_radio_value(fieldname,addkeyword){

	f = document.advancedsearchform;
	var rad_val ="";
	if (typeof (f.elements[fieldname]) != 'undefined'){ 
	
		usefield = f.elements[fieldname];
		
		for (var i=0; i < usefield.length; i++){
			if (usefield[i].checked){
				var rad_val = usefield[i].value;
				if (rad_val.length > 2 && fieldname !='color' && addkeyword ==1){
					addtocustom(rad_val)
				} 
			}
	   }
   }
   return rad_val;
}


function setCriteria(f, retainCurrent){
	//find if search is new, or in results
	nwsearch = 1;
	if(typeof(f.newinresRad) != 'undefined'){// there is a radiobuttno for new search / search in results. value 1 is inres, 0 is new
		for(x=0; x<f.newinresRad.length; x++){
			if(f.newinresRad[x].value==1 && f.newinresRad[x].checked)
				nwsearch = 0;
		}
	}
	
	//do criteriaAll
	if(typeof(f.searchinputAll) != 'undefined'){
		clearFld(f.searchinputAll, 'Zoekwoorden ');//default string is removed if its in there.
		val = f.searchinputAll.value;

		if(nwsearch==1 && val!='')
			tgt.criteriaAll.value = val;
		else if(nwsearch==1)
			tgt.criteriaAll.value = tgt.criteriaAllCur.value;
		else{
			tgt.criteriaAll.value = mergeKW(tgt.criteriaAllCur.value, val);
		}
	}
	else if(retainCurrent){
		tgt.criteriaAll.value = tgt.criteriaAllCur.value;
	}

	//do criteria
	if(typeof(f.searchinput) != 'undefined'){
		val = f.searchinput.value;
		if(nwsearch==1)
			tgt.criteria.value = val;
		else
			tgt.criteria.value = mergeKW(tgt.criteriaCur.value, val);
	}
	else if(retainCurrent){
		tgt.criteria.value = tgt.criteriaCur.value;
	}
	
	//do criteriaNot
	if(typeof(f.searchinputNot) != 'undefined'){
		val = f.searchinputNot.value;
		if(nwsearch==1)
			tgt.criteriaNot.value = val;
		else
			tgt.criteriaNot.value = mergeKW(tgt.criteriaNotCur.value, val);
	}
	else if(retainCurrent){
		tgt.criteriaNot.value = tgt.criteriaNotCur.value;
	}
	
	//do imagenr
	if(typeof(f.searchinputImgnr) != 'undefined'){
		val = f.searchinputImgnr.value;
		if(nwsearch==1)
			tgt.imgnr.value = val;
		else
			tgt.imgnr.value = mergeKW(tgt.imgnrCur.value, val);
	}
	else if(retainCurrent){
		tgt.imgnr.value = tgt.imgnrCur.value;
	}
	
	//do cdtitle
	if(typeof(f.searchinputCD) != 'undefined'){
		val = f.searchinputCD.value;
		if(nwsearch==1)
			tgt.cdtitle.value = val;
		else
			tgt.cdtitle.value = mergeKW(tgt.cdtitleCur.value, val);
	}
	else if(retainCurrent){
		tgt.cdtitle.value = tgt.cdtitleCur.value;
	}
	
	return true;
}


function setImagetype(f, retainCurrent){
	if(tgt.cdtitle.value!='' && tgt.criteriaAll.value==''){//search CD's, not images
		tgt.imagetype.value = 'CD';
		tgt.budgetimages.value = tgt.budgetimagesCur.value;
	}
	else{
		tp = ''
		bud = 1;
		
		if(typeof(f.imagetypeRFChk) != 'undefined'){//work with checkboxes

		
			
				if(typeof(f.imagetypeRFChk) != 'undefined' && f.imagetypeRFChk.checked){
					tp = tp + 'RF,';
				}	
			
				if(typeof(f.imagetypeEDChk) != 'undefined' && f.imagetypeEDChk.checked){
					tp = tp + 'ED,';
				}	
			
			if(typeof(f.imagetypeRFbudChk) != 'undefined' && f.imagetypeRFbudChk.checked){
				bud = 2;
			}
			else if(typeof(f.imagetypeRFbudChk) != 'undefined'){
				bud = 0;
			}
		}
		else if(typeof(f.imagetypeRad) != 'undefined'){//work with radiobuttons. take value from selected option
			//alert(2);
			thetp = getRadioValue(f.imagetypeRad);
			if(thetp.split(',').length > 1){//comma separated value for imagetype,budguet
				tp = thetp.split(',')[0];
				bud = thetp.split(',')[1];
			}	
			else{
				tp = thetp;
			}
		}
		else if(typeof(f.imagetype) != 'undefined'){//work with a hidden field, just copy value
			thetp = f.imagetype.value;
			numbers = '0123456789';
			if(thetp.split(',').length > 1 && numbers.indexOf(thetp.split(',')[1])!=-1){//comma separated value for imagetype,budguet
				tp = thetp.split(',')[0];
				bud = thetp.split(',')[1];
			}	
			else{
				tp = thetp;
			}
		}
		else if(retainCurrent==1){
			tp = tgt.imagetypeCur.value;
			bud = tgt.budgetimagesCur.value;
			
		}
		if(bud==2 && tp==''){
			tp = 'RF';
			bud = 2;
		}
		else if(bud==2 && tp.indexOf('RF')==-1){
			bud = 0;
		}
		if(tp==''){
			tp = 'RF,ED';
		}
		if(tp.substring(tp.length-1)==','){//remove comma at the end if it's there
			tp = tp.substring(0, tp.length-1);
		}

		tgt.imagetype.value = tp;
		tgt.budgetimages.value = bud;	
	}
		
	return true;
}

function setOrienations(f, retainCurrent){
	if(typeof(f.imageOrient_PChk) != 'undefined'){
		tgt.imageOrient_P.value = f.imageOrient_PChk.checked?1:0;
	}
	else{
		tgt.imageOrient_P.value = retainCurrent?tgt.imageOrient_PCur.value:1;
	}
	if(typeof(f.imageOrient_LChk) != 'undefined'){
		tgt.imageOrient_L.value = f.imageOrient_LChk.checked?1:0;
	}
	else{
		tgt.imageOrient_L.value = retainCurrent?tgt.imageOrient_LCur.value:1;
	}
	if(typeof(f.imageOrient_SChk) != 'undefined'){
		tgt.imageOrient_S.value = f.imageOrient_SChk.checked?1:0;
	}
	else{
		tgt.imageOrient_S.value = retainCurrent?tgt.imageOrient_SCur.value:1;
	}
	
	return true;
}

function setColortype(f, retainCurrent){
	if(typeof(f.imagecolor_CChk) != 'undefined'){
		tgt.imagecolor_C.value = f.imagecolor_CChk.checked?1:0;
	}
	else{
		tgt.imagecolor_C.value = retainCurrent?tgt.imagecolor_CCur.value:1;
	}
	
	if(typeof(f.imagecolor_BWChk) != 'undefined'){
		tgt.imagecolor_BW.value = f.imagecolor_BWChk.checked?1:0;
	}
	else{
		tgt.imagecolor_BW.value = retainCurrent?tgt.imagecolor_BWCur.value:1;
	}
	
	return true;
}

function setBrandfilter(f, retainCurrent){
	br = '';
	if(typeof(f.brandlistMSel) != 'undefined'){//use a multiple selectbox for the brands
		allfound = 1;
		for(x=0; x<f.brandlistMSel.options.length; x++){
			if(f.brandlistMSel.options[x].selected==1){
				br = br + f.brandlistMSel.options[x].value + ',';
			}
			else{
				allfound = 0;
			}
		}
		if(allfound==1){
			br = '';
		}
	}
	else if(typeof(f.brandlistChk) != 'undefined'){//use a checkbox list of collections
		for(x=0; x<f.brandlistMSel.length; x++){
			if(f.brandlistMSel[x].checked){
				br = br + f.brandlistMSel[x].value + ',';
			}
		}
	}
	else if(typeof(f.brandfilter) != 'undefined'){//use a hidden field to state one or more brand
		br = f.brandfilter.value;
	}
	else//no brandfilter box in the form
		br=0;
	
	if(tp[tp.length-1]==','){//remove comma at the end if it's there
		tp = tp.substring(0,tp.length-2);
	}
	
	if(br==0 && retainCurrent){
		tgt.brandFilter.value = tgt.brandFilterCur.value;
		tgt.brandsToFilter.value = tgt.brandsToFilterCur.value;
	}
	else{
		tgt.brandFilter.value = br==''?0:1;
		tgt.brandsToFilter.value = br;
	}	
	
	return true;
}

function setVehicles(f, retainCurrent){
	
	
	return true;
}

function setPositions(f, retainCurrent){
	//get startpos
	if(typeof(f.startposSel) != 'undefined'){//use dropdownbox for startpos
		tgt.startpos.value = f.startposSel.options[f.startposSel.options.selectedIndex].value;
	}
	else if(typeof(f.startpos) != 'undefined'){//use hidden field for passing the values
		tgt.startpos.value = f.startpos.value;
	}
	else
		tgt.startpos.value = 1;
	
	//get maxrows
	if(typeof(f.maxrowsSel) != 'undefined'){//use dropdownbox for startpos
		tgt.maxros.value = f.maxrowsSel.options[f.maxrowsSel.options.selectedIndex].value;
	}
	else if(typeof(f.maxrows) != 'undefined'){//use hidden field for passing the values
		tgt.maxros.value = f.maxrows.value;
	}
	else{//for maxrows, always retain current settings. 
		tgt.maxros.value = tgt.maxrosCur.value;
	}
	
	return true;
}

function setAdvancedSearch(f, retainCurrent){
	
		tgt.searchShowAdvanced.value = 0;
	
	return true;
}

//========================================================= Functions to submit a search form =================================================================
function setAutopreview(fld){
	onOff = fld.checked?0:1;
	
	swichPopupOnOff(onOff);
	hiddenControl('http:\/\/www.regionstock.com/_components/search/CRTL_AutoPreview.cfm?setAutoPreview='+onOff);
}

function setCurrentSearchPositions(start, end){
	curstartpos = start;
	currendpos = end;
}
function setTHdir(fld){
	newthdir = fld.options[fld.options.selectedIndex].value.toLowerCase();
	
	if(smsSearch2SetThdir!=null){//smsSearch2 is loaded
		smsSearch2SetThdir(newthdir);
	}
	
	hiddenControl('http:\/\/www.regionstock.com/_components/search/CTRL_thdir.cfm?setthdir='+newthdir);
}

function quickSearch(f,kw){
	//alert(f);
	//alert(kw);
	f.searchinputAll.value=kw;	
	setSearch(f, 0, 1);
}

function handleAllSup(){//triggered when clicking checkbox for all suppliers
	brandselector= document.getElementById("selRef");
	allbrands = 1;
	brandsToFilter='';

	for(x=0 ; x<brandselector.length ; x++){

		if(brandselector[x].selected){
			brandsToFilter = brandsToFilter + brandselector[x].value + ',';
		}
		else{
			allbrands = 0;
		}
	}
	newval=allbrands==1?0:1;
	for(x=0 ; x<brandselector.length ; x++){
		brandselector[x].selected=newval;
	}
	var doAllSupChecker = document.getElementById("doAllSupChecker");
	doAllSupChecker.checked = newval;	
}

function checkAllSup(){//triggered when clicking supplier for suppliers selector
	brandselector= document.getElementById("selRef");
	allbrands = 1;
	brandsToFilter='';

	for(x=0 ; x<brandselector.length ; x++){

		if(brandselector[x].selected){
			brandsToFilter = brandsToFilter + brandselector[x].value + ',';
		}
		else{
			allbrands = 0;
		}
	}
	newval=allbrands==1?0:1;
	var doAllSupChecker = document.getElementById("doAllSupChecker");
	doAllSupChecker.checked = allbrands;	
}

checkAllSup


function clearFld(fld, val){//if a field has the value 'val', the value is cleared upon focus
	if(fld.value.toLowerCase()==val.toLowerCase()){
		fld.value='';
	}
}

function getRadioValue(fld){//get the value of the selection option in a group of radio buttons
	for(x=0; x<fld.length; x++){
		if(fld[x].checked)
			return fld[x].value;
	}
	return null;
}

function dumpfrm(f){//dump all values of all formfields to screen
	for(x=0; x<f.elements.length; x++){
		if(typeof(f.elements[x].value) != 'undefined'){
			alert('form.'+f.elements[x].name+' = ' + f.elements[x].value);
		}
	}
}

//=============================== Form handling functions ==========================================
function checkformOnSubmit(frmname){
	cd = getFormCheckData(frmname);

	for(x=0;x<cd.length;x++){
		checker = getChecker(cd[x]);
		check_a = checker.check_a;
		for(c=0;c<check_a.length;c++){
			chkRes = checkFldOnSubmit(frmname, fn, ft, check_a[c]);
			if(chkRes != 'ok'){
				alert(chkRes);
				document.forms[frmname].elements[fn].focus();
				window.returnValue = false;
				return false;
			}
		}
	}
}

function getFormCheckData(frmname){
	if(typeof(document.forms[frmname].formCheckData) != 'undefined'){
		cd = document.forms[frmname].formCheckData.value;
		cd_a = cd.split(';');
		return cd_a;
	}
	else{
		return new Array();
	}
}

function checkFormFldChange(frmname, fldname, act){
	cd = getFormCheckData(frmname);
	for(x=0;x<cd.length;x++){
		checker = getChecker(cd[x]);
		if(checker.fn==fldname){
			check_a = checker.check_a;
			for(c=0;c<check_a.length;c++){
				chkRes = checkFldOnChange(frmname, fn, ft, check_a[c]);
				if(chkRes != 'ok'){
					alert(chkRes);
					document.forms[frmname].elements[fn].focus();
					window.returnValue = false;
					return false;
				}
			}
			//fld.focus();
			return true;
		}		
	}
}

function getChecker(ck){
	chk = cd[x].indexOf('_CK:')!=-1?cd[x].substring(cd[x].indexOf('_CK:')+4, cd[x].length):'';
	ft = '';
	fn = '';
	if(chk != ''){
		ft = cd[x].indexOf('_FT:')!=-1?cd[x].substring(cd[x].indexOf('_FT:')+4,cd[x].indexOf('_CK:')):'';
	}
	if(( chk != '') && (ft != '')){
		fn = cd[x].indexOf('_FN:')!=-1?cd[x].substring(cd[x].indexOf('_FN:')+4,cd[x].indexOf('_FT:')):'';
	}
	ret = new Object();
	ret.check_a = chk.toLowerCase().split(',');
	ret.ft = ft.toLowerCase();
	ret.fn = fn;
	return ret;
}

function checkFldOnSubmit(frmname, fldname, fldtype, check){
	fld = document.forms[frmname].elements[fldname];
	switch(check) {		
		case 'mandatory':
			if((typeof(fld.tagName)!='undefined')&&(fld.tagName=='INPUT')){				
				if(fldtype=='checkbox'){
					if(!fld.checked){
						msg = 'U heeft niet alle verplichte velden ingevuld.\nVelden met een * zijn verplicht.';
						return msg;
					}
				}
				else if(trim(fld.value)==''){
					msg = 'U heeft niet alle verplichte velden ingevuld.\nVelden met een * zijn verplicht.';
					return msg;
				}				
			}
			break;
		case 'copytext':
			ae_editsourceinline(1, false);//first put back on normal view in case of HTML view.
			theTxt = DHTMLSafe.DOM.body.innerText;
			document.forms[frmname].elements['HTMLtext_retainer_'+fldname].value = theTxt;
			break;
		case 'email':
			val = trim(fld.value);
			if( (val!='') && ( (val.length<8) || (val.indexOf('@')<2) || (val.indexOf('@')>val.lastIndexOf('.')) || (val.lastIndexOf('.')>val.length-3))){
				msg = "Het ingevoerde e-mailadres is ongeldig.";
				return msg;
			}
			break;
		case 'bankaccount':
			val = trim(fld.value);
			if(val.substring(0,1).toLowerCase() != 'p'){
				gotall=0;
				pos=0;
				cntpos=0;
				total=0;
				do{
					thenum = val.substring(pos, pos+1);
					if(thenum!='.'){
						thenum = parseInt(thenum);
						multiplier = 9-cntpos;
						theval = thenum * multiplier;
						total = total + theval;
						cntpos++;
					}
					pos++;
				}
				while( (cntpos<9) && (pos < val.length))
				
				if( (total%11!=0) || (cntpos!=9) ){
					msg = 'Het ingevoerde rekeningnummer is ongeldig';
					return msg;
				}				
			}
			break;
		case 'url':
			val = trim(fld.value);
			if( (val.toLowerCase()=='http://') || (val.toLowerCase()=='https://') ){
				fld.value='';
			}
			else if( (val.toLowerCase().indexOf('http:\/\/')!=0) && (val.toLowerCase().indexOf('https:\/\/')!=0) ){
				fld.value = 'http:\/\/'+val;
			}
			break;	
		
		case 'time':
			val = trim(fld.value);
			if( (val.length>3) && (val.indexOf(':')==-1) ){
				fld.value = val.substring(0,2) + ':' + val.substring(2,4);
			}
			else if(val.length>5){
				fld.value = val.substring(0,5);
			}
	
			if(val.length==5){
				if(parseInt(val.substring(0,2)) > 24){
					msg = "U heeft een ongeldige tijd ingevoerd";
					return msg;
				}
				if(parseInt(val.substring(3,5)) > 59){
					msg = "U heeft een ongeldige tijd ingevoerd";
					return msg;
				}
			}
			break;
			
		case 'number':
			if(setNumeric(fld.value)!=fld.value){
				return 'You entered an invalid number.';
			}
			break;
		case 'vat':
			var radioObj = document.forms[frmname].elements['QVAT'];
			var vatcheck = ''
				if(!radioObj)
					vatcheck = "";
				var radioLength = radioObj.length;
				if(radioLength == undefined)
					if(radioObj.checked)
						vatcheck = radioObj.value;
					else
						vatcheck = "";
				for(var i = 0; i < radioLength; i++) {
					if(radioObj[i].checked) {
						vatcheck = radioObj[i].value;
					}
				}
				if(vatcheck == 'yes'){
					if(fld.value == ''){
					msg = "Please enter your VAT munber.";
					return msg;
					}
				}
			break;
	}	
	if(fldtype=='date'){
		//check valid dates
		val = trim(fld.value);
		if(val!=''){
			dv = val.substring(0,2);
			mv = val.substring(3,5);
			yv = val.substring(6,10);
			
			days_a=(new String("31,28,31,30,31,30,31,31,30,31,30,31")).split(',');
			
			if(mv<1 || mv>12){
				msg = 'U heeft een ongeldige datum ingevoerd.';
				return msg;
			}
			maxday=((yv%4==0)&(mv==2))?29:days_a[mv-1];
			dv = parseInt(dv);
			maxday = parseInt(maxday);					
			if(dv<1 || dv>maxday){
				msg = 'U heeft een ongeldige datum ingevoerd.';
				return msg;
			}
		}
	}
	return 'ok';
}
	
function checkFldOnChange(frmname, fldname, fldtype, check){
	fld = document.forms[frmname].elements[fldname];	
	switch(fldtype.toLowerCase()) {		
		case 'date':
			if(fld.value!=''){		
				val = fld.value;
				newval = '';
				q=0;
				do{
					chr = val.substring(q,q+1);
					RE = "0,1,2,3,4,5,6,7,8,9,-";
					res = RE.indexOf(chr);
					if(res!=-1){
						if((q==1)&&(chr=='-')){
							val = '0' + val;
							newval = '0' + newval;
							q++;
						}
						if((q==2)&&(chr!='-')){
							val = '-' + val;
							newval = newval + '-';
							q++;
						}
						if((q==4)&&(chr=='-')){
							val = '0' + val;
							newval = newval.substring(0,3) + '0' + newval.substring(3,4);
							q++;
						}
						if((q==5)&&(chr!='-')){
							val = '-' + val;
							newval = newval + '-';
							q++;
						}						
						if( ((chr=='-') && ((q==2) || (q==5))) || (chr!='-')){
							newval += chr;
						}
					}
					q++;					
				}
				while(q<val.length&&q<10)

				fld.value = newval;
			}
			break;
		case '':
			break;
	}
	if(check.indexOf('maxlength') ==0){
		if(check.indexOf('=') != -1){
			ml = check.substring(check.indexOf('=')+1, check.length);
			if(fld.value.length > parseInt(ml)){
				fld.value = fld.value.substring(0, ml);
			}
		}
	}
	else if(check=='reloadonchange'){
		formReload(frmname);
	}
	else if(check=='alias'){
		newval = '';
		for(q=0;q<fld.value.length;q++){
			chr = fld.value.substring(q,q+1);
			RE = /[^\w|\s|\0,9]/i;	// at least one non word character
			res = chr.match(RE);
			//alert('res op match('+chr+') is '+res);
			if((res==null)&&(chr!=' ')){
				newval += chr;
			}
		}
		fld.value = newval;
	}
	else if(check=='number'){
		if(setNumeric(fld.value)!=fld.value){
			document.forms[frmname].elements[fldname].value = setNumeric(fld.value);
		}
	}
	else if(check=='time'){
		val = fld.value;
		if( (val.length>3) && (val.indexOf(':')==-1) ){
			fld.value = val.substring(0,2) + ':' + val.substring(2,4);
		}
		else if(val.length>5){
			fld.value = val.substring(0,5);
		}

		if(val.length==5){
			if(parseInt(val.substring(0,2)) > 24){
				msg = "U heeft een ongeldige tijd ingevoerd";
				return msg;
			}
			if(parseInt(val.substring(3,5)) > 59){
				msg = "U heeft een ongeldige tijd ingevoerd";
				return msg;
			}
		}
	}	
	return 'ok';
}

function checkDate(frmname, fldname){
	checkFldOnChange(frmname, fldname, 'date', 'date');
	
	fld = document.forms[frmname].elements[fldname];
	val = fld.value;
	if((val.length!=10)&&(val.length!=0)){
		if(val.split('-').length==3){
			//autocorrect yyyy value
			if(val.length>6){
				yyyy = parseInt(val.substring(6,val.length));
				if(yyyy<40){
					yyyy = 2000 + yyyy;
				}
				else if((yyyy>39) && (yyyy<100)){
					yyyy = 1900 + yyyy
				}
				else if(yyyy>99){
					yyyy = 1800 + yyyy;
				}
				else{
					alert("U heeft een ongeldige datum ingevoerd.");
					fld.focus();
					window.returnValue = false;
					return false;					
				}				
			}
			else{
				yyyy = new Date().getYear();
			}
			fld.value = fld.value.substring(0,6) + yyyy;
		}
		else{
			alert("U heeft een ongeldige datum ingevoerd.");
			fld.focus();
			window.returnValue = false;
			return false;					
		}
	}
	if(val.length!=0){
		if(val.split('-').length==3){
			//verify correct input
			val = fld.value;
			dv = val.substring(0,2);
			mv = val.substring(3,5);
			yv = val.substring(6,val.length);
			
			days_a=(new String("31,28,31,30,31,30,31,31,30,31,30,31")).split(',');
			
			if(mv<1 || mv>12)
			{
				alert("U heeft een ongeldige datum ingevoerd.");
				fld.focus();
				window.returnValue = false;
				return false;	
			}
			maxday=((yv%4==0)&(mv==2))?29:days_a[mv-1];
			dv = parseInt(dv);
			maxday = parseInt(maxday);					
			if(dv<1 || dv>maxday)
			{
				alert("U heeft een ongeldige datum ingevoerd.");
				fld.focus();
				window.returnValue = false;
				return false;	
			}
		}
		else{
			alert("U heeft een ongeldige datum ingevoerd.");
			fld.focus();
			window.returnValue = false;
			return false;					
		}
	}
}

//=========================================== user interface functions ===============================================
function switchLanguage(lang){
	hiddenControler('_components/header.languageSeelctor/CTRL_language.cfm?setlanguage='+lang);
}

function hiddenControl(theurl){//can load scripts in an invisible hidden iframe, used to store settings.
	controller = document.getElementById("hiddencontroller");
	controller.src = theurl;
}

function setDivContent(divid, cont){//put specific HTML into a certain DIV
	thediv = document.getElementById(divid);
	if(thediv!=null)
		thediv.innerHTML = cont;
}

function galleryToSearch(gallid){//javascript function to submit a gallery to a search
	galleryForm = document.getElementById("gallform");
	galleryForm.searchinputAll.value = 'gall:' + gallid;
	setSearch(galleryForm, 0, 1);
}

//=========================================== basket functions =======================================================
function recalcPrice(){
	document.basketform.submit();
}

function calcprice(idx, imgid, extintcode){//open RM calculator
	opts = 'width=450, height=600';
	theurl = '_snippets/SMS/priceCalculator/index.cfm?bidx='+idx+'&imgid='+imgid+'&extintcode='+extintcode;
	calcwin = window.open(theurl, 'calcwin', opts);
	calcwin.focus();
}

//=========================================== download functions =====================================================

function creditDownload(imgid, tp, prodtp, extintcode, extrainfo){
	location = 'http:\/\/www.regionstock.com/_components/_GenericScripts/dlImg_subs.cfm?imgid='+imgid+'&extintcode='+extintcode+'&'+extrainfo;
}

function subsDownload(imgid, tp, prodtp, extintcode, extrainfo){
	location = 'http:\/\/www.regionstock.com/_components/_GenericScripts/dlImg_subs.cfm?imgid='+imgid+'&extintcode='+extintcode+'&'+extrainfo;
}

//=========================================== miscelaneous functions =================================================
function setNumeric(str){
	var numbers = '0123456789';
	newval = '';
	for(sIdx=0; sIdx<str.length; sIdx++){
		thischar = str.substring(sIdx,eval(sIdx+1));
		if(numbers.indexOf(thischar)!=-1){
			newval += thischar;
		}
	}
	return newval;
}

function mergeKW(kw1, kw2){//for searching in results, merge the old and new keywords without having doubles
	res = kw1.toLowerCase();
	kw2_a = kw2.toLowerCase().split(' ');
	
	for(q=0; q<kw2_a.length; q++){
		if(res.indexOf(kw2_a[q])==-1){
			res = res + ' ' + kw2_a[q];
		}
	}
	return res;
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,'');
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function clearSelect(selObj){
	for(x=selObj.options.length; x>=0; x--){
		selObj.options[x] = null;
	}
}

function checkLoginForm(f){
	if(f.username.value=='' || f.password.value=='')
		return false;
	return true;
}

function forgotPassword(){
	location='index.cfm?/Login_NL&loginshowact=forgotpwd';
}

function register(){
	location='index.cfm?/register_NL'; 
}

var footerLightbox = 0;
function putImg(imgid, puttype, arttype, extIntCode, xtra){//put image into lightbox or shoppingbasket
	extIntCode = (extIntCode==null)?0:extIntCode;
	if(puttype=='1'){//lightbox
		
			alert('om je lightbox te gebruiken dien je ingelogd te zijn');
		
	}
	else{//add to basket
		
			hiddenControl('http:\/\/www.regionstock.com/_components/basket/CTRL_add2basket.cfm?lang=NL&id='+imgid+'&extIntCode='+extIntCode+'&baskettype='+arttype+'&'+xtra);
		
	}
}

function basketDelete(uuid){
	location = 'http:\/\/www.regionstock.com/index.cfm?/basket_NL&basketact=delete&basketUUID='+uuid;
}

function gotoBasket(){
	location = 'http:\/\/www.regionstock.com/index.cfm?/basket_NL';
}

function checkAgree(){
	chkbox = document.getElementById("baskettotals_F_agree");
	
	if(!chkbox.checked){
		alert("Je moet akkoord gaan om de bestelling af te maken.");
		window.returnValue = false;
		return false;
	}
	else{
		return true;
	}
}
function showConditions(){
	theurl = 'http:\/\/www.regionstock.com/index.cfm?/conditions_NL';
	opts = 'width=980, height=550, scrollbars=yes, resizable';
	conditionswin = window.open(theurl, 'conwin', opts);
	conditionswin.focus();
}
function showLicense(){
	theurl = 'http:\/\/www.regionstock.com/index.cfm?/Licenses_NL';
	opts = 'width=980, height=550, scrollbars=yes, resizable';
	licwin = window.open(theurl, 'conwin', opts);
	licwin.focus();
}

function printInvoice(id){
	opts = 'width=700, height=550, scrollbars=yes, resizable';
	theurl = 'http:\/\/www.regionstock.com/_components/myOrders.invoice/invoice.cfm?factuurid='+id;
	factuurwin = window.open(theurl, 'factuurwin', opts);
	factuurwin.focus();
}

function dokwsearch(f){
	criteriaAll = '';
	for(x=0;x<f.keywords.length;x++){
		if(f.keywords[x].checked){
			criteriaAll = criteriaAll+" "+f.keywords[x].value;
		}
	}
	if(criteriaAll!=''){
		f.searchinputAll.value=criteriaAll;	
		setSearch(f, 0, 1);
	}
	else
		return false;
}

function imgdetailshandler(act, id, extintcode){
	if(act=='downloadcomp'){
		
			alert('Downloading larger versions is only available to registered users.\n\nPlease login or register for free.');
		
	}
	else if(act=='email'){
		opts = 'width=400, height=250';
		theurl = 'http:\/\/www.regionstock.com/_components/imagedetails.action_links/emailLightbox.cfm?imgid='+id+'&extintcode='+extintcode;
		emlwin = window.open(theurl, 'emlwin', opts);
		emlwin.focus();
	}
	else if(act=='pdfimage'){
		location = 'http:\/\/www.regionstock.com/_components/imagedetails.action_links/pdfImage.cfm?imgid='+id+'&extintcode='+extintcode+'&action=pdfimage';
	}
}

function pagenavDoSubmit(){
	theForm = document.getElementById("frm_searchresultspaginator");
	setSearch(theForm, 1, 1);
}
function doPageLink(pos){
	theForm = document.getElementById("frm_searchresultspaginator");
	theForm.startpos.value = pos;
	setSearch(theForm, 1, 1);
}
function pagenavDoGo(formID){
	srcForm = document.getElementById(formID);
	theForm = document.getElementById("frm_searchresultspaginator");
	
	maxRows = '50';
	
	pagNum = srcForm.pagenum.value;
	if(pagNum!='' && pagNum > 0){
		thePos = (parseInt(pagNum)-1) * parseInt(maxRows) + 1;
		doPageLink(thePos);
	}
}
function pagenavCheckSubmit(srcForm){
	pagNum = srcForm.pagenum.value;
	maxRows = '50';
	if(pagNum!='' && pagNum > 0){//pagenumber field is filled
		thePos = (parseInt(pagNum)-1) * parseInt(maxRows) + 1;
		doPageLink(thePos);
	}
	else{
		pagenavDoSubmit();
	}
}

function checkLoginForm(f){
	if(f.username.value=='' || f.password.value=='')
		return false;
	return true;
}

swichPopupOnOff(1);


function calcprice(basketuuid, imgid, extintcode){
	opts = 'width=700, height=800, status=1, resizable, scrollbars';
	theurl = 'http:\/\/www.regionstock.com/_snippets/SMS/priceCalculator/index.cfm?olid='+basketuuid+'&imgid='+imgid+'&extintcode='+extintcode;
	calcwin = window.open(theurl, 'calcwin', opts);
	calcwin.focus();
}

function checkEmptyPrices(){
	hasNonCalcedRM = document.basketform.hasNonCalcedRM.value;
	if(hasNonCalcedRM==1){
		alert("Your basket contains RM images for which no price is calculated yet.\nPlease make sure to calculate a price for all your RM images before proceeding to check-out.");
		return false;
	}
	return true;
}

function noContext(){
	return false;
}

