function is_valid(obj, offertype) {
	
	fieldName = obj.attr('id');
	
	switch (fieldName) {
	// Ignorelist
	case 'marketplace_offer_description_duration_unlimited':
	case 'marketplace_offer_description_duration':
	case 'marketplace_offer_description_duration_type':
	
	case 'marketplace_offer_application_enddate_day':
	case 'marketplace_offer_application_enddate_month':
	case 'marketplace_offer_application_enddate_year':
	
	case 'marketplace_offer_zipcode':
	case 'marketplace_offer_city':
	case 'schoolselect_state':
	
	case 'schoolselect_school':
	case 'schoolselect_city':
	case 'marketplace_offer_training_school_id':
	
	case 'marketplace_offer_description_startdate_day':
	case 'marketplace_offer_description_startdate_month':
	case 'marketplace_offer_description_startdate_year':
	
	case 'marketplace_offer_description_duration':
	case 'marketplace_offer_description_duration_type':
	
	
		return false;
	
	}
	
	liObj = obj.parent().parent();
	olObj = liObj.parent();
	divObj = olObj.prev();
	
	if (liObj.hasClass(offertype + "Hide") || olObj.hasClass(offertype + "Hide") || divObj.hasClass(offertype + "Hide")) {
		return false;
	} 
	
	return true;
}

function show_datarow(obj) {

	obj.show();
	obj.prev().show();
	obj.parent().show();
	obj.parent().parent().show();
	obj.parent().parent().prev().show();
	
	obj.parent().parent().parent().show();
	
	
}

function init_preview() {

	
	$("#preview").find("dl").hide();
	$("#preview").find("dt").hide();
	$("#preview").find("dd").hide();
	
	
}

function select_to_text(objSelect) {
	
	selOption = objSelect.find('option:selected');

	value = selOption.text();
	keyval = selOption.val();
	
	if (keyval != 0 && keyval != 'null' && keyval != 'none') {
		return value;
	} else {
		return '';
	}
}



function handle_special_cases(offertype) {
	
	// Standort
	
	valZipcode = ($("#marketplace_offer_zipcode").val());
	valCity = ($("#marketplace_offer_city").val());
	valFederalstate = select_to_text($("#marketplace_offer_federalstate_id"));
	
	if (valZipcode != '' && valCity != '' && valFederalstate != '') {
	
		$("#preview_marketplace_offer_location").html(valZipcode + " " + valCity + " (" + valFederalstate + ") ");
		show_datarow($("#preview_marketplace_offer_location"));
	}
	
	// Standort Berufsschule
	
	valSchool = select_to_text($("#schoolselect_school"));
	valCity = select_to_text($("#schoolselect_city"));
	valFederalstate = select_to_text($("#schoolselect_state"));
	
	if (valSchool != '' && valCity != '' && valFederalstate != '' && offertype == 'training') {
	
		$("#preview_marketplace_offer_training_location").html(valSchool + ", " + valCity + " (" + valFederalstate + ") ");
		show_datarow($("#preview_marketplace_offer_training_location"));
	}
	
	// Beginn
	
	valDay = select_to_text($("#marketplace_offer_description_startdate_day"));
	valMonth = select_to_text($("#marketplace_offer_description_startdate_month"));
	valYear = select_to_text($("#marketplace_offer_description_startdate_year"));
	
	if($("#marketplace_offer_description_startnow").attr('checked') == true){
	
		$("#preview_marketplace_offer_description_start").html("ab sofort");
			show_datarow($("#preview_marketplace_offer_description_start"));
	}
	else {
		if (valDay != '' && valMonth != '' && valYear != '' ) {
			$("#preview_marketplace_offer_description_start").html(valDay +  valMonth + " " + valYear);
			show_datarow($("#preview_marketplace_offer_description_start"));
		}
	}
	
	// Dauer
	
	valDuration = ($("#marketplace_offer_description_duration").val());
	valType = select_to_text($("#marketplace_offer_description_duration_type"));

	if ($("#marketplace_offer_description_duration_unlimited").attr('checked') == true) {
	
		$("#preview_marketplace_offer_description_dura").html("unbefristet");
			show_datarow($("#preview_marketplace_offer_description_dura"));
	}
	else {
		if (valDuration != '' && valType != '' ) {
			
			if (valDuration == 1) {
				
				switch (valType) {
				
				case 'Jahre':
					valType = 'Jahr';
				break;
				case 'Monate':
					valType = 'Monat';
				break;
				case 'Wochen':
					valType = 'Woche';
				break;
				case 'Tage':
					valType = 'Tag';
				break;	
				}
			
			}
			
			$("#preview_marketplace_offer_description_dura").html(valDuration + " " + valType);
			show_datarow($("#preview_marketplace_offer_description_dura"));
		}
	}
	// Bewerbungsfrist
	
	valDay = select_to_text($("#marketplace_offer_application_enddate_day"));
	valMonth = select_to_text($("#marketplace_offer_application_enddate_month"));
	valYear = select_to_text($("#marketplace_offer_application_enddate_year"));
	
	if (valDay != '' && valMonth != '' && valYear != '') {
	
		$("#preview_marketplace_offer_application_enddate").html(valDay +  valMonth + " " + valYear);
		show_datarow($("#preview_marketplace_offer_application_enddate"));
	}
	
	// Ausbildung - Unterricht - Radiobutton
	if(offertype == 'training') {
		if( $('input[name=marketplace_offer_training_classtype]:checked').val() == 'block') {
			value = 'Blockunterricht';
			inputObj = $("#preview_" + $(this).attr('name'));
			inputObj.html(value);
			show_datarow(inputObj);
		} 
		if( $('input[name=marketplace_offer_training_classtype]:checked').val() == 'teil') {
			value = 'Teilzeitunterricht';
			inputObj = $("#preview_" + $(this).attr('name'));
			inputObj.html(value);
			show_datarow(inputObj);
		} 
	}
	
}

