function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? ";   path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function DeleteCookie (name,path,domain)
{
	document.cookie = name + "=" +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

function addr(city, address, phone, fax, id, skype, email) {
	this.city = city;
	this.address = address;
	this.phone = phone;
	this.fax = fax;
	this.id = id;
	this.skype = skype;
	this.email = email;
}



function myChange()
{
	var oListbox = document.getElementById('city_select');
	var iCityId = oListbox.options[oListbox.selectedIndex].value;
	document.getElementById('AddresText').innerHTML = myWriteAddress(iCityId);


	var expdate = new Date();
	var monthFromNow = expdate . getTime () + (30*24*60*60*1000);
	expdate.setTime(monthFromNow);
	setCookie('city_id', iCityId, expdate, '/');
}




function myWriteAddress(iCityId)
{
	var oAddr = data[iCityId];
	var html = '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
	html += '<tr valign=top><td colspan="2">' + oAddr.address + '</td></tr>';
	if (oAddr.phone) {html += '<tr valign=top><td>Телефон:</td><td>' + oAddr.phone + '</td></tr>'; }
	if (oAddr.fax) {html += '<tr valign=top><td>Факс:</td><td>' + oAddr.fax + '</td></tr>';		}
	if (oAddr.address) {html += '<tr valign=top><td colspan="2"><a href=/contact/view/' + oAddr.id + ' id="Address" target=_blank>схема проезда</a></td></tr>';}
	
	var dop = '';
	if (oAddr.skype != '') {dop = '<a href="skype:' + oAddr.skype + '?call" ><img src="/img/skype.png" style="border: medium none; vertical-align: middle;">&nbsp;<strong>SKYPE</strong></a>';}
	if (oAddr.skype != '' && oAddr.sip != '') {dop += '&nbsp;&nbsp;';}
	if (oAddr.email != '') {dop += ' <a href="mailto:' + oAddr.email + '" >E-mail: ' + oAddr.email + '</a>';}
	
	if (dop) {html += '<tr><td colspan="2">' + dop + '</td></tr>';}

	html += '</table>';

	return html;  
}


function load_options()
{
	var oListbox = document.getElementById("city_select");
	for (i = 0; i < data.length; i++)
	{
		addr = data[i];
		oListbox.options[i] = new Option(addr.city, i, false, false);
	}

	var iCityId = getCookie('city_id');
	if (iCityId == null || iCityId == 0)
	{
		if (contact_by_ip > 0) {
			for (i = 0; i < data.length; i++)
			{
				addr = data[i];
				if (addr.id == contact_by_ip) {
					iCityId = i;
				}
			}			
		} else {
			iCityId = 1;
		}
	}

	oListbox.options[iCityId].selected = true;
	myChange();

}