/*
	javascript development
	============================
	website 	: 	Cycladia
	date 		: 	28-9-2010		
	author 		: 	mayra metaxa / developer
	company		: 	mozaik creative business solutions
	url			:	http://www.mozaik.com

*/


function trackpage_ga(trackercat)
{
	//category, action, opt_label, opt_value
	_gaq.push(['_trackEvent', trackercat, 'click', tracking_page]);
	_gaq.push(['_trackPageview', trackercat]);
	
}

function trackev_ga(trackercat)
{
	//category, action, opt_label, opt_value
	_gaq.push(['_trackEvent', trackercat, 'click', tracking_page]);	
}

function trackev_ga_2(trackercat, trackervalue)
{
	//category, action, opt_label, opt_value
	_gaq.push(['_trackEvent', trackercat, 'click', trackervalue]);	
}

function track_ga_new(trackercat, tracktag)
{
	//category, action, opt_label, opt_value
	if (trackercat!="")
		_gaq.push(['_trackEvent', trackercat, 'click', tracking_page]);
		
	if (tracktag!="")
		_gaq.push(['_trackPageview', tracktag]);	
}

/*
==============================
_setCustomVar
Slots used:
==============================
1= Advertise
2= Listing
3= Smart web (form submit)
4= Join Cycladia (form submit)
5= Gender in NS (newsletter)
==============================
*/

function setga_var(slot, name, val, scope)
{
	_gaq.push(['_setCustomVar', slot, name, val, 1]);
	_gaq.push(['_trackEvent', name, 'Custom Var', tracking_page]);
	//_gaq.push(['_trackPageview']);
	
	/*
	Int      index       Required. The slot used for the custom variable. Possible values are 1-5, inclusive.
	String   name        Required. The name for the custom variable.
	String   value       Required. The value for the custom variable.
	Int      opt_scope   Optional. 1:visitor-level, 2: session-level, 3: page-level. 
	*/
}

/*
FB.Event.subscribe('edge.create', function(targetUrl) {
  _gaq.push(['_trackSocial', 'facebook', 'like', targetUrl]);
});

FB.Event.subscribe('edge.remove', function(targetUrl) {
  _gaq.push(['_trackSocial', 'facebook', 'unlike', targetUrl]);
});

FB.Event.subscribe('message.send', function(targetUrl) {
  _gaq.push(['_trackSocial', 'facebook', 'send', targetUrl]);
});*/

/* ========= FORM functions ========= */

function click_clear(id) 
{
	if (document.getElementById(id).value == document.getElementById(id).defaultValue)
		document.getElementById(id).value = "";
	else if (document.getElementById(id).value == "")
		document.getElementById(id).value = document.getElementById(id).defaultValue;
}

function click_clear_new(thisfield) 
{
	if (thisfield.value == thisfield.defaultValue)
		thisfield.value = "";
	else if (thisfield.value == "")
		thisfield.value = thisfield.defaultValue;
}

function show_error(id)
{
	$('#'+id).css("background", "#BDE4F7");
}

function show_error_club(id)
{
	$('#'+id).css("background", "#EFD667");
}


function clear_errors(formid)
{
	var form = document.getElementById(formid);
	
	for (i=0;i<form.elements.length;i++)
	{
		if ( (form.elements[i].type != "button") && (form.elements[i].type != "reset") )
			form.elements[i].style.background="url("+websiteURL+"images/input_tras_bg.png) repeat";
	}
}

function clear_errors_club(formid)
{
	var form = document.getElementById(formid);
	
	for (i=0;i<form.elements.length;i++)
	{
		if ( (form.elements[i].type != "button") && (form.elements[i].type != "reset") )
			form.elements[i].style.background="url("+websiteURL+"images/input_tras_bg.png) repeat";
	}
}


