﻿//-------------------------------------------------------------------
// Validate Numeric Value(val, fieldname)
// Returns true if the value is numeric.
//-------------------------------------------------------------------
function isNumeric(val, fieldname)
{
	if(isNaN(val)==true)
	{
		alert("Please Enter a Numeric Value");
		document.all[fieldname].focus();
		document.all[fieldname].value="";
		return false;
	}
	else
	{
		return true;
	}
}

function ShowHideDiv(Divid,ImageId)
{
    var e = document.getElementById(Divid);
    var f = document.getElementById(ImageId);
    
    if(e.style.display == 'none')
    {
    e.style.display = 'block';
    f.src="images/collapse.jpg";
    }
    else
    {
    e.style.display = 'none';
    f.src="images/expand.jpg";
    } 
}

//-------------------------------------------------------------------
// isBlank(value)
//   Returns true if value only contains spaces
//-------------------------------------------------------------------


function isBlank(FieldVal)
{
	if(FieldVal == null) {
		return true;
	}
	for(var i=0; i < FieldVal.length; i++) {
		if((FieldVal.charAt(i) != ' ') && (FieldVal.charAt(i) != "\t") && (FieldVal.charAt(i) != "\r") && (FieldVal.charAt(i) != "\n")) {
			return false;
		}
	}
	return true;
}


        
///----------------------------------------------------------------------
/// Viraj
/// for checking validations like numeric & non numeric etc
function checkVal(value,fieldname,val)
{
switch(val)
{
case '0':
        return true;
        break;
case '1':
        var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
        var checkStr = value;
        var allValid = true;
        for (i = 0;  i < checkStr.length;  i++)
        {
        ch = checkStr.charAt(i);
        for (j = 0;  j < checkOK.length;  j++)
        if (ch == checkOK.charAt(j))
        break;
        if (j == checkOK.length)
        {
        allValid = false;
        break;
        }
        }
        if (!allValid)
        {
        alert("Only space and letters are allowed.");
        document.all[fieldname].value="";
        document.all[fieldname].focus();
        return (false);
        }
        break;
case '2':
        var checkOK = "0123456789";
        var checkStr = value;
        var allValid = true;
        var allNum = "";
        for (i = 0;  i < checkStr.length;  i++)
        {
        ch = checkStr.charAt(i);
        for (j = 0;  j < checkOK.length;  j++)
        if (ch == checkOK.charAt(j))
        break;
        if (j == checkOK.length)
        {
        allValid = false;
        break;
        }
        if (ch != ",")
        allNum += ch;
        }
        if (!allValid)
        {
        document.all[fieldname].value="";
        document.all[fieldname].focus();
        return (false);
        }
        return true;
        break;
        
case '3':
        var checkOK = "!@#$%^&*()_+-=\|;:<,>.?/";
        var checkStr = value;
        var allValid = true;
        for (i = 0;  i < checkStr.length;  i++)
        {
        ch = checkStr.charAt(i);
        for (j = 0;  j < checkOK.length;  j++)
        if (ch == checkOK.charAt(j))
        break;
        if (j == checkOK.length)
        {
        allValid = false;
        break;
        }
        }
        if (!allValid)
        {
        alert("only special characters are allowed.");
        document.all[fieldname].value="";
        return (false);
        }
        break; 

case '4':
        var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ";
        var checkStr = value;
        var allValid = true;
        for (i = 0;  i < checkStr.length;  i++)
        {
        ch = checkStr.charAt(i);
        for (j = 0;  j < checkOK.length;  j++)
        if (ch == checkOK.charAt(j))
        break;
        if (j == checkOK.length)
        {
        allValid = false;
        break;
        }
        }
        if (!allValid)
        {
        alert("Only letters and numeric characters are allowed.");
        document.all[fieldname].value="";
        document.all[fieldname].focus();
        return (false);
        }
        return true;
        break;
        
case '5':
        var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()_+-=\|;:<,>.?/";
        var checkStr = value;
        var allValid = true;
        for (i = 0;  i < checkStr.length;  i++)
        {
        ch = checkStr.charAt(i);
        for (j = 0;  j < checkOK.length;  j++)
        if (ch == checkOK.charAt(j))
        break;
        if (j == checkOK.length)
        {
        allValid = false;
        break;
        }
        }
        if (!allValid)
        {
        alert("Only letters and special characters are allowed.");
        document.all[fieldname].value="";
        document.all[fieldname].focus();
        return (false);
        }
        break;

case '6':
        var checkOK = "0123456789!@#$%^&*()_+-=\|;:<,>.?/";
        var checkStr = value;
        var allValid = true;
        for (i = 0;  i < checkStr.length;  i++)
        {
        ch = checkStr.charAt(i);
        for (j = 0;  j < checkOK.length;  j++)
        if (ch == checkOK.charAt(j))
        break;
        if (j == checkOK.length)
        {
        allValid = false;
        break;
        }
        }
        if (!allValid)
        {
        alert("only numeric and special characters are allowed.");
        document.all[fieldname].value="";
        return (false);
        }
        break; 
case '7':
        var checkOK = "0123456789.";
        var checkStr = value;
        var allValid = true;
        var allNum = "";
        for (i = 0;  i < checkStr.length;  i++)
        {
        ch = checkStr.charAt(i);
        for (j = 0;  j < checkOK.length;  j++)
        if (ch == checkOK.charAt(j))
        break;
        if (j == checkOK.length)
        {
        allValid = false;
        break;
        }
        if (ch != ",")
        allNum += ch;
        }
        if (!allValid)
        {
        document.all[fieldname].value="";
        document.all[fieldname].focus();
        return (false);
        }
        return true;
        break;
        
case '8':
        var checkStr = value;
        if (checkStr == "")
        { 
        document.all[fieldname].value="";
        document.all[fieldname].focus();
        return (false);
        }
        return true;
        break;
        
case '9':
        var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
        var checkStr = value;
        var allValid = true;
        var allNum = "";
        for (i = 0;  i < checkStr.length;  i++)
        {
        ch = checkStr.charAt(i);
        for (j = 0;  j < checkOK.length;  j++)
        if (ch == checkOK.charAt(j))
        break;
        if (j == checkOK.length)
        {
        allValid = false;
        break;
        }
        if (ch != ",")
        allNum += ch;
        }
        if (!allValid)
        {
        alert("Only Use _ as Special Character");
        document.all[fieldname].value="";
        document.all[fieldname].focus();
        return (false);
        }
        return true;
        break; 
               
case '10':
        var checkOK = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()_+-=\|;:<,>.?/ ";
        var checkStr = value;
        var allValid = true;
        var allNum = "";
        for (i = 0;  i < checkStr.length;  i++)
        {
        ch = checkStr.charAt(i);
        for (j = 0;  j < checkOK.length;  j++)
        if (ch == checkOK.charAt(j))
        break;
        if (j == checkOK.length)
        {
        allValid = false;
        break;
        }
        if (ch != ",")
        allNum += ch;
        }
        if (!allValid)
        {
        alert("Please Enter Proper Value");
        document.all[fieldname].value="";
        document.all[fieldname].focus();
        return (false);
        }
        return true;
        break;        
        
case '11':
        var checkOK = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-. ";
        var checkStr = value;
        var allValid = true;
        var allNum = "";
        for (i = 0;  i < checkStr.length;  i++)
        {
        ch = checkStr.charAt(i);
        for (j = 0;  j < checkOK.length;  j++)
        if (ch == checkOK.charAt(j))
        break;
        if (j == checkOK.length)
        {
        allValid = false;
        break;
        }
        if (ch != ",")
        allNum += ch;
        }
        if (!allValid)
        {
        alert("Special characters are not allowed. Please enter proper remarks");
        return (false);
        }
        return true;
        break;

case '12':
        var checkOK = "0123456789-";
        var checkStr = value;
        var allValid = true;
        var allNum = "";
        for (i = 0;  i < checkStr.length;  i++)
        {
        ch = checkStr.charAt(i);
        for (j = 0;  j < checkOK.length;  j++)
        if (ch == checkOK.charAt(j))
        break;
        if (j == checkOK.length)
        {
        allValid = false;
        break;
        }
        if (ch != ",")
        allNum += ch;
        }
        if (!allValid)
        {
        document.all[fieldname].value="";
        document.all[fieldname].focus();
        return (false);
        }
        return true;
        break;
case '13':
        //For DropDownlist
        var checkStr = value;
        if (checkStr == "0")
        { 
        document.all[fieldname].selectedIndex=0;
        document.all[fieldname].focus();
        return (false);
        }
        return true;
        break;
default : alert("oh u have all rights");
     }
}  

