// JavaScript Document

function msieversion()
{
    var ua = window.navigator.userAgent
    var msie = ua.indexOf ( "MSIE " )

	if ( msie > 0 )      // If Internet Explorer, return version number
         return parseInt (ua.substring (msie+5, ua.indexOf (".", msie )));
	else                 // If another browser, return 0
         return 0
}


function errorHandler()
{
	return true;
}

function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 

function showMenu()
{
	if (ieVersion > 5 || ieVersion == 0)  {
		showDiv();
		opacity('dropDownContainer', 0, 100, 800);		
		tempVar = "menuOn";
		checkLight();
	}
}

function hideMenu()
{	
	if (ieVersion > 5 || ieVersion == 0) {
		opacity('dropDownContainer', 100, 0, 800);
		setTimeout('hideDiv()',800);	
		tempVar = "menuOff";
		checkLight();
	}
}
function showDiv() {
	document.getElementById("dropDownContainer").style.visibility = "visible"
	document.getElementById("dropDownContainer").style.display = "block"
}

function hideDiv() {
	document.getElementById("dropDownContainer").style.visibility = "hidden";
	document.getElementById("dropDownContainer").style.display = "none";
}

function checkLight()
{
	if (tempVar == "menuOff")
	{
		document.getElementByClassName("our_services").style.background = '#DBDCB4';			
	}
	else
	{
		document.getElementByClassName("our_services").style.background = '#D8BFCF';
	}
}

/* no of items in each dropdown specified in initial js files */
function menuMovementEvent(e)
{
	
	var aa 		= "" + findPosX(document.getElementByClassName("our_services")) + ""; // to start position of menu (X-coord)
	var bb 		= "150"; 	// menu width
	var cc 		= "" + findPosY(document.getElementByClassName("our_services")) + ""; // start of top of menu (top Y-coord)
	var xStart 	= "" + findPosX(document.getElementByClassName("our_services")) + ""; 	//specifies X-coord start menu region
	var yStart 	= "" + findPosY(document.getElementByClassName("our_services")) + ""; // specifies Y-coord start of menu region 
	var yEnd 	= "286" 	// specifies bottom Y-coord
		
	var part2 = xStart.length;
	if (IE5MAC)
	{
		var part3 = part2 + 2;
	}
	else
	{
		var part3 = part2 - 2;		
	}
	var part = xStart.substring(0,part3);		
	var xStart = eval(aa) + eval(part); // specifies X-coord start of menu region
	var xEnd = eval(bb) + eval(xStart); // specifies X-coord end of menu region 
	
	if (tempVar == "menuOn")
	{
		if (IE4 || IE5)
		{
			mX = event.clientX;
			mY = event.clientY;
			mXOff = document.body.scrollLeft;
			mYOff = document.body.scrollTop;
			mXX = mX + mXOff;
			mYY = mY + mYOff;
			if (document.documentElement && document.documentElement.scrollTop)
				theTop = document.documentElement.scrollTop;
			else if (document.body)
				theTop = document.body.scrollTop;			

			xStartX = parseInt(xStart) - parseInt(document.body.scrollLeft);
			xEndX = parseInt(xEnd) + parseInt(document.body.scrollLeft);
			yStartY = parseInt(yStart) - parseInt(theTop);
			yEndY = parseInt(yEnd) + parseInt(theTop);
		}
		else
		{
			mX = e.pageX;
			mY = e.pageY;
			mXOff = window.pageXOffset;
			mYOff = window.pageYOffset;
			mXX = mX + mXOff;
			mYY = mY + mYOff;
			xStartX = parseInt(xStart) - parseInt(window.pageXOffset);
			xEndX = parseInt(xEnd) + parseInt(window.pageXOffset);
			yStartY = parseInt(yStart) - parseInt(window.pageYOffset);
			yEndY = parseInt(yEnd) + parseInt(window.pageYOffset);
		}
		
		if ((mYY <= yStartY) || (mYY >= yEndY))
		{
			hideMenu();
	 	}
		else if ((mXX <= xStartX) || (mXX >= xEndX))
		{
			hideMenu();
		}
	}
	
}


