//********* IE6 flicker *********//
try 
{
    //fix for ie6 background image flicker on hovers
    document.execCommand("BackgroundImageCache", false, true);
} 
catch(err) 
{}
//********* end *********//


//**  hide long ugly urls from status bar **//
function hidestatus()
{
    window.status='';
    return true;
}

if (document.layers)
document.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT)

document.onmouseover=hidestatus
document.onmouseout=hidestatus
//********* end *********//

//************ detail page military response toggles *************//
function SetVisibilityFromDropDownList(sourceControlID, controlIDToSetVisibility, fieldvalidatorID) {
    var sourceControl = document.getElementById(sourceControlID);
    if (sourceControl != null) {
        var controlToSetVisibility = document.getElementById(controlIDToSetVisibility);
        var fieldvalidator = document.getElementById(fieldvalidatorID);
        if (fieldvalidator != null || fieldvalidator != "undefined")
            ValidatorEnable(fieldvalidator, false);
        if (sourceControl.options[sourceControl.selectedIndex].value == '1') {
            controlToSetVisibility.style.display = "block";
            ValidatorEnable(fieldvalidator, true);
        }
        else {
            controlToSetVisibility.style.display = "none";
            fieldvalidator.isvalid = true;
        }
    }
}
//*******end**********//

//************************** Validating Education History ****************************//
/* these variants is to be set at codebehind in startupscript */
var validatorArray = null;
var itemCount = 0;
function ValidateRepeaterForHistoryDetails() 
{
    if (validatorArray != null) {
        var validatorsPerItem = validatorArray.length / itemCount;
        var currentItem = 0;
        for (var i = 0; i < validatorArray.length; i++) {
            var validatorsContainer = new Array();
            var invalidControlCount = 0;
            var hiddenControls = 0;

            i = currentItem * validatorsPerItem; //corrects initial index of each item
            //looping each item or section
            for (var indexCount = 0; indexCount < validatorsPerItem; indexCount++) {
                                                           
                var controlHolder = validatorArray[i].split('~');
                var validator = document.getElementById(controlHolder[0]);
                var controlToValidate = document.getElementById(controlHolder[1]);
                
                //null controls indicate they are hidden
                if (validator == null) {
                    hiddenControls = hiddenControls + 1;
                }
                
                //condition check for validators which are hidden
                if (validator != null) {                    
                    validatorsContainer[indexCount] = validator;
                }
                
                if (controlToValidate != null) {
                    if (controlToValidate.type == "text" && controlToValidate.value == "")
                        invalidControlCount = invalidControlCount + 1;
                    if (controlToValidate.type == "select-one" && controlToValidate.options[controlToValidate.selectedIndex].value == "0")
                        invalidControlCount = invalidControlCount + 1;
                }
                i = i + 1; //increment index of parent loop
            }
            if (hiddenControls > 0) {
                invalidControlCount = invalidControlCount + hiddenControls;
            }
            
            // if all validators in an item are invalid then disable validator.
            if (invalidControlCount == validatorsPerItem)
                SetValidatorControls(validatorsContainer, false);
            else SetValidatorControls(validatorsContainer, true);

            currentItem = currentItem + 1;
        }
        
    }    
}

function SetValidatorControls(controlArray, isEnabled) {
    for (var index = 0; index < controlArray.length; index++) {
        if (controlArray[index] != null) {
            ValidatorEnable(controlArray[index], isEnabled);
        }        
    } 
}

//application locator date change script
function SetDays(monthControlID, dayControlID) 
{
    var monthDropDownList = document.getElementById(monthControlID);
    if (monthDropDownList != null) {
        var dayDropDownList = document.getElementById(dayControlID);
        var maxDays = 31;

        var selectedValue = monthDropDownList.options[monthDropDownList.selectedIndex].value;

        if (selectedValue == '2') {
            maxDays = 29;
        }

        if (selectedValue == '4' || selectedValue == '6' ||
                   selectedValue == '9' || selectedValue == '11') {
            maxDays = 30;
        }

        var currentSelection = dayDropDownList.options[dayDropDownList.selectedIndex].value;
        if (currentSelection > maxDays)
            currentSelection = 1;

        dayDropDownList.length = 0;

        var opt;
        for (i = 0; i < maxDays; i++) {
            opt = document.createElement("option");
            opt.text = i + 1;
            opt.value = i + 1;
            dayDropDownList.options.add(opt)
        }

        dayDropDownList.selectedIndex = currentSelection - 1;
    }
} 



