// JavaScript Document
Event.observe(window, "load", function(){
	new DavEffect.slide(
		$('smallPic1').getElementsByTagName('img'),
		$('bigPic1').getElementsByTagName('img'), 
		$('zoom1')
	);
	new DavEffect.slide(
		$('smallPic2').getElementsByTagName('img'),
		$('bigPic2').getElementsByTagName('img'), 
		$('zoom2')
	);
	//global.Element.checkNumber("apply[apply_member]");
	//global.Element.checkNumber("apply[apply_postcode]");
}, false);

/**
 * Member of the box to remove import value
 */
function clearValueOfApplyMember(event){
	$("apply[apply_member]").value = "";
}

function checkRadio(elements){
	var error = 1;
	$A(elements).each(function(element){
		if($(element).checked) error = 0;
		return false;
	});
	return error === 1 ? true : false;
}
var allElements = [];
function checkForm(){
	var errorMessage = "Bitte &uuml;berpr&uuml;fen Sie Ihre Eingabe. Es m&uuml;ssen alle Pflichtfelder ausgef&uuml;llt sein.";
	var errorElements = [],  intError = 0; valEmail = /^(([\-\w]+)\.?)+@(([\-\w]+)\.?)+\.[a-zA-Z]{2,4}$/;
	allElements = [];
	var noteEmailError = "Ihre E-Mail Adresse hat ein falsches Format.";
	var error_email = false;
	
	if(checkRadio($("orderForm")["apply[apply_sex]"])){
		intError ++;	//save the error number
		$("title_apply[apply_sex]").className = "errorAlert";
	}else{
		$("title_apply[apply_sex]").className = "";
	}
	//Name
	allElements.push("apply[apply_name]");
	if($("apply[apply_name]").value == ""){
		errorElements.push("apply[apply_name]");
	}
	
	//StraBe
	allElements.push("apply[apply_street]");
	if($("apply[apply_street]").value == ""){
		errorElements.push("apply[apply_street]");
	}
	
	//Ort
	allElements.push("apply[apply_city]");
	if($("apply[apply_city]").value == ""){
		errorElements.push("apply[apply_city]");
	}
	
	//Geburtsdatum
	allElements.push("apply[apply_birthday]");
	if($("apply[apply_birthday]").value == ""){
		errorElements.push("apply[apply_birthday]");
	}
	
	//Vorname
	allElements.push("apply[apply_name1]");
	if($("apply[apply_name1]").value == ""){
		errorElements.push("apply[apply_name1]");
	}
	
	//PLZ
	allElements.push("apply[apply_postcode]");
	if($("apply[apply_postcode]").value == ""){
		errorElements.push("apply[apply_postcode]");
	}
	
	//Land
	allElements.push("apply[apply_country]");
	if($("apply[apply_country]").value == ""){
		errorElements.push("apply[apply_country]");
	}
	
	//Tel. Privat
	allElements.push("apply[apply_private_tel]");
	if($("apply[apply_private_tel]").value == ""){
		errorElements.push("apply[apply_private_tel]");
	}
	//E-Mail-Adresse
	if(!valEmail.test($("apply[apply_email]").value)){
		$("apply[apply_email]").style.borderColor = "red";
		error_email = true;
	}else{
		$("apply[apply_email]").style.borderColor = "#A0A0A0";
	}

	//E-Mail-Adresse (Sicherungseingabe)
	if(!valEmail.test($("apply[apply_email1]").value)){
		$("apply[apply_email1]").style.borderColor = "red";
		error_email = true;
	}else{
		$("apply[apply_email1]").style.borderColor = "#A0A0A0";
	}
	
	if($("apply[apply_email1]").value != $("apply[apply_email]").value){
		$("apply[apply_email1]").style.borderColor = "red";
		error_email = true;
	}	

	if(!$("apply[apply_travel_rule]").checked){
		intError ++;
		$("title_apply[apply_travel_rule]").className = "errorAlert";
	}else{
		$("title_apply[apply_travel_rule]").className = "";
	}
	
	if(!$("apply[apply_agb]").checked){
		intError ++;
		$("title_apply[apply_agb]").className = "errorAlert";
	}else{
		$("title_apply[apply_agb]").className = "";
	}
	
	if(!$("apply[apply_safe_rule]").checked){
		intError ++;
		$("title_apply[apply_safe_rule]").className = "errorAlert";
	}else{
		$("title_apply[apply_safe_rule]").className = "";
	}
	
	clearInputErrorAlert();
	if(errorElements.length > 0 || intError > 0){
		if(error_email){
			errorMessage += "<br />"+noteEmailError;
		}
		errorElements.each(function(element){
			$(element).style.borderColor = "red";
		});
		showErrorMessage("showError", errorMessage);
		return false;
	}else{
		if(error_email){
			showErrorMessage("showError", noteEmailError);
			return false;
		}
	}
	showErrorMessage("showError", "");
	showContent();
	return false;
	}

