
var req = null;

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

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;
      } else {
        destination.innerHTML = "Loading data...";
      }
    }
    req.open("GET", SEO_SITE_ROOT+"/location.php?sec=" + section + "&sel=country", true);
    req.send(null);
  } else {
    destination.innerHTML = 'Browser unable to create XMLHttp Object';
  }
}

function SelectRegion(id_country, destination, destination2, postfix) {
	if (postfix == undefined) {
		postfix = '';
	}
  if (id_country != '') {
    InitXMLHttpRequest();
    // Load the result from the response page
    if (req) {
      req.onreadystatechange = function() {
        if (req.readyState == 4) {
          destination.innerHTML = req.responseText;
        } else {
          destination.innerHTML = "Loading data...";
        }
      }
      req.open("GET", SEO_SITE_ROOT+"/admin/admin_location.php?sel=region&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";
  }
  destination2.innerHTML = "";
}

function SelectCity(id_region, destination, postfix) {
	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;
        } else {
          destination.innerHTML = "Loading data...";
        }
      }
      req.open("GET", SEO_SITE_ROOT+"/admin/admin_location.php?sel=city&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 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 ajaxRequest(file_name, str, destination_odj, tmp_text, anisochronous){
	ajaxRequestResult = '';
  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 = ajaxRequestResult = 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){
		ajaxRequestResult = req.responseText;
		RunJS(destination_odj,req.responseText);
	  }
    } else {
      destination_odj.innerHTML = 'Browser unable to create XMLHttp Object';
	  ShowAjaxError('Browser unable to create XMLHttp Object', '', 'error');
    }
  } else {
    destination_odj.innerHTML = "no string";
	ShowAjaxError('no string', '', 'error');
  }
}
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 #D82B2B';
  divTag.style.background = '#FFFFAA';
  divTag.style.padding = '20px';
  divTag.style.fontSize = '13px';
  divTag.style.fontWeight = 'bold';

  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 = '2px';
  }
  divTag.style.left = '500px';

  divTag.style.filter = 'alpha(opacity=1)';
  divTag.style.opacity = '0.01';
  
  divTag.className ="dynamicDiv"; //"error_message";//
  divTag.innerHTML = code;
  document.body.appendChild(divTag);
}

function SelectUserTypes(section, id_usertypes, destination) {
    InitXMLHttpRequest();
    // Load the result from the response page
    if (req) {
        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                destination.innerHTML = req.responseText;
            } else {
                if (section == 'rpt') {
                    destination.innerHTML = "<select style=\"width:150px\"><option>Loading data...</option></select>";
                } else {
                    destination.innerHTML = ' Loading data...';
                }

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