

//******************************************************
//
//	Used For Profiles List; set active profile via 
//	radio button (can't use radio button group in 
//	datagrid as need a group name) 
//
//******************************************************
function ShowElement(ctrl){
		document.getElementById(ctrl).style.display='block'
	}

	function HideElement(ctrl){
		document.getElementById(ctrl).style.display='none'
	}

function SelectChange(chkB)
{
      var ID = chkB.id; 
      var count = document.getElementById("dgProfiles").rows.length;
      var i;
      for (i = 2; i <= count; i++)
      {
            var id = "dgProfiles__ctl" + i + "_RadioButton1";
            var elem= document.getElementById(id);
            elem.checked = false ;
      }
      document.getElementById(ID).checked = true;
}

//Used to select or deselect multiple checkboxes in a given datagrid

function SelectAll(DataGridId)
{
      //get main header checkbox state as this determines whether all checkboxes
      //are being checked or not

      var id_MainCheckBox = DataGridId + "__ctl1_chkSelectAll";
      var elem_MainCheckBox = document.getElementById(id_MainCheckBox);
      var state = elem_MainCheckBox.checked;
      
      var count = document.getElementById(DataGridId).rows.length;
      var i;
      for (i = 2; i <= count; i++)
      {
            var id = DataGridId + "__ctl" + i + "_ChkId";
            var elem= document.getElementById(id);            

	    //There will always be 1 checbox missing - for the current active profile which 
	    //cannot be deleted

	    if(elem != null){
		    elem.checked = state;
	    }
      }

}


function doNothing(){
	return false;
}


//Used to display Modal Dialogs

function showModal(oldUrL)
{
	//var posX    = event.screenX 
	//var posY    = event.screenY + 20 

	//var screenW = screen.width                                	// screen size
	//var screenH = screen.height - 20                           	// take taskbar into account

	//if (posX + 232 > screenW) { posX = posX - 232 - 40 }       	// if mouse too far right
	//if (posY + 164 > screenH) { posY = posY - 164 - 80 }       	// if mouse too far down 
	//var wPosition   = 'dialogLeft:' +posX+ '; dialogTop:' +posY 
	//var newUrl = showModalDialog('getUrl.aspx', oldUrL,'dialogWidth:298px; dialogHeight: 207px ' + 'resizable: no; help: no; status: no; scroll: no; ' + wPosition) 
	
	var wPosition   = 'dialogLeft:' +100+ '; dialogTop:' +100 	
	var newUrl = showModalDialog('getUrl.aspx', oldUrL,'dialogWidth:298px; dialogHeight: 207px ' + 'resizable: no; help: no; status: no; scroll: no; ' + wPosition) 
	return newUrl;
}

function GetVersions(){

	lstV = eval(document.getElementById('lstVersion'))

	var index  = lstV.selectedIndex
	var SelValue = lstV.options[index].value
	if(SelValue == "1"){

		document.getElementById('lstVersionOf').disabled = false;
	}
	else{
		document.getElementById('lstVersionOf').disabled = true;
	}
	
}

function CheckMasterCategory(control){
	
	//loop through each selection and check relevant exec box
	for(i=0; i<9; ++i){
		var control = "chkCategories_" + i;			
		
		if(document.all[control].checked == true){
			//Corpoate Finance and Corporate Broking come under Corporate Advisory
			if(i == 2 || i == 1 || i == 0){
				document.getElementById('chkCategories_0').checked = true;
				
			}

			//if(i == 3 || i == 4 || i == 5 || i == 6 ){
				//document.getElementById('chkCategories_7').checked = true;
				//return;
			//}
		
		}
	}
	return false;	
}

function generateEmailAddress(){


	var Firstname = document.getElementById('txtForename').value;
	var Surname = document.getElementById('txtSurname').value;

	//remove any blank spaces from beggind and end of each part of name
	Firstname = Firstname.replace(new RegExp(/^\s+/),""); // START
	Firstname  = Firstname.replace(new RegExp(/\s+$/),""); // END

	Surname = Surname.replace(new RegExp(/^\s+/),""); // START
	Surname = Surname.replace(new RegExp(/\s+$/),""); // END

	
	var address = (Firstname.toLowerCase()) + '.' + Surname.toLowerCase() + '@wdbcapital.com';
	document.getElementById('txtEmail').value = address;
	
}


function generateUserInfo(){


	var Firstname = document.getElementById('txtForename').value;
	var Surname = document.getElementById('txtSurname').value;

	//remove any blank spaces from beggind and end of each part of name
	Firstname = Firstname.replace(new RegExp(/^\s+/),""); // START
	Firstname  = Firstname.replace(new RegExp(/\s+$/),""); // END

	Surname = Surname.replace(new RegExp(/^\s+/),""); // START
	Surname = Surname.replace(new RegExp(/\s+$/),""); // END

	
	var address = Firstname.toLowerCase() + '.' + Surname.toLowerCase() + '@wdbcapital.com';
	document.getElementById('txtEmail').value = address;
	document.getElementById('txtUsername').value = address;
	document.getElementById('txtDisplayname').value = Firstname + ' ' + Surname;
	
}


function InitSelectedField(ctrl){
	document.getElementById(ctrl).focus();
}

function checkLoginFields(){

	//if username contains data then the user has been remembered so set password field as default
	if(document.getElementById('txtUsername').value.length > 0){
		InitSelectedField('txtPwd')
	}else{
		InitSelectedField('txtUsername')
	}
}

