function getXmlTagValue(tag) {
    if (false == tag.hasChildNodes()) {
        return '';
    }
    return tag.firstChild.nodeValue;
}

function getXmlAttributeValue(tag, attribute_name) {
    return tag.getAttribute(attribute_name);
}





function nowInChat(url)
{
    var options =
    {
        method: 'post',
        postBody: '',

        onSuccess: function(t)
        {
            var query_xml = t.responseXML.documentElement;

            var result = getXmlTagValue(query_xml.getElementsByTagName('result')[0]);
            var message = getXmlTagValue(query_xml.getElementsByTagName('message')[0]);

            switch(result)
            {
                case 'ERROR':
                    $('container-now-in-chat').innerHTML = '';
                    break;

                case 'OK':
                    $('container-now-in-chat').innerHTML = '(' + message + ' online)';
                    break;
            }
        },

        onFailure: function(t)
        {
            alert('Error ' + t.status + ' (' + t.statusText + ')');
        }
    }

    new Ajax.Request(url, options);
}






function loadCityOptions(url, country, lng, selectbox)
{
    var options =
    {
        method: 'post',
        postBody:  'country=' + country + '&lng=' + lng,
        onSuccess: function(t)
        {
            var query_xml = t.responseXML.documentElement;

            var result = getXmlTagValue(query_xml.getElementsByTagName('result')[0]);
    		selectbox.options.length = 0;

            switch(result)
            {
                case 'ERROR':
                    break;

                case 'OK':
		selectbox.options.length = 0;
		var i = 0;
		for (i=0;i<query_xml.getElementsByTagName('option').length;i++){
		selectbox.options[i] = new Option(getXmlTagValue(query_xml.getElementsByTagName('option')[i]),getXmlAttributeValue(query_xml.getElementsByTagName('option')[i],'value'));
		}
                    break;
            }
        },

        onFailure: function(t)
        {
            alert('Error ' + t.status + ' (' + t.statusText + ')');
        }
    }
    new Ajax.Request(url, options);
}





function loadNearestCities(url, add_from, add_cond, type, available_for, user_id, lng)
{
    var options =
    {
        method: 'post',
        postBody: 'add_from=' + add_from + '&add_cond=' + add_cond + '&type=' + type + '&available_for=' + available_for + '&user_id=' + user_id + '&lng=' + lng,

        onSuccess: function(t)
        {
            var query_xml = t.responseXML.documentElement;

            var result = getXmlTagValue(query_xml.getElementsByTagName('result')[0]);
            var message = getXmlTagValue(query_xml.getElementsByTagName('message')[0]);

            switch(result)
            {
                case 'ERROR':
                    $('container_nearest_cities').innerHTML = '<span class=\"error\">' + message + '</span>';
                    break;

                case 'OK':
                    $('container_nearest_cities').innerHTML = message;
                    break;
            }
        },

        onFailure: function(t)
        {
            alert('Error ' + t.status + ' (' + t.statusText + ')');
        }
    }

    new Ajax.Request(url, options);
}





function showSplash()
{
    if (null == ReadCookie('splash'))
    {
        showContainer('splash');
        hideContainer('page');
    }
}
    
function hideSplash()
{
    hideContainer('splash');
    showContainer('page');
    SetCookie('splash', 'seen', 1);
}





function showContainer(container_id)
{
    var element = document.getElementById(container_id);

    switch (element.tagName)
    {
        case 'SPAN':
            var display = 'inline';
            break;

        case 'DIV':
        default:
            var display = 'block';
    }

    element.style.display = display;
}

function hideContainer(container_id)
{
    document.getElementById(container_id).style.display = 'none';
}





function showChat(url)
{
    window.open(url, 'chat', 'menubar=no, resizable=yes, status=no, scrollbars=no, toolbar=no, width=900, height=800');
}

function showTermsAndConditions(url)
{
    window.open(url, 'showTerms', 'menubar=no, resizable=yes, status=no, scrollbars=yes, toolbar=no, width=640, height=480');
}





function GenerateListURL(path, type, city, available_for, page, user_key)
{
    if (type) path += '/' + type;
    if (city) path += '/' + city;
    if (available_for) path += '/' + available_for;
    if (page > 0) path += '/' + page;
    if (user_key) path += '/' + user_key + '.html';

    return path;
}

function RedirectListURL(path, type, city, available_for, page, user_key)
{
    path = GenerateListURL(path, type, city, available_for, page, user_key);
    document.location.replace(path);
}





function SimulateRadioWithCheckbox(checkboxes, value) {
    for (var i = 0; i < checkboxes.length; i++) {
        checkboxes[i].checked = (value == checkboxes[i].value)? true : false;
    }
}

function GetCheckboxValues(checkboxes) {
    var result = "";
    for (var i = 0; i < checkboxes.length; i++) {
        if(checkboxes[i].checked) {
            if ("" != result) {
                result += "+";
            }
            result += checkboxes[i].value;
        }
    }
    return result;
}

