// JavaScript Document
 /*
  *	 Function LoadLocation: This funtion changes regions drop downs when a country is selected
  */	

function addRSS(){
	var w = screen.width, h = screen.height;
	var popW = 800, popH = 600;
	var leftPos = (w-popW)/2, topPos = 50;
	
	window.open("http://cyprus.uniqueliving.com/rss/uniqueliving_rss.xml","UniqueLiving","width="+popW+",height="+popH+",top="+topPos+",left="+leftPos+",toolbar=no,directories=no,menubar=no,status=yes,scrollbars=yes");

}
function openInNewWindow(url) {

	// Change "_blank" to something like "newWindow" to load all links in the same new window
   var newWindow = window.open(url, '_blank');
   newWindow.focus();
   return false;
}

function rotateSections(){
	rotateMainImage(); 
	rotateDevelopments();
}

function LoadCountries(cid,target,target1,target2,sel){
	//delete countries
	document.getElementById(target).options.length=0;
	
	var Opt=document.createElement("option");
	Opt.value=0;	
	Opt.innerHTML="All Countries";
	document.getElementById(target).appendChild(Opt);

	for(x=0; x<countries.length; x++){
		var id =countries[x][0];

		var Opt=document.createElement("option");
		Opt.value=id;	
		Opt.innerHTML=countries[x][1];

		if(cid==2 && countryPropertiesRental[id]>0){
			document.getElementById(target).appendChild(Opt);
		}	
		else if(cid==1){	
			document.getElementById(target).appendChild(Opt);
		}	
	}
	
	LoadRegions(0,target1,target2,0);
	
	LoadPricesList(cid,'sPrice');
}

function LoadPricesList(cid,target){
	
	document.getElementById(target).options.length=0;
	
	var Opt=document.createElement("option");
	Opt.value = 0;
	Opt.innerHTML="All Prices";
	document.getElementById(target).appendChild(Opt);
	
	Opt=document.createElement("option");
	Opt.value = 1;
	Opt.innerHTML="Above &euro; 3 million";
	document.getElementById(target).appendChild(Opt);
		
	Opt=document.createElement("option");
	Opt.value = 2;
	Opt.innerHTML="&euro; 1 million - &euro; 3 million";
	document.getElementById(target).appendChild(Opt);
							
	Opt=document.createElement("option");
	Opt.value = 3;
	Opt.innerHTML="&euro; 0 - &euro; 1 million";
	document.getElementById(target).appendChild(Opt);
	
}

function LoadRegions(cid,target,target1,sel){

		document.getElementById(target).options.length=0;
		
		var countryPropertiesArray;
		var regionPropertiesArray;
		
		saleRental = 1;
		
		if(saleRental == 1){
			countryPropertiesArray = countryProperties;	
			regionPropertiesArray = regionProperties;
		}
		else if(saleRental == 2){
			countryPropertiesArray = countryPropertiesRental;
			regionPropertiesArray = regionPropertiesRental;
		}
		
		
		var Opt=document.createElement("option");
		
		if(cid>0){
			var Opt=document.createElement("option");
			Opt.value=0;

			if (sel==0){
				Opt.selected = true;
			}
			
			totalProperties = "";
			if(countryPropertiesArray[cid]){
				totalProperties = "["+countryPropertiesArray[cid]+"]";	
			}

			Opt.innerHTML = all_regions +" "+totalProperties;
			
			document.getElementById(target).appendChild(Opt);
			
	        }
		else{
			Opt.value = all_regions;
			Opt.innerHTML = all_regions;
			document.getElementById(target).appendChild(Opt);
		}
	
		var totalProperties = "";
		for (x=0;x<regions.length;x++){
			if (regions[x][1]==cid){
				var Opt=document.createElement("option");
				Opt.value=regions[x][0];
		
				if (regions[x][0]==sel){
					Opt.selected = true;
				}
	
				totalProperties = "";
				if(regionPropertiesArray[regions[x][0]]){
					totalProperties = "["+regionPropertiesArray[regions[x][0]]+"]";	
				}

				Opt.innerHTML=regions[x][2].replace('\\','')+" "+totalProperties;
				
				
				document.getElementById(target).appendChild(Opt);
			}
		}
		
		document.getElementById(target1).options.length=0;
		var Opt=document.createElement("option");
		
		var country_id = document.getElementById('sCountry').value;

		totalProperties = "";
		if(countryPropertiesArray[country_id]){
			totalProperties = "["+countryPropertiesArray[country_id]+"]";	
		}

		Opt.innerHTML = all_locations +" "+totalProperties;

		document.getElementById(target1).appendChild(Opt);
}
 
 /*
  *	 Function LoadLocation: This funtion changes location drop downs when a country is selected
  */	

