﻿//Function to check the datachange on page
var IsDataChanged = false;
function CheckDataChanged()
{
    
    if(IsDataChanged == true)
    {        
        var retVal = confirm("Data on the page has changed,  Are you sure want to leave page \r\nwithout saving? Click CANCEL to return for save.");
        
        return retVal;
    }
    else
    {
        return false;
    }
}
function CheckDataChangedWithoutMsg()
{
    
    if(IsDataChanged == true)
    {        
        return true;
    }
    else
    {
        return false;
    }
}
//Function to set the datachange on page
function SetDataChanged()
{        
    IsDataChanged = true;
}

//Function to reset the datachange on page
function ResetDataChanged()
{
    IsDataChanged = false;
}

var ISIE;
if (navigator.appName=="Microsoft Internet Explorer")
    ISIE = true;
else
    ISIE = false;


function openPopup(title,txtid)
{
    var url ="../Listofvalues.aspx?txtid=" + txtid;				
	GB_showCenter(title,url,400,600);
	return false;
}

function SaveClick()
{
    Page_ClientValidate('Save');
    if(Page_IsValid)
    {
        var blnAns = confirm('Click OK to Add');
        if(blnAns)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    else
    {
        return false;
    }
}

function DeleteClick()
{
    var blnAns = confirm('This Record will be Deleted.\r\nAre You Sure to Delete?');
    if(blnAns)
    {
        return true;
    }
    else
    {
       return false;
    }
}

function AdminDeleteClick()
{
    var blnAns = confirm('All Activity will be Deleted.\r\nAre You Sure to Delete?');
    if(blnAns)
    {
        return true;
    }
    else
    {
        alert('Cancel to Delete');
        return false;
    }
}


function ValidateNumericWithLength(strElement,Length)
{
        
if(strElement.value !='' )
{
    if(strElement.value.length == Length)
    {
      return false;
    }                        
}                       
else
{                      
   if(event.keyCode >= 48 && event.keyCode <= 57)
    {
      return true;
    }
    else
    {
     return false;
    }
}             
}               
                
                function ValidateNumericOnly(strElement)
                     {
                    
                       if(event.keyCode >= 48 && event.keyCode <= 57)
                        {
                          return true;
                        }
                        else
                        {
                         return false;
                        }
                     }
                     
             function ValidateNumericPlusCommaOnly(strElement)
                     {
                      
                       if((event.keyCode >= 48 && event.keyCode <= 57)){
                            return true;
                        }                        
                        if(event.keyCode == 44){
                            return true;
                        }
                        return false;
                        
                     }
                
        
     
        



// for Budding Page


        var values=new Array();
        var lastIndex;
        var txtValues = new Array();
        var ctlIds= new Array();
        var divIds =new Array();
        function validateInput(ctlid,row,col)
        {
       
            var startVal=txtValues[0];    
            var EndVal=txtValues[1];
            var ctlval =document.getElementById(ctlid).value;
            var index= (2 * row ) + col;
            //check for first and last cell
            if(row != 0 && row != values.length-1 && ctlval != "")
            {
                         var prevValue= document.getElementById(ctlIds[row-1]).value;     
                         var currValue= document.getElementById(ctlIds[row]).value;
                         if( (parseInt(currValue)>= parseInt(startVal)) && (parseInt(currValue)<= parseInt(EndVal)) )
                         {
                             if(col =="2")
                             {
                                if(parseInt(currValue) >= parseInt(EndVal) )
                                {
                                    document.getElementById(ctlid).value="";
                                    alert("Enter Valid Row Range Value");
                                }
                             }
                             if(col == "1")
                             {
                                if( (parseInt(currValue) == parseInt(prevValue) + 1) || (parseInt(currValue) == parseInt(prevValue)))
                                {
                                    
                                }
                                else
                                {
                                    document.getElementById(ctlid).value="";
                                    alert("Enter Valid Row Range Value");
                                }
                             }
                        }
                        else
                        {
                             document.getElementById(ctlid).value="";
                             alert('Value Must Be (Greater than or Equal to) Start Row & (Less than or Equal to) End Row');                             
                        }
            }//end of if(index != 0 && index != values.length-1)
        }//end of validateInput()
        
        function validateInputOnButtonClick()
        {
            var i=0;
           
            for(i=0;i< ctlIds.length ;i++)
            {
                if(i != 0 && i != ctlIds.length-1 )
                {
                    var prevVal = document.getElementById(ctlIds[i-1]).value;
                    var currVal = document.getElementById(ctlIds[i]).value;
                    if((parseInt(currVal) < parseInt(prevVal)))
                    {
                      alert("Please Check your Row Range Sequence");
                      return false;
                    }
                    if(i%2==0)
                    {
                        if(!( (parseInt(currVal) == parseInt(prevVal)) || (parseInt(currVal) ==  parseInt(prevVal) +1) ))
                        {
                             alert("Please Check your Row Range Sequence");
                             return false;
                        }
                    }
                }
            }return true;
        }
       
     // ***************** Transplantation Script *****************
     
     function validateTransInput(index,ctlNo)
        {
        
            var ctlid1;
            var ctlid2;
            var divid1;
            var divid2;
            var startVal;    
            var EndVal;
            
            if(ctlNo=="1")
            {
                divid1 = divIds[index];
                divid2 = divIds[index + 1];
                startVal= document.getElementById(divid1).innerHTML;
                EndVal= document.getElementById(divid2).innerHTML; 
            }
            else
            {
                divid1 = divIds[index-1];
                divid2 = divIds[index];
                startVal= document.getElementById(divid1).innerHTML;
                EndVal= document.getElementById(divid2).innerHTML; 
            }           
                        
            if(ctlNo=="1")
            
            {ctlid1=ctlIds[index];ctlid2=ctlIds[index+1]; }
            else{ctlid1=ctlIds[index-1];ctlid2=ctlIds[index];}
            
            //alert(ctlid1 +' ' + ctlid2  );
            
            var ctlval1 =document.getElementById(ctlid1).value;
            var ctlval2 =document.getElementById(ctlid2).value;
            
                        
            if((ctlval1 !="" && ctlNo==1) || (ctlval2 !="" && ctlNo==2))
            {
                
               
                var bol=false;
                
                if(ctlNo=="1")
                {
                    if((parseInt(ctlval1) >= parseInt(startVal)) && (parseInt(ctlval1) <= parseInt(EndVal)))
                    {bol=true;}else{bol=false;}
                }
                else if(ctlNo=="2")
                {
                    if((parseInt(ctlval2) >= parseInt(startVal)) && (parseInt(ctlval2) <= parseInt(EndVal)) && (parseInt(ctlval1) <= parseInt(ctlval2)))
                    {bol=true;}else {bol=false;}                 
                }
                
                if(bol)
                {
                    if(ctlNo=="1")
                    {
                        //Cheack Previous index Value must be greater than current Index Value.
//                        if(parseInt(index) != 0)
//                        {                            
//                            if(parseInt(values[parseInt(index)-1])  >= parseInt(ctlval1))
//                            {
//                                alert('Enter greater value than Previous End Row Value');
//                                values[index]=0;
//                                document.getElementById(ctlid1).value="";
//                                document.getElementById(ctlid1).focus();
//                                return false;                                
//                            }   
//                            else
//                            values[index]=document.getElementById(ctlid1).value;
//                        }    
                        values[index]=document.getElementById(ctlid1).value;                
                    }
                    else if(ctlNo=="2")
                    {
                        
                        values[index]=document.getElementById(ctlid2).value;
                    }
                }
                else
                {
                    if(ctlNo==1)
                    {
                        document.getElementById(ctlid1).value="";
                        document.getElementById(ctlid1).focus();
                    }
                    else
                    {
                        document.getElementById(ctlid2).value="";
                        document.getElementById(ctlid2).focus();
                    }
                    values[index]=0;
                    
                    alert('Value Must Be (Greater than or Equal to) Start Row & (Less than or Equal to) End Row');
                    return false;
                }
                
          }//end if(ctlval !="")
     
     }// ***************** End of Transplantation Script ************
     
        
// ************************** End script ***************************

 function CheckMaxLength(e, txt,max) 
       {       
         if (txt.value.length >= max && e.keyCode != 8)
         {
           txt.value = txt.value.slice(0,max);
           return false;
           }
           else
           {
           return true;
           }
       }
       
       function AllowNumericWithLength(strElement,Length)
                     {
                                
                      if(strElement.value !='' )
                        {
                    
                    
                        if(strElement.value.length == Length)
                        {
                          return false;
                        }
                        else
                        {
                        
                         if(event.keyCode >= 48 && event.keyCode <= 57)
                        {
                          return true;
                        }
                        else
                        {
                         return false;
                        }
                        
                        }
                        
                       }
                       
                        else
                       { 
                     
                       if(event.keyCode >= 48 && event.keyCode <= 57)
                        {
                          return true;
                        }
                        else
                        {
                         return false;
                        }
                     }
             
                }
                

var weekList=new Array();

function validateWeekEntry(weekStr)
{
    //don't allow to enter "0"
    if((event.keyCode==48) && (weekStr.value.length == 0))
        return false;
   
   //dont allow to enter "," at start
    if((event.keyCode==44) && (weekStr.value.length == 0))
        return false;
       
    if(ValidateNumericPlusCommaOnly(weekStr))
    {   
        if(event.keyCode==44)
        {
           
            
            var orgStr=weekStr.value; 
            
            orgStr = orgStr.replace(",0",",");
            
            weekList= orgStr.split(",");
            
            //first remove duplicate week from string
            var i=0;
            if(weekList.length-1==0)
            {
                if(parseInt(weekList[0])>52){
                    document.getElementById(weekStr.id).value ="";
                    return false;
                }
            }
            else
            {
               //debugger;
                for(i=0;i<weekList.length;i++)
                {
                    //Get last Value for comparision
                    var compStr=weekList[weekList.length-1];
                    var strToComp = orgStr.substring(0,orgStr.length -compStr.length);
                   
                    var curr =parseInt(weekList[i]);
                     
                    var j=0;
                    for(j=0;j<weekList.length;j++)
                    {
                        
                        var compVal =parseInt(weekList[j]);    
                        if((curr == compVal) && (j!=i))
                        {
                            document.getElementById(weekStr.id).value = orgStr.substring(0, strToComp.length);
                            return false;
                        }
                    }                    
                }
                 document.getElementById(weekStr.id).value = orgStr;
            }
           
                      
       }
       return true;
    }
    else
    {        
        return false;
    }
}

function onBlurWeekComp(weekStr)
{
            //debugger;
            var orgStr=weekStr.value;
            // Remove "," from last position
            var temp =orgStr.substring(orgStr.length-1);
            
             if(temp == ",")
             {
                orgStr = orgStr.substring(0,orgStr.length-1);
             }
             // end of 
             
            orgStr = orgStr.replace(",0,",",");
            orgStr = orgStr.replace(",0",",");
            
            weekList= orgStr.split(",");
            //first remove duplicate week from string
            var i=0;
            if(weekList.length-1==0)
            {  
                if(parseInt(weekList[0]) > 52){
                    document.getElementById(weekStr.id).value ="";
                    return false;
                }
            }
            else
            {
                var newStr="";
                for(i=0;i<weekList.length;i++)
                {
                    //Get last Value for comparision                    
                    var curr =parseInt(weekList[i]);
                     
                    var j=0;
                    for(j=0;j<weekList.length;j++)
                    {
                        
                        var compVal =parseInt(weekList[j]);    
                        if((curr == compVal) && (j!=i) && (curr != 0) )
                        {
                            weekList[j]=0;                            
                        }                        
                    }                    
                }
                 for(i=0;i<weekList.length;i++)
                {
                    if(weekList[i] != 0)
                    newStr = newStr + weekList[i] +",";
                }
                
                newStr = newStr.substring(0,newStr.length-1);
                orgStr  = newStr ;
                
            }     
            
            document.getElementById(weekStr.id).value = orgStr;
    
}

function onKeyUpWeekValidation(ctl)
{
    var ctlVal=ctl.value;
    ctl.value = ctlVal.replace(",,","");
    return false;
}

function CheckDec(ctl)
{
//    var i = parseFloat(0);
//    i = ctl.value;
//    if (!isNaN(i))
//    {
//        window.event.returnValue=false;
//        //alert("Invalid");
//        
//    }
//    else{
//        //alert("Valid");
//    }
//    /*
    //debugger;
    // Checks that an input string is a decimal number, with an optional +/- sign character. 
//    var isDecimal_re = "^\\d+(\\.\\d\\d)?$"; 
//    var re=new RegExp(isDecimal_re);
//    if(!(re.test(ctl.value)))
//    {
//        window.event.returnValue=false;
//         //return false; 
//    }
//    return true;
//    */

    var result = AllowDecimal(ctl,18,2);
    
    if(result==true)
    {
        
    }
    else
    {
        alert('invalid');
    }

}


function isDecimal(ctl,max,decPoint)
{
    
    var str = "^\\d{0," + max + "}(\\.\\d{0," + decPoint + "})?$";
    var re=new RegExp(str);
    if(!(re.test(ctl.value)))
    {   
        //negative + positive value validation
        //return /^[-+]?\d{3,5}(\.\d{1,3})?$/.test(str);        
        ctl.value="";
        //alert("not match");
    }    
}
function isNumeric(ctl)
{
   
   var str="^[-+]?\\d{0,10}$";
   var re=new RegExp(str);
    if(!(re.test(ctl.value)))
    {   
        //negative + positive value validation
        //return /^[-+]?\d{3,5}$/.test(str);
        ctl.value="";
        return false;
        //alert("not match");
    }    
    return true;
}

function isZerotoNine(ctl)
{
    var str="^[-+]?\\d{0,10}$";
    var re=new RegExp(str);
    if(!(re.test(ctl.value)))
    {
        ctl.value = '';
        return false;      
    }
    
    var vl = ctl.value;
    if(vl < 0 && vl > 999999)
    {
        ctl.value = '';
        return false;
    }
    return true;
}
 // this is used to Enter only numeric values in the text box 
    //Ex. onkeypress="return isNumberKey(this,3,event)"
    function isNumberKey(ctl,evt)
    {
        if(evt)
        {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;
         return true;
         }
    } 
    
    function isFloatKey(ctl,evt)
    {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode == 46)
            return true;
         
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;
         return true;
    } 
    
    
    function isFloat(ctl)
    {
         var str = "^\\d{0,18}(\\.\\d{0,2})?$";
        var re=new RegExp(str);
        if(!(re.test(ctl.value)))
        {
            ctl.value = '';
            return false;      
        }
        
        var vl = ctl.value;
        if(vl < 0 && vl > 99999)
        {
            ctl.value = '';
            return false;
        }
        return true;
    }
 
    
      

 