//--------------------------
//For Checking Date Format

function isDate(dateStr) 
{
var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
var matchArray = dateStr.match(datePat); // is the format ok?

if (matchArray == null) {
alert("Please enter date as either dd/mm/yyyy or dd-mm-yyyy.");
return false;
}

month = matchArray[1]; // p@rse date into variables
day = matchArray[3];
year = matchArray[5];

//month = matchArray[3]; // p@rse date into variables
//day = matchArray[1];
//year = matchArray[5];


if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}

if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}

if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn`t have 31 days!")
return false;
}

if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap)) {
alert("February " + year + " doesn`t have " + day + " days!");
return false;
}
}
return true; // date is valid
}


// Email Address Validation

function validemail(fieldname,fieldvalue)
{
    var emailID	 = fieldname;
    var email = fieldvalue;

	    if (echeck(email)==false)
	        {
		        alert("Invalid E-mail ID");
		        document.all[fieldname].value="";
                document.all[fieldname].focus();
		        return false;
	        }
	        return true
 }

function echeck(str)
{

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
    		
    	if (str.indexOf(at)==-1)
    		{
		       return false;
		    }

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		    {
		       return false;
		    }

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		    {
		        return false;
		    }

		 if (str.indexOf(at,(lat+1))!=-1)
		    {
		        return false;
		    }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		    {
		        return false;
		    }

		 if (str.indexOf(dot,(lat+2))==-1)
		    {
		        return false;
		    }
		
		 if (str.indexOf(" ")!=-1)
		    {
		        return false;
		    }

 		 return true;					
	}
function ValURL(obj) {
     var theurl=obj.value;
     theurl=String(theurl);

     if(theurl == "")
     {
        return true;
     }
     
     var tomatch= /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/
     if (tomatch.test(theurl))
     {
         return true;
     }
     else
     {
         window.alert("Please Enter Valid URL.");
         obj.value="";
         return false; 
     }
}
//below method is implemened by trusha on 15th december2009
function ValidURL(obj) {

     var theurl=obj.value;
     theurl=String(theurl);

     if(theurl == "")
     {
        return true;
     }
   
     var tomatch=  /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/
     if (tomatch.test(theurl))
     {
         return true;
     }
     else
     {
         window.alert("Please Enter Valid URL.");
         obj.value="";
         return false; 
     }
}

function ValLength(obj) {
    alert(obj.value);
     var theurl=obj.value;
     theurl=String(theurl);

     if(theurl.length == "")
     {
        return true;
     }
  
     if (theurl.length >= 6)
     {
         return true;
     }
     else
     {
         window.alert("Password Length (6 - 15)");
         obj.value="";
         return false; 
     }
}