﻿//================== Browser Identifier variables ==================
var isIE = (navigator.userAgent.indexOf("MSIE") > 1); 
var isMoz = (navigator.userAgent.indexOf("Mozilla/5.") == 0);
var showTR = isMoz?"table-row":"block";
//======================================================

function getScrollBottom(p_oElem)
{
    return p_oElem.scrollHeight - p_oElem.scrollTop - p_oElem.clientHeight;
}

//function to confirm delete operation
//evt:event
function confirmDelete(evt)
{
    return window.confirm("Are you sure you want to delete this record?");
}


//open security and roles dialog
function openSendPassword()
{
        window.showModalDialog("Forms/General/SendPassword.aspx",null,"center:1;status:0;dialogWidth:400px;dialogHeight:257px;");
}

//function to make sure only numeric values are allowed in textbox
//evt : event
//use : OnKeyPress(event)
function ensureNumeric(evt)
{
  if (isIE)
	var key =  evt.keyCode;
  else if(isMoz)
	var key =  evt.charCode;

  if (key!=0 && (key <48 || key >57) && key !=46)
  {
    evt.returnValue = false; 
    return false;
  }
}

//function to set the value of hidden variable(__hdnchksel) as per checkbox settings
//use : onmouseup="reg_chkSel();" - for the submit button of the page
function reg_chkSel()
{
    var nm = document.forms[0].chkSel.length;
    var ctrlArr = document.forms[0].chkSel;
    var idStr = "";
	
	//i=1 as the master checkbox is to be eliminated
	for(var i=1; i<ctrlArr.length; i++)
		if(ctrlArr[i].checked)
		    idStr += ctrlArr[i].getAttribute("keyval") + ";";
	
	if(idStr.length > 0)
	{
	    //set value in hidden control
	    document.getElementById("__hdnchksel").value = idStr;	
	        
	}	
}


//function to show lookup dialog
function showLookup(type,value,idTxtBox,nameTxtbox,selId)
{        
    var txtId = document.getElementById(idTxtBox);
    var txtText = document.getElementById(nameTxtbox);
    
    var url='Lookup.aspx?type='+type+'&dspName='+value+'&selId='+txtId.value;
    var result = window.showModalDialog(url,window,'dialogWidth:320px;dialogHeight:400px');	    	
    
    if(result==null)
        return;
    
    txtId.value = result[1];
    txtText.value = result[0];
}

//function to close the window onkeypress
function keyPressHandler(e) 
{
   var kC  = (window.event) ?    // MSIE or Firefox?
              event.keyCode : e.keyCode; 
   var Esc = (window.event) ?    
             27 : e.DOM_VK_ESCAPE // MSIE : Firefox
   if(kC==Esc) 
      window.close();
}
//function to select list item that matches key
//key to be matched
//source ddl
function selectDDLItem(key, ddl)
{
	if(key==null || key=="" || ddl == null) return;
	
	key = key.toUpperCase();
	var pos = 0;
	while(pos < ddl.options.length)
	{
		if(ddl.options[pos].value==key)
		 {
			ddl.selectedIndex = pos;
			break;
		 }
		 pos++;//increment counter           
	}
}

