var numberOfProductsSelected = 0;
var businessProductSelected = false;
var showCalcStage2PopupOnLoad = false;
var showContactPopupOnLoad = false;
var showCheckoutPopupOnLoad = false;
var showSendToFriendPopupOnLoad = false;
var showFreePhoneSystemPopupOnLoad = false;
var newConnectionCount = 0;
var isNewConnection = false;

function hiliteNav( obj ) {
	obj.previousClassName = obj.className;
	obj.className = 'navLinkOver';
}
function loliteNav( obj ) {
	obj.className = obj.previousClassName;
}

function launchLiveChat() {
}

function featuredProductClick( chkd ) {
	var selectableProducts = YAHOO.util.Dom.getElementsByClassName('selectableProduct');
	for ( var i in selectableProducts ) {
		if ( selectableProducts[i].id == 'selectableProduct_' + businessProductId ) {
			selectableProducts[i].checked = chkd;
			businessProductSelected = chkd;
		}
	}
	if ( chkd ) {
		numberOfProductsSelected++;
	} else {
		numberOfProductsSelected--;
	}
}

function validatePriceComparisonCalcStage1() {
	var validator = new validateForm();
	validator.checkText( 'calc_average_monthly_spend', 'Average monthly spend' );
	validator.checkSelect( 'calc_includes_rental', '', 'Does the line include rental' );
	validator.checkText( 'calc_number_of_lines', 'Number of lines' );
	validator.checkText( 'calc_monthly_spend_mobiles', 'Monthly spend to mobiles' );
	validator.checkSelect( 'calc_current_contract_length', '', 'Current contract length' );
	validator.checkSelect( 'calc_current_supplier', '', 'Current supplier' );
	
	if ( document.getElementById('calc_average_monthly_spend').value == '£' ) {
		validator.addCustomError( 'Average monthly spend' );
	}
	
	if ( document.getElementById('calc_monthly_spend_mobiles').value == '£' ) {
		validator.addCustomError( 'Monthly spend to mobiles' );
	}
	
	if ( validator.numberOfErrors() > 0 ) {
		validator.displayErrors();
	} else {
		showCalcStage2Popup();
	}

	// always return false so the popup gets displayed
	return false;
}

function validatePriceComparisonCalcStage2() {
	var validator = new validateForm();
	validator.checkText( 'calc2_name', 'Name' );
	validator.checkText( 'calc2_company_name', 'Company Name' );
	validator.checkText( 'calc2_telephone_number', 'Telephone Number' );
	validator.validateEmailAddress( 'calc2_email_address', 'Email Address' );
	validator.checkText( 'calc2_landline_number', 'Landline number the enquiry is regarding' );
	validator.validatePostCode( 'calc2_landline_number_postcode', 'Post Code for the landline number' );
	if ( validator.numberOfErrors() > 0 ) {
		validator.displayErrors();
		return false;
	} else {
		// populate the hidden inputs in the orignal form with the details from this form
		document.getElementById('calc_name').value = document.getElementById('calc2_name').value;
		document.getElementById('calc_company_name').value = document.getElementById('calc2_company_name').value;
		document.getElementById('calc_telephone_number').value = document.getElementById('calc2_telephone_number').value;
		document.getElementById('calc_email_address').value = document.getElementById('calc2_email_address').value;
		// submit the stage 1 form
		document.getElementById('priceComparisonCalcStage1Form').submit();
		return false;
	}
}

function validateCheckoutStage2() {
	var validator = new validateForm();
	validator.checkText( 'basket_name', 'Name' );
	validator.checkText( 'basket_company_name', 'Company Name' );
	validator.checkText( 'basket_telephone_number', 'Telephone Number' );
	validator.validateEmailAddress( 'basket_email_address', 'Email Address' );
	if ( validator.numberOfErrors() > 0 ) {
		validator.displayErrors();
		return false;
	} else {
		return true;
	}
}

function validateContactForm() {
	var validator = new validateForm();
	validator.checkText( 'contact_name', 'Name' );
	validator.validateEmailAddress( 'contact_email_address', 'Email Address' );
	validator.checkText( 'contact_message', 'Enquiry' );
	if ( validator.numberOfErrors() > 0 ) {
		validator.displayErrors();
		return false;
	} else {
		return true;
	}
}

function validateCallBack() {
	var validator = new validateForm();
	var validator2 = new validateForm();
	validator.checkText( 'callBackName', 'Name' );
	validator2.checkText( 'callBackTelephone', 'Telephone' );
	validator2.validateEmailAddress( 'callBackEmail', 'Email' );
	
	if ( validator2.numberOfErrors() > 1 ) {
		validator.addCustomError( 'Either your email address or telephone number' );
	}
	
	if ( validator.numberOfErrors() > 0 ) {
		validator.displayErrors();
		return false;
	} else {
		return true;
	}
}