/**
 * Clear all error alert
 */
function clearInputErrorAlert(){
	if(allElements.length > 0){
		allElements.each(function(element){
			$(element).style.borderColor = "#A0A0A0";
		});
		return true;
	}else{
		return false;
	}
}
function clearErrorAlert(){
	if(clearInputErrorAlert()){
		$("title_apply[apply_travel_rule]").className = "";
		$("title_apply[apply_agb]").className = "";
		$("title_apply[apply_safe_rule]").className = "";
		$("title_apply[apply_sex]").className = "";
		showErrorMessage("showError", "");
	}
}

function showContent(){
	//xiaowei add start
	if($F('is_categoryID') == 'YES')
	{
		$('show_is_is_categoryID_title').style.display = '';
		$('show_is_is_categoryID_content1').style.display = '';
		$('show_is_is_categoryID_content2').style.display = '';
		$('show_is_is_categoryID_content2_old').style.display = 'none';
	}
	else
	{
		$('show_is_is_categoryID_title').style.display = 'none';
		$('show_is_is_categoryID_content1').style.display = 'none';
		$('show_is_is_categoryID_content2').style.display = 'none';
		$('show_is_is_categoryID_content2_old').style.display = '';
	}
	//xiaowei add end
	
	var elements = $A($("orderForm").elements);
	$A($("orderForm")["apply[apply_sex]"]).each(function(element){
		if($(element).checked){
			if($(element).value == "1"){
				$("show_"+element.name).innerHTML = "Herr";
			}else if($(element).value == "0"){
				$("show_"+element.name).innerHTML = "Frau";
			}else{
				$("show_"+element.name).innerHTML = "";
			}
		}
	});
	
	elements.each(function(element){
		if($(element).type.toLowerCase() == "text" || $(element).type.toLowerCase() == "hidden"){
			if($("show_"+$(element).name))
				$("show_"+$(element).name).innerHTML = $(element).value;
		}
	});
	//xiaowei edit start
	if($F('is_categoryID') == 'YES')
	{
		var radioOnlyOfCheckboxItems = [
				"apply[apply_single_room]", "apply[apply_double_room]","apply[apply_buchung_bahnfahrkarte]",
				"apply[apply_buchung_flug]", "apply[apply_flug_selbstbucher]", "apply[apply_vegetarisches_essen]"
			];
	}
	else
	{
		var radioOnlyOfCheckboxItems = [
				"apply[apply_single_room]", "apply[apply_double_room]", "apply[apply_abs]",
				"apply[apply_is_train_ticket]", "apply[apply_is_card]", "apply[apply_activity]"
			];
	}
	//xiaowei edit end
	radioOnlyOfCheckboxItems.each(function(element){
		if($(element).checked){
			$("show_"+$(element).name).innerHTML = "gew&uuml;nscht";
		}else{
			$("show_"+$(element).name).innerHTML = "keine Angabe";
		}
	});

	try{
		if($("orderForm")["apply[apply_is_public_contact]"].checked){
			$("show_apply[apply_is_public_contact]").style.display = "";
		}else{
			$("show_apply[apply_is_public_contact]").style.display = "none";
		}
	}catch(e){}
	showOrderDetail();
}

function showErrorMessage(element, message){
	document.getElementById(element).innerHTML = message;
}

function showOrderForm(data){
	tabFrame("orderTable");
	//alert(typeof data);
	//if(data && typeof data == "string"){
		//data = eval('(' + data + ')');
		$("date_hidden").value = data.date;
		$("date_data_show").innerHTML = data.date;
		$("price_hidden").value = data.price;
	//}
}

function showOrderDetail(){
	tabFrame("orderDetail");
}

function showOrderSuccess(){
	tabFrame("orderSuccess");
}

function showOrderError(){
	tabFrame("orderError");
}
function showOrderBasic(){
	tabFrame("orderBasic");
}
function showcontinueorderError(){
	tabFrame("continueorderError");
}
function tabFrame(elementShow){
	var elements = ["orderBasic", "orderTable", "orderDetail", "orderSuccess", "orderError", "continueorderError"];
	try{
		elements.each(function(element){
			if($(element) == $(elementShow)){
				$(element).style.display = "";
			}else{
				$(element).style.display = "none";
			}
		});
		setFooterStyle();
	}catch(e){}
}

/**
 * Continue order
 *
 */
