/* All Ammo Javascript utility functions are defined in this file */


function doRedirection(){
  window.location.href="ArrangeDemo.aspx";
}

function SelectAllCheckboxes(CheckBoxControl) 
{
    if (CheckBoxControl.checked == true) 
    {
        var i;
        for (i=0; i < document.forms[0].elements.length; i++) 
        {
            if ((document.forms[0].elements[i].type == 'checkbox')) 
            {
                document.forms[0].elements[i].checked = true;
            }
        }
    } 
    else 
    {
        var i;
        for (i=0; i < document.forms[0].elements.length; i++) 
        {
            if ((document.forms[0].elements[i].type == 'checkbox')) 
            {
            document.forms[0].elements[i].checked = false;
            }
        }
       
    }
}

function SelectAllChildCheckboxes(CheckBoxControl,Parent, onFinished) 
{
    var inputsCol = document.getElementById(Parent).getElementsByTagName('INPUT');
    
    if (CheckBoxControl.checked == true) 
    {
        var i;
        
        for (i=0; i < inputsCol.length; i++) 
        {
            if ((inputsCol[i].type.toUpperCase() == 'CHECKBOX')) 
            {
                inputsCol[i].checked = true;
            }
        }
    } 
    else 
    {
        var i;
        
        for (i=0; i < inputsCol.length; i++) 
        {
            if ((inputsCol[i].type.toUpperCase() == 'CHECKBOX')) 
            {
                inputsCol[i].checked = false;
            }
        }
    }
    
    if(typeof onFinished == 'function'){
        onFinished();
    }
    
}

function handleInputClicks(ParentID, clickFunction)
{
    $('#'+ParentID).delegate("input", "click", clickFunction);
}

function handleShowSelected()
{
    if( $('#myammo-scroll-content input:checked').size() > 0)
    {
        $('#show-selected').fadeIn('slow');
    }
    else
    {
        $('#show-selected').delay(600).fadeOut('slow');
    }
}

function handleShowDeleteSelected()
{
    if( $('#saved-search-scroll-content input:checked.checkbox').size() > 0)
    {
        $('.header-col2','#content-box-controls').fadeIn('slow');
    }
    else
    {
        $('.header-col2','#content-box-controls').delay(600).fadeOut('slow');
    }
}


function SelectAll(CheckBoxControl) 
{
    if (CheckBoxControl.checked == true) 
    {
        var i;
        for (i=0; i < document.forms[0].elements.length; i++) 
        {
            if ((document.forms[0].elements[i].type == 'checkbox')) 
            {
                document.forms[0].elements[i].checked = true;
            }
        }
    } 
    else 
    {
        var i;
        for (i=0; i < document.forms[0].elements.length; i++) 
        {
            if ((document.forms[0].elements[i].type == 'checkbox')) 
            {
            document.forms[0].elements[i].checked = false;
            }
        }
       
    }
    setVisibility('divMessageBox', 'none','hidden');
}

function setVisibility(id,display, visibility) {
    document.getElementById(id).style.visibility = visibility;
    document.getElementById(id).style.display = display;
}

/* Function Name : setVisibility */
    function setVisibilityExt(id,display, visibility) {
        /*
        //document.getElementById(id).style.visibility = visibility;
        //document.getElementById(id).style.display = display;
        $('[id$=id]').css("visibility",visibility);
        //style.visibility = visibility;
        $('[id$=id]').css("display",display);
        //style.display = display;
        
        if(visibility=='hidden'){
           //document.getElementById('ammoSavedSearches_pnlConfirm').style.visibility='visible';
           $('[id$=ammoSavedSearches_pnlConfirm]').css('visibility','visible');
           $('.modal-background').css('visibility','visible');
//           var divLinks = document.form1.getElementsByTagName("div");
//           for(var i=0;i <divLinks.length; i++)
//           {
//               if (divLinks[i].className == 'modal-background'){
//                 divLinks[i].style.visibility = 'visible';
//                }
//           }
           
        
        }
        else{
            //document.getElementById('ammoSavedSearches_pnlConfirm').style.visibility='hidden';
            $('[id$=ammoSavedSearches_pnlConfirm]').css('visibility','hidden');
            $('.modal-background').css('visibility','hidden');
//            var divLinks = document.form1.getElementsByTagName("div");
//            for(var i=0;i <divLinks.length; i++)
//            {
//                if (divLinks[i].className == 'modal-background')
//                {
//                divLinks[i].style.visibility = 'hidden';
//                }
//            }
            

        }
        */
    }

function ValidateItems(ctrl)
{
        for (i=0; i < document.forms[0].elements.length; i++) 
        {
           var el=document.forms[0].elements[i];
            /* Check for 3 conditions
            Element is if type checkbox + element id is not checkAll checkbox id + element is checked 
            */
            if ((el.type == 'checkbox') && (el.id != ctrl) && (el.checked == true)) 
            {
                return true;
            }
        }
        setVisibilityExt("divMessageBox",'','visible');
 
        return false;
}

