function openWindow(url,width,height) {
    x = (640 - width)/2, y = (480 - height)/2;
	if (screen) {
        y = (screen.availHeight - height)/2;
        x = (screen.availWidth - width)/2;
    }
	window.open(url,'newWin','width='+width+',height='+height+',screenX='+x+',screenY='+y+',top='+y+',left='+x+',scrollbars=yes');
}

function countCaracteres(cual,counter){
	var max_chars=255;
	remainder=max_chars-cual.length;
	counter.value = remainder;
}
//	count link hits 
function trackclick(id,table) {
   if(document.images){ 
        (new Image()).src="http://www.gabinohome.com/trackclick.php?id="+id+"&table="+table+""; 
    } 
    return true;
}
function textCounter(field,countfield,maxlimit) {
	if ( field.value.length > maxlimit ) {
    	field.value = field.value.substring( 0, maxlimit );
    	alert( "Textarea value can only be "+maxlimit+" characters in length." );
		return false;
  	}else{
		countfield.value = maxlimit - field.value.length;
	}
}
function validNum(formField,fieldLabel,required){
	var result = true;
	if (required && !validRequired(formField,fieldLabel))
    	result = false;
	if (result){
		var num = parseInt(formField.value,10);
     	if (isNaN(num)){
       		alert('Please enter a number for the "' + fieldLabel +'" field.');
      		formField.focus();    
      		result = false;
    	}
  	} 
	return result;
}

//	used on keypress - won't allow invalid chars (according to "type" passed
function inputLimiter(e,allow) {
	var AllowableCharacters = '';
	//if (allow == 'Letters'){AllowableCharacters=' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';}
	//if (allow == 'Numbers'){AllowableCharacters='1234567890';}
	//if (allow == 'NameCharacters'){AllowableCharacters=' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-.\'';}
	if (allow == 'phone'){AllowableCharacters='1234567890+ ';}
	var k;
	k=document.all?parseInt(e.keyCode): parseInt(e.which);
	if (k!=13 && k!=8 && k!=0){
		if ((e.ctrlKey==false) && (e.altKey==false)) {
			return (AllowableCharacters.indexOf(String.fromCharCode(k))!=-1);
		} else {
			return true;
		}
	} else {
		return true;
	}
} 

function NoneWithCheck(ss) {
	for(var i = 0; i < ss.length; i++) {
		if(ss[i].checked) { 
			return false; 
		}
	}
	return true;
}