function correctResize()
{
	if (innerWidth != origWidth || innerHeight != origHeight)
	location.reload();
}


function findPosX(obj)
{
	var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj)
{
   	var curtop = 0;
   	if(obj.offsetParent)
     	while(1)
	 	{
       	curtop += obj.offsetTop;
       	if(!obj.offsetParent)
         	break;
       	obj = obj.offsetParent;
     	}
  		else if(obj.y)
      		curtop += obj.y;
  	return curtop;
}

function populateHeight() {
	if (screen.height>768) {
		if (document.body.clientHeight) {
			if (navigator.userAgent.indexOf("Firefox")!=-1) {
					document.getElementById("centerContent").style.minHeight = screen.height - 376 + "px";	
					
			} else if (navigator.userAgent.indexOf("Opera")!=-1	) {
					document.getElementById("centerContent").style.minHeight = screen.height - 390 + "px";		
					
			} else {
				document.getElementById("centerContent").style.height = screen.height - 400 + "px";				
			}
		}
	}
	/*position the dropdown
	document.getElementById("dropDownContainer").style.position = "absolute";
	document.getElementById("dropDownContainer").style.zIndex = "100";
	document.getElementById("dropDownContainer").style.top 	= ""+ (findPosY(document.getElementByClassName("our_services")) + 24 ) + "px";
	
	if (navigator.userAgent.indexOf("Firefox")!=-1)
		document.getElementById("dropDownContainer").style.left = "" + (findPosX(document.getElementByClassName("our_services")) ) + "px";
	else
		document.getElementById("dropDownContainer").style.left = "" + (findPosX(document.getElementByClassName("our_services")) + 1 ) + "px";
	
	*/
	
}


	
	function emailcheck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		
		if ((str==null)||(str=="")){
			return false
		}
	
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		 
		 if (str.indexOf(dot)+1==lstr){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}

	function initText() {
		if (document.forms[0]) {
			var e = document.forms[0];
			for(var i=0; i < e.elements.length; i++) {
				if (e.elements[i].type == "text" || e.elements[i].type == "password" || e.elements[i].type == "textarea") {
						e.elements[i].onfocus = function() {
							if (this.value.indexOf('[') != -1) {
								this.value = "";
							}this.style.backgroundColor = '#d1c6bf';
						};
						e.elements[i].onblur = function() {  
							if (this.value == "") {
								this.value = this.defaultValue;
							}this.style.backgroundColor = '#fff';
					 };
				} 
				if (e.elements[i].type == "submit") {
							e.elements[i].onmouseover = function() {
								this.style.cursor = 'pointer';								
							};
							
				}
			}
		}
	}
	
	function openWin() {
		return(window.confirm('Premier Pension Services is delighted to be able to offer our clients access to the FundsDirect on-line fund supermarket. This service provides policyholders and their IFA with the facility to not only view fund values on-line but also to buy and sell investments from a wide range of funds at advantageous dealing costs.\nPlease note that you will now be directed to the Funds Direct website where your policy account can be viewed.\n\nPremier Pension Services provides links to third-party sites for your convenience and information. We do not control content within those sites or their privacy practices, which may differ from ours. Please refer to our privacy statement for further details.'));
		
	}
	
	function openWin1() {
		return(window.confirm('You are about to be transferred to the Aries website, our partner in providing you with SIPP illustrations and projections. \nJardine Lloyd Thompson does not control the site you are to be redirected to which may contain different terms of use and privacy conditions to the site you are leaving.\n\nPlease read and ensure you are aware of the relevant terms and conditions for both sites. Please click ok to continue or click cancel to return to the Premier Pension Services website.'));
		
	}
	
function addMenuEvents () {		
	document.getElementByClassName("our_services").onmouseover 	= function(){showMenu();};	
	document.getElementByClassName("our_services").onfocus 	 	= function(){showMenu();};
}