function CheckMaxLength(e, txt,max) 
{       
    if (txt.value.length >= max && e.keyCode != 8)
    {
        txt.value = txt.value.slice(0,max);
        return false;
    }
    else
    {
        return true;
    }
} 
// onkeyup="return CheckMaxLength(onkeyup,this,500);" onblur="return CheckMaxLength(onblur,this,500)" 



// Dont Allow Paste In textbox while text box is Readonly
function doReadOnly(control)
{
   event.returnValue = false;
}


        function AllowDecimal(strElement,Length,DigitAfterDecimal,event)
        {        
          var DigitBeforeDecimal = parseInt(Length) - parseInt(DigitAfterDecimal) ;
                
                 // First element cannot be '.'
                        if(strElement.value  =='' )
                        {
                             if(event.keyCode >= 48 && event.keyCode <= 57)
                             {
                               window.event.returnValue = true;
                               return true;
                             }
                             else
                             {
                             window.event.returnValue = false;
                             return false;
                             }
                        }
                               
                    else
                    {
                      //if total length of text box equals length parameter + 1
                        if(strElement.value.length == Length + 1)
                        {
                         window.event.returnValue = false;
                         return false;
                        }
                         else
                         {
                          // if '.' is pressed
                             if(event.keyCode == 46)
                              {
                               // if there is already one '.'
                               if(strElement.value.indexOf('.') >= 0)
                                {
                                 window.event.returnValue = false;
                                 return false;
                                }
                                else
                                {
                                    window.event.returnValue = true;
                                    return true;
                                }
                               } 
                             
                             
                            if(event.keyCode >= 48 && event.keyCode <= 57)
                              {
                                //if  '.' is already entered..
                                  if(strElement.value.indexOf('.') >= 0)
                                   {
                                     //if digits after '.' are equal to  DigitAfterDecimal parameter...
                                       if(strElement.value.length == (strElement.value.indexOf('.')+ 1 + DigitAfterDecimal))
                                        {
                                          window.event.returnValue = false;
                                          return false;
                                        }
                                        else
                                        {
                                        window.event.returnValue = true;
                                        return true;
                                        }
                                   
                                   }
                                 //if textbox contais digits and not '.', and length of digits has reached to DigitBeforeDecimal parameter
                                 if(strElement.value.length == DigitBeforeDecimal && strElement.value.indexOf('.') < 0 )
                                   {
                                    window.event.returnValue = false;
                                    return false;
                                   } 
                                   else
                                   {
                                  window.event.returnValue = true;
                                  return true;
                                  }
                               }
                              else
                              {
                               window.event.returnValue = false;
                               return false;
                              }
                         }
                   }
        }
        


