var flag = 0;
var advflag = 0;
var acflag = 0;


//Single Combo Events

function setflag( value ) {
	flag = value;
}

function hide_stext( el ) {
	if(!el)
		I('stext').style.display = 'none';
	else
		el.style.display = 'none';
}

function show_stext( el ) {
	if( !el )
		I('stext').style.display = 'block';
	else
		el.style.display = 'block';
}

function show_contaier( el ) {
	el.style.display = '';
	el.focus();
}

function hide_contaier( div ) {
	//alert(div);
	//$('body').prepend(flag + ' test ');
	if(flag == 0) // o= mouse is out , 1 = mouse is in the div
	{
		
		I(div).style.display = "none";
		if(I('stext'))
			hide_stext();
		
		//$('body').prepend('test');
		/* document.body.onclick = function(){$('body').prepend('test')};
		document.body.onclick();
		document.body.onclick = ''; */
	}
}

function toggle_contaier( div ) {
	var el = I(div);
	//el.style.display = (el.style.display != 'none' ? 'none' : '' );
	if(el.style.display != 'none')
	{

		el.style.display = 'none';
	}
	else
	{	
		
		el.style.display = '';
		//I('price_container').focus();
		//setTimeout("I('price_container').focus()",1);	
	} 
}

function change_bg_color( el, clr ) {
	el.style.background = clr;
}

function check_input_range( val1, val2, min_length, error_msg ) {
	//var val1 = I(el_val1).value;  
    //var val2 = I(el_val2).value; 
	
	el_stext = I("stext");
	if(isNaN(val1) || isNaN(val2))
	{   								 
		el_stext.innerHTML = "<font color=\"red\">Values must contain numbers only</font>";  
		return false;
	}
	else if(val1 == '' || val2 == '')
	{    el_stext.innerHTML = "<font color=\"red\">Cannot Accept Empty Field</font>";    
		return false;
	}
	else
	{
		val1 = parseInt(val1);
		val2 = parseInt(val2);
			
		
		 if(val2 < val1)
		{
			el_stext.innerHTML = "<font color=\"red\">Invalid Range</font>"; 	
			return false;
		}
		else if(val1 < min_length || val2 < min_length)
		{
			el_stext.innerHTML = "<font color=\"red\">" + error_msg + "</font>";
			return false;
		   
		}
	}
	
	
	return true;

}

function add_select_option( select_div, input1_div, input2_div, max_value, error_msg ) {
	el_select = I(select_div);
	el_input1 = I(input1_div);
	el_input2 = I(input2_div);
	
	
	if(check_input_range(el_input1.value, el_input2.value, max_value, error_msg))
	{
		var new_select_text = el_input1.value + " to " + el_input2.value;
		var new_select_val = el_input1.value + "_" + el_input2.value;
		

		var option = document.createElement("option");
		option.innerHTML = new_select_text;
		option.value = new_select_val;
		el_select.appendChild(option);
		
		option.selected = "selected";
		var combo_div = select_div + "_combo_text";
		I(combo_div).innerHTML = new_select_text;
		I(select_div + "_combo").style.background = "#fbfaee";
		var container_div = select_div + "_container";
		hide_contaier(container_div);
		setflag(0);
			
	}
	else
	{
		
		display_stext(select_div);
		setflag(1);
	}
	

}

function display_stext(select_div, el) {
	
		var container_div = I(select_div + "_container");
		var p_top = container_div.offsetTop + container_div.offsetHeight + 10;
		var p_left = container_div.offsetLeft;
		
		el_stext = I("stext");
		if(el) {
				el_stext.innerHTML = showPriceText(el);
		}
		
		/* $("#stext").css({"border":"2px solid #585858","position":"absolute","top":p_top,"left":p_left,"display":"block","width":"207px","height":"18px","background-color":"#F0F0F0"});  */
		var s = I("stext"); 
		s.style.border = "2px solid #585858";
		s.style.position = "absolute";
		s.style.top = p_top + "px";
		s.style.left = p_left + "px";
		s.style.display = "block";
		s.style.width = "207px";
		s.style.height = "18px";
		s.style.background = "#F0F0F0";
}

