﻿// JScript File
/**************************************************************************************/
//This is common javascript file used to check all kind of validations, returns etc.
//this file is include in all the masters file.
/**************************************************************************************/
//Common Variables
var Display;
if(navigator.appName.indexOf("Microsoft")>=0)
    Display="block";
else
    Display="";
var submitValid=0;
var prename="ctl00$ContentBody$";
var preid="ctl00_ContentBody_";

//Function to rount the float number.
 function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}
//This fucntion is always return false inspite of browser is used
 function returnFalse()
    {
       if(navigator.appName.indexOf("Microsoft")>=0)
        {event.returnValue=false;}
       else
       {return false;}
    }
function fnChkSubmit()
{   
    if(submitValid==1)
    { submitValid=0;return returnFalse();}
    else
        return true;
}
 function fnDateOne(lsdateOne)
    {
        if (isDate(lsdateOne,"d/M/yyyy")==false && isDate(lsdateOne,"d-M-yyyy")==false && isDate(lsdateOne,"d-MMM-yyyy")==false)
        {
            alert("Invalid Date Selection");
	        return returnFalse;
        }
        else {return true;}
    }
    function fnDateChk(ctrl)
    {
        if (isDate(ctrl.value,"M/d/y")==false && isDate(ctrl.value,"M-d-y")==false && isDate(lsdateOne,"d-MMM-yyyy")==false)
        {
            alert("Invalid Date Selection");
	        return false;
        }
        else {return true;}
    }
function fnDateTwo(lsdateOne,lsdateTwo)
{
	if (isDate(lsdateOne,"d/M/yyyy") && isDate(lsdateTwo,"d/M/yyyy"))
	{return true;}
	else
	{
		alert("Invalid Date Selection");
		return false;
	}
}
function fnChkDate(lsDate1,lsDate2)
{	
	//Return 0 when d1==d2
	// Return 1 when d1>d2
	// Return -1 if d1=0 or d2=0
	
	if (compareDates(lsDate1,"d/M/yyyy",lsDate2,"d/M/yyyy")==1)
	{	
		alert("Date taken should be less than future date");
		return false;
	}
	else
		if (compareDates(lsDate1,"d/M/yyyy",lsDate2,"d/M/yyyy")==-1)
		{	
			alert("Date taken should be in proper format");
			return false;
		}
}
// This is used to check validity of Phone number Format:555-555-5555
function fnSetPhoneFormat(cntrl, lostFocus)
	{
		var baseValue=RemoveExtraChars(cntrl.value);
		var finalValue=""
		if (baseValue=="") 
		{
			if (lostFocus==1)
				cntrl.value="";
			return;
		}
		else
		{
			finalValue="(";
			if (baseValue.length>=3)
			{
				finalValue=finalValue+baseValue.substring(0,3) + ") ";
				if (baseValue.length>=6)
				{
					finalValue=finalValue+baseValue.substring(3,6) + "-";
					if (baseValue.length>=10)
					{
						finalValue=finalValue+baseValue.substring(6,10);
					}	
					else
					{
						finalValue=finalValue+baseValue.substring(6,baseValue.length);
					}
				}	
				else
				{
					finalValue=finalValue+baseValue.substring(3,baseValue.length);
				}
			}	
			else
			{
				finalValue=finalValue+baseValue;
			}
		}
		cntrl.value=finalValue;
	}
// This is used to check validity of Phone number Format:555-555-5555
function fnSetZIPFormat(cntrl, lostFocus)
	{
		var baseValue=RemoveExtraChars(cntrl.value);
		var finalValue=""
		if (baseValue=="") 
		{
			if (lostFocus==1)
				cntrl.value="";
			return;
		}
		else
		{
			finalValue="";
			finalValue=baseValue //.substring(0,5) + "-";
			if (baseValue.length>5)
			{
				finalValue=baseValue.substring(0,5) + "-"+baseValue.substring(5,6);
				if (baseValue.length>=10)
				{
					finalValue=finalValue+baseValue.substring(6,10);
				}	
				else
				{
					finalValue=finalValue+baseValue.substring(6,baseValue.length);
				}
			}	
		}
		cntrl.value=finalValue;
	}
