<!--

// BEELDBANK functions

// switch preview on selection input type="file": onchange="switchimage(this.id,'Preview');"

function switchimage(id, previewID) {
/*
	inputfile = new Object();
	inputfile = document.getElementById(id);

	var imagepath = inputfile.value;

	if (imagepath != "") {

		imagetype = new Array(".gif",".jpg",".jpeg",".png");

		for (i=0;i<4;i++) {
			if (imagepath.toLowerCase().indexOf(imagetype[i]) > -1) {
				document.images[previewID].src = 'file:///'+imagepath;
			}
		}

	}
*/
}

// reset preview to empty.gif selection input type="file": onchange="switchimage(this.id,'Preview','Foto');"

function resetimage(url, previewID, fotoID) {
	document.images[previewID].src = url;
	document.getElementById(fotoID).value = 'empty';
}

// adjust preview to max. 450px: img src="previewID" -> onload="adjustimagesize('Preview','Breedte', 450);"

function adjustimagesize(previewID, breedteID, maxwidth) {

	if (document.images[previewID].width > maxwidth) {
		document.images[previewID].width = maxwidth;
	}

	document.getElementById(breedteID).value = document.images[previewID].width;

}

// switch preview width: onclick="switchwidth('Preview','Breedte', 450);"

function switchwidth(previewID, breedteID, maxwidth) {
	var px = document.getElementById(breedteID).value;

	if (px > maxwidth) {
		px = maxwidth;
	}
	document.images[previewID].width = px;
	document.getElementById(breedteID).value = px;
}

// return HTML source for image, with/without align=left: onclick="returnSelected('http://www.images.com/img.gif');"

function returnSelected(imageurl, fieldID) {
	var text;

	if (document.radioform.imgalign[1].checked == true) {
		text = '<img src=\"'+imageurl+'\" align=\"left\" style=\"margin-right:10px\" />';
	} else if (document.radioform.imgalign[2].checked == true) {
		text = '<div align=center><img src=\"'+imageurl+'\" /></div><br />';
	} else if (document.radioform.imgalign[3].checked == true) {
		text = '<img src=\"'+imageurl+'\" align=\"right\" style=\"margin-left:10px\" />';
	} else {
		text = '<img src=\"'+imageurl+'\" /><br />';
	}

	window.opener.editor_insertHTML(fieldID,text);
	window.close();
}


// FORM functions
// -----------------------------------

// Make option in <select> disabled for IE, through class="disabled": onchange="checkDisabled(this);"

function checkDisabled(sel) {
	var opt = sel.options[sel.selectedIndex];
	if (opt.className == "disabled") {
	     // sel.selectedIndex = -1;
		 sel.selectedIndex = 2; // eerste optie
	}
}

// Force maximal chars

function TrackCount(fieldObj,maxChars) {
	var diff = maxChars - fieldObj.value.length;
	if (diff < 0) {
		fieldObj.value = fieldObj.value.substring(0,maxChars);
		diff = maxChars - fieldObj.value.length;
	}
}

// Check for excess of maximal chars

function LimitText(fieldObj,maxChars) {
	var result = true;
	if (fieldObj.value.length >= maxChars)
		result = false;
	if (window.event)
		window.event.returnValue = result;
	return result;
}

// Force number input: onkeypress="IsCijfer(false);"

function IsCijfer(Decimaal) {
	var keypressed = window.event.keyCode;
	var ElementText  = window.event.srcElement.value ;
	if (keypressed == 45){
		if (ElementText.length!=0) {
		  window.event.keyCode = 0;
		}

	} else {
		if (Decimaal) {
			var Reg = /\./g;
			if (Reg.test(ElementText)) {
				if( (keypressed >= 48 && keypressed <= 57) == false) {
				  window.event.keyCode = 0;
				}
			} else {
				if ((keypressed >= 48 && keypressed <= 57 || keypressed == 46) == false) {
				  window.event.keyCode = 0;
				}
			}
		} else {
			if ((keypressed >= 48 && keypressed <= 57) == false) {
			window.event.keyCode = 0;
			}
		}
	}
}

// Form validation: onsubmit="MM_validateForm('Email','','RisEmail');return document.MM_returnValue;"

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' moet een e-mailadres bevatten.\n';
      } else if (test!='R') {
        if (isNaN(val)) errors+='- '+nm+' moet een nummer bevatten.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (val<min || max<val) errors+='- '+nm+' moet een nummer bevatten tussen '+min+' en '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is verplicht.\n'; }
  } if (errors) alert('De volgende fout(en) doen zich voor:\n'+errors);
  document.MM_returnValue = (errors == '');
}


// RANDOM functions
// -----------------------------------

// Change border color: onmouseover="borderit(this,red);"

function borderit(which,color){
	//if IE 4+ or NS 6+
	if (document.all||document.getElementById){
		which.style.borderColor=color;
	}
}

// Collpase/expand table: onclick="collapse_expand('overzicht');"

function collapse_expand(tableid) {
	document.getElementById(tableid).style.display = ("block" == document.getElementById(tableid).style.display ? "none" : "block");
}

