function contact(dir) {
oMailwrap = document.getElementById('mail_wrap');
if (oMailwrap.style.visibility == 'visible' && dir == 'open') {
 return;
}

if (dir == 'open') {
 targetTop = oMailwrap.offsetTop - oMailwrap.offsetHeight + 9;
 oMailwrap.style.height = '0px';
 oMailwrap.style.top = oMailwrap.offsetTop - oMailwrap.offsetHeight + 'px';
 currentTop = oMailwrap.offsetTop;
 currentHeight = oMailwrap.offsetHeight;
 oMailwrap.style.visibility = 'visible';
} else {
 targetTop = oMailwrap.offsetTop + oMailwrap.offsetHeight - 9;
 currentTop = oMailwrap.offsetTop;
 currentHeight = oMailwrap.offsetHeight - 9;
}

contactAnimate(currentTop,currentHeight,targetTop,oMailwrap,dir);
}

function contactAnimate(currentTop,currentHeight,targetHeight,oMailwrap,dir) {
 if (dir == 'open') {
  
  oMailwrap.style.top = currentTop + 'px';
  oMailwrap.style.height = currentHeight + 'px';
  
  currentTop = currentTop - 20;
  currentHeight = currentHeight + 20;
  
  if (currentTop > targetTop) {
   setTimeout('contactAnimate('+currentTop+','+currentHeight+','+targetTop+',oMailwrap,\''+dir+'\')', 1);
  } else {
   oMailwrap.style.top = targetTop + 'px';
   oMailwrap.style.height = 'auto';
  }
 
 } else {
 
  oMailwrap.style.top = currentTop + 'px';
  oMailwrap.style.height = currentHeight + 'px';
  
  currentTop = currentTop + 20;
  currentHeight = currentHeight - 20;
  
  if (currentTop < targetTop) {
   setTimeout('contactAnimate('+currentTop+','+currentHeight+','+targetTop+',oMailwrap,\''+dir+'\')', 1);
  } else {
   oMailwrap.style.top = targetTop + 'px';
   oMailwrap.style.height = 'auto';
   oMailwrap.style.visibility = 'hidden';
  }
 
 }
}

var ajax_error = new Array();
ajax_error[0] = "Your email address is not in the correct format. Please check your entries and try again.";
ajax_error[1] = "Your friend's email address is not in the correct format. Please check your entries and try again.";
ajax_error[2] = "Your email address contains illegal characters. Please check your entries and try again.";
ajax_error[3] = "Your friend's email contains illegal characters. Please check your entries and try again.";
ajax_error[4] = "There are illegal characters in one of the fields you just entered. Please check your entries and try again.";

function abuseCheck() {
obj = document.emailForm;
if (obj.sender_name.value.length < 1) {
 alert("Please enter your name.\n\nThank you.");
 obj.sender_name.focus();
 return false;
} else if (obj.sender_email.value.length < 1) {
 alert("Please enter your email address.\n\nThank you.");
 obj.sender_email.focus();
 return false;
} else if (obj.message.value.length < 1) {
 alert("Please enter a message.\n\nThank you.");
 obj.message.focus();
 return false;
}

req = createXMLHttpRequest();

if (!req) {
 obj.submit();
} else {
 sender_name = obj.sender_name.value;
 sender_email = obj.sender_email.value;
 message = escape(obj.message.value);
 aid = obj.aid.value;
 
 url = 'classified_ads_process.php?action=email&ajax=true&sender_name='+sender_name+'&sender_email='+sender_email+'&message='+message+'&aid='+aid;
 req.open('GET', url, true);
 req.onreadystatechange = handleResponse;
 req.send(null);
 return false;
}

}

function handleResponse() {
  if(req.readyState == 4){
    if (req.status == 200) {
      var response = req.responseText;
      var update = new Array();
      update = response.split(':');
//      document.getElementById('info_box').style.visibility = 'hidden';
      if (update[0] == 'error') {
       changeDiv('mailbox',0.07);
       document.getElementById('error').innerHTML = "I'm sorry, but an error has occured.\n"+"<div class=\"error_div\">"+ajax_error[update[1]]+"</div>\n"+"Please <a href=\"javascript:ajax_goback('"+update[2]+"');\">click here</a> and try again.";
       document.getElementById('ajax_error').style.display = 'block';
       error_check = false;
      } else if (update[0] == 'success') {
       changeDiv('mailbox',0);
       document.getElementById('success').innerHTML = "<br><center><span style=\"font-weight: bold;\">Your email has been sent.</span></center><br>";
       document.getElementById('ajax_success').style.display = 'block';
//       document.getElementById('mailbox').style.visibility = 'hidden';
//       document.getElementById('mailbox').style.display = 'none';
       success_check = false;
      }		
    }
  }
}

function ajax_goback(elm) {
document.getElementById('ajax_error').style.display = 'none';
changeDiv('mailbox',1);
document.forms['emailForm'].elements[elm].focus();
error_check = true;
}

