﻿/* This function use for remove all space*/ 

function trimAll(sString)
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

/* This function use for check valid emailid */

function validEmail(email)
{
	var filter  = /^([\w]+(?:\.[\w]+)*)@((?:[\w]+\.)*\w[\w]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (!filter.test(email))
	{
		return email;
	}
}

/* This function use for check fields value non zero */

function nonZero(val)
{
	if(val <= 0)
	{
		return val;
	}
}

/* This function use for check valid specialcharacter */

function specialChar(stringVal)
{
	var iChars = "!@#%^*+=[]\';{}|\":<>?~`";
	for (var i = 0; i < stringVal.length; i++) 
	{
		if (iChars.indexOf(stringVal.charAt(i)) != -1)
		{
			return stringVal;
		}
	}
}

/* This function use for check valid specialcharacter */

function scriptCheck(stringVal)
{
	var iChars = "<>";
	for (var i = 0; i < stringVal.length; i++) 
	{
		if (iChars.indexOf(stringVal.charAt(i)) != -1)
		{
			return stringVal;
		}
	}
}


/* This function use for check valid password */

function validPassword(password)
{
	var iPChars = "!@#$%^&*()+=[]\\\';,./{}|\":<>?~`";
	for (var i = 0; i < password.length; i++) 
	{
		if (iPChars.indexOf(password.charAt(i)) != -1)
		{
			return password;
		}
	}
}

/* This function use for check valid images type */

function validImage(Images)
{
	if(Images.value !="")
	{
		if(!/(\.gif|\.jpeg|\.jpg|\.png)$/i.test(Images.value))
		{
			alert("Please upload image file of gif,jpeg,jpg or png format only.");
			Images.value = '';
			Images.focus();
			return false;
		}
	} 
} 

/* This function use for check valid date format(mm/dd.yyyy) */

function ValidDate(datefield)
{
    //declare valid variable with all valid characters: digits from 0 to 9 and backslash
    var valid = "0123456789/";
    //declare variable for counting number of slashes
    var slashcount = 0;
    //checking date length. If it not equals 10 - display warning and return false
	if (datefield.length!=10)
    {
    	alert("Invalid date! The correct date format is like 'mm/dd/yyyy'. Please try again.")
        return datefield;
    }
    //check each character in the date field, one at a time

	for (var i=0; i < datefield.length; i++)
	{
		temp = "" + datefield.substring(i, i+1);
		//if character is backslash- count it
		if (temp == "/")
		slashcount++;
    	//if character in temp does not exist in valid character string, display warning
		if (valid.indexOf(temp) == "-1")
		{
			alert("Invalid characters in your date. Please try again.")
			return datefield;
		}
    	//if number of slashes not equals 2 display warning
        if (slashcount != 2)
        {
    		alert("Invalid Date! The correct date format is like 'mm/dd/yyyy'. Please try again.")
        	return datefield;
        }
    	//check position of slashes in date string. It should be 2 and 5
    	// Because the first character position is 0 not one
        if((datefield.charAt(2)!= '/')||( datefield.charAt(5) != '/'))
        {
    		alert("Invalid date! The correct date format is like 'mm/dd/yyyy'. Please try again.")
    	    return datefield;
        }
    }
}

/* This function use for Reset Form */

function resetForm(frm)
{
	frm.reset();
	return false;
}

/* This function use for check valid alpha num value */

function AlphaNum(name)
{
 	for(var j=0; j<name.length; j++)
	{
		var alphaa = name.charAt(j);
		var hh = alphaa.charCodeAt(0);
		if((hh > 64 && hh<91) || (hh > 96 && hh<123))
		{
			flag=1;
			break;
		}
		else
		{
			flag=0;
		}
	}
	if(flag == 0)
	{
		return name;
	}
}

/* This function use for check valid UK Zip code/Pin Code */

function isValidZipCode(zipCode)
{
	//alert(zipCode);
	//var postcodeRegEx = /[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}/i;
	var postcodeRegEx = /([A-Z0-9]{3,4})+([\ ]{1})+([A-Z0-9]{3,5})/i;
   	return postcodeRegEx.test(zipCode);
}

/* This function use for compare today date and enteded date  */

function dateCompare(mydate)
{
	var a2 	= new Array();
	a2		= mydate.split(" ");
	
	date2 	= a2[0].split("-");
	var day2 	= date2[0];
	var month2 	= date2[1];
	var year2 	= date2[2];
	var end = year2+""+month2+""+day2;
	
	var todayDate = new Date()
	d = todayDate.getDate();
	m = todayDate.getMonth()+1;
	y = todayDate.getFullYear();
	
	for(var i=0; i <10; i++)
	{		
		if(m == i)
		{
			m = "0"+m;	
		}
		if(d == i)
		{
			d = "0"+d;	
		}
	}
	//year2+""+month2+""+day2;
	var today = y+""+m+""+d;
	//alert(end+" "+today);
	if(end < today)
	{
		return mydate;
	}
}

/* function for set focus */
function setfocus(Frm)
{
	Frm.focus();
	return true;	
}
function getkey(e)
{
	if (window.event)
	return window.event.keyCode;
	else if (e)
	return e.which;
	else
	return null;
}
//
function goodchars(e,goods)
{
	var key, keychar;
	key = getkey(e);
	if (key == null) return false;
	// get character
	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	goods = goods.toLowerCase();
	// check goodkeys
	if (goods.indexOf(keychar) != -1)
	return true;
	// control keys
	if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
	return true;
	// else return false
	return false;
}

