$(function(){

	$("input:text:visible:first").focus();

	$('div.aubrey a, div.misra a, div.turner a, li.express a, li.professional a, li.elite a, li.bespoke a').click(function(){
		return false;
	});	
	
	$('div.aubrey').qtip({
		content: { url: 'alanaubreybio.php', method: 'get', prerender: true },
		show: 'mouseover', 
		hide: { when: 'mouseout', delay: 500, fixed: true },
		position: { corner: { target: 'topMiddle', tooltip: 'bottomMiddle' } },
		style: {
			width: 500,
			height: 400,
			border: {
				width: 7,
				radius: 5,
				color: '#92C238'
			},
			tip: 'bottomMiddle'
		}
	});
	
	$('div.misra').qtip({
		content: { url: 'sunilmisrabio.php', method: 'get', prerender: true },
		show: 'mouseover', 
		hide: { when: 'mouseout', delay: 500, fixed: true },
		position: {
			corner: {
				target: 'topMiddle',
				tooltip: 'bottomMiddle'
			}
		},
		style: {
			width: 500,
			height: 400,
			border: {
				width: 7,
				radius: 5,
				color: '#17BDE3'
			},
			tip: 'bottomMiddle'
		}
	});
	
	$('div.turner').qtip({
		content: { url: 'stuartturnerbio.php', method: 'get', prerender: true },
		show: 'mouseover', 
		hide: { when: 'mouseout', delay: 500, fixed: true },
		position: {
			corner: {
				target: 'topMiddle',
				tooltip: 'bottomMiddle'
			}
		},
		style: {
			width: 500,
			height: 400,
			border: {
				width: 7,
				radius: 5,
				color: '#7D7D7D'
			},
			tip: 'bottomMiddle'
		}
	});
	
	$('li.express').qtip({
		content: { url: 'tipexpress.php', method: 'get', prerender: true },
		show: 'mouseover', 
		hide: { when: 'mouseout', delay: 500, fixed: true },
		position: {
			target: $('#statement li:first'),
			corner: {
				target: 'topLeft'
			}
		},
		style: {
			width: 810,
			height: 180,
			border: {
				width: 7,
				radius: 5,
				color: '#bc312d'
			}
		}
	});
	
	$('li.professional').qtip({
		content: { url: 'tipprofessional.php', method: 'get', prerender: true },
		show: 'mouseover', 
		hide: { when: 'mouseout', delay: 500, fixed: true },
		position: {
			target: $('#statement li:first'),
			corner: {
				target: 'topLeft'
			}
		},
		style: {
			width: 810,
			height: 180,
			border: {
				width: 7,
				radius: 5,
				color: '#705393'
			}
		}
	});
	
	$('li.elite').qtip({
		content: { url: 'tipelite.php', method: 'get', prerender: true },
		show: 'mouseover', 
		hide: { when: 'mouseout', delay: 500, fixed: true },
		position: {
			target: $('#statement li:first'),
			corner: {
				target: 'topLeft'
			}
		},
		style: {
			width: 810,
			height: 180,
			border: {
				width: 7,
				radius: 5,
				color: '#96b752'
			}
		}
	});
	
	$('li.bespoke').qtip({
		content: { url: 'tipbespoke.php', method: 'get', prerender: true },
		show: 'mouseover', 
		hide: { when: 'mouseout', delay: 500, fixed: true },
		position: {
			target: $('#statement li:first'),
			corner: {
				target: 'topLeft'
			}
		},
		style: {
			width: 810,
			height: 180,
			border: {
				width: 7,
				radius: 5,
				color: '#dd7e2c'
			}
		}
	});
	
	$("#nouexc").change(function () {
		var yo = $('#nouexc').val();
		if( yo >= 1 )
		{
			$('#products ul li.prodExc').addClass('selected');
		}
		if( yo <= 0 )
		{
			$('#products ul li.prodExc').removeClass('selected');
		}
		calcPrice();
		});
	
	$("#noufis").change(function () {
		var yo = $('#noufis').val();
		if( yo >= 1 )
		{
			$('#products ul li.prodFis').addClass('selected');
		}
		if( yo <= 0 )
		{
			$('#products ul li.prodFis').removeClass('selected');
		}
		calcPrice();
	});
	
	$("#nouvoi").change(function () {
		var yo = $('#nouvoi').val();
		if( yo >= 1 )
		{
			$('#products ul li.prodVoi').addClass('selected');
		}
		if( yo <= 0 )
		{
			$('#products ul li.prodVoi').removeClass('selected');
		}
		calcPrice();
	});
	
	$("#nousec").change(function () {
		var yo = $('#nousec').val();
		if( yo >= 1 )
		{
			$('#products ul li.prodSec').addClass('selected');
		}
		if( yo <= 0 )
		{
			$('#products ul li.prodSec').removeClass('selected');
		}
		calcPrice();
	});
	
	/* Buy Now product grid*/
	$('#products ul li').click(function(){
		$(this).toggleClass('selected');
		$('#packages ul li.selected').removeClass('selected');
		$('#packages ul li.bespack').addClass('selected');
		

		if ( $(this).hasClass('prodExc') ){
			if ( $(this).hasClass('selected') ){
				$('#nouexc').val("1");
			}
			else {
				$('#nouexc').val("0");
			}
		}
		if ( $(this).hasClass('prodFis') ){
			if ( $(this).hasClass('selected') ){
				$('#noufis').val("1");
			}
			else {
				$('#noufis').val("0");
			}
		}
		if ( $(this).hasClass('prodVoi') ){
			if ( $(this).hasClass('selected') ){
				$('#nouvoi').val("1");
			}
			else {
				$('#nouvoi').val("0");
			}
		}
		if ( $(this).hasClass('prodSec') ){
			if ( $(this).hasClass('selected') ){
				$('#nousec').val("1");
			}
			else {
				$('#nousec').val("0");
			}
		}

		calcPrice();
	});
	
	/* Buy Now product grid*/
	$('#packages ul li').click(function(){
		
		if ( $(this).children('p').hasClass('packExp') ){
			$('#packages ul li').removeClass('selected');
			$(this).addClass('selected');
			
			$('#products ul li').removeClass('selected');
			$('#products ul li.prodWeb').addClass('selected');
			$('#nouexc').val("0");
			$('#noufis').val("0");
			$('#nouvio').val("0");
			$('#nousec').val("0");
		}
		if ( $(this).children('p').hasClass("packPro") ){
			$('#packages ul li').removeClass('selected');
			$(this).addClass('selected');
			
			$('#products ul li').removeClass('selected');
			$('#products ul li.prodExc').addClass('selected');
			$('#products ul li.prodFis').addClass('selected');
			$('#products ul li.prodWeb').addClass('selected');
			$('#nouexc').val("1");
			$('#noufis').val("1");
			$('#nouvio').val("0");
			$('#nousec').val("0");
		}
		if ( $(this).children('p').hasClass("packEli") ){
			$('#packages ul li').removeClass('selected');
			$(this).addClass('selected');
			
			$('#products ul li').removeClass('selected');
			$('#products ul li.prodExc').addClass('selected');
			$('#products ul li.prodFis').addClass('selected');
			$('#products ul li.prodVoi').addClass('selected');
			$('#products ul li.prodWeb').addClass('selected');
			$('#nouexc').val("1");
			$('#noufis').val("1");
			$('#nouvoi').val("1");
			$('#nousec').val("0");
		}
		if ( $(this).children('p').hasClass("packBes") ){
			$('#packages ul li').removeClass('selected');
			$('#products ul li').removeClass('selected');
			$(this).addClass('selected');
			$('#nouexc').val("0");
			$('#noufis').val("0");
			$('#nouvio').val("0");
			$('#nousec').val("0");
		}

		calcPrice();
	});
	
	$('#orderone').click(function(){
		$(this).addClass('hidden');
		$('#ordertwo').removeClass('hidden');
	
			var tempUsers = 0;
			var tempTotal = 0;
			var tempQuote = 0;
			var tempString = "";
			var tempPOA = 0;
			$('#summary').empty();
	
			$('#products ul li.selected').each(function(){
		
				var tempPrice = parseFloat($(this).children('input').val());
				var tempProduct = $(this).text();
				tempProduct = tempProduct.split("POA");
				
				 
				if ( $(this).hasClass('prodExc') ){
					tempUsers = parseFloat($('#nouexc').val());
					tempTotal = tempPrice * tempUsers;
					tempQuote += tempTotal;
					
					$('#summary').append("<tr><td>" + tempUsers + " Exchange account(s)</td><td class=\"cost\">" + tempTotal.toFixed(2) + "</td></tr>");
					$('#summary').append("<tr class=\"total\"><td class=\"cost\">TOTAL(monthly excl VAT):</td><td class=\"cost\">" + tempQuote.toFixed(2) + "</td></tr>");
				}
				if ( $(this).hasClass('prodFis') ){
					$('tr.total').remove();
					tempUsers = parseFloat($('#noufis').val());
					tempTotal = tempPrice * tempUsers;
					tempQuote += tempTotal;
					
					$('#summary').append("<tr><td>" + tempUsers + " file server user(s)</td><td class=\"cost\">" + tempTotal.toFixed(2) + "</td></tr>");
					$('#summary').append("<tr class=\"total\"><td class=\"cost\">TOTAL(monthly excl VAT):</td><td class=\"cost\">&#163;" + tempQuote.toFixed(2) + "</td></tr>");
				}
				if ( $(this).hasClass('prodWeb') ){
					$('tr.total').remove();
					tempQuote += tempPrice;
					$('#summary').append("<tr><td>Website hosting</td><td class=\"cost\">" + tempPrice.toFixed(2) + "</td></tr>");
					$('#summary').append("<tr class=\"total\"><td class=\"cost\">TOTAL(monthly excl VAT):</td><td class=\"cost\">" + tempQuote.toFixed(2) + "</td></tr>");
					
					tempTotal = tempPrice;
				}
				if ( $(this).hasClass('prodVoi') ){
					$('tr.total').remove();
					tempUsers = parseFloat($('#nouvoi').val());
					tempTotal = tempPrice * tempUsers;
					tempTotal = Math.round(tempTotal*Math.pow(10,2))/Math.pow(10,2);
					tempQuote += tempTotal;
					
					$('#summary').append("<tr><td>" + tempUsers + " VoIP account(s)</td><td class=\"cost\">" + tempTotal.toFixed(2) + "</td></tr>");
					$('#summary').append("<tr class=\"total\"><td class=\"cost\">TOTAL(monthly excl VAT):</td><td class=\"cost\"> &#163;" + tempQuote.toFixed(2) + "</td></tr>");
				}
				if ( $(this).hasClass('prodSec') ){
					$('tr.total').remove();
					tempUsers = parseFloat($('#nousec').val());
					tempTotal = tempPrice * tempUsers;
					tempQuote += tempTotal;
					
					$('#summary').append("<tr><td>" + tempUsers + " security account(s)</td><td class=\"cost\">" + tempTotal.toFixed(2) + "</td></tr>");
					$('#summary').append("<tr class=\"total\"><td class=\"cost\">TOTAL(monthly excl VAT):</td><td class=\"cost\">" + tempQuote.toFixed(2) + "</td></tr>");
				}
				if ( $(this).hasClass('poa') ){
					if(tempPOA === 0) {
						$('#summary').append("<tr class=\"stuff\"><td colspan=\"2\">We will contact you to discuss: </td></tr>");
						tempPOA = 1;
					}
					$('#summary').append("<tr><td class=\"productindent\" colspan=\"2\">"+ tempProduct[0] + "</td></tr>");
				}
				tempTotal = 0;
			});
			tempInterested = $('#summary').html();
			$('#interestedin').val(tempInterested);
			$('#recaptcha_response_field').focus();
	});

	$('img.closebutton').click(function(){
		$('#orderone').removeClass('hidden');
		$('#ordertwo').addClass('hidden');
	});
	
	$('#submitorderbutton').click(function(){
		$('#orderone').removeClass('hidden');
		$('#ordertwo').addClass('hidden');
		
	});

	//- - - - - - - - - - - -
	function calcPrice() {
	
		var price = 0;
		var hasPOA = 0;
		
		$('#products ul li.selected').each(function(){
			var index = $(this).text();
			
			if ( $(this).hasClass('monthly') ){
					var tempPrice = 0;
					var tempUsers = 0;
				if ( $(this).hasClass('prodExc') ){
					tempPrice = parseFloat($(this).children('input').val());
					tempUsers = parseFloat($('#nouexc').val());
					tempPrice = tempPrice * tempUsers;
				}
				if ( $(this).hasClass('prodFis') ){
					tempPrice = parseFloat($(this).children('input').val());
					tempUsers = parseFloat($('#noufis').val());
					tempPrice = tempPrice * tempUsers;
				}
				if ( $(this).hasClass('prodWeb') ){
					tempPrice = parseFloat($(this).children('input').val());
				}
				if ( $(this).hasClass('prodVoi') ){
					tempPrice = parseFloat($(this).children('input').val());
					tempPrice = Math.round(tempPrice*Math.pow(10,2))/Math.pow(10,2);
					tempUsers = parseFloat($('#nouvoi').val());
					tempPrice = tempPrice * tempUsers;
				}
				if ( $(this).hasClass('prodSec') ){
					tempPrice = parseFloat($(this).children('input').val());
					tempUsers = parseFloat($('#nousec').val());
					tempPrice = tempPrice * tempUsers;
				}
				// Add the prices up
				price += tempPrice;
			}
			if ( $(this).hasClass('poa') ){
				index = index.split("POA");
				hasPOA = 1;
			}
		});
		
		price = Math.round(price*Math.pow(10,2))/Math.pow(10,2);
		$('#monthlyprice').val(price);
	}



});