window.onload = exGeForms;

ShowTooltip = function(e) {
	if ( !jQuery(this).data( 'tooltip' ) ) { 
		var text = jQuery(this).next('.show-tooltip-text');
		jQuery(this).data( 'tooltip', text.get(0) );
		document.body.appendChild( jQuery(this).data( 'tooltip' ) );
	}
	else {
		var text = jQuery( jQuery(this).data( 'tooltip' ) );
	}
	text.fadeIn().css('top', e.pageY).css('left', e.pageX+15);
	return false;
}
HideTooltip = function(e) {
	jQuery( jQuery(this).data( 'tooltip' ) ).fadeOut('fast');
}

SetupTooltips = function() {
	jQuery('.show-tooltip').each(function(){
		jQuery(this).after(jQuery('<span/>').attr('class', 'show-tooltip-text').html(jQuery(this).attr('title'))).attr('title', '');
	}).hover(ShowTooltip, HideTooltip);
}

jQuery(document).ready(function() {
	SetupTooltips();
});	

function exGeForms() {	
	if(document.getElementById('otherContainer')) {
		titleDrop = document.getElementById('title');
		otherCont = document.getElementById('otherContainer');
		otherInpu = document.getElementById('title_other');
		
		otherCont.style.display = "none";
		
		titleDrop.onchange = loadTitle;
	}
	
	if(document.getElementById('corrAddField')) {
		corrField = document.getElementById('corrAddField');
		corrField.style.display = "none";

		dispCorrT = document.getElementById('corr_address_choose_no');
		dispCorrF = document.getElementById('corr_address_choose_yes');

		dispCorrT.onfocus = function() { showHideCorrAdd("show");}
		dispCorrF.onfocus = function() { showHideCorrAdd("hide"); }
	}
	
	if(document.getElementById('payment_amount')) {
		mType = new Array();
		mType[0] = document.getElementById('meter_type_standard');
		mType[1] = document.getElementById('meter_type_e7');

		mType[0].onfocus = function() { defMonthAmount(0);}
		mType[1].onfocus = function() { defMonthAmount(1);}
	}
	
	//add Poscoder form and frame to site
	buildPostcoder();
}

function defMonthAmount(focType) {
	prices = new Array();
	
	prices[0] = 50;
	prices[1] = 71;
	
	inpPayAmount = document.getElementById('payment_amount');
	
	inpPayAmount.value = prices[focType];
}

function showHideCorrAdd(frmAct) {
	if(frmAct == "hide")
		corrField.style.display = "none";
	else if(frmAct == "show")
		corrField.style.display = "block";
		
	corrInp = new Array("corr_pcode","corr_number","corr_address","corr_town");
	
	for(k = 0; k < corrInp.length; k++) {
		if(frmAct == "show")
			document.getElementById(corrInp[k]).className += " required";
		else if(frmAct == "hide") {
			if(corrInp[k] == "corr_pcode")
				cnme = "noWidth";
			else
				cnme = "";
			
			document.getElementById(corrInp[k]).className = cnme;
		}
	}
	
}

function buildPostcoder() {
	//Name of the div to add postcode info to
	var pcDivName = "postcoderHTML";
	
	if(document.getElementById(pcDivName)) {
		
		pcDiv = document.getElementById(pcDivName);
		
		pcHTML = '<form name="S2SForm" method="post" action="/getThrfareAddresses.php" target="addresslookup">';
		pcHTML += '<input type="hidden" name="postcode" />';
		pcHTML += '<input type="hidden" name="pcFormNum" />';
		pcHTML += '<input type="hidden" name="identifier" value="Get Good Energy Form Addresses" />';
		pcHTML += '</form>';
		pcHTML += '<iframe src="blank.html" class="lookupframe" id="addresslookup" name="addresslookup" width="1" height="1" scrolling="no" frameborder="0"></iframe>';
		
		pcDiv.innerHTML = pcHTML;
	}
}

function loadTitle() {
	if(titleDrop.value == 'Other') {
		otherCont.style.display = "block";
		otherInpu.className = "required";
	}
	else {
		otherCont.style.display = "none";
		otherInpu.className = "";
	}
}