//Toggle show/hide for a section
//It assumes that the image will have the id = '<divID>Img'
var showImgPath = "../../Resources/images/aroL.gif";
var hideImgPath = "../../Resources/images/aroB.gif";
function showHideDiv(element)
{
    var divElem = document.getElementById(element);
    var img = document.getElementById(divElem.id+"Img");

    //if(divElem.style.display == "")
    //	divElem.style.display = "block";
    var showFlag = (divElem.style.display == "" || divElem.style.display == "block");
    divElem.style.display = showFlag?"none":"block";
    img.src = showFlag?showImgPath:hideImgPath;
}
function showHideSubItems(divID, itmCount)
{//Response.Write(string.Join("|",orgiteItems.Where(o => string.IsNullOrEmpty(o.ItemCode)).Select(o => o.SiteId + ";" + o.Id).ToArray<string>()));
 //alert(divID+":"+itmCount);
 var img = document.getElementById(divID+"_Img");
 var showFlag = false;
 for (i=0; i < parseInt(itmCount); i++)
 {
    var divElem = document.getElementById(divID+i.toString());
    if(divElem != null)
    {
        var showFlag = (divElem.style.display == "" || divElem.style.display == showTR);//"table-row");
        divElem.style.display = showFlag?"none":showTR;//"table-row";
        //divElem.style.height = showFlag?"0%":"100%";
    }
    else
        break;
 }
 //if(divElem.style.display == "")
 //	divElem.style.display = "block";
 img.src = showFlag?showImgPath:hideImgPath;
}
function isExpanded(divElem)
{
    return ((divElem!= null)&& (divElem.style.display == "" || divElem.style.display == showTR));
}
function toggleAllSites(showAll)
{
    try
    {
    var ids = siteIDs.split("|");
    var count = ids.length;
    
    for(var i=0; i < ids.length; i++)
    {
        var elem = ids[i].split(";")[0];
        var itmCount = ids[i].split(";")[1];
        //showHideAllDiv(elem);
        var isTRExpanded = isExpanded(document.getElementById(elem+"0"));
        if((showAll && !isTRExpanded) || (!showAll && isTRExpanded))//Hide it only if its visible
            showHideSubItems(elem, itmCount);
    }
    return false;
    }
    catch(err){}
}

function showHideAllDiv(element)
{//alert(element +":"+document.getElementById(element));
    var divElem = document.getElementById(element);
    var img = document.getElementById(divElem.id+"Img");
    var chkElem = document.getElementById(chkAllSitesElem);

    if(chkElem.checked)
    {
        divElem.style.display = "block";
        img.src = hideImgPath;
    }
    else
    {
        divElem.style.display = "none";
        img.src = showImgPath;
    }
        
}

function showDrilldown(itemId,siteId)
{
    window.open("ItemDrilldown.aspx?ItemId="+itemId+"&SiteId="+siteId,null,
    "directories=0,location=0,menubar=1,toolbar=0,resizable=1,scrollbars=1");
}

var swapAttr = "hoverSwapCSS";
function swapCSS(elem)
{
	if (elem == null)return;
	
	var tmp = elem.className;
	elem.className = elem.getAttribute(swapAttr);
	elem.setAttribute(swapAttr,tmp);
}

function sessionRedirect(page)
{
    alert('Your session has expired. Please login again.');
    if(typeof window.dialogArguments != 'undefined' || window.opener!= null)
    {window.close();}
    window.location = page;
}
function NewWindow(url,Name)
{
 var WindowOpen = window.open(url,Name);
  try
  {
    var obj = WindowOpen.name;
  } 
  catch(e)
  { 
    alert("Drilldown Report has been blocked by Browser POP-UP BLOCKER.\nPlease disable the Browser POP-UP BLOCKER and try again.");
  }
  return false;
}

function ensureCharacter(evt)
{
 if (isIE)
	var key =  evt.keyCode;
  else if(isMoz)
	var key =  evt.charCode;

  if (key==59)
  {
    alert('Character value ";" is not allowed. Please use "," instead to separate values.','Invalid value.');
    evt.returnValue = false; 
    return false;
  }        
}

/*
function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return myWidth;//We only need width for now  
}
*/
function getWindowWidth() {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;    
  } else if( document.documentElement && ( document.documentElement.clientWidth) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;    
  } else if( document.body && ( document.body.clientWidth ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;    
  }
  return myWidth;
}
function getWindowHeight() {
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}

