var type  = "IE";
var tandc = 0;
var riskw = 0;
var ordrx = 0;

function BrowserSniffer() {
	if (navigator.userAgent.indexOf("Opera")!=-1 && document.getElementById) type="OP";		//Opera
	else if (document.all) type="IE";														//Internet Explorer e.g. IE4 upwards
	else if (document.layers) type="NN";													//Netscape Communicator 4
	else if (!document.all && document.getElementById) type="MO";							//Mozila e.g. Netscape 6 upwards
	else type = "IE";
}

BrowserSniffer();

function GetValue(id){
	if (type=="IE") return document.all[id].value;
	if (type=="NN") return document.layer['id'].value;
	if (type=="MO" || type=="OP") return document.getElementById(id).value;
}

function SetValue(id,sVal){
	if (type=="IE") document.all[id].value = sVal;
	if (type=="NN") document.layer['id'].value = sVal;
	if (type=="MO" || type=="OP") document.getElementById(id).value = sVal;
}

function denyPaste(event, msisdn2) {
	if (msisdn2.value = window.clipboardData.getData("Text")) {
		alert("Sorry, you have to re-type your " + msisdn2);
		msisdn2.value = "";
		return false;
		} else {
		alert("returned true");
		return true;
	}
}

function HideRow(id){
	if (type=="IE") {
		document.all[id].style.visibility = 'hidden';
		document.all[id].style.position = 'absolute';
	}
	if (type=="NN") {
		document.layer['id'].style.visibility = 'hidden';
		document.layer['id'].style.position = 'absolute';
	}
	if (type=="MO" || type=="OP") {
		document.getElementById(id).style.visibility = 'hidden';
		document.getElementById("oldaddressbox_").style.height = '0px';
	}
}

function ShowRow(id){
	if (type=="IE") {
		document.all[id].style.visibility = 'visible';
		document.all[id].style.position = 'relative';
	}
	if (type=="NN") {
		document.layer['id'].style.visibility = 'visible';
		document.layer['id'].style.position = 'relative';
	}
	if (type=="MO" || type=="OP") {
		document.getElementById(id).style.visibility = 'visible';
		document.getElementById("oldaddressbox_").style.height = '150px';
	}
}

function displayOldAddress(ref) {
	if (ref.value==1) {
		ShowRow("oldaddressbox");
		SetValue("monthsatcurrent","5");
		SetValue("yearsatcurrent","0");
	} else {
		HideRow("oldaddressbox");
		SetValue("monthsatcurrent","0");
		SetValue("yearsatcurrent","1");
	}
}

var win=null;
function NewWindow(targetpage,w,h,scroll,pos){
	LeftPosition = (screen.width)?(screen.width-w)/2:100;
	TopPosition=(screen.height)?(screen.height-h)/2:100;
	settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
	window.name = "alookup"
	win=window.open(targetpage,"win",settings);
}


function LookupAddress(aType) {
	var postcode = "";
	var countryid = "";
	if (aType=="current") {
		postcode = GetValue("postcode");
	}
	if (aType=="old") {
		postcode = GetValue("oldpostcode");
	}
	if (postcode=="") {
		alert("Please enter your "+aType+" postcode");
		return false;
	}
	NewWindow("../common/frontend/public/urulookup.aspx?type="+aType+"&postcode="+postcode,285,255,true,0);
}


function GetCookie(sName) {
	var aCookie = document.cookie.split("; ");
	for (var i=0; i < aCookie.length; i++) {
		var aCrumb = aCookie[i].split("=");
		if (sName == aCrumb[0]) 
		return unescape(aCrumb[1]);
	}
	return null;
}



//FSA REGULATED
function ShowHideComplianceRow(ref) {
	var id = "fsaEmail";			
	if (ref.value=="YES") {
		if (type=="IE") document.all[id].style.position = 'relative';
		if (type=="NN") document.layer['id'].style.position = 'relative';
		if (type=="MO" || type=="OP") document.getElementById(id).style.position = 'relative';
		ShowRow("fsaEmail");
	} else {
		if (type=="IE") document.all[id].style.position = 'absolute';
		if (type=="NN") document.layer['id'].style.position = 'absolute';
		if (type=="MO" || type=="OP") document.getElementById(id).style.position = 'absolute';
		HideRow("fsaEmail");
	}
}

