﻿// JScript File
    var result="";
    var i=0;
    //************ Validates website URL **********************
    function validateWebsite(control,name)
    {
        if(strip_lspaces(control.value).length == 0)
            result = result + '\n'+ getI() + 'Enter ' + name ;
        else if(!/^(http:\/\/|https:\/\/|ftp:\/\/|ftps:\/\/)([a-z]{1,}[\w-.]{0,}).([a-z]{2,6})(\/{1}[\w_]{1}[\/\w-&?=_%]{0,}(.{1}[\/\w-&?=_%]{0,})*)*$/.test(strip_lspaces(control.value)))
            result = result + '\n'+ getI() + 'Invalid ' + name ;
        
    }
    
    //************ An integer validation *************
    function isempty_number(val,name)
	{
	    val=strip_lspaces(val.value);
	    if(val.length==0)
	        result = result + '\n'+ getI() + 'Enter ' + name ;
	    else if (isNaN(val) || val.indexOf('.')!=-1)
	        result = result + '\n'+ getI() + 'Invalid ' + name;
	}
    
    //************ Error count *************
    function getI()
    {
        if (++i<10)
            return '  (' + i + ') ';
        else
            return '(' + i + ') ';    
    }

    //*********** textbox has value or not ***********
    function isempty(control, name)
    {
        control = strip_lspaces(control.value);
	    if (control.length==0)
	    {
		    result = result + '\n'+ getI() + 'Enter ' + name ;
	    }
    }

    //*********** Trims left spaces of a control's value ***********
    function strip_lspaces(element){
        if (element != ''){
	        while('' + element.charAt(0) == ' '){
	        element = element.substring(1,element.length);
	        }
        }
        return element;
    }
    
    //************* Validates Zip Code (US-codes only) **************
    function validate_zip(zip)
    {
        zip= strip_lspaces(zip.value);
	    if(zip.length==0)
		    result=result + "\n" + getI() +"Enter Zip Code";
	    else if((zip.length!=0) && (zip.length < 5 || zip.length >5))
		    result=result + "\n" + getI() +"Invalid Zip Code";
	    else if(isNaN(zip)) 
		    result=result + "\n" + getI() + "Invalid Zip Code";
    }
			
	//************* Validates Phone number (US format) *****************
    function validate_phone(phone, name)
	{
		phone = strip_lspaces(phone.value);
		if (phone.length!=0)
			var sep = getseparator(phone);
		
		if(phone.length==0)
			result=result + "\n" + getI() +"Enter " + name;
		else if((phone.length!=0) && (phone.length < 12 || phone.length >12))
			result=result + "\n" + getI() +"Invalid " + name ;
		else if((isNaN(phone.substring(0,3))) || (isNaN(phone.substring(4,7))) || (isNaN(phone.substring(8,12)))) 
			result=result + "\n" + getI() + "Invalid " + name;				
		else if ((sep !="") && (phone.length!=0))
		{
			if((phone.indexOf(sep)!=3) || (phone.lastIndexOf(sep)!=7))
				result=result + "\n" + getI() + "Invalid " + name ;		
		}
		else if(sep =='') 
			result=result + "\n" + getI() + "Invalid " + name ;
	}
	
	//***************** returns separator in phone column **************
	function getseparator(phone_)
	{
		if(phone_.indexOf('-')!=-1)
			return '-';
		else
			return '';
	}
    
    //****************** Checks whether dropdown is selected or not *************************
    function isddlSelected(ddl,type)
    {
        if(ddl.selectedIndex==0)
            result=result + '\n'+ getI() + 'Select ' + type ;
    }
    
    //***************** Validates emil *************************
    function validate_email(s)
    {				
	    var email=strip_lspaces(s.value);
	    if(email.length==0 )
		    result=result +  '\n' + getI() + 'Enter Email Address';
	    else if ((email.indexOf('@')== -1) || (email.indexOf('.')==-1))
		    result= result + '\n' + getI() +'Invalid Email Address';
	    else if((email.indexOf('@')==0) || (email.indexOf('.')==0))
		    result= result + '\n' + getI() +'Invalid Email Address';
	    else if(((email.indexOf('.')==email.indexOf('@')+1))||(email.indexOf('@')> email.lastIndexOf('.')))
		    result= result + "\n" + getI() +'Invalid Email Address';
	    else if(email.lastIndexOf('.')==email.length-1)
		    result= result + "\n"+ getI() +'Invali Email Address';
        else if (email.substring(email.lastIndexOf('.') + 1,email.length).length > 3)
        {
            result= result + '\n' + getI() +'Invalid Email Address';
        }
	    else
	    {
		    var chararray =new Array('\\','\'','~','!','#',' ','$','%','^','&','*','-','+','<','>','=','(',')','|','"','/',';',':','`',',')
		    if(s=validate_chars(email,chararray))
			    result= result + "\n" + getI() +'Invalid Email Address';
	    }
	    for(count=0,j=0;j<email.length;++j)
	    {
		    if(email.charAt(j)=='@')
			    count++
		    if(count==2) {
		        result= result + "\n" + getI() +'Invalid Email Address';
		        break;
		        }
	    }
    }

    //This function checks whether the mail
    //contains any special characters other then
    //'@' and '.'.
    function validate_chars(field,arraychars)
    {
	    for(p=0;p<field.length;++p)
		    for(j=0;j<arraychars.length;++j)
			    if(field.charAt(p)==arraychars[j])			
				    return arraychars[j];
    }
    
    //******************** Validates date ******************************
    //          Accepts mm-dd-yyyy, mm/dd/yyyy and mm.dd.yyyy formats
    function validate_textdate(edate, name)
	{
		var date1=strip_lspaces(edate.value);
		var mm,dd,yy,sptr;
		sptr=getseparator_date(date1);
		if (sptr != null)
		{       
		    mm=date1.substring(0,date1.indexOf(sptr));
		    dd=date1.substring(date1.indexOf(sptr)+1,date1.lastIndexOf(sptr));
		    yy=date1.substring(date1.lastIndexOf(sptr)+1,date1.length);
		    if(date1.length==0)
		        result= result + '\n'+ getI() + "Invalid " + name;
		    else if(isNaN(mm)||isNaN(dd)||isNaN(yy))
			    result= result + '\n'+ getI() + "Invalid " + name;
		    else if(parseInt(dd)>31)
		    	result= result + '\n'+ getI() + "Invalid " + name;
		    else if(parseInt(mm)>12)
			    result= result + '\n'+ getI() + "Invalid " + name;
		    else if(yy.length==3 || yy.length==1 || yy.length>4 || yy.length==0)
			    result= result + '\n'+ getI() + "Invalid " + name;
		    else if((parseInt(mm)==4 || parseInt(mm)==6 || parseInt(mm)==9 || parseInt(mm)==11) && (parseInt(dd)>30))
		    	result= result + '\n'+ getI() + "Invalid " + name;
		    else if( (parseInt(yy))%4 == 0 && parseInt(dd)>28 && parseInt(mm)==2)
			    result= result + '\n'+ getI() + "Invalid " + name;
			else if(parseInt(yy)%4!=0 && parseInt(dd)>29 && parseInt(mm)==2)
		    	result= result + '\n'+ getI() + "Invalid " + name;
		 }
		else if(sptr == null && date1.length!=0)
		    result= result + '\n'+ getI() +"Enter "+ name+" in MM/DD/YYYY Format";		
	}
    
    function getseparator_date(u_date)
    // returns separator in date column
    {
        
        if((u_date.indexOf('/')!=-1) && (u_date.indexOf('.')!=-1))
            return ;
        else if((u_date.indexOf('.')!=-1) && (u_date.indexOf('-')!=-1))
            return ;
        else if((u_date.indexOf('-')!=-1) && (u_date.indexOf('/')!=-1))
            return ;
        else if(u_date.indexOf('/')!=-1)
            return "/";
        else if(u_date.indexOf('.')!=-1)
            return ".";
        else if(u_date.indexOf('-')!=-1)
            return "-";
    }
    
    //********************************* Validates Time **********************************
    function validateTime(control,name)
    {
        var time = strip_lspaces(control.value);
        if(time.length == 0)
            result = result + '\n' + getI() + 'Enter ' + name;
        else if(!(/^(\d{1,2}):(\d{2})(:(\d{2}))?(\s(AM|am|PM|pm))?$/.test(time)))
            result = result + '\n' + getI() + 'Invalid ' + name;
        else
        {
            var splitTime = time.split(':');
            var hrs = splitTime[0];
            var _min = splitTime[1].split(' ');
            var min = _min[0];
            var am_pm = _min[1].toLowerCase();
            if (hrs == '' || min == '' || am_pm == '')
                result = result + '\n' + getI() + 'Invalid ' + name;
            else if (hrs > 12)
                result = result + '\n' + getI() + 'Invalid Hours in ' + name;
            else if(min >= 60)
                result = result + '\n' + getI() + 'Invalid Minitues in ' + name;
        }
    }
    
    //******************************* Validates GVC *****************************************
    function validateGVC(gvc,hid_gvc)
    {
        gvc     = strip_lspaces(gvc.value);
        hid_gvc = strip_lspaces(hid_gvc.value);
        if (gvc.length==0)
        {
            result = result + '\n'+ getI() + 'Enter Graphic Verification Code';                
        }
        else if(gvc!=hid_gvc)
        {
            result = result + '\n'+ getI() + 'Code Is Case Sensitive. Please Enter the Code Again.';
        }
    }