var message = "Print this Page";
function printpage(o){
    alert("Please set your printer orientation to " + o +" and your paper size to A4")
    window.print()
}


function embedObject(flash_file, width, height) {
	document.write('<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0\" width=\"'+width+'\" height=\"'+height+'\">' +
	'    <param name=\"movie\" value=\"'+flash_file+'\" />' +
	'    <param name=\"quality\" value=\"high\" />' +
	'	 <param name=\"wmode\" value=\"transparent\" />' +
	'    <embed src=\"'+flash_file+'\" quality=\"high\" wmode=\"transparent\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\"'+width+'\" height=\"'+height+'\"></embed>' +
	'  </object>');
}
function AjaxDeleteSession(id){
	$.post('/ajax/delsess/'+id+'/', function(data){
		if(data != 'none'){
			alert(data);
		}
	});
}
function val_email(ele) {
    if (/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test($(ele).val()) == false){
        return false;
    }
    else{
	   return true;
    }
}
function formatCalc(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))	num = "0";
	
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	
	if(cents<10)	cents = "0" + cents;
	
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3)) + ',' + num.substring(num.length-(4*i+3));
	
	return (((sign)?'':'-') + '' + num);
}
function validateMLM() {
    if($('#id_first_name').val() == '' || $('#id_first_name').val() == 'Name'){
        alert('Please enter your name.');
    } else if($('#id_last_name').val() == '' || $('#id_last_name').val() == 'Surname'){
        alert('Please enter your surname.');
    } else if($('#id_email').val() == '' || $('#id_email').val() == 'Email Address'){
        alert('Please enter your email address.');
    } else if(val_email('#id_email') == false){
        alert('Please enter a valid email address.');
    } else {
        $('#MLMForm').submit();
    }
}
function validateContact() {
	
    if($('#first_name').val() == '' || $('#first_name').val() == 'First Name'){
        alert('Please enter your name.');
    } else if($('#last_name').val() == '' || $('#last_name').val() == 'Surname'){
        alert('Please enter your surname.');
    } else if($('#email').val() == '' || $('#email').val() == 'Email Address'){
        alert('Please enter your email address.');
    } else if(val_email('#email') == false){
        alert('Please enter a valid email address.');
    } else if($('#00N20000002bqT5').val() == '' || $('#00N20000002bqT5').val() == 'Contact Number'){
        alert('Please enter your contact number.');
    } else if($('#00N20000002bqQZ').val() == '' || $('#00N20000002bqQZ').val() == 'Comments'){
        alert('Please enter your comments.');
    } else {
       $('#contact').submit();
       
    }
}
function validateEmailProperty() {
    if($('#name').val() == ''){
        alert('Please enter your name.');
    } else if($('#email').val() == ''){
        alert('Please enter your email.');
    } else if(val_email('#email') == false){
        alert('Please enter a valid email address.');
    } else if($('#rec_name').val() == ''){
        alert('Please enter your recipient\'s name.');
    } else if($('#rec_email').val() == ''){
        alert('Please enter your recipient\'s email.');
    } else if(val_email('#rec_email') == false){
        alert('Please enter a valid recipient\'s email address.');

    } else {
        $('#EmailPropertyForm').submit();
    }
}

function getLocations() {
    $("#location").removeOption(/./);
    $("#location").addOption("", "Please Select");
    $.getJSON('/ajax/get-locations/',
    function(data){
        for(i=0; i<data.length; i++) {
            $('#location').addOption(data[i].name.replace(' / ','-').replace(' ','-').replace(' ','-').toLowerCase(), data[i].name + " (" + data[i].count + ")");
        }
        $("#location").val("");
    });
}

function getType(location) {
    $("#dev_type").removeOption(/./);
    $("#dev_type").addOption("", "Loading...");
    $.getJSON('/ajax/get-type/' + location + '/', function(data){
		$("#dev_type").attr("disabled", false);
		$("#dev_type").removeOption(/./);
		$("#dev_type").addOption("", "Please Select");
		for(i=0; i<data.length; i++){
			$('#dev_type').addOption(data[i].name.replace(/ /g,'-').toLowerCase(), data[i].name + " (" + data[i].count + ")");
		}
		$("#dev_type").val("");
    });
}

function searchDevelopments() {
    
    $('#price_to').val($('#price_to_slider').val().substr(2).replace(',', '').replace(',', ''));
    
    if ($('#location').val()=="") {
        alert("Please select a location type.");
    } else if ($('#dev_type').val()=="") {
        alert("Please select a leaseback type.");
    } else if ($('#price_to').val()=="0") {
        alert("Please select a maximum price.");
    } else {
        window.location = '/results/' + $('#location').val() + '/' + $('#dev_type').val() + '/'  + $('#price_to').val() + '/'
    }
}