function validateBecomeADealer() {
	var validator = new validateForm();
	var validator2 = new validateForm();
	validator.checkText( 'badTitle', 'Title' );
	validator.checkText( 'badFirstName', 'First Name' );
	validator.checkText( 'badLastName', 'Last Name' );
	validator.validatePostCode( 'badPostCode', 'Post Code' );
	validator2.checkText( 'badTelephone', 'Telephone' );
	validator2.validateEmailAddress( 'badEmail', 'Email' );
	
	if ( validator2.numberOfErrors() > 1 ) {
		validator.addCustomError( 'Either your email address or telephone number' );
	}
	
	if ( validator.numberOfErrors() > 0 ) {
		validator.displayErrors();
		return false;
	} else {
		return true;
	}
}

function initCalcStage2Popup() {
	calcStage2Box = 
			new YAHOO.widget.Panel("calcStage2Panel",  
											{ width:"323px", 
											  height:"386px", 
											  fixedcenter:true, 
											  close:false, 
											  draggable:false, 
											  modal:true,
											  visible:false,
											  underlay:"none",
											  effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.5} 
											} 
										);

	calcStage2Box.render(document.body);
	if( showCalcStage2PopupOnLoad ) {
		showCalcStage2Popup();
	}
}

function hideCalcStage2Popup() {
	if( typeof calcStage2Box != "undefined" )
		calcStage2Box.hide();
}

function showCalcStage2Popup() {
	if( typeof calcStage2Box != "undefined" ) {
		YAHOO.util.Dom.setStyle( 'calcStage2Panel', 'display', 'block' );
		calcStage2Box.show();
	} else {
		showCalcStage2PopupOnLoad = true;
	}
}

function initContactPopup() {
	contactBox = 
			new YAHOO.widget.Panel("contactFormPanel",  
											{ width:"323px", 
											  height:"346px", 
											  fixedcenter:true, 
											  close:false, 
											  draggable:false, 
											  modal:true,
											  visible:false,
											  underlay:"none",
											  effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.5} 
											} 
										);

	contactBox.render(document.body);
	if( showContactPopupOnLoad )
		contactBox.show();
}

function hideContactPopup() {
	if( typeof contactBox != "undefined" )
		contactBox.hide();
}

function showContactPopup() {
	if( typeof contactBox != "undefined" ) {
		YAHOO.util.Dom.setStyle( 'contactFormPanel', 'display', 'block' );
		contactBox.show();
	} else {
		showContactPopupOnLoad = true;
	}
}

function initCheckoutPanel() {
	checkoutBox = 
			new YAHOO.widget.Panel("checkoutPanel",  
											{ width:"323px", 
											  height:"346px", 
											  fixedcenter:true, 
											  close:false, 
											  draggable:false, 
											  modal:true,
											  visible:false,
											  underlay:"none",
											  effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.5} 
											} 
										);

	checkoutBox.render(document.body);
	if( showCheckoutPopupOnLoad )
		checkoutBox.show();
}

function hideCheckoutPopup() {
	if( typeof checkoutBox != "undefined" )
		checkoutBox.hide();
}

function showCheckoutPopup() {
	if( typeof checkoutBox != "undefined" ) {
		YAHOO.util.Dom.setStyle( 'checkoutPanel', 'display', 'block' );
		checkoutBox.show();
	} else {
		showCheckoutPopupOnLoad = true;
	}
}

function initSendToFriendPanel() {
	sendToFriendBox = 
			new YAHOO.widget.Panel("sendToFriendPanel",  
											{ width:"323px", 
											  height:"346px", 
											  fixedcenter:true, 
											  close:false, 
											  draggable:false, 
											  modal:true,
											  visible:false,
											  underlay:"none",
											  effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.5} 
											} 
										);

	sendToFriendBox.render(document.body);
	if( showSendToFriendPopupOnLoad )
		sendToFriendBox.show();
}

function hideSendToFriendPopup() {
	if( typeof sendToFriendBox != "undefined" )
		sendToFriendBox.hide();
}

function showSendToFriendPopup() {
	if( typeof sendToFriendBox != "undefined" ) {
		YAHOO.util.Dom.setStyle( 'sendToFriendPanel', 'display', 'block' );
		sendToFriendBox.show();
	} else {
		showSendToFriendPopupOnLoad = true;
	}
}