function checkEmail(elem)
{
   var field = document.getElementById(elem);
   var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-Z0-9]{2,4}$/;
   if (field.value.length ==0)
		return false;

   if(field.value.match(emailExp))
		return true;
   else 
		return false;   
}


function check_form(formid, mandatory_fields, numericfields, emailid)
{
	clear_errors(formid);
	
	//check mandatory fields
	var man = mandatory_fields.split(",");
	for (i=0;i<man.length;i++)
	{
		if ($('#'+man[i]).val()=="")
		{
			show_error(man[i]);
			alert ("You must fill all the mandatory fields (*)");
			return false;
		}
	}		

	if (emailid!="")
	{
		if (!checkEmail(emailid))
		{
			show_error(emailid);
			alert ("Your email address is not valid.");
			return false;
		}
	}
	
	//check numeric fields
	if (numericfields != "")
	{
		var numeric = numericfields.split(",");
		for (i=0;i<numeric.length;i++)
		{
			var nv = $('#'+numeric[i]).val().replace(/,/,".");
			$('#'+numeric[i]).val(nv);
			
			if (isNaN ($('#'+numeric[i]).val()))
			{
				show_error(numeric[i]);
				alert (numeric[i]+": should be a number. Plase check and try again!");
				return false;
			}
		}
	}
	
	if ( (document.getElementById('track_flag')) && (document.getElementById('track_flag_value')) )
	{
		var track_flag = $('#track_flag').val();	//document.getElementById('track_flag').value;
		var track_flag_value = $('#track_flag_value').val();	//document.getElementById('track_flag_value').value;
		
		_gaq.push(['_trackEvent', track_flag, 'submit', track_flag_value]);		
	}
	else if (document.getElementById('track_flag'))
	{
		var track_flag = $('#track_flag').val();	//document.getElementById('track_flag').value;
		_gaq.push(['_trackEvent', track_flag, 'submit', tracking_page]);
	}
	
	if (document.getElementById('temp_trackaction'))
	{
		if ($('#temp_trackaction').val()==1)
			setga_var(3, 'Smart web', 'submitted', 3);
		else if ($('#temp_trackaction').val()==2)
			setga_var(4, 'Join Cycladia', 'submitted', 3);
	}

	document.getElementById(formid).submit();
}



function check_form_prefilled_club(formid, mandatory_fields, prefilled_fields, numericfields, emailid, showerror)
{
	if (showerror==1)
		clear_errors_club(formid);		
		
	var man = mandatory_fields.split(",");
	for (i=0;i<man.length;i++)
	{	
		if ( (document.getElementById(man[i]).type) && (document.getElementById(man[i]).type == "select-one") )
		{
			if (document.getElementById(man[i]).selectedIndex == 0)
			{
				if (showerror==1)
					show_error_club(man[i]);
				alert ("You must fill all the mandatory fields (*)");
				return false;
			}
		}
		else
		{
			if (document.getElementById(man[i]).value == "")
			{
				if (showerror==1)
					show_error_club(man[i]);
				alert ("You must fill all the mandatory fields (*)");
				return false;
			}
		}
	}		

	//prefilled
	if (prefilled_fields!="")
		var pref_f = prefilled_fields.split(",");

	//check prefilled:
	if (prefilled_fields!="")
	{
		for (i=0;i<pref_f.length;i++)
		{
			//if (document.getElementById(pref_f[i]).value == pref_v[i])
			if (document.getElementById(pref_f[i]).value == document.getElementById(pref_f[i]).defaultValue)			
			{
				if (showerror==1)
					show_error_club(pref_f[i]);
				alert ("You must fill all the mandatory fields (*)");
				return false;
			}
		}
	}
	
	if (emailid!="")
	{
		if (!checkEmail(emailid))
		{
			if (showerror==1)
				show_error_club(emailid);
			alert ("Your email address is not valid.");
			return false;
		}
	}
	
	//check numeric fields
	if (numericfields != "")
	{
		var numeric = numericfields.split(",");
		for (i=0;i<numeric.length;i++)
		{
			document.getElementById(numeric[i]).value = document.getElementById(numeric[i]).value.replace(/,/,".");
			
			if (isNaN (document.getElementById(numeric[i]).value))
			{
				if (showerror==1)
					show_error_club(numeric[i]);
				alert (numeric[i]+": should be a number. Plase check and try again!");
				return false;
			}
		}
	}
	
	if (document.getElementById('track_flag'))
	{
		var track_flag = document.getElementById('track_flag').value;
		_gaq.push(['_trackEvent', track_flag, 'submit', tracking_page]);
		_gaq.push(['_trackPageview', track_flag]);
	}
	
	document.getElementById(formid).submit();
}

