﻿// JScript File

var n;
var p;
var p1;

function ValidatePhone()
{
    p=p1.value
    if(p.length==3)
    {
	    //d10=p.indexOf('(')
	    pp=p;
	    d5=p.indexOf('-')
	    if(d5==-1)
	    {
		    pp=pp+"-";
	    }
	    //pp="("+pp+")";
	    p1.value="";
	    p1.value=pp;
    }
    if(p.length>3)
    {
	    d1=0;
	    d2=p.indexOf('-')
	    if (d2==-1)
	    {
		    l30=p.length;
		    p30=p.substring(0,4);
		    //alert(p30);
		    p30=p30+")"
		    p31=p.substring(4,l30);
		    pp=p30+p31;
		    //alert(p31);
		    p1.value="";
		    p1.value=pp;
	    }
	}
    if(p.length>5)
    {
	    p11=p.substring(d1+1,d2);
	    if(p11.length>3)
	    {
	        p12=p11;
	        l12=p12.length;
	        l15=p.length
	        //l12=l12-3
	        p13=p11.substring(0,3);
	        p14=p11.substring(3,l12);
	        p15=p.substring(d2+1,l15);
	        p1.value="";
	        pp="("+p13+")"+p14+p15;
	        p1.value=pp;
	        //obj1.value="";
	        //obj1.value=pp;
	    }
	    l16=p.length;
	    p16=p.substring(d2+1,l16);
	    l17=p16.length;
	    if(l17>3&&p16.indexOf('-')==-1)
	    {
		    p17=p.substring(d2+1,d2+4);
		    p18=p.substring(d2+4,l16);
		    p19=p.substring(0,d2+1);
		    //alert(p19);
	        pp=p19+p17+"-"+p18;
	        p1.value="";
	        p1.value=pp;
	        //obj1.value="";
	        //obj1.value=pp;
	    }
    }
    //}
    setTimeout("ValidatePhone()",0)
}

function getIt(m)
{
    n=m.name;
    p1=m
    ValidatePhone()
}

function testphone(obj1)
{
    p=obj1.value
    //alert(p)
    p=p.replace("(","")
    p=p.replace(")","")
    p=p.replace("-","")
    p=p.replace("-","")
    //alert(isNaN(p))
    if (isNaN(p)==true)
    {
        alert("Check phone");
        return false;
    }
}


function phoneValidate(txtControl,checkEmpty,errorMgs1,errorMsg2)
{
    if (checkEmpty == "true")
    {
         if(txtControl.value.length==0)
         {
            alert(errorMgs1);
            txtControl.focus();
            return true;
         }
    }
    var atr=txtControl.value
    atr=atr.replace("-","");
    atr=atr.replace("-","");
    if (atr.length>0)
    {
        var validChars = "0123456789";
        var chars; 
        for (var i = 0; i < atr.length; i++) 
        { 
            chars = atr.charAt(i); 
            if (validChars.indexOf(chars) == -1) 
            {
                alert(errorMsg2);
                txtControl.focus();
                return true;
            }
        }

    }
    return false;                
}

function OnlyNumber(txtControl,checkEmpty,msgNumber,msgEmpty)
{
    if(checkEmpty == "true")
    {
         if(txtControl.value.length==0)
         {
           alert(msgEmpty);
           txtControl.focus();
            return true;
         }
    }
    if (txtControl.value.length>0)
    {
       var str=txtControl.value;	
        var ValidChars = "0123456789";
        var Char; 
        for (i = 0; i < str.length; i++) 
        { 
            Char = str.charAt(i); 
            if (ValidChars.indexOf(Char) == -1) 
            {
                alert(msgNumber);
                txtControl.focus();
                return true;
            }
        }
        return false;           
    }




    return false;
}


