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

var autoCompleteObj = {};
//Single Combo Events

function I(el)
{
	return document.getElementById(el);
}

function set_ac_flag(val) {
	acflag = val;
}

function setadvflag(value) {
	advflag = value;
}

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' : '' );
	//$('body').prepend("::" + advflag);
	//alert("test");
	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 = "#E5ECF9";
		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=\"#E5ECF9\">_</font>");

	if(value != 0)
		I(combo).style.background = "#E5ECF9";
	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(s) Selected";
		I(el+"_combo").style.background = "#E5ECF9";
	}
	else
	{
		I(el+"_combo_text").innerHTML = "Any";
		I(el+"_combo").style.background = "#FFFFFF";
	}
	
	
		I(el).value = target_val;
	I(el + "_container").blur();
	
}

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(combo_id,default_text){

	var index = 0;
	var last_index = 0;
	var tag_name;
	tag_name = I(combo_id + "_combo_text").tagName;
	while(el = I(combo_id + "_" + index + "_combo_text"))
	{
		if(el.innerHTML != 'Any')
		{
			last_index = index;
		}
		index++;
	}
	var combo_value = I(combo_id + "_" + last_index).value;
	var combo_text = I(combo_id + "_" + last_index + "_combo_text").innerHTML;

	
	combo_text = combo_text.replace(/<font color="#e5ecf9">_<\/font>/g, " ");
	combo_text = combo_text.replace(/<font color="#E5ECF9">_<\/font>/g, " ");
	combo_text = combo_text.replace(/<FONT color=#e5ecf9>_<\/FONT>/g, " ");
	
	if(combo_value > 0 && autoCompleteObj.id > 0){
	
		I(combo_id + "_confirm_label1").innerHTML = combo_text;
		I(combo_id + "_confirm_label2").innerHTML = autoCompleteObj.value;
		I(combo_id + "_confirm_opt1").value = combo_value;
		I(combo_id + "_confirm_opt2").value = autoCompleteObj.id;
		
		I(combo_id + "_confirm_box").style.display = 'block';
	}
	else if ( combo_value > 0  || combo_value.length > 0) {
		
		setadvflag(0);
		//hide_adv_contaier(combo_id + '_container');

		if(tag_name == 'DIV' || tag_name == "div")
		{
			I(combo_id + "_combo_text").innerHTML = combo_text;
			I(combo_id + "_combo").style.background = '#E5ECF9';
		}
		else
		{
		
			I(combo_id + "_combo_text").value = combo_text.replace(/&amp;/g,'&');
		
		}
			
		I(combo_id).value = combo_value;		

		
		//var test_combo_id = combo_id + "_cat_button";
		//setTimeout("I('test_combo_id').click()",1000)
				
		//alert("test");
		//var test_combo_id = combo_id + '_container';
		//setTimeout("hide_adv_contaier("+test_combo_id+")",10000)
		
		//I(combo_id + "_cat_button").click();
		hide_adv_contaier(combo_id + '_container');
		set_ac_flag(0);
		hide_ac();
		//I(combo_id + "_cat_button").blur();
		I(combo_id + "_container").blur();

		
		//alert(this.src);
	}
	else if ( autoCompleteObj.id > 0 ) {
		
		setadvflag(0);
		hide_adv_contaier(combo_id + '_container');
		
		if(tag_name == 'DIV' || tag_name == "div")
		{
			I(combo_id + "_combo_text").innerHTML = autoCompleteObj.value;
			I(combo_id + "_combo").style.background = '#E5ECF9';
		}
		else
		{
			I(combo_id + "_combo_text").value = autoCompleteObj.value;
		}
		
		I(combo_id).value = autoCompleteObj.id;
		
		set_ac_flag(0);
		hide_ac();
		if(autoCompleteObj)
		{
			autoCompleteObj = {};
			var auto = I(combo_id + "_auto_c").firstChild;
			auto.value = "Type Your Location Here...";
			auto.style.color = "#808080";
		}
		I(combo_id + "_auto_c").firstChild.blur();
		/* I(combo_id + "_container").blur();
		I(combo_id + "_cat_button").blur(); */
	}
	else
	{ 
		setadvflag(0);
		hide_adv_contaier(combo_id + '_container');
		if(tag_name == 'DIV' || tag_name == "div")
		{
			//if(!default_text)default_text = "Any";
			I(combo_id + "_combo_text").innerHTML = "Any";
		}
		else
		{
			if(!default_text)default_text = "Not Selected";
			I(combo_id + "_combo_text").value = default_text;
		}
		
		I(combo_id).value = 0;
		if(tag_name == 'DIV' || tag_name == "div")
		{
			I(combo_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(combo_id) {
	el_checkbox = I(combo_id + "_confirm_opt1");
	var tag_name;
	tag_name = I(combo_id + "_combo_text").tagName;
	if(el_checkbox.checked)
	{		
		if(tag_name == "DIV" || tag_name == "div")
			I(combo_id + "_combo_text").innerHTML = I(combo_id+"_confirm_label1").innerHTML;
		else
			I(combo_id + "_combo_text").value = I(combo_id+"_confirm_label1").innerHTML;
		
		I(combo_id).value = el_checkbox.value;
	}
	else
	{
		if(tag_name == "DIV" || tag_name == "div")
			I(combo_id + "_combo_text").innerHTML = I(combo_id + "_confirm_label2").innerHTML;
		else
			I(combo_id + "_combo_text").value = I(combo_id + "_confirm_label2").innerHTML;
		I(combo_id).value = I(combo_id + "_confirm_opt2").value;
	}
	if(tag_name == "DIV" || tag_name == "div")
		I(combo_id + "_combo").style.background = '#E5ECF9';
	I(combo_id + "_confirm_box").style.display = 'none';
	
	setadvflag(0);
	hide_adv_contaier(combo_id + '_container');
	if(autoCompleteObj)
	{
		autoCompleteObj = {};
		var auto = I(combo_id + "_auto_c").firstChild;
		auto.value = "Type Your Location Here...";
		auto.style.color = "#808080";
	}
}

function FocusAdvContainer(combo_id) {
	I(combo_id + "_container").focus();
}

function get_child_cat( el_select,webpath ) {

	var target_div = el_select.name;
		var id_array = new Array();
		
        id_array[0] = target_div.split(/_[0-9]/)[0];
		if(document.all)
			id_array[1] = target_div.split(/[a-z0-9_]*_/);
		else
			id_array[1] = target_div.split(/[a-z0-9_]*_/)[1];
		
        var id = parseInt(id_array[1]) + 1;
        target_div = 'category_'+id_array[0]+'_'+ id;
		target_select = 'cat_'+id;
		var load_image = "";
		if(id == 3 || id == 6)
			load_image += "<br /><br /><br />";
        load_image += "<img style='float:left;margin-left:55px;' src='"+webpath+"Images/loading1.gif' />";
			
        I(target_div).innerHTML = load_image;
		 
		 ajaxpostpage(webpath+"classifieds/includes/combo/child_locations.php?cat_id="+el_select.value+"&target_id="+id+"&pre="+id_array[0]+"",target_div);

}

function input_mousedown(el)
{
	el.focus();
	//alert(autoCompleteObj.value);
	if(el.value == "Type Your Location Here..." || !autoCompleteObj.value)
	{
	   el.value = "";
	   el.style.color = "#000000"; 
	}
}

function input_blur(el,combo_id)
{
	if(el.value == "")
	{
		el.value = "Type Your Location Here...";
		el.style.color = "#808080";
		
	}
	//set_ac_flag(0);
	hide_ac();
	hide_adv_contaier(combo_id + '_container');
}



//Common Events

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


//rapper functions
function liw(el)
{
	change_bg_color(el,"#FFFFFF");
}

function lib(el)
{
	change_bg_color(el,"#E5ECF9");
	setflag(1);
}

function liv(el,id,val)
{
	set_cm_value( id + '_combo', id + '_combo_text', id + '_container',id,el.innerHTML,val);
}

function hc(id)
{
	hide_contaier( id + '_container')
}


// 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 = '#E5ECF9';
				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);
	
	} */
	if(I('type_n'))
	{
		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;
	    };
	}
	
	if(I("purpose_n"))
	{
		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 ;	
			}
		};
	}
}