function LoadLocations(cid,target,sel){

		document.getElementById(target).options.length=0;
		
		saleRental = 1;

		if(saleRental == 1){
			countryPropertiesArray = countryProperties;	
			regionPropertiesArray = regionProperties;
		}
		else if(saleRental == 2){
			countryPropertiesArray = countryPropertiesRental;
			regionPropertiesArray = regionPropertiesRental;
		}
		
		var Opt=document.createElement("option");
	
		if(cid>0){
			var Opt=document.createElement("option");
			Opt.value=0;

			if (sel==0){
				Opt.selected = true;
			}
			
			
			totalProperties = "";
			if(regionPropertiesArray[cid]){
				totalProperties = "["+regionPropertiesArray[cid]+"]";	
			}
			
			Opt.innerHTML = all_locations+" "+totalProperties;
			document.getElementById(target).appendChild(Opt);
			
			for (x=0;x<locations.length;x++){
				if (locations[x][1]==cid){
					var Opt=document.createElement("option");
					Opt.value=locations[x][0];
			
					if (locations[x][0]==sel){
						Opt.selected = true;
					}
	
					totalProperties = "";
					if(locationProperties[locations[x][0]]){
						totalProperties = "["+locationProperties[locations[x][0]]+"]";	
					}
	
					Opt.innerHTML=locations[x][2].replace('\\','')+" "+totalProperties;
					document.getElementById(target).appendChild(Opt);
				}
			}
			
        }
		else{

			var country_id = document.getElementById('sCountry').value;

			totalProperties = "";
			if(countryPropertiesArray[country_id]){
				totalProperties = "["+countryPropertiesArray[country_id]+"]";	
			}

			Opt.innerHTML = all_locations+" "+totalProperties;

			document.getElementById(target).appendChild(Opt);
		}
}

function validate(form){

	var country_name;
	var location_name;
	var region_name;

	var country = document.getElementById('sCountry').value;
    country_name = countriesDefault[country];

	
	if(country > 0){
		
		var region = document.getElementById('sRegions').value;
		
		path = path + "property-for-sale/" + country_name.split(" ").join("-").toLowerCase();

		if(region > 0){
		
			region_name = regionsDefault[region];
		    region_name = region_name.split("&").join("and");
			path = path + "/"+ region_name.split(" ").join("-").toLowerCase();
			
			var location = document.getElementById('sLocations').value;
			if( location > 0){
				location_name = locationsDefault[location];
				location_name = location_name.split("&").join("and");
				path = path + "/" + location_name.split(" ").join("-").toLowerCase();
			}
		}
	
		//validate type of property
		var type = document.getElementById('sType').value;
	
		
		if ( type == 1 ){
			path = path + "/villa";
		}
		else if( type == 2 ){
			path = path + "/apartment";
		}
		else if( type == 3 ){
			path = path + "/plot";
		}

		form.action= path;
		form.submit();
	}
	else{
		alert(select_country);
	}
}


function showPropertiesPerPage(propertiesPerPage, url){

	pageIndex = url.indexOf("/page");
	
	if(pageIndex>0){
		url = url.substr(0,pageIndex);
	}

	if(url.indexOf("?")>0)
		url = url.substr(0,url.indexOf("?"));


	
	var keyChar = "?";
	
	//get bed value
	
	var beds = document.getElementById('sBeds').value;
	var session_bed = document.getElementById('session_bed').value;

	if( beds != session_bed){
		url += keyChar+"beds="+beds;
		keyChar = "&";	
	}

	var price = document.getElementById('sPrice').value;
	var session_price = document.getElementById('session_price').value;
	
	if( price != session_price ){
		url += keyChar+"price="+price;
	}
	
	url += keyChar+"properties_per_page="+propertiesPerPage;
	location.href=url;
}
  
function printProperty(propertyId){

	var w = screen.width, h = screen.height;
	var popW = 800, popH = 600;
	var leftPos = (w-popW)/2, topPos = 50;

	window.open("print.php?property_id="+propertyId,"UniqueLiving","width="+popW+",height="+popH+",top="+topPos+",left="+leftPos+",toolbar=no,directories=no,menubar=no,status=yes,scrollbars=yes");

}
 
  
  