function set_cm_value( combo, combo_text, container, select, text, value ) {
	
	setflag(0);
	hide_contaier(container);
	I(container).onblur();
	var browser=navigator.appName;
	if (browser != "Microsoft Internet Explorer")
	{
		I(combo).focus();
	}
	var el_select = I(select);
	el_select.value = value;
	I(combo_text).innerHTML = text.replace(/ /g, "<font color='#fbfaee'>_</font>");
	
	if(value != 0)
		I(combo).style.background = "#fbfaee";
	else
		I(combo).style.background = "#FFFFFF";
	
	el_select.onchange();
	
}


//Multi Combo Events

function select_option( el ) {

	var el_opt = I(el);
	
	if(el_opt.checked)
		el_opt.checked  = false;
	else
		el_opt.checked  = true;
}

function multi_fill_combo( el, value ) {
	
	// var el_select = I(el);
	// var old_val = el_select.value;
	// var new_val = old_val + "_" + value;
	// el_select.value = new_val;
}

function multi_combo_btn( el ) {
	
	var el_opt;
	var index = 1;
	var select_count = 0;
	var target_val = '';
	var opt_name = el + "_opt_" + index;  
	while( el_opt = I(opt_name) )
	{
		if(el_opt.checked == true)
		{
			target_val += el_opt.value + ':';
			select_count ++;
		
		}
		
		index++;
		opt_name = el + "_opt_" + index;  
	}
	if(select_count != 0)
	{
		I(el+"_combo_text").innerHTML = select_count + " item selected";
		I(el+"_combo").style.background = "#fbfaee";
	}
	else
	{
		I(el+"_combo_text").innerHTML = "Any";
		I(el+"_combo").style.background = "#FFFFFF";
	}
	
	
		I(el).value = target_val;
	
	
}

function multi_hide_container( el ) {
	
	if(flag == 0) // o= mouse is out , 1 = mouse is in the div
	{
		I( el + "_container" ).style.display = "none";
		multi_combo_btn( el );
		
		document.body.onclick = function(){};
		document.body.onclick();
		document.body.onclick = '';
	}
}



//Advance Comno Events

function set_adv_confirm(){

	
	var index = 0;
	var last_index = 0;
	
	while(el = I("cat_" + index + "_combo_text"))
	{
		if(el.innerHTML != 'Any')
		{
			last_index = index;
		}
		index++;
	}
	var combo_value = I("cat_" + last_index).value;
	var combo_text = I("cat_" + last_index + "_combo_text").innerHTML;
	
	if(combo_value > 0 && autoCompleteObj.id > 0){
	
		I("confirm_label1").innerHTML = combo_text;
		I("confirm_label2").innerHTML = I("input").value;
		I("confirm_opt1").value 		= combo_value;
		I("confirm_opt2").value 		= autoCompleteObj.id;
		
		I("confirm_box").style.display = 'block';
	}
	else if ( combo_value > 0 ) {
		
		setadvflag(0);
		hide_adv_contaier('cat_id_container');
		
		I("cat_id_combo_text").innerHTML = combo_text;
		I("cat_id").value = combo_value;
		
		I("cat_id_combo").style.background = '#fbfaee';
		set_ac_flag(0);
		hide_ac();
	}
	else if ( autoCompleteObj.id > 0 ) {
		
		setadvflag(0);
		hide_adv_contaier('cat_id_container');
		
		I("cat_id_combo_text").innerHTML = I("input").value;
		I("cat_id").value = autoCompleteObj.id;
		
		I("cat_id_combo").style.background = '#fbfaee';
		set_ac_flag(0);
		hide_ac();
	}
	else
	{ 
		
		setadvflag(0);
		hide_adv_contaier('cat_id_container');
		I("cat_id_combo_text").innerHTML = "Any";
		I("cat_id").value = 0;
		I("cat_id_combo").style.background = '#FFFFFF';
		set_ac_flag(0);
		hide_ac();
	}
	
	
	
	
}

function hide_ac() {
	//$('body').prepend(acflag);
	if(acflag == 0)
	{
		//alert(acflag);
		//$('body').prepend(acflag);
		el = I("jqac_id");
		if(el)
			el.style.display = 'none';
	}
}


function set_adv_value() {
	el_checkbox = I("confirm_opt1");
	if(el_checkbox.checked)
	{
		I("cat_id_combo_text").innerHTML = I("confirm_label1").innerHTML;
		I("cat_id").value = el_checkbox.value;
	}
	else
	{
		I("cat_id_combo_text").innerHTML = I("confirm_label2").innerHTML;
		I("cat_id").value = I("confirm_opt2").value;
	}
	I("cat_id_combo").style.background = '#fbfaee';
	I("confirm_box").style.display = 'none';
	
	setadvflag(0);
	hide_adv_contaier('cat_id_container');
}

