//-- Common Function to validate Add or Remove from User Ctrl of Multi List Box.
//Display date
function GetDay(intDay){
    var DayArray = new Array("Sunday", "Monday", "Tuesday", "Wednesday", 
                         "Thursday", "Friday", "Saturday")
    return DayArray[intDay]
    }

  function GetMonth(intMonth){
    var MonthArray = new Array("January", "February", "March",
                               "April", "May", "June",
                               "July", "August", "September",
                               "October", "November", "December") 
    return MonthArray[intMonth] 	  	 
    }
  function getDateStrWithDOW(){
    var today = new Date()
    var year = today.getFullYear()
    if(year<1000) year+=1900
    var todayStr = GetDay(today.getDay()) + ", "
    todayStr += GetMonth(today.getMonth()) + " " + today.getDate()
    todayStr += ", " + year
    return todayStr
    }



//-- Common Function to Trim the empty space in front and back value of each ctrl.
function stripBlanks(ObjCtrl)
{
 	    var newString;
	    var i;
	    var j;
	    var blank;
	    blank = " ";
	    newString = "";
	    aString = ObjCtrl.value;	
	    if(aString == null) aString = " ";
	    for (i=0; i<aString.length; i++)
	    { 
		    if (aString.charAt(i) != blank && aString.charCodeAt(i)!= 13 && aString.charCodeAt(i) != 10 )
		    {
			    break;
		    }
	    }
	    for (j=aString.length-1; j>=0; j--)
	    {
		    if (aString.charAt(j) != blank   && aString.charCodeAt(j)!= 13 && aString.charCodeAt(j) != 10 )
		    {
				    break;
		    }
	    }
	    for(k=i;k<=j;k++)
	    {
		    newString += aString.charAt(k);
	    }	
	    return newString;	
}

function isempty(strToCheck)
{
	var bStringEmpty = true;
	if( strToCheck != null )
	{

		//getting the length of the string
		var nLength = strToCheck.length
	
		//checking the length of the contents of the text box
	
		if(nLength > 0)
		{
			
			for(i=0;i<nLength;i++)
			{
				if (strToCheck.charAt(i) != " " && strToCheck.charAt(i) != "\t" && strToCheck.charCodeAt(i) != 13 && strToCheck.charCodeAt(i) != 10 )
				{
					bStringEmpty = false;
					break;
				}
			}
		}
	}
	return bStringEmpty;
}

//-- Common Function to validate where the ctrl(for all the ctrl) is left empty or not selected.
function checkIsEmpty(ObjCtrl,strTitle)
{
	var bStringEmpty = true;
	var strValue = ObjCtrl.value;
	var strType = ObjCtrl.type;
	strType = strType.toUpperCase();

	if(strType == "TEXT" || strType == "TEXTAREA" || strType == "PASSWORD")
	{
		if( strValue != null )
		{
			//getting the length of the string
			var nLength = strValue.length
		
			//checking the length of the contents of the text box
			if(nLength > 0)
			{
				for(i=0;i<nLength;i++)
				{
					if (strValue.charAt(i) != " " && strValue.charAt(i) != "\t" && strValue.charCodeAt(i) != 13 && strValue.charCodeAt(i) != 10 )
					{
						bStringEmpty = false;
						break;
					}
				}
			}
		}
		if (bStringEmpty)
		{
			 alert("Please enter the " + strTitle + ".");
			 ObjCtrl.focus();
		}
	}
	else if(strType == "SELECT-ONE")
	{
		if(ObjCtrl.selectedIndex > 0)
		{
			bStringEmpty = false;
		}
		if (bStringEmpty)
		{
			 alert("Please select a " + strTitle + ".");
			 ObjCtrl.focus();
		}
	}
	
	return bStringEmpty;
}


function checkIsSetEmpty(ObjCtrl)
{
	var bStringEmpty = true;
	var strValue = ObjCtrl.value;
	var strType = ObjCtrl.type;
	strType = strType.toUpperCase();

	if(strType == "TEXT" || strType == "TEXTAREA")
	{
		if( strValue != null )
		{
			//getting the length of the string
			var nLength = strValue.length
		
			//checking the length of the contents of the text box
			if(nLength > 0)
			{
				for(i=0;i<nLength;i++)
				{
					if (strValue.charAt(i) != " " && strValue.charAt(i) != "\t" && strValue.charCodeAt(i) != 13 && strValue.charCodeAt(i) != 10 )
					{
						bStringEmpty = false;
						break;
					}
				}
			}
		}
	}
	else if(strType == "SELECT-ONE")
	{
		if(ObjCtrl.selectedIndex > 0)
		{
			bStringEmpty = false;
		}
	}
	
	return bStringEmpty;
}



