/* raflaopas.fi js funcs */

// Function makes a querystring for new page load
// (For example when user wants to move to another
// main page). We want to remember the selected towns
// and restaurants!
// @param string page, main page where we are redirected
// @return void
function changePage(page, query)
{
	var i = 0;
	
	var town_string = '&kaupunki=';
	town_string += makeQueryPart(selected_towns);
	var group_string = '&rav=';
	group_string += makeQueryPart(selected_restaurant_groups);
	
	var event_string = '&tapahtumat=';
	event_string += makeQueryPart(selected_event_categories);
		
	if(query != 0)
		window.location = 'index.php?sivu=' + page + town_string + group_string + event_string + '&' + query;
	else
		window.location = 'index.php?sivu=' + page + town_string + group_string + event_string;
		
	return false;
}


/*
 * Modify array that it goes to querystring
 * array = value1, value2, => value1+value2
 * @param array arr array of strings
 * @return string in format value1+value2+ ... +valueN
 */
function makeQueryPart(arr)
{
	var ret_string ='';
	var i = 0;
	for(i = 0; i < arr.length; i++)
	{
		if(i==0)
			ret_string += arr[i];
		else
			ret_string += '+' + arr[i];
	}
	return ret_string;
}

/* returns array of spesific query string parameters */
// For exampe, if query is index.php?kaupunki=Kouvola+Lahti,
// and part = 'kaupunki',
// then this function will return following array: ('Kouvola', 'Lahti')
function getQueryPart(part)
{
	// store values in this array
	var ret_array = new Array;
	
	// get part after ?-sign
	var q = window.location.search.substring(1);
	
	// Convert special characters back to normal presentation
	q = q.replace(/%E4/g, "ä");
	q = q.replace(/%C4/g, "Ä");
	q = q.replace(/%F6/g, "ö");
	q = q.replace(/%D6/g, "Ö");
	q = q.replace(/%E5/g, "å");
	q = q.replace(/%C5/g, "Å");

	//alert(q);
	
	
	// split
	var parts = q.split("&");
	
	var var_name = 0;
	var i;
	for(i=0; i<parts.length; i++)
	{
		var_name = parts[i].split("=");
		
		if(var_name[0] == part)
		{
			// We got our variable, now split it
			ret_array = var_name[1].split("+")

			return ret_array;
		}
	}
	return false;
}

// Update array, that has 'selected' values inside (selected categories, groups, towns..)
// If 'Kaikki' is selected, then remove all and add only 'Kaikki'
// If array goes empty, add 'Kaikki'
// @param array arr : that we are updating
// @param string group : value that we are adding / or removing from array 
// @return updated selected array
function updateSelectedArray(arr, group)
{
		var ind = 0;
		var ind_all = 0;
		// Is group already selected?
		if((ind = hasValueInside(group, arr)) == -1)
		{
			/*
			 * 2008-08-08: for now on, only one town can be
			 * selected at the time. So empty the array first.
			 */
			if(arr == selected_towns)
				arr = []
			
			// Not, push to array
			arr.push(group);	
		}
		else
		{
			// Yes, remove from array
			// 2008-10-30: keep town selected, list 
			// town's restaurants if we are not already
			// in the list
			if (arr != selected_towns)
				arr.splice(ind, 1);
		}
		
		if(group == 'Kaikki')
		{
			// remove all selected groups from array
			arr = [];
			
			// ...and add 'Kaikki'
			arr.push(group);	
		}
		// If clicked group was something else than 'Kaikki' and 'Kaikki'
		// is still in array, remove it from there
		else
		{
			if((ind_all = hasValueInside('Kaikki', arr)) != -1)
			{
				arr.splice(ind_all, 1);
			}
		}
		
		// If our selected-array is empty, add 'Kaikki'
		if(arr.length == 0)
			arr.push('Kaikki');
		
		
		return arr;	
}


/*
 * Function checks if array/object is in array
 * It's kind of own implementation for js's indexOf-method that
 * should work with any kind of arrays (indexOf works only with
 * strings)
 * Return value: index number if needle was found, -1 if not found
 */
function hasValueInside(oNeedle,oHaystack)
{
	for( var i = 0; i < oHaystack.length; i++ )
	{
    	if( oNeedle == oHaystack[i] )
    	{
    		return i;
    	}
	}
	return -1;
}

