﻿var t;
var c = 0;
function StartCountdown(id) {
	if (id == null) { clearTimeout(t); c = 0; }
	c = c + 1;
	t = setTimeout("StartCountdown(1)", 1000);
	if (c > 5) { clearTimeout(t); c = 0; Filter(0); }
}

function GoToPage(str) {
	Filter(str);
}

function Filter(str) {
	clearTimeout(t);
	
	var a = document.filter_form;
	var o1 = window.document.getElementById("div_offers");
	var o2 = window.document.getElementById("h1");
	//var o3 = window.document.getElementById("div_hint");
	
	o2.innerHTML = "Se filtreaza lista <img src='http://icons.viaromania.eu/loading_bar.gif' border='0' align='bottom'>";
	//o3.style.display = "block";
	
	var cls = new Array();
	var typ = new Array();
	var loc = new Array();
	var prc = new Array();
	var mlt = new Array();
	var ngh = new Array();
	var ded = "";
	var key = window.document.getElementById("list_keywords").value;
	
	var cls_count = typ_count = loc_count = prc_count = mlt_count = ded_count = ngh_count = 0;
	
	for (i=0; i<a.length; i++) {
		if (a.elements[i].name == "cls") {
			cls_count += 1;
			if (a.elements[i].checked) cls[cls.length] = a.elements[i].value;
		}
		if (a.elements[i].name == "mlt") {
			mlt_count += 1;
			if (a.elements[i].checked) mlt[mlt.length] = a.elements[i].value;
		}
		if (a.elements[i].name == "typ") {
			typ_count += 1;
			if (a.elements[i].checked) typ[typ.length] = a.elements[i].value;
		}
		if (a.elements[i].name == "loc") {
			loc_count += 1;
			if (a.elements[i].checked) loc[loc.length] = a.elements[i].value;
		}
		if (a.elements[i].name == "prc") {
			prc_count += 1;
			if (a.elements[i].checked) prc[prc.length] = a.elements[i].value;
		}
		if (a.elements[i].name == "ngh") {
			ngh_count += 1;
			if (a.elements[i].checked) ngh[ngh.length] = a.elements[i].value;
		}
	}
	
	if (cls.length == cls_count) cls = "";
	if (typ.length == typ_count) typ = "";
	if (loc.length == loc_count) loc = "";
	if (prc.length == prc_count) prc = "";
	if (mlt.length == mlt_count) mlt = "";
	if (ngh.length == ngh_count) ngh = "";
	if (a.ded.selectedIndex > 0) ded = a.ded.options[a.ded.selectedIndex].value;
	
	var url_params = "event=" + event_name + "&cls=" + cls + "&typ=" + typ + "&loc=" + loc + "&prc=" + prc + "&mlt=" + mlt + "&ngh=" + ngh + "&ded=" + ded + "&str=" + str + "&key=" + escape(key);
	
	if (window.document.getElementById("div_save_link")) window.document.getElementById("div_save_link").style.display = "none";
	if (window.document.getElementById("div_yahoo_link")) window.document.getElementById("div_yahoo_link").style.display = "none";
	
	_gaq.push(['_trackPageview', 'http://' + server_name + '/?' + url_params]);
	CallAjax("litoral_offers.cfm?" + url_params + "&ref=" + Math.random(), 3, "div_offers", true);
	ScrollTo("search_box");
	
	if (window.document.getElementById("short_url_params")) window.document.getElementById("short_url_params").value = url_params;
}

var action_type = 1;
function GenerateShortLink(type) {
	var o1 = window.document.getElementById("short_url_params");
	action_type = type;
	CallAjax("includes/generate_short_url.cfm?short_url=" + server_name + "&" + o1.value, 1, GenerateShortLinkDone, true);
}
function GenerateShortLinkDone(text) {
	var s = new String(text);
	rExp = /<META NAME="ColdFusionMXEdition" CONTENT="ColdFusion DevNet Edition - Not for Production Use.">/gi;
	s = s.replace(rExp,'');
	
	if (window.document.getElementById("div_save_link")) window.document.getElementById("div_save_link").style.display = "none";
	if (window.document.getElementById("div_yahoo_link")) window.document.getElementById("div_yahoo_link").style.display = "none";
	
	var short_url = trimAll(s);
	if (action_type == 1) { window.document.getElementById("div_save_link").style.display = "block"; window.document.getElementById("short_link_save").value = short_url; window.document.getElementById("short_link_save").select(); }
	if (action_type == 2) { window.document.getElementById("div_yahoo_link").style.display = "block"; window.document.getElementById("short_link_yahoo").value = short_url; window.document.getElementById("short_link_yahoo").select(); }
	if (action_type == 3) window.open("http://www.facebook.com/sharer.php?u=" + escape(short_url) + "&t=Oferte turism viaRomania.eu");
	if (action_type == 4) window.open("http://twitter.com/home?status=" + escape(short_url));
}

function UncheckAllButOne(obj, id) {
	var a = document.filter_form;
	
	for (i=0; i<a.length; i++) {
		if (a.elements[i].name == obj) {
			if (a.elements[i].value == id) a.elements[i].checked = true;
			else a.elements[i].checked = false;
		}
	}
	Filter(0);
}

function SearchList() {
	var key = window.document.getElementById("list_keywords").value;
	if (key.length > 2) Filter(0);
}

function runScript(e) {
	if (e.keyCode == 13) {
		Filter(0);
	}
}

function init() {
	(function(d){
	  var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
	  js = d.createElement('script'); js.id = id; js.async = true;
	  js.src = "//connect.facebook.net/en_US/all.js#appId=123457241087801&xfbml=1";
	  d.getElementsByTagName('head')[0].appendChild(js);
	}(document));	
}
