<!--

function MM_findObj(n, d) { //v4.0
  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 && document.getElementById) x=document.getElementById(n); return x;
}

function positionItem(whichItem,itemLeft){ // Called from html_header.php
	object = document.getElementById(whichItem);
	if(object){
		object.style.left = offsetPosition + itemLeft + 'px';
		object.style.visibility = 'visible';
	}
}


function positionElements(){ // Called from html_header.php

	tableWidth = 968; // Width of table for content
	offsetPosition = 0; // Will be the left hand edge of the table
	
	// Work out offset so that main page is centred:
	if(window.innerWidth){
		offsetPosition = (window.innerWidth-tableWidth)/2;
	} else if(document.body.clientWidth){
		offsetPosition = (document.body.clientWidth-tableWidth)/2;
	}
	if(offsetPosition<0){ offsetPosition=0; }
	
	
	// Now move elements around and switch them to visible once moved:
	positionItem('background_image',0);
	positionItem('logo',34);
	positionItem('top_links',0);
	positionItem('page_heading',34);
	positionItem('text_box_1',34);
	positionItem('apt_detail_text_box_1',34); // Apartment detail
	positionItem('apt_detail_text_box_2',242); // Apartment detail
	positionItem('back_to_listings',34); // Apartment detail
	positionItem('email_friend',241); // Apartment detail
	positionItem('contact_me',241); // Apartment detail
	positionItem('calendars',34); // Apartment detail
	positionItem('main_image',458); // Homepage
	positionItem('apartment_main_image',458); // Apartment detail
	positionItem('apartment_thumbnails',458); // Apartment detail
	positionItem('search_for_accommodation',34); // Homepage
	positionItem('select_search_criteria',34); // Homepage
	positionItem('search_form_arrival',34); // Homepage
	positionItem('search_form_departure',264); // Homepage
	positionItem('search_minimum_people',34); // Homepage
	positionItem('search_submit_button',364); // Homepage
	positionItem('bottom_links',34); // Homepage
	positionItem('listing_frame',129); // Homepage
	positionItem('listing_frame_new',34); // Apartment listings page (post 10.4.08)
	positionItem('listing_text_1',750); // Apartment listings page (post 10.4.08)
	positionItem('link_home',57); // All pages
	positionItem('link_apartments',162); // All pages
	positionItem('link_useful_info',288); // All pages
	positionItem('link_getting_here',405); // All pages
	positionItem('link_contact_us',523); // All pages
	positionItem('link_links',658); // All pages
	positionItem('link_about_us',727); // All pages
	positionItem('contact_form',505); // Contact form
	positionItem('contact_form_submit',504); // Contact form
	positionItem('about_us_pic_1',34); // About us
	positionItem('about_us_pic_2',203); // About us
	positionItem('about_us_pic_3',422); // About us
	positionItem('about_us_pic_4',590); // About us
	positionItem('about_us_pic_5',809); // About us
	positionItem('map_1',199); // Getting here
	positionItem('map_2',399); // Getting here
	positionItem('map_3',599); // Getting here






}
	