function addLeftButtonEvent() {
	if (document.getElementById("viewFunds") != null)
	{			
		document.getElementById("viewFunds").onclick = function(){ if(openWin()) {document.getElementById("viewFunds").href = 'https://www.fundsdirect.co.uk/bks/Login.aspx?bksid=pps';  } else {return false;};}
	}
	
	if (document.getElementById("link1") != null)
	{			
		document.getElementById("link1").onclick = function(){ if(openWin1()) {document.getElementById("link1").href = 'http://www.ariespensions.co.uk/sipp';  } else {return false;};}
	}
}

function addLinkTargets() {
	for (var i=1; i<5;i++) {
		if (document.getElementById("link" + [i]) != null)	{
			document.getElementById("link" + [i]).target = "_blank";
		}
		else{
			break;
		}
	}	
}

function validate () {
		var e = document.forms[0];
		var check;
		var count;
		var current;
		var error = false;
		count = 0;
		var order = false;
		
		for(var i=0; i < e.elements.length; i++) {
			
			if (e.elements[i].type == "text" || e.elements[i].type == "textarea" || e.elements[i].type == "password") 
			{
					if (e.elements[i].name == "Position" || e.elements[i].name == "Organisation" || e.elements[i].name == "Telephone" || e.elements[i].name == "Fax" || e.elements[i].name == "FSAFirm") {
						continue;
					}
					else if (e.elements[i].value == "" || e.elements[i].value == e.elements[i].defaultValue) 
					{						
						alert("Please enter a value for \n\"" + eval("L" + e.elements[i].name + ".innerText") + "\"");								
						error = true;
						e.elements[i].focus();
						break;
					}
					else if (e.elements[i].name == "UserEmail") {
						if (!(emailcheck(e.elements[i].value))) {
							alert("Please enter a valid email address");								
							error = true;
							e.elements[i].focus();
							break; 
						}
					}			
					
									
			} /*else if (e.elements[i].type == "radio" || e.elements[i].type == "checkbox"){					
					
					check = false;
					count = 0;
					current = i;
					for (var j=0; j < eval("e." + e.elements[i].name + ".length"); j++) {
						if (eval("e." + e.elements[i].name + "[" + j + "].checked")) {
							check = true;
							count++;
						}
					}		
					
					if (!check) {
						alert("Please select a value");
						error = true;
						e.elements[i].focus();
						break;
					}
					//i = i + 1;		
					
			} */else if (e.elements[i].type == "select-one") {
					if (e.elements[i].value == "" || e.elements[i].value == e.elements[i].defaultValue) {
						alert("Please enter a value for \n\"" + eval("L" + e.elements[i].name + ".innerText") + "\"");
						error = true;
						e.elements[i].focus();
						break;
					}
					
			}
					
		}
		
				
		if (!error) {
			return true;
		} else {
			return false;
		}
	}
	
if (IE4 || IE5 || N6)
	{
		document.onmousemove = menuMovementEvent;
	}	
	/* determining whether msie or netscape being used and what versions */
	var IE4 = (document.all && !document.getElementById) ? 1 : 0;
	var NS4 = (document.layers) ? 1 : 0;
	var IE5 = (document.all && document.getElementById) ? 1 : 0;
	var N6 = (document.getElementById && !document.all) ? 1 : 0;
	var ie = (IE4 || IE5) ? 1 : 0;
	var ieVersion = msieversion();
	var n = (NS4 || N6) ? 1 : 0;
	var IE5MAC = false;
	var NS4MAC = false;
	var MAC = (navigator.userAgent.indexOf("Mac")) // determines platform
	
	if (MAC != -1)
	{
		if (IE5)
		{
			IE5MAC = true; //determines if ie5 on a mac
		}
		else
		{
			if (NS4 || N6)
			{
				NS4MAC = true; // determines if net 4 or 6 on mac
			}
		}
	}
	else
	{
	}
	
function init () {
	initText();	
	//populateHeight();
	//addMenuEvents();
	addLeftButtonEvent();
	addLinkTargets();	
	/* this is a work a round for the netscape resize bug */
	if (document.layers)
	{
		origWidth = innerWidth;
		origHeight = innerHeight;
	}
	
	if (document.layers) onResize = correctResize;
	
	
	
	/* error handling */
	window.onerror = errorHandler;
		
}

window.onload = init;



