﻿//Checks malicious input in form
function DenyMalInput()
{
	// If clientsidevalidation has been marked off, then do not validate
	// if (typeof DataValidate != "undefined")
	// if (!DataValidate) return true;     
  var length = document.forms[0].elements.length;
  
  for (var i = 0; i < document.forms[0].elements.length; i++) 
    {
		    var el = document.forms[0].elements[i];
    		if(el.tagName.toLowerCase()!="select")
    		 { 
        //		if (el.tagName.toLowerCase()!="textarea" &&		el.tagName.toLowerCase()!="select" &&
        //			!(el.tagName.toLowerCase()=="input" &&	el.type.toLowerCase()=="file" )&&
        //			el.value != ReplaceMalstring(el.value))
               if(el.id.indexOf('_')!=-1)
               {
                   var elementId=el.id;          
                   elementId=el.id.substring(el.id.lastIndexOf('_')+1);
                   elementId=Trim(elementId);
                  //allow & symbol if control is to enter company name 
                   if(elementId.indexOf("companyName")!=-1||elementId.indexOf("competitorsName")!=-1)
                   {
                            if (el.value != ReplaceMalstring(el.value,true))
		                    {
			                    var alertString = "A potentially malicious input string is received from " +
			                    "the form as part of input data.\nCheck your data for inputs such as '<,>,&#'.";
			                    alert (alertString);
			                    document.all(el.id).focus();
			                    return false;
		                    }
                   }
                   else
                   {
                          if (el.value != ReplaceMalstring(el.value,false))
		                    {		                     
			                    var alertString = "A potentially malicious input string is received from " +
			                    "the form as part of input data.\nCheck your data for inputs such as '<,>,&#'.";
			                    alert (alertString);
			                    document.all(el.id).focus();
			                    return false;
		                    }
                   } 
               } 
               else
               {
                    if (el.value != ReplaceMalstring(el.value,false))
		            {
			            var alertString = "A potentially malicious input string is received from " +
			            "the form as part of input data.\nCheck your data for inputs such as '<,>,&#'.";
			            alert (alertString);
			            document.all(el.id).focus(); 
			            return false;
		            }
		        }
		 }
   }
   return true;
}


function ReplaceMalstring(string,companyNameExists) 
{
  try
  {
    string = string.replace(/\</g,"");
    string = string.replace(/\>/g,"");    
//    string = string.replace(/\#/g,"");
    string = string.replace(/\&#/g, "");
	}
	catch(err)
	{}
	return (string);
}

//Does LTrim and RTrim to a string
function Trim(string)
{
	while (string.substring(0,1) == ' ')
	{
		string = string.substring(1, string.length);
	}
	
	while (string.substring(string.length-1, string.length) == ' ')
	{
		string = string.substring(0,string.length-1);
	}
	return string;
}

//Checks whether the given string is integer
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 IsNumeric(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")))
        {
            if(!(c=="." && i!= s.length-1))
                return false;
        }
    }
    // All characters are numbers.
    return true;
}