// This is used to check validity of Phone number Format:MM/dd/yyyy
function fnSetDateFormat(cntrl, lostFocus)
	{
		var baseValue=RemoveExtraChars(cntrl.value);
		var finalValue=""
		if (baseValue=="") 
		{
			if (lostFocus==1)
				cntrl.value="";
			return;
		}
		else
		{
			finalValue="";
			finalValue=baseValue //.substring(0,5) + "/";
			if (baseValue.length>2)
			{
				finalValue=baseValue.substring(0,2) + "/"+baseValue.substring(2,2);
				if (baseValue.length>=5)
				{
					finalValue=finalValue+baseValue.substring(5,12);
				}	
				else
				{
					finalValue=finalValue+baseValue.substring(6,baseValue.length);
				}
			}	
		}
		cntrl.value=finalValue;
	}
function fnCheckEmail(ctrl)
{  if(ctrl.value=="") return;
   //if(validateEmail(document.getElementById(preid+"tbxEmail").value,1,1)==false)
   if(validateEmail(ctrl.value,1,1)==false)
   {
   	alert("Invalid email address please check it");
   	//ctrl.focus;
   }
}
function fnCheckStateFacility(ctrl)
{  if(ctrl.value=="") return;
   //if(validateEmail(document.getElementById(preid+"tbxEmail").value,1,1)==false)
   if(ValidateStateFacility(ctrl.value,1,1)==false)
   {
   	alert("Invalid character found");
   	//ctrl.focus;
   }
}

function fnCheckEmailForHTMLInput(ctrl)
{
   if(ctrl.value=="") return; 
   if(validateEmail(document.getElementById("tbxEmail").value,1,1)==false)
   {
   	alert("Invalid email address please check it");
   	//document.getElementById("tbxEmail").focus();
   }
}
//It is used to add commas in amount value.
//It return an amount value with commas format.
function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
function RemoveCharsFromString(value,removeChar,replaceChar)
{ 
    var iCount;
	var returnVal="";
	if(value=="") return returnVal;
	if(value.length==null) return returnVal;
	for (iCount=0;iCount<value.length;iCount++)
	{
		if (value.charAt(iCount)==removeChar)
		    returnVal = returnVal+replaceChar;
		else	
			returnVal = returnVal+value.charAt(iCount);
	}
	return returnVal;
}
function RemoveExtraChars(value)
{
	var iCount;
	var returnVal="";
	for (iCount=0;iCount<value.length;iCount++)
	{
		if (!isNaN(value.charAt(iCount)) && value.charAt(iCount)!=" ")
			returnVal = returnVal+value.charAt(iCount);
	}
	return returnVal;
}

function CheckNumeric(ctrl){
    //alert(ctrl);
    var value=ctrl.value;
	var iCount;
	var returnVal="";
	for (iCount=0;iCount<value.length;iCount++)
	{   
		if (((!isNaN(value.charAt(iCount)) && value.charAt(iCount)!=" ")) || (value.charAt(iCount)=="." && returnVal.indexOf(".")==-1))
			returnVal = returnVal+value.charAt(iCount);
	}
	ctrl.value=returnVal;
}
///This function is use to trim the input value
function Trim(TRIM_VALUE){
            if(TRIM_VALUE.length < 1){
            return"";
            }
            TRIM_VALUE = RTrim(TRIM_VALUE);
            TRIM_VALUE = LTrim(TRIM_VALUE);
            if(TRIM_VALUE==""){
            return "";
            }
            else{
            return TRIM_VALUE;
            }
            } //End Function

            function RTrim(VALUE){
            var w_space = String.fromCharCode(32);
            var v_length = VALUE.length;
            var strTemp = "";
            if(v_length < 0){
            return"";
            }
            var iTemp = v_length -1;

            while(iTemp > -1){
            if(VALUE.charAt(iTemp) == w_space){
            }
            else{
            strTemp = VALUE.substring(0,iTemp +1);
            break;
            }
            iTemp = iTemp-1;

            } //End While
            return strTemp;

            } //End Function

            function LTrim(VALUE){
            var w_space = String.fromCharCode(32);
            if(v_length < 1){
            return"";
            }
            var v_length = VALUE.length;
            var strTemp = "";

            var iTemp = 0;

            while(iTemp < v_length){
            if(VALUE.charAt(iTemp) == w_space){
            }
            else{
            strTemp = VALUE.substring(iTemp,v_length);
            break;
            }
            iTemp = iTemp + 1;
            } //End While
            return strTemp;
            } //End Function
            
    /**************/
    // This function is used to check all the checkboxes provided in the grid by checkboxID.
    /*****************/
    function Chkall(ctrlChk1)
    {   
        var selectChk=document.getElementById(ctrlChk1);
        var chkBox=document.getElementsByName("chk");
        for(var iCount=0;iCount<chkBox.length;iCount++)
            document.getElementsByName("chk")[iCount].checked = selectChk.checked;
     }   