function FocusAdvContainer() {
	I("cat_id_container").focus();
}

function get_child_cat( el_select ) { 
	var target_div = el_select.name;
        var id = target_div.split('_');
        id = parseInt(id[1]) + 1;
        
        target_div = 'category_'+ id;
        target_select = 'cat_'+id;
        var load_image = "<img style='margin-left:55px;' src='" + this_domain + "/images/common/loading1.gif' />";
        I(target_div).innerHTML = load_image;
        
        $.get(this_domain + '/modules/search/child_locations.php', {cat_id:el_select.value,target_id:id}, 
        function(str) 
        {   
                I(target_div).innerHTML = str;
               // $("#"+target_select).ccombo({"type":"normal","parent":"advance","width":"80","dwidth":"180","ddheight":"370"});
        });        
}



//Common Events

function hide_adv_contaier( div ) {
	//alert(div);
	//alert(advflag);
	//$('body').prepend(advflag + 'test');
	if(advflag == 0) // o= mouse is out , 1 = mouse is in the div
	{
		I(div).style.display = "none";
		//I("cat_id_combo").focus();
	}
}




// Dynamic Events---------------------------------------------------

function last_events()
{
	
	$('input.complete').autocomplete({ ajax_get : get_ajax_suggs, callback: print_sugg, multi: true});
	
	I('input').onmousedown = function(){
		
		this.focus();
		if(this.value == "Type Your Location Here...")
		{
		   this.value = "";
		   this.style.color = "#000000"; 
		}
    };
	
	I('input').onblur = function(){
		if(this.value == "")
		{
			this.value = "Type Your Location Here...";
			this.style.color = "#808080";
			
		}
		//set_ac_flag(0);
		hide_ac();
		hide_adv_contaier('cat_id_container');
	};
	
	
	/* if(document.all)
	{
		var i = 0
		while(document.all.c_li[i] != null)
		{
			document.all.c_li[i].onmouseover = function(){		
				this.style.background = '#fbfaee';
				setflag(1);
			}
			document.all.c_li[i].onmouseout = function(){
				this.style.background = '#FFFFFF';
			}
			i++;
		}
	}	 */
	
	/* I('jqac-menu').onmouseover = function(){
	
		setadvflag(1);
	
	}
	I('jqac-menu').onmouseout = function(){
	
		setadvflag(0);
	
	} */
	I('type_n').onchange = function(){
		
		
        if(this.value == 1)
        {   
			
            I("subtype_container").innerHTML = I('subtypes_area_res').innerHTML;    
			I("subtype_container").style.display = "block" ;
			if(I("beds_container"))
			{
				I("beds_container").innerHTML = I('beds_area').innerHTML; 
				I("beds_container").style.display = "block";
			}
			if(I("baths_container"))
			{
				I("baths_container").innerHTML = I('baths_area').innerHTML; 
				I("baths_container").style.display = "block";
			}
				
        }
        else if(this.value == 2 )
        {
            I("subtype_container").innerHTML = I('subtypes_area_com').innerHTML;  
			I("subtype_container").style.display = "block";		
            if(I("beds_container"))
			{
				I("beds_container").style.display = "none";
			}
            if(I("baths_container"))
			{
				I("baths_container").style.display = "none";		
			}
        }
        else if(this.value == 0)
        {
            I("subtype_container").innerHTML = "";
			I("subtype_container").style.display = "none";
            
			if(I("beds_container"))
			{
				I("beds_container").style.display = "none";
			}
            
			if(I("baths_container"))
			{
				I("baths_container").style.display = "none";	
			}
        }
                
        return false;
    };
	
	I("purpose_n").onchange = function(){
		
		/* if(this.value == 1) {
		
			I("price_main").innerHTML = I("price_buy").innerHTML ; 
		
		} else if (this.value == 2) {
		
			I("price_main").innerHTML = I("price_rent").innerHTML ; 
		
		} else if(this.value == 3) {
		
			I("price_main").innerHTML = I("price_wanted").innerHTML ; 
		}
		else
		{
			I("price_main").innerHTML = I("price_buy").innerHTML ;	
		}
	 */
	
	};
}