function initFreePhoneSystemPanel() {
	freePhoneSystemBox = 
			new YAHOO.widget.Panel("freePhoneSystemPanel",  
											{ width:"403px", 
											  height:"350px", 
											  fixedcenter:true, 
											  close:false, 
											  draggable:false, 
											  modal:true,
											  visible:false,
											  underlay:"none",
											  effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.5} 
											} 
										);

	freePhoneSystemBox.render(document.body);
	if( showFreePhoneSystemPopupOnLoad )
		showFreePhoneSystemPopup();
}

function hideFreePhoneSystemPopup() {
	if( typeof freePhoneSystemBox != "undefined" )
		freePhoneSystemBox.hide();
}

function showFreePhoneSystemPopup() {
	if( typeof freePhoneSystemBox != "undefined" ) {
		YAHOO.util.Dom.setStyle( 'freePhoneSystemPanel', 'display', 'block' );
		freePhoneSystemBox.show();
	} else {
		showFreePhoneSystemPopupOnLoad = true;
	}
}

function changeProductQty( chkd, productId ) {
	document.getElementById( 'CMSCatalogueBasket_product_quantity_' + productId ).value = ( chkd ) ? 1 : 0;
	if ( productId == businessProductId ) {
		businessProductSelected = chkd;
	}
}


function inputBoxFocus( input, defaultText, passwordField ) {
	if( typeof( passwordField ) != 'undefined' ) {
		document.getElementById( passwordField ).style.display = '';
		document.getElementById( passwordField+'_text' ).style.display = 'none';
		document.getElementById( passwordField ).focus();
	} else {
		if( input.value == defaultText ) {
			input.value = '';
		}
	}
}

function inputBoxBlur( input, defaultText, passwordField ) {
	if( input.value == '' ) {
		if( typeof( passwordField ) != 'undefined' ) {
			document.getElementById( passwordField ).style.display = 'none';
			document.getElementById( passwordField+'_text' ).style.display = '';
		} else {
			input.value = defaultText;
		}
	}
}

function fadeInBasket() {
	var anim = new YAHOO.util.Anim('dynamicBasketContents', { opacity: { from: 0, to: 1 } }, 1, YAHOO.util.Easing.easeOut);
	anim.animate();
}

function addToBasket() {
	var handleSuccess = function(o){
		// increment the number of new connections we have in the basket
		if ( isNewConnection ) {
			newConnectionCount++;
		}
		// update the basket table
		document.getElementById('dynamicBasketContents').innerHTML = o.responseText;
		// fade the table in
		fadeInBasket();
		// show the details of the latest item added
		showLatestProductAdded();
		// show the next step button
		document.getElementById('nextStepButton').style.display = '';
		// update the totals
		document.getElementById('checkoutTotals').innerHTML = document.getElementById('checkoutTotalsHidden').innerHTML;
		document.getElementById('checkoutTotals').style.display = '';
		// reset the form inputs
		resetBasketForm();
	};
	var handleFailure = function(o){
		alert( 'Sorry, an error has occured' );
	};
	var callback = {
		success:handleSuccess,
		failure:handleFailure
	};
	if ( submitBasket() ) {
		var formObject = document.getElementById( 'basketForm' );
		YAHOO.util.Connect.setForm( formObject );
		var cObj = YAHOO.util.Connect.asyncRequest('POST', '/manage_basket.php', callback);
	}
	// don't submit the form
	return false;
}

function mouseoverSubmit( el ) {
	el.src = '/custom/images/submitbtnover.gif';
}
function mouseoutSubmit( el ) {
	el.src = '/custom/images/submitbtn.gif';
}

function validateFreePhoneSystemForm( suffix ) {
	var validator = new validateForm();
	validator.checkText( 'freeSystem' + suffix + '_company', 'Company' );
	validator.checkText( 'freeSystem' + suffix + '_contact', 'Contact' );
	validator.validateEmailAddress( 'freeSystem' + suffix + '_email', 'Email' );
	validator.checkText( 'freeSystem' + suffix + '_phone', 'Phone Number' );
	validator.checkText( 'freeSystem' + suffix + '_lineRental', 'Line rental charges per month', '£' );
	validator.checkText( 'freeSystem' + suffix + '_callCharges', 'Call charges per month', '£' );
	if ( validator.numberOfErrors() > 0 ) {
		validator.displayErrors();
		return false;
	} else {
		return true;
	}
}

YAHOO.util.Event.addListener( window, 'load', initContactPopup );
//YAHOO.util.Event.addListener( window, 'load', initSendToFriendPanel );
YAHOO.util.Event.addListener( window, 'load', initCalcStage2Popup );