function IsDate(args)
{
    var InpDate= document.getElementById(args).value;

    if(InpDate == "")
    {
    //    alert("Enter the Date field");
        return false;
    }
   else
   {
      if(InpDate.length < 8) 
        {
//         alert("Enter Correct Date")
         return false;
        }
     else
        {
             InpVal = InpDate;
            SendNext = "False";
            PrevSlash = "";
            LastSlash = "";
            for(i = 0;i <= InpDate.length-1;i++)
            {
                PrevSlash = LastSlash;
                LastSlash = InpDate.substring(i,i+1);
                if((LastSlash == '/' && i == 0 ))
                {
//                alert("Your Date Format is incorrect");
                    return false;
                    break;    
                }
                if ((LastSlash == '/' || LastSlash == '.') && (PrevSlash == '.' || PrevSlash == '/'))
                {
//                alert("Your Date Format is incorrect");
                    return false;
                    break;
                }
                else 
                {
                     if (LastSlash == '/' || LastSlash == '.') 
                        SendNext = "True";
                } 
            } 
            if (SendNext == "True") 
            {
                lBool="False";
                LMonth="False";
                LDate="False";
                var OutVal;
                var InVal;
                var RoundYear;
                var Mon;
                var LastSlashNumber;
                var j;
                var k;
                var x;
                x = InpDate;
                OutVal="";
                TotVal="";
                LastSlashNumber=0;
                for(i = 0;i <= x.length-1;i++) 
                {
                     LastSlash = x.substring(i,i+1);
                    if (LastSlash != 0 && LastSlash != 1 && LastSlash != 2 && LastSlash != 3 && LastSlash != 4 && LastSlash != 5 && LastSlash != 6 && LastSlash != 7 && LastSlash != 8 && LastSlash != 9 && LastSlash != '/' && LastSlash != '.') //fifth if
                    {
                        lBool="True";
                        break;
                    }
                  else 
                    {
                      if (LastSlash == '/' || LastSlash == '.' || LastSlashNumber == 2 ) //sixth if
                          {
                            InVal = OutVal;
                            if (InVal == '0' || InVal == '00' || InVal == '0000') //seventh if
                                {
//                                    alert("You entered Some Zero's in the field(Month/Date/Year)")
                                    return false;
                                    break;
                                }
                                TotVal = TotVal+InVal
                                OutVal="";
                                   if (LastSlashNumber == 0  )
                                    {
                                        LastSlashNumber  = LastSlashNumber + 1;
                                        Mon = InVal;
                                        if (InVal > 12)
                                            {
                                                LMonth ="True";
                                                InVal="";
                                                break;
                                            }
                                    }
                               else
                                    {
                                           if(LastSlashNumber == 1)
                                            {
                                                Dat = InVal;
                                                LastSlashNumber  = LastSlashNumber + 1;
                                                if(Mon == '01' || Mon == '1' || Mon == '03' || Mon == '3' || Mon == '05' || Mon == '5' || Mon == '07' || Mon == '7' || Mon == '08' || Mon == '8' || Mon == '10' || Mon == '12')
                                                    {
                                                          if (InVal > 31)
                                                            {
                                                                  LDate ="True";
                                                                   InVal="";
                                                                  break;
                                                            }
                                                    }
                                                else
                                                       {
                                                        if(Mon == '04' || Mon == '4' || Mon == '06' || Mon == '6' || Mon == '09' || Mon == '9' || Mon == '11')
                                                           {
                                                                if (InVal > 30)
                                                                    {
                                                                          LDate ="True";
                                                                           InVal="";
                                                                          break;
                                                                    }
                                                            }        
                                                          }
                                            }
                                    else
                                        {
                                            if(LastSlashNumber == 2 )
                                                   {
                                                    LastSlashNumber = LastSlashNumber + 1;
                                                    lYear = x.substring(TotVal.length+2,x.length);
                                                    if(lYear == '0000')
                                                        {
//                                                            alert("You entered Some Zero's in the field(Month/Date/Year)")
                                                            return false;                        
                                                        }
                                                    else
                                                        {
                                                            if(lYear.length <= 3 )
                                                                {
//                                                                    alert("Enter Four digits for Year");
                                                                    return false;
                                                                    break;
                                                                }
                                                            else
                                                                {
                                                                       if(lYear.length > 4 )
                                                                        {
//                                                                            alert("Enter Four digits for Year");
                                                                            return false;
                                                                            break;
                                                                        }
                                                                       else
                                                                        {
                                                                             RoundYear = Math.round(lYear/4); 
                                                                             if (lYear/4 != RoundYear) 
                                                                                {
                                                                                    if(Mon == 2 && Dat > 28 )
                                                                                        {
//                                                                                            alert("You entered More than 28 in the date field (it's not a leap year)");
                                                                                            return false;
                                                                                            break;
                                                                                        }
                                                                                    else
                                                                                    {
//                                                                                    alert("Entered Date is Correct");
                                                                                    return true;
                                                                                    }
                                                                                   }
                                                                            else
                                                                                   {
                                                                                    if(Mon == 2 && Dat > 29)
                                                                                        {
//                                                                                            alert("You entered More than 28 in the date field/month is febraury");
                                                                                            return false;
                                                                                            break;
                                                                                        }
                                                                                    else
                                                                                        {
//                                                                                        alert("Entered Date is Correct");
                                                                                        return true;
                                                                                        }
                                                                                   }
                                                                        }
                                                                }
                                                           }
                                                }    
                                                else
                                                {
//                                                alert("Entered Date is Correct");
                                                return true;
                                                }
                                            }
                                       }
                                }
                                       else
                                        OutVal=OutVal + LastSlash;
                            }
                        }
                    }
            else
                {
//                    alert("You Date format is incorrect");
                    return false;
                }
             }
    }
    if (lBool == "True")
        {
//            alert("You entered some alpha value in the Date field");
            return false;
        }
    else
        {
            if (LMonth == "True")
                {
//                    alert("Your Month is invalid");
                    return false;
                }
            else
                {
                    if (LDate == "True")
                        {
//                            alert("Your Date is invalid");
                            return false;
                        }
                    else
                        {
//                        alert("Entered Date is Correct");
                        return true;
                        }
                }
        }
}

