function clear_form_elements(ele) {
    $(ele).find(':input').each(function() {
        switch(this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'textarea':
                $(this).val('');
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
        }
    });

}

function stripslashes (str) {
		return (str+'').replace(/\\(.?)/g, function (s, n1) {
        switch (n1) {
            case '\\':
                return '\\';
            case '0':                
				return '\u0000';
            case '':
                return '';
            default:
                return n1;        
		}
    });
}

function submitForm(strForm){
	if(validateRequiredForm(strForm)){
		$.ajax({
            type: "POST",
            url: "/cmsForm/submit/index?format=json",
            data: "url=" + document.location.href +"&" +  $(strForm).serialize(),
            success: function(data){
				eval(stripslashes(data.message[1]));
				clear_form_elements(strForm);
				jQuery.noticeAdd({
                        text: data.message[0],
                        stay: true,
				   		strAppendTo: strForm
                });
			},
			failure: function(){
				return false;
			}
    	});	
	} else {
		return false;
	}	
	return true;
		
}

function validateRequiredForm(strForm){
	var bSubmit = true;
		$('.errors').remove();
		$(strForm + ' :input[class=required]').each(function(i){
			if(this.value == ""){
				//var e = $('<ul class="errors"><li>*</li></ul>');
				//e.insertAfter(this);
				bSubmit = false;
			}		
		});
		if(bSubmit == false){
			jQuery.noticeAdd({
                   text: 'Felterne med * skal udfyldes',
                   stay: false,
				   strAppendTo: strForm
                });
		}
	return bSubmit;
}


//$("input'[title]'").ezpz_hint();

//EZPZ Hint v1.1.1; Copyright (c) 2009 Mike Enriquez, http://theezpzway.com; Released under the MIT License
(function($){
	$.fn.ezpz_hint = function(options){
		var defaults = {
			hintClass: 'ezpz-hint',
			hintName: 'ezpz_hint_dummy_input'
		};
		var settings = $.extend(defaults, options);
		
		return this.each(function(){
			var hint;
			var dummy_input;

			// grab the input's title attribute
			text = $(this).attr('title');
			
			// create a dummy input and place it before the input
			$('<input type="text" name="temp" value="" />').insertBefore($(this));
			
			// set the dummy input's attributes
			hint = $(this).prev('input:first');
			hint.attr('class', $(this).attr('class'));
			hint.attr('size', $(this).attr('size'));
			hint.attr('name', settings.hintName);
			hint.attr('autocomplete', 'off');
			hint.attr('tabIndex', $(this).attr('tabIndex'));
			hint.addClass(settings.hintClass);
			hint.val(text);
			
			// hide the input
			$(this).hide();
			
			// don't allow autocomplete (sorry, no remember password)
			$(this).attr('autocomplete', 'off');
			
			// bind focus event on the dummy input to swap with the real input
			hint.focus(function(){
				dummy_input = $(this);
				$(this).next('input:first').show();
				$(this).next('input:first').focus();
				$(this).next('input:first').unbind('blur').blur(function(){
					if ($(this).val() == '') {
						$(this).hide();
						dummy_input.show();
					}
				});
				$(this).hide();
			});
			
			// swap if there is a default value
			if ($(this).val() != ''){
				hint.focus();
			};
			
			// remove the dummy inputs so that they don't get submitted
			$('form').submit(function(){
				$('.' + settings.hintName).remove();
			});
		});
		
	};
})(jQuery);

(function($){
	$.fn.ezpz_hint_textarea = function(options){
		var defaults = {
			hintClass: 'ezpz-hint',
			hintName: 'ezpz_hint_dummy_textarea'
		};
		var settings = $.extend(defaults, options);
		
		return this.each(function(){
			var hint;
			var dummy_textarea;

			// grab the textarea's title attribute
			text = $(this).attr('title');
			
			// create a dummy textarea and place it before the textarea
			$('<textarea name="temp" value="" />').insertBefore($(this));
			
			// set the dummy textarea's attributes
			hint = $(this).prev('textarea:first');
			hint.attr('class', $(this).attr('class'));
			hint.attr('size', $(this).attr('size'));
			hint.attr('name', settings.hintName);
			hint.attr('autocomplete', 'off');
			hint.attr('tabIndex', $(this).attr('tabIndex'));
			hint.addClass(settings.hintClass);
			hint.val(text);
			
			// hide the textarea
			$(this).hide();
			
			// don't allow autocomplete (sorry, no remember password)
			$(this).attr('autocomplete', 'off');
			
			// bind focus event on the dummy textarea to swap with the real textarea
			hint.focus(function(){
				dummy_textarea = $(this);
				$(this).next('textarea:first').show();
				$(this).next('textarea:first').focus();
				$(this).next('textarea:first').unbind('blur').blur(function(){
					if ($(this).val() == '') {
						$(this).hide();
						dummy_textarea.show();
					}
				});
				$(this).hide();
			});
			
			// swap if there is a default value
			if ($(this).val() != ''){
				hint.focus();
			};
			
			// remove the dummy textareas so that they don't get submitted
			$('form').submit(function(){
				$('.' + settings.hintName).remove();
			});
		});
		
	};
})(jQuery);


$(document).ready(function(){
	$("input'[title]'").ezpz_hint();
	$("textarea'[title]'").ezpz_hint_textarea();
	$('.if-form #if-form-submit').click(function(){
		if (!submitForm(".if-form")) {
            
        } 
		return false;
    });	
});