/* ========= ** ========= */

function guide_island_change(formid, islandselid)
{
	var islandselid = $("#"+islandselid).val();
	$ext = '';
	if (document.getElementById('srch_ext'))
		$ext =  $("#srch_ext").val();	//document.getElementById('srch_ext').value;
	
	document.getElementById(formid).action = document.getElementById(formid).action + islandselid + $ext;
	document.getElementById(formid).submit();
}


/* ========= ** ========= */

//var actions = ["", "stay.php", "bar-restaurant.php", "swim.php", "experience.php", "deals.php"];
var actions = ["", "best-hotels-stay-greece/", "best-restaurants-guide-greece/", "best-beaches-greece/", "experience-greece/", "greece-hotels-deals/"];

function load_search_divs()
{
	var sectionid = $("#srch_topsection").val();	
	var destinationid = $("#srch_topdest").val();
	/**/
	
	load_section_entries_search(sectionid, destinationid, 'div_sectionentries');

	/*ext = document.getElementById('srch_topdest').options[selIndex].text.toLowerCase()+"/";
	
	srch_ext_recom = '';
	if (document.getElementById('srch_ext_recom'))
		srch_ext_recom = document.getElementById('srch_ext_recom').value;*/
	
	document.getElementById('topsrchfrm').action = websiteURL+actions[sectionid];	//+srch_ext_recom+ext;
}

function srch_submit()
{	
	var ext = "";
	
	/*var destinationid = $("#srch_topdest").val();
	if (destinationid>0)
		ext = $("#srch_topdest :selected").text().toLowerCase()+"/";*/
	if ($("#submnusel_dest").val()!="")
		ext = $("#submnusel_dest").val().toLowerCase()+"/";
	
	if ($("#srch_key").val()!="Keyword")
		$(".search_ext").html('<input type="hidden" name="srch_key" value="'+$("#srch_key").val()+'" />');
		
	var sectionid = $("#submnusel_section").val();
	if (sectionid==0)
	{
		$(".label_click_submnu_sections").trigger('click');
		alert("Please select a section");
		return false;
	}
	
	srch_ext_recom = '';
	if (document.getElementById('srch_ext_recom'))
		srch_ext_recom = $("#srch_ext_recom").val();
	
	document.getElementById('topsrchfrm').action = websiteURL+actions[sectionid]+srch_ext_recom+ext;
	document.getElementById('topsrchfrm').submit();	
}

function srch_sec_submit()
{	
	var ext = "";
	
	var destinationid = $("#srchsec_dest").val();
	if (destinationid>0)
		ext = $.trim($("#srchsec_dest :selected").text()).toLowerCase()+"/";
	/*
	selIndex = document.getElementById('srch_topdest').selectedIndex;
	if (destinationid>0)
		ext = document.getElementById('srch_topdest').options[selIndex].text.toLowerCase()+"/";*/
		
	var sectionid = $("#srchsec_section").val();
	
	document.getElementById('secsrchfrm').action = websiteURL+actions[sectionid]+ext;
	document.getElementById('secsrchfrm').submit();	
}