function dollarAmount(form, field) 
{ // idea by Vinod Satapara   

    Num = "" + eval("document." + form + "." + field + ".value");
    dec = Num.indexOf(".");
    end = ((dec > -1) ? "" + Num.substring(dec,Num.length) : ".00");
    if(Num!='')
    {      
    Num = "" + parseFloat(Num.replace('$','').replace(',',''));

    var temp1 = "";
    var temp2 = "";


        if (end.length == 2) end += "0";
        if (end.length == 1) end += "00";
        if (end == "") end += ".00";

        var count = 0;
        for (var k = Num.length-1; k >= 0; k--) {
        var oneChar = Num.charAt(k);
        if (count == 3) {
        temp1 += ",";
        temp1 += oneChar;
        count = 1;
        continue;
    }
    else 
    {
        temp1 += oneChar;
        count ++;
    }
    }
    for (var k = temp1.length-1; k >= 0; k--) 
    {
        var oneChar = temp1.charAt(k);
        temp2 += oneChar;
    }
    temp2 = "$" + temp2 + end;
    eval("document." + form + "." + field + ".value = '" + temp2 + "';");

     } 
} 

function MaskDate(e,ctrlID)
{
    if(e.keyCode < 48 || e.keyCode > 57 )return false;
    var ctrlValue=document.getElementById(ctrlID).value;
    if(ctrlValue.length == 2 || ctrlValue.length == 5)
    {
        ctrlValue= ctrlValue + "/";
        document.getElementById(ctrlID).value = ctrlValue;        
    }    
        
    return true;
}

// JavaScript Document from solution corp
var min=11;
var max=20;
function increaseFontSize() {
   var p = document.getElementsByTagName('td');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=max) {
         s += 2;
      }
      p[i].style.fontSize = s+"px"
   }
}
function decreaseFontSize() {
   var p = document.getElementsByTagName('td');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=min) {
         s -= 2;
      }
      p[i].style.fontSize = s+"px"
   } 
}


function ValidateEmailAddress(txtNewsEmailId)
{
	var txtNewsEmail=document.getElementById(txtNewsEmailId);
	var validationExpression=/^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/;
	var emailId=txtNewsEmail.value;
	var result=emailId.match(validationExpression);
	if(result==null)
	{
		alert("- Please enter a valid email address");
		return false;
	}
	else
		return true;
 }  