function CheckIP(ipaddress) {
IPvalue = ipaddress;

errorString = "";
theName = "IPaddress";

var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
var ipArray = IPvalue.match(ipPattern); 

if (IPvalue == "0.0.0.0")
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
else if (IPvalue == "255.255.255.255")
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
if (ipArray == null)
errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
else {
for (i = 0; i < 4; i++) {
thisSegment = ipArray[i];
if (thisSegment > 255) {
errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
i = 4;
}
if ((i == 0) && (thisSegment > 255)) {
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
i = 4;
      }
   }
}
extensionLength = 3;
if (errorString == "")
	return true;
else
alert (errorString);
return false;
}

function updateField(val, ctrl){
	var targetCtrl = document.getElementById(ctrl)
	targetCtrl.value = val[0];
}

function getHTMLEditor(){
	try{

		//check window is not already open and just lost focus

		//window is open just set focus to it
		if(!w_HTMLEditor.closed){
			w_HTMLEditor.focus();
		}

		//window closed, continue as usual and open new window
		else{
			w_HTMLEditor = window.open('htmlEditor.html','w_HTMLEditor','toolbar=0,menubar=0,location=0,status=0,height=350,width=400') ;
			w_HTMLEditor.w_tttnew = parent;  /* allow window to refer to this window */
			w_HTMLEditor.currentSelection = getSelection();   /* int value  */
			w_HTMLEditor.tnt_code = 'NTRS';  /* char value */
		}
	}catch(e){
		w_HTMLEditor = window.open('htmlEditor.html','w_HTMLEditor','toolbar=0,menubar=0,location=0,status=0,height=350,width=400') ;
		w_HTMLEditor.w_tttnew = parent;  /* allow window to refer to this window */
		w_HTMLEditor.currentSelection = getSelection();   /* int value  */
		w_HTMLEditor.tnt_code = 'NTRS';  /* char value */

	}

}


//******************************************************
//
//	Used in orering items
//
//******************************************************





		function MoveItem(Direction, ctrl){
			//1: Up  
			//-1: Down			
			//index value of 0 = 1st

			//get control
			var TargetCtrl = document.getElementById(ctrl)
			var selectedIndex = TargetCtrl.options.selectedIndex
			var selectedTeamLocaleId = TargetCtrl.options[selectedIndex].value	
			var LastItemIndex = TargetCtrl.length-1;
			
			switch(selectedIndex){
				case 0:			
					//first Item - can't move item up
					if(Direction != -1){
						//check list does not contain only 1 item
						if(TargetCtrl.length != 1){
							//Move item down
							SwapValues(0, Direction, 'lbItemsToSort')
							
						}else{
							alert('This item cannot be moved down as there is only 1 item in the list')
						}
												
					}else{
						alert('This item can only be moved down the list')
					}	
					break;
				case LastItemIndex:
					//first Item - can't move item down
					if(Direction != 1){
						//check list does not contain only 1 item
						if(TargetCtrl.length != 1){
							//Move item down
							SwapValues(selectedIndex, Direction, 'lbItemsToSort')
							
						}else{
							alert('This item cannot be moved up as there is only 1 item in the list')
						}
												
					}else{
						alert('This item can only be moved up the list')
					}	
					break;
				default:			
					//Any item between first and last - can move up or down
					//check list does not contain only 1 item
					if(TargetCtrl.length != 1){
						//Move item 
						SwapValues(selectedIndex, Direction, 'lbItemsToSort')
						
					}else{
						alert('This item canno be moved down as there is only 1 item in the list')
					}
					break;
			}
			
		}
		function SwapValues(currentIndex, Direction, ctrl){
			var TargetCtrl = document.getElementById(ctrl);
			var selectedValue = TargetCtrl.options[TargetCtrl.options.selectedIndex].value
			var selectedText = TargetCtrl.options[TargetCtrl.options.selectedIndex].text
			var destinationValue = TargetCtrl.options[TargetCtrl.options.selectedIndex + Direction].value							
			var destinationText = TargetCtrl.options[TargetCtrl.options.selectedIndex + Direction].text							
			var TempValue = "";
			var TempText = "";

			//swap values
			TempValue = selectedValue;
			TempText = selectedText;

			TargetCtrl.options[currentIndex].value = destinationValue
			TargetCtrl.options[currentIndex].text = destinationText

			TargetCtrl.options[currentIndex + Direction].value = TempValue
			TargetCtrl.options[currentIndex + Direction].text = TempText

			//now select newly moved value
			TargetCtrl.options.selectedIndex = currentIndex + Direction;

			//reinitialise buttons
			CheckOptions('lbItemsToSort')

		}
		function CheckOptions(ctrl){
			var TargetCtrl = document.getElementById(ctrl)
			var LastItemIndex = TargetCtrl.length-1;
			
			switch(TargetCtrl.options.selectedIndex){
				case 0:			
					//first Item - can't move item up
					document.getElementById('btnUp').disabled=true
					document.getElementById('btnDown').disabled=false
					break;
				case LastItemIndex:
					//Last Item - can't move item down			
					document.getElementById('btnUp').disabled=false
					document.getElementById('btnDown').disabled=true
					break;
				default:			
					//Any item between first and last - can move up or down
					document.getElementById('btnUp').disabled=false
					document.getElementById('btnDown').disabled=false
					break;
			}
		}
		function getNewOrder(ctrl){
			var TargetCtrl = document.getElementById(ctrl)
			var newOrder = "";
			for(var i=0; i<TargetCtrl.length;i++){
				newOrder += TargetCtrl.options[i].value + ','
			}
			return newOrder;
		}