function validateAdvanced(form,path){

	var country_name;
	var region_name;
	var location_name;

	var country_id = document.getElementById('advancedCountry').value;
	var region_id;
	var location_id;

	if(country_id>0){
	
		for (x=0;x<countries.length;x++){
			if (countries[x][0]==country_id){
				country_name=countries[x][1];
				break;
			}
		}
		
		path += "property-for-sale/" + country_name.split(" ").join("-").toLowerCase();

		if(	(region_id = document.getElementById('advancedRegion').value)>0){
		
			for (x=0;x<regions.length;x++){
				if (regions[x][0]==region_id){
					region_name=regions[x][2];
					break;
				}
			}
			
			region_name = region_name.split("&").join("and");
			path += "/"+ region_name.split(" ").join("-").toLowerCase();
			
			if(	(location_id = document.getElementById('advancedLocation').value)>0){
		
				for (x=0;x<locations.length;x++){
					if (locations[x][0]==location_id){
						location_name=locations[x][2];
						break;
					}
				}
				
				location_name = location_name.split("&").join("and");
				path += "/" + location_name.split(" ").join("-").toLowerCase();
			}
		}
		var type = document.getElementById('advancedType').value;

		if ( type == 1 ){
			path += "/villa";
		}
		else if( type == 2 ){
			path += "/apartment";
		}
		else if( type == 3 ){
			path += "/plot";
		}

		var minPrice = document.getElementById('minPrice').value;
		var maxPrice = document.getElementById('maxPrice').value;
		
		var validPriceRange = false;
		if(minPrice==0 && maxPrice==0 ){
			validPriceRange = true;
		}
		if(minPrice==10 && maxPrice==0 ){
			validPriceRange = true;
		}
		if(minPrice<maxPrice ){
			validPriceRange = true;
		}

		if(validPriceRange){
			form.action= path;
			form.submit();	
		}
		else{
			alert("Invalid Price Range");	
		}
		
	}
	else{
		alert(select_country);
	}
}


function detectChange(inputElement, defaultText){
	if(inputElement.value.length==0)
		inputElement.value = defaultText;
}

function detectInitialChange(inputElement, defaultText){
	if(inputElement.value==defaultText)
		inputElement.value = "";
}

function highlightFeatTitle(id){
	document.getElementById(id).style.color='#E7B662';
}
function resetFeatTitle(id){
	document.getElementById(id).style.color='#FFFFFF';
}


function processForm(formId){
	var data = getFormData(formId);
	var w = screen.width, h = screen.height;
	var popW = 420, popH = 350;
	var leftPos = (w-popW)/2, topPos = (h-popH)/2;

	window.open("http://cyprus.uniqueliving.com/processRequest"+data,"UniqueLiving","width="+popW+",height="+popH+",top="+topPos+",left="+leftPos+",toolbar=no,directories=no,menubar=no,status=yes,scrollbars=no");

}
function processValidator(formId){
	var data = getFormData(formId);
	var url = document.getElementById(formId).action.value;
	window.opener.location.href=url+data;
    window.close();
}
function getFormData(formId){
	var form = document.getElementById(formId);
	var numElements = form.elements.length
	
	var data="";
	for(i=0; i< numElements; i++){
		
		if(i==0)
	 	 data ="?";
		else
		 data +="&";
			 
		 if(form.elements[i].type == "checkbox")	 
			 data +=form.elements[i].name+"="+form.elements[i].checked;
		 else
			 data += form.elements[i].name+"="+form.elements[i].value; 
	}
		
	return data;
}

function clearElements(form, propertyId){
	
	form.send_brochure.checked=false;
	form.email_option.checked=false;
	form.post_option.checked=false;
	
	form.contact_me.checked=false;
	form.prop_alert_news.checked=false;

	
	form.name_field.value='enter your name';
	form.telephone_field.value='enter your telephone number';
	form.email_field.value='enter your email address';
	form.address_field.value='Enter you address (for postal brochures)';
	
	form.comments_field.value='I would like to enquire regarding property '+propertyId;
}

function ReceivePropertyAlerts(){
	document.getElementById('property_alert_email_form').submit();
}


function showAdvanced(display){
	
	var displayStyle= "inline";
	var hiddenDisplayStyle= "none";
	
	if(display==true){
		document.getElementById("hide_advanced_search").style.display = displayStyle;
		document.getElementById("advanced_search").style.display = hiddenDisplayStyle;
		document.getElementById("advanced_search_option").value = 1;
	}
	else{
		document.getElementById("hide_advanced_search").style.display = hiddenDisplayStyle;
		document.getElementById("advanced_search").style.display = displayStyle;
		document.getElementById("advanced_search_option").value = 0;
		displayStyle = hiddenDisplayStyle;
	}

	document.getElementById("new_development_label").style.display = displayStyle;
	document.getElementById("resale_label").style.display = displayStyle;
	document.getElementById("pool_label").style.display = displayStyle;
	document.getElementById("garden_label").style.display = displayStyle;
	document.getElementById("golf_label").style.display = displayStyle;
	document.getElementById("parking_label").style.display = displayStyle;
	document.getElementById("new_development_checkbox").style.display = displayStyle;
	document.getElementById("resale_checkbox").style.display = displayStyle;
	document.getElementById("pool_checkbox").style.display = displayStyle;
	document.getElementById("garden_checkbox").style.display = displayStyle;
	document.getElementById("golf_checkbox").style.display = displayStyle;
	document.getElementById("parking_checkbox").style.display = displayStyle;
}

