function formatName(oldS) { 
	// Replaces oldS with fullS
	var fullS;
	
	
	fullS = oldS.substring(0,1).toUpperCase() + oldS.substring(1,oldS.length).toLowerCase();
	
	
	for  (i=fullS.length ; i>=0; i--){
	//take care of all characters following blanks
		if (fullS.charAt(i)==' '){
			fullS=fullS.substring(0,i) +" "+fullS.substring(i+1, i+2).toUpperCase()+fullS.substring(i+2, fullS.length);
		}
	//take care of all characters following hyphens	
		if (fullS.charAt(i)=='-'){
			fullS=fullS.substring(0,i) +"-"+fullS.substring(i+1, i+2).toUpperCase()+fullS.substring(i+2, fullS.length);
		}
	//take care of all characters following apostrophes	
		if (fullS.charAt(i)=='\''){
			fullS=fullS.substring(0,i) +"'"+fullS.substring(i+1, i+2).toUpperCase()+fullS.substring(i+2, fullS.length);
		}
	//take care of all characters following "Mc"	
		if (i!=fullS.length){
			if(fullS.substring(i, i+2)=="Mc"){
				fullS=fullS.substring(0,i) +"Mc"+fullS.substring(i+2, i+3).toUpperCase()+fullS.substring(i+3, fullS.length);
			}
		}
	//take care of all characters following "Mac" but not "Mace"
		if (i!=fullS.length){
			if(fullS.substring(i, i+3)=="Mac"){
				if(fullS.substring(i+3,i+4)!="e")
					fullS=fullS.substring(0,i) +"Mac"+fullS.substring(i+3, i+4).toUpperCase()+fullS.substring(i+4, fullS.length);
			}
		}		
	
	}

	return fullS;
}


function formatAddress(oldS) { 
	var fullS = oldS.substring(0,1).toUpperCase() + oldS.substring(1,oldS.length).toLowerCase();

	for  (i=fullS.length ; i>=0; i--){
		if (fullS.charAt(i)==' '){
			fullS=fullS.substring(0,i) +" "+fullS.substring(i+1, i+2).toUpperCase()+fullS.substring(i+2, fullS.length);
		}
		if (fullS.charAt(i)=='-'){
			fullS=fullS.substring(0,i) +"-"+fullS.substring(i+1, i+2).toUpperCase()+fullS.substring(i+2, fullS.length);
		}
		if (fullS.charAt(i)=='\''){
			fullS=fullS.substring(0,i) +"'"+fullS.substring(i+1, i+2).toUpperCase()+fullS.substring(i+2, fullS.length);
		}
		if (i!=fullS.length){
			if(fullS.substring(i, i+2)=="Mc"){
				fullS=fullS.substring(0,i) +"Mc"+fullS.substring(i+2, i+3).toUpperCase()+fullS.substring(i+3, fullS.length);
			}
		}
		if (i!=fullS.length){
			if(fullS.substring(i, i+3)=="Mac"){
				if(fullS.substring(i+3,i+4)!="e")
					fullS=fullS.substring(0,i) +"Mac"+fullS.substring(i+3, i+4).toUpperCase()+fullS.substring(i+4, fullS.length);
			}
		}		

	}

	return fullS;
}

// helper functions
function isLetter (c) {   
	return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) );
}


function isDigit (c) {   
	return ((c >= "0") && (c <= "9"));
}


function isLetterOrDigit (c) {   
	return (isLetter(c) || isDigit(c));
}


function formatPostcode(oldS) {
	var fullS="";
	for  (i=0; i<oldS.length; i++){
		if (isLetterOrDigit(oldS.charAt(i))) {
			fullS+=oldS.charAt(i).toUpperCase();
		}
	}
	return fullS;
}

