
var req = null;

function ChangeLocation(obj, from){
	from = from || 'rp';
    if($(obj).parent().attr('id') == 'region_div'){
        if($('#city_div').attr('id') == 'city_div'){
            SelectCity(from, $(obj).val(), document.getElementById('city_div'));
            return 1;
        }
    }
    if($(obj).parent().attr('id') == 'couple_region_div'){
        if($('#couple_city_div').attr('id') == 'couple_city_div'){
            SelectCity(from, $(obj).val(), document.getElementById('couple_city_div'), '', 'couple_');
            return 1;
        }
    }
    if($(obj).attr('name') == 'id_country'){
        if($('#city_div').attr('id') == 'city_div' && $('#region_div').attr('id') == 'region_div'){
            SelectRegion(from, $(obj).val(), document.getElementById('region_div'), document.getElementById('city_div'));
            return 1;
        }
    }
    if($(obj).attr('name') == 'couple_id_country'){
        if($('#couple_city_div').attr('id') == 'couple_city_div' && $('#couple_region_div').attr('id') == 'couple_region_div'){
            SelectRegion(from, $(obj).val(), document.getElementById('couple_region_div'), document.getElementById('couple_city_div'), '', 'couple_');
            return 1;
        }
    }
    SelectCity(from, $(obj).val(), document.getElementById('city_div'));
    return 1;
}

function InitXMLHttpRequest() {
  // Make a new XMLHttp object
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
}

function SelectCountry(section, destination) {
  InitXMLHttpRequest();
  // Load the result from the response page
  if (req) {
    req.onreadystatechange = function() {
      if (req.readyState == 4) {
        destination.innerHTML = req.responseText;
		  if(section == 'sw'){
			//$('#location_block').find('select').selectBox();
		  }
      } else {
        if (section == 'rp') {
          destination.innerHTML = "<select style=\"width:150px\"><option>Loading data...</option></select>";
        } else if (section == 'sw') {
          //destination.innerHTML = "<select style=\"width:100%\"><option>Loading data...</option></select>";
		  //$('#location_block').find('select').selectBox();
        } else {
          destination.innerHTML = "<select style=\"width:150px\" class=\"index_select\"><option>Loading data...</option></select>";
        }

      }
    }
    req.open("GET", SITE_ROOT+"/location.php?sec=" + section + "&sel=country", true);
    req.send(null);
  } else {
    destination.innerHTML = 'Browser unable to create XMLHttp Object';
  }
}

function SelectRegion(section, id_country, destination, destination2, postfix, prefix) {
	prefix = prefix || '';
	if (postfix == undefined) {
		postfix = '';
	}
  //if (id_country != '') {
    InitXMLHttpRequest();
    // Load the result from the response page
    if (req) {
      req.onreadystatechange = function() {
        if (req.readyState == 4) {
		  if(section == 'sw'){
			$('#region_div').find('select').selectBox('destroy').replaceWith(req.responseText);
			$('#region_div').find('select').selectBox();
		  }else{
			destination.innerHTML = req.responseText;
		  }
        } else {
          if (section == 'rp') {
            destination.innerHTML = "<select style=\"width:150px\"><option>Loading data...</option></select>";
          }else if (section == 'sw') {
			$('#city_div').find('select').html("<option value='0'></option>").selectBox('destroy').selectBox();
			$('#region_div').find('select').html("<option value='0'></option>").selectBox('destroy').selectBox();
          } else {
            destination.innerHTML = "<select style=\"width:150px\" class=\"index_select\"><option>Loading data...</option></select>";
          }
        }
      }
      req.open("GET", SITE_ROOT + "/location.php?sec=" + section + "&sel=region&prefix="+prefix+"&postfix="+postfix+"&id_country=" + id_country, true);
      req.send(null);
    } else {
      destination.innerHTML = 'Browser unable to create XMLHttp Object';
    }
  /*} else {
    destination.innerHTML = "Country is not selected";
  }*/
  if (section == 'as') {
    destination2.innerHTML = "<select style=\"width:150px\" class=\"index_select\"><option>All</option></select>";
  } else {
    if (section == 'rp') {
      destination2.innerHTML = "<select style=\"width:150px\" name='"+prefix+"id_city'><option value='0'>Please select...</option></select>";
    } else if (section == 'sw') {
		$('#city_div').find('select').html("<option value='0'></option>").selectBox('destroy').selectBox();
    } else {
      destination2.innerHTML = "<select style=\"width:150px\" class=\"index_select\"><option>Please select...</option></select>";
    }
  }
}