function createXMLHttpRequest() {

var request = false;

try {
     request = new XMLHttpRequest();
   } catch (trymicrosoft) {
     try {
       request = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (othermicrosoft) {
       try {
         request = new ActiveXObject("Microsoft.XMLHTTP");
       } catch (failed) {
         request = false;
       }  
     }
   }
  return request;
}

function resetDiv(div) {
hideDiv(div);
abuse_check = true;
success_check = true;
error_check = true;
mailbox_check = true;

oMailwrap = document.getElementById('mail_wrap');
targetTop = oMailwrap.offsetTop + oMailwrap.offsetHeight - 9;
currentTop = oMailwrap.offsetTop;
currentHeight = oMailwrap.offsetHeight - 9;
contactAnimate(currentTop,currentHeight,targetTop,oMailwrap,'close');
changeDiv('mailbox',1);
document.emailForm.message.value = '';
}

function hideDiv(div) {
document.getElementById(div).style.display = 'none';
}

function changeDiv(div,value) {
if (typeof obj.style.opacity != 'undefined') {
 var otype = 'w3c';
} else if (typeof obj.style.MozOpacity != 'undefined') {
 otype = 'moz';
} else if (typeof obj.style.KhtmlOpacity != 'undefined') {
 otype = 'khtml';
} else if (typeof obj.filters == 'object') {
 otype = 'ie';
} else {
 otype = 'none';
}

obj = document.getElementById(div);

switch(otype)
  {
    case 'ie':
      obj.style.filter = 'alpha(opacity='+value * 100+')';
      break;

    case 'khtml':
      obj.style.KhtmlOpacity = value;
      break;

    case 'moz':
      obj.style.MozOpacity = (value == 1 ? 0.9999999 : value);
      break;

    default:
      obj.style.opacity = (value == 1 ? 0.9999999 : value);
  }

}

/*=========================================

=========================================*/

var fps = 20;
var time = .4;
var picFrameId = 'galleryPic';
var picID;
var picTemp;

function showBigStart(picName) {
picTemp = new Image(); // Reset picTemp so that it "picTemp.complete" will give a reading on the newly loading picture.
picTemp.src = "images/ads/ad_photos/large/"+picName;
document.getElementById('loadingPic').style.display = 'block';
if (picTemp.complete) { // Check to see if the image is already in the browser cache.
 showBigFinish();
} else { // Image is not in the browser cache and is in the process of loading. Call function when it has completed loading.
 picTemp.onload = function() { showBigFinish(); };
}
}

function showBigFinish() {
picInfo = '<div style="text-align: right;"><a href="javascript:;" onclick="hideBig()">Back to Ad</a></div>';
darkenPage();
screenWidth = 775;
screenHeight = f_clientHeight();
scrollPosTop = f_scrollTop();
picWidth = picTemp.width;
picHeight = picTemp.height;
picLeft = parseInt((screenWidth-picWidth)/2);
picTop = parseInt(((screenHeight-105)-picHeight)/2);
objx = document.getElementById('bigPic');
objx.src = picTemp.src;
objx.width = picWidth;
objx.height = picHeight;
objy = document.getElementById('bigPic_container');
objy.style.display = 'block';
objy.style.left = picLeft+'px';
objy.style.top = scrollPosTop+picTop+'px';
objy.style.width = picWidth+2+'px';
document.getElementById('bigPic_text').innerHTML = picInfo;
document.getElementById('loadingPic').style.display = 'none';

obj = document.getElementById('bigPic');
var steps = time * fps;

if (typeof obj.style.opacity != 'undefined') {
 var otype = 'w3c';
} else if (typeof obj.style.MozOpacity != 'undefined') {
 otype = 'moz';
} else if (typeof obj.style.KhtmlOpacity != 'undefined') {
 otype = 'khtml';
} else if (typeof obj.filters == 'object') {
 otype = 'ie';
} else {
 otype = 'none';
}

dofade(steps, obj, 0, true, otype); // Fade in
}

function dofade(steps, obj, value, targetvisibility, otype) {
  value += (targetvisibility ? 1 : -1) / steps;
  if (targetvisibility ? value > 1 : value < 0) {
    value = targetvisibility ? 1 : 0;
  }

  setfade(obj, value, otype);

  if (targetvisibility ? value < 1 : value > 0) {
    setTimeout(function() { dofade(steps, obj, value, targetvisibility, otype); }, 1000 / fps);
  } else {
    if (targetvisibility == false) { // insert the new text and begin the fade in.
      checkLoad(steps, obj, otype);
    } else {
     // The fade out, loading, and fade in are complete.
     picClickable = true;
    }
  }
}

// Check if new picture is fully loaded yet.
function checkLoad(steps, obj, otype) {
if (picTemp.complete) {
 document.getElementById('loadingPic').style.display = 'none';
 //dofade(steps, obj, 0, true, otype);
 insertText(steps, obj, otype);
} else {
 document.getElementById('loadingPic').style.display = 'block';
 timerID = setTimeout(function() { checkLoad(steps, obj, otype); },10); 
}
}

function setfade(obj, value, otype)
{
  switch(otype)
  {
    case 'ie':
//      obj.filters.alpha.opacity = value * 100;
      obj.style.filter = 'alpha(opacity='+value * 100+')';
      break;

    case 'khtml':
      obj.style.KhtmlOpacity = value;
      break;

    case 'moz':
      obj.style.MozOpacity = (value == 1 ? 0.9999999 : value);
      break;

    default:
      obj.style.opacity = (value == 1 ? 0.9999999 : value);
  }
}

function hideBig() {
document.getElementById('bigPic_container').style.display = 'none';
lightenPage();
}

function darkenPage() {
pageScreen = document.getElementById('page_screen');
pageScreen.style.height = document.body.parentNode.scrollHeight + 'px';
pageScreen.style.display = 'block';
}

function lightenPage() {
pageScreen = document.getElementById('page_screen');
pageScreen.style.display = 'none';
}

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}