///* Function Name : SelectAll */
//function SelectAllSavedSearch(CheckBoxControl) 
//{
//    if (CheckBoxControl.checked == true) 
//    {
//        var i;
//        for (i=0; i < document.forms[0].elements.length; i++) 
//        {
//            if ((document.forms[0].elements[i].type == 'checkbox')) 
//            {
//                document.forms[0].elements[i].checked = true;
//            }
//        }
//    } 
//    else 
//    {
//        var i;
//        for (i=0; i < document.forms[0].elements.length; i++) 
//        {
//            if ((document.forms[0].elements[i].type == 'checkbox')) 
//            {
//            document.forms[0].elements[i].checked = false;
//            
//            }
//        }
//       
//    }
//   HandleAJAXModalAlert('divMessageBox', 'none','hidden');
//}



 /* The Below Function will ensure that the CheckBoxList Control's Check Boxes Adhere To the Css Standards */
 
function InitCheckBoxes(){
    /* For checkbox Elements */
    /*
       var CheckBoxElements = document.forms[0].elements;
       for (var i=0; i < CheckBoxElements.length; i++) {
            if (CheckBoxElements[i].type=='checkbox' || CheckBoxElements[i].type=='radio')
            {
               CheckBoxElements[i].className  = 'checkbox';
            }
       }
    */
    $("input[type=checkbox]").addClass("checkbox");
    $("input[type=radio]").addClass("checkbox");
}

/*
function initPopups(){

if (!document.getElementsByTagName) return false;

  // For Anchor Elements
     var anchorLinks = document.getElementsByTagName("a");

     for (var i=0; i < anchorLinks.length; i++) {
           if (anchorLinks[i].className.match("popup")){
                anchorLinks[i].onclick = function() 
                {
                    window.open(this.href);
                    return false;
                }
            }
        }
        
  // For image Links
     var imgLinks = document.getElementsByTagName("img")
  
     for (var i=0; i < imgLinks.length; i++) {
       if (imgLinks[i].className.match("popupImage")){
            imgLinks[i].onclick = function() 
                {
                    window.open(this.alt);
                    return false;
                }
            }
        }
     }
     */

//window.onload = initPopups;

/*jQuery.fn.fadeTo = function(speed,to,callback) {
    return this.animate({opacity: to}, speed, function() {
        if (to == 1 && jQuery.browser.msie) 
            this.style.removeAttribute('filter'); 
        if (jQuery.isFunction(callback))
            callback(); 
    });
};*/


function onUpdating(target, cover){
    var updateProgressDiv = $('#'+cover);
    var domTarget = $('#'+target);
    var updateProgressDivImg = $('#'+cover + " img");
    
    
    //updateProgressDiv[0].style.display = '';
    
    var pos = domTarget.offset();
    
    var w = domTarget.outerWidth() - 2;
    var h = domTarget.outerHeight() - 2;
    
    var l =  (pos.left) + 1;
    var t =  (pos.top) + 1;
    
    var imgl = Math.round(domTarget.outerWidth() / 2) - Math.round(updateProgressDivImg.outerWidth() / 2);
    var imgt = Math.round(domTarget.outerHeight() / 2) - Math.round(updateProgressDivImg.outerHeight() / 2);
    
    updateProgressDiv.css( { 
        position: 'absolute',
        zIndex: 5000,
        left: l + "px", 
        top: t + "px",
        width: w + "px",
        height: h + "px"
    } );

    updateProgressDivImg.css( { 
        position: 'absolute',
        zIndex: 5001,
        left: imgl + "px", 
        top: imgt + "px"
    } ); 
   
   //updateProgressDiv[0].style.display = '';
   //$('.overlay').css('filter', 'alpha(opacity=70)');
   //$('#'+cover).fadeIn();
   $('#'+cover).fadeTo('slow',0.7);
}
            
function onUpdated(target, cover) {
    // get the update progress div
    //var updateProgressDiv = $get(cover); 
    //var domTarget = $get(target); 
    // make it invisible
    
    //$('#' + cover).fadeOut();
    //$('.overlay').css('filter', 'alpha(opacity=0)');
    
    $('#' + cover).fadeTo('slow',0,function() {
      $(this).hide();
    });

   
    //if (jQuery.browser.msie)
    //{
    //    $('#'+target).style.removeAttribute('filter');
    //}
    
    
    //updateProgressDiv.style.display = 'none';
    //$('#'+ target).fadeTo('slow',1.0);
} 

function highlight(){
    var highlight = $("[id$=hfHighlight]");
    if(highlight.val() == '1')
    {
        /*$("[id$=news]").stop().delay(1000).fadeIn(2000, function() {
             if (jQuery.browser.msie)  this.style.removeAttribute('filter'); 
           }
        ).delay(1500).animate({color: "#61bc18"},2000).delay(1000).animate({color: "#ffffff"},2000);*/
        
        $("[id$=news]").stop().delay(1500).animate({color: "#61bc18"},2000).delay(1000).animate({color: "#ffffff"},2000);
        
     }
}

function getUrlParameter(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}


function GetBacktoServer()
{
    str = window.location.pathname;
    index = str.lastIndexOf('/')
    page = str.substr(index+1,str.length-index);
    data = readCookie('history');
    if(data.toLowerCase() != page.toLowerCase())
        window.location.reload()
}

 function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	
	return null;
}