var isopen = 0;
function adflyerShowall() 
{
    getdata('/ClassifiedAds/BuyTab.aspx', 'listall');
    if (isopen == 0)
    {
        objectById('listall').style.display = 'block';
        isopen = 1;
    }
    else
    {
        objectById('listall').style.display = 'none';
        isopen = 0;
    }
}

//TEMP TEMP
var beingCompared = "";

function compareAdvert(itemid)
{
    //alert(itemid);
    //alert(beingCompared.indexOf("," + itemid + ","));
    if (beingCompared.indexOf("," + itemid + ",") > 0)
    {
        //remove from the compare array
        beingCompared.replace("," + itemid + ",","");
        //alert(beingCompared);
        objectById("compare"+ itemid).src = "/Images/Buttons/Compare.png";
    }
    else
    {
        //add to the array
        beingCompared = beingCompared + "," + itemid + ",";
        //alert(beingCompared);
        objectById("compare"+ itemid).src = "/Images/Buttons/CompareOn.png";
    }
}
//END TEMP TEMP
function popupAdvert(url,itemid)
{
    var popUpWin = "popupwin" + itemid;
	if (popUpWin.open) popUpWin.close();
	window.open(url, "popupwin" + itemid, "width=750,height=510,scrollbars=1,toolbar=0,location=0")
}
/*
// generic popup image function - opens a set width and height and displays the image as passed in through imagePath at the full width and height specified
function popUpImage(imagePath, width, height, windowTitle, scroll)
{
	windowTitle = windowTitle || "AdFlyer";
	closePopups();
	popUpWin = window.open("", "popupwin", "width=" + width + ",height=" + height + ",left=" +( (screen.width / 2) - (width / 2)) + ",top=" + ((screen.height / 2) - (height / 2)) + ",scrollbars=" + ((scroll) ? 1 : 0));
	popUpWin.document.open();
	popUpWin.document.write("<html><head><title>" + windowTitle + "</title></head><body bottommargin=0 topmargin=0 leftmargin=0 rightmargin=0 marginwidth=0 marginheight=0><table border=0 cellspacing=0 cellpadding=0 width=100% height=100%><tr><td align=center valign=middle><img src='" + imagePath + "' alt='" + windowTitle + "'></td></tr></table></body></html>");
	popUpWin.document.close();
}
function Controls(obj) {
    alert(obj[obj.selectedIndex].value);
}*/
// cross browser function to find an element by id
function objectById(id)
{ 
	if (document.getElementById) return document.getElementById(id); 
	else if (document.all) return document.all[id]; 
	else if (document.layers) return document.layers[id]; 
} 

function quickSearchCatchEnter(e)
{
	if (e.keyCode == 13)
	{
		quickSearchGo();
		return false;
	}
	else return true;
}

function quickSearchGo()
{
	var url = HTTPHost + "/search-results";
	if ((document.forms[0].quickSearch.value != "") && (document.forms[0].quickSearch.value != "Enter Search Text")) url += "?a=" + document.forms[0].quickSearch.value;
	location.href = url;
}

function changeAttributeFilter(sender, keyName)
{
	// determine url without the current attribute filter, and append the new value if one is selected
    eval("var url = baseUrl.replace(/&Atts." + keyName + "=[0-9]+/, \"\")");
    if (url.indexOf('?') > 0) {
        if (sender.selectedIndex > 0) url += "&Atts." + keyName + "=" + sender.options[sender.selectedIndex].value;
    }
    else
    {
        if (sender.selectedIndex > 0) url += "?Atts." + keyName + "=" + sender.options[sender.selectedIndex].value;
    }
	location.href = url;
}

function changeRPP(sender, defaultRPP)
{
    // determine url without the RPP selection
	var url = '';
    eval("url = baseUrl.replace(/&RPP=[0-9]+/, \"\")");
    eval("url = baseUrl.replace(/\\?RPP=[0-9]+/, \"\")");
    if (url.indexOf('?') > 0 || url.indexOf('&') > 0) {
        if (sender.options[sender.selectedIndex].value != defaultRPP) url += "&RPP=" + sender.selectedIndex;
    }
    else {
        if (sender.options[sender.selectedIndex].value != defaultRPP) url += "?RPP=" + sender.selectedIndex;
    }
	location.href = url;
}

