/**
 * WysyÅanie podanego formularza ajaxem i wyâ¼Å¤wietlanie wyniku w podaneym elemencie
 * @param string formid ID formularza
 * @param string element ID elementu w ktâârym ma zostaâÃ§ wyâ¼Å¤wietlona odpowiedâ¼â
 */
function submit_form(formid,element) {
	// onBeforeReload();
	$(formid).request({
		evalScripts: true,
		onComplete: function(transport) {
			$(element).innerHTML = transport.responseText;
			
			// myLoader.stop();
			// onAjaxReload();
		},
		onError : function() { myLoader.stop(); $(element).innerHTML = 'ERROR while loading';	}		
	});
}

/**
 * Pobieranie urla i wyâ¼Å¤wietlanie jego zawartoâ¼Å¤ci w elemencie
 * @param string path URL do wyâ¼Å¤wietlenia
 * @param string element ID elementu w ktâârym zostanie wyâ¼Å¤wietlona odpowiedâ¼â
 */
function get_url(path,element) {
	if(!element) element = 'main_content';
	// onBeforeReload();
	new Ajax.Updater(element,path,{
		evalScripts: true,
		onComplete: function() { 
			// myLoader.stop();
			// onAjaxReload();
		},
		onError : function() { myLoader.stop(); $(element).innerHTML = 'ERROR while loading';	}
	});
}



function startBind() {
	bindMap2();
}

function checkRegistration(){
	if($('registrationForm')) {
		rows = $$('#registrationForm p.required input');
		
		valid = true;
		for(i=0;i<rows.length;i++) {
			if(!rows[i].value) { valid = false;
				rows[i].className = 'textEmpty';
			}
			else {
				rows[i].className = 'text';
			}
		}
		// Akceptacja //
		if($('accept').checked!=true) {
			valid = false;
		}
		
		if(valid) $('registrationForm').submit();
		else { alert('Please fill required fields and accept the terms of use.'); return false; }
	}
}

function show_mail(user,domena,kraj,attr) {
	var start = "<a href=\"mailto:"+user+"@"+domena+"."+kraj+"\" title=\"\""
	+ attr + ">";
	var end = "</a>";
	document.write(start+user+"@"+domena+"."+kraj+end);
	return 0;
}


/*
function $(label) {
	if(document.getElementById) o = document.getElementById(label);
		else o = document.all[label];
	return o;
}
*/


/* mapka flash */
function addItemFromFlash(args) {
	el = $('offerLocalizations');
	
	// check if there are any points //
	rows = el.getElementsBySelector('span');
	if(rows.length>0) {
		window.points = rows.length - 1;
	}
	// end //
	
	if(!window.points && window.points!=0) window.points = -1;
	window.points++;
	

	// add variables //
	input = document.createElement('input');
	input.id = 'pointID_'+window.points;
	input.type = 'hidden';
	input.name = 'pointID[]';
	input.value = args[0];
	el.appendChild(input);

	input = document.createElement('input');
	input.id = 'stateID_'+window.points;
	input.type = 'hidden';
	input.name = 'stateID[]';
	input.value = args[1];
	el.appendChild(input);

/*	
	input = document.createElement('input');
	input.id = 'relID_'+window.points;
	input.type = 'hidden';
	input.name = 'relID[]';
	el.appendChild(input);	
*/
	
	span = document.createElement('span');
	span.id = 'stateName_'+window.points;
	Element.update(span,args[2]);
	span.style.cursor = 'pointer';
	el.appendChild(span);
	
	
	Event.observe(span,'click',function() { removeItemFromFlash2(this); });	
	
}
function removeItemFromFlash(args) {
	el = $('offerLocalizations');
	Element.remove($('pointID_'+args[2]));
	Element.remove($('stateID_'+args[2]));
	Element.remove($('stateName_'+args[2]));
	if($('relID_'+args[2])) Element.remove($('relID_'+args[2]));
}
function removeItemFromFlash2(el) {
	if(el.id) {
		id = el.id;
	}
	else {
		id = this.id;
	}
	zmienne = id.split("_");
	locID = zmienne[1];
	
	// get pointID //
	
	pointID = $('pointID_'+locID);
	stateID = $('stateID_'+locID);
	window.points--;

	// REMOVE FORM OFFER //
	args = Array(pointID.value,stateID.value,locID);
	removeItemFromFlash(args);
	// END REMOVE FROM OFFER //

	// REMOVE FROM FLASH //
	// sprawdz czy są w ofercie jeszcze takie punky
	if($$('input[value='+pointID.value+']').length <= 0) {
		$('FlashMapTrip').addItemFromJS(stateID.value,pointID.value);
	}
	// END REMOVE FORM FLASH //
}

function displayMap(disp) {
	if(disp) {
		hideSelectBoxes();
		new Effect.SlideDown('lokalizacjeMapContainer');
	} else {
		new Effect.SlideUp('lokalizacjeMapContainer',{afterFinish: showSelectBoxes});
	}
}

function bindMap2() {
	
	if($('offerMapBox')) {
	}
}
/* </> mapka flash */

function checkForm(form) {
		
	var message;
	var error;
	message = "";
						
	if (form.termin_od.value=="" || form.termin_do.value=="") { message = "Выбери сроки (количество дней)\n"; error = true }
	if (form.kli_imie.value=="") { message = message +  "Имя\n"; error = true }
	if (form.kli_nazwisko.value=="") { message = message + "Фамилия\n"; error = true }
	reg = /^[a-zA-Z0-9_.]{1,30}@[a-zA-Z0-9ąćęłńóśżźĄĆĘŁŃÓŚŻŹ]+(\.[a-zA-Z0-9ąćęłńóśżźĄĆĘŁŃÓŚŻŹ]+)+$/;
	wyn = form.kli_email.value.match(reg);
	if (wyn == null) { message = message + "E-mail\n"; error = true }
	if (form.kli_telefon.value=="") { message = message + "Номер телефона\n"; error = true }
			
	if (error) {
		alert("Заполните следующую форму:\n\n" + message);
		return false;
	}
}

function tripUserData()
{
	if(window.user && !window.user.id)	{
		window.user = window.user.evalJSON();
		
		// set data
		$('kli_imie').value = window.user.firstname;
		$('kli_nazwisko').value = window.user.lastname;
		$('kli_firma').value = window.user.company;
		$('kli_adres').value = window.user.street + ',' + window.user.city;
		$('kli_email').value = window.user.email;
		$('kli_telefon').value = window.user.tel;
	}
}


/**
 * show_map_details
 * @param {type} param 
 */
 function show_map_details(obj,param1,param2) {
 	if($('offerMapBox')) {
 		// $('offerMapBox').style.height = '400px';
 		// $('offerMapBox').style.visibility = 'visible';	
		$('offerMapBox').show();
		
		if(Element.hasClassName(obj,'green')) {
			Element.removeClassName(obj,'green');
			$('FlashMapOffer').addItemFromJS(param1,param2);
		}
		else {
			Element.addClassName(obj,'green');
			if(!window.firstTimeMapShow) {
				setTimeout("$('FlashMapOffer').addItemFromJSon('" + param1 + "','" + param2 + "')",2500);		
				window.firstTimeMapShow = true;
			}
			else {
				$('FlashMapOffer').addItemFromJSon(param1,param2);
			}
		}
 	}
 }

Event.observe(window,'load',startBind);



/*

wyłaczenie obramowania wszystkich linków

window.onload = function() {
var body = document.getElementsByTagName("body").item(0).getElementsByTagName("a");
	for(var i=0; i<body.length; i++) {
		body[i].onfocus = body[i].blur;
	}
}
*/