function display_image(theimage,width,height) {
PreView = window.open("", "Gabinohome_Foto", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,copyhistory=0,width=460,height=460");
PreView.document.open();
PreView.document.write("<HTML><HEAD>");
PreView.document.write("<TITLE>Gabinohome</TITLE>");
PreView.document.write("</HEAD>");
//PreView.document.write("<link rel='stylesheet' href='http://www.gabinohome.com/css/style_3.css' type='text/css'>");
PreView.document.write("<BODY leftmargin='0' topmargin='0' marginwidth='0' marginheight='0' onload='self.focus();' scroll=AUTO>");
PreView.document.write("<IMG HSPACE=20 VSPACE=20 " + "SRC='" + theimage + "' width='400'>");
//PreView.document.write("<INPUT TYPE='button' VALUE='<?php $contents.= $lang_bt_close[$idioma]; ?>' " + "onClick='window.close()'>");

PreView.document.write("</BODY></HTML>");
PreView.document.close();
    }


//Make reference to the selected link
function makeLink(object,BASE_URL,LANG,link_type) {
	var country	=	object.options[object.selectedIndex].value;
	if(country!="-"){
		//alert(country);
		var new_url	=	"http://"+country+BASE_URL+"/"+LANG+"/search/";
		window.top.location.href =new_url; 
	}
}
//Make reference to the selected link
function makeLink2(object,URL) {
	var area	=	object.options[object.selectedIndex].value;
	if(area!="-"){
		var new_url	=	URL+"/"+area+"/";
		//alert(new_url);
		window.top.location.href =new_url; 
	}
}
function makeLink3(object,country,URL) {
	var area	=	object.options[object.selectedIndex].value;
	if(area!="-"){
		//alert(country);
		var new_url	=	country+"/"+URL+"/"+area+"/";
		window.top.location.href =new_url; 
	}
}

//	NEW VALIDATE FUNCTIONS - 24-02-2005

function WithoutContent(ss) {
if(ss.length > 0) { return false; }
return true;
}

function NoneWithContent(ss) {
for(var i = 0; i < ss.length; i++) {
	if(ss[i].value.length > 0) { return false; }
	}
return true;
}

function NoneWithCheck(ss) {
for(var i = 0; i < ss.length; i++) {
	if(ss[i].checked) { return false; }
	}
return true;
}

function WithoutCheck(ss) {
if(ss.checked) { return false; }
return true;
}

function WithoutSelectionValue(ss) {
for(var i = 0; i < ss.length; i++) {
	if(ss[i].selected) {
		if(ss[i].value.length) { return false; }
		}
	}
return true;
}

function validNum(the_num){
	if(the_num.value.length == 0) { 
		return false; 
    }
	if (isNaN(the_num.value)){
       	return false; 
  	} 
	return true;
}

function validEmail(the_email){
	 if(the_email.value.length == 0) { 
      //the_email.focus(); 
      //alert("Please enter your email."); 
      return false; 
    }
	if(-1 == the_email.value.indexOf("@")) { 
      //  the_email.focus(); 
       //alert("Your email must have a '@'."); 
       return false; 
    }
    if(-1 != the_email.value.indexOf(",")) { 
       the_email.focus(); 
      // alert("Your email must not have a ',' in it"); 
       return false; 
    }
    if(-1 != the_email.value.indexOf("#")) { 
      // the_email.focus(); 
      // alert("Your email must not have an '#' in it." ); 
       return false; 
    }
    if(-1 != the_email.value.indexOf("!")) { 
     //  the_email.focus(); 
      // alert("Your email must not have a '!' in it." ); 
       return false; 
    }
    if(-1 != the_email.value.indexOf(" ")) { 
     //  the_email.focus(); 
       //alert("Your email must not have a space in it." ); 
       return false; 
    }
    if(the_email.value.length == (the_email.value.indexOf("@")+1) ) {
     //  the_email.focus();
       //alert("Your email must have a domain name after the '@'.");
       return false;
    }
	return true;
}



//	redirect ferom select list
function goto_URL(object) {
    theUrl	=	object.options[object.selectedIndex].value;
	if(theUrl!='')	window.location.href = theUrl;
}

function subdomain_jump(theUrl) {
  //  theUrl	=	object.options[object.selectedIndex].value;
	//alert(theUrl);
	if(theUrl!='')	window.location.href = theUrl;
}

function show_hide(showIt){ 
	var el = document.getElementById(showIt) 
	if (el.style.display=="none"){ 
		el.style.display=""; //show element 
	}else{ 
		el.style.display="none"; //hide element 
	} 
} 
// Tested on IE, Opera, Netscape/Mozilla
//	HIDE row
function hideRow(DivName){ 
	document.getElementById(DivName).style.display = "none";
} 

//	SHOW row
function showRow(DivName){ 
	document.getElementById(DivName).style.display = "";
} 
function stripPrice(number) {
	//removes everything (except numbers) including decimals and commas
	var number = number.replace(/\D/g,"");
	return number;
}
//	add functions to onload
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != "function") {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
            	oldonload();
          	}
          	func();
		}
	}
}
function changeImage(filename){
	document['mainimage'].src = filename;
}
function disactivateButton(theForm){
	if(theForm.conf.disabled == false){
		theForm.conf.disabled = true;
	}else{
		theForm.conf.disabled = false;
	}
}

//	load functions on page load
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != "function") {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}


function getPage(){
	//	 we are going to need to use ajax to egt the url as it needs to be in full format
	// OR
	//	add options to regions.ajax.php doc ?????
	var the_zone	=	"";
	var url_zone	=	"";
	
	elm_country	= document.getElementById("search_id_country");
	elm_zone	= document.getElementById("search_id_zone");
	elm_region	= document.getElementById("search_id_region");
	elm_cat		= document.getElementById("search_id_cat");
	
	var the_country	=	elm_country[elm_country.selectedIndex].value;
	//alert(the_country);
	if(the_country=="usa"){
		var the_zone	=	elm_zone[elm_zone.selectedIndex].value;
		url_zone=the_zone+"/";
	}
	var the_region	=	elm_region[elm_region.selectedIndex].value;
	var the_cat		=	elm_cat[elm_cat.selectedIndex].value;
	//alert(the_country);
	if(the_country=="uk")	new_url			=	"http://www.gabinohome.co.uk/"+lang_lang+"/"+the_cat+"/"+url_zone+""+the_region+"";
	else					new_url			=	"http://"+the_country+".gabinohome.com/"+lang_lang+"/"+the_cat+"/"+url_zone+""+the_region+"";
	//alert(new_url);
	window.top.location.href =new_url; 
}

//	get text value of selecte item in select list
function get_selected_txt(field){
		var select_list_field = document.getElementById(field);
		var select_list_selected_index = select_list_field.selectedIndex;
		var text = select_list_field.options[select_list_selected_index].text;
		var value = select_list_field.value;
		//alert(text);
		return text;
	}


