/**
 * Form
 */

var leftPosition = 0;

var Form = {

	init : function(rolloId) {
		Form.logger("Form.init(" + rolloId + ")");
		if ($('.rollo' + rolloId + ' form[id!=googleform]').size()) {
			$('.rollo' + rolloId + ' form[id!=googleform]').each(function() {

				$form = $(this);
				idname = Form.changeAction($form);
				
				

				if (rolloId == 1) {
					$form.ajaxForm( {
						target : '#' + idname,
						success : Form.onSuccessRollo1
					});
				}
				if (rolloId == 2) {
					$form.ajaxForm( {
						target : '#' + idname,
						success : Form.onSuccessRollo2
					});
				}
				if (rolloId == 3) {
					$form.ajaxForm( {
						target : '#' + idname,
						success : Form.onSuccessRollo3
					});
				}
				if (rolloId == 4) {
					$form.ajaxForm( {
						target : '#' + idname,
						success : Form.onSuccessRollo4
					});
				}

			});

			Form.initFileUploadImage();
		}

		if ($('tx-indexedsearch. form.tx_indexedsearch').size()) {
			$('tx-indexedsearch. form.tx_indexedsearch').each(function() {
				$form = $(this);
				$form.ajaxForm( {
					target : '.tx-indexedsearch'
				});
			});
		}
		if ($('#parentID_RECORD').attr('value') == undefined) {
			Form.handleSsl();	
		}
		
		Form.initScroll();
		

	},

	/**
	 * DHA: FÃ¼gt einen SSL-Link an die Formulare an und handelt das Switchen zwischen HTTP/HTTPS-Versionen  
	 * 
	 */
	handleSsl : function() {
		
		var loc = window.location;
		
		// Alle Links im SSL-Modus auf HTTP zurÃ¼ck umbiegen
		if (loc.protocol == "https:") {
			$('a').not('a.keepSSL').unbind('click').click(function() {
				$(this).removeClass('thickbox')
				var http_location = addPreSlash(removeDomain($(this).attr('href'))).split("?");
				var redirect = 'http://' + loc.hostname + http_location[0];
//				alert (redirect);
//				return false;
				loc.href = redirect; 
				return false;
			});
		} else {
			// SSL-Link jedem Formular voranstellen und auf HTTPS-Version verlinken 
			var https_location = loc.href.replace(/^http/i, 'https');
			var ssl_link = '<a href="' + https_location + '" class="switch2ssl">Zum SSL-verschlüsselten Formular</a>';
			if ($('.switch2ssl').length == 0) {
				$('.formextension_form').prev('div').append(ssl_link).unbind('click');
			}
		}
		
	},

	initFileUploadImage : function() {
		Form.logger("Form.initFileUploadImage()");
		$(".js_uploadbutton img").attr("width", "337");
		$(".js_uploadbutton img").attr("height", "20");
		$(".js_uploadbutton img").attr("src",
				"/fileadmin/templates/images/generell/clear.gif");
	},

	initScroll : function() {
		Form.logger("Form.initScroll()");
		$(".content,.container-center").scroll(function() {
			if ($(".tmpErrorField").size()) {
				Form.hideErrorLayer();
				$('#formvars').attr('error_msg_field_id', '0');
			}
		});
	},

	unbindScroll : function() {
		Form.logger("Form.unbindScroll()");
		$(".content").unbind("scroll");
	},

	initThickbox : function() {
		Form.logger("Form.initThickbox()");
		if ($('#TB_ajaxContent form[id!=googleform]').size()) {
			$('#TB_ajaxContent form[id!=googleform]').each(function() {

				$form = $(this);
				idname = Form.changeAction($form);

				$form.ajaxForm( {
					target : '#' + idname,
					success : Form.onSuccessThickbox
				});

			});
		}

		$("#TB_ajaxContent").scroll(function() {
			Form.hideErrorLayer();
		});

		Form.initScroll();
	},

	faqInit : function(rolloId) {
		Form.logger("Form.faqInit(" + rolloId + ")");
		$(".rollo" + rolloId + " .bm_faq_cat_selector").change(function() {
			$(".rollo" + rolloId + " #bm_faq").closest("form").submit();
		});
	},

	changeAction : function($form) {
		Form.logger("Form.changeAction(" + $form + ")");
		url = $form.attr("action");

		idname = $form.closest("div[id^=ce]").attr("id");
		cename = idname.substr(2);

		
		// manipulating the cename if any parent ID is given
		if ($('#parentID_RECORD').attr('value') != undefined) {
			cename = $('#parentID_RECORD').attr('value');	
		}
		
		
		if (url.indexOf("?") == -1) {
			url += "?";
		}
		if (url.indexOf("type=5") == -1) {
			url += "&type=5";
		}
		/*
		if ($form.attr('id') != 'tarifFinderRedirect') {
			if (url.indexOf("type=5") == -1) {
				url += "&type=5";
			}
		}*/
		
		
		if ($form.attr('id') != 'tarifFinderRedirect') {
			if (url.indexOf("ce=" + cename) == -1) {
				url += "&ce=" + cename;
			}
		}
		
		if (url.indexOf("no_cache=1") == -1) {
			url += "&no_cache=1";
		}
		$form.attr("action", url);

		// setting the right id for the container for the replacement of the form
		if ($('#parentID_RECORD').attr('value') != undefined) {
			idname = 'ce' + cename;
		}
		return idname;
	},

	onSuccessRollo1 : function(responseText, statusText) {
		Form.logger("Form.onSuccessRollo1(" + responseText + "," + statusText
				+ ")");
		// $paneTarget = $(".rollo1 .subcontent .content");
		// $paneTarget.stop().scrollTo( { top:0,left:0} , 800 );
		Form.init(rolloId);
		History.initRollo(rolloId);
	},

	onSuccessRollo2 : function(responseText, statusText) {
		Form.logger("Form.onSuccessRollo2(" + responseText + "," + statusText
				+ ")");
		// $paneTarget = $(".rollo2 .subcontent .content");
		// $paneTarget.stop().scrollTo( { top:0,left:0} , 800 );
		Form.init(rolloId);
		History.initRollo(rolloId);
	},

	onSuccessRollo3 : function(responseText, statusText) {
		Form.logger("Form.onSuccessRollo3(" + responseText + "," + statusText
				+ ")");
		// $paneTarget = $(".rollo3 .subcontent .content");
		// $paneTarget.stop().scrollTo( { top:0,left:0} , 0 );
		Form.init(rolloId);
		History.initRollo(rolloId);
	},

	onSuccessRollo4 : function(responseText, statusText) {
		Form.logger("Form.onSuccessRollo4(" + responseText + "," + statusText
				+ ")");
		// $paneTarget = $(".rollo4 .subcontent .content");
		// $paneTarget.stop().scrollTo( { top:0,left:0} , 800 );
		Form.init(rolloId);
		History.initRollo(rolloId);
	},

	onSuccessThickbox : function(responseText, statusText) {
		Form.logger("Form.onSuccessThickbox(" + responseText + "," + statusText
				+ ")");
		// $paneTarget = $("#TB_ajaxContent .subcontent .content");
		// $paneTarget.stop().scrollTo( { top:0,left:0} , 800 );
		Form.initThickbox();
		History.initThickbox();
	},

	showErrorLayer : function(errorFieldId) {
		Form.logger("Form.showErrorLayer(" + errorFieldId + ")");
		$contentClass = $("#" + errorFieldId).closest(".rollo-home").parent()
				.attr("class");
		if ($("#TB_ajaxContent").size()) {
			// $errorField = $("#TB_ajaxContent #"+errorFieldId+":hidden");
			$errorField = $("#TB_ajaxContent #" + errorFieldId);
			$($errorField).show();
			leftPos = $($errorField).offset().left;
			topPos = $($errorField).offset().top;
			$($errorField).hide();
			$($errorField).clone().appendTo("body");
			$("body #" + errorFieldId + ":last").addClass("tmpErrorField");
		} else {
			// $errorField = $("#"+errorFieldId+":hidden");
			$errorField = $("#content-full-width form:visible").find(
					"#" + errorFieldId);
			$($errorField).show();
			leftPos = $($errorField).offset().left;
			topPos = $($errorField).offset().top;
			$($errorField).hide();
			$($errorField).clone().appendTo("body");
			$("body #" + errorFieldId + ":last").addClass("tmpErrorField");
		}

		if (leftPos) {
			leftPosition = leftPos;
		}

		$("body .tmpErrorField").css( {
			"position" : "absolute",
			"left" : leftPosition + "px",
			"top" : topPos + "px",
			"z-index" : 200
		});

		$("body .tmpErrorField").show();
	},

	hideErrorLayer : function(field_id) {
		Form.logger("Form.hideErrorLayer(" + field_id + ")");

		if (arguments.length < 1) {
			field_id = "";
		}

		$("body .tmpErrorField").each(function() {
			$formfield = $(this);
			if (field_id != $formfield.attr("id")) {
				$formfield.remove();
			}
		});

		// if (!field_id) {
	// $("body .tmpErrorField").remove();
	// }
},

setCustomerSelectorAction : function(url) {
	Form.logger("Form.setCustomerSelectorAction(" + url + ")");
	if (Rollos.getRolloByHash(url) == 1) {
		$("#customer-selector").attr("action", url);
	}
},

logger : function(logstring) {
	//log(logstring);
}
};

