/* open close div javascript function Used by DB - IP  REMOVE ON GO LIVE */
	function toggleSectionVisibility(sectionName){

    	// toggle the visibility of the html content

        toggleDisplay(document.getElementById(sectionName));

    }

	

    /* toggles the display property of a particular object in the page */

    function toggleDisplay(objectId) {

		//alert(objectId.style.display);

        if ( objectId.style.display == 'block' ){

            objectId.style.display = 'none';

        } else {

            objectId.style.display = 'block';

        }

    }

	

	function toggleArrow(whichOne) {

        if(document.getElementById('notesDropDownArrow'  + whichOne).className == 'noteOpen') {

			document.getElementById('notesDropDownArrow' + whichOne).className = 'noteClosed';

		} else {

			document.getElementById('notesDropDownArrow' + whichOne).className = 'noteOpen';

		}

    }
/* END open close div javascript function Used by DB - IP  REMOVE ON GO LIVE */

function toggleLayer(whichLayer) {
	if (document.getElementById) {
		if (!document.getElementById(whichLayer)) {return false;}
		// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
		style2.display = style2.display == 'block'? "none":"block";
	}
	else if (document.all) {
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
		style2.display = style2.display == 'block'? "none":"block";
	}
	else if (document.layers) {
		// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
		style2.display = style2.display == 'block'? "none":"block";
	}
}

// check whether the textField is with empty value
function isEmpty(fieldName) {
	if ((fieldName == null) || (fieldName.length == 0)) {
		return true;
	}
	else {	return false;	}
}

/* emergency e-mail validator which allows apostrophes */
function isValidEmail(email, required) {
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    } else if (email.indexOf("..") >= 0) { // two periods in a row is not valid
	return false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
	return false;
    }
    return true;
}

function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_&'";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}
/* emergency e-mail validator which allows apostrophes */

//function to open a pop up window
function popupURL(url,winWidth,winHeight,resizable) {
	if(winWidth == null){winWidth = '530';}
	if(winHeight == null){winHeight = '600';}
	if(resizable == null){resizable = 'yes';}
	myWindow=window.open(url,'popup','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=' + resizable + ',copyhistory=yes,width=' + winWidth + ',height=' + winHeight);
	myWindow.focus();
}

function submitForm(formName, actionPage){
	// Adjust the action page
	if(actionPage){
		document[formName].action = actionPage;
	}
	document[formName].submit();
}

function toggleLayerOn(whichLayer) {
	if (document.getElementById) {
		// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
		style2.display = "block";
	}
	else if (document.all) {
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
		style2.display = "block";
	}
	else if (document.layers) {
		// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
		style2.display = "block";
	}
}
function toggleLayerOff(whichLayer) {
	if (document.getElementById) {
		// this is the way the standards work
		if (!document.getElementById(whichLayer)) {return false;}
		var style2 = document.getElementById(whichLayer).style;
		style2.display = "none";
	}
	else if (document.all) {
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
		style2.display = "none";
	}
	else if (document.layers) {
		// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
		style2.display = "none";
	}
}

function checkEmail(str) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(str)){ return true; }
	return false;
}

function populateSelectBox(selectBoxObj,optionsValueArray,optionsIdArray){
	selectBoxObj.length = optionsValueArray.length;
	for(var i=0; i < optionsValueArray.length; i++){
		selectBoxObj.options[i] = new Option(optionsValueArray[i],optionsIdArray[i]);
	}
}

function setSelectBoxDefault(selectBoxObj,defaultArray){
	//alert(selectBoxObj + ', ' + defaultArray);
	//Loop over the array of selected items
	for(var i = 0; i < defaultArray.length; i++){
		var thisSelection = defaultArray[i];
		//alert('thisSelection = ' + thisSelection);
		//Loop over all items in the list and see if any value matches thisSelection
		for(var j = 0; j < selectBoxObj.length; j++){
			if(selectBoxObj.options[j].value == thisSelection){
				selectBoxObj.options[j].selected = true;
				break;
			}
		}
	}
}

function trim(myString)
{
	myString=myString.replace(/^\s*(.*)/, "$1");
	myString=myString.replace(/(.*?)\s*$/, "$1");
	return myString;
}

/*
Function Name:	doCount
Description:	Requires 3 inputs. Counts the number of words in a form textarea.
*/
function doCount(formField, formFieldWordCounter, wordLimit)	{
	var wordArray = formField.value.split(/\s+/g);	// split on spaces to put words into an array, thus getting number of words
	formFieldWordCounter.value = wordLimit - wordArray.length;
	var warningName = formField.name + "WordWarning";

	if(wordArray.length > wordLimit)	{			// word limit exceeded
		document.getElementById(warningName).style.visibility = "visible";	// display warning message
	}
	else{
		document.getElementById(warningName).style.visibility = "hidden";	// hide warning message
	}
}

function doCountUsingInnerHtml(formField, divID, formFieldWordCounter, wordLimit)	{
	var wordArray = formField.value.split(/\s+/g);	// split on spaces to put words into an array, thus getting number of words
	document.getElementById(divID).innerHTML = wordLimit - wordArray.length;
	formFieldWordCounter.value = wordLimit - wordArray.length;
	var warningName = formField.name + "WordWarning";

	if(wordArray.length > wordLimit)	{			// word limit exceeded
		document.getElementById(warningName).style.visibility = "visible";	// display warning message
	}
	else{
		document.getElementById(warningName).style.visibility = "hidden";	// hide warning message
	}
}