function GetCheckboxValuePart(checkboxes, index) {
    var value = GetCheckboxValues(checkboxes);
    var values = value.split("|");
    return ('null' == values[index])? null : values[index];
}





function openWindow(url, name, width, height)
{
 return window.open(url, name, 'width=' + width + ', height=' + height + ', resizable=no, scrollbars=yes, toolbar=no, location=no, directories=no, status=no, menubar=no, copyhistory=no');
}

function selectOptionsByValue(obj, value)
{
    for (var i = 0; i < obj.options.length; i++)
    {
        if (value == obj.options[i].value)
        {
            obj.options[i].selected = true;

            // call onchange event if defined
            if (obj.onchange)
            {
                obj.onchange();
            }
        }
    }
}

function selectUnselectMatchingOptions(obj, regex, which, only)
{
 if (window.RegExp)
 {
  if ("select" == which)
  {
   var selected1 = true;
   var selected2 = false;
  }
  else if ("unselect" == which)
  {
   var selected1 = false;
   var selected2 = true;
  }
  else
  {
   return;
  }
  var re = new RegExp(regex);
  for (var i = 0; i < obj.options.length; i++)
  {
   if (re.test(obj.options[i].text))
   {
    obj.options[i].selected = selected1;
    }
    else
    {
     if (true == only)
    {
     obj.options[i].selected = selected2;
    }
   }
  }
 }
}

function selectMatchingOptions(obj, regex)
{
 selectUnselectMatchingOptions(obj, regex, "select", false);
}

function selectOnlyMatchingOptions(obj, regex)
{
 selectUnselectMatchingOptions(obj, regex, "select", true);
}

function unSelectMatchingOptions(obj, regex)
{
 selectUnselectMatchingOptions(obj, regex, "unselect", false);
}

