﻿/*==========================================================================
    Author: SPatel
    Date: 30 Dec 2005
    Description: Contains all common function to be used in our projects 
                 specially to retrieve data from different type of controls.
========================================================================*/
    //validion flag
    //var isControlValidated = true;
    //end

    //Get the selected radiobutton from the radiobutton list of ASP.NET radiobutton list control.
    function GetRadioListData(rListControlName)
    {
        var ret = null;
        var rControls = window.document.getElementsByName(rListControlName);
        for(var i=0; i<rControls.length; i++)
        {
			if (rControls[i].type == 'radio' && rControls[i].checked == true)
            {
                return rControls[i].value;
            }
        }
        if(rListControlName.indexOf('_') > 0)
        {
         rListControlName = ReplaceString(rListControlName,'_','$');
         ret = GetRadioListData(rListControlName);
        }
        return ret;
    };
    
    //Get the selected Checkbox from the checkbox list of Asp.Net checkboxlist control.
    function GetCheckboxListData(cListControlName)
    {    
 
    
        var chkControls = window.document.getElementsByName(cListControlName);
       
        if (chkControls.length <= 1)
        {
            chkControls = window.document.getElementsByTagName("input");
        }
        var chkControlsValue = null;
       
        for(var i=0; i<chkControls.length; i++)
        {  
         
          //  if (chkControls[i].type == 'checkbox' && chkControls[i].name.indexOf(cListControlName) >= 0 && chkControls[i].checked == true)
            if (chkControls[i].type == 'checkbox' && chkControls[i].id.indexOf(cListControlName) >= 0 && chkControls[i].checked == true)
            {   
                var chkValue = chkControls[i].name.substring(cListControlName.length + 1);
                if(chkControlsValue == null)
                {chkControlsValue = chkValue; }//chkControls[i].value;}
                else
                {chkControlsValue += ',' + chkValue;} //chkControls[i].value;}
                //break;
            }
        }
        //alert(chkControlsValue);
        return chkControlsValue;
    };

    //Get the selected Checkbox from the checkbox list of Asp.Net checkboxlist control.
    function GetCheckboxListValues(cListControlName)
    {    
        var chkControls = window.document.getElementsByName(cListControlName);
       
        if (chkControls.length <= 1)
        {
            chkControls = window.document.getElementsByTagName("input");
        }
        var chkControlsValue = null;
       
        for(var i=0; i<chkControls.length; i++)
        {  
         
          //  if (chkControls[i].type == 'checkbox' && chkControls[i].name.indexOf(cListControlName) >= 0 && chkControls[i].checked == true)
            if (chkControls[i].type == 'checkbox' && chkControls[i].name.indexOf(cListControlName) >= 0 && chkControls[i].checked == true)
            {   
                var chkValue = chkControls[i].value;
                if(chkControlsValue == null)
                {chkControlsValue = chkValue; }//chkControls[i].value;}
                else
                {chkControlsValue += ',' + chkValue;} //chkControls[i].value;}
                //break;
            }
        }
        //alert(chkControlsValue);
        return chkControlsValue;
    };
        
    //Get the selected Checkbox from the collection of Checkboxes that are not asp.net controls.
    //Note: Name of all checkboxes will be the same.
    function GetCheckboxListDataByName(cListControlName)
    {
     // alert("Common" + cListControlName);
        var chkControls = window.document.getElementsByName(cListControlName);
        var chkControlsValue = null;
        for(var i=0; i<chkControls.length; i++)
        {  
            if (chkControls[i].checked == true)
            {
                var chkValue = chkControls[i].value; 
                if(chkControlsValue == null)
                {chkControlsValue = chkValue; }
                else
                {chkControlsValue += ',' + chkValue;} 
            }
        }
        return chkControlsValue;
    };
    
    //Get the not selected Checkbox from the collection of Checkboxes that are not asp.net controls.
    //Note: Name of all checkboxes will be the same.
    function GetNotCheckedCheckboxListByName(cListControlName)
    {
        var chkControls = window.document.getElementsByName(cListControlName);
        var chkControlsValue = null;
        for(var i=0; i<chkControls.length; i++)
        {  
            if (chkControls[i].checked != true)
            {
                var chkValue = chkControls[i].value; 
                if(chkControlsValue == null)
                {chkControlsValue = chkValue; }
                else
                {chkControlsValue += ',' + chkValue;} 
            }
        }
        return chkControlsValue;
    };
    
    //Get the value from the textbox.
    function GetTextboxData(txtControlName)
    {
        var txtControl = window.document.getElementById(txtControlName);
        var txtControlValue = null;
        if ((txtControl.type == 'text') || (txtControl.type == 'password'))
            {
                txtControlValue = txtControl.value;
            }
        return txtControlValue;
    };
    
     //Get the value from the dropdown list.
    function GetDropDownData(drpControlName)
    {
        var drpControl = window.document.getElementById(drpControlName);
        var drpControlValue = null;
        //alert(drpControl.type); 
        if (drpControl.type == 'select-one')
        {
            drpControlValue = drpControl.value;
        }
        return drpControlValue;
    };
    
    //Get the true/false value of a Check box.
    //If checkbox is selected return true, else return false;
    function GetCheckboxData(chkControlName)
    {
        var chkControl = window.document.getElementById(chkControlName);
        var chkControlValue = false;
      
        if (chkControl.type == 'checkbox' && chkControl.checked == true)
        {
           chkControlValue = true;
        }
        return chkControlValue;
    };
    
      function GetListboxData(cListControlName)
    {
     var lstControl = window.document.getElementById(cListControlName);
     var lstControlsValue = null;
     if(lstControl != null)
     {
      for(i = 0; i < lstControl.options.length; i++)
     {  
        if(lstControlsValue == null)
         {lstControlsValue = lstControl.options[i].value; }
          else
         {lstControlsValue += ',' + lstControl.options[i].value;} 
     }
     }
    
     return lstControlsValue;
    };

