function PopUpWindow(mypage, myname, w, h, scroll, resizeable) {
  var winl = (screen.width-w)/2;
  var wint = (screen.height-h)/2;
  var settings  ='height='+h+',';
      settings +='width='+w+',';
      settings +='top='+wint+',';
      settings +='left='+winl+',';
      settings +='scrollbars='+scroll+',';
      settings +='resizable='+resizeable+',';
  win = window.open(mypage, myname, settings);
  if (win.opener == null) {  // for Nav 2.0x
      win.opener = self  // this creates and sets a new property
   }
  if(parseInt(navigator.appVersion) >= 4){win.window.focus();}
}

//XMLHTTP object ;)
function getHTTPObject() {
  if(window.XMLHttpRequest){
    xmlhttp = new XMLHttpRequest();
  }
  else if (window.ActiveXObject){
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    if (!xmlhttp){
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }

  }
  return xmlhttp;
}

function toUri(path, elm) {
	var uri = elm.options[elm.selectedIndex].value;
	if (uri > 0) {
		document.location.href = path+uri;
	}
}

function getCities(elm, num) {
	var car  = elm.options[elm.selectedIndex].value;
	var box  = document.getElementById('town'+num);
	if (car > 0 && num > 0) {
		var http = getHTTPObject();
		http.open("GET", "/getcities.php?id=" + escape(car), true);
		http.onreadystatechange = function() {
			if ( http.readyState == 4 ) {
				if (http.status == 200) {
					var xml     = http.responseXML;
					var results = xml.getElementsByTagName("x")[0].firstChild.nodeValue;
					box.innerHTML = results;
				}
				else
					box.innerHTML = 'Услугате не е налична за момента.';
			}
		}
		http.send(null);
	}
}

function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit) field.value = field.value.substring(0, maxlimit);
	else countfield.value = maxlimit - field.value.length;
}

function setFilter(cat) {
	var id1 = document.getElementById('state');
	var id2 = document.getElementById('town');
	
	var country  = id1.options[id1.selectedIndex].value;
	var town  = id2.options[id2.selectedIndex].value;
	
	alert(cat);
	alert(country);
	alert(town);
	alert("/setfilter.php?type=" + escape(cat) + "&country="+escape(country)+"&town="+escape(town));
	if (country > 0) {
		var http = getHTTPObject();
		http.open("GET", "/setfilter.php?type=" + escape(cat) + "&country="+escape(country)+"&town="+escape(town), true);
		http.onreadystatechange = function() {
			if ( http.readyState == 4 ) {
				if (http.status == 200) {
					
					var xml     = http.responseXML;
					var results = xml.getElementsByTagName("x")[0].firstChild.nodeValue;
					alert('bla');
					alert(xml);
	        		alert(results);
					return;
				}
				else
					box.innerHTML = 'Услугате не е налична за момента.';
			}
		}
		http.send(null);
	}

}

function select_set() {

	var model = document.getElementById("town");
	var submenu = document.getElementById("state");
	var index = submenu.options[submenu.selectedIndex].value;

	model.options.length=0;
	model.options[0] = new Option("Изберете град","", true );

	for(var i=0; i<towns[index].length; i=i+2) {    	 
		model.options[model.options.length] = new Option(towns[index][i+1],towns[index][i]);    	 
    }
    //model.options[model.options.length] = new Option("","");

}


function select_init() {
	
	var menu_from = document.getElementById("state");
	menu_from.options.length=0;
	
	var count=0;
	for(i in states)
    { 
    	
    	if( i == 0 ) { 
			menu_from.options[menu_from.options.length] = new Option(states[i],i,true); 
			menu_from.selectedIndex = count;}
    	else {
			menu_from.options[menu_from.options.length] = new Option(states[i],i);
		}
    	count++;
    } 	
}

function MM_openBrWindow(theURL,winName,features) { //v1.2
  window.open(theURL,winName,features);
}