//-- Common Function to validate where the ctrl value has valid Numbers.
function checkNumber(ObjCtrl,strTitle)
{
	
	var strTmp = ObjCtrl.value;
	
	var valid = "0123456789";
	var ok = "yes";
	var temp;
	for (var i=0; i<strTmp.length; i++)
	{
		temp = ""+ strTmp.substring(i, i+1);
				
		if (valid.indexOf(temp) == -1) 
		{
			ok = "no";
			break;
		}
	}
	if (ok == "no")
	{  
		alert("Please enter only numbers in " + strTitle +".");
		ObjCtrl.focus();
		return false;
	}	
	return true;
}      

//-- Common Function to validate where the ctrl value has valid Decimal Numbers.
function checkDecimalNumber(ObjCtrl,strTitle)
{
				
		var strTmp = ObjCtrl.value;
	
		var valid = "0123456789.";
		var ok = "yes";
		var temp;
		for (var i=0; i<strTmp.length; i++)
		{
			temp = ""+ strTmp.substring(i, i+1);
					
			if (valid.indexOf(temp) == -1) 
			{
				ok = "no";
				break;
			}
		}
		if (ok == "no")
		{  
			alert("Please enter only numbers in " + strTitle +".");
			ObjCtrl.focus();
			return false;
		}	
		return true;
}
        
//-- Common Function to validate where the ctrl value has valid Phone Numbers with Plus sign and etc.
function checkPhoneNumberPlus(ObjCtrl,strTitle)
{
		var strTmp = ObjCtrl.value;
	
		var valid = "1234567890 ()+" ;
		var ok = "yes";
		var temp;
		for (var i=0; i<strTmp.length; i++)
		{
			temp = ""+ strTmp.substring(i, i+1);
					
			if (valid.indexOf(temp) == -1) 
			{
				ok = "no";
				break;
			}
		}
		if (ok == "no")
		{  
			alert("Please enter only numbers, space, plus and parenthesis in " + strTitle + ".")
			ObjCtrl.focus();
			return false;
		}	

		return true;
}

