function redir(value){
  window.location.pathname=value;
}

function GetXmlHttpObject(){
  var xmlHttp=null;
  try{
    // Firefox, Opera 8.0+, Safari, Chrome
    xmlHttp=new XMLHttpRequest();
  }
  catch(e){
    //Internet Explorer
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  return xmlHttp;
}

//Show and hide an element
function ShowHide(id) {
	var cur = document.getElementById(id).style;
	if (cur.display != "none") {
		cur.display = "none";
	}
  else {
		cur.display = "inline";
	}
	return true;
}

//Give focus to an element
function setFocus(id) {
	var focusField = document.getElementById(id);
	focusField.focus();
}

//Open a new window
function MessageWindow(value){
    AB = window.open(value, '', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, top=200, left=170, width=840, height=420');
    AB.focus();
}

//Close the current window
function closewin(){
    self.close();
}


//Clear options in select element
function options_clear(id){
  while (document.getElementById(id).childNodes.length > 0) {
    document.getElementById(id).removeChild(document.getElementById(id).childNodes[0])
  }
}

//Change image source in an element
function ChangeImage(id, imgsrc, w, h){
  a = document.getElementById(id);
  b = document.getElementById('listing-bigimg');
  maxh = parseInt(b.style.height);
  mt = Math.round((maxh-h)/2);
  a.src = imgsrc;
  a.style.marginTop = mt+'px';
  if(w!=null){
    a.width=w;
  }
  if(h!=null){
    a.height=h;
  }
}

//Confirm deletion
function confirm_delete(value){
  c=confirm("The following listing will be deleted:\n\n"+value);
  if(c){
    return true;
  }
  else{
    return false;
  }
}

function checkboxSelectAll(formname){
  a = document.forms[formname];
  len = a.elements.length;
  for(i=0;i<len;i++){
    if(a.elements[i].type == "checkbox"){
      a.elements[i].checked = true;
    }
  }
}

function checkboxClearAll(formname){
  a = document.forms[formname];
  len = a.elements.length;
  for(i=0;i<len;i++){
    if(a.elements[i].type == "checkbox"){
      a.elements[i].checked = false;
    }
  }
}

function cursorPointer(a){
  a.style.cursor='pointer';
}

function reportOver(id){
  a = document.getElementById(id);
  a.style.cursor = 'pointer';
  a.style.color='#D60030';
  a.style.textDecoration='none';
}

function reportOut(id){
  a = document.getElementById(id);
  a.style.cursor = 'pointer';
  a.style.color='#000099';
  a.style.textDecoration='underline';
}

function reportClick(id, adid){
  a = document.getElementById(id);
  var pos = new Array();
  pos = findPos(id);
  y = (pos[1]+80)+"px";
  if(window.innerWidth){
    x = (window.innerWidth-400)/2+"px";
  }
  else if(document.body){
    x = (document.body.clientWidth-400)/2+"px";
  }
  else if(document.documentElement && document.documentElement.clientWidth){
    x = (document.documentElement.clientWidth-400)/2+"px";
  }
  else{
    x = (pos[0]-400)+"px";
  }
  d = document.getElementById("rep-d");
  if(d==null){
    var d = document.createElement("div");
  }
  d.setAttribute("id", "rep-d");
  d.setAttribute("class", "rep-d");
  document.body.appendChild(d);
  var txt = "<div class=\"rep-c\"><span id=\"rep-c\" onmouseover=\"cursorPointer(this);\" onclick=\"reportClose();\">";
  txt += "Close&nbsp;<img src=\"img/delete.gif\"></span></div><form id=\"rep-f\" action=\"\"><div id=\"warn\" class=\"warn\"></div>";
  txt += "<br /><input type=\"radio\" name=\"rp\" value=\"1\" /><b>Illegal</b> - this ad is about illegal services/goods";
  txt += "<br /><input type=\"radio\" name=\"rp\" value=\"2\" /><b>Scam / Fraudulent</b> - you suspect this to be a scam";
  txt += "<br /><input type=\"radio\" name=\"rp\" value=\"3\" /><b>Inappropriate</b> - there is inappropriate or offensive text/images";
  txt += "<br /><input type=\"radio\" name=\"rp\" value=\"4\" /><b>Wrong Category</b> - this ad does not belong in this category";
  txt += "<br /><br /><br /><input type=\"button\" name=\"cancel\" value=\"Cancel\" onclick=\"reportClose();\" />";
  txt += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"button\" name=\"sbm\" value=\"Submit\" onclick=\"ajUserReport("+adid+");\" />";
  document.getElementById("rep-d").innerHTML = txt;
  var a = document.getElementById("rep-d").style;
  a.position = "absolute";
  a.left = x;
  a.top = y;
  a.zIndex = "99";
  a.display = "block";
}

function reportClose(){
  a = document.getElementById("rep-d");
  a.style.display = "none";
  a.innerHTML = "";
}

function findPos(id){
  var p = document.getElementById(id);
  var px = 0;
  var py = 0;
  while(p!=null){
    px += p.offsetLeft;
    py += p.offsetTop;
    p = p.offsetParent;
  }
  return [px,py];
}

function ajUserReport(adid){
  var a = document.getElementById("rep-f");
  var rflag = '';
  for(i=0;i<a.rp.length;i++){
    if(a.rp[i].checked){
      rflag = a.rp[i].value;
    }
  }
  if(rflag==''){
    document.getElementById("warn").innerHTML = "Please choose one of the options below";
    return;
  }
  var strURL = "aj_user_report.php";
	var req = GetXmlHttpObject();
  params = "adid="+adid+"&rflag="+rflag;
  var msg = "Thank you for reporting.<br /><br /><input type=\"button\" name=\"cancel\" value=\"Cancel\" onclick=\"reportClose();\" />";
  msg_err = "An error occurred. Please try again later.";
  if(req){
    req.onreadystatechange = function(){
      if(req.readyState==4){
        if(req.status==200){
          if(req.responseText=="3"){
            document.getElementById("rept").innerHTML = "Thank you for reporting";
            reportClose();
          }
          else{
            a.innerHTML = msg_err;
          }
		    }
        else{
          a.innerHTML = msg_err;
        }
		  }
    }
		req.open("POST", strURL, true);
    req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		req.send(params);
	}
}