//********* availability toggles *********//
function SetVisibilityFromCheckBox(checkboxID, divID, fromDropDownID, toDropDownID)
{

    var checkBox = document.getElementById(checkboxID);
    var div = document.getElementById(divID);
    if (checkBox && div)
    {
        if(checkBox.checked)
        {
            div.style.display = "block"; 
        }
        else
        {
            div.style.display = "none";
            var fromDropDown = document.getElementById(fromDropDownID);
            var toDropDown = document.getElementById(toDropDownID);
            fromDropDown.selectedIndex = 0;
            toDropDown.selectedIndex = 0;  
        }           
    }    
}
//********* end *********//

function ComboKeyPress(event) 
{
    var e = getEvent(event);
    var sndr = getSource(e);   //window.event.srcElement;
    var key = getKey(e);    //window.event.keyCode;
    var charCode = String.fromCharCode(key).toLowerCase();
    var sysdate = new Date();
     
     if(sndr.lastPressTime)
     {
         if(sysdate.getTime()- sndr.lastPressTime > 2000) 
         {
              sndr.keyStrokes = "";
              sndr.rotation = 0;
         }
     }
     else
     {
        //initialize
        sndr.keyStrokes = "";
        sndr.rotation = 0;
     }
     
     if(key==32)//space
     {
        charCode = "";
        sndr.rotation++;
     }
     else if(key==8) //esc
     {
         charCode = "";
         sndr.keyStrokes = "";
         sndr.rotation = 0;
     }
     else
     {
         sndr.rotation = 0;
         fireChangeEvent(sndr);
     }
     
     sndr.lastPressTime = sysdate.getTime();
     
     var matchExp1, matchExp2;
     var match = false;
     var indexList = new Array();
     var indexListIndex = 0;
     var matchString1, matchString2;
     var separatorIndex, rightPartLength;
     var optionIndexToSelect;
     
     for(var i=0; i<sndr.options.length; i++) 
     {
     
        matchExp1 = sndr.keyStrokes + charCode;
        //matchExp2 = " - " + sndr.keyStrokes + charCode;
        matchString1 = sndr.options[i].text.toLowerCase();
        
        separatorIndex = matchString1.indexOf(" - ") + 3;
        rightPartLength = matchString1.length - separatorIndex;
        
        if(separatorIndex>=3 && rightPartLength>0)
        {
            matchString2=matchString1.substring(separatorIndex);
        }
        else
        {
            matchString2="";
        }
        
          
        matchString1 = matchString1.replace(/\s/g, ""); //strip spaces
        matchString2 = matchString2.replace(/\s/g, ""); //strip spaces
        

        
        if(matchString1.indexOf(matchExp1)==0 || matchString2.indexOf(matchExp1)==0)
        {
            indexList.push(i);
            match = true;
        }
        

     }
     
     if(match)
     {
     
        sndr.keyStrokes += charCode;
        sndr.rotation = sndr.rotation % indexList.length;
        indexListIndex = sndr.rotation;
        if(indexListIndex<0) indexListIndex = indexList.length - indexListIndex;
        
        optionIndexToSelect = indexList[indexListIndex];
        
        //if(optionIndexToSelect!=0)
        //{
        //    alert(indexListIndex);
        //}
        
        
        if(sndr.options[optionIndexToSelect].selected==false)
        {
            sndr.options[optionIndexToSelect].selected=true;
            //sndr.fireEvent('onchange')
            fireChangeEvent(sndr);
        }
        
     }
     
     // sink the keypress, ie don't pass it on to Windows or anything else
     e.returnValue = false;     // window.event.returnValue = false;
 }

 function getEvent(e) {
     return e || window.event;
 }

 function getSource(e) {
     return e.target || e.srcElement;
 }

 function getKey(e) {
     return e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
 }

 function fireChangeEvent(control) 
 {
     if (document.all) {
         control.fireEvent("onchange");
     }
     else {
         var changeEvent = window.document.createEvent("HtmlEvents");
         changeEvent.initEvent("change", false, true);
         control.dispatchEvent(changeEvent);
     }
 }

//********* end *********//