//-- Common Function to validate where the ctrl value has valid Phone Numbers.
function checkPhoneNumber(ObjCtrl,strTitle)
{
		var strTmp = ObjCtrl.value;
	
		var valid = "1234567890 " ;
		var ok = "yes";
		var temp;
		for (var i=0; i<strTmp.length; i++)
		{
			temp = ""+ strTmp.substring(i, i+1);
					
			if (valid.indexOf(temp) == -1) 
			{
				ok = "no";
				break;
			}
		}
		if (ok == "no")
		{  
			alert("Please enter only numbers and space in " + strTitle + ".")
			ObjCtrl.focus();
			return false;
		}	
		return true;
}
        
        
//-- Common Function to validate where the ctrl value has valid EmailID.
function checkEmail(ObjCtrl, strTtile)
{
		var strValue = ObjCtrl.value;
		var filter  = /^([a-zA-Z0-9_\.\-'])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (!filter.test(strValue))
		{
			alert("E-mail ID should be of the form - name@company.com or name@company.co.country in " + strTtile + ". Please try again.");
			ObjCtrl.focus();
			return false;			
		}  
		return true;			        
}	

function ConfirmIdentityValidate(strEmailID)
    {
        var EmailFormat  = /^([a-zA-Z0-9_\.\-'])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (!EmailFormat.test(strEmailID))
        {
            alert("E-mail ID should be of the form - name@company.com or name@company.co.country in HOD E-mail ID. Please try again.")
            return false;
        } 
        else
        {
            return true;
        }
    }   
      
//-- Common Function to validate where the ctrl value has valid Name.
function checkName(ObjCtrl,strTitle)
{
		var strTmp = ObjCtrl.value;  	
		
		var valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz " ;
		var temp;
		for (var i=0; i<strTmp.length; i++)
		{
			temp = ""+ strTmp.substring(i, i+1);
					
			if (valid.indexOf(temp) == -1) 
			{
				alert("Please enter only alphabetic characters and space in " + strTitle + ".");
				ObjCtrl.focus();
				return false;
				break;
			}
		}
		return true;   	
}
//-- Common Function to validate where the ctrl value has valid Name.
function checkFLName(ObjCtrl,strTitle)
{
		var strTmp = ObjCtrl.value;  	
		
		var valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' " ;
		var temp;
		for (var i=0; i<strTmp.length; i++)
		{
			temp = ""+ strTmp.substring(i, i+1);
					
			if (valid.indexOf(temp) == -1) 
			{
				alert("Please enter only alphabetic characters,single quote and space in " + strTitle + ".");
				ObjCtrl.focus();
				return false;
				break;
			}
		}
		return true;   	
}
//-- Common Function to validate where the ctrl value has valid specified length.
function checkMaxLength(ObjCtrl,intLength,strTitle)
{
		var sourcetextlen = ObjCtrl.value.length;
		if (sourcetextlen > intLength)
		{
			alert("Please enter only a maximum of " + intLength + " characters in " + strTitle + "." );
			ObjCtrl.focus();
			return false;
		}
		return true;
}

//-- Common Function to validate where the ctrl value has atleast specified length.
function checkMinLength(ObjCtrl,intLength,strTitle)
{
		var sourcetextlen = ObjCtrl.value.length;
		if (sourcetextlen < intLength)
		{
			alert("Please enter atleast a minimum of " + intLength + " characters in " + strTitle + "." );
			ObjCtrl.focus();
			return false;
		}
		return true;
}
//-- Common Function to validate where the ctrl value has valid Name.
function checkFirstLetterAlbhabet(ObjCtrl,strTitle)
{
		var strTmp = ObjCtrl.value;  	
		
		var valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" ;
		var temp;
		temp = ""+ strTmp.substring(0,1);
		if (valid.indexOf(temp) == -1) 
		{
				alert("Please enter only alphabetic character first character " + strTitle + ".");
				ObjCtrl.focus();
				return false;
		}
		return true;   	
}
//-- Common Function to validate where the ctrl value has valid Alphabets.
function checkAlphabet(ObjCtrl,strTitle)
{
		var strTmp = ObjCtrl.value;  	
		
		var valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" ;
		var temp;
		for (var i=0; i<strTmp.length; i++)
		{
			temp = ""+ strTmp.substring(i, i+1);
					
			if (valid.indexOf(temp) == -1) 
			{
				alert("Please enter only alphabetic characters in " + strTitle + ".");
				ObjCtrl.focus();
				return false;
				break;
			}
		}
		return true;   	
}

//-- Common Function to validate where the ctrl value has valid Alpha Numeric.
function checkAlphaNumeric(ObjCtrl,strTitle)
{
	
		var strTmp = ObjCtrl.value;
		
		var valid = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz." ;
		var ok = "yes";
		var temp;
		for (var i=0; i<strTmp.length; i++)
		{
			temp = ""+ strTmp.substring(i, i+1);
					
			if (valid.indexOf(temp) == -1) 
			{
				ok = "no";
				break;
			}
		}
		if (ok == "no")
		{  
			alert("Please enter only alphabetic characters and numbers in " + strTitle +".");
			ObjCtrl.focus();
			return false;
		}	
		return true;
}

//-- Common Function to validate where the ctrl value has valid Zip Code.
function checkZipCode(ObjCtrl,strTitle)
{
		var strTmp = ObjCtrl.value;
		var intBlankOccurance = 0;
		var strAlertMsg;
		var valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 ";
		var temp;
	
		for (var i=0; i<strTmp.length; i++)
		{
			temp = ""+ strTmp.substring(i, i+1);
	
			if (valid.indexOf(temp) == -1)
			{
				return false;
				break;
			}
			else
			{
				if(temp == " ")
				{
					intBlankOccurance = intBlankOccurance + 1;
				}
			}
		}
		if (intBlankOccurance > 1)
		{
			alert("Please enter only alphabetic characters, numbers and space in " + strTitle + ".");
			ObjCtrl.focus();
			return false;
		}
		else
			return true;
}	

function checkDeleteConfirm(ObjCtrl,strTitle)
{
	var strValue = ObjCtrl.options[ObjCtrl.selectedIndex].text 
	if (confirm("Are you sure you want to delete the " + strTitle + " '" + strValue + "' ?"))
		return true;
	else
		return false;
}


function checkRestoreConfirm(ObjCtrl,strTitle)
{
	var strValue = ObjCtrl.options[ObjCtrl.selectedIndex].text 
	if (confirm("Are you sure you want to Restore the " + strTitle + " '" + strValue + "' ?"))
		return true;
	else
		return false;
}

function checkDecimalNumber(ObjCtrl,strTitle)

{     

      var strTmp = ObjCtrl.value;      

      var valid = "0123456789.";
      var ok = "yes";
      var temp;
      var intDotCnt = 0;
      

      for (var i=0; i<strTmp.length; i++)
      {
            temp = ""+ strTmp.substring(i, i+1);                       

            if (valid.indexOf(temp) == -1) 
            {
                  ok = "no";
                  break;
            }
            if (temp == '.')

                  intDotCnt = intDotCnt + 1;

            if (intDotCnt > 0 && temp == ',')

                  intDotCnt = 100;

            if (i == 0 && temp == ',')

                  intDotCnt = 100;

            if (i == strTmp.length -1 && (temp == ',' || temp == '.'))

                  intDotCnt = 100;
      }

      if (strTmp.indexOf("-") > 0)
            intDotCnt = 100;
         

      if (ok == "no")
      {  
            alert("Please enter only numbers and decimal in " + strTitle +".");
            ObjCtrl.focus();
            return false;
      }     

      if (intDotCnt > 1)
      {
            alert("Please enter a valid decimal value in " + strTitle +".");
            ObjCtrl.focus();
            return false;
      }

      return true;

}

//-- Common Function to validate where the ctrl value has valid Alpha Numeric.
function checkAlphaNumericandSpace(ObjCtrl,strTitle)
{
	
		var strTmp = ObjCtrl.value;
		
		var valid = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz " ;
		var ok = "yes";
		var temp;
		for (var i=0; i<strTmp.length; i++)
		{
			temp = ""+ strTmp.substring(i, i+1);
					
			if (valid.indexOf(temp) == -1) 
			{
				ok = "no";
				break;
			}
		}
		if (ok == "no")
		{  
			alert("Please enter only alphabetic characters, numbers and space in " + strTitle +".");
			ObjCtrl.focus();
			return false;
		}	
		return true;
}

//-- Common Function to validate where the  value has valid Alpha Numeric, Number, Space, , Hyphen, Ambersand & Single Quote.
function checkAlphaNumericSpaceHyphenAmbersandSingleQuote(ObjCtrl,strTitle)
//checkAlphaNumericSpaceHyphenAmbersandSingleQuote
{
	
		var strTmp = ObjCtrl.value;
		
		var valid = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz -&'" ;
		var ok = "yes";
		var temp;
		for (var i=0; i<strTmp.length; i++)
		{
			temp = ""+ strTmp.substring(i, i+1);
					
			if (valid.indexOf(temp) == -1) 
			{
				ok = "no";
				break;
			}
		}
		if (ok == "no")
		{  
			alert("Please enter only alphabetic characters, numbers, space, single quote, ampersand and hyphen in " + strTitle +".");
			ObjCtrl.focus();
			return false;
		}	
		return true;
}


// Calender Scripts
    var oldLink = null;
    function setActiveStyleSheet(link, title) 
    {
      var i, a, main;
      for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
        if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
          a.disabled = true;
          if(a.getAttribute("title") == title) a.disabled = false;
        }
      }
      
    }
    function selected(cal, date) 
    {
      cal.sel.value = date; 
      if (cal.dateClicked && (cal.sel.id == "sel1" || cal.sel.id == "sel3"))
        cal.callCloseHandler();
    }
    
    function closeHandler(cal) {
       cal.hide();                     
      _dynarch_popupCalendar = null;
    }
    
    function showCalendar(id, showsTime, showsOtherMonths) 
    {
      var format = "%d/%m/%Y";
      var el = document.getElementById(id);
      
      if (_dynarch_popupCalendar != null) {
        _dynarch_popupCalendar.hide(); 
      } else {

        var cal = new Calendar(1, null, selected, closeHandler);
       
        if (typeof showsTime == "string") {
               
          cal.showsTime = true;
          cal.time24 = (showsTime == "24");
        }
        if (showsOtherMonths) {
          cal.showsOtherMonths = true;
        }
        _dynarch_popupCalendar = cal; 
        cal.setRange(1900, 2070);
        cal.create();
      }
      _dynarch_popupCalendar.setDateFormat(format); 
      _dynarch_popupCalendar.parseDate(el.value);
      _dynarch_popupCalendar.sel = el; 
      _dynarch_popupCalendar.showAtElement(el, "Br"); 
      return false;
    }
    
    function showListCalendar(id, showsTime, showsOtherMonths) 
    {
      var format = "%d/%m/%Y";
      var el = document.getElementById(id);
      
      if (_dynarch_popupCalendar != null) {
        _dynarch_popupCalendar.hide(); 
      } else {

        var cal = new Calendar(1, null, selected, closeHandler);
       
        if (typeof showsTime == "string") {
               
          cal.showsTime = true;
          cal.time24 = (showsTime == "24");
        }
        if (showsOtherMonths) {
          cal.showsOtherMonths = true;
        }
        _dynarch_popupCalendar = cal; 
        cal.setRange(1900, 2070);
        cal.create();
      }
      _dynarch_popupCalendar.setDateFormat(format); 
      _dynarch_popupCalendar.parseDate(el.value);
      _dynarch_popupCalendar.sel = el; 
      _dynarch_popupCalendar.showAtElement(el, "Br");
      
      optDate = new Option(MA[intLoopOuterIndex],MA[intLoopOuterIndex]);
	  document.frmbsmclientDates.lstSelectedDays.options.add(optDate); 
      return false;
    }
    
    var MINUTE = 60 * 1000;
    var HOUR = 60 * MINUTE;
    var DAY = 24 * HOUR;
    var WEEK = 7 * DAY;

    function isDisabled(date) 
    {
      var today = new Date();
      return (Math.abs(date.getTime() - today.getTime()) / DAY) > 10;
    }

    function flatSelected(cal, date) 
    {
      var el = document.getElementById("preview");
      el.innerHTML = date;
    }

    function showFlatCalendar() 
    {
    
      var parent = document.getElementById("display");
      var cal = new Calendar(0, null, flatSelected);
      cal.weekNumbers = false;
      cal.setDisabledHandler(isDisabled);
      cal.setDateFormat("%A, %B %e");
      cal.create(parent);
      cal.show();
    }
    
//End of Calendar Scripts

function callPopupWindowOne(url)
{
	window.open(url,'mypopup2','toolbars=0,menubars=0,width=800,height=650,left=0,top=20,scrollbars=yes,resizable=yes');
}
function callPopupWindowTwo(url)
{
	window.open(url,'mypopup2','toolbars=0,menubars=0,width=550,height=400,left=300,top=170,scrollbars=no,resizable=no');
}

function callPopupWindowOneSmall(url)
{
	window.open(url,'mypopup2','toolbars=0,menubars=0,width=650,height=120,left=170,top=250,bottom=300, scrollbars=no,resizable=yes');
}


function ActionVaidate(RowCount,CntrlName,strTitle,strPageName,strParam)
{

    var TotCount = parseInt(RowCount);
    var strActionValue = "";
    
    for(var x=1 ; x<=TotCount ; x++)
    {
        var strCtrl = CntrlName + x.toString();
        var ele = document.getElementById(strCtrl);
        if(ele.checked && !ele.disabled)
        {
            if(strActionValue=="")
            {
                strActionValue = ele.value;
            }
            else
            {
                strActionValue = strActionValue + "," + ele.value;                    
            }
        }
    }
    
    if(strActionValue!="")
    {
        callPopupWindowOne(strPageName + '.aspx?ActionValue=' + strActionValue + '&Parameter1=' + strParam)
    }    
    else
    {
        alert(strTitle + ".");
        return false;
    }
    return false;
}


function DateValidation(objFromDate,objToDate,strMessage)
{
    var intFromMonth = objFromDate;
    var intToMonth = objToDate;
    
    intFromMonth = intFromMonth.substr(3,2);
    intToMonth = intToMonth.substr(3,2);
                
    if( intFromMonth <= "09" )
        intFromMonth = parseInt(intFromMonth.substr(1,1)) - 1;
    else
        intFromMonth = parseInt(intFromMonth) - 1;

    if( intToMonth <= "09" )
        intToMonth = parseInt(intToMonth.substr(1,1)) - 1;
    else
        intToMonth = parseInt(intToMonth) - 1;

    var SelDate = new Date( objFromDate.substr(6,4), intFromMonth , objFromDate.substr(0,2) );
    var CurrDate  = new Date( objToDate.substr(6,4), intToMonth , objToDate.substr(0,2) );							

    if(CurrDate < SelDate)
    {
        alert("Please select a valid date. " + strMessage);
        return false;
    }
    else
    {
        return true;
    }
}


function PopupWindowA(strUrl)
{
		if (screen.width == "800")
		{
			winStatus = window.open(strUrl,'Task','menubar=no,toolbar=no,location=no,hotkeys=no,status=no,copyhistory=no,width=720,height=671,scrollbars=yes,resizable=no,top=19,left=0')
		}
		else
		{
			winStatus = window.open(strUrl,'Task','menubar=no,toolbar=no,location=no,hotkeys=no,status=no,copyhistory=no,width=950,height=671,scrollbars=yes,resizable=no,top=19,left=0')
		}
        if(winStatus!=null && winStatus)
        {
            winStatus.focus();
        }			
}