function checkIfSelected(value, selected_value, selected_array)
{
	if(hasValueInside(value, selected_array) != -1)
	{
		if(selected_value == 1)
			return true;
		else	
			return false;
	}
}
		
function getExtraInfoCheckBoxes()
{
	var ret = '<div id="lisatiedot" style="background-color: #f2f2f2; padding: 5px;margin-bottom: 20px;"><table>';
	//var ret = '';
	//ret += '<select name="lisatiedot">';
	
	for(i=1; i < all_extra_infos.length; i++)
	{ 
		ret += '<td><input type="checkbox" name="lisatiedot[]" value="' + all_extra_infos[i] + '" onclick="return updateLists(0, 0, 0, \'' + all_extra_infos[i] + '\')"';
		
		if((hasValueInside(all_extra_infos[i], selected_extra_infos) != -1))
			ret += 'checked="checked" ';
		
		ret += '/></td><td>'  + all_extra_infos_names[i] + '</td>';
		
		if(i%2 ==0)
			ret +=  '</tr>';
	}   	   		
    
    // Hakukentta
    
	document.getElementById('piilota_linkki_div').innerHTML = '<br /><a href="javascript:;" onclick="return updateLists(0, 0, 0, \'' + all_extra_infos[0] + '\')" style="margin-left:10px;">Piilota</a><br /><br /></div>';
	ret += '</table></div>';
	
	
	//alert(ret);

	
		return ret;
}

function getSearchField()
{
	var ret = '';
	 // Hakukentta
      //ret += '<span style="margin-left: 10px; margin-top:0; padding-top:0;"> Hae ravintolan nimellä: <input id="hakukentta" type="text" size="20" maxlength="15" value="' + search + '" onkeyup="ajax_search(event, this.value)" /> <span id="search_loader"></span></span>';
      
       ret += '<table><tr><td style="padding-left: 10px; height: 20px;">Hae ravintolan nimellä: <input id="hakukentta" type="text" size="20" maxlength="15" value="' + search + '" onkeyup="ajax_search(event, this.value)" /></td><td> <span id="search_loader"></span></td></tr></table>';

	return ret;
}
		
function printExtraInfoCheckBoxes()
{	
	show_extra_info = true;
	var ret = getExtraInfoCheckBoxes();
	
	if(document.getElementById("lisatiedot") != null)
	{
		document.getElementById("lisatiedot").innerHTML = ret;
	}
	
	if(document.getElementById("lisatiedot_linkki") != null)
	{
		document.getElementById("lisatiedot_linkki").innerHTML = '';
	}
	
	/*
	 2008-08-08 poistettu hakukenttä tästä 
	if(document.getElementById("hakukentta_div") != null)
	{
		document.getElementById("hakukentta_div").innerHTML = getSearchField();
	}
	*/
}	

function StringCat()
{
    var sp;
    var ep;
    var l=0;
    
    this.push=function(what)
    {
        if(typeof(sp)=='undefined')
        {
            ep=new Array();
            sp=ep;
        }
        else
        {
            var oep=ep;
            ep=new Array();
            oep[1]=ep;
        }
        ep[0]=what;
        ++l;
    }
    
    this.toString=function()
    {
        if(l==0)return ;

        while(l>1)
        {
            var ptr=sp;
            var nsp=new Array();
            var nep=nsp;
            var nl=0;
            
            while(typeof(ptr)!='undefined')
            {
                if(typeof(nep[0])=='undefined')
                {
                    nep[0]=ptr[0];
                    ++nl;
                }
                else
                {
                    if(typeof(ptr[0])!='undefined')nep[0]+=ptr[0];
                    nep[1]=new Array();
                    nep=nep[1];
                }
                ptr=ptr[1];
            }
            sp=nsp;
            ep=nep;
            l=nl;
        }
        return sp[0];
    }    
}

function convert_chars(q)
{
	q = q.replace(/ä/g, "%E4");
	q = q.replace(/Ä/g, "%C4");
	q = q.replace(/ö/g, "%F6");
	q = q.replace(/Ö/g, "%D6");
	q = q.replace(/å/g, "%E5");
	q = q.replace(/Å/g, "%C5");
	
	return q;
}

			
				