//********* postback functions *********//
function BeginPostback(sender, args) 
{
    
    currentAjaxCalls++;
   
    if (currentAjaxCalls>1) 
    {
        return;
    }
    
    
    //alert('beginpostback');
    
//    if (prm.get_isInAsyncPostBack()) 
//    {
//        if(args)
//        {
//            
 //           alert('cancelling this postback');
 //           args.set_cancel(true);
 //           return;
 //       }
//    }
    
    
    //HideProgressIndicator
    var pi = document.getElementById("progress_indicator_container");
    if (pi)
    {
        //pi.style.display = "none";  
    }    
    
    //ShowPostBackImage()
    var img = document.getElementById("postback_image");
    if (img)
    {
        img.src = img.src; //reload it to keep it spinning
        //img.style.visibility = "16px"; //show it
        img.style.visibility = "visible"; //show it
    }
    
    
    //DisableToolbarButtons()
    var buttonBar = document.getElementById("button_bar");
    if (buttonBar)
    {
        var buttons = buttonBar.getElementsByTagName("a");
        if (buttons)
        {
            for (var i = 0; i < buttons.length; i++) 
            {

                //save for restore
                if(buttons[i].className!="button_disabled")
                {
                    buttonStyles[i] = buttons[i].className;
                    buttonHrefs[i] = buttons[i].href;
                }
                
                //apply disabled style and behavior
                buttons[i].className="button_disabled"; 
                buttons[i].removeAttribute("href");
                
            }  
        }
    }
    
    //DisableToolbar2Buttons()
    buttonBar = document.getElementById("button_bar2");
    if (buttonBar)
    {
        var buttons = buttonBar.getElementsByTagName("a");
        if (buttons)
        {
            for (var i = 0; i < buttons.length; i++) 
            {
                //save for restore
                if(buttons[i].className!="button_disabled")
                {
                    button2Styles[i] = buttons[i].className;
                    button2Hrefs[i] = buttons[i].href;
                }

                //apply disabled style and behavior
                buttons[i].className="button_disabled"; 
                buttons[i].removeAttribute("href");
            }  
        }
    }
    

}

function EndPostback(sender, args) 
{

    currentAjaxCalls--;
    
    if (currentAjaxCalls>0) 
    {
        return;
    }

    //alert('ending postback');
    
    //ShowProgressIndicator
    var pi = document.getElementById("progress_indicator_container");
    if (pi)
    {
        //pi.style.display = "block";  
    }    
        
    //HidePostBackImage()
    var img = document.getElementById("postback_image");
    if (img)
    {
        //img.style.width = "0px"; //hide it
        img.style.visibility = "hidden"; //hide it
    }
    
    //restore toolbar
    var buttonBar = document.getElementById("button_bar");
    if (buttonBar)
    {

        var buttons = buttonBar.getElementsByTagName("a");
        if (buttons)
        {
            for (var i = 0; i < buttons.length; i++) 
            {
                
                //restore
                buttons[i].className = buttonStyles[i];
                buttons[i].href =  buttonHrefs[i];
            }
        }
    }
    var buttonBar = document.getElementById("button_bar2");
    if (buttonBar)
    {
        var buttons = buttonBar.getElementsByTagName("a");
        if (buttons)
        {
            for (var i = 0; i < buttons.length; i++) 
            {
                //restore
                buttons[i].className = button2Styles[i];
                buttons[i].href = button2Hrefs[i];
            }
        }
    }
    
}
//********* end *********//

//********* Functions for PageBase.SetMultiLineLengthRestriction() *********//
// Keep user from entering more than maxLength characters
function doKeypress(control)
{
    maxLength = control.attributes["maxLength"].value;
    value = control.value;
     if(maxLength && value.length > maxLength-1)
     {
          event.returnValue = false;
          maxLength = parseInt(maxLength);
     }
}
// Cancel default behavior
function doBeforePaste(control)
{
    maxLength = control.attributes["maxLength"].value;
     if(maxLength)
     {
          event.returnValue = false;
     }
}
// Cancel default behavior and create a new paste routine
function doPaste(control)
{
    maxLength = control.attributes["maxLength"].value;
    value = control.value;
     if(maxLength)
     {
          event.returnValue = false;
          maxLength = parseInt(maxLength);
          var oTR = control.document.selection.createRange();
          var iInsertLength = maxLength - value.length + oTR.text.length;
          var sData = window.clipboardData.getData("Text").substr(0,iInsertLength);
          oTR.text = sData;
     }
}
//********* end *********//


//********* disabling submit with Enter key *********//
function fnTrapEnterKey(event) 
{
    var e = getEvent(event);
    var sndr = getSource(e); 
    var key = getKey(e);

    if (key == 13) 
    {
        if (isApplicableControl(sndr)) 
        {
            e.returnValue = false;
            e.cancel = true;
        }
    }

}