function set_lang(lang, reload){
	
	
	$.ajax({
        url: "/ajax/set-lang/" + lang + "/",
        async: false,
        success: function(msg){
			if(msg == ''){
				alert('Your language preference could not be set.');
			}
			else{
				if(msg == 'french'){
					
					$('#id_lang_french').removeClass('fade');
					$('#id_lang_english').addClass('fade');
				}
				else if (msg == 'english'){
					$('#id_lang_english').removeClass('fade');
					$('#id_lang_french').addClass('fade');
				}
				
				new_location=reload
				window.location= new_location
				
			}
		}
    });
}
function validateseminar() {
	
    if($('#contact_first_name').val() == '' || $('#contact_first_name').val() == 'First Name'){
        alert('Please enter your name.');
    } else if($('#contact_last_name').val() == '' || $('#contact_last_name').val() == 'Surname'){
        alert('Please enter your surname.');
    } else if($('#contact_number').val() == 'Tel Number eg. +44 (0)203 174 0155' || $('#contact_number').val() == ''){
        alert('Please enter your contact number.');
    } else if($('#contact_email').val() == '' || $('#contact_email').val() == 'Email Address'){
        alert('Please enter your email address.');
    }  else if($('#id_seminar_date_time').val() == '' ){
        alert('Please enter select a seminar date.');
    }  else if(val_email('#contact_email') == false){
        alert('Please enter a valid email address.');
	} else if($('#contact_comments').val() == '' || $('#contact_comments').val() == 'Comments'){
        alert('Please enter your comments.');
    } else {
		
        $('#contactseminar').submit();
    }
}


function validate_competition() {
	
	if($('#contact_name').val() == '') {
        alert('Please enter your name.');
	}
	else if(val_email('#email_address') == false || $('#email_address') =='' ){
        alert('Please enter a valid email address.');
	}
	else if($('#contact_number').val() == '') {
        alert('Please enter your contact number.');
	}
	else if($('#contact_number').val() == '') {
        alert('Please enter your contact number.');
	}
	else if (!($('#tc').is(':checked'))) {
		alert('You must accept the terms and conditions in order to continue')	
	}
	else {
		$('#contact').submit()
	}
}

function hide_show(value) {
	
	for (j=2;j<=50;j++){
		 $('#ref'+j).remove()
	}
	for (i=1;i<=value;i++) {
	new_ref = "<div id='ref" + i+ "' class='column last span-18'"
	
	new_ref += "style=\"margin-left:0px; vertical-align:middle;\">";	
		
	if (i <10) {
	new_ref +="<span style=\"padding-right:8px;\">"


	}
	new_ref +="Referral " +i+" </span> &nbsp;&nbsp; <input id='id_fname" + i +"'maxlength=\"50\"'" + "'" + ' ' +" name='fname" + i +"' class='textbox vTextField2' type='text' value='First Name' onfocus=\"if (this.value=='First Name') this.value=''\" onblur=\"if (this.value=='') this.value='First Name'\"/>"
	new_ref +="&nbsp;<input id='id_lname" + i + "'" + ' ' +" name='lname" + i + "'maxlength=\"50\"'" +"' class='textbox vTextField2' type='text' value='Last Name' onfocus=\"if (this.value=='Last Name') this.value=''\" onblur=\"if (this.value=='') this.value='Last Name'\"/>"
	new_ref +="&nbsp;<input id='id_email" + i + "'" + ' ' +" name='email" + i +"'maxlength=\"80\"'" +"' class='textbox vTextField2' type='text' value='Email Address' onfocus=\"if (this.value=='Email Address') this.value=''\" onblur=\"if (this.value=='') this.value='Email Address'\"/>"
	new_ref += "</div>"
	
	$(new_ref).insertAfter('#ref' + (i-1));
	
	}
}

function validate_referrals(index) {
	
	for (i=1;i<=index;i++) {
		
		if($('#id_fname'+i).val() == 'First Name') {
			
			alert("Please enter the first name of Referral " + i)
			break;
		}
		else if($('#id_lname'+i).val() == 'Last Name') {
			
			alert("Please enter the last name of Referral " + i)
			break;
		}
		
		else if($('#id_email'+i).val() == 'Email Address') {
			
			alert("Please enter a valid email address for Referral " + i)
			break;
		}
		
		else if(val_email('#id_email'+ i) == false) {
			
			alert("Please enter a valid email address for Referral " + i)
			break;
		}
	
	}
	
	// The loop stops at one value higher than the index in order to reach here ...
	var counter = i-1
	
	if (counter==index) {
			$("#referrals").attr("action",'/send-referrals/' +index + '/'); 
			$('#referrals').submit()
	}
	
		
	}

function trim(text){
    return text.replace(/^\s+|\s+$/g, '');
}

function ValidateEmailNewsForm(){
    if (trim($("#id_name").val()) == ""){
        alert("Please enter your name");
        
    } else if ($("#id_email").val() == "") {
        alert("Please enter your email address");
        
    } else if(val_email('#id_email') == false) {
		alert("Please enter a valid email address")
	
	} else if ($("#id_recname").val() == ""){
        alert("Please enter a recipient's name");
        
    }  else if ($("#id_rec_email").val() == "") {
        alert("Please enter the recipient's email address");
    
    } else if(val_email('#id_rec_email') == false) {
		alert("Please enter a valid recipient email address")
        
    } else  if ($("#id_comments").val() == ""){
        alert("Please fill in a comment");
        
    } else {
        $("#id_email_news_form").submit();
    }
}