function changeSortBy(sender, defaultSort)
{
	// determine url without the sort by selection
	var url = '';
    eval("url = baseUrl.replace(/&Sort=[0-9]+/, \"\")");
    eval("url = baseUrl.replace(/\\?Sort=[0-9]+/, \"\")");
    if (url.indexOf('?') > 0 || url.indexOf('&') > 0) {
        if (sender.options[sender.selectedIndex].value != defaultSort) url += "&Sort=" + sender.selectedIndex;
    }
    else {
        if (sender.options[sender.selectedIndex].value != defaultSort) url += "?Sort=" + sender.selectedIndex;
    }
	location.href = url;
}

function changeShow(sender, defaultSort) {
    // determine url without the sort by selection
    var url = '';
    eval("url = baseUrl.replace(/&Type=[0-9]+/, \"\")");
    eval("url = baseUrl.replace(/\\?Type=[0-9]+/, \"\")");
    if (url.indexOf('?') > 0 || url.indexOf('&') > 0) {
        if (sender.options[sender.selectedIndex].value != '') url += "&Type=" + sender.options[sender.selectedIndex].value;
    }
    else {
        if (sender.options[sender.selectedIndex].value != '') url += "?Type=" + sender.options[sender.selectedIndex].value;
    }
    location.href = url;
}


//AJAX LOADER
// here we define global variable
var ajaxdestination="";

function getdata(what,where) { // get data from source (what)
 try {
   xmlhttp = window.XMLHttpRequest?new XMLHttpRequest():
  		new ActiveXObject("Microsoft.XMLHTTP");
 }
 catch (e) { /* do nothing */ }

 objectById(where).innerHTML ="<center><br><br><img src='/images/waiting.gif'></center>";
// we are defining the destination DIV id, must be stored in global variable (ajaxdestination)
 ajaxdestination=where;
 xmlhttp.onreadystatechange = triggered; // when request finished, call the function to put result to destination DIV
 xmlhttp.open("GET", what);
 xmlhttp.send(null);
  return false;
}

function triggered() { // put data returned by requested URL to selected DIV
  if (xmlhttp.readyState == 4) if (xmlhttp.status == 200)
      objectById(ajaxdestination).innerHTML = xmlhttp.responseText;
}


// snap a floating object to a static object, with an x and y offset for more control over positioning
function findPos(snapTo, object) {
    var x, y, ox, oy, p, tx, a, b, k, d, da, tw, q0, xx, yy;
    var xOffset = -8
    var yOffset = -18;
    a = parseInt(a);

    if ((objectById(snapTo)) != null) {
        if ((objectById(object)) != null) {
            a = parseInt(xOffset);
            b = parseInt(yOffset);
            x = 0;
            y = 0;
            ox = 0;
            oy = 0;
            p = "";
            tx = 1;
            da = "document.all['" + snapTo + "']";
            if (document.getElementById) {
                d = "document.getElementsByName('" + snapTo + "')[0]";
                if (!eval(d)) {
                    d = "document.getElementById('" + snapTo + "')";
                    if (!eval(d)) d = da;
                }
            }
            else if (document.all) d = da;

            if (document.all || document.getElementById) {
                while (tx == 1) {
                    p += ".offsetParent";
                    if (eval(d + p)) {
                        x += parseInt(eval(d + p + ".offsetLeft"));
                        y += parseInt(eval(d + p + ".offsetTop"));
                    }
                    else tx = 0;
                }
                ox = parseInt(objectById(snapTo).offsetLeft);
                oy = parseInt(objectById(snapTo).offsetTop);
                tw = x + ox + y + oy;
                if (tw == 0 || (navigator.appVersion.indexOf("MSIE 4") > -1 && navigator.appVersion.indexOf("Mac") > -1)) {
                    ox = 0;
                    oy = 0;
                    if (objectById(snapTo).style.left) {
                        x = parseInt(objectById(snapTo).style.left);
                        y = parseInt(objectById(snapTo).style.top);
                    }
                    else {
                        a = (Math.abs(a) < 1000) ? 0 : a;
                        b = (Math.abs(b) < 1000) ? 0 : b;
                        x = document.body.scrollLeft + event.clientX;
                        y = document.body.scrollTop + event.clientY;
                    }
                }
            }
            else if (document.layers) {
                x = objectById(snapTo).x;
                y = objectById(snapTo).y;
                q0 = document.layers, dd = "";
                for (var s = 0; s < q0.length; s++) {
                    dd = 'document.' + q0[s].name;
                    if (eval(dd + '.document.' + snapTo)) {
                        x += eval(dd + '.left');
                        y += eval(dd + '.top');
                        break;
                    }
                }
            }
            var el = (document.layers) ? objectById(object) : objectById(object).style;
            xx = parseInt(x + ox + a), yy = parseInt(y + oy + b);
            if (navigator.appVersion.indexOf("MSIE 5") > -1 && navigator.appVersion.indexOf("Mac") > -1) {
                xx += parseInt(document.body.leftMargin);
                yy += parseInt(document.body.topMargin);
            }
            el.left = xx + (document.layers || window.opera ? "" : "px");
            el.top = yy + (document.layers || window.opera ? "" : "px");
        }
    }
}

