// rules for behaviour.js 

var change_count=0;

var glide_tooltips = $H({
	'#tenant_street':'This should only contain the street name, not your house number.',
	'#tenant_postcode':'We need to know where the house is so we can get you the best prices for your area.<br /><br />If you don\'t know your postcode we can help locate it. Just tick the box',
	'#tenant_no_of_tenants':'We need to know how many people in the house so we can work out how many services you will consume every month.',
	'#tenant_contract_duration':'We need to know how long your contract is so that you don\'t pay too much!',
	'.gas_electricity_service':'These are the essentials and without these you won’t be able to make food, have a shower or listen to music.',
	'.tv_license_service':'If you’ve got a TV in your home, you need a TV licence or you’ll get a big fine. Make sure you’re covered.',
	'.gas_service':'These are the essentials and without these you won’t be able to make food, have a shower or listen to music.',
	'.electricity_service':'These are the essentials and without these you won’t be able to make food, have a shower or listen to music.',
	'.water_service':'These are the essentials and without these you won’t be able to make food, have a shower or listen to music.',
	'.telephone_line_service':'Make extremely cheap calls and also required for broadband.',
	'.broadband_service':'Broadband is essential for any home. Download music, watch YouTube or catch up with your mates on Facebook. Our free wireless router means you can connect wirelessly throughout your home.',
	'.tenant_password':'This password will be used to access your online portal where you can manage your Glide account.',
	'#tenant_billing_postcode':'Please enter the postcode of your billing address i.e. the address that your card is registered to.',
	'.broadband_package_base':'Ideal for homes using the internet for just email and surfing',
	'.broadband_package_mid':'Mid package for people who do a bit more downloading',
	'.broadband_package_max':'Max package for those who like downloading music',
	'.cardholders_name':'Please enter the name as it appears on the card',
	'.card_issue_number':'Older Switch/Maestro cards only',
	'.card_cv2':'Please enter the last 3 digits on the signature strip of your card'
});

var tooltip_rules = {};

glide_tooltips.each(function(pair) {
	tooltip_rules[pair.key] = function(element) {
		new Tip(element, pair.value, {className: 'tooltip', hook:{target: 'topRight', tip: 'topLeft'}});
	};
});

Behaviour.add(tooltip_rules);