function read_form() {

	init_preview();
	
	offertype = $("#marketplace_offer_type").val(); // -training-
	
	$("#market_data").find('select').each(function() {
	
		keyval = $(this).val();
		
		value = select_to_text($(this));
		
		if (value != '' && is_valid($(this),offertype)) {
			
			inputObj = $("#preview_" + $(this).attr('name'));
			inputObj.html(value);
			show_datarow(inputObj);
					
		} 	
	});
	
	$("#market_data").find('input').each(function() {
	
		inputType = $(this).attr('type');
		
		switch(inputType) {
			case 'text':
				value = $(this).val();
				if (value != '' && is_valid($(this),offertype)) {
					inputObj = $("#preview_" + $(this).attr('name'));
					inputObj.html(value);
					show_datarow(inputObj);
				}
				break;
			case 'checkbox':
			case 'radio':
			case 'hidden' :
			
			// do nothing
				break;
		}
		
	});

	$("#market_data").find('textarea').each(function() {
	
		value = $(this).val();
		if (value != '' && is_valid($(this),offertype)) {
			inputObj = $("#preview_" + $(this).attr('name'));
			inputObj.html(nl2br(value));
			show_datarow(inputObj);
		}

	});	
	
	
	handle_special_cases(offertype);
}

function show_preview_from_form() {
	
	data = read_form();
	
}

function preview() {

	Shadowbox.init({
        skipSetup: true // skip the automatic setup
    });
    
    read_form();
    //$("#preview_marketplace_offer_company").html($("#marketplace_offer_company").val());
    $("#preview_marketplace_offer_teaching_theory").html($("#marketplace_offer_teaching_theory").val());
    
    titel = 'Vorschau  Ausbildung: ' + select_to_text($("#marketplace_offer_job_id"));
     
    // open a welcome message
    Shadowbox.open({
        title:      titel,
        type:       'html',
        content:    $("#preview").html(),
        height:     350,
        width:      505
    });

}

function login_required(url_encoded) {
	buttons = new Array();
	buttons[0] = new Object();
	buttons[0]['label'] = 'Registrieren'; 
	buttons[0]['callback'] = 'parent.location=\'/user-register.html?network=trainer\'';
	buttons[1] = new Object();
	buttons[1]['label'] = 'Anmelden'; 
	buttons[1]['callback'] = 'parent.location=\'/l?r=' + url_encoded + '\'';
		
	Box = new floatingBox;
	Box.build('myoffers_delete_box');
	Box.setTitle('Registrieren / Anmelden');
	Box.setContentTxt('Dieser Bereich steht nur registrierten Mitgliedern zur Verfügung. Bitte registrieren oder mit Benutzername und Passwort anmelden.');
	Box.setButtons(buttons);
	Box.show();
}

function nl2br(text){
	
	text = escape(text);
	
	replace = false;
	
	if(text.indexOf('%0D%0A') > -1){
		re_nlchar = /%0D%0A/g ;
		replace = true;
		
	}else if(text.indexOf('%0A') > -1){
		re_nlchar = /%0A/g ;
		replace = true;
		
	}else if(text.indexOf('%0D') > -1){
		re_nlchar = /%0D/g ;
		replace = true;
	}
	if (replace == true) {
		return unescape( text.replace(re_nlchar,'<br />') );
	} else {
		return unescape(text);
	}
}

function test_activated(objTest) {
	
	if ($('#marketplace_offer_replyoptions_test').val() == 0) {
	
		objTest.next().show();
		objTest.next().next().show();
		objTest.next().next().next().show();
		objTest.next().next().next().next().show();
	
	} else {
		objTest.next().hide();
		objTest.next().next().hide();
		objTest.next().next().next().hide();
		objTest.next().next().next().next().hide();
		
	}
	
}
