//*******************************************************************************
//'* Purpose  : Commonly used Javascript Functions.
//'* Auth     : Paresh Bhurke
//'* Date     : 03-Sep-2004
//'* Author   : Paresh A Bhurke
//'* History      Auth        Date	Description
//'  --------     --------    ------- ---------------------------------------------
//'
//'*******************************************************************************
// Start
function DatePicker(thisForm,thisControl) 
{
  
var url = "f.aspx?url=DatePicker.aspx?fn="+thisForm+"&tn="+thisControl;
var returnVal = window.showModalDialog(url, null, "dialogWidth:225px;dialogHeight:230px; center:yes;help:no;resizable:no;scroll:no;status:no;")

if (returnVal != null)
{

	document.all(thisControl).value = returnVal.r_name;
	
	if (returnVal.r_refresh)
	{
		document.Form1.submit();
	}

}
      
}


function MO(e)
{

if (!e)
var e=window.event;
var S=e.srcElement;
while (S.tagName!="TD")
{S=S.parentElement;}
S.className="T";
}
function MU(e)
{
if (!e)
var e=window.event;
var S=e.srcElement;
while (S.tagName!="TD")
{S=S.parentElement;}
S.className="P";
}
function alltrim(strLine)
{	var trimstr = ""
	trimstr = rtrim(ltrim(strLine))
	return trimstr
}

function ltrim(strLine)
{var trimstr = ""
	var blkflg = 0
	var len    = strLine.length
	for (var i=0; i<=len-1; i++)
	{if(strLine.charAt(i) != " " )
		{{
				trimstr= strLine.substring(i,len)
				blkflg = 1
				break }}}
	return trimstr
}	

function rtrim(strLine)
{var trimstr = ""
	var blkflg = 0
	var len    = strLine.length-1
	var i = len
	while (strLine.charAt(i) == " ") {i=i-1 }
	trimstr = strLine.substring(0,i+1)
	return trimstr
}

function isblank(objfld)
{
	if(alltrim(objfld.value)==""){
		var objType = objfld.type
		if(objType!='hidden'){
			objfld.focus()
		}
		return true
	}
	else{
		return false}
}

function ispositive(strLineval)
{
	var strLine1 = ltrim(rtrim(strLineval))
	if (isnumeric(strLine1)) {
	   return(strLine1>0?true:false)
	}
	else {
	    return false
	}
}

function ContainOnlyAlphabets(strValue)
{
          strValue = strValue.toUpperCase();
          for (i=0;i<strValue.length;i++)
          {
               if (strValue.charAt(i)<"A" || strValue.charAt(i) > "Z")
               {
                    return false;
               }
          }
          return true;
}

function isnegative(objfldval)
{
   if (isnumeric(objfldval)) 
   {
      return(objfldval<0?true:false)
   }
   else 
   {
	   return false
   }
}

function isinteger(objfldval)
{
    if (isnumeric(objfldval)) {
		return ((objfldval+"").indexOf(".")>=0?false:true) 
		}
	else
	{
	return false
	}
}

function isnumeric(objfldval) 
{
   return (isNaN(objfldval)?false:true)
}

function isreal(strLineval)
{
    if (isnumeric(strLineval)) {
		return (strLineval.indexOf(".")<0?false:true) 
		}
	else
	{return false
	}
}

function contains(strLine,strLine2)
{
	return (strLine.indexOf(strLine2)>=0?true:false)
}

function upper(strLine)
{
	return (strLine.toUpperCase())
}

function lower(strLine)
{
	return (strLine.toLowerCase())
}
function isEmail(strObject)
{
	EStr = strObject.value
	
	/*EArray = EStr.split(",")

	for (var i = 0; i < EArray.length; i++) 	
	{
		if(EArray[i].indexOf("@")==-1 || EArray[i].indexOf(".")== -1)
			{
				strObject.focus();
				return false
			}
	}
	return true*/
	
	if(EStr.length>0)
	{
		var chkEmail = false;				
		
		var sEmail = strObject.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);		
		if (sEmail!=null){															
			chkEmail = true;
			
		} else {					
			chkEmail = false;
		}
		
		return chkEmail
	}			
	return false;
}