function promptIfEmpty(ctrl)
{
	if(ctrl.value=='')
	{
		alert("This is a required field");
		if (document.all)//for IE
			ctrl.focus();
		else // for STUPID Mozilla (no consistent focus method
		{
		ctrl.value = "somevalue";
		document.getElementById(ctrl.id).focus();
		}
		return false;
	}
}
function setEnableSite(checked, SiteId)
{
    //alert(checked + ":" + document.getElementById("OrgSpecificSiteCode"+SiteId));
    document.getElementById("OrgSpecificSiteCode"+SiteId).style.display = checked?"inline":"none";
    document.getElementById("OrgSpecificSiteName"+SiteId).style.display = checked?"inline":"none";

    document.getElementById("OrgSpecificSiteCodeLabel"+SiteId).style.display = checked?"none":"inline";
    document.getElementById("OrgSpecificSiteNameLabel"+SiteId).style.display = checked?"none":"inline";
    //document.getElementById("OrgSpecificSiteCode"+SiteId).disabled = !checked;
}
function setEnableItem(checked, ItemId)
{
    //alert(checked + ":" + document.getElementById("OrgSpecificItemCode"+ItemId));
    document.getElementById("OrgSpecificItemCode"+ItemId).style.display = checked?"inline":"none";
    document.getElementById("OrgSpecificItemName"+ItemId).style.display = checked?"inline":"none";

    document.getElementById("OrgSpecificItemCodeLabel"+ItemId).style.display = checked?"none":"inline";
    document.getElementById("OrgSpecificItemNameLabel"+ItemId).style.display = checked?"none":"inline";
    //document.getElementById("OrgSpecificSiteCode"+ItemId).disabled = !checked;
}
function setEnableItemForItem(checked, ItemId)
{
    //alert(checked + ":" + document.getElementById("OrgSpecificItemCode"+ItemId));
    document.getElementById("ItemCode"+ItemId).style.display = checked?"inline":"none";
    document.getElementById("ItemDescr"+ItemId).style.display = checked?"inline":"none";

    document.getElementById("ItemCodeLabel"+ItemId).style.display = checked?"none":"block";
    document.getElementById("ItemDescrLabel"+ItemId).style.display = checked?"none":"block";
    //document.getElementById("OrgSpecificSiteCode"+ItemId).disabled = !checked;
}
//HT: Kept to learn in future : required field
//function setSearchCriteriaTitle(titleTextBoxId)
//{
//    var txtTitle = document.getElementById(titleTextBoxId);
//    txtTitle.value = "";//Reset title
//    //Loop until user provides a value
//    while(txtTitle.value != null && txtTitle.value.length < 1)
//    {
//        var dt = new Date();
//        txtTitle.value = prompt("Saved search title",dt.toDateString() + " " + dt.toTimeString());
//    }
//    return true;
//}

/* START ::: Custom filter textbox scripts: Note: if grows pls create a separate script file*/
function doEvent(evtID,txt,ddl)
{
    //Get txt or ddl from either of the control which has values
    if(txt==null) txt = document.getElementById(ddl.getAttribute("txt"));
    else if(ddl==null) ddl = document.getElementById(txt.getAttribute("ddl"));
    else return;//invalid call!
    
    //Do process as per the event
    switch (evtID)
        {        
            case "1": toggleDDL(txt,ddl);break;//evt.toggleDDL
            case "2": setFiltered(ddl.value,txt);break;//evt.ddlChange X toggleDDL(txt,ddl);
            case "3": ddl.onchange();break;//evt.txtChange
            //default: txt.value = optr + val + optr;break; // opts.contains 
        }
}
function toggleDDL(txt,ddl)
{
    flag = (ddl.style.display == "none");//(document.getElementById(ddlID)
    
    ddl.style.display = flag?"inline":"none";
    ddl.style.width = flag?"80px":"0px";    
    
    var width = txt.getAttribute("width1");
    txt.style.width = flag?"53px":width;
 }

function setFiltered(filter, txt)
{
    var optr = "%";    
    //txt = document.getElementById(txt);
    var val = txt.value.replace("%","").replace("%","").replace("%","").replace("%","");//replace % 4 times
    
    if(val != null && val.length > 0)
    {//Proceed only if value is non-empty
        switch (filter)
        {        
            case "3": txt.value = val + optr;break;//opts.starts_with
            case "4": txt.value = optr + val;break;//opts.ends_with
            case "2": txt.value = val;break;//opts.exact
            
            case "1": 
            default: txt.value = optr + val + optr;break; // opts.contains 
        }
     }
    txt.focus();//set back focus
    //Set value in filter-textbox (if any)
    var paramTxt = txt.getAttribute("paramTxt");    
    if(paramTxt!= null && paramTxt.length > 0)
        document.getElementById(paramTxt).value = txt.value;    
}
/* END ::: Custom filter textbox scripts: Note: if grows pls create a separate script file*/