function SelectCity(section, id_region, destination, postfix, prefix) {
	prefix = prefix || '';
	if (postfix == undefined) {
		postfix = '';
	}
  //if (id_region != '') {
    InitXMLHttpRequest();
    // Load the result from the response page
    if (req) {
      req.onreadystatechange = function() {
        if (req.readyState == 4) {
          destination.innerHTML = req.responseText;
		  if(section == 'sw'){
			$('#city_div').find('select').selectBox('destroy').replaceWith(req.responseText);
			$('#city_div').find('select').selectBox();
		  }
        } else {
          if (section == 'rp') {
            destination.innerHTML = "<select style=\"width:150px\"><option>Loading data...</option></select>";
          } else if (section == 'sw') {
			$('#city_div').find('select').html("<option value='0'></option>").selectBox('destroy').selectBox();
          } else {
            destination.innerHTML = "<select style=\"width:150px\" class=\"index_select\"><option>Loading data...</option></select>";
          }
        }
      }
      req.open("GET", SITE_ROOT + "/location.php?sec=" + section + "&sel=city&prefix="+prefix+"&postfix="+postfix+"&id_region=" + id_region, true);
      req.send(null);
    } else {
      destination.innerHTML = 'Browser unable to create XMLHttp Object';
    }
  /*} else {
    destination.innerHTML = "Region is not selected";
  }*/
}

function CheckLogin(section, login, destination) {
  if (login != '') {
    InitXMLHttpRequest();
    // Load the result from the response page
    if (req) {
      req.onreadystatechange = function() {
        if (req.readyState == 4) {
          destination.innerHTML = req.responseText;
        }
      }
      req.open("GET", SITE_ROOT + "/location.php?sec=" + section + "&sel=login&login=" + login, true);
      req.send(null);
    } else {
      destination.innerHTML = 'Browser unable to create XMLHttp Object';
    }
  } else {
    destination.innerHTML = "Nick is empty";
  }
}

function ChengeBreadCrumbs(tab, link){ 
    link = link.split('/');
    link = link[link.length-1].split('?');
    var category = link[0].split('.');
    var id = link[1].split('&');

    InitXMLHttpRequest();
    // Load the result from the response page
    if (req) {
    req.onreadystatechange = function() {
      if (req.readyState == 4) {
               $('#breadcrumbs').html(req.responseText);
      }
    }
    req.open("GET", SITE_ROOT+"/backend.php?sel=breadcrumbs&tab="+tab+"&cat="+category[0]+"&"+id[1], true);    
    req.send(null);
    } else {
    return false;
    }
}

function ShowTab(sel, mlink, sub, sec_par) {

  destination = document.getElementById('tab_div');
  
	if (sel > 6 && sub != 0) {
	} else {	
		$('.tab_block').removeClass('active');
		$('#menu'+sel).addClass('active');
	}
  
	if(window.$){
		var data = {'act': 'ajax'};
		if (sel > 5 && sub != 0) {
			data.sub = sel;
			data.action = sub;
		}
		$.get(
			mlink,
			data,
			function(responseText){
				$(destination).html(responseText);
				if (sel < 6) ChengeBreadCrumbs(sel, mlink);
			}
		);
	}else{
		InitXMLHttpRequest();
		// Load the result from the response page
		if (req) {
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					RunJS(destination, req.responseText);
					if (sel < 6)
						ChengeBreadCrumbs(sel, mlink);
			  }
			}
			if (sel > 5 && sub != 0) {
				req.open("GET", mlink + "&sub=" + sel + "&act=ajax&action="+ sub, true);
			} else {
				req.open("GET", mlink + "&act=ajax", true);
			}
			req.send(null);
		} else {
			destination.innerHTML = 'Browser unable to create XMLHttp Object';
		}
	}
}

function RunJS(dest, response){   
       
  if (response.search(/<script/) == -1){
    dest.innerHTML = response;
    return false;
  }
  //Check user browser
  var agent=(window.navigator.appName=='Netscape')?'Firefox':'IE';
  if(agent=='IE'){
    ///place &nbsp; before <scrript> tag
    dest.innerHTML=response;
    var arr_scripts = dest.getElementsByTagName("script");
    for(i in arr_scripts){
      var src = arr_scripts[i].src;
      if(null!=src){
        var has_question = (src.indexOf('?', 1)>0 ? true : false);
        if(0==src.length)
          arr_scripts[i].setAttribute('src', '');
        else
          arr_scripts[i].setAttribute('src', arr_scripts[i].src + (has_question==true ? '&' : '?') + '1=1');
      }
    }
  }else{
    //Mozilla
    var d=dest.parentNode;
    var d_new=document.createElement('div');

    //Set same attributes as original
    var attributes=dest.attributes;
    var len=attributes.length;

    d_new.innerHTML=response;
    for(var i=0; i<len; i++){
      d_new.setAttribute(attributes[i].name, attributes[i].value);
    }

    d.replaceChild(d_new, dest);
  }
}