function continue_order(op)
{
	//xiaowei edit start
	var sex;
	$A($("orderForm")['apply[apply_sex]']).each(function(element){
		if($(element).checked) sex = $(element).value;
	});
	var vorname = $F('apply[apply_name1]');
	
	
	var buchungscode = $F('Bookingcode');
	var region = $F('region');
	var travel_date = $F('date_hidden');
	var travel_price = $F('price_hidden');
	var apply_number = encodeURIComponent($F('apply[apply_number]'));
	
	
	var apply_name = encodeURIComponent($F('apply[apply_name]'));
	var apply_postcode = encodeURIComponent($F('apply[apply_postcode]'));
	var apply_street = encodeURIComponent($F('apply[apply_street]'));
	var apply_country = encodeURIComponent($F('apply[apply_country]'));
	var apply_city = encodeURIComponent($F('apply[apply_city]'));
	var apply_member_number = encodeURIComponent($F('apply[apply_member_number]'));
	var apply_client_number = encodeURIComponent($F('apply[apply_client_number]'));
	var apply_fax = encodeURIComponent($F('apply[apply_fax]'));
	var apply_birthday = encodeURIComponent($F('apply[apply_birthday]'));
	var apply_email = encodeURIComponent($F('apply[apply_email]'));
	var apply_tel = encodeURIComponent($F('apply[apply_tel]'));
	var apply_email1 = encodeURIComponent($F('apply[apply_email1]'));
	var apply_private_tel = encodeURIComponent($F('apply[apply_private_tel]'));
	
	
	var apply_single_room = $('apply[apply_single_room]').checked == true ? "checked" : "";
	var apply_double_room = $('apply[apply_double_room]').checked == true ? "checked" : "";
	var apply_double_room_with = encodeURIComponent($F('apply[apply_double_room_with]'));
	if($F('is_categoryID') == 'YES')
	{
		var is_categoryID = 'YES';
		var apply_reisepassnummer = $F('apply[apply_reisepassnummer]');
		var apply_ausstellungsort = $F('apply[apply_ausstellungsort]');
		var apply_von = $F('apply[apply_von]');
		var apply_bis = $F('apply[apply_bis]');
		var apply_nationalitat = $F('apply[apply_nationalitat]');
		var apply_beruf = $F('apply[apply_beruf]');
		
		var apply_buchung_bahnfahrkarte = $('apply[apply_buchung_bahnfahrkarte]').checked == true ? "checked" : "";
		var apply_buchung_flug = $('apply[apply_buchung_flug]').checked == true ? "checked" : "";
		var apply_abflugort_flug = $F('apply[apply_abflugort_flug]');
		var apply_zielort_flug = $F('apply[apply_zielort_flug]');
		var apply_flug_selbstbucher = $('apply[apply_flug_selbstbucher]').checked == true ? "checked" : "";
		var apply_flug_ab = $F('apply[apply_flug_ab]');
		var apply_ankunft_zielgebiet_um = $F('apply[apply_ankunft_zielgebiet_um]');
		var apply_uhr_mit_flugnr1 = $F('apply[apply_uhr_mit_flugnr1]');
		var apply_abflug_zielgebiet_um = $F('apply[apply_abflug_zielgebiet_um]');
		var apply_uhr_mit_flugnr2 = $F('apply[apply_uhr_mit_flugnr2]');
		var apply_vegetarisches_essen = $('apply[apply_vegetarisches_essen]').checked == true ? "checked" : "";
		var apply_postil = $F('apply[apply_postil]');
		
		var old_value = '&apply_reisepassnummer='+apply_reisepassnummer+'&apply_ausstellungsort='+apply_ausstellungsort+'&apply_von='+apply_von+'&apply_bis='+apply_bis+'&apply_nationalitat='+apply_nationalitat+'&apply_beruf='+apply_beruf+'&apply_buchung_bahnfahrkarte='+apply_buchung_bahnfahrkarte+'&apply_buchung_flug='+apply_buchung_flug+'&apply_abflugort_flug='+apply_abflugort_flug+'&apply_zielort_flug='+apply_zielort_flug+'&apply_flug_selbstbucher='+apply_flug_selbstbucher+'&apply_flug_ab='+apply_flug_ab+'&apply_ankunft_zielgebiet_um='+apply_ankunft_zielgebiet_um+'&apply_uhr_mit_flugnr1='+apply_uhr_mit_flugnr1+'&apply_abflug_zielgebiet_um='+apply_abflug_zielgebiet_um+'&apply_uhr_mit_flugnr2='+apply_uhr_mit_flugnr2+'&apply_vegetarisches_essen='+apply_vegetarisches_essen+'&apply_postil='+apply_postil;
	}
	else
	{
		var is_categoryID = '';
		var apply_abs = $('apply[apply_abs]').checked == true ? "checked" : "";
		var apply_is_train_ticket = $('apply[apply_is_train_ticket]').checked == true ? "checked" : "";
		var apply_originate = $F('apply[apply_originate]');
		var apply_is_card = $('apply[apply_is_card]').checked == true ? "checked" : "";
		var apply_activity = $('apply[apply_activity]').checked == true ? "checked" : "";
		var apply_postil = $F('apply[apply_postil]');
		
		var old_value = '&apply_abs='+apply_abs+'&apply_is_train_ticket='+apply_is_train_ticket+'&apply_originate='+apply_originate+'&apply_is_card='+apply_is_card+'&apply_activity='+apply_activity+'&apply_postil='+apply_postil;
	}
	
	var apply_urgent_person = encodeURIComponent($F('apply[apply_urgent_person]'));
	var apply_urgent_address = encodeURIComponent($F('apply[apply_urgent_address]'));
	var apply_urgent_tel = encodeURIComponent($F('apply[apply_urgent_tel]'));
	var apply_is_public_contact = $('apply[apply_is_public_contact]').checked == true ? "checked" : "";
	var apply_travel_rule = $('apply[apply_travel_rule]').checked == true ? "checked" : "";
	var apply_agb = $('apply[apply_agb]').checked == true ? "checked" : "";
	var apply_safe_rule = $('apply[apply_safe_rule]').checked == true ? "checked" : "";
	
	//Tourist access to telephone contact
	var email_hidden = $F('email_hidden');
	
	var param = 'sex='+sex+'&vorname='+vorname+'&buchungscode='+buchungscode+'&travel_price='+travel_price+'&travel_date='+travel_date+'&apply_number='+apply_number+'&apply_name='+apply_name+'&apply_postcode='+apply_postcode+'&apply_street='+apply_street+'&apply_country='+apply_country+'&apply_city='+apply_city+'&apply_member_number='+apply_member_number+'&apply_client_number='+apply_client_number+'&apply_fax='+apply_fax+'&apply_birthday='+apply_birthday+'&apply_email='+apply_email+'&apply_tel='+apply_tel+'&apply_email1='+apply_email1+'&apply_private_tel='+apply_private_tel+'&apply_single_room='+apply_single_room+'&apply_double_room='+apply_double_room+'&apply_double_room_with='+apply_double_room_with+'&apply_urgent_person='+apply_urgent_person+'&apply_urgent_address='+apply_urgent_address+'&apply_urgent_tel='+apply_urgent_tel+'&apply_is_public_contact='+apply_is_public_contact+'&apply_travel_rule='+apply_travel_rule+'&apply_agb='+apply_agb+'&apply_safe_rule='+apply_safe_rule+'&email_hidden='+email_hidden+'&op='+op+'&region='+region+'&is_categoryid='+is_categoryID+old_value+'';

	//google  Analytics
	//$('orderForm').observe('submit', function(event){pageTracker._linkByPost(this);});
	Event.observe($('orderForm'), 'submit', function(event) { pageTracker._linkByPost(Event.element(event)); });
	
	new Ajax.Request('index.php?id=75&send=CREATE_SESSION',
	{
		parameters:param,
		method:'post',
		onComplete:function(x)
		{
			//alert(x.responseText);
			if(x.responseText == 'ERROR')
			{
				$('show_order_error').style.display = 'none';
				showcontinueorderError();
			}
			else if(x.responseText == 'SENDSUCCESS')
			{
				$('show_order_error').style.display = 'none';
				showOrderSuccess();
			}
			else if(x.responseText == 'SENDERROR')
			{
				$('show_order_error').style.display = 'none';
				showOrderError();
			}
			else
			{
				$('show_order_error').style.display = 'none';
				clearFormValues('orderForm');
				showOrderForm();
			}	
		},
		onLoading:function()
		{
			$('show_order_error').style.display = 'block';
		}
	});	
	//xiaowei edit end
}
/**
 * Clear form value
 *
 * @param {object} elementForm  Form ID 
 */
function clearFormValues(elementForm){
	var elements = $(elementForm).elements;
	for(var i = 0, n = elements.length; i < n; i ++){
		var typeOfElement = elements[i].type.toLowerCase(), tagName = elements[i].tagName.toLowerCase();
		if(typeOfElement == "text" || typeOfElement == "hidden" || tagName == "textarea" || tagName == "select"){
			if(!(elements[i].getAttribute("name") == "date_hidden" || elements[i].getAttribute("name") == "price_hidden" || elements[i].getAttribute("name") == "email_hidden" || elements[i].getAttribute("name") == "Bookingcode" || elements[i].getAttribute("name") == "region" || elements[i].getAttribute("name") == "is_categoryID"))
				elements[i].value = "";
		}else if(typeOfElement == "checkbox" || typeOfElement == "radio"){
			elements[i].checked = false;
		}
	}
}