//your name


function validateFriendForm(form){

	var name = trim(form.name_field.value);
	var telephone = trim(form.telephone_field.value);
	var email = trim(form.email_field.value);
	var friend_name = trim(form.friend_name_field.value);
	var friend_email = trim(form.friend_email_field.value);
			
	var message = "";

	//Validate name 
	if(name==""){
		message += '<p><font color="#D38069">Invalid Name</font></p><br>';
	}
	
	//Validate telephone number
	if(telephone=="" ){
		message += '<p><font color="#D38069">Invalid Telephone</font> </p><br>';
	}
	else if(isNaN(telephone)){
			message += '<p><font color="#D38069">Telephone must be a number</font></p><br>';
	}
	else if(telephone.length<8){
			message += '<p><font color="#D38069">Telephone is too short</font></p><br>';
	}	
	
	//Validate if the email is valid
	if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(email))){
		message += '<p><font color="#D38069">Invalid Email</font></p><br>';
	}


	if(friend_name==""){
		message += '<p><font color="#D38069">Invalid Friends Name</font></p><br>';
	}

	if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(friend_email))){
		message += '<p><font color="#D38069">Invalid Friends Email</font></p><br>';
	}

	if(message.length>0){
		DWRUtil.setValue('friend_msg',message);
	}
	else{
		form.submit();
	}
}

function validateCallMeForm(form){

	var name = trim(form.name_field.value);
	var telephone = trim(form.telephone_field.value);
	var email = trim(form.email_field.value);
	
	var errors = false;
	
	//Validate name 
	if(name==""){
		DWRUtil.setValue('name', 'Name:* <br> <font color="#D38069">Invalid Name</font>');
		errors = true;
	}
	else{
		DWRUtil.setValue('name', 'Name:* ');
	}
	
	//Validate telephone number
	if(telephone==""){
		DWRUtil.setValue('telephone', 'Telephone:* <br> <font color="#D38069">Invalid Telephone</font>');
		errors = true;
	}
	else if(isNaN(telephone)){
		DWRUtil.setValue('telephone', 'Telephone:* <br><font color="#D38069">Telephone must be a number</font>');
		errors = true;
	}
	else if(telephone.length<8){
		DWRUtil.setValue('telephone', 'Telephone:* <br><font color="#D38069">Telephone is too short</font>');
		errors = true;
	}	
	else{
		DWRUtil.setValue('telephone', 'Telephone:* ');
	}

	//Validate if the email is valid
	if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))){
		DWRUtil.setValue('email', 'Email:* <br> <font color="#D38069">Invalid Email</font>');
 		errors = true;
	}
	else{
		DWRUtil.setValue('email', 'Email:* ');
	}
	
	if(!errors){
		form.submit();
	}
}

function add_call_me(id){
	reference = trim(DWRUtil.getValue('my_favourites_message'));
    references = reference.split("\n");
	
	exist=false;

	for(i=0; i< references.length; i++)
		if(references[i]== id ){
			exist=true;
			break;
		}
		
	if(!exist){
		if(reference.length>0)
			reference = reference+"\n"+id;
		else 
			reference = id;
	
	}
		
	DWRUtil.setValue('my_favourites_message',reference);
	
}


function trim(sString)
{
	while (sString.substring(0,1) == ' '){
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' '){
		sString = sString.substring(0,sString.length-1);
	}
	
	return sString;
}	

function highlight(){
	document.getElementById('alert_name').style.background='#FFFFCC';
	document.getElementById('alert_telephone').style.background='#FFFFCC';
	document.getElementById('alert_email').style.background='#FFFFCC';
}

function highlight1(id){
	document.getElementById('alert_name').style.background='#FFFFCC';
	document.getElementById('alert_telephone').style.background='#FFFFCC';
	document.getElementById('alert_email').style.background='#FFFFCC';
	DWRUtil.setValue('alert_message','Please call me to discuss property ref: '+id);
}
	
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_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_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];}
}
	