//Opening a new window Starts Here
var win= null;
function NewWindow(mypage,myname,w,h,scroll,resize)
{
  
  var winl = (window.screen.width-w)/2;
 
  var wint = (window.screen.height-h)/2;
 
  var settings  ='height='+h+',';
      settings +='width='+w+',';
      settings +='top='+wint+',';
      settings +='left='+winl+',';
      settings +='scrollbars='+scroll+',';
      settings +='resizable='+resize+'';
     
      //,myname,settings
      if(navigator.appName!="Microsoft Internet Explorer")
        win=window.open(mypage,myname,settings);
        else
        win=window.open(mypage,"mywindow","width="+w+",height="+h+",screenX=50,left="+winl+",screenY=50,top="+winl+",status=no,menubar=no,scrollbars=yes,title=no,toolbar=no,location=no,directories=no");
  
  if(parseInt(navigator.appVersion) >= 4){win.window.focus();}
};
//Opening a new window Ends Here


/* repostioning the 3 content columns starts here */
function DivResposition()
{
//alert(window.screen.width)
var ScrWidth = window.screen.availWidth;
if (ScrWidth == 800) 
 {
 document.getElementById('leftColumn').style.marginLeft="auto";
 document.getElementById('leftColumn').style.left="0%";
 document.getElementById('centerColumn').style.marginLeft="215px";
 document.getElementById('rightColumn').style.marginLeft="375px";
 document.getElementById('rightColumn').style.left="50%";
// document.getElementById('centerColumn').style.left="0%";
	}
};

function welcometxtpos()
{
 document.getElementById('welcomtxtpos').style.marginLeft="-373px";
 document.getElementById('welcomtxtpos').style.Left="50%";
};

