﻿// JScript File

var txtSubject = "";
var txtEmailAddress = "";
var txtTelephone = "";
var txtAreaDetails = "";

browserName = navigator.appName;
browserVer = parseInt(navigator.appVersion);
ie4up = (browserName.indexOf("Microsoft") >= 0 && browserVer >= 4);

var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()-,(). ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function InitializeClientIDs()
{
  txtSubject = document.aspnetForm['MP1_ContentPlaceHolder1_txtSubject'];
  txtEmailAddress = document.aspnetForm['MP1_ContentPlaceHolder1_txtEmailAddress'];
  txtTelephone = document.aspnetForm['MP1_ContentPlaceHolder1_txtTelephone'];
  txtAreaDetails = document.aspnetForm['MP1_ContentPlaceHolder1_txtAreaDetails'];
}

function validate()
{
  InitializeClientIDs();
  if(IsEmpty(txtSubject,"Select the Subject"))
    return false;
 
  if(invalidchar(txtSubject))
  return false;
  
  if (IsEmpty(txtEmailAddress,"Please enter Email address"))
      return false;
      
  if(invalidchar(txtEmailAddress))
  return false;
     
  if(IsEmail(txtEmailAddress))
        return false;
  if (txtEmailAddress.value.indexOf("'",0)!= -1 )
   {
        alert("\nPlease do not enter a ( ' ) in your userid")
        txtEmailAddress.focus()
        return false;
   } 
  if(txtTelephone.value > "")
   {  
       if(isNaN(txtTelephone.value))
         {
            alert("Enter Telephone Number in Numerics Only"); 
            txtTelephone.focus();
            return false;
         }
       if (checkInternationalPhone(txtTelephone.value)==false)
	    {
		    alert("Please Enter a Valid Telephone Number and 10 digits minimum")
		    txtTelephone.value=""
		    txtTelephone.focus();
		    return false;
	    }
   }
  if(invalidchar(txtTelephone))
     return false;
      
  if(invalidchar(txtAreaDetails))
     return false;
     
  if (IsEmpty(txtAreaDetails,"Please enter the Mail Body"))
      return false;
  if(invalidchar(txtAreaDetails))
     return false;
  
     return true; 
}


//isivalid function to validate injection code

function invalidchar(isinvalidstring)
{
             var tstring =isinvalidstring.value;
    	     var theLength = isinvalidstring.length;
	
	         var temp_Length = tstring.length;
	          
	         for(var i=0;i<temp_Length;i++)
			 {
				var theChar_value = ascii_value(tstring.substring(i,i+1));
				var theChar = tstring.substring(i,i+1);
				if(theChar_value == 38 || theChar_value == 60 || theChar_value == 62 ||theChar_value == 39)
				{
				  
				        if(!ie4up)
				            alert("                                                             Invalid Character \n\nYou have entered one of the invalid characters shown below. Please remove these characters and try again\n                                                                 <   >   '   &\n  ")
				        else
				            alert("                                                                Invalid Character \n\nYou have entered one of the invalid characters shown below. Please remove these characters and try again\n\n                                                                    <   >   '   & ")
				        isinvalidstring.focus();
                        isinvalidstring.select(); 
				        return true;
				  
				 
				}
			 }
               
}


//ascii - value 
function ascii_value (c)
{
	// restrict input to a single character
	c = c . charAt (0);

	// loop through all possible ASCII values
	var i;
	for (i = 0; i < 256; ++ i)
	{
		// convert i into a 2-digit hex string
		var h = i . toString (16);
		
		if (h . length == 1)
			h = "0" + h;

		// insert a % character into the string
		h = "%" + h;

		// determine the character represented by the escape code
		h = unescape (h);

		// if the characters match, we've found the ASCII value
		if (h == c)
			break;
	}
	return i;
}

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 stripCharsInBag(s, bag)
{   
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone)
{
    s=stripCharsInBag(strPhone,validWorldPhoneChars);
    return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}
    