var input_id=0;

function NotInterested(real_estate_id,search_id)
{
	$.ajax({
	  type: "GET",
	  url: "interested_not.php?real_estate_id="+real_estate_id+"&search_id="+search_id,
	  success: function(data,text) {
		  alert("This propety has been set NOT interested");
	  }
	});
}

function pageNumber(value,url)
{
	document.location.href=url.replace(/#pag#/,value);
}

function loadAjax($url)
{
	$("#ajax").load($url);	
}

function checkAllAuto(id)
{
	  $("#select_"+id+" INPUT[type='checkbox']").attr('checked', $('#checkAllAuto_'+id).is(':checked'));   
}


function Trakers(id,table)
{
	var t=scrollTop();
	$("#divTrakes").css("top",t+50+'px');
	$("#divTrakes").fadeIn("slow");
	$("#divTrakes").load("ajax/trakers.php?id="+id+"&table="+table);		
}

function Close(div)
{
	$("#"+div).fadeOut("slow");
	$("#"+div).html('Loading...');
}

function inputTypeCheckBox(table,field,value,id,other)
{
	input_id++
	var inp='t'+input_id;
	var chk="";
	if (value=="1")
	{
		chk=' checked="checked"';
	}
	
	document.write('<input type="checkbox" id="'+inp+'_id" name="'+inp+'_id" class="no_width" onclick="clickCheckBox(\''+table+'\',\''+field+'\',\''+id+'\',\''+inp+'_id\')" tabindex="'+input_id+chk+'" '+other+'>');
}

function clickCheckBox(table, field, id , obj)
{
	var val=0
	if($('#'+obj).is(':checked'))
	{
		val=1
	}
	$.ajax({
	  type: "GET",
	  url: "ajax/inputTypeText.php?table="+table+"&field="+field+"&id="+id+"&value="+val
	});
}

function inputTypeText(table,field,value,id,other)
{
	input_id++
	var inp='t'+input_id;
	document.write('<input type="text" id="'+inp+'_id" name="'+inp+'_id" value="'+value+'" class="input" onchange="inputTypeTextBlur(\''+table+'\',\''+field+'\',\''+id+'\',\''+inp+'_id\')" tabindex="'+input_id+'" '+other+'>');
}

function inputTypeTextBlur(table, field, id , obj)
{
	
	var val=$('#'+obj).val();
	$.ajax({
	  type: "GET",
	  url: "ajax/inputTypeText.php?table="+table+"&field="+field+"&id="+id+"&value="+encodeURIComponent( val )
	});
}

function hasMany(first,second,first_id,status)
{
	input_id++
	var inp='t'+input_id;
	document.write('<input type="text" id="'+inp+'_id" name="'+inp+'_id" value="" class="input" tabindex="'+input_id+'">'+
				   '<div id="'+inp+'_show" class="autocomplete_results"></div>');
	
	var url="ajax/hasMany.php?first="+first+"&second="+second+"&first_id="+first_id+"&status="+status;
	$('#'+inp+'_id').autocomplete(url, {
		width: 320,
		max: 4,
		selectFirst:false,
		highlight: false,
		scroll: true,
		delay:0,
		scrollHeight: 300,
		formatItem: function(data, i, n, value) {
			return value.split("#")[1];
		}
	});
	
	$('#'+inp+'_id').result(function(event, data, formatted) {					   
	     $('#'+inp+'_id').attr( "value", "" );	 		 
		 
		 if (!data) { if(formatted==""){ return; };  var name=formatted; var id=0; if ( ConfirmQuestion() ){ } else {  return; } 
		 }else{ var name=formatted.split("#")[1]; var id=formatted.split("#")[0]; }	 
		 
		 $("#"+inp+"_show").load(url+"&id="+id+"&name="+escape(name)+"&action=add&show=1");		 

	})
	
	$("#"+inp+"_show").load(url+"&show=1");
}

function SearchFilter(table,redirect)
{
	var t=scrollTop();
	$("#divTrakes").css("top",t+50+'px');
	$("#divTrakes").fadeIn("slow");
	$("#divTrakes").load("ajax/searchFilter.php?table="+table+"&redirect="+redirect);		
}function SearchFilter2(table,redirect){	var t=scrollTop();	$("#divTrakes").css("top",t+50+'px');	$("#divTrakes").fadeIn("slow");	$("#divTrakes").load("ajax/searchFilter2.php?table="+table+"&redirect="+redirect);		}

function addBelongTo(first,second,first_id,id,callback_function)
{
	 
	 
	 var url="ajax/belongTo.php?first="+first+"&second="+second+"&status="+status+"&first_id="+first_id;
	 $("#auto_"+first_id+'_'+second).load(url+"&id="+id+"&action=add&show=1");
	 if ( callback_function ) { eval(callback_function);}
	 Close('divTrakes');
}
function searchBelongTo(first,second,first_id,status,callback_function)
{

	var url="ajax/belongTo.php?first="+first+"&second="+second+"&status="+status+"&first_id="+first_id+'&callback_function='+callback_function;
	
	var t=scrollTop();
	$("#divTrakes").css("top",t+50+'px');
	$("#divTrakes").fadeIn("slow");
	$("#divTrakes").load(url+'&list');		
}

function deleteBelongToForm(second)
{
	$("#autocomplete_"+second).html('');
	$("#search_"+second+"_id").attr('value',0);
}
function addBelongToForm(second,value,id)
{
	$("#autocomplete_"+second).html('<a href="javascript:;" onclick="deleteBelongToForm(\''+second+'\')">'+value+' <strong>x</strong></a>');
	$("#search_"+second+"_id").attr('value',id);
}

function belongTo(first,second,first_id,status,options)
{
	var settings = {
		callback_function : ""
	};
	
    if(options) {
		jQuery.extend(settings, options);
	};

	
	input_id++
	var inp='t'+input_id;
	document.write('<div class="belongTo"><input type="text" id="'+inp+'_id" name="'+inp+'_id" value="" tabindex="'+input_id+'"><img src="images/search.gif"  onclick="searchBelongTo(\''+first+'\',\''+second+'\','+first_id+',\''+status+'\',&quot;'+(settings.callback_function).toString()+'&quot;)"></div>'+
				   '<div id="auto_'+first_id+'_'+second+'" class="autocomplete_results"></div>');
	
	var url="ajax/belongTo.php?first="+first+"&second="+second+"&status="+status+"&first_id="+first_id;
	$('#'+inp+'_id').autocomplete(url, {
		width: 320,
		max: 4,
		selectFirst:false,
		highlight: false,
		scroll: true,
		delay:0,
		scrollHeight: 300,
		formatItem: function(data, i, n, value) {
			return value.split("#")[1];
		}
	});
	
	$('#'+inp+"_id").result(function(event, data, formatted) {							   
		 $('#'+inp+"_id").attr( "value", "" );	
		 
		 if (!data) { if(formatted==""){ return; };  var name=formatted; var id=0; if ( ConfirmQuestion() ){ } else {  return; } 
		 }else{ var name=formatted.split("#")[1]; var id=formatted.split("#")[0]; }	 
		 $("#auto_"+first_id+'_'+second).load(url+"&id="+id+"&name="+escape(name)+"&action=add&show=1");
		 
		 if ( settings.callback_function ) { eval(settings.callback_function);}
		 
	})
	
	if ( settings.callback_function) { eval(settings.callback_function);}
	$("#auto_"+first_id+'_'+second).load(url+"&show=1");
}

function changeCity(obj)
{
	var city_id = obj.value;
	$("#zones").load("/ajax/zones.php?city_id="+city_id);
}

function changeDistrict(id)
{
	$("#all_location").load('changeDistrict.php?id='+id);
}

function changeContact(id)
{
	$("#all_contact_attr").load('contacts_choose.php?id='+id);
}
function addTypeHasBelongToMany(first,second,pivot,first_id,inp)
{
	var url="ajax/hasBelongToMany.php?first="+first+"&second="+second+"&pivot="+pivot+"&first_id="+first_id;	
	$("#"+inp+"_show").load(url+"&websites_type_id="+$("#websites_type_id").val()+"&action=add_type&show=1");
	Close('divTrakes');
}
function addHasBelongToMany(first,second,pivot,first_id,id,inp)
{
	if ($('#tr_'+pivot+'_'+id).is('.selected')) {
	}else{
		var url="ajax/hasBelongToMany.php?first="+first+"&second="+second+"&pivot="+pivot+"&first_id="+first_id;
		$("#"+inp+"_show").load(url+"&id="+id+"&action=add&show=1");
		$('#tr_'+pivot+'_'+id).addClass('selected');
	}
}
function searchHasBelongToMany(first,second,pivot,first_id,status,inp)
{
	var url="ajax/hasBelongToMany.php?first="+first+"&second="+second+"&pivot="+pivot+"&status="+status+"&first_id="+first_id+"&inp="+inp;
	
	var t=scrollTop();
	$("#divTrakes").css("top",t+50+'px');
	$("#divTrakes").fadeIn("slow");
	$("#divTrakes").load(url+'&list');	
}

function hasBelongToMany(first,second,pivot,first_id,status)
{
	input_id++
	var inp='t'+input_id;
	
	document.write('<div class="belongTo"><input type="text" id="'+inp+'_id" name="'+inp+'_id" value="" tabindex="'+input_id+'"><img src="images/search.gif"  onclick="searchHasBelongToMany(\''+first+'\',\''+second+'\',\''+pivot+'\','+first_id+',\''+status+'\',\''+inp+'\')"></div>'+
				   '<div id="'+inp+'_show" class="autocomplete_results"></div>');
	
	var url="ajax/hasBelongToMany.php?first="+first+"&second="+second+"&pivot="+pivot+"&status="+status+"&first_id="+first_id;
	$('#'+inp+'_id').autocomplete(url, {
		width: 320,
		max: 4,
		selectFirst:false,
		highlight: false,
		scroll: true,
		delay:0,
		scrollHeight: 300,
		formatItem: function(data, i, n, value) {
			return value.split("#")[1];
		}
	});
	
	$('#'+inp+'_id').result(function(event, data, formatted) {					   
		 $('#'+inp+'_id').attr( "value", "" );	
		 
		 if (!data) { if(formatted==""){ return; }; var name=formatted; var id=0; if ( ConfirmQuestion() ){ } else {  return; } 
		 }else{ var name=formatted.split("#")[1]; var id=formatted.split("#")[0]; }	 
		 $("#"+inp+"_show").load(url+"&id="+id+"&name="+escape(name)+"&action=add&show=1");
		 
	})
	
	$("#"+inp+"_show").load(url+"&show=1");
}



function ConfirmQuestion(obj)
{
	return confirm('This name does\'t exist. Do you want to add it?')?true:false;
}

function DoSubmit()
{
	if (Check())
	{
		document.formular.submit();
	}
}

function ObjectSearch(obj)
{
	 var offset = $(obj).offset();
	 
	 $("#add").css("left",offset.left-210);
	 $("#add").css("top",offset.top+15);
	 
	 $("#add").toggle();
	 
	 $("#add INPUT[@name=name][type='text']").focus();
	 
	
	 
}

function ObjectAdd(obj)
{
	 var offset = $(obj).offset();
	 $("#add").css("left",offset.left-270);
	 $("#add").css("top",offset.top+15);
	 
	 $("#add").toggle();
	 $("#add INPUT[@name=name][type='text']").focus();
	 /*$("#add").click(function(e){
		var x = e.pageX - this.offsetLeft;
		var y = e.pageY - this.offsetTop;
		 alert(x +', '+ y);
	 });*/
}

function Delete()
{
	return confirm('Are you sure you what to delete this?')?true:false;
}



function DeleteRow(obiect,table,id)
{
	if (Delete())
	{
		$.ajax({
		  type: "GET",
		  url: "delete.php?table="+table+"&id="+id,
		  success: function(data,text) {
			 if (data!="")
			 {
			 	alert("You can not delete this. This is already linked to:\n"+data);
			 }else{
				 $("#"+obiect).fadeOut("fast");	 
			 }
		  }
		});
		
	}
	return false;	
}

function ObjectDeleteRedirect(obiect,url)
{
	if (Delete())
	{
		document.location.href=url;
	}
}

function ObjectDelete(obiect,url)
{
	if (Delete())
	{
		$.ajax({
		  type: "GET",
		  url: url
		});
		$("#"+obiect).fadeOut("fast");	 
	}
	return false;	
}

function ObjectRestore(obiect,url)
{
	$.ajax({
	  type: "GET",
	  url: url
	});
	$("#"+obiect).fadeOut("fast");	 
	return false;	
}

function Yahoo()
{
	window.location=encodeURI('ymsgr:im?msg='+(window.location.href));
}


//---------------------send interested		
function SendInterested(){
	if (CheckFormInterested()){
		$.ajax({
		  type: "POST",
		  url: "interested.php",
		  data: $('#sendemail').serialize(),  	
		  success: function(data,text) {
			$("#box_email").html(data);
		  }
		});
		return false;
	}	 
}

function CheckFormInterested(){
	
	if( $("#first_name").attr("value") == "" )
	{
		alert( 'Please fill in First name.' );
		$('#first_name').focus();
		return false;
	}
	if( $("#last_name").attr("value") == "" )
	{
		alert( 'Please fill in Last name.' );
		$('#last_name').focus();
		return false;
	}
	if( $('#email').attr("value") == "" )
	{
		alert( 'Nu ati completat campul Email' );
		$('#email').focus();
		return false;
	}
	var temp= new String($('#email').attr("value"));
	var len=temp.length - 3;
	if(temp=="" || temp.indexOf("@")<0 || temp.indexOf(".")<0 || temp.lastIndexOf(".")> len || temp.length-temp.lastIndexOf(".")>5)
	{
		alert("Va rugam introduceti o adresa de email corecta!");
	//	$('#email').attr("value") = "";
		$('#email').focus();
		return false;
	}
	
	if( $("#phone").attr("value") == "" )
	{
		alert( 'Please fill in Phone.' );
		$('#phone').focus();
		return false;
	}
	
	return true;
}

//---------------------send email		
function SendEmail(url){
	if (CheckFormEmail()){			//alert($("#sendemail input[name=url]").val());		if (loc_url!="") {$("#sendemail input[name=url]").val("http://www.idromania.ro/search.php?"+loc_url);			$("#sendemail textarea[name=lnk]").val("http://www.idromania.ro/search.php?"+loc_url);		}		//alert($("#sendemail input[name=url]").val());
		$.ajax({
		  type: "POST",
		  url: url,
		  data: $('#sendemail').serialize(),  	
		  success: function(data,text) {
			$("#box_email").html(data);
		  }
		});
		return false;
	}	 
}function SendEmail2(url){	if (CheckFormEmail()){			//alert($("#sendemail input[name=url]").val());									//alert($("#sendemail input[name=url]").val());		$.ajax({		  type: "POST",		  url: url,		  data: $('#sendemail').serialize(),  			  success: function(data,text) {			$("#box_email").html(data);		  }		});		return false;	}	 }

function CheckFormEmail(){
	
	
	if( $('#email').attr("value") == "" )
	{
		alert( 'Nu ati completat campul Email' );
		$('#email').focus();
		return false;
	}
	var temp= new String($('#email').attr("value"));
	var len=temp.length - 3;
	if(temp=="" || temp.indexOf("@")<0 || temp.indexOf(".")<0 || temp.lastIndexOf(".")> len || temp.length-temp.lastIndexOf(".")>5)
	{
		alert("Va rugam introduceti o adresa de email corecta!");
	//	$('#email').attr("value") = "";
		$('#email').focus();
		return false;
	}
	
	
	return true;
}


function OpenBox(url){
	 $("#box_email").load(url);
	 
	ttop=0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
		ttop=window.pageYOffset;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
		ttop=document.documentElement.scrollTop;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
		ttop=document.body.scrollTop;
	}

	l = (myWidth-700)/2;
	t = (myHeight-250)/2+ttop ; 
	
	 $('#box_email').css('left',l+'px');
	 $('#box_email').css('top' , t+'px'); 
	 
	 $('#box_email').css('display','block');
}


function scrollTop()
{
	ttop=0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		ttop=window.pageYOffset;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		ttop=document.documentElement.scrollTop;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		ttop=document.body.scrollTop;
	}

	return ttop;
	
}