function srchside_submit()
{	
	var query = '';
	var ext = "";
	var fields = '';
	
	var sectionid = $("#srchside_section").val();
	
	var destinationid = $("#srchside_dest").val();
	if (destinationid>0)
		ext = $.trim($("#srchside_dest :selected").text()).toLowerCase()+"/";

	var location_id = $("#srchside_loc").val();
	if (location_id>0)
		fields = fields + '<input type="hidden" name="srch_loc" id="sidesrch_loc" value="'+location_id+'" />';
	
	/*var price_range = '';
	$('input[id^="sidechk1_"]').each(function (i) {
		if ($(this).attr('checked'))
        	price_range = price_range +","+ $(this).val();
    });
	price_range = price_range.substr(1);*/

	var category = '';
	$('input[id^="sidechk2_"]').each(function (i) {
		if ($(this).attr('checked'))
        	category = category +","+ $(this).val();
    });
	category = category.substr(1);
	if (category!="")
		fields = fields + '<input type="hidden" name="srchs_cats" value="'+category+'" />';
	
	if (sectionid==1)
	{
		var style = '';
		$('input[id^="sidechk3_"]').each(function (i) {
			if ($(this).attr('checked'))
				style = style +","+ $(this).val();
		});
		style = style.substr(1);
		if (style!="")
			fields = fields + '<input type="hidden" name="srchs_styles" value="'+style+'" />';
	}
	else if (sectionid==2)
	{
		var style = $("#srchside_style").val();
		if (style!="" && style!="0")
			fields = fields + '<input type="hidden" name="srchs_styles" value="'+style+'" />';
	}
	
	var keyw = $("#srchside_key").val();
	if (keyw!="" && keyw!="Keyword:")
		fields = fields + '<input type="hidden" name="srch_key" value="'+keyw+'" />';
	
	var new_action = websiteURL+actions[sectionid]+ext;

	$("#sidesrch_fields").html(fields);	

	document.getElementById('sidesrchfrm').action = new_action;
	document.getElementById('sidesrchfrm').submit();
}

function load_locations_sidesrch()
{
	var dest_id = $("#srchside_dest").val();
	var section_id = $("#srchside_section").val();
	$.ajax({
		url: websiteURL+'ajax_load_side_locations.php',
		type: 'GET',
		dataType: 'html',
		data: 'dest_id='+dest_id+'&section_id='+section_id,
		cache: false,
		beforeSend: function(){
			$('.ajaxreply_sidesrch').html('<div style="text-align:center;"><img src="images/loading.gif" alt="loading" /></div>');
		},
		error: function(){
			$('.ajaxreply_sidesrch').html('Error triggered.');
		},
		success: function(reply) {
			$('.ajaxreply_sidesrch').html(reply);
		}
	});
}

function load_locations_secsrch()
{
	var dest_id = $("#srchsec_dest").val();
	var section_id = $("#srchsec_section").val();
	$.ajax({
		url: websiteURL+'ajax_load_srchsec_locations.php',
		type: 'GET',
		dataType: 'html',
		data: 'dest_id='+dest_id+'&section_id='+section_id,
		cache: false,
		beforeSend: function(){
			$('#ajax_seclocations').html('<div style="text-align:center;"><img src="images/loading.gif" alt="loading" /></div>');
		},
		error: function(){
			$('#ajax_seclocations').html('Error triggered.');
		},
		success: function(reply) {
			$('#ajax_seclocations').html(reply);
		}
	});
}


/**/

function load_recommendations(selectbox, divid) 
{	
	var dest_id = $("#"+selectbox).val();
	$.ajax({
		url: websiteURL+'ajax_load_recommendations.php?dest_id='+ encodeURIComponent(dest_id),
		type: 'GET',
		dataType: 'html',
		data: '',
		cache: false,
		beforeSend: function(){
			$('#'+divid).html('<div style="text-align:center;"><img src="images/loading.gif" alt="loading" /></div>');
		},
		error: function(){
			$('#'+divid).html('Error triggered.');
		},
		success: function(reply) {
			$('#'+divid).html(reply);
		}
	});
	
	var sel_dest = $.trim($("#recomhotels_dest option:selected").text()).toLowerCase(); 
	$("#morelink_rec").attr("href", websiteURL+"best-hotels-stay-greece/"+sel_dest+"/");
}