function ShowHideEmployerRow(ref) {
	var id = "employername";
	if (ref.value=="Employed") {
		if (type=="IE") document.all[id].style.position = 'relative';
		if (type=="NN") document.layer['id'].style.position = 'relative';
		if (type=="MO" || type=="OP") document.getElementById(id).style.position = 'relative';
		ShowRow("employername");
	} else {
		if (type=="IE") document.all[id].style.position = 'absolute';
		if (type=="NN") document.layer['id'].style.position = 'absolute';
		if (type=="MO" || type=="OP") document.getElementById(id).style.position = 'absolute';
		HideRow("employername");
	}
}

function IsVisible(id) {
	var retval = false;
	if (type=="IE") {if (document.all[id].style.visibility=='visible') {retval = true;}}
	if (type=="NN") {if (document.layer['id'].style.visibility=='visible') {retval = true;}}
	if (type=="MO" || type=="OP") {if (document.getElementById(id).style.visibility=='visible') {retval = true;}}
	return retval;
}

function checkTandC(formfield) {
    formfield.checked = false;
    if(tandc==0) {
        alert("Please click on the link to read our Terms and Conditions first.");
    }
    else formfield.checked = true;
}

function checkRiskw(formfield) {
    formfield.checked = false;
    if(riskw==0) {
        alert("Please click on the link to read our Risk Warning first.");
    }
    else formfield.checked = true;
}

function checkOrdrx(formfield) {
    formfield.checked = false;
    if(ordrx==0) {
        alert("Please click on the link to read our Order Execution Policy.");
    }
    else formfield.checked = true;
}

function setTandC() {tandc = 1;}
function setRiskw() {riskw = 1;}
function setOrdrx() {ordrx = 1;}


function showHide(div_id) {
	if (document.getElementById(div_id).style.display == 'none'){
		document.getElementById(div_id).style.display = '';
		document.getElementById('pm').innerHTML = '[-]';
	} else {
		document.getElementById(div_id).style.display = 'none';
		document.getElementById('pm').innerHTML = '[+]';
	}
} 



function displayPreviousAddress(ref) {
	if (ref.value==1) {
		document.getElementById('oldaddressbox').style.display = 'block';
		SetValue("monthsatcurrent","5");
		SetValue("yearsatcurrent","0");
	} else {
		document.getElementById('oldaddressbox').style.display = 'none';
		SetValue("monthsatcurrent","0");
		SetValue("yearsatcurrent","1");
	}
}


function chanDiv(id){
    var layer = document.getElementById("submenu_" + id);
    var clickedObj = document.getElementById("btn_" + id); 
    // if the div is hidden, then show
    if(layer.style.display == 'none') {
        layer.style.display = 'block';
        clickedObj.className = 'open';
    } else { 
        layer.style.display = 'none';
        clickedObj.className = 'closed';
    }
}


function cookie_email_address() {
    var emailID = document.getElementById("emailaddress");
    if (emailID.value !=="")
    {
        set_Cookie('cemail', emailID.value);
        return true;
    }
    return true;
}

function set_Cookie(name, value) {
  var argv = set_Cookie.arguments;
  var argc = set_Cookie.arguments.length;
  var exdate = new Date();
  exdate.setDate(exdate.getDate() + 1);
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
  document.cookie = name + "=" + escape (value) + ((exdate == null) ? "" : ("; expires=" + exdate.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : "");
  return true;
}
function validate_step1() {
    var status = performCheck('frm', rules,'innerHtml');
    addErrors();
    if (status == true) {
    	cookie_email_address();
        document.frm.submit();
    } else {return false;}
}

function isHttp(){
	var protocol = window.location.protocol;
	var http = "";
		if(protocol == "https:"){
			http = "https:";
		}
		else{
			http  = "http:";
		}
	return http;
}
