// Patterns
var formValidationMasks = new Array();
formValidationMasks['email'] = /\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/gi;	// Email
formValidationMasks['numeric'] = /^[0-9]+$/gi;	// Numeric
formValidationMasks['alpha'] = /^[a-z\.]+$/gi;	// Numeric
formValidationMasks['alphanum'] = /^[0-9a-z]+$/gi;	// Numeric
formValidationMasks['zip'] = /^[0-9]{5}\-[0-9]{4}$/gi;	// Numeric
formValidationMasks['postcode'] = /^[0-9]{4}[ ]{0,3}[a-z]{2}$/gi;	// Numeric

var formElementArray = new Array();

function validateInput(e,inputObj)
{
	if(!inputObj)inputObj = this;		
	var inputValidates = true;
	if (inputObj.type=='checkbox') {
		if(formElementArray[inputObj.name]['required'] && inputObj.tagName=='INPUT' && inputObj.checked==false)inputValidates = false;
	}
	if (inputObj.type=='textarea') {
		if(formElementArray[inputObj.name]['required'] && inputObj.tagName=='TEXTAREA' && inputObj.value.length==0)inputValidates = false;
	}
	if(formElementArray[inputObj.name]['required'] && inputObj.tagName=='TEXTAREA' && inputObj.value.length==0)inputValidates = false;
	if(formElementArray[inputObj.name]['required'] && inputObj.tagName=='INPUT' && inputObj.value.length==0)inputValidates = false;
	if(formElementArray[inputObj.name]['required'] && inputObj.tagName=='SELECT' && inputObj.selectedIndex==0){
		inputValidates = false;
	}
	if(formElementArray[inputObj.name]['mask'] && !inputObj.value.match(formValidationMasks[formElementArray[inputObj.name]['mask']]))inputValidates = false;

	if(formElementArray[inputObj.name]['freemask']){
		var tmpMask = formElementArray[inputObj.name]['freemask'];
		tmpMask = tmpMask.replace(/-/g,'\\-');
		tmpMask = tmpMask.replace(/S/g,'[A-Z]');
		tmpMask = tmpMask.replace(/N/g,'[0-9]');
		tmpMask = eval("/^" + tmpMask + "$/gi");
		if(!inputObj.value.match(tmpMask))inputValidates = false
	}	
	
	if(formElementArray[inputObj.name]['regexpPattern']){
		var tmpMask = eval(formElementArray[inputObj.name]['regexpPattern']);
		if(!inputObj.value.match(tmpMask))inputValidates = false
	}
	if(!formElementArray[inputObj.name]['required'] && inputObj.value.length==0 && inputObj.tagName=='INPUT')inputValidates = true;
	
	
	if(inputValidates){
		inputObj.parentNode.className='validInput';
	}else{
		inputObj.parentNode.className='invalidInput'
	}
}

function isFormValid()
{
	var divs = document.getElementsByTagName('DIV');
	for(var no=0;no<divs.length;no++){
		if(divs[no].className=='invalidInput')return false;
	}
	return true;	
}




function initFormValidation()
{
	var inputFields = document.getElementsByTagName('INPUT');
	var selectBoxes = document.getElementsByTagName('SELECT');
	var textareas	= document.getElementsByTagName('TEXTAREA');
	var inputs = new Array();
	
	for(var no=0;no<inputFields.length;no++){
		inputs[inputs.length] = inputFields[no];
	}	
	for(var no=0;no<selectBoxes.length;no++){
		inputs[inputs.length] = selectBoxes[no];
	}
	for(var no=0;no<textareas.length;no++){
		inputs[inputs.length] = textareas[no];
	}
	
	for(var no=0;no<inputs.length;no++){
		var required = inputs[no].getAttribute('required');
		if(!required)required = inputs[no].required;		
		
		var mask = inputs[no].getAttribute('mask');
		if(!mask)mask = inputs[no].mask;
		
		var freemask = inputs[no].getAttribute('freemask');
		if(!freemask)freemask = inputs[no].freemask;
		
		var regexpPattern = inputs[no].getAttribute('regexpPattern');
		if(!regexpPattern)regexpPattern = inputs[no].regexpPattern;
		
		var div = document.createElement('DIV');
		div.className = 'invalidInput';
		inputs[no].parentNode.insertBefore(div,inputs[no]);
		div.appendChild(inputs[no]);
		div.style.width = inputs[no].offsetWidth + 'px';
		
		inputs[no].onblur 	= validateInput;
		if (inputs[no].onchange == null) {
			inputs[no].onchange = validateInput;
		}
		inputs[no].onpaste 	= validateInput;
		inputs[no].onkeyup 	= validateInput;
		
		
		formElementArray[inputs[no].name] = new Array();
		formElementArray[inputs[no].name]['mask'] = mask;
		formElementArray[inputs[no].name]['freemask'] = freemask;
		formElementArray[inputs[no].name]['required'] = required;
		formElementArray[inputs[no].name]['regexpPattern'] = regexpPattern;

		validateInput(false,inputs[no]);
			
	}	
}

