function strReplace(subject, search, replace) {
	aSubstr = subject.split(search);
	newStr = "";
	for (i = 0; i < aSubstr.length - 1; i++) {
		newStr += aSubstr[i] + replace;
	}
	newStr += aSubstr[aSubstr.length - 1];
	return newStr;
}

function isEmpty(value) {
	return value == undefined || value == "";
}

jQuery.fn.validateForm = function() {
	var bValidated = true;
	var children = $(this).children();
	
	if (typeof children == "undefined" || children.size() === 0) { 
		return; 
	} 
	children.each(function(){ 
		var child = $(this); 
		if (child.children().size() > 0) { 
			valid = child.validateForm();
			if (bValidated) { bValidated = valid; }
		} 
		if (!isEmpty($(this).attr('validate'))) {
			valid = child.validate();
			if (bValidated) { bValidated = valid; }
		}
	});
	
	return bValidated;

	//$(this).children().map(function() {
	//	if (!isEmpty($(this).attr('validate'))) {
	//		bValidated = bValidated ? $(this).validate() : false;
	//	}
	//});
	
}

jQuery.fn.validate = function() {
	var aValidate = $(this).attr('validate').split(' ');
	var value = $(this).is(':radio') ? $(this+":checked").val() : $(this).val();
	var bValidated = true;
	var errors = '';
	if (jQuery.inArray('required', aValidate) > -1) {
		errors += !isEmpty(value) ? '' : ((errors == '' ? '' : '\r\n')+'Deze veld is verplicht.');
	}
	if (jQuery.inArray('email', aValidate) > -1) {
		var pattern = new RegExp(/^(("[\w-+\s]+")|([\w-+]+(?:\.[\w-+]+)*)|("[\w-+\s]+")([\w-+]+(?:\.[\w-+]+)*))(@((?:[\w-+]+\.)*\w[\w-+]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][\d]\.|1[\d]{2}\.|[\d]{1,2}\.))((25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{1,2})\.){2}(25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{1,2})\]?$)/i);
		errors += pattern.test(value) ? '' : ((errors == '' ? '' : '\r\n')+'Er is geen geldig emailadres ingevuld.');
	}
	if (jQuery.inArray('numeric', aValidate) > -1) {
		var pattern = new RegExp(/^([0-9]+)$/i);
		errors += pattern.test(value) ? '' : ((errors == '' ? '' : '\r\n')+'Ingevulde waarde is niet numeriek');
	}
	if (jQuery.inArray('postalcode', aValidate) > -1) {
		var pattern = new RegExp(/^([0-9]{4}[a-z]{2})$/i);
		errors += pattern.test(value) ? '' : ((errors == '' ? '' : '\r\n')+'Er is geen geldig postcode ingevuld');
	}
	
	if (!isEmpty(errors)) {
		$(this).css('background', '#ff8888');
		$(this).attr('title', errors);
		return false;
	} else {
		return true;
	}
}