// snap a floating object to a static object, with an x and y offset for more control over positioning
function snapPos(snapTo, object, xx, yy) {
    var x, y, ox, oy, p, tx, a, b, k, d, da, tw, q0, xx, yy;
    var xOffset = xx;
    var yOffset = yy;
    a = parseInt(a);

    if ((objectById(snapTo)) != null) {
        if ((objectById(object)) != null) {
            a = parseInt(xOffset);
            b = parseInt(yOffset);
            x = 0;
            y = 0;
            ox = 0;
            oy = 0;
            p = "";
            tx = 1;
            da = "document.all['" + snapTo + "']";
            if (document.getElementById) {
                d = "document.getElementsByName('" + snapTo + "')[0]";
                if (!eval(d)) {
                    d = "document.getElementById('" + snapTo + "')";
                    if (!eval(d)) d = da;
                }
            }
            else if (document.all) d = da;

            if (document.all || document.getElementById) {
                while (tx == 1) {
                    p += ".offsetParent";
                    if (eval(d + p)) {
                        x += parseInt(eval(d + p + ".offsetLeft"));
                        y += parseInt(eval(d + p + ".offsetTop"));
                    }
                    else tx = 0;
                }
                ox = parseInt(objectById(snapTo).offsetLeft);
                oy = parseInt(objectById(snapTo).offsetTop);
                tw = x + ox + y + oy;
                if (tw == 0 || (navigator.appVersion.indexOf("MSIE 4") > -1 && navigator.appVersion.indexOf("Mac") > -1)) {
                    ox = 0;
                    oy = 0;
                    if (objectById(snapTo).style.left) {
                        x = parseInt(objectById(snapTo).style.left);
                        y = parseInt(objectById(snapTo).style.top);
                    }
                    else {
                        a = (Math.abs(a) < 1000) ? 0 : a;
                        b = (Math.abs(b) < 1000) ? 0 : b;
                        x = document.body.scrollLeft + event.clientX;
                        y = document.body.scrollTop + event.clientY;
                    }
                }
            }
            else if (document.layers) {
                x = objectById(snapTo).x;
                y = objectById(snapTo).y;
                q0 = document.layers, dd = "";
                for (var s = 0; s < q0.length; s++) {
                    dd = 'document.' + q0[s].name;
                    if (eval(dd + '.document.' + snapTo)) {
                        x += eval(dd + '.left');
                        y += eval(dd + '.top');
                        break;
                    }
                }
            }
            var el = (document.layers) ? objectById(object) : objectById(object).style;
            xx = parseInt(x + ox + a), yy = parseInt(y + oy + b);
            if (navigator.appVersion.indexOf("MSIE 5") > -1 && navigator.appVersion.indexOf("Mac") > -1) {
                xx += parseInt(document.body.leftMargin);
                yy += parseInt(document.body.topMargin);
            }
            el.left = xx + (document.layers || window.opera ? "" : "px");
            el.top = yy + (document.layers || window.opera ? "" : "px");
        }
    }
}
