function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function slideSwitch() {
    var $active = $('#slideshow IMG.active');
    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow IMG:first');
    $active.addClass('last-active');
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 2000, function() {
            $active.removeClass('active last-active');
        });
}

$(function() {
    setInterval( "slideSwitch()", 10000 );
});

function getCities(source,target,city) {
	$("#"+target).empty();
	$("#"+target).append('<option value="">...Wait...</option>');
	var county=$("#"+source).val();
     JsHttpRequest.query(
            '/ajax/getCities.php', // backend
            {
                'id': county
            },
            function(result, errors) {
            	if(result) {
            		$("#"+target).empty();
            		$("#"+target).append('<option value="">Select City</option>');
            		for(i=0;i<result['str'].length;i++) {
            			$("#"+target).append('<option value="'+result['str'][i]['cityName']+'" ' + ( (city!='' && city == result['str'][i]['cityCode']) ? 'selected="selected"' : '') + '>'+result['str'][i]['cityName']+'</option>');
            		}
            		//if (city!="") $("select[name='CITY[]']").val(city);
           		}
            },
            true  // do not disable caching
     );
}

function checkForm() {
	if($("select[name='CITY[]']").val()=="") {
		alert("Select any city and try again.");
		return false;
	}
	return true;
}

function expand(node) {
	var src=node.firstChild.getAttribute("src");
	var block=node.parentNode.getElementsByTagName("div");
	//var status="";
	//var num=node.getAttribute("name");
	if(src.search("off")>0) {
		node.firstChild.setAttribute("src","images/tree-on.gif");
		block[0].style.display="block";
		status=1;
	}
	else {
		node.firstChild.setAttribute("src","images/tree-off.gif");
		block[0].style.display="none";
		status=0;
	}
	/*
     JsHttpRequest.query(
            '/ajax/setMenu.php', // backend
            {
                'status': status,
                'block': num
            },
            function(result, errors) {
            	//alert(result['str']);
            },
            true  // do not disable caching
     );
     */
}

function cma_key() {
	if((event.keyCode >= 48)&&(event.keyCode <= 57)) return true;
	return false;
}

function sform_Sub() {
	if(document.sform.elements[1].name!='PHPSESSID') {
		if((document.sform.elements[1].value=='on'&&document.sform.elements[2].value=='on'&&document.sform.elements[3].value=='on')||(document.sform.elements[4].value==''&&document.sform.elements[6].value=='')) {
			alert('You have to select Property Type and City or Property Type and Zip code.Please try again.');
			return false;
		}
	}
	else {
		if((document.sform.elements[2].value=='on'&&document.sform.elements[3].value=='on'&&document.sform.elements[4].value=='on')||(document.sform.elements[5].value==''&&document.sform.elements[7].value=='')) {
			alert('You have to select Property Type and City or Property Type and Zip code.Please try again.');
			return false;
		}
	}
	return true;
}

function sform_Sub1() {
	if(document.sform.elements[1].name!='PHPSESSID') {
		if(document.sform.elements[1].value==''&&document.sform.elements[2].value=='') {
			alert('You have to select City or Zip code.Please try again.');
			return false;
		}
	}
	else {
		if(document.sform.elements[2].value==''&&document.sform.elements[3].value=='') {
			alert('You have to select City or Zip code.Please try again.');
			return false;
		}
	}
	return true;
}

function replaceImg(num) {
	var current=$("#statusA").text();
	stepcarousel.stepBy('gallery', num-current);
}

function submitQuick() {
	if (checkForm()) $("form[name='quick']").submit();
}

function changeStyle(node) {
	var value=$(node).val();
	var options='<option value="">No Preference</opton>';
	var opts = '';
	switch (value) {
		case "RES": case 'Residential':
			opts = options;
			options+='<option value="SFR">Single Family Home</opton>';
			options+='<option value="CONDO">Condo / Town House</opton>';
			opts += '<option value="Single Family">Single Family</option>';
			opts += '<option value="Condominium">Condominium</option>';
			opts += '<option value="Townhouse">Townhouse</option>';
			opts += '<option value="Loft">Loft</option>';
			break;
		case "LSE": case 'Residential Lease':
			opts = options;
			options+='<option value="SFR">Single Family Home</opton>';
			options+='<option value="CONDO">Condo / Town House</opton>';
			options+='<option value="APT">Apartment</opton>';
			options+='<option value="LOFT">Loft</opton>';
			opts += '<option value="Single Family">Single Family</option>';
			opts += '<option value="Condominium">Condominium</option>';
			opts += '<option value="Townhouse">Townhouse</option>';
			opts += '<option value="Apartment">Apartment</option>';
			break;
	}
	$("select[name='PROPERTY_TYPE']").empty().html(options);
	$('select[name=pt]').empty().html(opts);
}

function number_format(number) {
	number=number.toString().split('');
	var result="";
	var len=number.length;
    for (var i = (len - 1); i >= 0; i--) {
    	result+=number[i];
        if ((len-i) % 3 == 0 && i!=0) result += ',';
    }
    return result.split('').reverse().join('');
}