/* repostioning the 3 content columns ends here */

        //Check or Uncheck the Check boxes having the given name
        function CheckUnCheckAll(ctrl, chkControlName)
		{  
            var chkCtrls = window.document.getElementsByName(chkControlName);
            //alert(chkCtrls);
            if(chkCtrls != null)
            {
                for (var i=0;i<chkCtrls.length;i++)
                {
                    chkCtrls[i].checked = ctrl.checked;
                }
            }
		    return true;
		};

        //Check or Uncheck the Check boxes having the given name
        function CheckUnCheckAll(ctrl, chkControlName, chkHeaderControlName)
		{  
            var chkCtrls = window.document.getElementsByName(chkControlName);
            if(chkCtrls != null)
            {
                for (var i=0;i<chkCtrls.length;i++)
                {
                    chkCtrls[i].checked = ctrl.checked;
                }
            }   
            var chkHeaderCtrls = window.document.getElementsByName(chkHeaderControlName);
            if(chkHeaderCtrls != null)
            {
                for (var i=0;i<chkHeaderCtrls.length;i++)
                {
                    chkHeaderCtrls[i].checked = ctrl.checked;
                }
            }
		    return true;
		};
		 
		function UnCheckHeader(ctrl, chkControlName)
		{
		   if(ctrl.checked == false)
		   {
		      var chkCtrls = window.document.getElementById(chkControlName);
		      if(chkCtrls != null)
		      {
		         if(chkCtrls.checked == true)
		         {
		            chkCtrls.checked = false;
		         }
		      }
		   }
		};
		
		//Function to selectAll when all item is selected . by mukesh haval. 
		function CheckAll(ctrl, chkControlName)
		{  
		    var chkCtrls= window.document.getElementsByName('chkId');
            var ncnt = 0;
            if(chkCtrls != null)
            {
                for (var i=0;i<chkCtrls.length;i++)
                {
                    if(chkCtrls[i].checked)
                        ncnt ++;
                    else
                       ncnt --;
                }
                if(ncnt == chkCtrls.length)
                    document.getElementById('chkSelect').checked = true;
                else
                    document.getElementById('chkSelect').checked = false;
            }
		};
		
		 //Recursive function to replace All searchText with replaceText from content.
    function ReplaceString(content, searchText, replaceText)
    {
      content = content.replace(searchText, replaceText);
      if(content.indexOf(searchText) > -1)
      {
        content = ReplaceString(content, searchText, replaceText);
      }
      return content;
    };
    
     //Function used to check - uncheck rows in gridview
     function CheckAllDataGridCheckBoxes(aspCheckBoxID, selectAllId,chkControlId) {


        checkVal = document.getElementById(selectAllId);
        if (chkControlId)
        {
            chkControlId = document.getElementById(chkControlId); 
            if (!chkControlId.checked)
            {
                checkVal.checked=false;
                return;
            }
            return;
        }
        
        checkVal = checkVal.checked;
        re = new RegExp( aspCheckBoxID + '$')  //generated control name starts with a colon
        for(i = 0; i < document.forms[0].elements.length; i++) {

            elm = document.forms[0].elements[i];

            if (elm.type == 'checkbox') {

                if (re.test(elm.name)) {

                    elm.checked = checkVal;

                }
            }
        }
    };
    
    //Check whether records are selected to delete or not
    function CheckDataGridToDelete(aspCheckBoxID,msgDelete, msgSelect)
    { 
       var count =0;
       re = new RegExp( aspCheckBoxID + '$')  //generated control name starts with a colon
      
       for(i = 0; i < document.forms[0].elements.length; i++) {

            elm = document.forms[0].elements[i];


            if (elm.type == 'checkbox') {

                if (re.test(elm.name)) {
                   
                    if (elm.checked == true)
                    { 
                                                           
                      // alert(elm.getAttribute('dipti'));
                 
                       count = 1;
                    }

                }
            }
        }
        if (count == 1)
        {
            if(confirm(msgDelete))
				return true;
			else
	            return false;
	    }
        else
	         alert(msgSelect);
        return false;
    };

function CheckNotEmpty(poControlName)
{
  var poControl = window.document.getElementById(poControlName);
  if(poControl.value == "")
     {return false;}
  else
     {return true;}      
};


function Form_KeyPress(event, target,validateFunction) 
{		
  var sourceElement = event.srcElement;
  if(!sourceElement) sourceElement = event.target;
    if (event.keyCode == 13 && 
            !(sourceElement && 
                    (sourceElement.tagName.toLowerCase() == "textarea" || 
                    sourceElement.tagName.toLowerCase() == "select" ||
                    sourceElement.tagName.toLowerCase() == "a" ||
                    sourceElement.type == "submit" ||
                    sourceElement.type == "reset" ||
                    sourceElement.type == "file" ||
                    sourceElement.type == "image" )
            )
    ) {
        var defaultButton = document.getElementById(target);
        if (defaultButton && typeof (defaultButton.click) != "undefined") {
            
            event.cancelBubble = true;
            if (event.stopPropagation) {
                event.stopPropagation();
            }
           //return document.getElementById(target).click();
            //if(isControlValidated)
            if (validateFunction != undefined){
                if(validateFunction())            
                {
                   __doPostBack(target,'');
               }
            }
           return false;
        }
    }
    return true;
};