//

function doSort ( p ) {
	frm = document.getElementById ('frmSort');
	frm.sorder.value=p;
	frm.submit();
}

function changeto(highlightcolor){ 
	source=event.srcElement 
	if (source.tagName=="TR"||source.tagName=="TABLE") 
		return 
	while(source.tagName!="TD") 
		source=source.parentElement 
	if (source.style.backgroundColor!=highlightcolor&&source.id!="ignore") 
		source.style.backgroundColor=highlightcolor 
} 
	
function changeback(originalcolor){ 
	if (event.fromElement.contains(event.toElement)||source.contains(event.toElement)||source.id=="ignore") 
		return 
	if (event.toElement!=source) 
		source.style.backgroundColor=originalcolor 
} 

/*
*/
var form_1_errors = 0;

function convert_error(err_code, r) {
	switch (err_code) {
		case 0:
			r.className='input_OK';
			warnIcon = '<img src="/templates/images/icons/ok.gif" border="0" alt="De invoer voor dit veld is juist."/>&nbsp;Correct';
			return warnIcon ;
			break;
		case 1:
			r.className='input_WARNING';
			warnIcon = '<img src="/templates/images/icons/warning.gif" border="0" alt="Deze titel is al in gebruik"/>&nbsp;Deze titel is al in gebruik';
			return warnIcon ;
			break;
		case 2:
			r.className='input_NOT_OK';
			warnIcon = '<img src="/templates/images/icons/warning.gif" border="0" alt="Dit emailadres is reeds bekend bij ons. Vul a.u.b. uw wachtwoord in"/>&nbsp;Vul a.u.b. uw wachtwoord in';
			return warnIcon ;
			break;
		case 3:
			r.className='input_NOT_OK';
			warnIcon = '<img src="/templates/images/icons/error.gif" border="0" alt="Dit emailadres is niet correct"/>&nbsp;Dit emailadres is niet correct';
			return warnIcon ;
			break;
		case 4:
			r.className='input_NOT_OK';
			warnIcon = '<img src="/templates/images/icons/error.gif" border="0" alt="Het wachtwoord is niet juist"/>&nbsp;Het wachtwoord is niet juist';
			return warnIcon ;
			break;
	}
}

function show_message (thedif, theresult) {
	ref = document.getElementById (thedif);
	ref.innerHTML = convert_error(theresult, ref);
	ref.style.visibility='visible';
	ref.style.display='block';
	return theresult;
}

function set_msg_titel(result) {
	ret = show_message ('msg_titel', result);
}
function set_map_info(result) {
//	alert(result);
	eval(result);
}
function set_msg_email(result) {
	ret = show_message ('msg_email', result)
	
	switch (ret) {
		case 2:	// Need password
			ref = document.getElementById ('inp_extra_fld');
			if ( !document.getElementById ('inpPassword') ) {
				ref.innerHTML='<input type=password id=inpPassword name=inpPassword onChange="do_check_credentials(); return true;">';
				ref = document.getElementById ('inp_extra_tekst');
				ref.innerHTML='Wachtwoord';
			}
			ref = document.getElementById ('inpPassword');
			ref.focus();
			break;
		case 3:	// Fout
			ref = document.getElementById ('inp_extra_tekst');
			ref.innerHTML='<b>Hint</b>';
			ref = document.getElementById ('inp_extra_fld');
			ref.innerHTML='<i>Een emailadres ziet er bv. als volgt uit: <nb>naam@domein.nl</b><i/>';
			break;
		case 0:	// Ok
			ref = document.getElementById ('inp_extra_fld');
			ref.innerHTML='';
			ref = document.getElementById ('inp_extra_tekst');
			ref.innerHTML='';
			break;
	}
}

function set_cat_list(result) {
	eval (result);
}

function set_credentials_info (result) {
	eval (result);
}

function set_foto_list (result) {
	alert (result);
}


/*
	'onChange' Check functions
*/
function do_check_email() {
	var x;
	x = document.getElementById("inpEmail");
	x_check_email(x.value, set_msg_email);
}
function do_check_titel() {
	var x;
	x = document.getElementById("inpTitel");
	x_check_titel(x.value, set_msg_titel);
}
function do_check_pcd() {
	var p, h;
	p = document.getElementById("inpPostcode");
	h = document.getElementById("inpHuisnummer");
	x_check_pcd(p.value, h.value, set_map_info);
}
function do_check_credentials() {
	var u, p;
	u = document.getElementById("inpEmail");
	p = document.getElementById("inpPassword");
	if (!p) return;
	x_check_credentials(u.value, p.value, set_credentials_info);
}
function do_load_cat () {
	var x;
	x = document.getElementById("selRubriek");
	x_load_cat(x.value, set_cat_list);
}