function validateNumber(txtControl,checkEmpty,msgNumber,msgEmpty)
{
    if(checkEmpty == "true")
    {
         if(txtControl.value.length==0)
         {
            alert(msgEmpty);
            txtControl.focus();
            return true;
         }
    }
    if (txtControl.value.length>0)
    {
        //var str=/^\d+$|^\d+\.\d{2}$/ ;
        if (isNaN(txtControl.value))
        {
            alert(msgNumber);
            txtControl.focus();
	        return true;
        }
        return false;
    }




    return false;
}

function validateCharacter(txtControl,checkEmpty,msgNumber,msgEmpty)
{
 
    if(checkEmpty == "true")
    {
         if(txtControl.value.length==0)
         {
            alert(msgEmpty);
            txtControl.focus();
            return true;
         }
    }
    if (txtControl.value.length>0)
    {
       // alert(txtControl.value);
        txtControl.value = Trim(txtControl.value);
        var str= /^[a-zA-Z\s'"]+$/;  
        if (str.test(txtControl.value))
            return false;
        else
        {
            alert(msgNumber);
	        txtControl.focus();
	        return true;
        }
    }
   return false;     
}

function IsSelected(txtControl,errorMsg)
{
    if (txtControl.value==0)    
    {
        alert(errorMsg);
        txtControl.focus();
        return true;
    }
    else
    {
        return false;
    }
}

function CheckStringSpecial(txtControl,strEmpty,errorMsg1,errorMsg2)
{
    if (strEmpty=="true")
    {
        if (txtControl.value.length==0)
        {
            alert(errorMsg1);
		    txtControl.focus();
		    return true;
        }
    }
    else
    {  
        var str=/^[a-zA-Z0-9_\s-.,"'@#$!&*%]*$/;
	    if (str.test(txtControl.value))
		    return false;
	    else
	    {
		    alert(errorMsg2);
		    txtControl.focus();
		    return true;
        }
    }
}

function IsEmpty(txtControl,msgEmpty)
{
	txtControl.value = Trim(txtControl.value);
	if(txtControl.value.length == 0)
	{
		alert(msgEmpty);
		txtControl.focus();
		return true;
	}
	else
	{
		return false;
	}
}


function ValidateMaxLength(txtControl,allowedLength)
{
	var controlValue = Trim(txtControl.value);
	
	if(controlValue.length > allowedLength)
	{
		alert("Character length exceeds the maximum allowed length of " + allowedLength + " characters.");
		txtControl.focus();
		return false;
	}
	else
	{
		return true;
	}
}

function CompareToday(fromdate,errorMessage)
{
    if(fromdate=="")
    {
        alert(errorMessage);
        return false;
    }
    else
    {
        var today=new Date()
        var todayday=today.getDate()
        var todaymonth=today.getMonth() + 1
        var todayyear=today.getFullYear()
                
        var dtStartDate=fromdate.value	    
        var fromdateday=dtStartDate.substring(0,2)
        var fromdatemonth=dtStartDate.substring(3,dtStartDate.length-5)        
        var fromdateyear=dtStartDate.substring(6,dtStartDate.length)      
              
        if(todayyear>fromdateyear)
        {
            alert(errorMessage)
            fromdate.focus()
            return false;
        }
        else if(todayyear==fromdateyear)
        {

            if(todaymonth>fromdatemonth)
            {
                alert(errorMessage)
                fromdate.focus()
                return false;
            }
            else if(todaymonth == fromdatemonth)
            {

                if(todayday > fromdateday)
                {
                    alert(errorMessage)
                    fromdate.focus()
                    return false;
                }
            }            
         }
    }
    return true;
}

// to compare two dates.
// it will compare firstDate with  secondDate and if firstDate is greater than secondDate date then it will
// give error message.
function CompareDates(firstDate,secondDate,errorMessage)
{
	if(firstDate == "" && secondDate != "")
	{
			alert(errorMessage);
			return false;
	}
	else if(firstDate != "" && secondDate != "")
	{
//        var startDate = FormatDate(firstDate,'dd/mm/yyyy');
//        var endDate = FormatDate(secondDate,'dd/mm/yyyy');

//	    var dtStartDate = new Date();
//	    var dtEndDate = new Date();
//				
//	    dtStartDate=firstDate.value;                   //code written by Gunjan & make comment Original code
//        dtEndDate=secondDate.value;
        
        var dtStartDate=firstDate.value	
        var dtEndDate=secondDate.value	
         
        var year1=dtStartDate.substring(6,dtStartDate.length)
        var month1=dtStartDate.substring(3,dtStartDate.length-5)
        var day1=dtStartDate.substring(0,2) 
        
        var year2=dtEndDate.substring(6,dtEndDate.length)
        var month2=dtEndDate.substring(3,dtStartDate.length-5)
        var day2=dtEndDate.substring(0,2)
        
        
         if(year1>year2)
        {
            alert(errorMessage)
            firstDate.focus()
            return false;
        }
        else if(year1==year2)
        {

            if(month1>month2)
            {
                alert(errorMessage)
                firstDate.focus()
                return false;
            }
            else if(month1 == month2)
            {

                if(day1 > day2)
                {
                    alert(errorMessage)
                    firstDate.focus()
                    return false;
                }

                if(day1==day2)
                {
                    alert(errorMessage)
                    firstDate.focus()
                    return false;
                }
            }            
         }
  
		
//		dtStartDate.setFullYear(startDate.substring(6,startDate.length),
//								startDate.substring(3,startDate.length-5),
//								startDate.substring(0,2));

//		dtEndDate.setFullYear(endDate.substring(6,endDate.length),
//								endDate.substring(3,endDate.length-5),
//								endDate.substring(0,2));            
//		if( dtStartDate > dtEndDate )
//		{
//			alert(errorMessage);
//			return false;
//		}
	}
	return true;
}

/********* Compare Date Range **************/
function CompareDateRange(firstDate,secondDate)
{
	if(firstDate == "" && secondDate != "")
	{
			return false;
	}
	else if(firstDate != "" && secondDate != "")
	{
		var startDate = FormatDate(firstDate,'dd/mm/yyyy');
		var endDate = FormatDate(secondDate,'dd/mm/yyyy');
		var dtStartDate = new Date();
		var dtEndDate = new Date();
		
		dtStartDate.setFullYear(startDate.substring(6,startDate.length),
								startDate.substring(3,startDate.length-5),
								startDate.substring(0,2));

		dtEndDate.setFullYear(endDate.substring(6,endDate.length),
								endDate.substring(3,endDate.length-5),
								endDate.substring(0,2));
		if( dtStartDate > dtEndDate)
		{
			return false;
		}
	}
	return true;
}

/********* Round Off functionality with Numeric check **************/
function RoundOff(txtControl)
{

    var txtValue = Trim(txtControl.value);
    var retValue ;
    var tempStr='';
    var checkValue=8;

    if(NumberValidate(txtValue))
	{
	    alert("Please enter a numeric value");
		txtControl.value="";
		txtControl.focus();
	}
	else if(txtValue!="")
	{
	 		tempStr = Math.round(txtValue).toString();
	 		var Index = tempStr.indexOf("-");
			if (Index != -1)
				checkValue += 1;
			if (tempStr.length > checkValue)
			{
				alert("Please enter a number of max 8 digit length") ;
				txtControl.value='';
				txtControl.focus();
			}
			else
			{
				txtControl.value=Math.round(txtValue);
			}
	}
}


/******** Checking for both numeric and integer added by cyril******************/
function IsInteger(txtControl)
{
	txtControl.value = Trim(txtControl.value);
	
	if(NumberValidate(txtControl.value))
	{
		alert("Please enter a numeric value");
		txtControl.value="";
		txtControl.focus();
		return false;
	}
	else if ( txtControl.value.indexOf(".") >= 0 )
	{
		alert("Please enter a number without decimal point.");
		txtControl.value="";
		txtControl.focus();
		return false;
	}
	else if(txtControl.value > 2147483647)
	{
		alert("Please enter a number less than 2147483648.");
		txtControl.value="";
		txtControl.focus();
		return false;
	}
	return true;
	
}

function IsSmallInteger(txtControl)
{
	txtControl.value = Trim(txtControl.value);
	
	if(NumberValidate(txtControl.value))
	{
		alert("Please enter a numeric value");
		txtControl.value="";
		txtControl.focus();
		return false;
	}
	else if ( txtControl.value.indexOf(".") >= 0 )
	{
		alert("Please enter a number without decimal point");
		txtControl.value="";
		txtControl.focus();
		return false;
	}
	else if(txtControl.value > 32767)
	{
		alert("The input field has exceeded it's maximum limit");
		txtControl.value="";
		txtControl.focus();
		return false;
	}
	return true;
	
}

/******** Checking for email validation ******************/
var testresults
function CheckEmail(txtControl)
{
	
	    var str=txtControl.value
	    var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	    if (filter.test(str))
		    testresults=true
	    else
	    {
		    alert("Please enter a valid email address.");
		    txtControl.focus();
		    testresults=false;
	    }

	return (testresults)
}

function RoundToCoordinates(txtValue)
{	
	return RoundToInput(txtValue,5,6);
}

/******** Checking for Date validation ****************** By Gunjan***********/
function CheckDate(txtControl)
{
	var str=txtControl.value
	var filter = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
	if (filter.test(str))
		testresults=true
	else
	{
		alert("Please Enter a Valid Date .");
		txtControl.focus();
		testresults=false;
	}
	return (testresults)
}

/******** Checking for amount,percentage validation added by cyril******************/
function RoundToAmount(txtValue) 
{
	return RoundToInput(txtValue,9,0);
}
function RoundToPercentage(txtValue)
{
	return RoundToInput(txtValue,7,2);
}
function RoundToInput(txtValue,leftValues,decimals) 
{
	
	var original_number = Trim(txtValue.value);
	
	if (original_number == '')
	{
		txtValue.value = '';
		return true;
	}
	
	var retValue = true;
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    
    var tempStr = '';
    var DecimalPart = '';
    
    if (! NumberValidate(result3))
    {
 		tempStr = result3.toString();
 		
 		if (tempStr.indexOf("e") != -1)
			retValue = false;
		
		var Index = tempStr.indexOf(".");
		
		if (Index != -1)
		{
			DecimalPart = tempStr.substring(Index,tempStr.length);
			tempStr = tempStr.substring(0,Index);
		}
			
		if (! NumberValidate(tempStr))
		{
			Index = tempStr.indexOf("-");
		
			if (Index != -1)
				leftValues += 1;
				
			if (tempStr.length > leftValues)
				retValue = false;
				
		}
		if(DecimalPart.length > 0)
			tempStr += DecimalPart;
	}
	else
		retValue = false;
		
	if (!retValue)
	{
		alert("Please enter a number in " + leftValues + "," + decimals + " decimal format.");
		txtValue.value = "";
		txtValue.focus();
		return false;
	}
	else
	{
		txtValue.value = tempStr;
		return true;
	}
		
}


/******** Trim Function Starts Here ******************/
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;
	}
} 
/******** Trim Function Ends Here ******************/

/******** LTrim Function Starts Here ******************/

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;
} 
/******** LTrim Function Here ******************/





/******** RTrim Function Starts Here ******************/
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;
} 
/******** RTrim Function Ends Here ******************/

function OpenPopup(url,windowName)
{
window.open(url, windowName, 'toolbar=0,scrollbars=yes, titlebar=0, location=0,statusbar=0, frames=0, menubar=0,resizable=0,width=280, height=115, left = 420, top = 350');
}


/**********AJAX FUNCTION **********/

function GetXmlHttpObject()
{
    var xmlHttp;
    try
    {  
        // Firefox, Opera 8.0+, Safari  
        xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {  
        try
        {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}