function isApplicableControl(control) 
{
    //alert (control.type);
    if (control.type == "text"  || control.type == "select-one" 
            || control.type == "checkbox" || control.type == "radio" || typeof(control.type) == 'undefined')    // undefined is when the form has focus
        return true;
    else
        return false;
}

//********* end *********//


//********* graph animation *********//
function doWidthChangeMem(elem,startWidth,endWidth,steps,intervals,powr) 
{ 
    if (elem.widthChangeMemInt)
        window.clearInterval(elem.widthChangeMemInt);
    
    var actStep = 0;
    elem.widthChangeMemInt = window.setInterval(
    function() { 
    
    elem.currentWidth = easeInOut(startWidth,endWidth,steps,actStep,powr);
    
     //and make it visible if needed
    if( elem.style.visibility != "Visible")
    {
        elem.style.visibility = "Visible";
    }
    
    //stop right border from pushing out the containing bar one pixel
    if(elem.currentWidth==endWidth && elem.hideBorderWhenExpanded==true)
    {
        elem.style.borderRight = "0px"
    }
    
    //set Sthe width
    elem.style.width = elem.currentWidth + "px"; 

    actStep++;
    if (actStep > steps) window.clearInterval(elem.widthChangeMemInt);
    } ,intervals)
}

function easeInOut(minValue,maxValue,totalSteps,actualStep,powr) 
{ 
    var delta = maxValue - minValue; 
    var stepp = minValue+(Math.pow(((1 / totalSteps) * actualStep), powr) * delta); 
    return Math.ceil(stepp) 
} 


function expandBarChart(elemId, width, hideBorderWhenExpanded){
    var bar = document.getElementById(elemId);
    if(bar)
    {
        //if no memory is set, 
        if (!bar.currentWidth)
        {
            //set it first to the starting position of zero 
            bar.currentWidth = 0;
            //remember this
            bar.hideBorderWhenExpanded = hideBorderWhenExpanded;
        }
        
        doWidthChangeMem(bar,bar.currentWidth,width,25,25,0.2); 
    }
}
//********* end *********//

//********* grid hover *********//  
function HoverRow(anchor, enter)
{

    if(enter)
    {
        if (anchor.parentNode.parentNode.className != 'selected_row')
        {
            anchor.parentNode.parentNode.prevClassName = anchor.parentNode.parentNode.className;
            anchor.parentNode.parentNode.className = 'selected_row';
        }
    }
    else
    {
        anchor.parentNode.parentNode.className = anchor.parentNode.parentNode.prevClassName;
    }
}
//********* end *********//   


//********* grid hover *********//  
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}
//********* end *********//   


//********* validator enhancement  *********//  
var originalValidatorUpdateDisplay;

function hijackedValidatorUpdateDisplay(val)
{
   
    originalValidatorUpdateDisplay(val);

    var allValidatorsForThisControlValid = true;
    for (i = 0; i < Page_Validators.length; i++) 
    {
        if(Page_Validators[i].controltovalidate==val.controltovalidate)
        {
            if(!Page_Validators[i].isvalid)
            {
                allValidatorsForThisControlValid=false;
                break;
            }
        }
    }
    
    var control = document.getElementById(val.controltovalidate);
    
    if(control)
    {
        if(allValidatorsForThisControlValid)
        {
            if(control.className != "")
            {
                //don't change it right away because it is receiving an event
                //it can cause a flicker of the entire page
                setTimeout("changeControlClass('" + val.controltovalidate + "','')",100);
            }
        }
        else
        {
            if(control.className != "error_input")
            {   
               //don't change it right away because it is receiving an event
               //it can cause a flicker of the entire page
               setTimeout("changeControlClass('" + val.controltovalidate + "','error_input')",100);
            }    
        }    
    }
 
}

function changeControlClass(controlID, className)
{
    var control = document.getElementById(controlID);
    if(control)
    {
        control.className = className;
    }
}

window.onload = function()
{
    //hijack ValidatorUpdateDisplay() method
    if(typeof ValidatorUpdateDisplay != 'undefined') //if the page has validators
    {
        originalValidatorUpdateDisplay = ValidatorUpdateDisplay;
        ValidatorUpdateDisplay = hijackedValidatorUpdateDisplay;
    }
}


//********* end *********//   


//********* print functions  *********//  
function printMe()
{
    //alert('invoking print');
    this.focus();
    this.print(); 
}
//********* end *********//   