/**/

function load_estates(sectionfield, destinationfield, divid)
{	
	var dest_id = $("#"+destinationfield).val();
	var sectiont = $("#"+sectionfield).val();
	
	$.ajax({
		url: websiteURL+'ajax_load_estates.php?dest_id=' + encodeURIComponent(dest_id)+"&sectiont=" + encodeURIComponent(sectiont),
		type: 'GET',
		dataType: 'html',
		data: '',
		cache: false,
		beforeSend: function(){
			$('#'+divid).html('<div style="text-align:center;"><img src="images/loading.gif" alt="loading" /></div>');
		},
		error: function(){
			$('#'+divid).html('Error triggered.');
		},
		success: function(reply) {
			$('#'+divid).html(reply);
		}
	});
}

function load_section_entries_search(sectionid, destinationid, divid)
{	
	$.ajax({
		url: websiteURL+'ajax_section_entries_search.php?sectionid=' + encodeURIComponent(sectionid)+"&destinationid=" + encodeURIComponent(destinationid),
		type: 'GET',
		dataType: 'html',
		data: '',
		cache: false,
		beforeSend: function(){
			$('#'+divid).html('<div style="text-align:center;"><img src="images/loading.gif" alt="loading" /></div>');
		},
		error: function(){
			$('#'+divid).html('Error triggered.');
		},
		success: function(reply) {
			$('#'+divid).html(reply);
		}
	});
}

function load_sectionentries_contributions(selectbox) 
{	
	var sectionid = $("#"+selectbox).val();
	$.ajax({
		url: websiteURL+'ajax_sectionentries_contributions.php?sectionid=' + encodeURIComponent(sectionid),
		type: 'GET',
		dataType: 'html',
		data: '',
		cache: false,
		beforeSend: function(){
			$('#ajax_reply').html('<div style="text-align:center;"><img src="images/loading.gif" alt="loading" /></div>');
		},
		error: function(){
			$('#ajax_reply').html('Error triggered.');
		},
		success: function(reply) {
			$('#ajax_reply').html(reply);
		}
	});
}

function load_map_section(sectionid, id)
{
	var divid = 'ajax_reply_map';
	$("#gallerydivholder, #ajax_reply_maptop").css("display", "none");
	$(".close_mapdiv").css("display", "block");
	$("#maplink").html('<span class="linklike" onclick="load_gallery('+sectionid+', '+id+');">View image gallery</span>');
	
	$.ajax({
		url: websiteURL+'ajax_sectiontable_map.php?sectionid=' + encodeURIComponent(sectionid)+"&id=" + encodeURIComponent(id),
		type: 'GET',
		dataType: 'html',
		data: '',
		cache: false,
		beforeSend: function(){
			$('#'+divid).html('<div style="text-align:center;"><img src="images/loading.gif" alt="loading" /></div>');
		},
		error: function(){
			$('#'+divid).html('Error triggered.');
		},
		success: function(reply) {
			$('#'+divid).html(reply);
		}
	});
}

function load_gallery(sectionid, id)
{
	$("#ajax_reply_map, .close_mapdiv").css("display", "none");
	$("#gallerydivholder").css("display", "block");
	
	$("#maplink").html('<span class="linklike" onclick="load_map_section('+sectionid+', '+id+');">View map</span>');	
}