function stripCharacters(myObj,whichType,spacesAllowed){
	//myObj = form object to check, whichType of check (i.e. an = alphanumeric, em = email, tx = text)
	//spacesAllowed = determines if spaces should be returned or not. NOTE This script will not return characters
	//outside of the normal range, which includes currency symbols such as � and the Yen.
	
	var i,myString,toReturn,toCheck,errors,spaces_in_string;
	errors = false;
	myString = MM_findObj(myObj).value;
	toReturn = "";
	spaces_in_string = false;//set to true if spaces are present
	for(i=0;i<myString.length;i++){
		toCheck = myString.charCodeAt(i);
		if(toCheck==32) { spaces_in_string = true }
		if(spacesAllowed&&toCheck==32){ // if spaces are allowed, return a space
			toReturn+=" ";
		} else if(toCheck<32||toCheck>126) {//Make sure nothing outside normal range is returned
			errors = true;
		} else if(whichType=="an"&&(toCheck<48 || (toCheck>57&&toCheck<65) || (toCheck>90&&toCheck<97) || toCheck>122)){ 
			// Check alphanumeric
			errors = true;
		} else if(whichType=="em"&&(toCheck<38 || toCheck==40 || toCheck==41 || toCheck==44 || (toCheck>57&&toCheck<61) || toCheck==62 || (toCheck>90&&toCheck<94) || toCheck==96 || toCheck==124)){ // check email
			errors = true;
		} else if(whichType=="tx"&&(toCheck==35 || (toCheck>90&&toCheck<97) || toCheck>122)){ // check text
			errors = true;
		} else { // this is a valid character - add it to the string
			toReturn+=myString.substring(i,i+1);
		}
		
	}
	MM_findObj(myObj).value = toReturn;
	if(errors&&whichType=="an"){	errormsg = "You can only use alpha-numeric characters (i.e. 0-9, A-Z, a-z)." } else 	if(errors&&whichType=="em"){	errormsg = "You have used invalid characters for an e-mail address." } else if(errors&&whichType=="tx"){	errormsg = "You have used invalid characters." }
	if(spaces_in_string&&!spacesAllowed){
		errormsg += "\nSpaces are not allowed in this field.";
		errors = true;
	}
	if(errors){
		alert(errormsg);
	}
}

function  track_radio_button(varToChange,toWhat){
	MM_findObj(varToChange).value = toWhat;
}


