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;
}


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 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 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;
}


/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}

function stripCharsInBag(s, bag) {
    /*LT says: Reporting to my master checkInternationalPhone() */
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
    /*LT says: Reporting to my master validatePhone() */
    var bracket=3;
    strPhone=trim(strPhone);
    if(strPhone.indexOf("+")>1) return false
    if(strPhone.indexOf("-")!=-1)bracket=bracket+1
    if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
    var brchr=strPhone.indexOf("(")
    if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
    if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
    s=stripCharsInBag(strPhone,validWorldPhoneChars);
    return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function validatePhone(phoneNum, fieldName){
    /*LT says: Pass me a phone number and the field ID containing the phone number and I'll check and tell you if the type is correct*/
    if (phoneNum != "") {
	    if (checkInternationalPhone(phoneNum)==false){
		    return "Please enter a valid " + fieldName;
            //return null;
	    }
	}
 }
 
 function get_radio_value(frm, obj) {
    /*LT says: Pass the form ID and radio button and I'll pass you the checked value */
    var radios = document[frm].elements[obj]; 
    for (var i=0; i <radios.length; i++) {
        if (radios[i].checked) {
            return radios[i].value;
        }
    }
 }
 
 function open_appForm(targetpage) {
    var w = "1000px";
    var h = "900px";
    var scroll = "1";
	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=1';
	window.name  = "appForm"
	win          = window.open(targetpage,"win",settings);
}

function validate_demo_form() {
    var status = performCheck('frm', rules,'innerHtml');
    addErrors();
    if (status == true) {
    	cookie_email_address();
        document.frm.submit();
        if (top.location!= self.location) {
            top.location = "../demo/demo_yes.shtml?";//self.location.href
        }
    } else {return false;}
}

function form_pre_validation()
{
	//Call the client check page to validate the new user, (client). 
	// This uses ajax to enhance the user experience
	$.ajax
	({
		async: false,
		type: "GET",
		url: "/common/frontend/public/checkclient.aspx",
		data: "username=" + $('#username').attr('value'),
		success: function(response)
		{
			//Client validation successful, validate the rest of the form
			validate_form();
		},
		error: function(response)
		{
			//There is a problem with this client, alert the user
			alert(response.responseText);
		}
	});
	
}


function get_queryString() {
    if(window.location != null && window.location.search.length > 1 )
    {
        var urlParameters = window.location.search.substring(1);
        var parameterPair = urlParameters.split('&');
        var pos = parameterPair[0].indexOf('=');
        var argName = parameterPair[0].substring(0, pos);
        var argVal = parameterPair[0].substring(pos + 1);
        return argVal;
    } 
    else {return "";}
}

function check_FSAstring() {
    var argVal = get_queryString();
    if(argVal == 'fsa' )
    {
        var obj = document.getElementById('fsa');
        obj.style.display = 'block';
    }                            
}


function isHttp(){
	var protocol = window.location.protocol;
	var http = "";
		if(protocol == "https:"){
			http = "https:";
		}
		else{
			http  = "http:";
		}
	return http;
}

function sitestat(x){
}