// Search Class
function GenericSearch(event, controlID, callBackFnc, isButton)
{		
	var element = null;
	var keyCode = null;
	try
	{				
		element = document.getElementById(controlID);
		if(element)
		{
			if(!isButton)
			{
				if(BarTab.Browser.IE)
					keyCode = event.keyCode;
				else if(BarTab.Browser.Mozilla)
					keyCode = event.which;
				
				if (keyCode == 13)
				{	
					if(callBackFnc)
					{
					    if(callBackFnc.indexOf("(") == -1)
							{eval(callBackFnc + "();");}
						else
							{eval(callBackFnc);}	
					}
					return true;
				}
			}
			else
			{
				return false;	
			}
		}
		else
			return false;
	}
	catch(e)
	{
		return false;
	}
	finally
	{
		element = null;
		keyCode = null;						
	}
};
	

/**
 * @member String: Encodes html characters
 * @return Encoded string 
 * @type String
 * version 1.0.0
 * author Pratik.Gohil
 */
String.prototype.encodeHTML = function()
{
	var encodedHTML	= this;	
	encodedHTML		= encodedHTML.replace( new RegExp( "&", "g" ), "&amp;");		
	encodedHTML		= encodedHTML.replace( new RegExp( "\"", "g" ), "&quot;");
	encodedHTML		= encodedHTML.replace( new RegExp( "<", "g" ), "&lt;");
	encodedHTML		= encodedHTML.replace( new RegExp( ">", "g" ), "&gt;");
	encodedHTML		= encodedHTML.replace( new RegExp( "'", "g" ), "&apos;");
	encodedHTML		= encodedHTML.replace( new RegExp( "&", "g" ), "~~##~~" );	
	return encodedHTML;		
};




/**
 * @member String: Decodes html characters
 * @return Decoded string 
 * @type String
 * version 1.0.0
 * author Pratik.Gohil
 */
String.prototype.decodeHTML = function()
{
	var decodeHTML	= this;
	decodeHTML		= decodeHTML.replace( new RegExp( "~~##~~", "g" ), "&");	
	decodeHTML		= decodeHTML.replace( new RegExp( "&amp;", "g" ), "&" );
	decodeHTML		= decodeHTML.replace( new RegExp( "&quot;", "g" ), "\"" );
	decodeHTML		= decodeHTML.replace( new RegExp( "&lt;", "g" ), "<" );
	decodeHTML		= decodeHTML.replace( new RegExp( "&gt;", "g" ), ">" );
	decodeHTML		= decodeHTML.replace( new RegExp( "&apos;", "g" ), "'" );
	return decodeHTML;		
};

/**
 * @member String: Trim input string
 * @return Trimed string 
 * @type String
 * version 1.0.0
 * author Pratik.Gohil
 */
String.prototype.Trim = function()
{
	return( this.replace(new RegExp("^([\\s]+)|([\\s]+)$", "gm"), "") );
};

/**
 * @member String: Left trim input string
 * @return Left trimed string 
 * @type String
 * version 1.0.0
 * author Pratik.Gohil
 */
String.prototype.LTrim = function()
{
	return( this.replace(new RegExp("^[\\s]+", "gm"), "") );
};
					 
/**
 * @member String: Right trim input string
 * @return Right trimed string 
 * @type String
 * version 1.0.0
 * author Pratik.Gohil
 */
String.prototype.RTrim = function()
{		
	return( this.replace(new RegExp("[\\s]+$", "gm"), "") );
};


// Code added by NG (04 April 2008)
function ShowHide(ctrlMoreID, ctrlLessID, status)
{
    
    if(status == 0)
    {
        document.getElementById(ctrlMoreID).style.display = '';
        document.getElementById(ctrlLessID).style.display = 'none';
    }
    else
    {
         document.getElementById(ctrlMoreID).style.display = 'none';
         document.getElementById(ctrlLessID).style.display = '';
    }
    return false;
    
};
// End of NG Code (04 April 2008)

function RefreshURL()
{
	var IsMozilla = navigator.userAgent.toLowerCase().indexOf('firefox') >= 0 ? true : false;
	var browserURL = parent.window.location.href;
	if(browserURL.indexOf("#refresh") != -1)
	{							
		browserURL = browserURL.replace("#refresh", "");
		parent.window.location.href = browserURL + "#refresh";
	}
	else
		{parent.window.location.href = browserURL + "#refresh";}
	browserURL = null;
	if(IsMozilla)
		{setTimeout("parent.window.location.reload();", 2000);}
};