Array.find = function(ary, element){
    for(var i=0; i<ary.length; i++){
        if(ary[i] == element){
            return i;
        }
    }
    return -1;
}

/* **************************************************
 BEGIN : FUNCTIONS TO CONTROL THE TOP HORIZONTAL NAV
*************************************************** */
var iCurrentlyOpenSubmenu = 0;
var timeoutToSubMenuHide;
		
function openCloseSubmenu(iMenuIndex)
{
	if(iCurrentlyOpenSubmenu != 0)
		toggleLayerOff('submenu_'+iCurrentlyOpenSubmenu);
	if(iMenuIndex != iCurrentlyOpenSubmenu)
	{
		toggleLayerOn('submenu_'+iMenuIndex);
		iCurrentlyOpenSubmenu = iMenuIndex;
		timeoutToSubMenuHide = setTimeout("toggleLayerOff('submenu_"+iMenuIndex+"'); iCurrentlyOpenSubmenu = 0; unhideSelectElements();", 8000);
		hideSelectElements();
	}
	else 
	{
		iCurrentlyOpenSubmenu = 0;
		unhideSelectElements();
	}
}

function mouseoutSubmenu(iMenuIndex)
{
		timeoutToSubMenuHide = setTimeout("toggleLayerOff('submenu_"+iMenuIndex+"'); iCurrentlyOpenSubmenu = 0; unhideSelectElements();", 200);		
}
function mouseoverSubmenu()
{
	clearTimeout(timeoutToSubMenuHide);
	hideSelectElements();
}


document.onclick=function(e)
{
	// if the user clicks anywhere on the page and a sub menu is open - close the sub menu.
	// if the area of the page the user clicks on is a link - allow the link action to continue.
	if(iCurrentlyOpenSubmenu != 0)
	{
		if(document.all)
			sThisClass = event.srcElement.className;
		else if(document.layers)
			sThisClass = '';
		else if(document.getElementById)
			sThisClass = e.target.className; 
	
		if(sThisClass!='dropdown' && sThisClass!='dropdowntext')
		{
			clearTimeout(timeoutToSubMenuHide);
			toggleLayerOff('submenu_'+iCurrentlyOpenSubmenu); 
			iCurrentlyOpenSubmenu = 0; 
			unhideSelectElements();
			return true;
		}
	}
}

function unhideSelectElements()
{
	if(typeof(bIsIE6orEarlier) != "undefined" && bIsIE6orEarlier == 1) 
	{
		var arSelects = document.getElementById('efcContentLayoutTop').getElementsByTagName('select');
		for (var i=0; i<arSelects.length; i++)
			removeClassName(arSelects[i],'hideSelect');
	}
}
function hideSelectElements()
{
	if(typeof(bIsIE6orEarlier) != "undefined" && bIsIE6orEarlier == 1) 
	{
		var arSelects = document.getElementById('efcContentLayoutTop').getElementsByTagName('select');
		for (var i=0; i<arSelects.length; i++)
			addClassName(arSelects[i],'hideSelect');
	}
}
	
function addClassName(objElm, strClassName)
{
  var strCurrentClass = objElm.className;
  if(!new RegExp(strClassName, "i").test(strCurrentClass))
  	objElm.className = strCurrentClass + ((strCurrentClass.length > 0)? " " : "") + strClassName;
 
}
function removeClassName(objElm, strClassName)
{
  var objClassToRemove = new RegExp((strClassName + "\s?"), "i");
  objElm.className = objElm.className.replace(objClassToRemove, "").replace(/^\s?|\s?$/g, "");
}
/* *************************************************
 END : FUNCTIONS TO CONTROL THE TOP HORIZONTAL NAV
************************************************** */

/* ********************************************************************************************************
BEGIN : FUNCTIONS TO FIND NUMBER OF CITITES FOR A COUNTRYID PASSED
At the moment the countCitiesForCountry function is used by v2/js/coldfusionJs/jobApplyValidationJS.cfm
* Note: It uses the iCountry_id and iCity_id arrays which is generated 
by v2/includes/locationAndSectorJavascript.cfm in v2/views/wrappers/contentwrapper.cfm
Added by Gitesh on 17-April-2008 to fix the problem related to ticket4230
********************************************************************************************************* */
function countCitiesForCountry(countryId) {
	//Default values if countryId is not passed
	var countryID = 0;
	var numberOfCities = 0;
	for(i=0; i <= iCountry_id.length; i++)	{
		// checks if the passed country id is matched with the one of the value in iCountry_id array
		if(iCountry_id[i] == countryId)	{
			if(iCity_id[i].length > 1) {
				numberOfCities = iCity_id[i].length;
				break;
			}	
			else	{
				numberOfCities = 0;
				break;
			}	
		}
	}	
	return numberOfCities;
}
/* ********************************************************************************************************
 END : FUNCTIONS TO FIND NUMBER OF CITITES FOR A COUNTRYID PASSED
********************************************************************************************************* */


/* sg stuff - hacks that go here will eventually be cleaned and moved to static.efinancialcareers */
if (typeof jQuery != "undefined") {
	$(function () {

		// auto focus username input on login/register pages
		var el = $("#efcHolder #efcContent #efcSiteLayoutT42.loginOrRegister #efcContentLayoutMiddleCol2 #userLoginMod input#username1,#efcHolder #efcContent #efcSiteLayoutT33.jobApplyLoginOrRegister #efcContentLayoutMiddleCol2 #jobViewLoginMod input#username").focus()
	
	})
}