function MM_ImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImgRestore(e, color) { //v3.0
	if (document.getElementById('tabChoice').value == e) return;
	document.getElementById('Image'+e).src = '/images/'+color+'/search/inactive'+e+'.jpg';
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function statesInit() {
	$('#dropDowns').css('display','block');
	
	/*form.innerHTML = '<label for="state"> Държава: </label><br clear="all"/>'+
				'<input type="text" name="country_name" id="suggest66" class="mailform" style="width:140px;"/>'+
				'<br clear="all"/>'+
				'<label for="state"> Град: </label><br/>'+								
				'<input type="text" name="town_name" id="suggest1" class="mailform" style="width:140px;" />';
	*/
	//select_init();
}

function selectTab(e,color,extra) {	
	var extra = extra ? extra : '';
	var image = 'Image'+e;
	var act = document.getElementById('tabChoice').value;
	document.getElementById('tabChoice').value = e;	
	document.getElementById('Image'+act).src = '/images/'+color+'/search/inactive'+act+'.jpg';
	//document.getElementById('link'+act).onmouseout='MM_swapImgRestore()';
	document.getElementById(image).src = '/images/'+color+'/search/active'+e+'.jpg';
	document.getElementById(image).focus = null;
	StatesField = document.getElementById('dropDowns');
	statesInit();

	SelectField = document.getElementById('extraSort');	
	SelectField.innerHTML = '';
	
	if (color == 'green') SelectField.innerHTML = extra;
	if (color == 'purple' && e == 5) {
		$('#extraSort').html("<label for='ime'> Име </label> <input name='ime' type='text' value ='' class='top10 bottom10' style='width:140px;'/>");
		$('#dropDowns').css('display','none');	
	}
	if (color == 'green' && (e == 4 || e == 5)) $('#dropDowns').css('display','none');	
	
}

function selectTitle(e) {
	var title = document.getElementsByName("tabTitle");
	title[0].innerHTML = title[0].innerHTML + e; 
}

function want(id) {
	var want = document.getElementById("wantImg"+id);
	var been = document.getElementById("beenImg"+id);
	var lived = document.getElementById("livedImg"+id);
	
	$(want).css('background-image', "url(/images/purple/main_info_field/profile_map_icon_1_active.png)");
	$(been).css('background-image', "url(/images/purple/main_info_field/profile_map_icon_2_inactive.png)");
	$(lived).css('background-image', "url(/images/purple/main_info_field/profile_map_icon_3_inactive.png)");
	
	document.getElementById('country'+id).value = 1;
}

function been(id) {
	var want = document.getElementById("wantImg"+id);
	var been = document.getElementById("beenImg"+id);
	var lived = document.getElementById("livedImg"+id);
	
	$(want).css('background-image', "url(/images/purple/main_info_field/profile_map_icon_1_inactive.png)");
	$(been).css('background-image', "url(/images/purple/main_info_field/profile_map_icon_2_active.png)");
	$(lived).css('background-image', "url(/images/purple/main_info_field/profile_map_icon_3_inactive.png)");
	
	document.getElementById('country'+id).value = 2;
}

function lived(id) {
	var want = document.getElementById("wantImg"+id);
	var been = document.getElementById("beenImg"+id);
	var lived = document.getElementById("livedImg"+id);
	
	$(want).css('background-image', "url(/images/purple/main_info_field/profile_map_icon_1_inactive.png)");
	$(been).css('background-image', "url(/images/purple/main_info_field/profile_map_icon_2_inactive.png)");
	$(lived).css('background-image', "url(/images/purple/main_info_field/profile_map_icon_3_active.png)");
	
	document.getElementById('country'+id).value = 3;
}

function deletePhoto(id,num) {
	var box  = document.getElementById('snimka'+num);
		
		var http = getHTTPObject();
		http.open("GET", "/deletesnimki.php?id=" + escape(id), true);
		http.onreadystatechange = function() {
			if ( http.readyState == 4 ) {
				if (http.status == 200) {
					var xml     = http.responseXML;
					var results = xml.getElementsByTagName("x")[0].firstChild.nodeValue;
	        		if (results) box.innerHTML = '';
				}
			}
		}
		http.send(null);
}

function deleteVideo(id,num) {
	var box  = document.getElementById('video'+num);
	
	var http = getHTTPObject();
		http.open("GET", "/deleteVideo.php?id=" + escape(id), true);
		http.onreadystatechange = function() {
			if ( http.readyState == 4 ) {
				if (http.status == 200) {
					var xml     = http.responseXML;
					var results = xml.getElementsByTagName("x")[0].firstChild.nodeValue;
	        		if (results) { box.innerHTML = ''; kosh.innerHTML='';}
				}
			}
		}
		http.send(null);
}

function goToAnchor() {
document.body.onload = null;
TB_show('','/images/welcome_new_version.gif',false);
}

var timeout;
var delay = 5000;
var currentId = 0;

function startMainSlideshow(time)
{
	if (time) delay = time;
	
	timeout = window.setTimeout('goNext()', delay);
}

function goNext()
{
	$('#bubble'+currentId).attr('class', 'green_bubble');
	
	currentId++;
	if (currentId > 4) currentId = 0;
	
	$('#frontPic').attr('src', ThumbsArray[currentId]);	
	$('#frontText').html(TitlesArray[currentId]);
	
	$('.frontID').attr('href', IdArray[currentId]);
	
	$('#bubble'+currentId).attr('class', 'green_bubble_on');
		
	timeout = window.setTimeout('goNext()', delay);
}

function manualSelect(id)
{
	$('#bubble'+currentId).attr('class', 'green_bubble');
	
	currentId = id;
	
	$('#frontPic').attr('src', ThumbsArray[currentId]);	
	$('#frontText').html(TitlesArray[currentId]);
	$('.frontID').attr('href', IdArray[currentId]);
	$('#bubble'+currentId).attr('class', 'green_bubble_on');
	
	window.clearTimeout(timeout);
	timeout = window.setTimeout('goNext()', 10000);
	
	return false;
}