function customValidation() {
	var regionSelected = $('#optGR').is(':checked') || $('#optFR').is(':checked') || $('#optDR').is(':checked') || 
		$('#optGD').is(':checked') || $('#optUT').is(':checked') || $('#optLB').is(':checked') || $('#optNB').is(':checked') || 
		$('#optZH').is(':checked') || $('#optNH').is(':checked') || $('#optFL').is(':checked') || $('#optOV').is(':checked') || 
		$('#optZL').is(':checked');
	var worktimeAdded = !isEmpty($('#wtMonF').val()) && !isEmpty($('#wtMonT').val()) || !isEmpty($('#wtTueF').val()) && !isEmpty($('#wtTueT').val()) || 
		!isEmpty($('#wtWedF').val()) && !isEmpty($('#wtWedT').val()) || !isEmpty($('#wtThuF').val()) && !isEmpty($('#wtThuT').val()) || 
		!isEmpty($('#wtFriF').val()) && !isEmpty($('#wtFrit').val()) || !isEmpty($('#wtSatF').val()) && !isEmpty($('#wtSatT').val()) || 
		!isEmpty($('#wtSunF').val()) && !isEmpty($('#wtSunT').val());
	if (!regionSelected) {
		errmsg = "U dient minimaa 1 provincie te selecteren";
		$('#optGR').css('background', '#ff8888').attr('title', errmsg);
		$('#optFR').css('background', '#ff8888').attr('title', errmsg);
		$('#optDR').css('background', '#ff8888').attr('title', errmsg);
		$('#optOV').css('background', '#ff8888').attr('title', errmsg);
		$('#optGD').css('background', '#ff8888').attr('title', errmsg);
		$('#optUT').css('background', '#ff8888').attr('title', errmsg);
		$('#optFL').css('background', '#ff8888').attr('title', errmsg);
		$('#optNH').css('background', '#ff8888').attr('title', errmsg);
		$('#optZH').css('background', '#ff8888').attr('title', errmsg);
		$('#optZL').css('background', '#ff8888').attr('title', errmsg);
		$('#optNB').css('background', '#ff8888').attr('title', errmsg);
		$('#optLB').css('background', '#ff8888').attr('title', errmsg);
	}
	if (!worktimeAdded) {
		if (isEmpty($('#wtMonF').val()) && isEmpty($('#wtMonT').val()) && isEmpty($('#wtTueF').val()) && isEmpty($('#wtTueT').val()) && 
		isEmpty($('#wtWedF').val()) && isEmpty($('#wtWedT').val()) && isEmpty($('#wtThuF').val()) && isEmpty($('#wtThuT').val()) && 
		isEmpty($('#wtFriF').val()) && isEmpty($('#wtFrit').val()) && isEmpty($('#wtSatF').val()) && isEmpty($('#wtSatT').val()) && 
		isEmpty($('#wtSunF').val()) && isEmpty($('#wtSunT').val())) {
			errmsg = "U heeft uw werktijden niet ingevuld";
			$('#wtMonF').css('background', '#ff8888').attr('title', errmsg);
			$('#wtMonT').css('background', '#ff8888').attr('title', errmsg);
			$('#wtTueF').css('background', '#ff8888').attr('title', errmsg);
			$('#wtTueT').css('background', '#ff8888').attr('title', errmsg);
			$('#wtWedF').css('background', '#ff8888').attr('title', errmsg);
			$('#wtWedT').css('background', '#ff8888').attr('title', errmsg);
			$('#wtThuF').css('background', '#ff8888').attr('title', errmsg);
			$('#wtThuT').css('background', '#ff8888').attr('title', errmsg);
			$('#wtFriF').css('background', '#ff8888').attr('title', errmsg);
			$('#wtFriT').css('background', '#ff8888').attr('title', errmsg);
			$('#wtSatF').css('background', '#ff8888').attr('title', errmsg);
			$('#wtSatT').css('background', '#ff8888').attr('title', errmsg);
			$('#wtSunF').css('background', '#ff8888').attr('title', errmsg);
			$('#wtSunT').css('background', '#ff8888').attr('title', errmsg);
		} else {
			errmsg = "U heeft geen geldige werktijd ingevuld";
			if (!isEmpty($('#wtMonF').val()+$('#wtMonT').val()) && (isEmpty($('#wtMonF').val()) || isEmpty($('#wtMonT').val()))) {
				$('#wtMonF').css('background', '#ff8888').attr('title', errmsg);
				$('#wtMonT').css('background', '#ff8888').attr('title', errmsg);
			}
			if (!isEmpty($('#wtTueF').val()+$('#wtTueT').val()) && (isEmpty($('#wtTueF').val()) || isEmpty($('#wtTueT').val()))) {
				$('#wtTueF').css('background', '#ff8888').attr('title', errmsg);
				$('#wtTueT').css('background', '#ff8888').attr('title', errmsg);
			}
			if (!isEmpty($('#wtWedF').val()+$('#wtWedT').val()) && (isEmpty($('#wtWedF').val()) || isEmpty($('#wtWedT').val()))) {
				$('#wtWedF').css('background', '#ff8888').attr('title', errmsg);
				$('#wtWedT').css('background', '#ff8888').attr('title', errmsg);
			}
			if (!isEmpty($('#wtThuF').val()+$('#wtThuT').val()) && (isEmpty($('#wtThuF').val()) || isEmpty($('#wtThuT').val()))) {
				$('#wtThuF').css('background', '#ff8888').attr('title', errmsg);
				$('#wtThuT').css('background', '#ff8888').attr('title', errmsg);
			}
			if (!isEmpty($('#wtFriF').val()+$('#wtFriT').val()) && (isEmpty($('#wtFriF').val()) || isEmpty($('#wtFriT').val()))) {
				$('#wtFriF').css('background', '#ff8888').attr('title', errmsg);
				$('#wtFriT').css('background', '#ff8888').attr('title', errmsg);
			}
			if (!isEmpty($('#wtSatF').val()+$('#wtSatT').val()) && (isEmpty($('#wtSatF').val()) || isEmpty($('#wtSatT').val()))) {
				$('#wtSatF').css('background', '#ff8888').attr('title', errmsg);
				$('#wtSatT').css('background', '#ff8888').attr('title', errmsg);
			}
			if (!isEmpty($('#wtSunF').val()+$('#wtSunT').val()) && (isEmpty($('#wtSunF').val()) || isEmpty($('#wtSunT').val()))) {
				$('#wtSunF').css('background', '#ff8888').attr('title', errmsg);
				$('#wtSunT').css('background', '#ff8888').attr('title', errmsg);
			}
		}
	}
	if (!regionSelected || !worktimeAdded) {
		return false;
	}
	return true;
}