function MM_validateForm() { //v4.0
	var i,p,q,nm,test,num,min_amt,max_amt,errors='',args=MM_validateForm.arguments;
	for (i=0; i<(args.length-2); i+=3) {
		test=args[i+2];
		val=MM_findObj(args[i]);
    	if (val) {
	 		nm=val.name;
			if(nm == "input_name"){nm = "Property name"} 
			else if(nm == "input_bedrooms"){nm = "No. of bedrooms"} 
			else if(nm == "input_bathrooms"){nm = "No. of bathrooms"} 
			else if(nm == "input_price_peak"){nm = "Price peak"}
			else if(nm == "input_price_offpeak"){nm = "Price off-peak"}
			else if(nm == "input_sleeps_max"){nm = "Sleeps maximum"}
			else if(nm == "your_name"){nm = "Your name"}
			else if(nm == "your_email"){nm = "Your e-mail"}
			else if(nm == "friends_name"){nm = "Friend's name"}
			else if(nm == "friends_email"){nm = "Friend's email"}
			else if(nm == "name"){nm = "Your name"}
			else if(nm == "user_name"){nm = "Your name"}
			else if(nm == "email"){nm = "E-mail address"}

			if ((val=val.value)!="") {
      			if (test.indexOf('isEmail')!=-1) {
					p=val.indexOf('@');
        			if (p<1 || p==(val.length-1)){
						errors+='- '+nm+' must contain a valid e-mail address.\n';
					}
      			} else if(test.indexOf('shorter')!=-1){
					p=test.indexOf('shorter');
					min_amt=test.substring(p+7,p+8);
					if(val.length<min_amt){
						errors+='- '+nm+' length must be at least '+min_amt+' characters.\n';
					}
				} else if (test!='R') {
        			if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        			if (test.indexOf('inRange') != -1) {
						p=test.indexOf(':');
          				min_amt=parseInt(test.substring(8,p));
						max_amt=parseInt(test.substring(p+1));
//alert("min ="+min_amt+"; max = "+max_amt+"; val ("+val+")parsed = "+parseFloat(val));
          				if (parseFloat(val)<min_amt || max_amt<parseFloat(val)) errors+='- '+nm+' must contain a number between '+min_amt+' and '+max_amt+'.\n';
					}
				}
			} else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  		}
		if(MM_findObj("email")&&MM_findObj("confirm_email")){//These values are not present on all forms
			email1=MM_findObj("email");
			email2=MM_findObj("confirm_email");
			if(email1.value!=email2.value){errors+="- Confirm e-mail must be the same as E-mail.\n"}
		}
		if(MM_findObj("password")&&MM_findObj("confirm_password")){//These values are not present on all forms
			password1=MM_findObj("password");
			password2=MM_findObj("confirm_password");
			if(password1.value!=password2.value){errors+="- Confirm password must be the same as Password.\n"}
		}
		if (errors){
			return (errors);
		} else {
			return ("");
		}

	document.MM_returnValue = (errors == '');
}

function check_admin_edit_property_form(){// Called from admin_video_add.php
	anyproblems = "";
	anyproblems = MM_validateForm('input_name','','R','input_bedrooms','','RinRange0:12','input_bathrooms','','RinRange0:12','input_price_peak','','inRange0:99999','input_price_offpeak','','inRange0:99999','input_sleeps_max','','RinRange1:25','input_friendly_url','','R');
	if(anyproblems){
		alert('The following error(s) occurred:\n'+anyproblems);
	} else {
		MM_findObj("post_name").value = escape(MM_findObj("input_name").value);
		escape("input name = "+MM_findObj("post_name").value);
		MM_findObj("post_price_peak").value = MM_findObj("input_price_peak").value;
		MM_findObj("post_price_offpeak").value = MM_findObj("input_price_offpeak").value;
		MM_findObj("post_bedrooms").value = MM_findObj("input_bedrooms").value;		
		MM_findObj("post_bathrooms").value = MM_findObj("input_bathrooms").value;		
		MM_findObj("post_location").value = escape(MM_findObj("input_location").value);		
		MM_findObj("post_sleeps_max").value = MM_findObj("input_sleeps_max").value;	
		if ( MM_findObj("input_active").checked )  MM_findObj("post_active").value = '1';
		//Update radio button values:
/*		if (MM_findObj("radio_parking")[0].checked){MM_findObj("post_parking").value = 1} else {MM_findObj("post_parking").value = 0;}
		if (MM_findObj("radio_fireplace")[0].checked){MM_findObj("post_fireplace").value = 1} else {MM_findObj("post_fireplace").value = 0;}
		if (MM_findObj("radio_balcony")[0].checked){MM_findObj("post_balcony").value = 1} else {MM_findObj("post_balcony").value = 0;}
		if (MM_findObj("radio_cd_dvd")[0].checked){MM_findObj("post_cd_dvd").value = 1} else {MM_findObj("post_cd_dvd").value = 0;}
		if (MM_findObj("radio_cot")[0].checked){MM_findObj("post_cot").value = 1} else {MM_findObj("post_cot").value = 0;}
*/
		MM_findObj("post_special_features").value = escape(MM_findObj("input_special_features").value);	
		MM_findObj("post_meta_title").value = escape(MM_findObj("input_meta_title").value);	
		MM_findObj("post_meta_keywords").value = escape(MM_findObj("input_meta_keywords").value);	
		MM_findObj("post_meta_description").value = escape(MM_findObj("input_meta_description").value);	
		MM_findObj("post_friendly_url").value = encode_text_to_url(MM_findObj("input_friendly_url").value);	
		if ( MM_findObj("input_address") ){
			MM_findObj("post_address").value = escape(MM_findObj("input_address").value);	
			MM_findObj("post_zoom").value = MM_findObj("input_zoom").value;	
		}

		MM_findObj("hiddenForm").submit();
	}
}

function check_availability_form(){
	anyproblems = "";
	test_start_day = MM_findObj("start_day").value;
	test_start_month = MM_findObj("start_month").value;
	test_start_year = MM_findObj("start_year").value;
	test_end_day = MM_findObj("end_day").value;
	test_end_month = MM_findObj("end_month").value;
	test_end_year = MM_findObj("end_year").value;
	test_contract_day = MM_findObj("contract_day").value;
	test_contract_month = MM_findObj("contract_month").value;
	test_contract_year = MM_findObj("contract_year").value;
	
	//Check start date is a valid date:
	if(test_start_day == 31&&(test_start_month==4||test_start_month==6||test_start_month==9||test_start_month==11)){
		anyproblems+= "- Start date is not a valid date.\n";	
	} else if(test_start_month==2&&(test_start_day>28)){
		checkForLeapYear = test_start_year/4;
		checkForLeapYearComparison = Math.floor(checkForLeapYear);
		if(((checkForLeapYear!=checkForLeapYearComparison)&&(test_start_day>28))||test_start_day>29){
			anyproblems+= "- Start date is not a valid date.\n";
		}
	}
	//Check end date is a valid date:
	if(test_end_day == 31&&(test_end_month==4||test_end_month==6||test_end_month==9||test_end_month==11)){
		anyproblems+= "- End date is not a valid date.\n";	
	} else if(test_end_month==2&&(test_end_day>28)){
		checkForLeapYear = test_end_year/4;
		checkForLeapYearComparison = Math.floor(checkForLeapYear);
		if(((checkForLeapYear!=checkForLeapYearComparison)&&(test_end_day>28))||test_end_day>29){
			anyproblems+= "- End date is not a valid date.\n";
		}
	}
	//Check contract date is a valid date:
	if(test_contract_day == 31&&(test_contract_month==4||test_contract_month==6||test_contract_month==9||test_contract_month==11)){
		anyproblems+= "- Contract date is not a valid date.\n";	
	} else if(test_contract_month==2&&(test_contract_day>28)){
		checkForLeapYear = test_contract_year/4;
		checkForLeapYearComparison = Math.floor(checkForLeapYear);
		if(((checkForLeapYear!=checkForLeapYearComparison)&&(test_contract_day>28))||test_contract_day>29){
			anyproblems+= "- Contract date is not a valid date.\n";
		}
	}
	if(anyproblems){
		alert('The following error(s) occurred:\n'+anyproblems);
	} else {
		MM_findObj("post_start_day").value = test_start_day;
		MM_findObj("post_start_month").value = test_start_month;
		MM_findObj("post_start_year").value = test_start_year;
		MM_findObj("post_end_day").value = test_end_day;		
		MM_findObj("post_end_month").value = test_end_month;		
		MM_findObj("post_end_year").value = test_end_year;		
		MM_findObj("post_contract_day").value = test_contract_day;		
		MM_findObj("post_contract_month").value = test_contract_month;		
		MM_findObj("post_contract_year").value = test_contract_year;	
		MM_findObj("post_details").value = MM_findObj("details").value;	
		MM_findObj("post_initials").value = MM_findObj("initials").value;	
		//Update radio button values:
		if (MM_findObj("type_of_availability")[0].checked){
			MM_findObj("post_type").value = 1
		} else if(MM_findObj("type_of_availability")[1].checked){
			MM_findObj("post_type").value = 2;
		} else {
			MM_findObj("post_type").value = 3;	
		}

		MM_findObj("hiddenForm").submit();
	}
	
}

function check_homepage_form(){ // Called from homepage
	anyproblems = "";
	test_start_day = MM_findObj("start_day").value;
	test_start_month = MM_findObj("start_month").value;
	test_start_year = MM_findObj("start_year").value;
	test_end_day = MM_findObj("end_day").value;
	test_end_month = MM_findObj("end_month").value;
	test_end_year = MM_findObj("end_year").value;
	
	// If start date is set, check start date is a valid date: (for the purposes of being set, all 3 boxes need a value
	if(test_start_day>0&&test_start_month>0&&test_start_year>0){
		if(test_start_day == 31&&(test_start_month==4||test_start_month==6||test_start_month==9||test_start_month==11)){
			anyproblems+= "- Start date is not a valid date.\n";	
		} else if(test_start_month==2&&(test_start_day>28)){
			checkForLeapYear = test_start_year/4;
			checkForLeapYearComparison = Math.floor(checkForLeapYear);
			if(((checkForLeapYear!=checkForLeapYearComparison)&&(test_start_day>28))||test_start_day>29){
				anyproblems+= "- Start date is not a valid date.\n";
			}
		}
	} else { // if(test_start_day>0&&test_start_month>0&&test_start_year>0){
		// Set all values to 0 just in case one has been set but not all
		test_start_day = 0;
		test_start_month = 0;
		test_start_year = 0;
	}
	// If end date is set, check end date is a valid date: (for the purposes of being set, all 3 boxes need a value
	if(test_end_day>0&&test_end_month>0&&test_end_year>0){
		if(test_end_day == 31&&(test_end_month==4||test_end_month==6||test_end_month==9||test_end_month==11)){
			anyproblems+= "- End date is not a valid date.\n";	
		} else if(test_end_month==2&&(test_end_day>28)){
			checkForLeapYear = test_end_year/4;
			checkForLeapYearComparison = Math.floor(checkForLeapYear);
			if(((checkForLeapYear!=checkForLeapYearComparison)&&(test_end_day>28))||test_end_day>29){
				anyproblems+= "- End date is not a valid date.\n";
			}
		}
	} else { // if(test_end_day>0&&test_end_month>0&&test_end_year>0){
		// Set all values to 0 just in case one has been set but not all
		test_end_day = 0;
		test_end_month = 0;
		test_end_year = 0;
	}
	if(anyproblems){
		alert('The following error(s) occurred:\n'+anyproblems);
	} else {
		MM_findObj("post_start_day").value = test_start_day;
		MM_findObj("post_start_month").value = test_start_month;
		MM_findObj("post_start_year").value = test_start_year;
		MM_findObj("post_end_day").value = test_end_day;		
		MM_findObj("post_end_month").value = test_end_month;		
		MM_findObj("post_end_year").value = test_end_year;		
		MM_findObj("post_minimum").value = MM_findObj("minimum_people").value;	

		MM_findObj("hiddenForm").submit();
	}
	
}

function checkMailAFriendForm(){ // Called from email_a_friend.php
	anyproblems = "";
	anyproblems = MM_validateForm('your_name','','R','your_email','','RisEmail','friends_name','','R','friends_email','','RisEmail');
	if(anyproblems){
		alert('The following error(s) occurred:\n'+anyproblems);	
	} else {
		MM_findObj("post_your_name").value = MM_findObj("your_name").value;
		MM_findObj("post_your_email").value = MM_findObj("your_email").value;
		MM_findObj("post_friends_name").value = MM_findObj("friends_name").value;
		MM_findObj("post_friends_email").value = MM_findObj("friends_email").value;
		
		MM_findObj("hiddenForm").submit();
	}
}

function checkContactForm(){ // Called from contact_rentals_chamonix.php
	anyproblems = "";
	anyproblems = MM_validateForm('user_name','','R','email','','RisEmail');
	if(anyproblems){
		alert('The following error(s) occurred:\n'+anyproblems);	
	} else {
		MM_findObj("post_name").value = escape(MM_findObj("user_name").value);
		MM_findObj("post_email").value = escape(MM_findObj("email").value);
		MM_findObj("post_telephone").value = escape(MM_findObj("telephone").value);
		MM_findObj("post_query").value = escape(MM_findObj("query").value);
		
		MM_findObj("hiddenForm").submit();
	}
}

function encode_text_to_url( tmp_text ) {

	var url = tmp_text
		.toLowerCase() // change everything to lowercase
		.replace(/^\s+|\s+$/g, "") // trim leading and trailing spaces		
		.replace(/[_|\s]+/g, "-") // change all spaces and underscores to a hyphen
		.replace(/[^a-z0-9-]+/g, "") // remove all non-alphanumeric characters except the hyphen
		.replace(/[-]+/g, "-") // replace multiple instances of the hyphen with a single instance
		.replace(/^-+|-+$/g, "") // trim leading and trailing hyphens				
		; 
	return url;	
}

function calculate_url() {
	/**
	 * Calculate friendly URL based upon Meta title entered by user
	 */

	 meta_title		= document.getElementById('input_meta_title').value;
	 friendly_url	= document.getElementById('input_friendly_url').value;
	 
	 if ( friendly_url.length===0 ) { // Only change friendly URL if it is currently empty
	 	encoded_item	= encode_text_to_url ( meta_title );
		 document.getElementById('input_friendly_url').value	= encoded_item;
	 }
}

function photo_description_changed ( ) {
	 description	= document.getElementById('input_description').value;
	 friendly_url	= document.getElementById('input_url').value;
	 encoded_item	= encode_text_to_url ( description );
	 document.getElementById('input_url').value	= encoded_item;
}
//-->