function do_save_image() {
	var x;
	x = document.getElementById("aSubmit");
	x.value="1";
	x = document.getElementById("frmPlaatsen");
//	do_check_email();
//	do_check_titel();
//	do_check_pcd();
//	do_check_credentials();
	x.submit();
}

function do_set_prijs (p) {
	x = document.getElementById("inpPrijs");
	if (p.value == 1) {
		x.style.backgroundColor='#ffffff';
		x.disabled=false;
	} else {
		x.style.backgroundColor='#dddddd';
		x.disabled=true;
	}
}

function MultiSelector( list_target, max ){

	// Where to write the list
	this.list_target = list_target;
	// How many elements?
	this.count = 0;
	// How many elements?
	this.id = 0;
	// Is there a maximum?
	if( max ){
		this.max = max;
	} else {
		this.max = -1;
	};
	
	/**
	 * Add a new file input element
	 */
	this.addElement = function( element ){

		// Make sure it's a file input element
		if( element.tagName == 'INPUT' && element.type == 'file' ){

			// Element name -- what number am I?
			element.name = 'file_' + this.id++;

			// Add reference to this object
			element.multi_selector = this;

			// What to do when a file is selected
			element.onchange = function(){

				// New file input
				var new_element = document.createElement( 'input' );
				new_element.type = 'file';

				// Add new element
				this.parentNode.insertBefore( new_element, this );

				// Apply 'update' to element
				this.multi_selector.addElement( new_element );

				// Update list
				this.multi_selector.addListRow( this );

				// Hide this: we can't use display:none because Safari doesn't like it
				this.style.position = 'absolute';
				this.style.left = '-1000px';

			};
			// If we've reached maximum number, disable input element
			if( this.max != -1 && this.count >= this.max ){
				element.disabled = true;
			};

			// File element counter
			this.count++;
			// Most recent element
			this.current_element = element;
			
		} else {
			// This can only be applied to file input elements!
			alert( 'Error: not a file input element' );
		};

	};

	/**
	 * Add a new row to the list of files
	 */
	this.addListRow = function( element ){

		// Row div
		var new_row = document.createElement( 'div' );

		// Delete button
		var new_row_button = document.createElement( 'input' );
		new_row_button.type = 'button';
		new_row_button.value = 'X';
		new_row_button.className = 'btnUpload';

		// References
		new_row.element = element;
		new_row.className = 'rowUpload';

		// Delete function
		new_row_button.onclick= function(){

			// Remove element from form
			this.parentNode.element.parentNode.removeChild( this.parentNode.element );

			// Remove this row from the list
			this.parentNode.parentNode.removeChild( this.parentNode );

			// Decrement counter
			this.parentNode.element.multi_selector.count--;

			// Re-enable input element (if it's disabled)
			this.parentNode.element.multi_selector.current_element.disabled = false;

			// Appease Safari
			//    without it Safari wants to reload the browser window
			//    which nixes your already queued uploads
			return false;
		};

		// Set row value
		new_row.innerHTML = element.value;

		// Add button
		new_row.appendChild( new_row_button );

		// Add it to the list
		this.list_target.appendChild( new_row );
		
	};

};

var req;

function loadXMLDoc(url) {
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
   	        req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function processReqChange() {
	if (req.readyState == 4) {
		if (req.status == 200) {
			// ...processing statements go here...
			getSubmenu ('', req.responseText);
		} else {
			alert("There was a problem retrieving the XML data:\n" + req.statusText);
		}
	}
}
function getSubmenu (input, response) {
	if (response != ''){     // Response mode
		message   = document.getElementById('aSubMenu');
		message.innerHTML = response;
	} else {// Input mode
		url  = 	'http://www.wzdv.nl/xml/getSubmenu.php?q=' + input;
		loadXMLDoc(url);
	}
}
function popUp(url){
	window.open(url,"pop","width=650,height=500,toolbars=0,scrollbars=1")
}			 


var Type = 'Z';
function StartFloat() {
	if(document.all) {
		document.all.AdFloater.style.pixelLeft = document.body.clientWidth - document.all.AdFloater.offsetWidth;
		document.all.AdFloater.style.visibility = 'visible';
		Type = 'A';
		}
	else if(document.layers) {
		document.AdFloater.left = window.innerWidth - document.AdFloater.clip.width - 16;
		document.AdFloater.visibility = 'show';
		Type = 'B';
		}
	else if(document.getElementById) {
		document.getElementById('AdFloater').style.left = (window.innerWidth - 140) + 'px';
		document.getElementById('AdFloater').style.visibility = 'visible';
		Type = 'C';
		}
	if (document.all) { 
		window.onscroll = Float; 
	} else { 
		setInterval('Float()', 100); 
	}
}
function Float() {
if (Type == 'A') { document.all.AdFloater.style.pixelTop = document.body.scrollTop + 235;}
else if (Type == 'B') { document.AdFloater.top = window.pageYOffset  + 235; }
else if (Type == 'C') { document.getElementById('AdFloater').style.top = window.pageYOffset  + 235 + 'px'; }
}