//	MOOTOOLS FUNCTIONS
//	ajax reload list
function reload_list(val,next_box,req_type,txt_type){
	
	next_box.disabled = 1;  
	var req = new Request({
		method: 'get',
		url: '/includes/ajax/mootools_list.ajax.php',
		data: {'id':val,'type':''+req_type+'','lang':''+lang_lang+''},
		onRequest: function() { 
			next_box.empty();
			var o = new Element('option', {'value':'','html':''+lang_loading+''});
			o.inject(next_box);
			next_box.setStyle('color','#666666');  
			next_box.disabled = 0;  
		},
		onComplete: function(response) {
			//	empty box
			next_box.empty();
			if(response=="0"){
				var o = new Element('option', {'value':'','html':' - '+lang_select_no_results+' - '});
				o.inject(next_box);
			}else{
				//	 add select text IF defined
				if(txt_type!=""){
					var o = new Element('option', {'value':'','html':' - '+txt_type+' - '});
					o.inject(next_box);
				}
				if(req_type=='country_zone'){
					//	force user to select a zone
					$('search_id_region').empty();
					var o = new Element('option', {'value':'','html':' - '+lang_select_region+' - '});
					o.inject($('search_id_region'));
					$('search_id_region').disabled = 1;  
					$('search_id_region').setStyle('color','#000000');
					//	add zones
					//var o = new Element('option', {'value':'','html':' - '+lang_select_zone+' - '});
					//o.inject(next_box);
				
				}else if(req_type=='country_code_zone_id'){
					elm_add_region.empty();
					var o = new Element('option', {'value':'','html':' - '+lang_select_region+' - '});
					o.inject(elm_add_region);
					elm_add_region.disabled = 1;  
					elm_add_region.setStyle('color','#000000');
				}else if(req_type=='country_code_cat_id'){
					var o = new Element('option', {'value':'','html':' - '+lang_select_cat+' - '});
					o.inject(next_box);

				}
				eval(response);
				next_box.setStyle('color','#000000');
			}
		}
	}).send();
}

		
//	common function for all items in favs list
function favs_remove(item_id){
	advert_icon_favs=false;
	if($('icon_favs')) advert_icon_favs=$('icon_favs');

	var myHTMLRequest = new Request({
		url:'/includes/ajax/favs.ajax.php',
		method:'get',
		async:false,
		data: {
			'id_advert':item_id,
			'lang':lang_lang
			},
		onRequest: function() {
			if(advert_icon_favs)advert_icon_favs.set('src','/images/favs-loader.gif');
		},
		onComplete: function(responseText) {
			//	remove item from favs
			if($('favs_item_'+item_id+''))$('favs_item_'+item_id+'').dispose();
			
			//	if the page is the advert, we must update from the fav icon 
			if(advert_icon_favs)advert_icon_favs.set('src','/images/icons/icon_favs_blue.jpg');
		}
	}).send();
}
		
		
window.addEvent('domready', function(){
	//	define search elements
	var elm_countries	= $('search_id_country');
	var elm_zones		= $('search_id_zone');
	var elm_regions		= $('search_id_region');
	if($('search_id_area'))var elm_areas		= $('search_id_area');
	var elm_cats		= $('search_id_cat');
	var elm_search_bt	= $('quick_search_button');
	//if($('search_map')) var elm_map			= $('search_map');
	//	ajax search
	elm_countries.addEvent('change', function(e) {
		e.stop();
		elm_search_bt.disabled=1;
		var val=elm_countries.get('value');
		//make the ajax call
		if(val=='usa'){
			//	show zones
			$('list_zones').setStyle('display', ''); 
			var next_box=elm_zones;
			var req_type='country_zone';
			txt_next_select=lang_select_zone;
			zoom_level=3;
		}else{
			$('list_zones').setStyle('display', 'none'); 
			var next_box=elm_regions;
			var req_type='country_region';
			txt_next_select=lang_select_region;
			zoom_level=5;
			
		}
		//alert(next_box);
		reload_list(val,next_box,req_type,txt_next_select);
		
		//	now reload categories to get country cat definitions
		if(elm_cats){
			reload_list(val,elm_cats,'country_cat','');
		}
		
		if($('search_map')){
			reload_list(val,$('map_cat'),'country_cat_id','');
			//	get latlan for country to move map and reload adverts
			get_geo(val,'country_subdomain',zoom_level,1);
		}
	});

	//	menu
	
	//	zones - reload regions
	elm_zones.addEvent('change', function(e) {
		e.stop();
		var val=elm_zones.get('value');
		var next_box=elm_regions;
		var lang_select_type =lang_select+' '+lang_region;
		reload_list(val,next_box,'zone_region',lang_select_region);
		if($('search_map')){
			get_geo(val,'zone_url',8);
		}
	});
	elm_regions.addEvent('change', function(e) {
		e.stop();
		var val=elm_regions.get('value');
		if(val!=''){
			elm_search_bt.disabled=0;
		}
		if($('search_map')){
			get_geo(val,'region_url',12);
		}
	});
	
	//	list page - reload areas on provinia change IF defined
	if($('filter_region')){
		$('filter_region').addEvent('change',function(){
			$('row_areas').dispose();
			//var val=this.get('value');
			//reload_list(val,$('filter_area'),'region_area','');
		});
	}
	
	//	remove items from favs
	$$('.favs_icon_remove').addEvent('click',function(){
		var item_id=this.getAttribute('rel');
		favs_remove(item_id);
	}).setStyle('cursor','pointer');
});
var noobSlide = new Class({
	initialize: function(params){
		this.items = params.items;
		this.mode = params.mode || 'horizontal';
		this.modes = {horizontal:['left','width'], vertical:['top','height']};
		this.size = params.size || 240;
		this.box = params.box.setStyle(this.modes[this.mode][1],(this.size*this.items.length)+'px');
		this.button_event = params.button_event || 'click';
		this.handle_event = params.handle_event || 'click';
		this.onWalk = params.onWalk || null;
		this.currentIndex = null;
		this.previousIndex = null;
		this.nextIndex = null;
		this.interval = params.interval || 5000;
		this.autoPlay = params.autoPlay || false;
		this._play = null;
		this.handles = params.handles || null;
		if(this.handles){
			this.addHandleButtons(this.handles);
		}
		this.buttons = {
			previous: [],
			next: [],
			play: [],
			playback: [],
			stop: []
		};
		if(params.addButtons){
			for(var action in params.addButtons){
				this.addActionButtons(action, $type(params.addButtons[action])=='array' ? params.addButtons[action] : [params.addButtons[action]]);
			}
		}
		this.fx = new Fx.Tween(this.box,$extend((params.fxOptions||{duration:500,wait:false}),{property:this.modes[this.mode][0]}));
		this.walk((params.startItem||0),true,true);
	},

	addHandleButtons: function(handles){
		for(var i=0;i<handles.length;i++){
			handles[i].addEvent(this.handle_event,this.walk.bind(this,[i,true]));
		}
	},

	addActionButtons: function(action,buttons){
		for(var i=0; i<buttons.length; i++){
			switch(action){
				case 'previous': buttons[i].addEvent(this.button_event,this.previous.bind(this,[true])); break;
				case 'next': buttons[i].addEvent(this.button_event,this.next.bind(this,[true])); break;
				case 'play': buttons[i].addEvent(this.button_event,this.play.bind(this,[this.interval,'next',false])); break;
				case 'playback': buttons[i].addEvent(this.button_event,this.play.bind(this,[this.interval,'previous',false])); break;
				case 'stop': buttons[i].addEvent(this.button_event,this.stop.bind(this)); break;
			}
			this.buttons[action].push(buttons[i]);
		}
	},

	previous: function(manual){
		this.walk((this.currentIndex>0 ? this.currentIndex-1 : this.items.length-1),manual);
	},

	next: function(manual){
		this.walk((this.currentIndex<this.items.length-1 ? this.currentIndex+1 : 0),manual);
	},

	play: function(interval,direction,wait){
		this.stop();
		if(!wait){
			this[direction](false);
		}
		this._play = this[direction].periodical(interval,this,[false]);
	},

	stop: function(){
		$clear(this._play);
	},

	walk: function(item,manual,noFx){
		if(item!=this.currentIndex){
			this.currentIndex=item;
			this.previousIndex = this.currentIndex + (this.currentIndex>0 ? -1 : this.items.length-1);
			this.nextIndex = this.currentIndex + (this.currentIndex<this.items.length-1 ? 1 : 1-this.items.length);
			if(manual){
				this.stop();
			}
			if(noFx){
				this.fx.cancel().set((this.size*-this.currentIndex)+'px');
			}else{
				this.fx.start(this.size*-this.currentIndex);
			}
			if(manual && this.autoPlay){
				this.play(this.interval,'next',true);
			}
			if(this.onWalk){
				this.onWalk((this.items[this.currentIndex] || null), (this.handles && this.handles[this.currentIndex] ? this.handles[this.currentIndex] : null));
			}
		}
	}
	
});


var containsBannedWords = function(){
	//Put our list of banned words into an array
	var banned_words = ['www', 'http', '@', '.com','.es'];	
 
	var errors=0;
	$$('.wordCheck').each(function(el){
	 	error_this=0;
	 	//Iterate over each of the banned words
		banned_words.each(function(banned_word){
			//Look for the current banned word in the textarea contents
			if (el.get('value').contains(banned_word)){
				//Tell the user not to use that word
				errors++;
				error_this++;
			}
		});
		if(error_this!=0)el.setStyle('border','1px solid red');
		else 			 el.setStyle('border','1px solid #CCC');				
	});
	if(errors!=0) return false;
	else return true;
}