function resetCalcForm (form) {
  for (var i=0; i < form.length; i++) {
	if (form.elements[i].type.toLowerCase().indexOf('text')>=0) {
	  if (form.elements[i].name.toLowerCase().indexOf('percent')>=0)
	    form.elements[i].value='';
	  else if (form.elements[i].name.toLowerCase().indexOf('currency')>=0)
	    form.elements[i].value='';
	  else if (form.elements[i].name.toLowerCase().indexOf('number')>=0)
	    form.elements[i].value='';
	  else if (form.elements[i].name.toLowerCase().indexOf('date')>=0)
		form.elements[i].value='';
	  }
	if (form.elements[i].type.toLowerCase().indexOf('select')>=0)
	  form.elements[i].selectedIndex=0;
	if (form.elements[i].type.toLowerCase().indexOf('checkbox')>=0)
	  form.elements[i].checked=false;
	}
  }

function glossary(item) {
  var gloss = window.open("/docs/glossary/index.jhtml?item=" + item, "milestonesGlossary", "width=400,height=500,status=yes");
  if (gloss.focus)
    gloss.focus();
}

function manageInvestments(portfolio) {
  var value = document.forms[0].action.options[document.forms[0].action.selectedIndex].value;
  if (value == 'delete') {
    newWindow = window.open(
      'delete/?portfolio=' + portfolio, 
      '', 
      'location=0,width=440,height=300');
	newWindow.focus();
  }
  else if (value == 'rename') {
    newWindow = window.open(
      'rename/?portfolio=' + portfolio, 
      '', 
      'location=0,width=440,height=340');
	newWindow.focus();
  }
  else if (value == 'create') {
  }
  else if (value.indexOf('view') == 0) {
    var index = value.indexOf('_')+1;
    var portfolio = value.substr(index, value.length-index);
    window.location = './?portfolio=' + portfolio;
  }
}

function openWindow(url, width, height) {
  newWindow = window.open(
    url, 
    '', 
    'location=0,width=' + width + ',height=' + height);
  newWindow.focus();
}

function openWindow( url, name, width, height) {
  newWindow = window.open(
    url, 
    name, 
    'location=0,width=' + width + ',height=' + height );
  newWindow.focus();
}

function openWindowNoFocus( url, name, width, height) {
  newWindow = window.open(
    url, 
    name, 
    'location=0,width=' + width + ',height=' + height );
}

function openScrollingWindow(url, name, width, height) {
  newWindow = window.open(
    url, 
    name, 
    'location=0,width=' + width + ',height=' + height + ',scrollbars,resizable');
  newWindow.focus();
}

function openRegistrationWindow(url, name, width, height) {
  additionalOptions='';
  if (document.layers) // quick test for NS
	  additionalOptions=',scrollbars';
  
  newWindow = window.open(
    url, 
    name, 
    'location=0,width=' + width + ',height=' + height + additionalOptions);
  newWindow.focus();
}

function openScrollingResizableWindow(url, name, width, height) {
  newWindow = window.open(
    url, 
    name, 
    'location=0,width=' + width + ',height=' + height + ',scrollbars,resizable');
  newWindow.focus();
}

function closeAndReplaceOpener(url) {
  if(window.opener && !window.opener.closed){
    window.opener.location = url;
  }

  window.close();

  if(window.opener && !window.opener.closed){
    window.opener.focus();
  }
}

function closeAndFocusOnOpener() {
  window.close();

  if(window.opener && !window.opener.closed){
    window.opener.focus();
  }
}

// Pages call this function to validate text input
// field contains the text to check
// fieldName is the wording to be used to represent the field in an error message
// validation type can be:
// a - letters,numbers,$,-,£, , only.  Not Null
// 1 - Numbers only.  Not Null
function validate(field, fieldName, validationType) {

  returnVal = checkNotNull(field,fieldName);

  if (returnVal != "") {
	return returnVal;
  } else {
    if (validationType == 'a') {
        return checkValidLetter(field,fieldName);
    } else {
	return checkValidNumber(field,fieldName);
    }
  }
}

function checkNotNull(field, fieldName) {

  if (field.length < 1) {
	return "* "+fieldName+": Please enter a value for this field<br>";
  } else {
	return "";
  }
}

