// JavaScript Document
$(document).ready(function(){ 
	initpage();
});	
	
function initpage()
{ 
	for (i=0;i<3;i++) {
		$("#HideMe"+i).hide();
	}
	$(".makechoice").click(function(){
		for (i=0;i<3;i++) {						
			$(".makechoice:checked").val() == i ? $("#HideMe"+i).show() : $("#HideMe"+i).hide() && $("#HideMe"+i).children("input").attr('value','');
		}
	});
	
	// SET VARIABLS FROM HIDDEN FIELDS
	var actionVal 	= $("#Action").val(); 				// "buy" | "sell" :defines which process the user is within
	var buyOrSell = $("#function").val(); 				// "buy" | "sell" :defines which process the user is within [check with Mike the different]
	var clientNoVal = $("#Client_No").val();			// [numeric value] :defines the client account number
	var dType = $("#deal_type").val();					// 
	var etfCheck 	= $("#ETFclient").val();			// 
	var etfInv = $("#ETFinv").val();					// 
	var functionVal = $("#function").val();				// 
	var inv_noVal = $("#inv_no").val();					// 
	var invTypeVal 	= $("#inv_type").val();				// 1 = unit trust | 2 = equity | 3 = sharemark : defines the investment type
	var limitDuration = $("#lim_dur").val();			// [numeric value] :defines the value entered for the duration of a limit sell or purchase
	var limitPrice = $("#lim_price").val();				// [numeric value] :defines the value entered for the price of a limit sell or purchase
	var livePrice = $("#live_price").val();				// [numeric value] :defines the current live price
	var marketStatus = $("#open_close").val();			// 0 = market closed | 1 = market open : defines the state of the market
	var optionVal 	= $("#option").val();				// "buynow" | "limbuy" | "sellnow" | "selllimit" :defines what sort of purchase/sell it is
	var portNo = $("#PortNo").val();					// [numeric value] :defines the portfolio number
	var priceImprove = $("#price_improved").val();		// '' | [numeric value] :defines the price improvement from stage 3 to stage 4 of buy and sell process
	var ptmVal = $("#ptm").val();						// [numeric value] :defines the value of PTM for orders
	var stageVal 	= $("#stage").val();				// 2 | 3 | 4  :defines the value of the next stage
	var stoplossDuration = $("#stoploss_dur").val();	// [numeric value] :defines the value entered for the duration of a stop-loss limit sale
	var stoplossPrice = $("#stoploss").val();			// [numeric value] :defines the value entered for the price of a stop-loss limit sell
	var trackDuration = $("#track_dur").val();			// [numeric value] :defines the value entered for the duration of the tracking stop-loss limit sale
	var trackPrice = $("#track").val();					// [numeric value] :degines the value eneered for the price of the tracking stop-loss limit sale
	
	
	// SWITCH FOR MARKET STATUS BANNER
	invTypeVal==5 ? '' : marketStatus==1 ? $("#marketStatusImg").attr("src","/web/images/marketOpen.gif") : $("#marketStatusImg").attr("src","/web/images/marketClosed.gif");
	
	marketStatus == 0 ? $("#contain1 tr").removeClass("limbuy") & $("td.openMarket").css("display","none") & $("td.closedMarket").css("display","table-row") & $("p.closedMarket").css("display","block") & $("#contain3").addClass("displayNone") & $("#contain4").addClass("displayNone") & $("#dealref").addClass("displayNone") & $("p.closeMarket").removeClass("unittrust").css("display","block") & $("p.openMarket").removeClass("equity").css("display","none") : '';
	marketStatus == 1 ? $("td.openMarket").css("display","table-row") & $("#closedMarket").css("display","none") : '';
	
	
	// EFT CHECKER
	etfInv==1 ?  etfCheck==2 ? $("#etfStatement").hide() : $(".cta").attr("href","javascript:void()") : $("#etfStatement").hide() & $("#buynow").attr("href",returnURL('buynow')) & $("#limbuy").attr("href",returnURL('limbuy'));
	
	// ALERT TO CHECK EFT WHEN LINKS CLICKED
	$(".cta").click(function(){
		etfCheck!=2 ? $("#etfStatement").addClass('alert') : '';
	});

	// CHECKBOX TOGGLE FUNCTIONALITY
	$("#ETFread").click(function(){
		$("#ETFread").is(":checked") 
		? $("#ETFclient").val(2) & $("#limbuy").attr("href",returnURL('limbuy')) & $("#buynow").attr("href",returnURL('buynow')) & $("#etfStatement").removeClass('alert')
		: $("#ETFclient").val(1) & $(".cta").attr("href","javascript:void()") & $("#etfStatement").addClass('alert')
	});
	
	// CHECKER FOR INPUT VALLUES
	$("#inet_qty").val()=='' ? '' : $("#inet_qty").parent("span").css("display","block");
	$("#inet_val").val()=='' ? '' : $("#inet_val").parent("span").css("display","block");
	
	// FUNCTION TO BUILD AND RETURN URLS
	function returnURL(option) {
		var urlString = "/cgi-bin/oicgi.exe/inet_raccount?function="+functionVal+"&action="+actionVal+"&option="+option+"&stage="+stageVal+"&client_no="+clientNoVal+"&PortNo="+portNo+"&inv_no="+inv_noVal+"&inv_type="+invTypeVal+"&etfCheck="+$("#ETFclient").val();
		return urlString;
	}
	
	// TURN ON NEEDED ELEMENTS
	
	// stage two - buy
	optionVal=="buynow" ? $(".buynow").css("display","block") : '';
	optionVal=="limbuy" ? $(".limbuy").css("display","block") & $(".stage2cta").text("place order") : '';
	optionVal=="selllimit" ? $(".selllimit").css("display","block") & $(".limitinline").css("display","inline") : '';
	optionVal=="sellnow" ? $(".sellnow").css("display","block") : '';
    (optionVal=="sellnow" && invTypeVal==1) ? $("#s4s_contain3").addClass("displayNone") &$("#s4s_contain4").addClass("displayNone") & $(".sellInlineUnittrust").css("display","inline") : $(".sellInlineEquity").css("display","inline");

	// stage three-buy
	invTypeVal==5 ? $(".unit_trust").css("display","block") : '';
	invTypeVal==1 ? $(".equity").css("display","block") : '';
	invTypeVal==4 ? $(".sharemark").css("display","block") & $("#stoploss").addClass("displayNone") & $("#tracking").addClass("displayNone") : '';
	(invTypeVal==5) && (optionVal="limbuy") ? $(".stage2cta").text("review order") & $(".stage3cta").text("place order") : '';
	optionVal=="selllimit" ? $(".stage2cta").text("review order") & $(".stage3cta").text("place order") & $(".limit_inline").css("display","inline") : '';
	invTypeVal==1 && optionVal=="limbuy" ? $("#contain3").toggleClass("equity").addClass("displayNone") & $("#contain4").toggleClass("equity").addClass("displayNone") : '';
	invTypeVal==1 && optionVal=="selllimit" ? $("#s4s_contain3").removeClass("equity").addClass("displayNone") & $("#s4s_contain4").removeClass("equity").addClass("displayNone") & $(".selllimit_inline").css("display","inline") : '';
	

	// VALUE OR QUANTITY
	dType=="quantity" ? $(".qtyDeal").css("display","block") : '';
	dType=="value" ? $(".valDeal").css("display","block") : '';
	
	// PTM
	ptmVal!="0.00" ? $(".ptm").css("display","block") : '';
	
	// STAGE 4 : Unit trust | Equity
	invTypeVal==5 ? $(".unittrust").css("display","block") & $("#inet_val").parent("span").css("display","block") & $("#buyVal").attr("checked","checked") & $("#quantityValueHeading").text("How much would you like to "+functionVal+"?") : '';
	invTypeVal==1 ? $(".equity").css("display","block") : '';
	invTypeVal==4 ? $("#inet_val").parent("span").css("display","block") & $("#buyVal").attr("checked","checked") & $("#quantityValueHeading").text("How much would you like to "+functionVal+"?") : '';

	// PRICE IMPROVEMENT FOR EQUITY
	priceImprove != '' ? $(".improvement").css("display","block") : ''; 
	
	// WEBSTYLE SWITCHS FOR PHONE NUMBERS
	var webbyStyle	= $("#WebStyle").val();
	webbyStyle == '' || webbyStyle == 0 || webbyStyle == 1 || webbyStyle == 2 || webbyStyle == 13 ? $(".webstylePhone").text("01296 41 42 43") : ''; 
	webbyStyle == 4 ? $(".webstylePhone").text("01296 41 46 42") : ''; 
	webbyStyle == 5 ? $(".webstylePhone").text("0845 6185143") : ''; 
	
	// INCLUDES COPY RELEVANT TO SELLING PROCESS
	buyOrSell=="sell" ? $(".sellInlineCopy").css("display","inline") : '';
		
	// PAUSE COUNTDOWN ON CLICK OF ACCEPT BUTTON
	$('#acceptButton').click(function(){
		$('#dealingCountdown').countdown("pause"); 								  
   	})
	
	// only need the countdown for the Acceptance phase and Equity
	if (stageVal == 4 && invTypeVal==1)
		$('#dealingCountdown').countdown({until: +15, format: 'S', compact: true, expiryUrl: 'javascript:history.back()'});
	
	// check if portno set
	if (portNo && typeof(ports) != "undefined")
		setfields(portNo);
		
	// Check to sell stage 4 to see if any limits set
	limitPrice!='' && limitDuration!='' ? $("#atlimit").css("display","table-row") : $("#atlimit").css("display","none");
	stoplossPrice!='' && stoplossDuration!='' ? $("#atstoploss").css("display","table-row") : $("#atstoploss").css("display","none");
	trackPrice!='' && trackDuration!='' ? $("#attracking").css("display","table-row") : $("#attracking").css("display","none");
	
	// LIMIT ALLERT ON STAGE 3 FOR WHEN LIMIT PRICE IS HIGHER THAN BID PRICE
	limitPrice!='' ? $(".limHighterAlert").css("display","block") : '';
	
	// STAGE 4 SELL TURN OF SELL NOW EQUITY ONLY DIVS
} 
	

function setfields(portno)
{
	for (port in ports)
	{
		vals = ports[port];
		if (vals.port_no == portno)
		{
			acashobj = document.getElementById('Avail_Cash');
			acashobj.innerHTML = vals.avail_cash;
			holdingobj = document.getElementById('Holding');
			if (vals.holding == "0")
				holdingobj.innerHTML = '';
			else
				holdingobj.innerHTML = 'You hold ' + vals.holding +  ' shares in ' + vals.invdets;
			thisportnoid = document.getElementById('This_PortNo');
			thisportnoid.value = port*1+1;
		}
	}
}

function updatefields()
{
	port = document.getElementById('This_PortNo').value;
	vals = ports[port-1];
	portobj = document.getElementById('PortNo');
	portobj.value = vals.port_no;
	acashobj = document.getElementById('Avail_Cash');
	acashobj.innerHTML = vals.avail_cash;
	holdingobj = document.getElementById('Holding');
	if (vals.holding == "0")
		holdingobj.innerHTML = '';
	else
		holdingobj.innerHTML = 'You hold ' + vals.holding +  ' shares in ' + vals.invdets;
}

function goBack()
{
	window.history.go(-1);
}