function btn_load_map_section(sectionid, id)
{
	var divid = 'ajax_reply_map';
	$(".slide_div_inner, .slide_legend, .slide_banner, #maplink, #ajax_reply_maptop").css("display", "none");
	$("#gallerylink").css({display: 'block', paddingTop: '6px', height:"20px"});
	$('#'+divid+", .close_mapdiv").css("display", "block");

	if ($("#divid").html()=="" || $("#divid").html()==null)
	{
		$.ajax({
			url: websiteURL+'ajax_sectiontable_map.php?sectionid=' + encodeURIComponent(sectionid)+"&id=" + encodeURIComponent(id),
			type: 'GET',
			dataType: 'html',
			data: '',
			cache: false,
			beforeSend: function(){
				$('#'+divid).html('<div style="text-align:center;"><img src="images/loading.gif" alt="loading" /></div>');
			},
			error: function(){
				$('#'+divid).html('Error triggered.');
			},
			success: function(reply) {
				$('#'+divid).html(reply);
			}
		});
	}
}
function btn_load_gallery(sectionid, id)
{
	$("#ajax_reply_map, #gallerylink, #ajax_reply_maptop, .close_mapdiv").css("display", "none");
	$(".slide_div_inner, .slide_legend, .slide_banner, #maplink").css("display", "block");	
}

function load_topmap_home(selectbox_dest, mapid, sectionid)
{
	$(".slide_div_inner, .slide_legend, .slide_banner").css("display", "none");
	$('#ajax_reply_maptop, .close_mapdiv, #ajax_reply_map').css("display", "block");
	
	var destid = $("#map_dest_id").val();
	sectionid = $("#submnusel_section").val();

	initialize();
	if (destid>0 && sectionid>0)
		showmap_island(destid, sectionid);
		
	$.scrollTo({top:'0px', left:'0px'}, 500 );	
}

function load_topmap(selectbox_dest, mapid, sectionid)
{
	$(".slide_div_inner, .slide_legend, .slide_banner").css("display", "none");
	$('#ajax_reply_maptop, .close_mapdiv, #ajax_reply_map').css("display", "block");
	
	var destid = $("#srchsec_dest").val();
	
	initialize();
	if (destid>0 && sectionid>0)
		showmap_island(destid, sectionid);
		
	$.scrollTo({top:'0px', left:'0px'}, 500 );
	/*
	//var sectionid = $("#srch_topsection").val();
	
	$(".slide_div_inner, .slide_legend, .slide_banner, #ajax_reply_map").css("display", "none");
	$('#ajax_reply_maptop, .close_mapdiv').css("display", "block");
	
	$.ajax({
		url: websiteURL+'ajax_maptop.php',
		type: 'GET',
		dataType: 'html',
		data: '&destid='+destid+'&mapid='+mapid+'&sectionid='+sectionid,
		cache: false,
		beforeSend: function(){
			//$('#ajax_reply_maptop').html('<div style="text-align:center;"><img src="images/loading.gif" alt="loading" /></div>');
		},
		error: function(){
			$('#ajax_reply_maptop').html('Error triggered.');
		},
		success: function(reply) {
			$('#ajax_reply_maptop').html(reply);
		}
	});*/	
}

function loadmapapi_section(id, title, lan, lon, table)
{
	$(".slide_div_inner, .slide_legend, .slide_banner").css("display", "none");
	$('#ajax_reply_maptop, .close_mapdiv, #ajax_reply_map').css("display", "block");
	
	initialize();
	
	for (i in markers_array)
		markers_array[i].setMap(null);
		   
	var dot = "'"+title+"', "+lan+", "+lon+", 1"+id;
	setMarkers_table(dot, table);
	
	$.scrollTo({top:'0px', left:'0px'}, 500 );
}


function sidesrch_keypressed(e)
{
	var numCharCode;
   
   // get event if not passed
   if (!e) var e = window.event;

   // get character code of key pressed
   if (e.keyCode) 
   		numCharCode = e.keyCode;
   else if (e.which) 
   		numCharCode = e.which;
		
	//if (e.preventDefault)
		//e.preventDefault();

   if ( numCharCode == 13 )
   		srchside_submit();
}
