﻿// JScript File
    var result="";
    var i=0;
    //************ Validates website URL **********************
    function validateWebsite(control,name)
    {
       //if(control.length == 0)
       //     result = result + '\n'+ getI() + 'Enter ' + name ;
       //else 
       if(control.length!=0){
//           if(!/^https?:\/\/([\w-]+\.?)+[\w-]+(\/[\w- ./?%&=]*)?$/.test(strip_lspaces(control).value))
           if(!/^https?:\/\/([\w-]+\.?)+[\w-]+(\/[\w- ./?%&=]*)?$/.test(strip_lspaces(control)))
		   
                result = result + '\n'+ getI() + 'Invalid url for' + name ;
       } 
    }
    //************* An Fckeditor Validation **********
    
    function  validateFCKEditor(FCKInst, name)
	    {
	      if(window.navigator.appName.indexOf('Opera')==-1)
	        {
                var oEditor = FCKeditorAPI.GetInstance(FCKInst);
                
                oEditor.UpdateLinkedField();
            }
            
	        var v_editor = document.getElementById(FCKInst).value;
                if(v_editor =='' || v_editor=='<p>&nbsp;</p>')
                {
                    result = result + '\n'+ getI() + 'Enter ' + name;
                }
         }
    
    //************ An integer validation *************
    function isempty_number(val,name)
	{
	   if(val.length==0)
	        result = result + '\n'+ getI() + 'Invalid data in ' + name + '. Please enter any number from 0 to 9999';
	    else if (parseInt(val) < 0 || isNaN(val) || val.indexOf('.')!=-1  || parseInt(val) > 9999)
	        result = result + '\n'+ getI() + 'Invalid data in ' + name + '. Please enter any number from 0 to 9999';
	}
	
	 //************ An floating validation *************
    function isempty_floatnumber(val,name)
	{
	    if(val.length==0)
	        result = result + '\n'+ getI() + 'Enter ' + name ;
	    else if (isNaN(val))
	        result = result + '\n'+ getI() + 'Invalid ' + name;
	    
	}
	
	 //************ An floating validation & accept null*************
    function isempty_floatnumbernull(val,name)
	{
	     if (isNaN(val))
	        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(controlVal, name)
    {
	    if (controlVal.length==0)
	    {
		    result = result + '\n'+ getI() + 'Enter ' + name ;
	    }
    }

 //*********** image upload or not or not ***********
    function isemptyimg(controlVal, name)
    {
	    if (controlVal.length==0)
	    {
		    result = result + '\n'+ getI() + 'Browse Image to ' + name ;
	    }
	    if(controlVal!="") 
		{
		  valid=ValFileField(controlVal)
		  if(!valid)
		  {		    
		    result = result + '\n'+ getI() + 'Upload image with .gif or .jpg or .png or .bmp or .jpeg extension only' + 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,name)
	{
		//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 " + name ;
		else if(isNaN(zip)) 
			result=result + "\n" + getI() + "Invalid " + name ;
	}
			
	//************* Validates Phone number (US format) *****************
    function validate_phone(phone, name)
	{
		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 ;
        if(ddl==0)
            result=result + '\n'+ getI() + 'Select ' + type ;
    }
    
    //***************** Validates email *************************
    function validate_email(s)
    {				
	    var email=s;
	    if(email.length==0 )
		    result=result +  '\n' + getI() + 'Enter Email Id';
	    else if ((email.indexOf('@')== -1) || (email.indexOf('.')==-1))
		    result= result + '\n' + getI() +'Invalid Email Id';
	    else if((email.indexOf('@')==0) || (email.indexOf('.')==0))
		    result= result + '\n' + getI() +'Invalid Email Id';
	    else if(((email.indexOf('.')==email.indexOf('@')+1))||(email.indexOf('@')> email.lastIndexOf('.')))
		    result= result + "\n" + getI() +'Invalid Email Id';
	    else if(email.lastIndexOf('.')==email.length-1)
		    result= result + "\n"+ getI() +'Invali Email Id';
        else if (email.substring(email.lastIndexOf('.') + 1,email.length).length > 3)
        {
            result= result + '\n' + getI() +'Invalid Email Id';
        }
	    else
	    {
		    var chararray =new Array('\\','\'','~','!','#',' ','$','%','^','&','*','-','+','<','>','=','(',')','|','"','/',';',':','`',',')
		    if(s=validate_chars(email,chararray))
			    result= result + "\n" + getI() +'Invalid Email Id';
	    }
	    for(count=0,j=0;j<email.length;++j)
	    {
		    if(email.charAt(j)=='@')
			    count++
		    if(count==2) {
		        result= result + "\n" + getI() +'Invalid Email Id';
		        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=edate;
		var mm,dd,yy,sptr;
		if (date1.length ==0)
		{
		    result= result + '\n'+ getI() + "Enter " + name;
		}
		else
		{
		    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)>29 && parseInt(mm)==2)
			        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(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(controlVal,name)
    {
        var time = controlVal;
        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;
        }
    }
    
    function displayErrors()
    {
        if (result.length==0 || result=='')
		    return true;
	    else
	    {
		    alert('Please check the following information \n '+result);
		    result='';
		    return false;
	    }
	    return false;
    }

    function openDisplay(name,url,hit,wit) 
    {
	    var hWnd = window.open(url,"OpenWin","width=" + wit + ", height=" + hit +  ",toolbar=no,menubar=no,location=no,resizable=no,scrollbars=yes");
	}