//copy to clipboard
function CopyToClipboard (textToClipboard) {
    //var input = document.getElementById ("toClipboard");
    //var textToClipboard = input.value;
            
    var success = true;
	if (window.clipboardData) { // Internet Explorer
    	window.clipboardData.setData ("Text", textToClipboard);
    }
    else {
    	// create a temporary element for the execCommand method
	    var forExecElement = CreateElementForExecCommand (textToClipboard);

		/* Select the contents of the element 
    		(the execCommand for 'copy' method works on the selection) */
    	SelectContent (forExecElement);

    	var supported = true;

    	// UniversalXPConnect privilege is required for clipboard access in Firefox
		try {
			if (window.netscape && netscape.security) {
        		netscape.security.PrivilegeManager.enablePrivilege ("UniversalXPConnect");
        	}

        	// Copy the selected content to the clipboard
        	// Works in Firefox and in Safari before version 5
        	success = window.execCommand ("copy", false, null);
        }
        catch (e) {
        	success = false;
        }
                
        // remove the temporary element
    	document.body.removeChild (forExecElement);
    }

	if (success) {
		//alert ("The text is on the clipboard, try to paste it!");
    } else {
		//alert ("Your browser doesn't allow clipboard access!");
		OpenCannotCopyForm(textToClipboard);
    }
}

function CreateElementForExecCommand (textToClipboard) {
    var forExecElement = document.createElement ("div");
    // place outside the visible area
    forExecElement.style.position = "absolute";
    forExecElement.style.left = "-10000px";
    forExecElement.style.top = "-10000px";
    // write the necessary text into the element and append to the document
    forExecElement.textContent = textToClipboard;
    document.body.appendChild (forExecElement);
    // the contentEditable mode is necessary for the  execCommand method in Firefox
    forExecElement.contentEditable = true;

	return forExecElement;
}

function SelectContent (element) {
    // first create a range
    var rangeToSelect = document.createRange ();
    rangeToSelect.selectNodeContents (element);

    // select the contents
    var selection = window.getSelection ();
    selection.removeAllRanges ();
	selection.addRange (rangeToSelect);
}