// Hide/display field when value == valueShow: onchange="toggleFieldDisplay(this.selectedIndex, 2, 'NieuwePlaatsNaam');"

function toggleFieldDisplay (value, valueShow, fieldID) {

	field = document.getElementById(fieldID);

	if (value == valueShow) {
		field.style.display = '';
		field.focus();
	} else {
		field.style.display = 'none';
	}

}

// Jump to date view: onclick="dateJump('http://www.domain.com/admin/stats_pages/2005/06);"

function dateJump(url) {
	var monthyear = document.dateselector.monthyear.options[document.dateselector.monthyear.selectedIndex].value;
	
	location.href = url + '/' + monthyear;
	
	return false;
}

function toggleMenu(menuID) {

	var menu = document.getElementById('menu-paginas-sub-'+menuID);

	if (menu.style.display == 'none') {
		menu.style.display = '';
	} else {
		menu.style.display = 'none';
	}

}

/*
document.observe('dom:loaded', function() {
	if( (link = $('addbouwnummer'))) {
		link.observe('click', function(event) {
			event.stop();
			tr = this.up('tr').previous('tr');
	   		tr.insert({after: '<tr>'+tr.innerHTML+'</tr>'});
		});
	}

	if( (select = $('woning-select'))) {
		select.observe('change', function(event) {
			var val = $F(this);
			if (val != 0) {
				window.location.href = BaseURL + '/admin/objecten_edit/' + val;
			} else {
				this.selectedIndex = 0;
			}
		});
	}

});
*/
//-->


function fillNewsPaperParts(theArea, divideOver){

    var text = theArea.value;
	var temp;
	var spaceIndex;
	var word;
	var character;

	
    $$("."+divideOver).each(function(div){
        div.innerHTML = "";
		var scrollHieght = div.scrollHeight;

		//setTimeout('void(null)',1000);
        while (text.length > 0 && scrollHieght <= div.offsetHeight){
            temp = div.innerHTML;


            spaceIndex = text.indexOf(" ");
			spaceIndex++;
            if (spaceIndex < 1)
                spaceIndex = 1;
            word       = text.substr(0,spaceIndex);

            while (word.length > 0)
            {
                character = word.substr(0,1);

                div.innerHTML += (character == "\n") ? "<br>" : character;
                word = word.substr(1, word.length);
            }

            scrollHieght = div.scrollHeight;
            if (scrollHieght > div.offsetHeight)
            {
                div.innerHTML = temp;
            }
            else
            {
                text = text.substr(spaceIndex, text.length);
            }
        }
    });
}

function countLines(theArea, divideOver){
    var theLines  = new Array();
    var tempLines = new Array();
    var found     = new Array();
    var matche   = (new RegExp(".{"+theArea.cols+"}","g"));
    var tempText = "";
    var text = "";
    var i = 0;
    var lines = 0;

    //first get all lines
    $$("."+divideOver).each(function(theArea){
        matche   = (new RegExp(".{"+theArea.cols+"}","gm"));
        tempText = theArea.value;
        found = matche.exec(tempText, "gm");
        found = tempText.match(matche);
        if (found != null) {
            for (var i = 0; i < found.length; i++)
            {
                tempText = tempText.replace(found[i], found[i]+"\n")
            }
        }
        tempLines = tempText.split("\n");
        //if(tempLines[tempLines.length-1]=="") tempLines.length--;
        for (var j =0; j < tempLines.length ; j++)
            theLines.push(tempLines[j]);
    });
    //theLines.concat(" ");
    //then divide
    $$("."+divideOver).each(function(theArea){
        text  = "";
        lines = theArea.rows;
         if (theLines.length > 0){
            for (i = 0; i < lines; i++)
            {

                if (theLines.length > 0){
                    tempText = theLines.shift();
                    if (tempText.length != theArea.cols && tempText[tempText.length -1] != "\n")
                        tempText += "\n";
                    text += tempText;
                }
            }
        }
       theArea.value = text;
    });
}
/*
function countLines(theArea, divideOver){
    var matche   = (new RegExp(".{"+theArea.cols+"}","g"));
    matche = matche.exec(theArea.value, "g");

    for (var i = 0; i < matche.length; i++)
    {
        theArea.value =theArea.value.replace(matche[i],matche[i]+"\n")
    }
    var theLines = theArea.value.split("\n");
    if(theLines[theLines.length-1]=="") theLines.length--;


  alert(theLines.join());
  //theArea.form.lineCount.value = theLines.length;
}*/


// Get width for Lightwindow
/*
function LigthWindowPosition() {
	var FromCenter = 90;
	var Center = parseFloat(document.body.clientWidth/2);
	return(Center+FromCenter);
}
*/


//
var arrFields = new Array();
function checkFields(){
	var i=0;
	var mssg = '';
	while(arrFields.length > i){
		if (document.getElementById(arrFields[i]).value.length == 0){
			mssg += arrFields[i] + ' is verplicht\n';
		}
		i++;
	}
	if (mssg.length > 0){
		alert(mssg);
		return false;
	}else{
		document.getElementById('artikel-form').submit();
		return true;
	}
}

function addToCheckArray(str){
	arrFields[arrFields.length] = str;
	
}

