﻿// JScript File
    var result="";
    var i=0;
    
    //**************Clearing Spaces******************
    
    function strip_lspaces(element){
	if (element != ""){
		 while('' + element.charAt(0) == ' '){
		   element = element.substring(1,element.length);
		}
	 }
	 return element;
    }
    
//    //**************Remaining Chars******************
    
     function remain_chars(control,display,val)
    {
        var s = control.value;
        if (s.length > val)
        {
            control.value= s.substring(0,val);
        }
        display.value = val - control.value.length;
    }
    
     
    //************ Validates website URL **********************
    function validateWebsite(control,name)
    {
        if(control.length == 0)
        {
            result = result + '\n'+ getI() + 'Enter ' + name ;
        }   
       
      //else if (!/^https?:\/\/([\w-]+\.?)+[\w-]+(\/[\w- ./?%&=]*)?$/.test(control))      
      
     else if (control.substring(0,11) == "http://www.")
      {
        if (!/^https?:\/\/([\w-]+\.?)+[\w-]+(\/[\w- ./?%&=]*)?$/.test(control))
        {
            result = result + '\n'+ getI() + 'Invalid ' + name ;
        }
      }
      else if (control.substring(0,4) == "www.")
      {
         if (!/^([\w-]+\.?)+[\w-]+(\/[\w- ./?%&=]*)?$/.test(control))       
           {
             result = result + '\n'+ getI() + 'Invalid ' + name ;
           }
      }
      else
      {
             result = result + '\n'+ getI() + 'Invalid ' + 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() + 'Enter ' + name ;
	    else if (isNaN(val) || val.indexOf('.')!=-1)
	        result = result + '\n'+ getI() + 'Invalid ' + name;
	}
	
	function initCap(val,name)
	{
	    if(val.length==0)
	        result = result + '\n'+ getI() + 'Enter ' + name ;
//	    else if (isNaN(val) || val.indexOf('.')!=-1)
//	        result = result + '\n'+ getI() + 'Invalid ' + name;
	    else {
	        var ch = val.substring(0,1);
	        var rest = val.substring(1);
	        rest = rest.toLowerCase();
	        
	        var up=ch.toUpperCase();
	        
	        var UpperVal = up + rest;
	        
	        return UpperVal;
	        //alert(UpperVal); return false;        
	      }  
	    }
	
	 //************ An floating validation *************
    function isempty_floatnumber(val,name)
	{
	    if(val.length==0)
	        result = result + '\n'+ getI() + 'Enter ' + name ;
	    else{
	     if (val == 0)
	        result = result + '\n'+ getI() + 'Invalid ' + name;
	    }
	}
	
	 //************ An floating validation & accept null*************
    function isempty_floatnumbernull(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 and date*************
    function isempty_floatnumbernulldate(val,name)
	{
	    if (isNaN(val))
	        result = result + '\n'+ getI() + 'Invalid ' + name;	    
	    else {   
	            if (val <= 1909)
	            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 ;
	    }
    }
 
    function acceptAlphaNumeric(controlVal, name)
    {       
        if (controlVal.length==0)
	    {
		    result = result + '\n'+ getI() + 'Enter ' + name ;
	    }        
        else if (!/^[a-zA-Z0-9 ]+$/. test(controlVal))
        {
            result = result + '\n'+ getI()+ 'Invalid ' + name + ' Accept only AlphaNumeric';
        }
    }
    
         function acceptAlphaNumericHypen(controlVal, name)
    {       
        if (controlVal.length==0)
	    {
		    result = result + '\n'+ getI() + 'Enter ' + name ;
	    }        
        else if (!/^[a-zA-Z0-9- ]+$/. test(controlVal))
        {
            result = result + '\n'+ getI()+ 'Invalid ' + name + ' Accept only AlphaNumeric & -';
        }
    }
    
     function acceptAlphabets(controlVal, name)
    {      
     if (controlVal.length==0)
	    {
		    result = result + '\n'+ getI() + 'Enter ' + name ;
	    }  
       else if (controlVal.length!=0)
	    {
		  if (!/^[a-zA-Z ]+$/. test(controlVal))
            {
                result = result + '\n'+ getI() + 'Invalid ' + name + ' Accept only [A-Z a-z]';
            }   
         }        
    }
    
     function acceptAddress(controlVal, name)
    {  
     if (controlVal.length==0)
	    {
		    result = result + '\n'+ getI() + 'Enter ' + name ;
	    }   
        else if (controlVal.length != 0)
        {
            if (!/^[a-zA-Z0-9,#, ,.,-,,,]+$/. test(controlVal))
                {
                    result = result + '\n'+ getI() + 'Invalid Address';
                }
        }   
    }
    
     //**************FileUpload Extension Checking******************
    function FileUpCheck(filename,name)    
     {     
        ext	=	filename.substr(filename.lastIndexOf(".")).toLowerCase();
			
		   		if (ext!=".xls" && ext!=".gif" && ext!=".jpeg" && ext!=".bmp" )
		   		{
		   		     result = result + '\n'+ getI() +  name + 'has Invalid Format!' ;		   		    
				}
	 }
			

 //*********** 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)
	{
	    if (zip.length ==0)
	    {
	        	    result=result + "\n" + getI() + "Enter Zip";
	    }
		else if(zip.length!=0)
		{	
		     if((zip.length < 5 || zip.length >6))
			    result=result + "\n" + getI() +"Invalid Zip";
		    else if(isNaN(zip)) 
			    result=result + "\n" + getI() + "Invalid Zip";
		}
	}
			
	//************* Validates Phone number (US format) *****************
    function validate_phone(phone, name)
	{
	    var formate = "use xxx-xxx-xxxx format";
	    
		if (phone.length!=0)
			var sep = getseparator(phone);
		if(phone.length==0)
			result=result + "\n" + getI() +"Enter " + name + " ! " + format;
		else if((phone.length!=0) && (phone.length < 12 || phone.length >12))
			result=result + "\n" + getI() +"Invalid " + name + " ! " + format;
		else if((isNaN(phone.substring(0,3))) || (isNaN(phone.substring(4,7))) || (isNaN(phone.substring(8,12)))) 
			result=result + "\n" + getI() + "Invalid " + name + " ! \n " + format;
		else if ((sep !="") && (phone.length!=0)) {
			if((phone.indexOf(sep)!=3) || (phone.lastIndexOf(sep)!=7))
				result=result + "\n" + getI() + "Invalid " + name + " ! " + format; 	
		}
		else if(sep =='') 
			result=result + "\n" + getI() + "Invalid " + name + " ! " + format; 
	}
	
	//***************** returns separator in phone column **************
	function getseparator(phone_)
	{
		if(phone_.indexOf('-')!=-1)
			return '-';
		else
			return '';
	}
	
	/********************************  Validate Phone number when using 3 text boxes  ****************************************/
	function validatePhone(phone1,phone2,phone3)
	{
	    if (isNaN(phone1) || isNaN(phone2) || isNaN(phone3)) 
	       result=result + '\n'+ getI() + 'Invalid Emergency Contact Phone Number';
	}
	function validatePhone1(phone1,phone2,phone3)
	{
	    if (isNaN(phone1) || isNaN(phone2) || isNaN(phone3)) 
	       result=result + '\n'+ getI() + 'Invalid Home Phone Number';
	}
    
    //****************** 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 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=edate;
		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(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 datecheck(field){
				var f_date = strip_lspaces(field.value);
				date_split = f_date.split("/") ;
				
				if (date_split.length < 3){
					field.select();
					result = result + '\n' + getI() + " Enter correct date. Use mm/dd/yyyyy format.";
				} 
				
				if (date_split[0].length < 0 || date_split[1].length < 0 || date_split[2] < 0){
					result = result +  '\n' + getI() +" Enter correct date. Use mm/dd/yyyyy format.";
				}
				
				if (date_split[0] < 1 || date_split[0] > 12) {
					result = result + '\n' + getI() + "Month value should be between 1 and 12.";
				}
				
				if (date_split[0] == 1 || date_split[0] == 3 || date_split[0] == 5 || date_split[0] == 7 || date_split[0] == 8 || date_split[0] == 10 || date_split[0] == 12){
					if (date_split[1] < 1 || date_split[1] > 31){
						result = result + '\n' + getI() + "Date value must be between 1 and 31";
					}
				}
				
				if (date_split[0] == 4 || date_split[0] == 6 || date_split[0] == 9 || date_split[0] == 11) { 
					if (date_split[1] < 1  || date_split[1] > 30){
						result = result + '\n' + getI() + "Date value must be between 1 and 30";
					}
				}
				
				if (date_split[2].length != 4) {
					result = result + '\n' + getI() + "Please specify a 4 year digit for year";
				}
				
				checkLeap = date_split[2] % 4 ;
				if (date_split[0] == 2 && checkLeap == 0){
					if (date_split[1] < 1 || date_split[1] > 29){
						result = result + '\n' + getI() + "Date for the month of February in a leap year must be between 1 and 29";
					}
				}
				
				if (date_split[0] == 2 && checkLeap != 0){
					if (date_split[1] < 1 || date_split[1] > 28){
						result = result + '\n' + getI() + "Date for the month of February must be between 1 and 28";
					}
				}
				
				return result;
			}
			
    
        function validate(field, type) {
		        		
			var  num = "0123456789" ;
			var valid = "";
			var rs = "" ;
			var c;
			if (type == "integer") { valid= valid + num}
			if (type == "name"){ valid = valid  + (rs = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ")}
			if (type == "money"){ valid = valid + num + (rs = ".")}
			if (type == "phone"){ valid = valid + num + (rs = "- ")}
			if (type == "mobile"){ valid = valid + num + (rs = ") (-");}
			if (type == "date"){ valid = valid + num + (rs = "/"); }
			if (type == "zip"){ valid = valid + num + (rs = "() "); }
			if (type  == "password") {valid = valid + num +(rs= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()`~");}
			if (type == "userid"){ valid = valid  + (rs = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");}
			if (type == "subject"){ valid = valid  + (rs = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ");}
			if (type == "alphanumeric"){ valid = valid  + (rs = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ()-.");}
			if (type == "vat"){ valid = valid  + (rs = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 -");}
			if (type == "statename"){ valid = valid  + (rs = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 -&");}
			if (type == "weight"){ valid = valid + num + (rs = " -");}
			if (rs != ""){rs = "and ' " + rs + " '"} 
			var msg = "and "+ valid
			var ok = "yes";
			var temp;
			if (type == "email"){ 
				if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(field.value)){
				    return " Invalid Email Address! please re-enter ";
				}else{
					return "Valid!!";
				}
			}
			
						
				
			for (var i=0; i<field.value.length; i++) {
				temp = "" + field.value.substring(i, i+1);
				if (valid.indexOf(temp) == "-1") ok = "no";
			}
			    if (ok == "no")
			    {
			    if(type=="phone" || type=="zip" || type=="mobile")
			    {
			    return " Invalid entry! Only  Numbers are accepted ";
			    }
			    else if(type== "password")
			    {
			    return " Invalid entry! Only Numbers, Characters and '!@#$%^&*()' are accepted ";
			    }
			    else if(type=="userid")
			    {
			    return " Invalid entry! Only Numbers, Characters and _ are accepted ";
			    }
			    else if(type== "subject")
			    {
			    return " Invalid entry! Only Numbers, Characters are accepted ";
			    }
			    else if(type == "vat")
			    {
			    return " Invalid entry! Only Numbers Characters and -  are accepted ";
			    }
			    else if(type == "alphanumeric")
			    {
			    return " Invalid entry! Only Numbers, Characters,(,),.  are accepted ";
			    }
			    else if(type == "integer")
			    {
			    return "Invalid entry! Only Numbers are accepted";
			    }
			    else if(type == "money")
			    {
			    return "Invalid Pgo Price! Please Use 123/123.00 Pricing Format ";
			    }
			    else if(type == "weight")
			    {
			    return "Invalid Weight! Please Use 125/ 125 - 250 Format ";
			    }
			    else
			    {
					return " Invalid entry! Only  Characters are accepted ";
					}
				}
				return "Valid!!";
		}
		
		function compareValues(field1, field2, mess)
		{
		   if (field2 == "")
		   {    
		        result = result + '\n'+ getI() + 'Enter the' + mess ;
		   }	
		   else if (field1 != field2)
           {   
                result = result + '\n'+ getI() + 'Invalid ' + mess ;                    
           }
		}
    
    function displayErrors()
    {
        if (result.length==0 || result=='')
        {
		    return true;
		}
	    else
	    {
		    alert('Please Check The Following Information: \n '+ result);
		    result='';
		    return false;
	    }
	    return false;
}

 //**************FileUpload Extension Checking******************
    function FileUpCheck(filename,name)    
     {     
        ext	=	filename.substr(filename.lastIndexOf(".")).toLowerCase();
		if (ext!=".jpg" && ext!=".gif" && ext!=".jpeg" && ext!=".bmp" && ext!=".png" )
   		{
   		     result = result + '\n'+ getI() +  name  + ' has Invalid Format! \n Upload image with .gif or .jpg or .png or .bmp or .jpeg extension only ' ;		   		    
		}
	 }
	 
	 function DataCompare(fromDate, toDate, name)
	 {
	    if (Date.parse(fromDate) < Date.parse(toDate)) {
	    result = result + '\n'+ getI() + 'Invalid ' + name;  
        //alert("Invalid Date Range!\nStart Date cannot be after End Date!")
        return false;
        }
	 }
	 
    function RadioCheck(control1, control2)
    {  
        if (control1 == false && control2 ==false) 
        {
            result = result + '\n'+ getI() + 'Select Gender';
        }
    }
    
    function isempty2(control1, control2, control3, control4, control5, control6, control7, msg)
    {
	    if(control1 ==''  && control2 ==''  && control3 ==''  && 
	       control4 =='' && control5 =='' && control6 =='' && control7 =='')
        {
            result = result + '\n'+ getI() + msg;
        }
    }
    
    function Agree(control, msg)
    {  
        if (control == false) 
        {
            result = result + '\n'+ getI() + msg;
        }
    }
    
    //Function to get the Radio Value.
	function GetSelectValue(name) {
        
		var a = "";
		var f = document.forms[0];
		var e = f.elements[name];

		if(e.length != null)
		{	          
			for (var i=0; i < e.length; i++) 
			{
				if (e[i].checked) 
				{ 
					a = a + e[i].value ;
				}
			}
			return a;
		}
		else 
		{
			if (eval("document.forms[0]."+name+".checked")) 
			{
				if (eval("document.forms[0]."+name+".value") != undefined) 
				{
					a= eval("document.forms[0]."+name+".value")		            
				}
				else 
				{
					a="";
				}
			}
			return a;
		}
	}
	
	 function RadioChecking(control1, control2)
    {  
        if (control1 == false && control2 ==false) 
        {
            result = result + '\n'+ getI() + 'Select I would like to pay by check or credit card';
        }
    }
    
     function RadioChecking5(control1, control2, control3, control4,control5)
    {  
        if (control1 == false && control2 ==false &&  control3 == false && control4 ==false && control5 ==false) 
        {
            result = result + '\n'+ getI() + 'Select Shirt Size ';
        }
    }
    
      function RadioChecking4(control1, control2, control3, control4)
    {  
        if (control1 == false && control2 ==false &&  control3 == false && control4 ==false) 
        {
            result = result + '\n'+ getI() + 'Select Shirt Size ';
        }
    }
    
    
     function RadioChecking2(control1, control2)
    {  
        if (control1 == false && control2 ==false) 
        {
            result = result + '\n'+ getI() + 'Select Select One  ';
        }
    }