var myrules = {
	'input.datefield': function(element) {
		var button = document.createElement('img');
		button.src = '/themes/default/graphics/calendar.gif';
		button.alt = 'Choose Date';
		button.id = element.id+'_button';
		element.parentNode.appendChild(button);
		var options= {
			ifFormat: "%d/%m/%Y",
			showsTime: false,
			step:1,
			button: button.id,
			inputField: element.id,
			firstDay:1
		};
		var cal = Calendar.setup(options);
	},
	'input.login_text_box': function(element) {
		element.observe('focus',
			function(e) {
				if (this.value == 'email')
					this.value = '';
			}.bindAsEventListener(element)
		);
		element.observe('blur',
			function(e) {
				if (this.value == '')
					this.value = 'email';
			}.bindAsEventListener(element)
		);
	},
	'#home_header': function(element) {
		var FO = { movie:"/themes/default/graphics/preloader.swf", width:"798", height:"151",majorversion:"6", build:"0"};
		UFO.create(FO, element.id);
	},
	'#tenants_sign_up_form': function(element) {
		Event.observe(element, 'submit',
			function(e) {
				Event.stop(e);
				var pb = $('tenants_sign_up_form').serialize();
				var url = element.action + 'ajax/';
				$('main_section_main').innerHTML = '<div id="loading"><img src="/themes/default/graphics/ajax-loader.gif" alt="Loading..." /><p>Loading...</p></div>';
				new Ajax.Request(url, {
					postBody: pb,
					onComplete: function(xhr) {
						$('main').innerHTML = xhr.responseText;
						Behaviour.go($('main'));
						change_count++;
						if (change_count%2 == 0) {
							var loc = '/themes/default/graphics/headers/'+(Math.floor((Math.random())*4)+1)+'.jpg';
							$('header').down('img').src = loc;
						}
					}
				});
			}
		);
	},
	'a.tenant_sign_up_button': function(element) {
		Event.observe(element, 'click',
			function(e) {
				url = element.href + 'ajax/';
				$('main_section_main').innerHTML = '<div id="loading"><img src="/themes/default/graphics/ajax-loader.gif" alt="Loading..." /><p>Loading...</p></div>';
				new Ajax.Request(url, {
					onComplete: function(xhr) {
						$('main').innerHTML = xhr.responseText;
						Behaviour.go($('main'));
						change_count++;
						if (change_count%2 == 0) {
							var loc = '/themes/default/graphics/headers/'+(Math.floor((Math.random())*4)+1)+'.jpg';
							$('header').down('img').src = loc;
						}
					}
				});
				Event.stop(e);
			}
		);
	},
	'#contact_us_map': function(element) {
		var map = new GMap2(element);
        map.setCenter(new GLatLng(52.48702, -1.912887), 13);
        map.setZoom(15);
        var marker = new GMarker(new GLatLng(52.486824, -1.912909));
      	map.addOverlay(marker);
	},
	'#tenant_payment_party_self': function(element) {
		if (element.checked) {
			$('tenant_third_party_first_name').setAttribute('disabled','disabled');
			$('tenant_third_party_surname').setAttribute('disabled','disabled');
			$('tenant_third_party_email').setAttribute('disabled','disabled');
			$('tenant_third_party_mobile').setAttribute('disabled','disabled');
		}
		else {
			$('tenant_billing_postcode').setAttribute('disabled','disabled');
		}
		Event.observe(element,'click',function(e) {
			$('tenant_third_party_first_name').setAttribute('disabled','disabled');
			$('tenant_third_party_surname').setAttribute('disabled','disabled');
			$('tenant_third_party_email').setAttribute('disabled','disabled');
			$('tenant_third_party_mobile').setAttribute('disabled','disabled');		
			$('tenant_billing_postcode').removeAttribute('disabled');
		});
		Event.observe($('tenant_payment_party_third'),'click',function(e) {
			$('tenant_third_party_first_name').removeAttribute('disabled');
			$('tenant_third_party_surname').removeAttribute('disabled');
			$('tenant_third_party_email').removeAttribute('disabled');
			$('tenant_third_party_mobile').removeAttribute('disabled');
			$('tenant_billing_postcode').setAttribute('disabled','disabled');
		});
	},
	'#tenant_find_postcode': function(element) {
		if (element.checked) {
			if ($('tenant_postcode')) {
				$('tenant_postcode').setAttribute('disabled','disabled');
			}
			if ($('tenant_billing_postcode')) {
				$('tenant_billing_postcode').setAttribute('disabled','disabled');
			}
			if ($('tenant_unique_delivery_id')) {
				$('tenant_unique_delivery_id').setAttribute('disabled','disabled');
			}
		}
		Event.observe(element,'click',function(e) {
			if (element.checked) {
				if ($('tenant_postcode')) {
					$('tenant_postcode').setAttribute('disabled','disabled');
				}
				if ($('tenant_billing_postcode')) {
					$('tenant_billing_postcode').setAttribute('disabled','disabled');
				}
				if ($('tenant_unique_delivery_id')) {
					$('tenant_unique_delivery_id').setAttribute('disabled','disabled');
				}
			}
			else {
				if ($('tenant_postcode')) {
					$('tenant_postcode').removeAttribute('disabled');
				}
				if ($('tenant_billing_postcode')) {
					$('tenant_billing_postcode').removeAttribute('disabled');
				}
				if ($('tenant_unique_delivery_id')) {
					$('tenant_unique_delivery_id').removeAttribute('disabled');
				}
			}
		});
	},
	'#tenant_can_contact': function(element) {
		Event.observe(element,'click',function(e) {
			if (!element.checked) {
				var uncheck = confirm('By unticking this box, we will be unable to provide you with offers from our chosen partners. Offers can include free alcohol.');
				if (!uncheck) {
					element.checked = true;
				}
			}
		});
	},
	'#home_menu_image': function(element) {
		Event.observe(element,'mouseover',function(e) {
			element.setAttribute('src','/themes/default/graphics/home_button_selected.png');
		});
		Event.observe(element,'mouseout',function(e) {
			element.setAttribute('src','/themes/default/graphics/home_button.png');
		});
	},
	'.service_image': function(element) {
		element.observe('click',function(e) {
			var checkbox = this.up('td').next('td').down('input');
			checkbox.checked = !checkbox.checked;
		});
	},
	'#tenant_business_source': function(element) {
		$('tenant_business_source_other').style.display = 'none';
		Event.observe(element,'change',function(e) {
			if (element.value == 'Other') {
				$('tenant_business_source_other').style.display = 'table-row';
			}
			else {
				$('tenant_business_source_other').style.display = 'none';
			}
		});
	},
	'#refer_friend_add_row': function(element) {
		var start_row = 1;
		element.observe('click',function(e) {
			var new_row = Builder.node('tr');
			var template_row = $('refer_friend_template_row');
			new_row.innerHTML = template_row.innerHTML;
			start_row++;
			var all_inputs = new_row.getElementsByTagName('input');
			$A(all_inputs).each(function(ef){
				ef.name = ef.name.replace('1',start_row);
			});
			$('refer_friend_table').down('tbody').appendChild(new_row);
		});
	},
	'.show_services_link': function(element){
		$$('.show_services_cell').each(function(e){e.show()});
		$$('.services_column').each(function(e){e.hide()});
		$$('.rowspan_one').each(function(e){
			e.writeAttribute('rowspan', '1');
		});
		var id = $(element).id;
		element.observe('click', function(e){
			Event.stop(e);
			//hide itself
			$('show_services_portfolio_'+id).hide();
			//show hide link
			$('hide_services_portfolio_'+id).show();
			//alter rowspans
			if($('rowspan_value_'+id)){
				var rowspan_count = $F('rowspan_value_'+id);
			} else {
				var rowspan_count = 7;
			}
			$$('.rowspan_'+id).each(function(e){
				e.writeAttribute('rowspan', rowspan_count);
			});
			//show services
			$$('.services_'+id).each(function(f){
				f.show();
			});
		});
	},
	'.hide_services_link': function(element){
		var id = $(element).id;
		element.observe('click', function(e){
			Event.stop(e);
			//hide itself
			$('hide_services_portfolio'+id).hide();
			//show show link
			$('show_services_portfolio'+id).show();
			//alter rowspan
			$$('.rowspan'+id).each(function(e){
				e.writeAttribute('rowspan', '1');
			});
			//hide services
			$$('.services'+id).each(function(f){
				f.hide();
			});
		});
	},
	'#select_meter_reads': function(el) {
		el.observe('change', function(e) {
			if(el.value == 'gas'){
				$$('.view_gas_reads').invoke('show');
				document.getElementById('view_gas_reads').style.display = '';
				$$('.view_elec_reads').invoke('hide');
				document.getElementById('view_elec_reads').style.display = 'none';
				$$('.view_water_reads').invoke('hide');
				document.getElementById('view_water_reads').style.display = 'none';
			}
			else if(el.value == 'elec'){
				$$('.view_gas_reads').invoke('hide');
				document.getElementById('view_gas_reads').style.display = 'none';
				$$('.view_elec_reads').invoke('show');
				document.getElementById('view_elec_reads').style.display = '';
				$$('.view_water_reads').invoke('hide');
				document.getElementById('view_water_reads').style.display = 'none';
			}
			else if(el.value == 'water'){
				$$('.view_gas_reads').invoke('hide');
				document.getElementById('view_gas_reads').style.display = 'none';
				$$('.view_elec_reads').invoke('hide');
				document.getElementById('view_elec_reads').style.display = 'none';
				$$('.view_water_reads').invoke('show');
				document.getElementById('view_water_reads').style.display = '';
			}
			else if(el.value == 'none'){
				$$('.view_gas_reads').invoke('hide');
				document.getElementById('view_gas_reads').style.display = 'none';
				$$('.view_elec_reads').invoke('hide');
				document.getElementById('view_elec_reads').style.display = 'none';
				$$('.view_water_reads').invoke('none');
				document.getElementById('view_water_reads').style.display = 'none';
			}
		}
	)},
	'#contract_start_date': function(element){
		var id = $(element).id;
		element.observe('change', function(e){
			Event.stop(e);
			var selects = document.getElementsByTagName('select');
			var duration = document.getElementById('tenant_contract_duration').value;

			if ( selects[0].options[selects[0].selectedIndex].value <= 7 ) duration = duration - 1 ;

			var myDate = new Date();
			myDate.setFullYear(selects[2].options[selects[2].selectedIndex].value);
			myDate.setMonth(parseInt(selects[1].selectedIndex) + parseInt(duration) );

			var month=new Array(12);
			month[0]="January";
			month[1]="February";
			month[2]="March";
			month[3]="April";
			month[4]="May";
			month[5]="June";
			month[6]="July";
			month[7]="August";
			month[8]="September";
			month[9]="October";
			month[10]="November";
			month[11]="December";

			document.getElementById('contract_end_date').innerHTML = month[parseInt(myDate.getMonth())] + " " + myDate.getFullYear();
			document.getElementById('tenant_end_month').value = "01/" + (parseInt(myDate.getMonth())+1) + "/" + myDate.getFullYear();

		});
	},
	'#ticket_send_email':function(a){
		a.observe('click', function(e) {
			Event.stop(e);
			var old_value = a.innerHTML;
			var dd = a.parentNode;
			var ticket_hash = document.getElementById('ticket_hash').value;
			window.location = '/tickets.php?t=' + ticket_hash + '&toggle_send_email=' + a.checked;
		});
	},
	'#ticket_send_text':function(a){
		a.observe('click', function(e) {
			Event.stop(e);
			var old_value = a.checked;
			var dd = a.parentNode;

			
			var ticket_hash = document.getElementById('ticket_hash').value;
			window.location = '/tickets.php?t=' + ticket_hash + '&toggle_send_text=' + a.checked;
		});
	},

};
Behaviour.add(myrules);