// This is used to Check/Uncheck checkboxes, according to "onclick on CheckAll checkbox".
// All check should have name="chk"
function fncheckAll(ctrl)
{ 
 var length=document.getElementsByName("chk").length;
 if(length>0)
 {
  for(var i=0;i<length;i++)
  {document.getElementsByName("chk")[i].checked=ctrl.checked; }
 }
}

   function fnChangeColor(lcase,ctrl)
   {
        if(lcase=="Over")
        {
            ctrl.style.backgroundColor="#E1E1E1";
        }
        else if(lcase=="Out")
        {
            ctrl.style.backgroundColor="#c8c7c2";
        }
   }
   function fnChangeImg(lcase,ctrl,imgname)
   {
        if(lcase=="Over")
        {
            ctrl.src=appPath+"/Images/MenuImg/"+imgname ;
        }
        else if(lcase=="Out")
        {
            ctrl.src=appPath+"/Images/MenuImg/"+imgname ;
        }
   }
   
function chkForUnchk(ctrl,ctrlchk)
{
    if (ctrl.checked==false)
    {
        document.getElementById(ctrlchk).checked=false;
    }
}

function fnOutOver(ctrl,v)
{
    if(v=="1")
    {
        ctrl.style.backgroundColor="#ffbb00";
        ctrl.style.color="#000000";
        ctrl.childNodes[0].style.color="#000000";
    }
    else
    {
        ctrl.style.backgroundColor="#638EBD";
        ctrl.style.color="#ffffff";
        ctrl.childNodes[0].style.color="#ffffff";
    }
}


/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function CheckisDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

//////////////
// common AJAX code fro all Admin master pages
var ctrl;
var commonRequest;
var commonResponse;
    function xmlError(e) 
		{
		//if there was an error, show the user
			alert(e);
		}
		
	function CommonInitializeRequest()
	{
		try
		{
			commonRequest= new ActiveXObject("Microsoft.XMLHTTP");//Try creating an XMLHTTP Object
		}
		catch(Ex)
		{
			try
			{
				commonRequest = new ActiveXObject("Microsoft.XMLHTTP");//First failure, try again creating an XMLHTTP Object
			}
			catch(Ex)
			{
				commonRequest = null;//Else assign null to request
			}
		}
		if(!commonRequest && typeof XMLHttpRequest != 'undefined')
		{
			commonRequest = new XMLHttpRequest();
		}
	}
	function CommonInitializeRequestObject()
	{   var objRequest;
		try
		{
			objRequest= new ActiveXObject("Microsoft.XMLHTTP");//Try creating an XMLHTTP Object
		}
		catch(Ex)
		{
			try
			{
				objRequest= new ActiveXObject("Microsoft.XMLHTTP");//First failure, try again creating an XMLHTTP Object
			}
			catch(Ex)
			{
				objRequest= null;//Else assign null to request
			}
		}
		if(!objRequest && typeof XMLHttpRequest != 'undefined')
		{
			objRequest = new XMLHttpRequest();
		}
		return objRequest;
	}
	function CommonSendRequest(loc)
	{
	    //ctrl=document.getElementById(id);
	    //var d =new Date();
	    CommonInitializeRequest();//Call InitializeRequest to set request object
	    var dt =new Date();
		var url = sPath+"/Admin/Services/"+loc+"&dt="+dt.getSeconds();//Create the url to send the request to
		//url=sPath+"/Test.aspx";
		//alert(url);
		commonRequest.onreadystatechange = CommonProcessRequestState;//Delegate ProcessRequest to onreadystatechange property so it gets called for every change in readyState value
		commonRequest.open("GET", url, true);//Open a GET request to the URL
		commonRequest.send(null);//Send the request with a null body.
		
	}
	function CommonProcessRequestState()
	{   
		if(commonRequest.readyState == 4)//If the readyState is in the "Ready" Country
		{  
			if(commonRequest.status == 200)//If the returned status code was 200. Everything was OK.
			{
				if(commonRequest.responseText != "")//If responseText is not blank
				{
				 //alert(commonRequest.responseText);
				}
				else
				{
				}
			}
		}
		return true;//return
	}
	     