function checkValidLetter(field, fieldName) {

  for(var i = 0; i < field.length; i++) {
	var c = field.charAt(i);

	if (!(((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')) || ((c >= '0') && (c <= '9')) || (c == ' ') || (c == '&') || (c == '$') || (c == '\'') || (c == '£') || (c == '.') || (c == ',') || (c == '-'))) {
		return "* "+fieldName+": Invalid value entered.  Valid values are letters, numbers, commas,-,',.,$,£, and &<br>";
	}	
  } 
  return "";   
}

function checkValidNumber(field, fieldName) {
  for(var i = 0; i < field.length; i++) {
	var c = field.charAt(i);

	if (!(((c >= '0') && (c <= '9')) || (c == '.'))) {
	   return "* "+fieldName+" fields must all be numbers<br>";
	}	
  } 

  return "";
}

function addressPopUp( url, id, width, height,url1 ) {
  if ( id !=null) {
    url = url + '&sentId=' + id + '&url=' + url1 ;
  }
  else {
	  url = url + '&url=' + url1 ;
  }
  newWindow = window.open( url, '', 'location=0,width=' + width + ',height=' + height + ',top=60,left=100,scrollbars,resizable');
                                   
  newWindow.focus();
}

function resetAddressFrag(person) {
   person.forenames.value='';
   person.lastName.value='';
   person.jobTitle.value='';
   person.dob.value='';
   person.postcode.value='';
   person.address1.value='';
   person.address2.value='';
   person.address3.value='';
   person.address4.value='';
   person.submit();
 }

function resetEmailFrag(person) {
  person.forenames.value='';
  person.lastName.value='';
  person.jobTitle.value='';
  person.dob.value='';
  person.email1.value='';
  person.email2.value='';
  person.email3.value='';
  person.url1.value='';
}

function resetAddressNoteFrag(person) {
  person.forenames.value='';
  person.lastName.value='';
  person.jobTitle.value='';
  person.dob.value='';
  person.addressNote.value='';
}

function textCounter(person) {
	//alert('inside the textCounter') ;
	person.submitted=true ;
	//alert(person);
        if (person){
          if (person.addressNote){
            if (person.addressNote.value){	
	      if (person.addressNote.value.length > 30) { 
		alert("You can enter only upto 30 characters") ;
		return false;
	      }
	    }
	  }
	}
	
	return true;
	
}


function checkXButton(xClose,url) {
	if (xClose) { 
		//alert('submit is true') ;
	}
	else {
		//alert('submit is false ') ;
		window.close();
		window.opener.focus();
	}
	return true;
}

function resetPhoneFrag(person) {
  person.forenames.value='';
  person.lastName.value='';
  person.jobTitle.value='';
  person.dob.value='';
  person.homePhone.value='';
  person.businessPhone.value='';
  person.mobilePhone.value='';
  person.pagerNumber.value='';
  person.faxNumber.value='';
}


function pushPage(url) {
  window.opener.location.href=url;
}
 
function openDemoWindow(url,height,width) {
  newWindow = window.open(
    url, 
    'demo', 
    'location=0,width=' + width + ',height=' + height);
  newWindow.focus();
}
function popBlank(target,w,h) {
var handle = window.open('/eifa/popups/blank.html',target,'width='+w+',height='+h+',resizable=yes,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes');
handle.focus();
}

//For accessible popup windows
function popUp(strURL,strType,strHeight,strWidth) {
var strOptions="";
if (strType=="console") strOptions="resizable,height="+strHeight+",width="+strWidth;
if (strType=="fixed") strOptions="status,height="+strHeight+",width="+strWidth;
if (strType=="elastic") strOptions="toolbar,menubar,scrollbars,resizable,location,height="+strHeight+",width="+strWidth;
window.open(strURL, 'newWin', strOptions);
}

//For accessible popup window No.2
function popUp2(strURL,strType,strHeight,strWidth) {
var strOptions="";
if (strType=="console") strOptions="resizable,height="+strHeight+",width="+strWidth;
if (strType=="fixed") strOptions="status,height="+strHeight+",width="+strWidth;
if (strType=="elastic") strOptions="toolbar,menubar,scrollbars,resizable,location,height="+strHeight+",width="+strWidth;
window.open(strURL, 'newWin2', strOptions);
}

//For accessible popup window No.3
function popUp3(strURL,strType,strHeight,strWidth) {
var strOptions="";
if (strType=="console") strOptions="resizable,height="+strHeight+",width="+strWidth;
if (strType=="fixed") strOptions="status,height="+strHeight+",width="+strWidth;
if (strType=="elastic") strOptions="toolbar,menubar,scrollbars,resizable,location,height="+strHeight+",width="+strWidth;
window.open(strURL, 'newWin3', strOptions);
}

//For accessible popup window No.4
function popUp4(strURL,strType,strHeight,strWidth) {
var strOptions="";
if (strType=="console") strOptions="resizable,height="+strHeight+",width="+strWidth;
if (strType=="fixed") strOptions="status,height="+strHeight+",width="+strWidth;
if (strType=="elastic") strOptions="toolbar,menubar,scrollbars,resizable,location,height="+strHeight+",width="+strWidth;
window.open(strURL, 'newWin4', strOptions);
}
//For accessible popup window No.5
function popUp5(strURL,strType,strHeight,strWidth) {
var strOptions="";
if (strType=="console") strOptions="resizable,height="+strHeight+",width="+strWidth;
if (strType=="fixed") strOptions="status,height="+strHeight+",width="+strWidth;
if (strType=="elastic") strOptions="toolbar,menubar,scrollbars,resizable,location,height="+strHeight+",width="+strWidth;
window.open(strURL, 'newWin5', strOptions);
}

//For accessible popup window No.6
function popUp6(strURL,strType,strHeight,strWidth) {
var strOptions="";
if (strType=="console") strOptions="resizable,height="+strHeight+",width="+strWidth;
if (strType=="fixed") strOptions="status,height="+strHeight+",width="+strWidth;
if (strType=="elastic") strOptions="toolbar,menubar,scrollbars,resizable,location,height="+strHeight+",width="+strWidth;
window.open(strURL, 'newWin6', strOptions);
}

//For accessible popup window No.7
function popUp7(strURL,strType,strHeight,strWidth) {
var strOptions="";
if (strType=="console") strOptions="resizable,height="+strHeight+",width="+strWidth;
if (strType=="fixed") strOptions="status,height="+strHeight+",width="+strWidth;
if (strType=="elastic") strOptions="toolbar,menubar,scrollbars,resizable,location,height="+strHeight+",width="+strWidth;
window.open(strURL, 'newWin7', strOptions);
}

//For accessible popup window No.8
function popUp8(strURL,strType,strHeight,strWidth) {
var strOptions="";
if (strType=="console") strOptions="resizable,height="+strHeight+",width="+strWidth;
if (strType=="fixed") strOptions="status,height="+strHeight+",width="+strWidth;
if (strType=="elastic") strOptions="toolbar,menubar,scrollbars,resizable,location,height="+strHeight+",width="+strWidth;
window.open(strURL, 'newWin8', strOptions);
}

/**
 * Function to show/hide html elements Membersite.
 */
function showhide(id){
	if (document.getElementById){
		obj = document.getElementById(id);
		var imgId = id+"Img";
		imgObj = document.getElementById(imgId);

		if (obj.style.display == 'none' || obj.style.display == ''){		
			// Display element
			obj.style.display = 'block';
			imgObj.src = imgObj.src.replace("arrow_right","arrow_down");
		} else {	
			// Hide element
			obj.style.display = 'none';
			imgObj.src = imgObj.src.replace("arrow_down","arrow_right");
		}

	}
}

/**
 * Function to show/hide html elements Customer Site.
 */
function showhideCS(id){
	if (document.getElementById){
		obj = document.getElementById(id);
		var imgId = id+"Img";
		imgObj = document.getElementById(imgId);

		if (obj.style.display == 'none' || obj.style.display == ''){		
			// Display element
			obj.style.display = 'block';
			imgObj.src = imgObj.src.replace("header_arrow_1","header_arrow_2");
		} else {	
			// Hide element
			obj.style.display = 'none';
			imgObj.src = imgObj.src.replace("header_arrow_2","header_arrow_1");
		}

	}
}

/**
 * Function to show/hide html elements.
 */
function load(id){

	if (document.getElementById){
		obj = document.getElementById(id);		
		
			// Hide element
		obj.style.display = 'block';			
	
	}
	showhide(id);
}