function CountSelectedItemsInList(listobj){
	if(listobj!=null){
		var i
		var mulselect = 0
		for(i=0;i<listobj.length;i++){
			if(listobj.options[i].selected==true){
				mulselect = mulselect+1
			}
		}
		return mulselect
	}	
}

function CountSelectedItemsInList(listobj){
	if(listobj!=null){
		var i
		var mulselect = 0
		for(i=0;i<listobj.length;i++){
			if(listobj.options[i].selected==true){
				mulselect = mulselect+1
			}
		}
		return mulselect
	}	
}

// End



function ShowLayer(sShowElement, aElements)
{
	for (var i = 0; i < aElements.length; i++)
	{
		var dispStatus = document.getElementById(aElements[i]).style.display
		var obj
		if (sShowElement == aElements[i])
		{	
				obj = document.getElementById(aElements[i])
				obj.style.display = "";
		}
		else
		{
			document.getElementById(aElements[i]).style.display = "none";
		}
	}
}

//Function for value Trimming

function trim(strTrim){

	 while(strTrim.charAt(0) == " " || strTrim.charAt(eval(strTrim.length-1)) == " "){
	 if(strTrim.charAt(0) == " "){
			strTrim = strTrim.slice(1,strTrim.length)
			}
	 if(strTrim.charAt(eval(strTrim.length-1)) == " "){
			strTrim = strTrim.slice(0,eval(strTrim.length-1))
			}
	 }
	 return strTrim
}

//Function for Quote display

function quote(fieldvalue){

		if(fieldvalue.match(squote) != null) return 0
		else return 1
}	

//Start Functions for ExpandAll / CloseAll / Drill down functionality
		function ShowDrillDownLayer(LayerPrefix, LayerID, customStatus)
		{
			
			var layerObj = document.getElementById(LayerPrefix + "_" + LayerID)
			if(layerObj==null)
			{
				return;
			}
			var dispStatus = layerObj.style.display
				
					if(customStatus!=null)
					{
						dispStatus = ""
						if(customStatus=="expand"){dispStatus = "none"}
					}
					
					if(dispStatus==""){
						layerObj.style.display = "none";
					}else{
						layerObj.style.display = "";
					}

					eval("var img=document.forms(0)." + LayerPrefix + "_Img" + LayerID)
					SwapImage(img, customStatus)
			
		}
		
//End Functions for ExpandAll / CloseAll / Drill down functionality
function OpenExcelWindow(PageName)
{
	NewWin = window.open("PreExportToExcel.aspx","PreviewProfile","width=800,height=600,status=no,scrollbars=yes,minimize=no,top=10,left=0");
	return false;
}

/* Functions for soft lock implementation - Anshul */

	//This function will unlock the notice in the session		
	function RemoveLock(itemID, callingFunction)
	{
		// Create the xmlhttp request to send to release notice lock
		document.cookie = "PageEditMode=0";
		var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		xmlhttp.open("POST","../SoftLockActions.aspx?mode=LS&Type=Unlock&Item=" + itemID + "&Function=" + callingFunction,false);			
		xmlhttp.send();	
		var responseText = xmlhttp.responseText;
		return responseText;
	}

	//Function to lock the selected notice
	function SetLock(itemID, callingFunction)
	{
		// Create the xmlhttp request to send to set notice status
		document.cookie = "PageEditMode=1";
		var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		xmlhttp.open("POST","../SoftLockActions.aspx?mode=LS&Type=Lock&Item=" + itemID + "&Function=" + callingFunction,false);			
		xmlhttp.send();
		return;
	}
	

	
	//Function to get the notice lock status
	function GetLockStatus(itemID, callingFunction)
	{			
		try
		{
			// Create the xmlhttp request to send to get notice status
			var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			xmlhttp.open("POST","../SoftLockActions.aspx?mode=LS&Type=GetStatus&Item=" + itemID + "&Function=" + callingFunction,false);			
			xmlhttp.send();
				
			//DownLoad NoticeStatus
			var responseText = xmlhttp.responseText;
			return responseText;
		}
		catch(Exception)
		{
		}
	}
		 