function CommonSendSearchRequestST(OrderBy,PageIndexNo,PageNo)
{   
    document.getElementById("hdnSortField").value="";
    SendRequestST(OrderBy,PageIndexNo,PageNo,'Asc');
}	
// This is called by paging link
function CommonSendRequestWithPagingWithOrderBy(OrderBy,PageIndexNo,PageNo)
{   document.getElementById("hdnPageIndexNo").value=PageIndexNo;
    document.getElementById("hdnPageNo").value=PageNo;
    if (document.getElementById("hdnSortField")!=null)
	   {if (document.getElementById("hdnSortField").value=="" || document.getElementById("hdnSortField").value=="null")
	        SendRequestST(OrderBy,PageIndexNo,PageNo,'Asc');
	    else    
	        SendRequestST(document.getElementById("hdnSortField").value,PageIndexNo,PageNo,document.getElementById("hdnSortOrder").value);
	   }     
	else
	    SendRequestST(OrderBy,PageIndexNo,PageNo,'Asc');
}
function CommonSendRequestWithPaging1(OrderBy,PageIndexNo,PageNo)
{   document.getElementById("hdnPageIndexNo").value=PageIndexNo;
    document.getElementById("hdnPageNo").value=PageNo;
    if (document.getElementById("hdnSortField")!=null)
	   {if (document.getElementById("hdnSortField").value=="" || document.getElementById("hdnSortField").value=="null")
	        SendRequestST(OrderBy,PageIndexNo,PageNo,'Asc');
	    else    
	        SendRequestST(document.getElementById("hdnSortField").value,PageIndexNo,PageNo,document.getElementById("hdnSortOrder").value);
	   }     
	else
	    SendRequestST(OrderBy,PageIndexNo,PageNo,'Asc');
}
//This is overloading function without OrderBy parameter
function CommonSendRequestWithPaging(PageIndexNo,PageNo)
{   //alert(PageIndexNo);
    document.getElementById("hdnPageIndexNo").value=PageIndexNo;
    document.getElementById("hdnPageNo").value=PageNo;
    if (document.getElementById("hdnSortField")!=null)
	   {if (document.getElementById("hdnSortField").value=="" || document.getElementById("hdnSortField").value=="null")
	        { //SendRequestST(OrderBy,PageIndexNo,PageNo,'Asc');
	          SendRequestST('',PageIndexNo,PageNo,'Asc');
	        }
	    else    
	        SendRequestST(document.getElementById("hdnSortField").value,PageIndexNo,PageNo,document.getElementById("hdnSortOrder").value);
	   }     
	else
	    SendRequestST(OrderBy,PageIndexNo,PageNo,'Asc');
}

// This is called click on heading for sorting
function CommonSendRequestByOrderST(OrderBy)
{   var SortOrder="";
    if (document.getElementById("hdnSortField")!=null)
		{
		    if (document.getElementById("hdnSortField").value==OrderBy)
		        {
		         if(document.getElementById("hdnSortOrder").value=="Desc")
		            SortOrder="Asc";
		         else   
		            SortOrder="Desc";
		         //document.getElementById("hdnSortField").value="";
		        }
		    else
		        {SortOrder="Asc";
		         document.getElementById("hdnSortField").value=OrderBy;
		        }
		}
		else
		    {SortOrder="Asc";
	         document.getElementById("hdnSortField").value=OrderBy;
	        }
    document.getElementById("hdnSortOrder").value=SortOrder;
    SendRequestST(OrderBy,document.getElementById("hdnPageIndexNo").value,document.getElementById("hdnPageNo").value,SortOrder);
}