var blocks_count = 10;

var oTimer = null;
var iUpdateInterval = 15 * 1000; //--- in milliseconds
var sImagesUrl = sHomeUrl + 'media/images/directory/';


//--- Start timers ---//
var oldHandler = window.onload;
if(typeof window.onload != 'function') {
	window.onload = startTimer;
}
else {
	window.onload = function() {
		if(oldHandler) oldHandler();
		startTimer();
	}
}

function startTimer()
{
	getImage();
	oTimer = window.setInterval('getImage()', iUpdateInterval);
}

function getImage()
{
	var oImageHolder = document.getElementById('middleRight');
	var iImageIndex = Math.floor(Math.random() * aImages.length);
	oImageHolder.firstChild.src = sImagesUrl + aImages[iImageIndex];
}

function AddPhotoBlock() {

	var counter = document.getElementById('photo_counter');
	var id = parseInt(counter.value); 
	var blocks_div = document.getElementById('photos_block');
	var src = document.getElementById('photo_div_phid_');
	counter.value = ++id;
	var new_block = document.createElement('DIV');
	new_block.id = 'photo_div'+id;
	new_block.innerHTML = src.innerHTML.replace(/_phid_/g, id);
	blocks_div.appendChild(new_block);
}
function RemoveLastPhotoBlock() {
	
	var blocks_div = document.getElementById('photos_block');
	if (blocks_div.lastChild.id == 'photo_div1') return;
	blocks_div.removeChild(blocks_div.lastChild);

  	var counter = document.getElementById('photo_counter');
	var id = parseInt(counter.value);
	if (id > 1)
	counter.value = --id;
}

function AddVideoBlock() {

	var counter = document.getElementById('video_counter');
	var id = parseInt(counter.value);
	var blocks_div = document.getElementById('videos_block');
	var src = document.getElementById('video_div_phid_');
	counter.value = ++id;
	var new_block = document.createElement('DIV');
	new_block.id = 'video_div'+id;
	new_block.innerHTML = src.innerHTML.replace(/_phid_/g, id);
	blocks_div.appendChild(new_block);
}

function RemoveLastVideoBlock() {
	
	var blocks_div = document.getElementById('videos_block');
	if (blocks_div.lastChild.id == 'video_div1') return;
	blocks_div.removeChild(blocks_div.lastChild);

  	var counter = document.getElementById('video_counter');
	var id = parseInt(counter.value);
	if (id > 1)
	counter.value = --id;
}


function showPhone(listingid, default_url) {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (exc) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (ex) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				xmlhttp = false;
			}
		}
	}
	if (xmlhttp) {
		xmlhttp.open("GET", default_url+'countclick.php?type=phone&id='+listingid, true);
		xmlhttp.send(null);
	}
	document.getElementById("phoneLink"+listingid).className = "controlPhoneHide";
	document.getElementById("phoneNumber"+listingid).className = "controlPhoneShow";
}

function showFax(listingid, default_url) {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (exc) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (ex) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				xmlhttp = false;
			}
		}
	}
	if (xmlhttp) {
		xmlhttp.open("GET", default_url+'countclick.php?type=fax&id='+listingid, true);
		xmlhttp.send(null);
	}
	document.getElementById("faxLink"+listingid).className = "controlFaxHide";
	document.getElementById("faxNumber"+listingid).className = "controlFaxShow";
}

function changeVideoTo(sBase, vidThumb, vidFLV)
{
	
	getHtmlData( 'player_block', 'directory.php?base='+sBase+'&video='+vidFLV+'&thumb='+vidThumb+'&show_only=video');

}


function textCounter (field, countfield, maxlimit) {
      if (field.value.length > maxlimit) {
        field.value = field.value.substring(0, maxlimit);
      } else { 
        countfield.value = maxlimit - field.value.length;
      }
}

function addOption(selectId, val, txt) {
    var objOption = new Option(txt, val);
    document.getElementById(selectId).options.add(objOption);
}

function rewrite(text, target, validchars, separator) {
    var str = "";
    var i;
    var exp_reg = new RegExp("[" + validchars + separator + "]");
    var exp_reg_space = new RegExp("[ ]");
    text.toString();
    for (i=0; i < text.length; i++) {
        if (exp_reg.test(text.charAt(i))) {
            str = str+text.charAt(i);
        } else {
            if (exp_reg_space.test(text.charAt(i))) {
                if (str.charAt(str.length-1) != separator) {
                    str = str + separator;
                }
            }
        }
    }
    if (str.charAt(str.length-1) == separator) str = str.substr(0, str.length-1);
    document.getElementById(target).value = str.toLowerCase();
}