function VoteAction(id_upload, vote, id_category, destination, upload_type) {
  InitXMLHttpRequest();
    if (req){
    req.onreadystatechange = function() {
      if (req.readyState == 4) {
        destination.innerHTML = req.responseText;
      } else {
        destination.innerHTML = "Loading...";
      }
    }
    req.open('GET', SEO_SITE_ROOT+'/gallary.php?sel=vote&id_upload=' + id_upload + '&vote=' + vote + '&id_category=' + id_category+ '&upload_type=' + upload_type, true);
    req.send(null);
    }
    else{
       destination.innerHTML = 'Browser unable to create XMLHttp Object';
    }
    return;
}

function ajaxRequest(file_name, str, destination_odj, tmp_text, anisochronous){
  if (str != '') {
    str = str+'&ajax=1';
    InitXMLHttpRequest();
    // Load the result from the response page
    if (req) {
      if (anisochronous){
        req.onreadystatechange = function() {
          if (req.readyState == 4) {
            //destination_odj.innerHTML = req.responseText;
            RunJS(destination_odj,req.responseText);
          } else {
            destination_odj.innerHTML = tmp_text;
          }
        }
      }
      req.open("POST", file_name+'?rnd='+Math.random(), anisochronous);
      req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8 ');
      req.setRequestHeader("Content-length", str.length);
          req.setRequestHeader("Connection", "close");
      req.send(str);
      if (!anisochronous) RunJS(destination_odj,req.responseText);
    } else {
      destination_odj.innerHTML = 'Browser unable to create XMLHttp Object';
    }
  } else {
    destination_odj.innerHTML = "no string";
  }
}
opacity = 0;
i1 = 0;
i2 = 0;
t1 = 0;
function noticeCode(code){
  //if(code == '') return false;
  addNoticeDiv(code);
  divTag = document.getElementById('notice_div');
  opacity = 0;
  i1 = setInterval('increaseOpacity(divTag);',2);
}

function increaseOpacity(divTag){
  opacity++;
  if (opacity >= 100){
    clearInterval(i1);
    setTimeout("i2 = setInterval('decreaseOpacity(divTag)',5)",3000);
    return false;
  }
  setOpacity(divTag,opacity);
}

function decreaseOpacity(divTag){
  opacity = opacity-10;
  if (opacity <= 1){
    clearInterval(i2);
    clearTimeout(t1);
    document.body.removeChild(divTag);
    return false;
  }
  setOpacity(divTag,opacity);
}
function setOpacity(divTag,i){
  divTag.style.filter = 'alpha(opacity='+i+')';
  divTag.style.opacity = i/100;
}
function addNoticeDiv(code){
  if (document.getElementById('notice_div')){
    clearInterval(i1);
    clearInterval(i2);
    clearTimeout(t1);
    divTag = document.getElementById('notice_div');
    if (divTag) document.body.removeChild(divTag);
  }

  var divTag = document.createElement("div");

  divTag.id = 'notice_div';
  divTag.setAttribute('align','center');
  divTag.style.margin = '0px auto';
  divTag.style.border = '1px solid #000000';
  divTag.style.background = '#ffffff';
  divTag.style.padding = '15px'

  if (navigator.appName == 'Microsoft Internet Explorer'){
    divTag.style.position = 'absolute';
    divTag.style.top = document.body.scrollTop+10;
  }else{
    divTag.style.position = 'fixed';
    divTag.style.top = '10px';
  }
  divTag.style.left = '10px';

  divTag.style.filter = 'alpha(opacity=1)';
  divTag.style.opacity = '0.01';

  divTag.className ="dynamicDiv";
  divTag.innerHTML = code;
  document.body.appendChild(divTag);
}


function SelectUserTypes (section, id_usertypes, destination, type) {
	if(!type) type="";
     str = "sec=" + section + "&sel=usertypes&id_usertypes="+id_usertypes+"&type="+type;
     destination_odj = destination;
     anisochronous = true;
     ajaxRequest("location.php", str, destination_odj, "", anisochronous);
}