// how many options should be skipped when sorting select
// (usefull for example when ALL option is at begining)
var iSortSkip = 0;
function sortSelect(obj)
{
 if (1 < arguments.length)
 {
  iSortSkip = arguments[1];
 }
 var o = new Array();
 var selectedText = null;
 if (null == obj.options)
 {
  return;
 }
 for (var i = iSortSkip; i < obj.options.length; i++)
 {
  if (obj.options[i].selected)
  {
   selectedText = obj.options[i].text;
  }
  o[o.length] = new Option(obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected);
 }

 if (0 == o.length)
 {
  return;
 }
 o = o.sort(
  function(a,b)
  {
   if ((a.text + "") < (b.text + ""))
   {
    return -1;
   }
   if ((a.text + "") > (b.text + ""))
   {
    return 1;
   }
   return 0;
  }
 );
 for (var i = 0; i < o.length; i++)
 {
  obj.options[iSortSkip + i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
 }
 if (null != selectedText)
 {
  selectOnlyMatchingOptions(obj, "^" + selectedText + "$");
 }
 iSortSkip = 0;
}

function selectAllOptions(obj)
{
 for (var i = 0; i < obj.options.length; i++)
 {
  obj.options[i].selected = true;
 }
}

function moveSelectedOptions(from, to)
{
 if (3 < arguments.length)
 {
  var regex = arguments[3];
  if (regex != "")
  {
   unSelectMatchingOptions(from, regex);
  }
 }
 for (var i = 0; i < from.options.length; i++)
 {
  var o = from.options[i];
  if (o.selected)
  {
   to.options[to.options.length] = new Option(o.text, o.value, false, false);
  }
 }
 for (var i = (from.options.length - 1); i >= 0; i--)
 {
  var o = from.options[i];
  if (o.selected)
  {
   from.options[i] = null;
  }
 }
 if ((3 > arguments.length) || (true == arguments[2]))
 {
  sortSelect(from);
  sortSelect(to);
 }
 from.selectedIndex = -1;
 to.selectedIndex = -1;
}

function copySelectedOptions(from, to)
{
 var options = new Object();
 for (var i = 0; i < to.options.length; i++)
 {
  options[to.options[i].value] = to.options[i].text;
 }
 for (var i = 0; i < from.options.length; i++)
 {
  var o = from.options[i];
  if (o.selected)
  {
   if (options[o.value] == null || options[o.value] == "undefined" || options[o.value]!=o.text)
   {
    to.options[to.options.length] = new Option(o.text, o.value, false, false);
   }
  }
 }
 if ((3 > arguments.length) || (true == arguments[2]))
 {
  sortSelect(to);
 }
 from.selectedIndex = -1;
 to.selectedIndex = -1;
}

function moveAllOptions(from,to) {
 selectAllOptions(from);
 if (arguments.length==2) {
  moveSelectedOptions(from,to);
 }
 else if (arguments.length==3) {
  moveSelectedOptions(from,to,arguments[2]);
 }
 else if (arguments.length==4) {
  moveSelectedOptions(from,to,arguments[2],arguments[3]);
 }
}

function copyAllOptions(from, to)
{
 selectAllOptions(from);
 if (2 == arguments.length)
 {
  copySelectedOptions(from, to);
 }
 else if (3 == arguments.length)
 {
  copySelectedOptions(from, to, arguments[2]);
 }
}

function swapOptions(obj, i, j)
{
 var o = obj.options;
 var i_selected = o[i].selected;
 var j_selected = o[j].selected;
 var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
 var temp2 = new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
 o[i] = temp2;
 o[j] = temp;
 o[i].selected = j_selected;
 o[j].selected = i_selected;
}

function moveOptionUp(obj)
{
 for (i = 0; i < obj.options.length; i++)
 {
  if (obj.options[i].selected)
  {
   if (i != 0 && !obj.options[i - 1].selected)
   {
    swapOptions(obj, i, i - 1);
    obj.options[i - 1].selected = true;
   }
  }
 }
}

function moveOptionDown(obj)
{
 for (i = obj.options.length - 1; i >= 0; i--)
 {
  if (obj.options[i].selected)
  {
   if (i != (obj.options.length - 1) && ! obj.options[i + 1].selected)
   {
    swapOptions(obj, i, i + 1);
    obj.options[i + 1].selected = true;
   }
  }
 }
}

function removeSelectedOptions(from)
{
 for (var i = (from.options.length - 1); i >= 0; i--)
 {
  var o = from.options[i];
  if (o.selected)
  {
   from.options[i] = null;
  }
 }
 from.selectedIndex = -1;
}

function removeAllOptions(from)
{
 for (var i = (from.options.length - 1); i >= 0; i--)
 {
  from.options[i] = null;
 }
 from.selectedIndex = -1;
}

function addOption(obj, text, value, selected)
{
 if (null != obj && null != obj.options)
 {
  obj.options[obj.options.length] = new Option(text, value, false, selected);
 }
}

function exportSelect(obj)
{
 var string = "";
 if (null != obj && null != obj.options)
 {
  var o = obj.options;
  for (var i = 0; i < o.length; i++)
  {
   string += o[i].value + ",";
  }
  if (-1 != string.indexOf(","))
  {
   string = string.substring(0, string.length - 1);
  }
 }
 return string;
}

function inArray(array, val)
{
    for (var i in array)
    {
        if (val == array[i])
        {
            return true;
        }
    }

    return false;
}

function sortArrayByIndex(array, sorting)
{
 var counter = 0;
 var aIndex = Array();
 var aSorted = new Object();
 for (var i in array)
 {
  aIndex[counter++] = i;
 }
 if ('lexicographical' == sorting)
 {
  aIndex.sort(
  function(a,b)
  {
   if (a < b)
   {
    return -1;
   }
   if (a > b)
   {
    return 1;
   }
   return 0;
  }
  );
 }
 else
 {
  aIndex.sort(
  function(a,b)
  {
   if (parseInt(a) < parseInt(b))
   {
    return -1;
   }
   if (parseInt(a) > parseInt(b))
   {
    return 1;
   }
   return 0;
  }
  );
 }
 for (var i = 0; i < aIndex.length; i++)
 {
  aSorted[aIndex[i]] = array[aIndex[i]];
 }
 return aSorted;
}

function showState(country_iso, usa_iso)
{
 element = document.getElementById("state");
 if (country_iso == usa_iso)
 {
  element.style.visibility = "visible";
 }
 else
 {
  element.style.visibility = "hidden";
 }
}

function redirect_city(c, reviews_prefix, city_prefix)
{
	
   if(document.location.href.indexOf("/en/") == -1)
   {
   		path = '/' + reviews_prefix;
   }
   else
   {
   		path = '/' + reviews_prefix + '/en'
   }

   if(c!='')
   path = path+'/' + city_prefix + '/'+c;
   
   if ('' != '')
   path = path+'/'+'.html';
   document.location.replace(path);
}


function click(n)
{
    if (document.images)
    {
        (new Image()).src = "/redirect.php?" + n;
    }
    return true;
}





function SetCookie(cookieName, cookieValue)
{
    document.cookie = cookieName + "=" + escape(cookieValue) + ";path=/";
}

function SetCookieDays(cookieName, cookieValue, nDays)
{
    var today = new Date();
    var expire = new Date();
    if (nDays==null || nDays==0) nDays=1;
    expire.setTime(today.getTime() + 3600000*24*nDays);
    document.cookie = cookieName + "=" + escape(cookieValue) + ";path=/;expires=" + expire.toGMTString();
}

function ReadCookie(cookieName)
{
	var nameEQ = cookieName + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function submitenter(e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   	 process("frmForm");
     return false;
   }
else
   return true;
}





function RedirectDirectory(url, country, region, available) {
    url = url.replace(/%COUNTRY%/, country);
    url = url.replace(/%REGION%/, region);
    url = url.replace(/%AVAILABLE%/, available);
    document.location.replace(url);
}