/* end of functions for soft lock implementation - Anshul */




//Added by Paresh, for handling OnSubmit Validation in Reports GroupBy functionality
//Start
function GroupByValidations(rbControls,lstControls, ErrorMessages )
{

	var rbObj, lstObj
	for(i=0;i<=(rbControls.length-1);i++)
	{
		rbObj=null;
		lstObj=null;
		if(rbControls[i]!='') {eval('rbObj  = document.forms(0).' + rbControls[i])}
		if(lstControls[i]!=''){eval('lstObj = document.forms(0).' + lstControls[i])}
		if(rbObj!=null && lstObj!=null)
		{
			if(rbObj.checked)
			{
				if(CountSelectedItemsInList(lstObj)<=0)
				{
					alert(ErrorMessages[i]);
					lstObj.focus();
					return false;
				}
			}
		}
	}
}

// Entered by Neha for checking white spaces in a field

var whitespace = " \t\n\r";

function isEmpty(s)
{   
	return ((s == null) || (s.length == 0))
}
				
	
function isWhitespace (s)
{   
	var i;
	if (isEmpty(s)) return true;
	for (i = 0; i < s.length; i++)
	{   
		// Check that current character isn't whitespace.
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1) return false;
	}
	// All characters are whitespace.
	return true;
}

		
//End
		

//-- Start for showing selected radio button and multi selected  list box on lick of Back Link in Report.
function DetectSelectedRadioButton()
{
		var x;
		for (x = 0;x < document.all.length; x++)
		{
			if(document.all(x).type=="radio")
			{
				if(document.all(x).checked==true)
				{
					document.all(x).click()
				}
			}
		}
}
//----- End
        function TrapEnterKey(btn, event)
        {
			if (document.all)
			{
		        if (event.keyCode == 13)
		        {
			        event.returnValue=false;
			        event.cancel = true;
					document.getElementById(btn).click();
				}
			}
			else if (document.getElementById)
			{
				if (event.which == 13)
				{
					event.returnValue=false;
					event.cancel = true;
					document.getElementById(btn).click();
				}
			}
			else if(document.layers)
			{
		        if(event.which == 13)
		        {
			        event.returnValue=false;
				    event.cancel = true;
					document.getElementById(btn).click();
				}
			}
        }

//****************************************************************************************
//* Function:	 fnCheckEmail 
//* Description: Returns true if the input string is a represents a valid email otherwise false.
//*				 e.g. abc@yahoo.co.in 
//* Added by pravin for EmailProfile Pages
//*****************************************************************************************
function fnCheckEmail(sField) 
{
var value = sField;      	
var rx = new RegExp("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");	
var matches = rx.exec(value);
var succ = (matches != null && value == matches[0]);    
return succ;
}

function textCounter(field,maxlimit,displayfield, evt) {
	if (evt.shiftKey) 
	{ //alert(evt.keyCode);
		if ((evt.keyCode <33 || evt.keyCode>40) && (field.value.length >=maxlimit))
			{
			displayfield.innerHTML = field.value.length+" / "+maxlimit;
			return false;
			}
		displayfield.innerHTML = field.value.length+" / "+maxlimit;
		return true;
	}
	var allowKey = false;
	if ((field.value.length > 0) && (field.value.length <maxlimit))
		allowKey = true;
	else {
			var keyCode = document.layers ? evt.which : evt.keyCode;
			if ((keyCode <= 40  && keyCode !=13 && keyCode !=32) || keyCode==46)
				allowKey = true;
			else {
				field.value = field.value.substring(0,maxlimit);
				allowKey = field.value.length < maxlimit;
			}
		}
	displayfield.innerHTML = field.value.length+" / "+maxlimit;
	return allowKey;
}	